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/drawing canvas: preserve the size of the original image block
Adorian Doranfdddb865387f
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex 516e17f6db..4589a5ebe8 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -46,6 +46,9 @@ export type BoxSizeValue = typeof BOX_SIZES[number];/** The box sizes that can be resized. Tiny and Full fit their content. */const RESIZABLE_BOX_SIZES: unknown[] = [ 'small', 'medium', 'expandable' ];+/** The least size that an embed can be resized to, in `em`. */+export const MIN_EMBED_SIZE = { width: 10, height: 3 };+/** The flags of an embed, each saved as `"true"` in a `data-*` attribute while it is on. */const FLAG_ATTRIBUTES = [[ 'hideTitle', 'data-hide-title' ],@@ -357,8 +360,8 @@ class ContentEmbedEditing extends Plugin {isHeightResizable: true,isCentered: true,heightTarget: '.include-note-content',- minWidth: 10,- minHeight: 3,+ minWidth: MIN_EMBED_SIZE.width,+ minHeight: MIN_EMBED_SIZE.height,isResizable: embed => RESIZABLE_BOX_SIZES.includes( embed.getAttribute( 'boxSize' ) )} );diff --git a/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts b/packages/ckeditor5/src/plugins/image_to_drawing.spec.tsindex f8bc63378c..fd85d13890 100644--- a/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts+++ b/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts@@ -71,6 +71,26 @@ describe("ImageToDrawing", () => {.toBe(`<paragraph>a</paragraph>${CAPTIONED_IMAGE}`);});+ it("gives the embed the size that the image is shown at, from the least size up", async () => {+ const root = editor.editing.view.getDomRoot();+ if (root) root.style.fontSize = "10px";+ const imageOf = (width: number, height: number) =>+ `[<imageBlock src="${SRC}" width="${width}" height="${height}"></imageBlock>]`;+ const embedOf = (width: string, height: string) => "<contentEmbed attachmentId=\"d1\""+ + ` boxSize="medium" customHeight="${height}" customWidth="${width}" editable="true"`+ + " hideTitle=\"true\"></contentEmbed>";++ setModelData(editor.model, imageOf(300, 150));+ await editor.execute("convertImageToDrawing");+ expect(getModelData(editor.model, { withoutSelection: true }))+ .toBe(embedOf("30em", "15em"));++ setModelData(editor.model, imageOf(40, 20));+ await editor.execute("convertImageToDrawing");+ expect(getModelData(editor.model, { withoutSelection: true }))+ .toBe(embedOf("10em", "3em"));+ });+it("leaves an empty caption behind", async () => {setModelData(editor.model, `[<imageBlock src="${SRC}"><caption></caption></imageBlock>]`);diff --git a/packages/ckeditor5/src/plugins/image_to_drawing.ts b/packages/ckeditor5/src/plugins/image_to_drawing.tsindex 51639dba17..9b745f1956 100644--- a/packages/ckeditor5/src/plugins/image_to_drawing.ts+++ b/packages/ckeditor5/src/plugins/image_to_drawing.ts@@ -1,9 +1,13 @@import { ButtonView, Command, ImageUtils, Plugin, type ModelElement } from "ckeditor5";import bxsPen from "boxicons/svg/solid/bxs-pen.svg?raw";+import { MIN_EMBED_SIZE } from "./content_embed/content_embed.js";+import { SIZE_ATTRIBUTES } from "./resizable_widgets/resizable_widgets.js";+/*** Adds the `convertImageToDrawing` button to the image toolbar. The host attaches a drawing canvas- * that shows the selected block image, which an editable, untitled Medium embed of it replaces.+ * that shows the selected block image, which an editable, untitled Medium embed of it replaces, at+ * the size that the image is shown at.*/export default class ImageToDrawing extends Plugin {@@ -60,6 +64,7 @@ export class ConvertImageToDrawingCommand extends Command {const src = image?.getAttribute("src");if (!image || typeof src !== "string") return;+ const size = this.getShownSize(image);const host = glob.getComponentByEl<EditorComponent>(editor.editing.view.getDomRoot());this.forceDisabled(CONVERTING);let attachmentId: string | null | undefined;@@ -79,7 +84,8 @@ export class ConvertImageToDrawingCommand extends Command {attachmentId,boxSize: "medium",hideTitle: true,- editable: true+ editable: true,+ ...size});const caption = getCaption(image);if (caption && !caption.isEmpty) {@@ -94,6 +100,24 @@ export class ConvertImageToDrawingCommand extends Command {});}+ /** The size that `image` is shown at, as the size attributes of an embed. */+ private getShownSize(image: ModelElement): Record<string, string> {+ const editing = this.editor.editing;+ const viewElement = editing.mapper.toViewElement(image);+ const figure = viewElement && editing.view.domConverter.mapViewToDom(viewElement);+ if (!(figure instanceof HTMLElement)) return {};++ const { width, height } = figure.querySelector("img")?.getBoundingClientRect()+ ?? { width: 0, height: 0 };+ if (!width || !height) return {};++ const em = parseFloat(getComputedStyle(figure).fontSize);+ return {+ [SIZE_ATTRIBUTES.width]: toEm(width / em, MIN_EMBED_SIZE.width),+ [SIZE_ATTRIBUTES.height]: toEm(height / em, MIN_EMBED_SIZE.height)+ };+ }+private getSelectedBlockImage() {const selection = this.editor.model.document.selection;const image = this.editor.plugins.get(ImageUtils).getClosestSelectedImageElement(selection);@@ -111,6 +135,11 @@ function getCaption(image: ModelElement) {return null;}+/** `value` in `em`, rounded to hundredths and no less than `min`. */+function toEm(value: number, min: number) {+ return `${Math.max(Math.round(value * 100) / 100, min)}em`;+}+/** Whether `element` is still in the document, rather than removed while the host worked. */function isInDocument(element: ModelElement) {const root = element.root;