Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/block drag handle: improve coverage

Adorian Doran0469983d79c7
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
index 03c4e92235..a4f7184130 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
@@ -1,6 +1,6 @@
import {
- _getModelData as getModelData, _setModelData as setModelData, type ClassicEditor, Essentials,
- Paragraph
+ _getModelData as getModelData, _setModelData as setModelData, type ClassicEditor, env,
+ Essentials, Paragraph, Table, TableCaption
} from "ckeditor5";
import editorStylesheetUrl from "ckeditor5/ckeditor5.css?url";
import { beforeAll, describe, expect, it } from "vitest";
@@ -25,14 +25,7 @@ describe("BlockDragHandle", () => {
const buttonView = editor.plugins.get(BlockDragHandle).buttonView;
expect(buttonView?.isVisible).toBe(true);
expect(button.getAttribute("draggable")).toBe("true");
-
- const buttonRect = button.getBoundingClientRect();
- const buttonCenter = buttonRect.top + buttonRect.height / 2;
- const editableRect = getEditable(editor).getBoundingClientRect();
- const secondBlockRect = getBlock(editor, 1).getBoundingClientRect();
- expect(buttonRect.right).toBeCloseTo(editableRect.left, 0);
- expect(buttonCenter).toBeGreaterThan(secondBlockRect.top);
- expect(buttonCenter).toBeLessThan(secondBlockRect.bottom);
+ expect(isBesideBlock(editor, button, 1)).toBe(true);
button.focus();
button.click();
@@ -43,46 +36,118 @@ describe("BlockDragHandle", () => {
editor.disableReadOnlyMode("spec");
expect(buttonView?.isVisible).toBe(true);
+ // A caption is in no block.
+ setModelData(editor.model, "<paragraph>one</paragraph><table><tableRow><tableCell>"
+ + "<paragraph>cell</paragraph></tableCell></tableRow>"
+ + "<caption>Cap[]tion</caption></table>");
+ expect(buttonView?.isVisible).toBe(false);
+
getEditable(editor).blur();
await expect.poll(() => buttonView?.isVisible).toBe(false);
});
+ it("follows its block when the window resizes or the page scrolls", async () => {
+ const { editor, button } = await createEditor("<paragraph>on[]e</paragraph>");
+ const editorElement = editor.ui.view.element;
+ // Without a line height in pixels, the first line is measured from the font size.
+ getEditable(editor).style.lineHeight = "normal";
+
+ editorElement?.style.setProperty("margin", "40px 150px");
+ window.dispatchEvent(new Event("resize"));
+ window.dispatchEvent(new Event("resize"));
+ await expect.poll(() => isBesideBlock(editor, button, 0)).toBe(true);
+
+ editorElement?.style.setProperty("margin", "80px 200px");
+ document.dispatchEvent(new Event("scroll"));
+ await expect.poll(() => isBesideBlock(editor, button, 0)).toBe(true);
+ });
+
it("drags the selected block to where it is dropped", async () => {
const { editor, button } = await createEditor(
"<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>"
+ "<paragraph>third</paragraph>"
);
- // A constructed `DataTransfer` ignores `effectAllowed` writes, so every drop is a copy.
- const dataTransfer = new DataTransfer();
- Object.defineProperty(dataTransfer, "effectAllowed", { value: "none", writable: true });
const editableRect = getEditable(editor).getBoundingClientRect();
const lastBlockRect = getBlock(editor, 2).getBoundingClientRect();
- const dropPoint = {
- clientX: editableRect.left - 50,
- clientY: lastBlockRect.bottom - 2
- };
- button.dispatchEvent(new DragEvent("dragstart", { bubbles: true, dataTransfer }));
+ const dataTransfer = startDrag(button);
expect(getModelData(editor.model)).toBe(
"<paragraph>[first]</paragraph><paragraph>second</paragraph>"
+ "<paragraph>third</paragraph>"
);
- const dropInit = { bubbles: true, dataTransfer, ...dropPoint };
- document.dispatchEvent(new DragEvent("dragover", dropInit));
- document.dispatchEvent(new DragEvent("drop", dropInit));
- button.dispatchEvent(new DragEvent("dragend", { bubbles: true, dataTransfer }));
-
+ const isAccepted = drop(button, dataTransfer, {
+ clientX: editableRect.left - 50,
+ clientY: lastBlockRect.bottom - 2
+ });
+ expect(isAccepted).toBe(true);
expect(editor.getData()).toBe("<p>second</p><p>third</p><p>first</p>");
});
+
+ it("mirrors the handle and its drops in a right-to-left editor", async () => {
+ const { editor, button } = await createEditor(
+ "<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>",
+ { language: "ar" }
+ );
+ const editableRect = getEditable(editor).getBoundingClientRect();
+ const lastBlockRect = getBlock(editor, 1).getBoundingClientRect();
+ expect(button.getBoundingClientRect().left).toBeCloseTo(editableRect.right, 0);
+
+ drop(button, startDrag(button), {
+ clientX: editableRect.right + 50,
+ clientY: lastBlockRect.bottom - 2
+ });
+ expect(editor.getData()).toBe("<p>second</p><p>first</p>");
+ });
+
+ it("ignores other drags, and drags that start read-only or end off the editable", async () => {
+ const { editor, button } = await createEditor(
+ "<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>"
+ );
+ const editableRect = getEditable(editor).getBoundingClientRect();
+ const lastBlockRect = getBlock(editor, 1).getBoundingClientRect();
+ const margin = { clientX: editableRect.left - 50, clientY: lastBlockRect.bottom - 2 };
+
+ const text = new DataTransfer();
+ text.setData("text/plain", "dropped");
+ const textInit = { bubbles: true, dataTransfer: text, ...margin };
+ document.dispatchEvent(new DragEvent("dragover", textInit));
+ document.dispatchEvent(new DragEvent("drop", textInit));
+
+ editor.enableReadOnlyMode("spec");
+ const readOnlyStart = new DragEvent("dragstart", {
+ bubbles: true, cancelable: true, dataTransfer: new DataTransfer()
+ });
+ button.dispatchEvent(readOnlyStart);
+ expect(readOnlyStart.defaultPrevented).toBe(true);
+ editor.disableReadOnlyMode("spec");
+
+ const offEditable = { ...margin, clientY: editableRect.bottom + 50 };
+ expect(drop(button, startDrag(button), offEditable)).toBe(false);
+ expect(editor.getData()).toBe("<p>first</p><p>second</p>");
+ });
+
+ it("adds no handle on Android, where the editor cannot drag", async () => {
+ const { isAndroid } = env;
+ Object.defineProperty(env, "isAndroid", { value: true });
+ try {
+ const editor = await createTestEditor([ Essentials, Paragraph, BlockDragHandle ]);
+ expect(editor.plugins.get(BlockDragHandle).buttonView).toBeUndefined();
+ } finally {
+ Object.defineProperty(env, "isAndroid", { value: isAndroid });
+ }
+ });
});
-async function createEditor(modelData: string) {
- const editor = await createTestEditor([Essentials, Paragraph, BlockDragHandle]);
- const editorElement = editor.ui.view.element;
- if (editorElement) {
- editorElement.style.margin = "0 100px";
- }
+async function createEditor(
+ modelData: string,
+ config: Parameters<typeof createTestEditor>[1] = {}
+) {
+ const editor = await createTestEditor(
+ [ Essentials, Paragraph, Table, TableCaption, BlockDragHandle ],
+ config
+ );
+ editor.ui.view.element?.style.setProperty("margin", "0 100px");
setModelData(editor.model, modelData);
editor.editing.view.focus();
await expect.poll(() => editor.ui.focusTracker.isFocused).toBe(true);
@@ -96,6 +161,40 @@ async function createEditor(modelData: string) {
return { editor, button };
}
+/** Whether `button` is left of the editable, level with the first line of the block at `index`. */
+function isBesideBlock(editor: ClassicEditor, button: HTMLElement, index: number) {
+ const buttonRect = button.getBoundingClientRect();
+ const buttonCenter = buttonRect.top + buttonRect.height / 2;
+ const blockRect = getBlock(editor, index).getBoundingClientRect();
+ const editableLeft = getEditable(editor).getBoundingClientRect().left;
+ return Math.abs(buttonRect.right - editableLeft) < 1
+ && buttonCenter > blockRect.top
+ && buttonCenter < blockRect.bottom;
+}
+
+/** Starts a drag at `button`, with a `DataTransfer` that the drops reuse. */
+function startDrag(button: HTMLElement) {
+ // A constructed `DataTransfer` ignores `effectAllowed` writes, so every drop is a copy.
+ const dataTransfer = new DataTransfer();
+ Object.defineProperty(dataTransfer, "effectAllowed", { value: "none", writable: true });
+ button.dispatchEvent(new DragEvent("dragstart", { bubbles: true, dataTransfer }));
+ return dataTransfer;
+}
+
+/** Drags over `point`, drops there and ends the drag. Returns whether the drag over was accepted. */
+function drop(
+ button: HTMLElement,
+ dataTransfer: DataTransfer,
+ point: { clientX: number; clientY: number }
+) {
+ const dropInit = { bubbles: true, cancelable: true, dataTransfer, ...point };
+ const dragover = new DragEvent("dragover", dropInit);
+ document.dispatchEvent(dragover);
+ document.dispatchEvent(new DragEvent("drop", dropInit));
+ button.dispatchEvent(new DragEvent("dragend", { bubbles: true, dataTransfer }));
+ return dragover.defaultPrevented;
+}
+
function getEditable(editor: ClassicEditor) {
const editable = editor.ui.getEditableElement();
if (!editable) {
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.ts b/packages/ckeditor5/src/plugins/block_drag_handle.ts
index 6b1aaf9406..ee91b7e472 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.ts
@@ -37,16 +37,18 @@ export default class BlockDragHandle extends Plugin {
tooltip: true,
isToggleable: false
});
+ buttonView.extendTemplate({
+ attributes: { draggable: "true" },
+ on: {
+ dragstart: buttonView.bindTemplate.to((domEvent) => {
+ this.startDrag(domEvent as DragEvent);
+ })
+ }
+ });
buttonView.on("execute", () => editor.editing.view.focus());
editor.ui.view.body.add(buttonView);
this.buttonView = buttonView;
- const element = buttonView.element;
- if (!element) {
- return;
- }
- element.setAttribute("draggable", "true");
-
this.listenTo(editor.ui, "update", () => this.update());
this.listenTo(editor, "change:isReadOnly", () => this.update(), { priority: "low" });
this.listenTo(editor.ui.focusTracker, "change:isFocused", () => this.update());
@@ -57,9 +59,6 @@ export default class BlockDragHandle extends Plugin {
usePassive: true
});
- this.domEmitter.listenTo(element, "dragstart", (_evt, domEvent: DragEvent) => {
- this.startDrag(domEvent);
- });
this.domEmitter.listenTo(document, "dragover", (_evt, domEvent: DragEvent) => {
this.forwardDrag(domEvent);
});