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/content embed: center horizontally when resized
Adorian Doran60d7faeff0d1
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 9944c5d099..2970566ddf 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1578,6 +1578,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t/* Tiny and Full fit their content: only these box sizes take a resized size. */.include-note:is([data-box-size="small"], [data-box-size="medium"], [data-box-size="expandable"]) {+ margin-inline: auto;max-width: 100%;width: var(--include-note-width, auto);diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.htmlindex 3e7e2e5baa..9c38869dfc 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html@@ -54,7 +54,9 @@the include first to show them.</li><li>While the height changes, moving the handle near the top or the bottomof the note scrolls the note.</li>- <li>Making an include as wide as the note returns it to the full width.</li>+ <li>An include narrower than the note is centered, so changing its width moves+ both of its sides. Making it as wide as the note returns it to the full+ width.</li><li>To cancel a resize, press <kbd>Esc</kbd> before releasing the handle.</li><li>To reset the size, double-click a handle, or double-tap it on a touchscreen. The corner handle resets both the width and the height.</li>diff --git a/docs/User Guide/User Guide/Note Types/Text/Include Note.md b/docs/User Guide/User Guide/Note Types/Text/Include Note.mdindex 2833938fa8..6f2ce9aa9e 100644--- a/docs/User Guide/User Guide/Note Types/Text/Include Note.md+++ b/docs/User Guide/User Guide/Note Types/Text/Include Note.md@@ -36,7 +36,7 @@ A _Small_, _Medium_ or _Expandable_ include can be resized to any width and heig* To resize an include, select it and drag one of its handles: the handle on its right edge changes the width, the handle on its bottom edge changes the height, and the handle in its bottom-right corner changes both. In right-to-left text, the width handle is on the left edge. With a mouse, the handles also show while the pointer is over the include.* The handles work with a mouse, a pen or a finger. On a touch screen, tap the include first to show them.* While the height changes, moving the handle near the top or the bottom of the note scrolls the note.-* Making an include as wide as the note returns it to the full width.+* An include narrower than the note is centered, so changing its width moves both of its sides. Making it as wide as the note returns it to the full width.* To cancel a resize, press <kbd>Esc</kbd> before releasing the handle.* To reset the size, double-click a handle, or double-tap it on a touch screen. The corner handle resets both the width and the height.* Choosing a box size resets the height. Choosing _Tiny_ or _Full_ resets the width too.diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex 847e863bac..8583f7620d 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -299,6 +299,7 @@ class ContentEmbedEditing extends Plugin {propertyPrefix: '--include-note',isWidthResizable: true,isHeightResizable: true,+ isCentered: true,heightTarget: '.include-note-content',minWidth: 10,minHeight: 3,diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.tsindex 188dcc5dc3..459129d2cb 100644--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.spec.ts@@ -19,6 +19,7 @@ const STYLES = `.test-box { width: var(--test-width, auto); }.test-box-content { height: var(--test-height, 50px); }.test-rtl .test-box { direction: rtl; }+ .test-centered .test-box { margin-inline: auto; }.test-scroller .ck.ck-editor__main { height: 150px; overflow-y: auto; }.test-scroller .ck.ck-editor__editable { height: 1000px; }`;@@ -38,7 +39,7 @@ describe("ResizableWidgets", () => {registerTestCleanup(() => {style.remove();capture.mockRestore();- document.body.classList.remove("test-rtl", "test-scroller");+ document.body.classList.remove("test-rtl", "test-scroller", "test-centered");});});@@ -209,6 +210,21 @@ describe("ResizableWidgets", () => {expect(getSize()).toBe("<testBox customWidth=\"35em\"></testBox>");});+ it("keeps the dragged edge of a centered widget under the pointer", async () => {+ document.body.classList.add("test-centered");+ await createEditor({ isCentered: true });+ editor.setData("<div class=\"test-box\"></div>");+ const { left, right } = getBox().getBoundingClientRect();++ press(getHandle("width"));+ move(50, 300);+ const resized = getBox().getBoundingClientRect();+ expect([ resized.left - left, resized.right - right ]).toEqual([ 50, -50 ]);+ release();++ expect(getSize()).toBe("<testBox customWidth=\"30em\"></testBox>");+ });+it("cancels on Escape or a pointercancel, and ignores other pointers", async () => {await createEditor();editor.setData(RESIZED);diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.tsindex 531cb0fc27..30fd046db0 100644--- a/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resizable_widgets.ts@@ -21,6 +21,8 @@ export interface ResizableWidgetConfig {propertyPrefix: string;isWidthResizable?: boolean;isHeightResizable?: boolean;+ /** Whether the widget's stylesheet centers it, so that its width changes on both sides. */+ isCentered?: boolean;/** A selector for the element inside the widget whose height changes, or the widget. */heightTarget?: string;/** The smallest width, in `em`. */@@ -182,6 +184,7 @@ export default class ResizableWidgets extends Plugin {handle,axes,widthTarget: targets.width,+ isCentered: !!config.isCentered,heightTarget: targets.height,minWidth: toPixels(config.minWidth ?? DEFAULT_MIN_SIZE, targets.width),minHeight: toPixels(config.minHeight ?? DEFAULT_MIN_SIZE, targets.height),diff --git a/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts b/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.tsindex 7df5966ca7..443230de11 100644--- a/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts+++ b/packages/ckeditor5/src/plugins/resizable_widgets/resize_gesture.ts@@ -16,6 +16,8 @@ export interface ResizeGestureOptions {axes: ResizeAxes;/** The element whose width changes. Its width is full once it reaches its parent's. */widthTarget: HTMLElement;+ /** Whether the width target is centered, so that its width changes on both sides. */+ isCentered: boolean;/** The element whose height changes. */heightTarget: HTMLElement;/** The smallest width, in pixels. */@@ -43,7 +45,8 @@ const AUTO_SCROLL_STEP = 16;/*** Resizes until the pointer pressed in `event` is released. The width grows towards the inline- * end, the height towards the bottom, and the scroll container scrolls near its edges.+ * end, or on both sides when centered, the height towards the bottom, and the scroll container+ * scrolls near its edges.*/export function startResizeGesture(event: PointerEvent, options: ResizeGestureOptions) {const { axes, widthTarget, heightTarget } = options;@@ -52,6 +55,7 @@ export function startResizeGesture(event: PointerEvent, options: ResizeGestureOpconst startHeight = parseFloat(getComputedStyle(heightTarget).height);const maxWidth = getContainerWidth(widthTarget);const inlineDirection = getComputedStyle(widthTarget).direction === "rtl" ? -1 : 1;+ const widthFactor = inlineDirection * (options.isCentered ? 2 : 1);const scroller = findScrollContainer(heightTarget);const startScrollTop = scroller.scrollTop;@@ -65,7 +69,7 @@ export function startResizeGesture(event: PointerEvent, options: ResizeGestureOpconst getSize = () => {const size: ResizeSize = {};if (axes.width) {- const width = Math.max(startWidth + (x - startX) * inlineDirection, options.minWidth);+ const width = Math.max(startWidth + (x - startX) * widthFactor, options.minWidth);size.width = width >= maxWidth ? null : width;}if (axes.height) {