From 1756bca212a70d380ca0db3285f5d081d8d9610e Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Wed, 21 Jul 2021 02:15:10 +0200 Subject: [PATCH] Add image-handle-dimensions to show dimensions directly on image --- ts/editor/ImageHandle.svelte | 62 ++++++++++++++++++++++++--- ts/editor/ImageHandleContainer.svelte | 17 -------- ts/editor/editing-area.ts | 26 +++-------- 3 files changed, 63 insertions(+), 42 deletions(-) delete mode 100644 ts/editor/ImageHandleContainer.svelte diff --git a/ts/editor/ImageHandle.svelte b/ts/editor/ImageHandle.svelte index 68e8f73b8..098c3fc92 100644 --- a/ts/editor/ImageHandle.svelte +++ b/ts/editor/ImageHandle.svelte @@ -6,12 +6,41 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import { getContext } from "svelte"; import { nightModeKey } from "components/context-keys"; - export let hidden: boolean; + export let image: HTMLImageElement | null = null; + export let container: HTMLElement | null = null; - export let top: number = 0; - export let left: number = 0; - export let width: number = 0; - export let height: number = 0; + let hidden = true; + + let naturalWidth = 0; + let naturalHeight = 0; + + let containerTop = 0; + let containerLeft = 0; + + let top = 0; + let left = 0; + let width = 0; + let height = 0; + + $: if (image) { + const imageRect = image.getBoundingClientRect(); + const containerRect = ( + container ?? document.documentElement + ).getBoundingClientRect(); + + naturalWidth = image.naturalWidth; + naturalHeight = image.naturalHeight; + + (containerTop = containerRect.top), + (containerLeft = containerRect.left), + (top = imageRect.top - containerTop), + (left = imageRect.left - containerLeft), + (width = image.clientWidth), + (height = image.clientHeight), + (hidden = false); + } else { + hidden = true; + } function setPointerCapture(event: PointerEvent): void { if (event.pointerId !== 1) { @@ -25,6 +54,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html if (!(event.target as Element).hasPointerCapture(event.pointerId)) { return; } + + const dragWidth = event.clientX - containerLeft - left; + const dragHeight = event.clientY - containerTop - top; + + width = dragWidth; + image!.style.width = `${dragWidth}px`; + height = dragHeight; + image!.style.height = `${dragHeight}px`; } const nightMode = getContext(nightModeKey); @@ -36,6 +73,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html {hidden} >
+
+ {width}×{height} (Original: {naturalWidth}×{naturalHeight}) +
@@ -66,6 +106,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html opacity: 0.2; } + .image-handle-dimensions { + pointer-events: none; + user-select: none; + bottom: 3px; + right: 3px; + + background-color: rgba(0 0 0 / 0.3); + border-color: black; + border-radius: 0.25rem; + padding: 0 5px; + } + .image-handle-control { width: 7px; height: 7px; diff --git a/ts/editor/ImageHandleContainer.svelte b/ts/editor/ImageHandleContainer.svelte deleted file mode 100644 index c3f2b640d..000000000 --- a/ts/editor/ImageHandleContainer.svelte +++ /dev/null @@ -1,17 +0,0 @@ - - - -
- -
diff --git a/ts/editor/editing-area.ts b/ts/editor/editing-area.ts index efc754f93..ecee9ac1a 100644 --- a/ts/editor/editing-area.ts +++ b/ts/editor/editing-area.ts @@ -5,7 +5,7 @@ @typescript-eslint/no-non-null-assertion: "off", */ -import ImageHandleContainer from "./ImageHandleContainer.svelte"; +import ImageHandle from "./ImageHandle.svelte"; import type { EditableContainer } from "./editable-container"; import type { Editable } from "./editable"; @@ -22,7 +22,7 @@ function onCutOrCopy(): void { } export class EditingArea extends HTMLDivElement { - imageHandle: ImageHandleContainer; + imageHandle: ImageHandle; editableContainer: EditableContainer; editable: Editable; codable: Codable; @@ -44,16 +44,10 @@ export class EditingArea extends HTMLDivElement { document.documentElement.classList.contains("night-mode") ); - this.imageHandle = new ImageHandleContainer({ + this.imageHandle = new ImageHandle({ target: this, anchor: this.editableContainer, - props: { - hidden: true, - top: 0, - left: 0, - width: 0, - height: 0, - }, + props: { container: this.editable }, context, } as any); @@ -172,20 +166,12 @@ export class EditingArea extends HTMLDivElement { showImageHandle(event: MouseEvent): void { if (event.target instanceof HTMLImageElement) { - const image = event.target; - const imageRect = image.getBoundingClientRect(); - const editableRect = this.editable.getBoundingClientRect(); - (this.imageHandle as any).$set({ - hidden: false, - top: imageRect.top - editableRect.top, - left: imageRect.left - editableRect.left, - width: image.clientWidth, - height: image.clientHeight, + image: event.target, }); } else { (this.imageHandle as any).$set({ - hidden: true, + image: null, }); } }