Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: fix some issues

Adorian Doranf340999d9abb
diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
index d639c7168a..4da297b0bc 100644
--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
@@ -207,6 +207,43 @@ describe("FileUploadCommand", () => {
expect(getEmbedBoxSize).not.toHaveBeenCalled();
});
+ it("places an embed that takes the focus as an image, keeping the text and the cursor", () => {
+ installGlobMock({ getComponentByEl: () => ({}) });
+ const insert = (data: string, focusEmbed: boolean) => {
+ setModelData(editor.model, data);
+ editor.execute("fileUpload", {
+ file: [ new File([ "{}" ], "Canvas.excalidraw") ],
+ asEmbed: true,
+ focusEmbed
+ });
+ return getModelData(editor.model).replace(/<contentEmbed[^>]*><\/contentEmbed>/, "<E>");
+ };
+
+ // Beside the block of the selection, which collapses to its start.
+ expect([
+ insert("<paragraph>f[oo] bar</paragraph><paragraph>baz</paragraph>", true),
+ insert("<paragraph>foo [bar</paragraph><paragraph>ba]z</paragraph>", true),
+ insert("<paragraph>foo[] bar</paragraph>", true),
+ insert("<paragraph>[foo bar]</paragraph>", true),
+ insert("<paragraph>foo bar[]</paragraph>", true),
+ insert(
+ "<paragraph>foo</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph>",
+ true
+ )
+ ]).toEqual([
+ "<E><paragraph>f[]oo bar</paragraph><paragraph>baz</paragraph>",
+ "<E><paragraph>foo []bar</paragraph><paragraph>baz</paragraph>",
+ "<E><paragraph>foo[] bar</paragraph>",
+ "<paragraph>[]foo bar</paragraph><E>",
+ "<paragraph>foo bar[]</paragraph><E>",
+ // An empty line becomes the embed, as it becomes an image.
+ "<paragraph>foo[]</paragraph><E><paragraph>bar</paragraph>"
+ ]);
+ // Other embeds take the place of the selection.
+ expect(insert("<paragraph>f[oo] bar</paragraph>", false))
+ .toBe("<paragraph>f</paragraph><E><paragraph>[] bar</paragraph>");
+ });
+
it("marks the loaders of a quiet upload and of a focus upload, and only those", () => {
installGlobMock({ getComponentByEl: () => ({}) });
setModelData(editor.model, "<paragraph>[]</paragraph>");
diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts
index f1828d8233..d2c6cbf09b 100644
--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts
@@ -1,6 +1,7 @@
import {
Command,
FileRepository,
+ findOptimalInsertionRange,
type Editor,
type FileLoader,
type Model,
@@ -22,7 +23,10 @@ export interface FileUploadOptions {
editable?: boolean;
/** Skips the `upload` event, for a file that the editor created itself. */
quiet?: boolean;
- /** Gives the focus to what the embeds show, once the upload ends. */
+ /**
+ * Gives the focus to what the embeds show, once the upload ends. The embeds go beside the
+ * block of the selection, as an image does, and the cursor stays where the selection starts.
+ */
focusEmbed?: boolean;
}
@@ -77,7 +81,8 @@ export default class FileUploadCommand extends Command {
insertEmbedPlaceholder(writer, model, loader.id, file.name, {
boxSize: boxSize ?? getEmbedBoxSize(this.editor, file),
hideTitle,
- editable
+ editable,
+ isBesideSelection: focusEmbed
});
} else {
insertPlaceholder(writer, model, loader.id, file.name);
@@ -107,7 +112,7 @@ function insertEmbedPlaceholder(
model: Model,
uploadId: string,
fileName: string,
- { boxSize, hideTitle, editable }: { boxSize: string; hideTitle?: boolean; editable?: boolean }
+ { boxSize, hideTitle, editable, isBesideSelection }: EmbedPlaceholderOptions
) {
const placeholder = writer.createElement("contentEmbed", {
boxSize,
@@ -116,7 +121,21 @@ function insertEmbedPlaceholder(
uploadId,
uploadFileName: fileName
});
- model.insertObject(placeholder, model.document.selection, null, { setSelection: "after" });
+ if (isBesideSelection) {
+ const selection = model.document.selection;
+ model.insertObject(placeholder, findOptimalInsertionRange(selection, model));
+ writer.setSelection(selection.getFirstPosition());
+ } else {
+ model.insertObject(placeholder, model.document.selection, null, { setSelection: "after" });
+ }
+}
+
+interface EmbedPlaceholderOptions {
+ boxSize: string;
+ hideTitle?: boolean;
+ editable?: boolean;
+ /** Places the embed beside the block of the selection, which collapses to its start. */
+ isBesideSelection?: boolean;
}
/** The box size the host gives an embed of `file`, `medium` when the host does not say. */
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 b2a6f643e2..f326a83d4b 100644
--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts
+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts
@@ -334,23 +334,29 @@ describe("ResizableWidgets", () => {
expect(editor.getData()).toBe(RESIZED);
});
- it("stops a resize when another pointer starts one", async () => {
- await createEditor();
+ it("cancels a resize when another pointer starts one", async () => {
+ await createEditor({}, [ WidgetToolbarRepository ]);
editor.setData(RESIZED);
const box = getBox();
+ const toolbars = editor.plugins.get(WidgetToolbarRepository);
+ const getWidth = () => box.style.getPropertyValue("--test-width");
+ const pressPen = () => press(getHandle("width"), { pointerId: 2, pointerType: "pen" });
press(getHandle("width"));
move(150, 300);
- expect(box.style.getPropertyValue("--test-width")).toBe("35em");
+ expect([ getWidth(), toolbars.isEnabled ]).toEqual([ "35em", false ]);
- press(getHandle("width"), { pointerId: 2, pointerType: "pen" });
+ // A tap of the pen ends the drag of the mouse as Escape does.
+ pressPen();
+ window.dispatchEvent(new PointerEvent("pointerup", { pointerId: 2 }));
+ expect([ getWidth(), toolbars.isEnabled ]).toEqual([ "30em", true ]);
move(250, 300);
- expect(box.style.getPropertyValue("--test-width")).toBe("35em");
- move(120, 300, 2);
- expect(box.style.getPropertyValue("--test-width")).toBe("37em");
+ expect(getWidth()).toBe("30em");
+ pressPen();
+ move(120, 300, 2);
window.dispatchEvent(new PointerEvent("pointerup", { pointerId: 2 }));
- expect(getSize()).toContain("customWidth=\"37em\"");
+ expect(getSize()).toContain("customWidth=\"32em\"");
});
it("stops resizing once the editor is destroyed", async () => {
diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
index a620129f73..98366fe299 100644
--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts
@@ -61,8 +61,8 @@ const EM_PATTERN = /^\d+(\.\d+)?em$/;
export default class ResizableWidgets extends Plugin {
private lastTap: { handle: Element; time: number } | null = null;
- /** Ends the resize under way, if any, without a change. */
- private stopResize: (() => void) | null = null;
+ /** Cancels the resize under way, if any. */
+ private cancelResize: (() => void) | null = null;
static get pluginName() {
return "ResizableWidgets" as const;
@@ -125,7 +125,7 @@ export default class ResizableWidgets extends Plugin {
}
override destroy() {
- this.stopResize?.();
+ this.cancelResize?.();
super.destroy();
}
@@ -187,9 +187,9 @@ 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, {
+ // One resize at a time: another pointer, such as a pen beside the mouse, cancels the first.
+ this.cancelResize?.();
+ this.cancelResize = startResizeGesture(event, {
handle,
axes,
widthTarget: targets.width,
diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts
index 8d4c05b72d..4f1ef79714 100644
--- a/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts
+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts
@@ -46,7 +46,8 @@ const AUTO_SCROLL_STEP = 16;
/**
* Resizes until the pointer pressed in `event` is released. The width grows towards the inline
* end, or on both sides when centered, the height towards the bottom, and the scroll container
- * scrolls near its edges. Returns a function that ends the gesture without calling back.
+ * scrolls near its edges. Returns a function that cancels the gesture as Escape does, while it is
+ * under way.
*/
export function startResizeGesture(event: PointerEvent, options: ResizeGestureOptions) {
const { axes, widthTarget, heightTarget } = options;
@@ -62,6 +63,7 @@ export function startResizeGesture(event: PointerEvent, options: ResizeGestureOp
let x = startX;
let y = startY;
let isMoved = false;
+ let isActive = true;
let frame = 0;
options.handle.setPointerCapture(pointerId);
@@ -132,6 +134,7 @@ export function startResizeGesture(event: PointerEvent, options: ResizeGestureOp
};
const stop = () => {
+ isActive = false;
cancelAnimationFrame(frame);
window.removeEventListener("pointermove", onPointerMove, true);
window.removeEventListener("pointerup", onPointerUp, true);
@@ -150,7 +153,12 @@ export function startResizeGesture(event: PointerEvent, options: ResizeGestureOp
frame = requestAnimationFrame(scrollStep);
}
- return stop;
+ return () => {
+ if (isActive) {
+ stop();
+ options.onCancel();
+ }
+ };
}
/** The width inside the padding of the parent of `element`. */