Skip to content

Commit 4ea9f2f

Browse files
committed
Make the focal point resettable
1 parent db9a786 commit 4ea9f2f

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

wp-focalpoint.css

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,33 @@
55
/* display: none !important; */
66
}
77

8+
/**
9+
* Hidden elements if not initialized
10+
*/
11+
focal-point-picker [data-focalpoint-handle],
12+
focal-point-picker [data-focalpoint-preview] {
13+
display: none;
14+
}
15+
16+
/**
17+
* The input element
18+
*/
19+
[data-focalpoint-input-wrap] {
20+
display: flex;
21+
gap: 0.5rem;
22+
}
23+
[data-focalpoint-input-wrap] > * {
24+
flex: 1;
25+
}
26+
[data-focalpoint-reset] {
27+
cursor: pointer;
28+
max-width: fit-content;
29+
}
30+
831
/**
932
* The handle
1033
*/
11-
[data-focal-point-handle] {
34+
[data-focalpoint-handle] {
1235
box-sizing: border-box;
1336
position: absolute;
1437
z-index: 2;
@@ -22,10 +45,10 @@
2245
user-select: all;
2346
filter: drop-shadow(1px 1px 2px rgb(0 0 0 / 0.3));
2447
}
25-
[data-focal-point-handle]:focus {
48+
[data-focalpoint-handle]:focus {
2649
outline: 0 !important;
2750
}
28-
[data-focal-point-handle]::before {
51+
[data-focalpoint-handle]::before {
2952
content: "";
3053
display: block;
3154
position: absolute;
@@ -35,7 +58,7 @@
3558
background: white;
3659
transform: translate(-50%, -50%);
3760
}
38-
[data-focal-point-handle]:after {
61+
[data-focalpoint-handle]:after {
3962
content: "";
4063
display: block;
4164
--size: 1.2rem;
@@ -71,7 +94,7 @@
7194
z-index: 99999999;
7295
--size: 20svw;
7396
width: var(--size);
74-
top: 0;
97+
bottom: 0;
7598
right: 0;
7699
box-sizing: border-box;
77100
padding: 1rem;

wp-focalpoint.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
preview;
5151
/** @type {HTMLButtonElement} handle */
5252
handle;
53+
/** @type {HTMLButtonElement} resetButton */
54+
resetButton;
5355
/** @type {boolean} dragging */
5456
dragging = false;
5557

@@ -62,7 +64,10 @@
6264
this.querySelector("[data-focalpoint-preview]")
6365
);
6466
this.handle = /** @type {!HTMLButtonElement} */ (
65-
this.querySelector("[data-focal-point-handle]")
67+
this.querySelector("[data-focalpoint-handle]")
68+
);
69+
this.resetButton = /** @type {!HTMLButtonElement} */ (
70+
this.querySelector("[data-focalpoint-reset]")
6671
);
6772
}
6873

@@ -124,14 +129,20 @@
124129
* @return {void}
125130
*/
126131
disconnectedCallback() {
127-
const { handle, preview } = this;
132+
const { handle, preview, img, resetButton } = this;
128133

129134
if (preview) {
130135
this.appendChild(preview);
131136
}
132137
if (handle) {
133138
this.appendChild(handle);
134139
}
140+
if (img) {
141+
img.removeEventListener("click", this.onImageClick);
142+
}
143+
if (resetButton) {
144+
resetButton.removeEventListener("click", this.reset);
145+
}
135146
window.removeEventListener("resize", this.onResize);
136147
}
137148

@@ -140,7 +151,7 @@
140151
* @return {void}
141152
*/
142153
initializeUI = () => {
143-
const { imageWrap, img, handle, preview } = this;
154+
const { imageWrap, img, handle, preview, resetButton } = this;
144155

145156
if (!imageWrap || !img) {
146157
console.error("Some elements are missing", { imageWrap, img });
@@ -156,12 +167,9 @@
156167
this.onResize();
157168

158169
img.addEventListener("click", this.onImageClick);
170+
resetButton.addEventListener("click", this.reset);
159171

160-
$(handle).on("dblclick", () => {
161-
this.setHandlePosition(0.5, 0.5);
162-
this.applyFocalPointFromHandle();
163-
$("#focalpoint-input").trigger("change");
164-
});
172+
$(handle).on("dblclick", this.reset);
165173

166174
$(handle).on("mouseenter", () => this.togglePreview(true));
167175
$(handle).on("mouseleave", () => {
@@ -253,6 +261,15 @@
253261
);
254262
};
255263

264+
/**
265+
* Resets the focal point
266+
*/
267+
reset = () => {
268+
this.setHandlePosition(0.5, 0.5);
269+
this.applyFocalPointFromHandle();
270+
$("#focalpoint-input").trigger("change");
271+
};
272+
256273
/**
257274
* Set the handle position, based on the image
258275
* @param {number} leftPercent - The left position as a percentage.
@@ -298,10 +315,20 @@
298315
const handleRect = handle.getBoundingClientRect();
299316
const imgRect = img.getBoundingClientRect();
300317

301-
return [
318+
const point = [
302319
(handleRect.left - imgRect.left) / imgRect.width,
303320
(handleRect.top - imgRect.top) / imgRect.height,
304-
].map((number) => parseFloat((number * 100).toFixed(2)));
321+
].map((value) => {
322+
/** We want percentages */
323+
value *= 100;
324+
/** Round if close to 50 */
325+
if (Math.abs(value - 50) < 2) {
326+
value = 50;
327+
}
328+
return value;
329+
});
330+
331+
return point.map((number) => parseFloat(number.toFixed(2)));
305332
}
306333

307334
/**

wp-focalpoint.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ public static function attachmentFieldsToEdit(
7171
ob_start() ?>
7272

7373
<focal-point-picker>
74-
<input type='text' readonly value="<?= $focalPoint['left'] ?> <?= $focalPoint['top'] ?>" id='focalpoint-input' name='attachments[<?= $post->ID ?>][focalpoint]'>
74+
<div data-focalpoint-input-wrap>
75+
<input data-focalpoint-input type='text' readonly value="<?= $focalPoint['left'] ?> <?= $focalPoint['top'] ?>" id='focalpoint-input' name='attachments[<?= $post->ID ?>][focalpoint]'>
76+
<button data-focalpoint-reset type="button" class="button-primary">Reset</button>
77+
</div>
78+
7579
<div data-focalpoint-preview aria-hidden="true">
76-
<div data-landscape></div>
7780
<div data-portrait></div>
81+
<div data-landscape></div>
7882
</div>
79-
<button data-focal-point-handle aria-hidden="true" tabindex="-1" type="button" title="Drag to change. Double-click to reset."></button>
83+
<button data-focalpoint-handle aria-hidden="true" tabindex="-1" type="button" title="Drag to change. Double-click to reset."></button>
8084
</focal-point-picker>
8185

8286
<?php $html = ob_get_clean();

0 commit comments

Comments
 (0)