Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: fix some issues

Adorian Doran47402650fe8e
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 44c2ab6b76..62ebb3d5f0 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -3,7 +3,7 @@ import "./code.css";
import { default as VanillaCodeMirror, getThemeById } from "@triliumnext/codemirror";
import { NoteType } from "@triliumnext/commons";
import { Ref } from "preact";
-import { useEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { CommandListenerData } from "../../../components/app_context";
import FNote from "../../../entities/fnote";
@@ -89,12 +89,12 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC
const editorRef = useRef<VanillaCodeMirror>(null);
const containerRef = useRef<HTMLPreElement>(null);
const [ editorView, setEditorView ] = useState<VanillaCodeMirror | null>(null);
- const combinedEditorRef = (view: VanillaCodeMirror | null) => {
+ const combinedEditorRef = useCallback((view: VanillaCodeMirror | null) => {
editorRef.current = view;
setEditorView(view);
if (typeof externalEditorRef === "function") externalEditorRef(view);
else if (externalEditorRef) externalEditorRef.current = view;
- };
+ }, [externalEditorRef]);
const [ vimKeymapEnabled ] = useTriliumOptionBool("vimKeymapEnabled");
const [ noteTabWidth ] = useNoteLabelInt(note, "tabWidth");
const [ noteUseTabs ] = useNoteLabelOptionalBool(note, "indentWithTabs");
diff --git a/apps/client/src/widgets/type_widgets/code/snippets.ts b/apps/client/src/widgets/type_widgets/code/snippets.ts
index 44fa815619..b89d543ce0 100644
--- a/apps/client/src/widgets/type_widgets/code/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/code/snippets.ts
@@ -80,6 +80,8 @@ export function buildSnippetCompletions(snippets: CodeSnippet[]): Completion[] {
*/
export function useCodeSnippets(matches: (note: FNote) => boolean, reloadKey: string, enabled = true) {
const snippetsRef = useRef<CodeSnippet[]>([]);
+ // Monotonic id so a slow fetch that resolves after a newer reload is discarded (avoids stale data).
+ const reloadCountRef = useRef(0);
// Keep the latest predicate without making it a dependency (it's a fresh closure each render);
// `reloadKey` is the stable trigger for filter changes.
const matchesRef = useRef(matches);
@@ -90,8 +92,11 @@ export function useCodeSnippets(matches: (note: FNote) => boolean, reloadKey: st
snippetsRef.current = [];
return;
}
+ const reloadId = ++reloadCountRef.current;
void getCodeSnippets((note) => matchesRef.current(note)).then((snippets) => {
- snippetsRef.current = snippets;
+ if (reloadId === reloadCountRef.current) {
+ snippetsRef.current = snippets;
+ }
});
}, [enabled]);
@@ -124,7 +129,10 @@ export function useSnippetSlashCommands(editorView: VanillaCodeMirror | null, ma
override: [(context: CompletionContext) => {
const match = context.matchBefore(SLASH_COMMAND_REGEX);
if (!match) return null;
- return { from: match.from, options: buildSnippetCompletions(snippetsRef.current) };
+ const options = buildSnippetCompletions(snippetsRef.current);
+ // No matching snippets (e.g. none for this MIME, or disabled) → no source, so an
+ // empty completion popup never appears when the user types "/".
+ return options.length ? { from: match.from, options } : null;
}],
activateOnTyping: true
});