Skip to content

Commit

Permalink
fix: #7
Browse files Browse the repository at this point in the history
  • Loading branch information
SeptemberHX committed Sep 13, 2022
1 parent 21bbff3 commit 3a4a282
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
27 changes: 26 additions & 1 deletion src/codemirror/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,35 @@ module.exports = {
}, 100);
cm.on('cursorActivity', changeDebounce);
});

CodeMirror.defineExtension('scrollToLine', function scrollToLine(lineno) {
// temporary fix: sometimes the first coordinate is incorrect,
// resulting in a difference about +- 10 px,
// call the scroll function twice fixes the problem.
this.scrollTo(null, this.charCoords({ line: lineno, ch: 0 }, 'local').top);
this.scrollTo(null, this.charCoords({ line: lineno, ch: 0 }, 'local').top);
});

CodeMirror.defineOption("markdownHeaderChange", [], async function(cm, val, old) {
const headerChangeDebounce = debounce(async function() {
var rect = cm.getWrapperElement().getBoundingClientRect();
return await _context.postMessage({
from: cm.lineAtHeight(rect.top, "window"),
to: cm.lineAtHeight(rect.bottom, "window")
});
}, 10);
cm.on('scroll', headerChangeDebounce);
cm.on('change', function (cm, changeObjs) {
if (changeObjs.origin === 'setValue') {
headerChangeDebounce();
}
});
});
},
codeMirrorOptions: {
'sidebar_cm_commands': true,
'cursorChangeNotification': true
'cursorChangeNotification': true,
'markdownHeaderChange': true
},
assets: function() {
return [ ];
Expand Down
34 changes: 0 additions & 34 deletions src/outline/codeMirrorScroller.js

This file was deleted.

8 changes: 1 addition & 7 deletions src/outline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ class OutlinePlugin extends SidebarPlugin {
this.sidebars = sidebars;
await registerSettings();

await joplin.contentScripts.register(
ContentScriptType.CodeMirrorPlugin,
'codeMirrorScroller',
'./outline/codeMirrorScroller.js',
);

await joplin.contentScripts.onMessage('codeMirrorScroller', async (msg) => {
await joplin.contentScripts.onMessage('sidebar_cm_commands', async (msg) => {
const note = await joplin.workspace.selectedNote();
if (note) {
const headers = mdHeaders(note.body, msg.from, msg.to);
Expand Down

0 comments on commit 3a4a282

Please sign in to comment.