Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
Merge branch 'main' into refactor/note-autocomplete-rewrite
Claude90b03d492da4
diff --cc apps/client/src/widgets/dialogs/content_embed.spec.tsxindex 0000000000,32055bf5dd..96d259b835mode 000000,100644..100644--- a/apps/client/src/widgets/dialogs/content_embed.spec.tsx+++ b/apps/client/src/widgets/dialogs/content_embed.spec.tsx@@@ -1,0 -1,97 +1,96 @@@+ import { act } from "preact/test-utils";+ import { beforeEach, describe, expect, it, vi } from "vitest";++ import Component from "../../components/component";+ import type FNote from "../../entities/fnote";+ import type { Suggestion } from "../../services/note_autocomplete";+ import { buildNote } from "../../test/easy-froca";+ import { renderInto } from "../../test/render";+ import { ParentComponent } from "../react/react_utils";+ import ContentEmbedDialog from "./content_embed";++ const autocomplete = vi.hoisted(() => ({+ onChange: undefined as ((suggestion: Suggestion | null) => void) | undefined+ }));++ vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));-vi.mock("../../services/note_autocomplete", () => ({ triggerRecentNotes: vi.fn() }));+ vi.mock("../react/NoteAutocomplete", () => ({+ default: ({ onChange }: { onChange: (suggestion: Suggestion | null) => void }) => {+ autocomplete.onChange = onChange;+ return <input />;+ }+ }));++ describe("ContentEmbedDialog", () => {+ let host: Component;+ let container: HTMLElement;+ const editorApi = { addContentEmbed: vi.fn(), addImage: vi.fn() };++ beforeEach(async () => {+ vi.clearAllMocks();+ host = new Component();+ container = renderInto(+ <ParentComponent.Provider value={host}>+ <ContentEmbedDialog />+ </ParentComponent.Provider>+ );+ await open();+ });++ async function open() {+ await act(async () => {+ void host.handleEventInChildren("showContentEmbedDialog", { editorApi });+ });+ }++ async function pick(note: FNote) {+ await act(async () => {+ autocomplete.onChange?.({ notePath: `root/${note.noteId}` });+ });+ }++ function checkedSize() {+ return container.querySelector<HTMLInputElement>("input[type='radio']:checked")?.value;+ }++ async function choose(size: string) {+ const radio = container.querySelector<HTMLInputElement>(+ `input[type='radio'][value='${size}']`+ );+ if (!radio) throw new Error(`No radio for ${size}.`);+ await act(async () => {+ radio.checked = true;+ radio.dispatchEvent(new Event("change", { bubbles: true }));+ });+ }++ async function submit() {+ const form = container.querySelector("form");+ if (!form) throw new Error("The dialog has no form.");+ await act(async () => {+ form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));+ });+ }++ it("preselects the size that suits the picked note, until another size is chosen", async () => {+ const script = buildNote({ title: "Script", type: "code", mime: "text/javascript" });+ const song = buildNote({ title: "Song", type: "file", mime: "audio/mpeg" });+ expect(checkedSize()).toBe("medium");++ await pick(script);+ expect(checkedSize()).toBe("full");+ await choose("tiny");+ expect(checkedSize()).toBe("tiny");++ // Another note, or opening the dialog again, drops the size chosen by hand.+ await pick(song);+ expect(checkedSize()).toBe("small");+ await choose("expandable");+ await open();+ expect(checkedSize()).toBe("small");++ await choose("expandable");+ await submit();+ expect(editorApi.addContentEmbed).toHaveBeenCalledWith(song.noteId, "expandable");+ });+ });diff --cc apps/client/src/widgets/dialogs/content_embed.tsxindex 639a0f4e1f,088067d313..4da6db3f94--- a/apps/client/src/widgets/dialogs/content_embed.tsx+++ b/apps/client/src/widgets/dialogs/content_embed.tsx@@@ -3,32 -4,39 +4,39 @@@ import { t } from "../../services/i18n"import FormGroup from "../react/FormGroup";import FormRadioGroup from "../react/FormRadioGroup";import Modal from "../react/Modal";-import NoteAutocomplete from "../react/NoteAutocomplete";+import NoteAutocomplete, { type NoteAutocompleteHandle } from "../react/NoteAutocomplete";import Button from "../react/Button";-import { Suggestion, triggerRecentNotes } from "../../services/note_autocomplete";+import { Suggestion } from "../../services/note_autocomplete";import tree from "../../services/tree";import froca from "../../services/froca";- import { useTriliumEvent, useTriliumOption } from "../react/hooks";+ import { useNote, useTriliumEvent } from "../react/hooks";import type { BoxSize, CKEditorApi } from "../type_widgets/text/CKEditorWithWatchdog";- export interface IncludeNoteOpts {- editorApi: Pick<CKEditorApi, "addIncludeNote" | "addImage">;+ export interface ContentEmbedOpts {+ editorApi: Pick<CKEditorApi, "addContentEmbed" | "addImage">;}- export default function IncludeNoteDialog() {- const editorApiRef = useRef<Pick<CKEditorApi, "addIncludeNote" | "addImage">>(null);+ export default function ContentEmbedDialog() {+ const editorApiRef = useRef<Pick<CKEditorApi, "addContentEmbed" | "addImage">>(null);const [suggestion, setSuggestion] = useState<Suggestion | null>(null);- const [defaultBoxSize, setDefaultBoxSize] = useTriliumOption("includeNoteDefaultBoxSize");- const [boxSize, setBoxSize] = useState<string>(defaultBoxSize);+ // The size chosen by hand, otherwise the one that suits the picked note.+ const [chosenBoxSize, setChosenBoxSize] = useState<BoxSize | null>(null);const [shown, setShown] = useState(false);+ const note = useNote(suggestion?.notePath ? tree.getNoteIdFromUrl(suggestion.notePath) : null);+ const boxSize = chosenBoxSize ?? (note ? getEmbedBoxSize(note) : "medium");- useTriliumEvent("showIncludeNoteDialog", ({ editorApi }) => {+ useTriliumEvent("showContentEmbedDialog", ({ editorApi }) => {editorApiRef.current = editorApi;- setBoxSize(defaultBoxSize); // Reset to default when opening dialog+ setChosenBoxSize(null);setShown(true);});+ const changeSuggestion = useCallback((newSuggestion: Suggestion | null) => {+ setSuggestion(newSuggestion);+ setChosenBoxSize(null);+ }, []);+- const autoCompleteRef = useRef<HTMLInputElement>(null);+ const autocompleteRef = useRef<NoteAutocompleteHandle>(null);return (<Modal@@@ -52,8 -56,8 +56,8 @@@<FormGroup name="note" label={t("include_note.label_note")}><NoteAutocompleteplaceholder={t("include_note.placeholder_search")}- onChange={setSuggestion}+ onChange={changeSuggestion}- inputRef={autoCompleteRef}+ handleRef={autocompleteRef}opts={{hideGoToSelectedNoteButton: true,allowCreatingNotes: true