2021-07-21 01:32:09 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-07-28 20:17:31 +02:00
|
|
|
<script lang="ts">
|
2021-08-06 02:19:36 +02:00
|
|
|
import WithDropdown from "components/WithDropdown.svelte";
|
|
|
|
import ButtonDropdown from "components/ButtonDropdown.svelte";
|
|
|
|
import Item from "components/Item.svelte";
|
|
|
|
|
|
|
|
import WithImageConstrained from "./WithImageConstrained.svelte";
|
2021-08-05 02:58:52 +02:00
|
|
|
import HandleBackground from "./HandleBackground.svelte";
|
2021-08-04 22:25:05 +02:00
|
|
|
import HandleSelection from "./HandleSelection.svelte";
|
2021-08-04 22:58:24 +02:00
|
|
|
import HandleControl from "./HandleControl.svelte";
|
2021-08-05 03:24:04 +02:00
|
|
|
import HandleLabel from "./HandleLabel.svelte";
|
2021-09-02 21:01:29 +02:00
|
|
|
import ImageHandleFloatButtons from "./ImageHandleFloatButtons.svelte";
|
2021-07-21 14:13:56 +02:00
|
|
|
import ImageHandleSizeSelect from "./ImageHandleSizeSelect.svelte";
|
|
|
|
|
2021-08-05 04:07:04 +02:00
|
|
|
import { onDestroy } from "svelte";
|
2021-07-21 01:32:09 +02:00
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
export let container: HTMLElement;
|
2021-07-28 20:17:31 +02:00
|
|
|
export let sheet: CSSStyleSheet;
|
|
|
|
export let activeImage: HTMLImageElement | null = null;
|
|
|
|
export let isRtl: boolean = false;
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-28 20:17:31 +02:00
|
|
|
$: naturalWidth = activeImage?.naturalWidth;
|
|
|
|
$: naturalHeight = activeImage?.naturalHeight;
|
2021-07-26 22:31:47 +02:00
|
|
|
$: aspectRatio = naturalWidth && naturalHeight ? naturalWidth / naturalHeight : NaN;
|
2021-07-21 15:52:48 +02:00
|
|
|
|
2021-08-05 03:24:04 +02:00
|
|
|
let customDimensions: boolean = false;
|
2021-07-30 20:37:10 +02:00
|
|
|
let actualWidth = "";
|
|
|
|
let actualHeight = "";
|
2021-07-21 18:22:09 +02:00
|
|
|
|
2021-08-05 03:24:04 +02:00
|
|
|
function updateDimensions() {
|
2021-07-26 22:31:47 +02:00
|
|
|
/* we do not want the actual width, but rather the intended display width */
|
2021-07-29 20:37:51 +02:00
|
|
|
const widthAttribute = activeImage!.getAttribute("width");
|
2021-07-21 19:03:21 +02:00
|
|
|
customDimensions = false;
|
|
|
|
|
2021-07-29 20:37:51 +02:00
|
|
|
if (widthAttribute) {
|
|
|
|
actualWidth = widthAttribute;
|
2021-07-21 19:03:21 +02:00
|
|
|
customDimensions = true;
|
2021-07-21 18:32:07 +02:00
|
|
|
} else {
|
2021-07-21 19:03:21 +02:00
|
|
|
actualWidth = String(naturalWidth);
|
2021-07-21 18:32:07 +02:00
|
|
|
}
|
2021-07-21 18:22:09 +02:00
|
|
|
|
2021-07-29 20:37:51 +02:00
|
|
|
const heightAttribute = activeImage!.getAttribute("height");
|
|
|
|
if (heightAttribute) {
|
|
|
|
actualHeight = heightAttribute;
|
2021-07-21 19:03:21 +02:00
|
|
|
customDimensions = true;
|
2021-07-29 20:37:51 +02:00
|
|
|
} else if (customDimensions) {
|
|
|
|
actualHeight = String(Math.trunc(Number(actualWidth) / aspectRatio));
|
2021-07-21 18:32:07 +02:00
|
|
|
} else {
|
2021-07-21 19:03:21 +02:00
|
|
|
actualHeight = String(naturalHeight);
|
2021-07-21 18:32:07 +02:00
|
|
|
}
|
2021-07-30 02:07:41 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 22:48:36 +02:00
|
|
|
let updateSelection: () => void;
|
|
|
|
|
2021-07-30 02:07:41 +02:00
|
|
|
async function updateSizesWithDimensions() {
|
2021-08-05 22:48:36 +02:00
|
|
|
updateSelection();
|
2021-08-05 03:24:04 +02:00
|
|
|
updateDimensions();
|
2021-07-30 02:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* window resizing */
|
|
|
|
const resizeObserver = new ResizeObserver(async () => {
|
2021-09-02 20:03:12 +02:00
|
|
|
await updateSizesWithDimensions();
|
2021-07-30 02:07:41 +02:00
|
|
|
});
|
|
|
|
|
2021-09-02 20:03:12 +02:00
|
|
|
$: observes = Boolean(activeImage);
|
|
|
|
$: if (observes) {
|
2021-09-02 21:01:29 +02:00
|
|
|
resizeObserver.observe(container);
|
2021-09-02 20:03:12 +02:00
|
|
|
} else {
|
2021-09-02 21:01:29 +02:00
|
|
|
resizeObserver.unobserve(container);
|
2021-09-02 20:03:12 +02:00
|
|
|
}
|
2021-07-30 02:07:41 +02:00
|
|
|
|
2021-07-28 20:17:31 +02:00
|
|
|
/* memoized position of image on resize start
|
|
|
|
* prevents frantic behavior when image shift into the next/previous line */
|
|
|
|
let getDragWidth: (event: PointerEvent) => number;
|
|
|
|
let getDragHeight: (event: PointerEvent) => number;
|
2021-07-21 01:32:09 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
function setPointerCapture({ detail }: CustomEvent): void {
|
2021-09-02 20:03:12 +02:00
|
|
|
const pointerId = detail.originalEvent.pointerId;
|
|
|
|
|
|
|
|
if (pointerId !== 1) {
|
2021-08-04 22:58:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageRect = activeImage!.getBoundingClientRect();
|
2021-07-28 20:17:31 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
const imageLeft = imageRect!.left;
|
|
|
|
const imageRight = imageRect!.right;
|
|
|
|
const [multX, imageX] = detail.west ? [-1, imageRight] : [1, -imageLeft];
|
2021-07-28 20:17:31 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
getDragWidth = ({ clientX }) => multX * clientX + imageX;
|
2021-07-28 23:41:46 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
const imageTop = imageRect!.top;
|
|
|
|
const imageBottom = imageRect!.bottom;
|
|
|
|
const [multY, imageY] = detail.north ? [-1, imageBottom] : [1, -imageTop];
|
2021-07-28 23:41:46 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
getDragHeight = ({ clientY }) => multY * clientY + imageY;
|
2021-07-28 23:41:46 +02:00
|
|
|
|
2021-08-04 22:58:24 +02:00
|
|
|
const target = detail.originalEvent.target as Element;
|
2021-09-02 20:03:12 +02:00
|
|
|
target.setPointerCapture(pointerId);
|
2021-08-04 22:58:24 +02:00
|
|
|
}
|
2021-07-21 01:32:09 +02:00
|
|
|
|
2021-07-30 01:03:21 +02:00
|
|
|
$: [minResizeWidth, minResizeHeight] =
|
|
|
|
aspectRatio > 1 ? [5 * aspectRatio, 5] : [5, 5 / aspectRatio];
|
|
|
|
|
2021-07-30 02:07:41 +02:00
|
|
|
async function resize(event: PointerEvent) {
|
2021-07-21 17:37:58 +02:00
|
|
|
const element = event.target! as Element;
|
|
|
|
|
|
|
|
if (!element.hasPointerCapture(event.pointerId)) {
|
2021-07-21 01:32:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-28 20:17:31 +02:00
|
|
|
const dragWidth = getDragWidth(event);
|
|
|
|
const dragHeight = getDragHeight(event);
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-26 22:31:47 +02:00
|
|
|
const widthIncrease = dragWidth / naturalWidth!;
|
|
|
|
const heightIncrease = dragHeight / naturalHeight!;
|
2021-07-21 02:37:07 +02:00
|
|
|
|
2021-08-05 22:48:36 +02:00
|
|
|
let width: number;
|
|
|
|
|
2021-07-26 23:09:14 +02:00
|
|
|
if (widthIncrease > heightIncrease) {
|
2021-07-30 01:03:21 +02:00
|
|
|
width = Math.max(Math.trunc(dragWidth), minResizeWidth);
|
2021-07-21 02:37:07 +02:00
|
|
|
} else {
|
2021-08-05 22:48:36 +02:00
|
|
|
let height = Math.max(Math.trunc(dragHeight), minResizeHeight);
|
2021-07-27 16:54:23 +02:00
|
|
|
width = Math.trunc(naturalWidth! * (height / naturalHeight!));
|
2021-07-21 02:37:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 20:17:31 +02:00
|
|
|
activeImage!.width = width;
|
2021-07-21 01:32:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 20:17:31 +02:00
|
|
|
onDestroy(() => resizeObserver.disconnect());
|
2021-07-21 01:32:09 +02:00
|
|
|
</script>
|
|
|
|
|
2021-08-06 02:19:36 +02:00
|
|
|
{#if sheet}
|
2021-08-06 03:53:44 +02:00
|
|
|
<WithDropdown
|
|
|
|
placement="bottom"
|
|
|
|
autoOpen={true}
|
|
|
|
autoClose={false}
|
|
|
|
let:createDropdown
|
|
|
|
let:dropdownObject
|
2021-08-06 02:19:36 +02:00
|
|
|
>
|
2021-08-06 03:53:44 +02:00
|
|
|
<WithImageConstrained
|
|
|
|
{sheet}
|
|
|
|
{container}
|
|
|
|
{activeImage}
|
|
|
|
on:update={() => {
|
|
|
|
updateSizesWithDimensions();
|
|
|
|
dropdownObject.update();
|
|
|
|
}}
|
|
|
|
let:toggleActualSize
|
|
|
|
let:active
|
|
|
|
>
|
|
|
|
{#if activeImage}
|
2021-08-06 02:19:36 +02:00
|
|
|
<HandleSelection
|
|
|
|
bind:updateSelection
|
|
|
|
{container}
|
|
|
|
image={activeImage}
|
|
|
|
on:mount={(event) => createDropdown(event.detail.selection)}
|
|
|
|
>
|
2021-09-02 21:01:29 +02:00
|
|
|
<HandleBackground on:dblclick={toggleActualSize} />
|
2021-08-06 02:19:36 +02:00
|
|
|
|
|
|
|
<HandleLabel {isRtl} on:mount={updateDimensions}>
|
|
|
|
<span>{actualWidth}×{actualHeight}</span>
|
|
|
|
{#if customDimensions}
|
|
|
|
<span>(Original: {naturalWidth}×{naturalHeight})</span
|
|
|
|
>
|
|
|
|
{/if}
|
|
|
|
</HandleLabel>
|
|
|
|
|
|
|
|
<HandleControl
|
|
|
|
{active}
|
2021-08-31 22:52:23 +02:00
|
|
|
activeSize={8}
|
2021-08-06 02:19:36 +02:00
|
|
|
offsetX={5}
|
|
|
|
offsetY={5}
|
|
|
|
on:pointerclick={(event) => {
|
|
|
|
if (active) {
|
|
|
|
setPointerCapture(event);
|
|
|
|
}
|
|
|
|
}}
|
2021-08-06 03:53:44 +02:00
|
|
|
on:pointermove={(event) => {
|
|
|
|
resize(event);
|
|
|
|
updateSizesWithDimensions();
|
|
|
|
dropdownObject.update();
|
|
|
|
}}
|
2021-08-06 02:19:36 +02:00
|
|
|
/>
|
|
|
|
</HandleSelection>
|
|
|
|
<ButtonDropdown>
|
|
|
|
<div on:click={updateSizesWithDimensions}>
|
|
|
|
<Item>
|
2021-09-02 21:01:29 +02:00
|
|
|
<ImageHandleFloatButtons
|
2021-08-06 03:53:44 +02:00
|
|
|
image={activeImage}
|
|
|
|
{isRtl}
|
|
|
|
on:update={dropdownObject.update}
|
|
|
|
/>
|
2021-08-06 02:19:36 +02:00
|
|
|
</Item>
|
|
|
|
<Item>
|
|
|
|
<ImageHandleSizeSelect
|
|
|
|
{active}
|
|
|
|
{isRtl}
|
|
|
|
on:click={toggleActualSize}
|
|
|
|
/>
|
|
|
|
</Item>
|
|
|
|
</div>
|
|
|
|
</ButtonDropdown>
|
2021-08-06 03:53:44 +02:00
|
|
|
{/if}
|
|
|
|
</WithImageConstrained>
|
|
|
|
</WithDropdown>
|
2021-08-06 02:19:36 +02:00
|
|
|
{/if}
|