Skip to content

Commit

Permalink
fix: CM5 / CM6 script loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alondmnt committed Mar 29, 2024
1 parent ca74eb4 commit ad69afa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/codeMirror5Scroller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function plugin(CodeMirror) {
if (CodeMirror.cm6) { return; }

CodeMirror.defineExtension('scrollToLine', function scrollToLine(lineno) {
// temporary fix: sometimes the first coordinate is incorrect,
// resulting in a difference about +- 10 px,
Expand Down
39 changes: 18 additions & 21 deletions src/codeMirror6Scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@ import { EditorView } from '@codemirror/view';
export default (context: ContentScriptContext): MarkdownEditorContentScriptModule => {
return {
plugin: (editorControl: any) => {
if (editorControl.cm6) {
// Running in CM6
editorControl.registerCommand('scrollToLine', (lineNumber: number) => {
const editor: EditorView = editorControl.editor;
console.log('scrollToLine', lineNumber);
if (!editorControl.cm6) { return; }

// Bounds checking
if (lineNumber < 0) {
lineNumber = 0;
}
if (lineNumber > editor.state.doc.lines) {
lineNumber = editor.state.doc.lines;
}
// Running in CM6
editorControl.registerCommand('scrollToLine', (lineNumber: number) => {
const editor: EditorView = editorControl.editor;
console.log('scrollToLine', lineNumber);

// Scroll to line, place the line at the *top* of the editor
const lineInfo = editor.state.doc.line(lineNumber);
editor.dispatch(editor.state.update({
effects: EditorView.scrollIntoView(lineInfo.from, {y: 'start'})
}));
});
// Bounds checking
if (lineNumber < 0) {
lineNumber = 0;
}
if (lineNumber > editor.state.doc.lines) {
lineNumber = editor.state.doc.lines;
}

} else {
// Running in CM5, see codeMirror5Scroller.js
}
// Scroll to line, place the line at the *top* of the editor
const lineInfo = editor.state.doc.line(lineNumber);
editor.dispatch(editor.state.update({
effects: EditorView.scrollIntoView(lineInfo.from, {y: 'start'})
}));
});
},
};
};

0 comments on commit ad69afa

Please sign in to comment.