The editor is unloaded when no matching note is found in search

Bug report

Is it intended behavior that the editor gets unloaded when there are no matching notes during a search?

When the editor is unloaded, any event listeners or settings applied to the editor via inkdrop.getActiveEditor() or inkdrop.onEditorLoad((editor) => ...) are cleared.

To work around this, it seems necessary to reinitialize in onEditorUnload like the example below:

onEditorLoad((editor) => {
  editor.cm.setOption("cursorBlinkRate", 0);
}, true);

const onEditorLoad = (func, isReAttach) => {
  const editor = inkdrop.activeEditor;
  if (editor != null) {
    func(editor);
  } else {
    inkdrop.onEditorLoad((editor) => func(editor));
  }

  if (isReAttach) {
    inkdrop.onEditorUnload(() => reAttach(func));
  }
};

const reAttach = (func) => {
  const editor = inkdrop.getActiveEditor();
  if (editor == null) {
    setTimeout(() => reAttach(func), 1000);
    return;
  }

  func(editor);
};

In my case, the table-editor plugin was affected, but I was able to fix it with the following modification:

diff --git a/src/index.js b/src/index.js
index 0376a5f..862e55d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -39,11 +39,22 @@ export function activate() {
 
   subscriptions.add(
     inkdrop.onEditorUnload(() => {
-      editor.dispose();
+      reAttach();
     }),
   );
 }
 
+function reAttach() {
+  const activeEditor = inkdrop.getActiveEditor();
+  if (activeEditor == null) {
+    console.log('reattache ...');
+    setTimeout(() => reAttach(), 1000);
+    return;
+  }
+  console.log('reattache ... done');
+  editor = new Editor(activeEditor.cm);
+}
+
 export function deactivate() {
   subscriptions.dispose();
   editor.dispose();

Environment

  • Platform:
  • Platform version: Sonoma 14.7.1
  • App Version: 5.11.4

How to reproduce

  • Search using a word that yields no results.

日本語で (In Japanese)

title: 検索時に対象ノートがない場合にエディターがアンロードされる。

検索時に対象ノートがない場合、エディターがアンロードされるのは仕様ですか?
アンロードされることで、 inkdrop.getActiveEditor()inkdrop.onEditorLoad((editor) => ・・・ )) で取得したエディタに対して設定したイベントや設置がクリアされます。

回避するためには記載したように onEditorUnload で初期化処理を再実行する必要があります。

Hi @shimizu_tatsuya -san,

Yes, that’s intended. The editor should be unloaded when nothing is selected on the note list.
When the editor is loaded again, the onEditorLoad event is fired.
So, you should always listen to this event in activate in your plugin, like the vim plugin.

I hope it helps.

1 Like

Thank you. I was able to resolve the issue.

I submitted a pull request.

1 Like

Cool, thank you shimizu-san!
This table editor extension will be available out of the box in the next-gen editor that I’m currently working on :raised_hands: Can’t wait to release it.
Have a nice daay

1 Like