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
fix(presentation): edit an empty text slide on a double-click
Elian Doran116cc453b604
diff --git a/apps/client/src/widgets/collections/presentation/slide.spec.tsx b/apps/client/src/widgets/collections/presentation/slide.spec.tsxindex c101e279b2..5cfc9e3eb4 100644--- a/apps/client/src/widgets/collections/presentation/slide.spec.tsx+++ b/apps/client/src/widgets/collections/presentation/slide.spec.tsx@@ -206,3 +206,76 @@ describe("Presentation keys", () => {expect(press(field)).toBe(false);});});++describe("Presentation double-click", () => {+ afterEach(() => {+ vi.doUnmock("reveal.js");+ vi.resetModules();+ document.body.replaceChildren();+ });++ it("edits the slide shown on a double-click beside its content, as an empty slide has none", async () => {+ const reveal = { currentNoteId: "empty" };+ vi.doMock("reveal.js", () => ({+ default: class {+ element: HTMLElement;+ constructor(element: HTMLElement) { this.element = element; }+ initialize() { return Promise.resolve(); }+ destroy() {}+ isReady() { return true; }+ layout() {}+ sync() {}+ slide() {}+ on() {}+ off() {}+ isOverview() { return false; }+ getIndices() { return { h: 0, v: 0 }; }+ getRevealElement() { return this.element; }+ getCurrentSlide() { return this.element.querySelector(`#slide-${reveal.currentNoteId}`); }+ }+ }));+ const { Presentation } = await import("./slides");+ const onEditSlide = vi.fn();+ const container = document.createElement("div");+ document.body.append(container);+ await act(async () => render(+ <Presentation+ presentation={{ slides: [+ { noteId: "empty", type: "text", content: { __html: "" }, verticalSlides: undefined },+ { noteId: "web", type: "webView", content: { __html: "" }, verticalSlides: undefined }+ ] }}+ setApi={() => {}}+ ntxId={undefined}+ editedNoteId={undefined}+ isEditorReady={false}+ closingContents={{}}+ onEditSlide={onEditSlide}+ onEditorHost={() => {}}+ pendingNoteId={undefined}+ onPendingSlideShown={() => {}}+ onStartPresentation={undefined}+ />,+ container+ ));+ const dblclick = (target: Element | null) => {+ expect(target).not.toBeNull();+ act(() => {+ target?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, composed: true }));+ });+ };++ // Reveal.js sizes a slide to its content to center it, so the slide area around it is not the slide.+ await vi.waitFor(() => {+ dblclick(container.querySelector(".slides"));+ expect(onEditSlide.mock.calls).toEqual([ [ "empty" ] ]);+ });+ // A double-click on the slide itself edits it once.+ dblclick(container.querySelector("#slide-empty"));+ expect(onEditSlide.mock.calls).toEqual([ [ "empty" ], [ "empty" ] ]);++ // A slide that cannot be edited in place stays as it is.+ reveal.currentNoteId = "web";+ dblclick(container.querySelector(".reveal"));+ expect(onEditSlide).toHaveBeenCalledTimes(2);+ });+});diff --git a/apps/client/src/widgets/collections/presentation/slides.tsx b/apps/client/src/widgets/collections/presentation/slides.tsxindex 92e73d7635..b75d59e91a 100644--- a/apps/client/src/widgets/collections/presentation/slides.tsx+++ b/apps/client/src/widgets/collections/presentation/slides.tsx@@ -126,9 +126,16 @@ export function Presentation({onPendingSlideShown(pendingNoteId);}, [ presentation, revealApi, pendingNoteId ]);+ /** Starts editing `slide` on a double-click, where it can be edited in place and is not edited yet. */+ function getEditHandler(slide: PresentationSlideBaseModel | undefined) {+ if (!slide || !onEditSlide || slide.noteId === editedNoteId || !canEditSlideInPlace(slide)) return;+ return () => {+ if (!revealApi?.isOverview() && !document.fullscreenElement) onEditSlide(slide.noteId);+ };+ }+function renderSlide(slide: PresentationSlideBaseModel) {const isEdited = slide.noteId === editedNoteId;- const canEdit = onEditSlide && !isEdited && canEditSlideInPlace(slide);const closingContent = closingContents[slide.noteId];return (<Slide@@ -139,15 +146,27 @@ export function Presentation({editorHostRef={isEdited && slide.type === "text" ? onEditorHost : undefined}isCanvasEdited={isEdited && slide.type === "canvas"}isEditorReady={isEditorReady}- onEdit={canEdit ? () => {- if (!revealApi?.isOverview() && !document.fullscreenElement) onEditSlide(slide.noteId);- } : undefined}+ onEdit={getEditHandler(slide)}/>);}+ // Reveal.js sizes the section of a slide to its content, to center it, so a double-click beside+ // the content, or anywhere on an empty slide, edits the slide shown. Its section handles one on it.+ function onSlideAreaDblClick(e: MouseEvent) {+ const isOnSlide = e.composedPath().some((target) => (+ target instanceof HTMLElement && target.matches("section[data-note-id]")+ ));+ if (isOnSlide) return;+ const noteId = getNoteIdFromSlide(revealApi?.getCurrentSlide());+ const slide = presentation.slides+ .flatMap((topLevelSlide) => [ topLevelSlide, ...topLevelSlide.verticalSlides ?? [] ])+ .find((candidate) => candidate.noteId === noteId);+ getEditHandler(slide)?.();+ }+return (- <div ref={containerRef} className="reveal">+ <div ref={containerRef} className="reveal" onDblClick={onSlideAreaDblClick}><div className="slides">{presentation.slides?.map(slide => {if (!slide.verticalSlides) {