From b67079d0ed44913ae1357f98aa5849ccbbfd1349 Mon Sep 17 00:00:00 2001 From: Max Wu Date: Mon, 1 Jun 2020 23:01:17 +0800 Subject: [PATCH] Update to make editor have extra scroll padding in the bottom that can scroll over editor lines and leave only one line on top of the editor Signed-off-by: Max Wu --- public/js/index.js | 28 +++++++++++++++++++++++----- public/js/lib/editor/ui-elements.js | 3 +++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/public/js/index.js b/public/js/index.js index b1a0a479..5b2d8861 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -588,6 +588,7 @@ function checkEditorStyle () { } // workaround editor will have wrong doc height when editor height changed editor.setSize(null, ui.area.edit.height()) + checkEditorScrollOverLines() // make editor resizable if (!ui.area.resize.handle.length) { ui.area.edit.resizable({ @@ -674,6 +675,15 @@ function checkEditorScrollbarInner () { editor.scrollTo(null, scrollInfo.top) } +function checkEditorScrollOverLines () { + const desireHeight = parseInt(ui.area.codemirrorScroll[0].style.height) || parseInt(ui.area.codemirrorScroll[0].style.minHeight) + // make editor have extra padding in the bottom (except small screen) + const paddingBottom = editor.doc && editor.doc.height > defaultTextHeight ? (desireHeight - defaultTextHeight) : 0 + if (parseInt(ui.area.codemirrorLines.css('padding-bottom')) !== paddingBottom) { + ui.area.codemirrorLines.css('padding-bottom', paddingBottom + 'px') + } +} + function checkTocStyle () { // toc right var paddingRight = parseFloat(ui.area.markdown.css('padding-right')) @@ -2554,9 +2564,11 @@ function enforceMaxLength (cm, change) { } return false } +let lastDocHeight var ignoreEmitEvents = ['setValue', 'ignoreHistory'] editorInstance.on('beforeChange', function (cm, change) { if (debug) { console.debug(change) } + lastDocHeight = editor.doc.height removeNullByte(cm, change) if (enforceMaxLength(cm, change)) { $('.limit-modal').modal('show') @@ -2590,6 +2602,7 @@ editorInstance.on('paste', function () { // na }) editorInstance.on('changes', function (editor, changes) { + const docHeightChanged = editor.doc.height !== lastDocHeight updateHistory() var docLength = editor.getValue().length // workaround for big documents @@ -2605,13 +2618,18 @@ editorInstance.on('changes', function (editor, changes) { viewportMargin = newViewportMargin windowResize() } - checkEditorScrollbar() - if (ui.area.codemirrorScroll[0].scrollHeight > ui.area.view[0].scrollHeight && editorHasFocus()) { - postUpdateEvent = function () { - syncScrollToView() - postUpdateEvent = null + if (docHeightChanged) { + checkEditorScrollbar() + checkEditorScrollOverLines() + // always sync edit scrolling to view if user is editing + if (ui.area.codemirrorScroll[0].scrollHeight > ui.area.view[0].scrollHeight && editorHasFocus()) { + postUpdateEvent = function () { + syncScrollToView() + postUpdateEvent = null + } } } + lastDocHeight = editor.doc.height }) editorInstance.on('focus', function (editor) { for (var i = 0; i < onlineUsers.length; i++) { diff --git a/public/js/lib/editor/ui-elements.js b/public/js/lib/editor/ui-elements.js index 48d8f707..d084607f 100644 --- a/public/js/lib/editor/ui-elements.js +++ b/public/js/lib/editor/ui-elements.js @@ -70,6 +70,9 @@ export const getUIElements = () => ({ codemirrorSizerInner: $( '.ui-edit-area .CodeMirror .CodeMirror-sizer > div' ), + codemirrorLines: $( + '.ui-edit-area .CodeMirror .CodeMirror-lines' + ), markdown: $('.ui-view-area .markdown-body'), resize: { handle: $('.ui-resizable-handle'),