Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Image Cropper handle (focal point) #1021

Merged
merged 95 commits into from
Oct 16, 2024
Merged
Changes from 8 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
6d59028
Adjust Image Cropper focal point to be consistent with Color Area handle
bjarnef Nov 26, 2023
ca3474c
Make focus point focusable via keyboard
bjarnef Nov 26, 2023
c2c7b48
Update import of utils from uui
bjarnef Nov 27, 2023
ebedf33
Merge branch 'main' into feature/image-cropper-handle
bjarnef Nov 27, 2023
b9de615
Merge branch 'main' into feature/image-cropper-handle
bjarnef Nov 28, 2023
dc3c532
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef Nov 28, 2023
1683038
Merge branch 'feature/image-cropper-handle' of github.com:bjarnef/Umb…
bjarnef Nov 28, 2023
e9c7b2f
Make focal point accessible via keyboard
bjarnef Nov 28, 2023
00d6c72
Move logic to function
bjarnef Nov 28, 2023
87eca87
Reset coords
bjarnef Nov 28, 2023
03a3a7e
Remove reset coords
bjarnef Nov 28, 2023
11666ac
Reset coords if focal point is updated to center
bjarnef Nov 28, 2023
5249f95
Format document
bjarnef Nov 28, 2023
03bcee2
Set focal point property instead
bjarnef Nov 29, 2023
61956d4
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef Feb 21, 2024
ef5fc6d
Formatting
bjarnef Feb 21, 2024
86b8b3b
Fix warnings
bjarnef Feb 21, 2024
6d9e3be
Width and height
bjarnef Feb 21, 2024
23ff2de
Remove added outline so we can see when focal point has focus via key…
bjarnef Feb 22, 2024
a251fb4
Reset coords
bjarnef Feb 22, 2024
57b09e5
Merge branch 'main' into feature/image-cropper-handle
bjarnef Feb 22, 2024
122ad4b
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef Feb 22, 2024
42268ca
Cleanup
bjarnef Feb 22, 2024
6a92562
Update import
bjarnef Feb 22, 2024
f3f7d06
Merge branch 'main' into feature/image-cropper-handle
bjarnef Feb 23, 2024
00b7188
Merge branch 'main' into feature/image-cropper-handle
bjarnef Feb 29, 2024
3988aaa
Merge branch 'main' into feature/image-cropper-handle
bjarnef Feb 29, 2024
37ef47f
Merge branch 'main' into feature/image-cropper-handle
bjarnef Mar 2, 2024
ac1ab94
Merge branch 'main' into feature/image-cropper-handle
bjarnef Mar 2, 2024
9731fc4
Merge branch 'main' into feature/image-cropper-handle
bjarnef Mar 3, 2024
6a83928
Merge branch 'main' into feature/image-cropper-handle
bjarnef Mar 6, 2024
f068d07
Merge branch 'main' into feature/image-cropper-handle
bjarnef Mar 26, 2024
a8f354a
Merge branch 'main' into feature/image-cropper-handle
bjarnef May 14, 2024
ffcc397
Merge branch 'main' into feature/image-cropper-handle
bjarnef May 17, 2024
6968e49
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef May 17, 2024
75ac167
Cleanup
bjarnef May 17, 2024
3e5a981
Use UmbChangeEvent
bjarnef May 17, 2024
d47f4b1
Adjust elements
bjarnef May 17, 2024
f71a727
Import drag for uui
bjarnef May 17, 2024
fe3a642
Cleanup styling
bjarnef May 20, 2024
72c6bba
Cleanup
bjarnef May 20, 2024
346f804
Remove unnecessary events
bjarnef May 20, 2024
d855dc0
Container is HTMLElement
bjarnef May 20, 2024
49a3b9a
Check focal point updated
bjarnef May 20, 2024
2a02dce
Merge branch 'main' into feature/image-cropper-handle
bjarnef May 23, 2024
a0d7b29
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef Jun 14, 2024
43af264
Fix conflicts after merge
bjarnef Jun 14, 2024
b8feef8
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jun 19, 2024
ce6478d
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 17, 2024
113dbe7
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 23, 2024
dfd4902
Null check
bjarnef Jul 24, 2024
d7021a1
Update correct element types
bjarnef Jul 24, 2024
57a0cfc
Revert to custom event
bjarnef Jul 24, 2024
6b57d0b
Cleanup console
bjarnef Jul 24, 2024
3cf4f00
Don't hide overflow, so focal point is not cut off on edges on image
bjarnef Jul 24, 2024
fa84877
Add margin top at actions so focal point doesn't overlap buttons
bjarnef Jul 24, 2024
ace53c4
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 25, 2024
52a4c01
Top margin at actions
bjarnef Jul 25, 2024
3b32ad6
Crosshair cursor when focal point is not hidden
bjarnef Jul 25, 2024
4a7bab7
Cancel update focal point
bjarnef Jul 25, 2024
e8e839a
Add move cursor to viewport image
bjarnef Jul 25, 2024
12418b7
Use crop label
bjarnef Jul 25, 2024
4461e35
Add missing label
bjarnef Jul 25, 2024
f32c600
Disable draggable of image as in old backoffice
bjarnef Jul 25, 2024
fb7948d
Fix previous merge conflicts
bjarnef Jul 26, 2024
6f75128
Update src/packages/media/media/components/input-image-cropper/image-…
bjarnef Jul 26, 2024
72bfa93
Update src/packages/media/media/components/input-image-cropper/image-…
bjarnef Jul 26, 2024
dfef9ea
Update src/packages/media/media/components/input-image-cropper/image-…
bjarnef Jul 26, 2024
47a32aa
Update src/packages/media/media/components/input-image-cropper/image-…
bjarnef Jul 26, 2024
2c4de9e
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 26, 2024
4df432a
Remove disconnectedCallback
bjarnef Jul 26, 2024
864395a
Ignore in the debug element
bjarnef Jul 26, 2024
f1449dd
Add custom event for focal point change
bjarnef Jul 26, 2024
10a6251
Import type
bjarnef Jul 26, 2024
459665b
Replace with UmbImageCropChangeEvent
bjarnef Jul 26, 2024
952826c
Use UmbFocalPointModel
bjarnef Jul 26, 2024
eb30ac8
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 26, 2024
e307bd4
Move set focal pont style to setter
bjarnef Jul 26, 2024
108c021
Update custom event name
bjarnef Jul 28, 2024
e29c947
Merge branch 'main' of github.com:umbraco/Umbraco.CMS.Backoffice into…
bjarnef Jul 29, 2024
a829ed6
Localize focal point label
bjarnef Jul 29, 2024
ad19d24
Fix import of UmbFocalPointModel type
bjarnef Jul 29, 2024
9057fc4
Localize buttons
bjarnef Jul 29, 2024
b426eb7
Update labels
bjarnef Jul 29, 2024
09639de
Combine imports
bjarnef Jul 29, 2024
bbc3b66
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 30, 2024
7133a06
Merge branch 'main' into feature/image-cropper-handle
bjarnef Jul 31, 2024
9418110
Merge branch 'main' into feature/image-cropper-handle
bjarnef Aug 5, 2024
a4121e3
Merge branch 'main' into feature/image-cropper-handle
bjarnef Aug 9, 2024
7e553ac
Merge branch 'main' into feature/image-cropper-handle
bjarnef Aug 15, 2024
53cad7f
Merge branch 'main' into feature/image-cropper-handle
bjarnef Aug 19, 2024
3567f06
Merge branch 'main' into feature/image-cropper-handle
bjarnef Sep 3, 2024
ed30328
Merge branch 'main' into feature/image-cropper-handle
bjarnef Sep 17, 2024
1d32afb
Merge branch 'main' into feature/image-cropper-handle
bjarnef Oct 9, 2024
2c1c9af
Merge branch 'main' into feature/image-cropper-handle
iOvergaard Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clamp } from './mathUtils.js';
//import { clamp } from './mathUtils.js';
import { drag, clamp } from '@umbraco-cms/backoffice/external/uui';
import { UmbImageCropperFocalPoint } from './index.js';
import {
LitElement,
Expand All @@ -8,19 +9,28 @@
nothing,
customElement,
property,
state,
query,
} from '@umbraco-cms/backoffice/external/lit';
import { classMap } from 'lit/directives/class-map.js';

Check failure on line 15 in src/packages/core/components/input-image-cropper/image-cropper-focus-setter.element.ts

View workflow job for this annotation

GitHub Actions / build (20)

node_modules imports should be proxied through `@umbraco-cms/backoffice/external`. Please create it if it does not exist
import { ifDefined } from 'lit/directives/if-defined.js';

Check failure on line 16 in src/packages/core/components/input-image-cropper/image-cropper-focus-setter.element.ts

View workflow job for this annotation

GitHub Actions / build (20)

node_modules imports should be proxied through `@umbraco-cms/backoffice/external`. Please create it if it does not exist

@customElement('umb-image-cropper-focus-setter')
export class UmbImageCropperFocusSetterElement extends LitElement {
@query('#image') imageElement!: HTMLImageElement;
@query('#wrapper') wrapperElement!: HTMLImageElement;
@query('#focal-point') focalPointElement!: HTMLImageElement;

@state() private isDraggingGridHandle = false;

@state() private coords = { x: 0, y: 0 };
iOvergaard marked this conversation as resolved.
Show resolved Hide resolved

@property({ type: String }) src?: string;
@property({ attribute: false }) focalPoint: UmbImageCropperFocalPoint = { left: 0.5, top: 0.5 };

#DOT_RADIUS = 6 as const;
@property({ type: Boolean, reflect: true }) disabled = false;

#DOT_RADIUS = 8 as const;

connectedCallback() {
super.connectedCallback();
Expand Down Expand Up @@ -59,47 +69,30 @@
this.wrapperElement.style.height = '100%';
}

// Init x and y coords from half of rendered image size, which is equavalient to focal point { left: 0.5, top: 0.5 }.
this.coords.x = this.imageElement.clientWidth / 2;
this.coords.y = this.imageElement.clientHeight / 2;

this.imageElement.style.aspectRatio = `${imageAspectRatio}`;
this.wrapperElement.style.aspectRatio = `${imageAspectRatio}`;
};
}

async #addEventListeners() {
await this.updateComplete; // Wait for the @query to be resolved
this.imageElement?.addEventListener('mousedown', this.#onStartDrag);
window.addEventListener('mouseup', this.#onEndDrag);
}

#removeEventListeners() {
this.imageElement?.removeEventListener('mousedown', this.#onStartDrag);
window.removeEventListener('mouseup', this.#onEndDrag);

}

#onStartDrag = (event: MouseEvent) => {
event.preventDefault();
window.addEventListener('mousemove', this.#onDrag);
};

#onEndDrag = (event: MouseEvent) => {
event.preventDefault();
window.removeEventListener('mousemove', this.#onDrag);
};
#setFocalPoint(x: number, y: number) {

#onDrag = (event: MouseEvent) => {
event.preventDefault();
this.#onSetFocalPoint(event);
};

#onSetFocalPoint(event: MouseEvent) {
event.preventDefault();
const grid = this.wrapperElement;
const { width, height } = grid.getBoundingClientRect();

const image = this.imageElement.getBoundingClientRect();

const x = clamp(event.clientX - image.left, 0, image.width);
const y = clamp(event.clientY - image.top, 0, image.height);

const left = clamp(x / image.width, 0, 1);
const top = clamp(y / image.height, 0, 1);
const left = clamp((x / width), 0, 1);
const top = clamp((y / height), 0, 1);

this.focalPointElement.style.left = `calc(${left * 100}% - ${this.#DOT_RADIUS}px)`;
this.focalPointElement.style.top = `calc(${top * 100}% - ${this.#DOT_RADIUS}px)`;
Expand All @@ -113,13 +106,81 @@
);
}

#handleGridDrag(event: PointerEvent) {
if (this.disabled) return;

const grid = this.wrapperElement;
const handle = this.focalPointElement;

handle.focus();
event.preventDefault();
event.stopPropagation();

this.isDraggingGridHandle = true;

drag(grid, {
onMove: (x, y) => {
// check if coordinates are not NaN (can happen when dragging outside of the grid)
if (isNaN(x) || isNaN(y)) return;

this.coords.x = x;
this.coords.y = y;

this.#setFocalPoint(x, y);
},
onStop: () => (this.isDraggingGridHandle = false),
initialEvent: event,
});
}

#handleGridKeyDown(event: KeyboardEvent) {
if (this.disabled) return;
const increment = event.shiftKey ? 1 : 10;

const grid = this.wrapperElement;
const { width, height } = grid.getBoundingClientRect();

if (event.key === 'ArrowLeft') {
event.preventDefault();
this.coords.x = clamp(this.coords.x - increment, 0, width);
this.#setFocalPoint(this.coords.x, this.coords.y);
}

if (event.key === 'ArrowRight') {
event.preventDefault();
this.coords.x = clamp(this.coords.x + increment, 0, width);
this.#setFocalPoint(this.coords.x, this.coords.y);
}

if (event.key === 'ArrowUp') {
event.preventDefault();
this.coords.y = clamp(this.coords.y - increment, 0, height);
this.#setFocalPoint(this.coords.x, this.coords.y);
}

if (event.key === 'ArrowDown') {
event.preventDefault();
this.coords.y = clamp(this.coords.y + increment, 0, height);
this.#setFocalPoint(this.coords.x, this.coords.y);
}
}

render() {
if (!this.src) return nothing;

return html`
<div id="wrapper">
<img id="image" @click=${this.#onSetFocalPoint} @keydown=${() => nothing} src=${this.src} alt="" />
<div id="focal-point"></div>
<div id="wrapper"
@mousedown=${this.#handleGridDrag}
@touchstart=${this.#handleGridDrag}>
<img id="image" @keydown=${() => nothing} src=${this.src} alt="" />
<span id="focal-point"
class=${classMap({
'focal-point--dragging': this.isDraggingGridHandle,
})}
tabindex=${ifDefined(this.disabled ? undefined : '0')}
aria-label="Focal Point"
@keydown=${this.#handleGridKeyDown}>
</span>
</div>
`;
}
Expand All @@ -140,6 +201,9 @@
margin: auto;
max-width: 100%;
max-height: 100%;
box-sizing: border-box;
cursor: crosshair;
forced-color-adjust: none;
}
#image {
margin: auto;
Expand All @@ -151,11 +215,18 @@
position: absolute;
width: calc(2 * var(--dot-radius));
height: calc(2 * var(--dot-radius));
outline: 3px solid black;
top: 0;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
border: solid 2px white;
border-radius: 50%;
pointer-events: none;
background-color: white;
background-color: var(--uui-palette-spanish-pink-light);
transition: 150ms transform;
box-sizing: inherit;
}
.focal-point--dragging {
cursor: none;
transform: scale(1.5);
}
`;
}
Expand Down
Loading