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
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
2021-07-21 16:48:02 +02:00
|
|
|
import ImageHandleFloat from "./ImageHandleFloat.svelte";
|
2021-07-21 14:13:56 +02:00
|
|
|
import ImageHandleSizeSelect from "./ImageHandleSizeSelect.svelte";
|
|
|
|
|
2021-07-21 01:32:09 +02:00
|
|
|
import { getContext } from "svelte";
|
|
|
|
import { nightModeKey } from "components/context-keys";
|
|
|
|
|
2021-07-21 02:15:10 +02:00
|
|
|
export let image: HTMLImageElement | null = null;
|
2021-07-21 14:13:56 +02:00
|
|
|
export let imageRule: CSSStyleRule | null = null;
|
2021-07-21 16:48:02 +02:00
|
|
|
export let isRtl: boolean = false;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
export let container: HTMLElement;
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-21 15:52:48 +02:00
|
|
|
$: showDimensions = image
|
|
|
|
? parseInt(getComputedStyle(image).getPropertyValue("width")) > 220
|
|
|
|
: false;
|
|
|
|
|
|
|
|
$: selector = `:not([src="${image?.getAttribute("src")}"])`;
|
|
|
|
$: active = imageRule?.selectorText.includes(selector) as boolean;
|
|
|
|
|
2021-07-21 02:15:10 +02:00
|
|
|
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) {
|
2021-07-21 14:13:56 +02:00
|
|
|
updateSizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSizes() {
|
|
|
|
const imageRect = image!.getBoundingClientRect();
|
2021-07-21 16:48:02 +02:00
|
|
|
const containerRect = container.getBoundingClientRect();
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
naturalWidth = image!.naturalWidth;
|
|
|
|
naturalHeight = image!.naturalHeight;
|
2021-07-21 02:15:10 +02:00
|
|
|
|
2021-07-21 02:37:07 +02:00
|
|
|
containerTop = containerRect.top;
|
|
|
|
containerLeft = containerRect.left;
|
|
|
|
top = imageRect.top - containerTop;
|
|
|
|
left = imageRect.left - containerLeft;
|
2021-07-21 14:13:56 +02:00
|
|
|
width = image!.clientWidth;
|
|
|
|
height = image!.clientHeight;
|
2021-07-21 02:15:10 +02:00
|
|
|
}
|
2021-07-21 01:32:09 +02:00
|
|
|
|
|
|
|
function setPointerCapture(event: PointerEvent): void {
|
|
|
|
if (event.pointerId !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(event.target as Element).setPointerCapture(event.pointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resize(event: PointerEvent): void {
|
|
|
|
if (!(event.target as Element).hasPointerCapture(event.pointerId)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 02:15:10 +02:00
|
|
|
|
|
|
|
const dragWidth = event.clientX - containerLeft - left;
|
|
|
|
const dragHeight = event.clientY - containerTop - top;
|
|
|
|
|
2021-07-21 02:37:07 +02:00
|
|
|
const widthIncrease = dragWidth / naturalWidth;
|
|
|
|
const heightIncrease = dragHeight / naturalHeight;
|
|
|
|
|
|
|
|
if (widthIncrease > heightIncrease) {
|
2021-07-21 15:52:48 +02:00
|
|
|
width = Math.trunc(dragWidth);
|
|
|
|
height = Math.trunc(naturalHeight * widthIncrease);
|
2021-07-21 02:37:07 +02:00
|
|
|
} else {
|
2021-07-21 15:52:48 +02:00
|
|
|
height = Math.trunc(dragHeight);
|
|
|
|
width = Math.trunc(naturalWidth * heightIncrease);
|
2021-07-21 02:37:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:52:48 +02:00
|
|
|
showDimensions = width > 220;
|
|
|
|
|
2021-07-21 02:37:07 +02:00
|
|
|
image!.style.width = `${width}px`;
|
|
|
|
image!.style.height = `${height}px`;
|
2021-07-21 01:32:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:52:48 +02:00
|
|
|
let sizeSelect: any;
|
|
|
|
|
|
|
|
function onDblclick() {
|
|
|
|
sizeSelect.toggleActualSize();
|
|
|
|
}
|
|
|
|
|
2021-07-21 01:32:09 +02:00
|
|
|
const nightMode = getContext(nightModeKey);
|
|
|
|
</script>
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
{#if image && imageRule}
|
2021-07-21 03:50:52 +02:00
|
|
|
<div
|
|
|
|
style="--top: {top}px; --left: {left}px; --width: {width}px; --height: {height}px;"
|
|
|
|
class="image-handle-selection"
|
|
|
|
>
|
2021-07-21 15:52:48 +02:00
|
|
|
<div class="image-handle-bg" on:dblclick={onDblclick} />
|
2021-07-21 16:48:02 +02:00
|
|
|
<div class="image-handle-buttons" class:is-rtl={isRtl}>
|
|
|
|
<ImageHandleFloat {isRtl} bind:float={image.style.float} />
|
2021-07-21 03:50:52 +02:00
|
|
|
</div>
|
2021-07-21 16:48:02 +02:00
|
|
|
<div class="image-handle-size-select" class:is-rtl={isRtl}>
|
2021-07-21 14:13:56 +02:00
|
|
|
<ImageHandleSizeSelect
|
2021-07-21 15:52:48 +02:00
|
|
|
bind:this={sizeSelect}
|
|
|
|
bind:active
|
2021-07-21 14:13:56 +02:00
|
|
|
{image}
|
|
|
|
{imageRule}
|
2021-07-21 16:48:02 +02:00
|
|
|
{selector}
|
|
|
|
{isRtl}
|
2021-07-21 14:13:56 +02:00
|
|
|
on:update={updateSizes}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-07-21 15:52:48 +02:00
|
|
|
{#if showDimensions}
|
2021-07-21 16:48:02 +02:00
|
|
|
<div class="image-handle-dimensions" class:is-rtl={isRtl}>
|
2021-07-21 15:52:48 +02:00
|
|
|
{width}×{height} (Original: {naturalWidth}×{naturalHeight})
|
|
|
|
</div>
|
|
|
|
{/if}
|
2021-07-21 03:50:52 +02:00
|
|
|
<div class:nightMode class="image-handle-control image-handle-control-nw" />
|
|
|
|
<div class:nightMode class="image-handle-control image-handle-control-ne" />
|
2021-07-21 03:59:58 +02:00
|
|
|
<div
|
|
|
|
class:nightMode
|
2021-07-21 14:13:56 +02:00
|
|
|
class:active
|
|
|
|
class="image-handle-control image-handle-control-sw"
|
2021-07-21 03:59:58 +02:00
|
|
|
on:pointerdown={setPointerCapture}
|
|
|
|
on:pointermove={resize}
|
|
|
|
/>
|
2021-07-21 03:50:52 +02:00
|
|
|
<div
|
|
|
|
class:nightMode
|
2021-07-21 14:13:56 +02:00
|
|
|
class:active
|
|
|
|
class="image-handle-control image-handle-control-se"
|
2021-07-21 03:50:52 +02:00
|
|
|
on:pointerdown={setPointerCapture}
|
|
|
|
on:pointermove={resize}
|
2021-07-21 03:18:33 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2021-07-21 03:50:52 +02:00
|
|
|
{/if}
|
2021-07-21 01:32:09 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
div {
|
|
|
|
position: absolute;
|
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-selection {
|
|
|
|
top: var(--top);
|
|
|
|
left: var(--left);
|
|
|
|
width: var(--width);
|
|
|
|
height: var(--height);
|
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-bg {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
background-color: black;
|
|
|
|
opacity: 0.2;
|
|
|
|
}
|
|
|
|
|
2021-07-21 03:18:33 +02:00
|
|
|
.image-handle-buttons {
|
|
|
|
top: 3px;
|
|
|
|
left: 3px;
|
2021-07-21 16:48:02 +02:00
|
|
|
|
|
|
|
&.is-rtl {
|
|
|
|
left: auto;
|
|
|
|
right: 3px;
|
|
|
|
}
|
2021-07-21 03:18:33 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
.image-handle-size-select {
|
|
|
|
bottom: 3px;
|
|
|
|
left: 3px;
|
2021-07-21 16:48:02 +02:00
|
|
|
|
|
|
|
&.is-rtl {
|
|
|
|
left: auto;
|
|
|
|
right: 3px;
|
|
|
|
}
|
2021-07-21 14:13:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 02:15:10 +02:00
|
|
|
.image-handle-dimensions {
|
|
|
|
pointer-events: none;
|
|
|
|
user-select: none;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
font-size: 13px;
|
2021-07-21 02:15:10 +02:00
|
|
|
background-color: rgba(0 0 0 / 0.3);
|
|
|
|
border-color: black;
|
|
|
|
border-radius: 0.25rem;
|
|
|
|
padding: 0 5px;
|
2021-07-21 16:48:02 +02:00
|
|
|
|
|
|
|
bottom: 3px;
|
|
|
|
right: 3px;
|
|
|
|
|
|
|
|
&.is-rtl {
|
|
|
|
right: auto;
|
|
|
|
left: 3px;
|
|
|
|
}
|
2021-07-21 02:15:10 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 01:32:09 +02:00
|
|
|
.image-handle-control {
|
|
|
|
width: 7px;
|
|
|
|
height: 7px;
|
|
|
|
border: 1px solid black;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
&.active {
|
2021-07-21 01:32:09 +02:00
|
|
|
background-color: black;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.nightMode {
|
|
|
|
border-color: white;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
&.active {
|
2021-07-21 01:32:09 +02:00
|
|
|
background-color: white;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-control-nw {
|
|
|
|
top: -5px;
|
|
|
|
left: -5px;
|
|
|
|
border-bottom: none;
|
|
|
|
border-right: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-control-ne {
|
|
|
|
top: -5px;
|
|
|
|
right: -5px;
|
|
|
|
border-bottom: none;
|
|
|
|
border-left: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-control-sw {
|
|
|
|
bottom: -5px;
|
|
|
|
left: -5px;
|
|
|
|
border-top: none;
|
|
|
|
border-right: none;
|
2021-07-21 03:59:58 +02:00
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
&.active {
|
2021-07-21 03:59:58 +02:00
|
|
|
cursor: sw-resize;
|
|
|
|
}
|
2021-07-21 01:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.image-handle-control-se {
|
|
|
|
bottom: -5px;
|
|
|
|
right: -5px;
|
|
|
|
border-top: none;
|
|
|
|
border-left: none;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
&.active {
|
2021-07-21 01:32:09 +02:00
|
|
|
cursor: se-resize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|