Skip to content

Commit c6b6d63

Browse files
authored
Merge pull request #1531 from hackmdio/feature/editor-scroll-over-lines
Make editor can scroll over lines
2 parents 4fdb7f8 + b67079d commit c6b6d63

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

public/js/index.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ function checkEditorStyle () {
588588
}
589589
// workaround editor will have wrong doc height when editor height changed
590590
editor.setSize(null, ui.area.edit.height())
591+
checkEditorScrollOverLines()
591592
// make editor resizable
592593
if (!ui.area.resize.handle.length) {
593594
ui.area.edit.resizable({
@@ -674,6 +675,15 @@ function checkEditorScrollbarInner () {
674675
editor.scrollTo(null, scrollInfo.top)
675676
}
676677

678+
function checkEditorScrollOverLines () {
679+
const desireHeight = parseInt(ui.area.codemirrorScroll[0].style.height) || parseInt(ui.area.codemirrorScroll[0].style.minHeight)
680+
// make editor have extra padding in the bottom (except small screen)
681+
const paddingBottom = editor.doc && editor.doc.height > defaultTextHeight ? (desireHeight - defaultTextHeight) : 0
682+
if (parseInt(ui.area.codemirrorLines.css('padding-bottom')) !== paddingBottom) {
683+
ui.area.codemirrorLines.css('padding-bottom', paddingBottom + 'px')
684+
}
685+
}
686+
677687
function checkTocStyle () {
678688
// toc right
679689
var paddingRight = parseFloat(ui.area.markdown.css('padding-right'))
@@ -2554,9 +2564,11 @@ function enforceMaxLength (cm, change) {
25542564
}
25552565
return false
25562566
}
2567+
let lastDocHeight
25572568
var ignoreEmitEvents = ['setValue', 'ignoreHistory']
25582569
editorInstance.on('beforeChange', function (cm, change) {
25592570
if (debug) { console.debug(change) }
2571+
lastDocHeight = editor.doc.height
25602572
removeNullByte(cm, change)
25612573
if (enforceMaxLength(cm, change)) {
25622574
$('.limit-modal').modal('show')
@@ -2590,6 +2602,7 @@ editorInstance.on('paste', function () {
25902602
// na
25912603
})
25922604
editorInstance.on('changes', function (editor, changes) {
2605+
const docHeightChanged = editor.doc.height !== lastDocHeight
25932606
updateHistory()
25942607
var docLength = editor.getValue().length
25952608
// workaround for big documents
@@ -2605,13 +2618,18 @@ editorInstance.on('changes', function (editor, changes) {
26052618
viewportMargin = newViewportMargin
26062619
windowResize()
26072620
}
2608-
checkEditorScrollbar()
2609-
if (ui.area.codemirrorScroll[0].scrollHeight > ui.area.view[0].scrollHeight && editorHasFocus()) {
2610-
postUpdateEvent = function () {
2611-
syncScrollToView()
2612-
postUpdateEvent = null
2621+
if (docHeightChanged) {
2622+
checkEditorScrollbar()
2623+
checkEditorScrollOverLines()
2624+
// always sync edit scrolling to view if user is editing
2625+
if (ui.area.codemirrorScroll[0].scrollHeight > ui.area.view[0].scrollHeight && editorHasFocus()) {
2626+
postUpdateEvent = function () {
2627+
syncScrollToView()
2628+
postUpdateEvent = null
2629+
}
26132630
}
26142631
}
2632+
lastDocHeight = editor.doc.height
26152633
})
26162634
editorInstance.on('focus', function (editor) {
26172635
for (var i = 0; i < onlineUsers.length; i++) {

public/js/lib/editor/ui-elements.js

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const getUIElements = () => ({
7070
codemirrorSizerInner: $(
7171
'.ui-edit-area .CodeMirror .CodeMirror-sizer > div'
7272
),
73+
codemirrorLines: $(
74+
'.ui-edit-area .CodeMirror .CodeMirror-lines'
75+
),
7376
markdown: $('.ui-view-area .markdown-body'),
7477
resize: {
7578
handle: $('.ui-resizable-handle'),

0 commit comments

Comments
 (0)