Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: fix some issues

Adorian Doran18867143b5b3
diff --git a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
index 6046d33879..78c7aa1951 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
@@ -10,9 +10,10 @@ import { renderInto } from "../../test/render";
import { ParentComponent } from "../react/react_utils";
const CANVAS_MIME = "application/vnd.excalidraw+json";
-const { getRenderedContent, drawingMounts } = vi.hoisted(() => ({
+const { getRenderedContent, drawingMounts, drawingProps } = vi.hoisted(() => ({
getRenderedContent: vi.fn(async () => ({ $renderedContent: [] })),
- drawingMounts: vi.fn()
+ drawingMounts: vi.fn(),
+ drawingProps: vi.fn()
}));
// Nothing initialises i18next in a unit test, and an uninitialised one answers every lookup with an
@@ -32,9 +33,10 @@ vi.mock("../../services/content_renderer", () => ({
},
hasRenderedPreview: (attachment: FAttachment) => attachment.mime === CANVAS_MIME
}));
-// Counts the mounts of the drawing editor, which remounts to load a change saved elsewhere.
+// Counts the mounts of the drawing editor, which loads a change saved elsewhere in place.
vi.mock("./canvas/CanvasDrawing", () => ({
- CanvasDrawingDetail: () => {
+ CanvasDrawingDetail: (props: { revision?: number }) => {
+ drawingProps(props);
useEffect(() => drawingMounts(), []);
return <div className="drawing-detail-stub" />;
}
@@ -105,6 +107,8 @@ describe("AttachmentList", () => {
describe("AttachmentDetail", () => {
it("edits a canvas drawing, loading again only what another component saved", async () => {
+ const getRevision = () =>
+ (drawingProps.mock.lastCall?.[0] as { revision?: number }).revision;
const drawing = { ...role("file"), mime: CANVAS_MIME } as FAttachment;
vi.spyOn(froca, "getAttachment").mockResolvedValue(drawing);
getRenderedContent.mockClear();
@@ -129,9 +133,12 @@ describe("AttachmentDetail", () => {
await reloadAttachment(component, drawing.attachmentId, component.componentId);
await reloadAttachment(component, "otherAttachment", "otherComponent");
expect(drawingMounts).toHaveBeenCalledOnce();
+ expect(getRevision()).toBe(0);
+ // The detail stays mounted, so that its unsaved changes carry over to the drawing.
await reloadAttachment(component, drawing.attachmentId, "otherComponent");
- await vi.waitFor(() => expect(drawingMounts).toHaveBeenCalledTimes(2));
+ await vi.waitFor(() => expect(getRevision()).toBe(1));
+ expect(drawingMounts).toHaveBeenCalledOnce();
});
});
diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsx
index 894053228d..0be8ae4cba 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx
@@ -356,7 +356,8 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
>
<Suspense fallback={null}>
<CanvasDrawingDetail
- key={`${attachment.attachmentId}-${externalRevision}`}
+ key={attachment.attachmentId}
+ revision={externalRevision}
attachment={attachment}
note={ownerNote}
noteContext={noteContext}
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
index 91001ee416..f1b533dc31 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
@@ -18,8 +18,17 @@ vi.mock("./persistence", () => ({
parseContent: (content: string) => JSON.parse(content),
getInlineFiles: () => ({})
}));
-const detailEditor = vi.hoisted(() => ({ canEdit: () => true, release: () => {} }));
-vi.mock("../text/attachment_saves", () => ({ useAttachmentEditor: () => detailEditor }));
+const { detailEditor, attachmentEditorMounts, canvasEditorMounts } = vi.hoisted(() => ({
+ detailEditor: { canEdit: () => true, release: () => {} },
+ attachmentEditorMounts: vi.fn(),
+ canvasEditorMounts: vi.fn()
+}));
+vi.mock("../text/attachment_saves", () => ({
+ useAttachmentEditor: () => {
+ useEffect(() => attachmentEditorMounts(), []);
+ return detailEditor;
+ }
+}));
const canvasEditorProps = vi.fn();
const persistenceArgs = vi.fn();
@@ -37,6 +46,7 @@ function MockCanvasEditor(props: MockCanvasEditorProps) {
canvasEditorProps(props);
const [ isLoaded, setIsLoaded ] = useState(false);
useEffect(() => setIsLoaded(true), []);
+ useEffect(() => canvasEditorMounts(), []);
return isLoaded ? <div className="excalidraw" tabIndex={0} /> : <span className="loading" />;
}
@@ -612,6 +622,23 @@ describe("CanvasDrawing", () => {
editable.remove();
});
+ it("loads the drawing again for each revision, keeping its detail editor", async () => {
+ const note = buildNote({ title: "Owner" });
+ const mountDetail = (revision: number) => act(async () => {
+ render(
+ <CanvasDrawingDetail attachment={ATTACHMENT} note={note} revision={revision} />,
+ box
+ );
+ });
+ attachmentEditorMounts.mockClear();
+ canvasEditorMounts.mockClear();
+
+ await mountDetail(0);
+ await mountDetail(1);
+ expect(canvasEditorMounts).toHaveBeenCalledTimes(2);
+ expect(attachmentEditorMounts).toHaveBeenCalledOnce();
+ });
+
it("edits the drawing in the detail of its attachment unless its note is read-only", async () => {
async function mountDetail(title: string, labels: Record<string, string> = {}) {
const note = buildNote({ title, ...labels });
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
index 2b77c27fbc..5fabb11a63 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
@@ -106,16 +106,39 @@ interface CanvasDrawingDetailProps {
/** The note that owns the attachment. */
note: FNote;
noteContext?: NoteContext;
+ /** Counts the changes to the drawing saved elsewhere, each of which loads it again. */
+ revision?: number;
}
/**
* A canvas drawing saved in an attachment, edited in the full detail of the attachment. It is
* read-only while its note is.
*/
-export function CanvasDrawingDetail({ attachment, note, noteContext }: CanvasDrawingDetailProps) {
+export function CanvasDrawingDetail({
+ attachment, note, noteContext, revision
+}: CanvasDrawingDetailProps) {
+ // Outlives the drawing loaded again for each revision, which starts from its unsaved changes.
+ const editor = useAttachmentEditor(note, noteContext);
+
+ return (
+ <DetailDrawing
+ key={revision}
+ attachment={attachment}
+ note={note}
+ noteContext={noteContext}
+ editor={editor}
+ />
+ );
+}
+
+interface DetailDrawingProps extends Omit<CanvasDrawingDetailProps, "revision"> {
+ editor: AttachmentEditor;
+}
+
+/** The drawing of `CanvasDrawingDetail`, saved by `editor`. */
+function DetailDrawing({ attachment, note, noteContext, editor }: DetailDrawingProps) {
const apiRef = useRef<ExcalidrawImperativeAPI>(null);
const colorScheme = useColorScheme();
- const editor = useAttachmentEditor(note, noteContext);
const isNoteReadOnly = useEffectiveReadOnly(note, noteContext);
const canEdit = editor.canEdit(attachment) && !isNoteReadOnly
&& !options.is("databaseReadonly");
diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts
index 48f0e504fe..b2a6f643e2 100644
--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts
+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts
@@ -178,6 +178,27 @@ describe("ResizableWidgets", () => {
.toEqual([ "\"\"", "\"\"", "\"\"" ]);
});
+ it("sizes the handles for touch whenever there is a touch screen, primary or not", () => {
+ const sheet = [ ...document.styleSheets ].find((candidate) => [ ...candidate.cssRules ]
+ .some((rule) => rule instanceof CSSStyleRule
+ && rule.selectorText === ".ck .ck-widget__resize-handle"));
+ const conditions: string[] = [];
+ const visit = (rules: CSSRuleList) => {
+ for (const rule of rules) {
+ if (rule instanceof CSSMediaRule) {
+ conditions.push(rule.conditionText);
+ }
+ if ("cssRules" in rule) {
+ visit(rule.cssRules as CSSRuleList);
+ }
+ }
+ };
+ visit(sheet?.cssRules ?? ([] as unknown as CSSRuleList));
+
+ // The hit size and the grips of the edges, for a laptop whose mouse is primary too.
+ expect(conditions).toEqual([ "(any-pointer: coarse)", "(any-pointer: coarse)" ]);
+ });
+
it("marks a widget resizable while isResizable allows it", async () => {
await createEditor({ isResizable: (element) => element.getAttribute("kind") !== "fixed" });
editor.setData("<div class=\"test-box\"></div><p>text</p>");
@@ -313,6 +334,25 @@ describe("ResizableWidgets", () => {
expect(editor.getData()).toBe(RESIZED);
});
+ it("stops a resize when another pointer starts one", async () => {
+ await createEditor();
+ editor.setData(RESIZED);
+ const box = getBox();
+
+ press(getHandle("width"));
+ move(150, 300);
+ expect(box.style.getPropertyValue("--test-width")).toBe("35em");
+
+ press(getHandle("width"), { pointerId: 2, pointerType: "pen" });
+ move(250, 300);
+ expect(box.style.getPropertyValue("--test-width")).toBe("35em");
+ move(120, 300, 2);
+ expect(box.style.getPropertyValue("--test-width")).toBe("37em");
+
+ window.dispatchEvent(new PointerEvent("pointerup", { pointerId: 2 }));
+ expect(getSize()).toContain("customWidth=\"37em\"");
+ });
+
it("stops resizing once the editor is destroyed", async () => {
await createEditor();
editor.setData("<div class=\"test-box\"></div>");
diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
index 88dd9f0e39..a620129f73 100644
--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
@@ -187,6 +187,8 @@ export default class ResizableWidgets extends Plugin {
this.showSize(viewElement, getSavedSize(element, config), config, false);
};
+ // One resize at a time: another pointer, such as a pen beside the mouse, ends the first.
+ this.stopResize?.();
this.stopResize = startResizeGesture(event, {
handle,
axes,
diff --git a/packages/ckeditor5/src/theme/resizable_widgets.css b/packages/ckeditor5/src/theme/resizable_widgets.css
index 27a59f31a3..259753fa93 100644
--- a/packages/ckeditor5/src/theme/resizable_widgets.css
+++ b/packages/ckeditor5/src/theme/resizable_widgets.css
@@ -6,7 +6,7 @@
--ck-resize-handle-grip-thickness: 4px;
}
-@media (pointer: coarse) {
+@media (any-pointer: coarse) {
:where(:root) {
--ck-resize-handle-hit-size: 32px;
}
@@ -47,7 +47,7 @@
}
/* The edges show a grip on a touch screen only. */
- @media (pointer: coarse) {
+ @media (any-pointer: coarse) {
&::after {
content: "";
}