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
client/text editor: show a content hint for the resize corner dot
Adorian Doran37b007a1571c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 47a040765b..ef1cdee34c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4511,6 +4511,7 @@"card": "Card","caution": "Caution","changes": "Changes",+ "click-and-drag-to-resize-the-block-double-click-to-reset-to-the-default-size": "Click and drag to resize the block. Double-click to reset to the default size.","click-on-the-arrow-or-press-0-to-collapse-expand": "Click on the arrow or press %0 to collapse/expand.","collapsible-block": "Collapsible block","convert-image-to-a-drawing-canvas": "Convert image to a drawing canvas",diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.tsindex 5840208f13..9107fe517e 100644--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts@@ -48,7 +48,8 @@ describe("ResizableWidgets", () => {async function createEditor(config: Partial<ResizableWidgetConfig> = {},- extraPlugins: Array<typeof Plugin> = []+ extraPlugins: Array<typeof Plugin> = [],+ editorConfig: Record<string, unknown> = {}) {class ResizableTestBox extends Plugin {static get requires() {@@ -81,7 +82,7 @@ describe("ResizableWidgets", () => {editor = await createTestEditor([Essentials, Paragraph, Undo, ResizableTestBox, ...extraPlugins- ]);+ ], editorConfig);return editor;}@@ -499,6 +500,62 @@ describe("ResizableWidgets", () => {expect(getSize()).toBe("<testBox></testBox>");});+ it("shows a hint while the pointer rests on the corner, until it leaves or presses", async () => {+ const getHint = () => document+ .querySelector(".tooltip.content-hint.text-editor-content-tooltip")?.textContent ?? null;+ const enter = (name: string) => getHandle(name)+ .dispatchEvent(new PointerEvent("pointerenter"));+ await createEditor();+ editor.setData(RESIZED);+ const box = editor.model.document.getRoot()?.getChild(0);+ if (!box) {+ throw new Error("Expected a box.");+ }+ editor.model.change((writer) => writer.setSelection(box, "on"));+ editor.editing.view.focus();+ await expect.poll(() => getComputedStyle(getHandle("corner")).display).toBe("block");+ vi.useFakeTimers({ toFake: [ "setTimeout", "clearTimeout" ] });+ registerTestCleanup(() => vi.useRealTimers());++ enter("corner");+ vi.advanceTimersByTime(200);+ expect(getHint()).toBe("Click and drag to resize the block. "+ + "Double-click to reset to the default size.");+ editor.editing.view.forceRender();+ vi.advanceTimersByTime(60_000);+ expect(getHint()).not.toBeNull();+ press(getHandle("corner"));+ expect(getHint()).toBeNull();+ release();++ enter("corner");+ vi.advanceTimersByTime(200);+ getHandle("corner").dispatchEvent(new PointerEvent("pointerleave"));+ expect(getHint()).toBeNull();++ // A touch presses during the dwell, and only the corner has a hint.+ enter("corner");+ press(getHandle("corner"));+ release();+ enter("width");+ vi.advanceTimersByTime(200);+ expect(getHint()).toBeNull();++ // A removed handle gets no `pointerleave`.+ enter("corner");+ vi.advanceTimersByTime(200);+ expect(getHint()).not.toBeNull();+ editor.setData("<p>text</p>");+ expect(getHint()).toBeNull();++ vi.useRealTimers();+ await createEditor({}, [], { contentHintsEnabled: false });+ editor.setData(RESIZED);+ enter("corner");+ await sleep(300);+ expect(getHint()).toBeNull();+ });+it("resizes the height of the widget itself without a height target", async () => {await createEditor({ heightTarget: undefined });editor.setData("<div class=\"test-box\"></div>");@@ -632,3 +689,7 @@ function tap(target: Element) {function nextFrame() {return new Promise((resolve) => requestAnimationFrame(resolve));}++function sleep(ms: number) {+ return new Promise((resolve) => setTimeout(resolve, ms));+}diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.tsindex 7e631ba1f7..9e5917e8e5 100644--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts@@ -10,6 +10,7 @@ import {WidgetToolbarRepository} from "ckeditor5";+import { ContentHintManager, type HintHandle } from "../../content_hint_manager.js";import {type ResizeAnchor, type ResizeAxes, type ResizeSize, startResizeGesture} from "./resize_gesture.js";@@ -72,6 +73,7 @@ const RESIZING_CLASS = "ck-widget_resizing";const TOOLBAR_LOCK = "ResizableWidgets";const DEFAULT_MIN_SIZE = 2;const DOUBLE_TAP_MS = 400;+const HINT_DWELL_MS = 200;const EM_PATTERN = /^\d+(\.\d+)?em$/;/**@@ -84,11 +86,31 @@ export default class ResizableWidgets extends Plugin {private lastTap: { handle: Element; time: number } | null = null;/** Cancels the resize under way, if any. */private cancelResize: (() => void) | null = null;+ /** Shows the hints of the corner handles, or `null` while content hints are switched off. */+ private hintManager: ContentHintManager | null = null;+ /** The corner handle that the pointer rests on, and its hint. */+ private hoveredHint: { handle: HTMLElement; hint: HintHandle } | null = null;static get pluginName() {return "ResizableWidgets" as const;}+ init() {+ if (this.editor.config.get("contentHintsEnabled") !== false) {+ this.hintManager = new ContentHintManager({+ tooltipOptions: { customClass: "text-editor-content-tooltip" }+ });+ // The editor can hide or remove the hovered handle without a `pointerleave`.+ this.listenTo(this.editor.editing.view, "render", () => {+ const hovered = this.hoveredHint;+ if (hovered && !hovered.handle.checkVisibility()) {+ hovered.hint.hide();+ this.hoveredHint = null;+ }+ });+ }+ }+/** Makes the widgets of `modelName` resizable. Call it from `init()` of the widget's plugin. */register(modelName: string, config: ResizableWidgetConfig) {const editor = this.editor;@@ -147,6 +169,7 @@ export default class ResizableWidgets extends Plugin {override destroy() {this.cancelResize?.();+ this.hintManager?.destroy();super.destroy();}@@ -155,6 +178,7 @@ export default class ResizableWidgets extends Plugin {const startResize = (event: PointerEvent, handle: HTMLElement, name: string) => {this.startResize(event, handle, HANDLES[name], subject);};+ const addHint = (handle: HTMLElement) => this.addCornerHint(handle);return writer.createUIElement("div", {class: "ck-widget__resize-handles",@@ -166,12 +190,38 @@ export default class ResizableWidgets extends Plugin {const handle = domDocument.createElement("div");handle.className = `ck-widget__resize-handle ck-widget__resize-handle_${name}`;handle.addEventListener("pointerdown", (event) => startResize(event, handle, name));+ if (name === "corner") {+ addHint(handle);+ }domElement.append(handle);}return domElement;});}+ /** Shows the hint of `handle` while the pointer rests on it, until it is pressed. */+ private addCornerHint(handle: HTMLElement) {+ const t = this.editor.t;+ const hint = this.hintManager?.createHandle(handle, t(+ "Click and drag to resize the block. Double-click to reset to the default size."+ ));+ if (!hint) {+ return;+ }++ const hide = () => {+ this.hoveredHint = null;+ hint.hide();+ };+ // A tap enters before its `pointerdown` cancels the dwell; its `mouseenter` comes after.+ handle.addEventListener("pointerenter", () => {+ this.hoveredHint = { handle, hint };+ hint.showAfter(HINT_DWELL_MS);+ });+ handle.addEventListener("pointerleave", hide);+ handle.addEventListener("pointerdown", hide);+ }+private startResize(event: PointerEvent,handle: HTMLElement,