From b83589fc73a20826f618eb62476ece159f2941d8 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sun, 13 Dec 2020 19:05:00 +0100 Subject: [PATCH] use the helper from scroll.js --- src/static/js/ace2_inner.js | 2 +- src/static/js/pad_editor.js | 65 ++++++++++++------------------------- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index 99fd9795e..a5bf4207c 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -682,7 +682,7 @@ function Ace2Inner() { editorInfo.ace_doReturnKey = doReturnKey; editorInfo.ace_isBlockElement = isBlockElement; editorInfo.ace_getLineListType = getLineListType; - editorInfo.ace_setSelection = setSelection; + editorInfo.ace_scroll = scroll; editorInfo.ace_callWithAce = function (fn, callStack, normalize) { let wrapper = function () { diff --git a/src/static/js/pad_editor.js b/src/static/js/pad_editor.js index db5ee3bef..bba82a7f6 100644 --- a/src/static/js/pad_editor.js +++ b/src/static/js/pad_editor.js @@ -51,10 +51,9 @@ const padeditor = (function () { window.location.hash = `L${targetLineNumber}`; }); - exports.focusOnLine(self.ace); + setTimeout(() => exports.focusOnLine(self.ace), 2000); } } - self.ace = new Ace2Editor(); self.ace.init('editorcontainer', '', aceReady); self.ace.setProperty('wraps', true); @@ -175,47 +174,25 @@ exports.padeditor = padeditor; exports.focusOnLine = (ace) => { // If a number is in the URI IE #L124 go to that line number - const lineNumber = window.location.hash.substr(1); - if (lineNumber) { - if (lineNumber[0] === 'L') { - const $outerdoc = $('iframe[name="ace_outer"]').contents().find('#outerdocbody'); - const lineNumberInt = parseInt(lineNumber.replace('L', '')); - if (lineNumberInt) { - const $inner = $('iframe[name="ace_outer"]').contents().find('iframe') - .contents().find('#innerdocbody'); - const line = $inner.find(`div:nth-child(${lineNumberInt})`); - if (line.length !== 0) { - let offsetTop = line.offset().top; - offsetTop += parseInt($outerdoc.css('padding-top').replace('px', '')); - const hasMobileLayout = $('body').hasClass('mobile-layout'); - if (!hasMobileLayout) { - offsetTop += parseInt($inner.css('padding-top').replace('px', '')); - } - const $outerdocHTML = $('iframe[name="ace_outer"]').contents() - .find('#outerdocbody').parent(); - $outerdoc.css({top: `${offsetTop}px`}); // Chrome - $outerdocHTML.animate({scrollTop: offsetTop}); // needed for FF - const node = line[0]; - ace.callWithAce((ace) => { - const selection = { - startPoint: { - index: 0, - focusAtStart: true, - maxIndex: 1, - node, - }, - endPoint: { - index: 0, - focusAtStart: true, - maxIndex: 1, - node, - }, - }; - ace.ace_setSelection(selection); - }); - } - } - } + let lineNumber = window.location.hash.substr(1); + if (lineNumber && lineNumber[0] === 'L' && (lineNumber = parseInt(lineNumber.substr(1)))) { + ace.callWithAce((ace) => { + const rep = ace.ace_getRep(); + if (lineNumber <= 0 || lineNumber - 1 > rep.lines.length()) return; + // we assume that rep.lines.atIndex is successful now + + const lineNode = rep.lines.atIndex(lineNumber - 1).lineNode; + // offset of the first line and editor space + // const editorTop = ace.ace_scroll._getEditorPositionTop(); + // const firstLineOffset = rep.lines.atIndex(0).lineNode.offsetTop; + // ace.ace_scroll.setScrollY(lineNode.offsetTop + editorTop + firstLineOffset); + ace.ace_scroll.setScrollY(lineNode.offsetTop); + + // place the caret on the beginning of the new line + // rep.lines.atIndex(lineNumber-1).lineMarker) + rep.selEnd = [lineNumber - 1, 0]; + rep.selStart = [lineNumber - 1, 0]; + ace.ace_updateBrowserSelectionFromRep(); + }); } - // End of setSelection / set Y position of editor };