Skip to content

Commit

Permalink
Support for images in ACF blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hirasso committed Jul 24, 2024
1 parent 6ae79d1 commit 8a4d50a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
22 changes: 15 additions & 7 deletions focal-point-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,25 @@
const mediaModalRoot = this.closest(".media-frame-content");
const classicRoot = this.closest("#post-body-content");

this.imageWrap = mediaModalRoot
const imageWrap = mediaModalRoot
? mediaModalRoot.querySelector(".thumbnail-image")
: classicRoot
? classicRoot.querySelector(".wp_attachment_image p")
: undefined;

if (
!this.imageWrap ||
this.imageWrap.hasAttribute("data-fcp-wrap")
) {
if (!imageWrap) {
console.error("No imageWrap found", this);
return;
}

if (imageWrap.hasAttribute("data-fcp-wrap")) {
console.log("already initialized", this);
return;
}
this.imageWrap.setAttribute("data-fcp-wrap", "");

imageWrap.setAttribute("data-fcp-wrap", "");

this.imageWrap = imageWrap;
this.img = this.imageWrap.querySelector("img");
if (!this.img) {
console.error("no image found in imageWrap", this.imageWrap);
Expand All @@ -130,7 +135,7 @@
* @return {void}
*/
disconnectedCallback() {
const { handle, preview, img, resetButton } = this;
const { handle, preview, img, imageWrap, resetButton } = this;

if (preview) {
this.appendChild(preview);
Expand All @@ -141,6 +146,9 @@
if (img) {
img.removeEventListener("click", this.onImageClick);
}
if (imageWrap) {
imageWrap.removeAttribute("data-fcp-wrap");
}
if (resetButton) {
resetButton.removeEventListener("click", this.reset);
}
Expand Down
2 changes: 1 addition & 1 deletion focal-point-picker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: focal-point-picker
* Description: Set a custom focal point for your images, directly in the WP media grid
* Version: 1.2.6
* Version: 1.3.0
* Author: Rasso Hilber
* Author URI: https://rassohilber.com/
* Tested up to: 6.7
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"prepare": "husky",
"format:assets": "npx prettier --write '**/*.{js,css}'",
"format:php": "vendor/bin/php-cs-fixer fix --config .phpcsfixer.config.php --allow-risky=yes",
"readme:generate": "curl -L https://raw.githubusercontent.com/fumikito/wp-readme/master/wp-readme.php | php"
},
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ object(FocalPointPicker\FocalPoint)#2796 (4) {
float(50)
["topPercent"]=>
float(50)
["x"]=>
float(0.5)
["y"]=>
float(0.5)
["xPercent"]=>
float(50)
["yPercent"]=>
float(50)
}
```

Expand Down
20 changes: 20 additions & 0 deletions src/FocalPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ final class FocalPoint
public float $leftPercent;
public float $topPercent;

public float $x;
public float $y;

public float $xPercent;
public float $yPercent;

public function __construct(WP_Post|int $post)
{
$post = get_post($post);
Expand All @@ -32,6 +38,20 @@ public function __construct(WP_Post|int $post)

$this->leftPercent = $this->left * 100;
$this->topPercent = $this->top * 100;

$this->x = $this->left;
$this->y = $this->top;

$this->xPercent = $this->x * 100;
$this->yPercent = $this->y * 100;
}

/**
* Is the focal point's value equal to the default (0.5, 0.5)?
*/
public function hasDefaultValue(): bool
{
return $this->x === 0.5 && $this->y === 0.5;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/FocalPointPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function attachmentFieldsToEdit(
<focal-point-picker>
<div data-focalpoint-input-wrap>
<input data-focalpoint-input type='text' readonly value="<?php esc_attr_e($focalPoint->left) ?> <?php esc_attr_e($focalPoint->top) ?>" id='focalpoint-input' name='attachments[<?php esc_attr_e($post->ID) ?>][focalpoint]'>
<button data-focalpoint-reset disabled type="button" class="button-primary">Reset</button>
<button data-focalpoint-reset <?= disabled($focalPoint->hasDefaultValue()) ?> type="button" class="button-primary">Reset</button>
</div>

<div data-focalpoint-preview aria-hidden="true">
Expand Down

0 comments on commit 8a4d50a

Please sign in to comment.