From ff7b10e5af5732389cac5479ea351c6e551fe24d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 21 Apr 2021 16:33:55 -0400 Subject: [PATCH] WORK IN PROGRESS --- src/static/js/ace2_inner.js | 19 ++++++++- src/static/js/pad.js | 3 -- src/static/js/pad_editor.js | 53 ++++-------------------- src/static/js/scroll.js | 4 ++ src/tests/frontend/specs/inner_height.js | 9 +--- 5 files changed, 32 insertions(+), 56 deletions(-) diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index 03dd925bb..1de43c9a2 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -56,8 +56,25 @@ function Ace2Inner(editorInfo, cssManagers) { let disposed = false; - const focus = () => { + const focus = (line = null) => { window.focus(); + if (line == null) return; + const lines = $(document.body).children(); + if (lines.length === 0) return; + if (line > lines.length - 1) line = lines.length - 1; + window.console.log(`scrolling to line ${line}`); + const node = lines[line]; + window.console.log('node.offsetTop', node.offsetTop); + window.console.log('node.getBoundingClientRect().top', node.getBoundingClientRect().top); + window.console.log(rep); + //const scrollAmount = node.getBoundingClientRect().top + iframe + outerWin.document.documentElement.scrollTop = node.getBoundingClientRect().top; + window.console.log('document.body', + 'scrollTop', document.body.scrollTop, + 'clientHeight', document.body.clientHeight, + 'scrollHeight', document.body.scrollHeight); + const point = {index: 0, focusAtStart: true, maxIndex: 1, node}; + setSelection({startPoint: point, endPoint: point}); }; const outerWin = window.parent; diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 6a8e95a89..11898be26 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -491,9 +491,6 @@ const pad = { $('#chatloadmessagesbutton').css('display', 'none'); } - setTimeout(() => { - padeditor.ace.focus(); - }, 0); // if we have a cookie for always showing chat then show it if (padcookie.getPref('chatAlwaysVisible')) { chat.stickToScreen(true); // stick it to the screen diff --git a/src/static/js/pad_editor.js b/src/static/js/pad_editor.js index 1c7a4b33c..f58f327bc 100644 --- a/src/static/js/pad_editor.js +++ b/src/static/js/pad_editor.js @@ -46,9 +46,16 @@ const padeditor = (() => { $outerdoc.find('#sidedivinner').on('click', 'div', function () { const targetLineNumber = $(this).index() + 1; window.location.hash = `L${targetLineNumber}`; - focusOnLine(self.ace); }); - focusOnLine(self.ace); + const focusOnHash = () => { + const lstr = window.location.hash.substr(1); + if (!lstr || lstr[0] !== 'L') return; + const line = parseInt(lstr.substr(1)); + if (!line) return; + self.ace.focus(line - 1); + }; + $(window).on('hashchange', focusOnHash); + setTimeout(focusOnHash, 500); self.ace.setProperty('wraps', true); self.initViewOptions(); self.setViewOptions(initialViewOptions); @@ -162,45 +169,3 @@ const padeditor = (() => { })(); exports.padeditor = padeditor; - -const 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 || lineNumber[0] !== 'L') return; - const $outerdoc = $('iframe[name="ace_outer"]').contents().find('#outerdocbody'); - const lineNumberInt = parseInt(lineNumber.substr(1)); - if (!lineNumberInt) return; - const $inner = $('iframe[name="ace_outer"]').contents().find('iframe') - .contents().find('#innerdocbody'); - const line = $inner.find(`div:nth-child(${lineNumberInt})`); - if (line.length === 0) return; - 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); - }); - // End of setSelection / set Y position of editor -}; diff --git a/src/static/js/scroll.js b/src/static/js/scroll.js index 86d6a3344..c296b65f5 100644 --- a/src/static/js/scroll.js +++ b/src/static/js/scroll.js @@ -19,6 +19,10 @@ function Scroll(outerWin) { this.rootDocument = parent.parent.document; } +Scroll.prototype.scrollToLine = function (line) { + +}; + Scroll.prototype.scrollWhenCaretIsInTheLastLineOfViewportWhenNecessary = function (rep, isScrollableEvent, innerHeight) { // are we placing the caret on the line at the bottom of viewport? diff --git a/src/tests/frontend/specs/inner_height.js b/src/tests/frontend/specs/inner_height.js index d1a6b118b..3968df680 100644 --- a/src/tests/frontend/specs/inner_height.js +++ b/src/tests/frontend/specs/inner_height.js @@ -16,14 +16,7 @@ describe('height regression after ace.js refactoring', function () { it('client height should be less than scrollHeight with many lines', async function () { await helper.clearPad(); - await helper.edit('Test line\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' + - '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'); + await helper.edit(`Test line${'\n'.repeat(281)}`); const outerHtml = helper.padChrome$('iframe')[0].contentDocument.documentElement; // Need to poll because the heights take some time to settle. await helper.waitForPromise(() => outerHtml.clientHeight < outerHtml.scrollHeight);