Skip to content

Build your personal knowledge base with Trilium Notes

fix(code/find): crashes at dialog shown

Elian Doranf20232c077a3
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index 81eae41e1a..dd2391b678 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -11,6 +11,7 @@ import type { ViewScope } from "../services/link.js";
import type FNote from "../entities/fnote.js";
import type TypeWidget from "../widgets/type_widgets/type_widget.js";
import type { CKTextEditor } from "@triliumnext/ckeditor5";
+import type CodeMirror from "@triliumnext/codemirror";
export interface SetNoteOpts {
triggerSwitchEvent?: unknown;
@@ -312,7 +313,7 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
async getCodeEditor() {
return this.timeout(
- new Promise<CodeMirrorInstance>((resolve) =>
+ new Promise<CodeMirror>((resolve) =>
appContext.triggerCommand("executeWithCodeEditor", {
resolve,
ntxId: this.ntxId
diff --git a/apps/client/src/widgets/find.ts b/apps/client/src/widgets/find.ts
index 8db84e448f..094a0a197b 100644
--- a/apps/client/src/widgets/find.ts
+++ b/apps/client/src/widgets/find.ts
@@ -203,7 +203,7 @@ export default class FindWidget extends NoteContextAwareWidget {
selectedText = $content.find('.cm-matchhighlight').first().text();
} else {
const codeEditor = await this.noteContext.getCodeEditor();
- selectedText = codeEditor.getSelection();
+ selectedText = codeEditor.getSelectedText();
}
} else {
selectedText = window.getSelection()?.toString() || "";
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index bb0f8c6089..fa491d0749 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -104,6 +104,17 @@ export default class CodeMirror extends EditorView {
return this.state.doc.toString();
}
+ /**
+ * Returns the currently selected text.
+ *
+ * If there are multiple selections, all of them will be concatenated.
+ */
+ getSelectedText() {
+ return this.state.selection.ranges
+ .map((range) => this.state.sliceDoc(range.from, range.to))
+ .join("");
+ }
+
setText(content: string) {
this.dispatch({
changes: {