Skip to content

Commit 105e176

Browse files
KMchaudharyKMchaudharyclaude
authored
Layer API: let developers register their own layer types (EPIC #2290) (#2345)
* test(layers): add Layer API contract tests and RFC draft Pin down how layers behave today, so the Layer API refactor (#2290) can move core layer types onto a registry and prove nothing changed: - Editor: the data each core type starts with, the add-on menu and creator path, media-type gating, settings-panel selection, and the window globals add-ons use. - Player: which manager gets each layer, add-on managers, validation, and the shared front-end registry and queue. - Image block: which saved layers reach the browser, and how the image renderer draws hotspot and woo layers. Tests marked "current behaviour" pin what the refactor intends to change on purpose. Also add the RFC draft (docs/rfcs/layer-api.md) with the audit of every reader of saved layers, and keep /docs/ out of the release package. Refs #2291 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(layers): keep GoDAM Central out of the Layer API RFC The Layer API EPIC covers the plugin only. Central adopts the API from the plugin once it lands, so drop the open question about Central's player and say so in the RFC's scope. Refs #2291 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ci: run unit tests on PRs into the Layer API base branch Every Layer API PR (#2290) targets feature/layer-api, which neither the JS nor the PHP unit-test workflow listed, so those PRs got no unit tests. Remove the entries once feature/layer-api is merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(layers): mark the Layer API RFC accepted Accepted as written. D1 now separates the keys GoDAM manages (id, type, name, displayTime) from shared keys a layer type may give its own default (duration, allow_skip, custom_css, trigger, watchDepth), which core types already rely on. Refs #2291 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit afa0664) * feat(layers): add the Layer API editor registry Add a layer type registry the editor and add-ons share (#2292): - `godam-layers` script (`window.godam.layers`, `@godam/layers`): a `godam/layers` wp-data store with registerLayerType(), unregisterLayerType() and the `godam.layers.registerLayerType` filter. - Core layer types (CTA, Hotspot, Form, Poll, Ad) register through it, with metadata in assets/src/layers/<type>/layer.json. Form plugins are variations of the Form type; Form's save check is its validate(). - The add menu, settings panel, layer list, header, timeline, save validation and image gating read the registry. Removes utils/layerTypes.js and the allowedLayerTypes allowlist. - Add-on types from the old hooks (godam_video_editor_layer_options and friends) are bridged into the registry, so godam-for-woo is unchanged. - `window.godam.layerEditor` (`godam-layer-editor` handle) gives add-on panels useLayer() / useLayerAttributes() and the shared controls. - A saved layer whose type isn't registered stays in the list with a warning and opens a "needs an add-on" notice instead of the CTA editor (RFC D2). Timeline chips for Marketo and HubSpot form layers now name the form plugin; they showed "Gravity Forms" before. Refs #2292 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): register form providers as Form layer variations Each of the 12 form plugins is now one registerLayerVariation( 'form', … ) call carrying its title, icon, availability, panel, form-ID field and settings URL, replacing the FormLayerComponentType table. Add-ons can register their own provider; one without a panel supplies getForms() and getFormHtml( id ) and gets a shared FormProviderPanel. A form layer whose provider isn't registered shows a notice instead of an error. Adds registerLayerVariation, unregisterLayerVariation, getLayerVariationByName and getUnavailableReason to the registry. A variation registered before its type is kept and added later. Editor only; the player and PHP parts move to #2294 and #2295. Refs #2293 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): run video and image layers on one front-end runtime Adds the Layer API front-end runtime (#2294): registerLayerRuntime( name, { validate, mount, supports, usesIconFont, variations } ) on window.godam.layers, plus a layer host that mounts each saved layer and forwards player events (onReady, onTimeUpdate, onPause, onEnded, onPlay, onResize, onFullscreenChange, onReplay, destroy) to the handlers mount() returns. ctx.api gives pause, play, seek, show, hide, track and isSuppressed. - LayersManager and render-image-frame both mount through the host and no longer branch on layer type names. - CTA, Form, Poll and Hotspot are core runtimes; CTA, Form and Poll share one FormLayerManager, which now takes a per-type behaviour. - The 12 form providers register their front-end part as Form variations: availability, "submitted" detection and analytics label. This replaces LayerValidator, FORM_TYPES and hasConfirmationMessage. - Hover-preview suppression covers every runtime, including layers from GoDAMAPI.createLayer() (RFC D5), which now mount on the same host. - window.godamLayerRegistry is a shim over the new registry, and still drains window.godamLayerRegistryQueue. The window store workaround is gone, and godam-for-woo's manager runs unchanged on video and images. - An unregistered type is logged once and skipped (RFC D2). Refs #2294 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): register layer types in PHP and render them through callbacks Adds the PHP half of the Layer API (#2295): - godam_register_layer_type( $path_or_metadata, $args ) on a new godam_init action, reading layer.json, with the godam_layer_type_metadata filter, and godam_register_layer_variation() / godam_get_layer_type() / godam_render_layer(). - render_callback( $layer, $instance_id, $context ) prints a layer's container. The core CTA, Form, Poll and Hotspot types carry the markup the player template printed inline, so godam-player.php loops over godam_render_layer() with no layer type names. Add-on output goes through wp_kses_post() unless the type sets supports.rawHtml (RFC D3). - The 12 form providers register their PHP part as Form variations: render callback and is_available. godamPluginDependencies is now built from each type's and provider's is_available. - godam/image sends the layers whose type supports images and has something to draw (should_render), instead of hard-coding hotspot and woo. - The ad tag is requested for layers whose type supports adTag. Typeless layers no longer warn there. - A type with no PHP registration still renders through the old godam_player_render_layer action, and the Woo layer is registered as a legacy type until #2296 moves godam-for-woo over. Sanitizing on save, localizing PHP metadata to the editor and per-page script loading follow in a second PR for #2295. Refs #2295 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): clean layers on save, and load layer scripts only where used Second part of #2295: - Saving rtgodam_meta cleans each layer against its type's declared attributes (RFC D3, Layer_Sanitizer). Add-on types are strict: declared values are sanitized by type and format (plain, html, url, color), a wrong type falls back to the default, and undeclared keys are dropped. Core types stay lenient (keep_undeclared_attributes) until their layer.json lists every attribute: they keep undeclared keys, mismatched values and HTML (escaped at render) as saved. Unregistered types are kept unchanged. - Attributes marked "private": true are left out of the player's data-options and the image block's layer data. - layer.json editorScript and viewScript ("file:./…" or a handle) are registered as scripts. Editor scripts load in the video editor; a view script loads only when a layer of its type renders. A new godam-layer-runtime script handle exposes the front-end runtime registry for add-on view scripts. - The video editor gets each PHP-registered type's metadata and availability (godamVideoEditorConfig.layerTypes / layerAvailability), and registerLayerType() fills in that metadata, so an add-on can register by name. The videoData *Active form flags now come from the registry. Refs #2295 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): deprecate the old add-on layer hooks behind shims Part of #2296. The hooks the Layer API replaces keep working, fed into the registry, and log a deprecation notice. They are deprecated since 2.7.0 and removed in 2.9.0: - PHP: godam_video_editor_layer_options and godam_video_editor_layer_components (apply_filters_deprecated), and godam_player_render_layer (do_action_deprecated). - JS: window.godamLayerComponents, window.godamLayerCreators, window.godamVideoEditorActions and window.godamLayerRegistry.registerLayerType(), each logging one console notice per page. So godam-for-woo can register its own `woo` type: - The legacy `woo` registration runs late on godam_init, after add-ons. - The editor accepts the `woo` name without a namespace, because saved layers use it. - Panels get a useMediaType() hook. - The analytics page reads add-on icons from layer types registered through the API. Refs #2296 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(layers): add the Layer API developer guide docs/layer-api.md, for add-on developers (#2297): - how a layer type fits together - a quick-start countdown layer plugin - the layer.json reference - the PHP, editor and front-end runtime references - form providers - the saved data shape and analytics - moving from the deprecated hooks - troubleshooting Refs #2297 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(layers): give each core layer type its own folder, built like core blocks GoDAM's core layer types now follow the structure an add-on layer type uses, the way Gutenberg's block-library holds each core block: assets/src/layers/<type>/ layer.json metadata, with "viewScript": "file:./view.js" <type>.php render callback + godam_register_layer_type() editor.js registerLayerType() settings edit.js the settings panel (moved from pages/video-editor) view.js registerLayerRuntime() (moved from godam-player/layers) A new `layers` webpack config builds each view.js (with its view.asset.php) into assets/build/layers/<type>/ and copies layer.json and the PHP beside it. Core_Layer_Types loads each type from the build, falling back to the source when the plugin isn't built. - View scripts load only on pages that show that layer type (Gutenberg viewScript behaviour), instead of being bundled into the player and image scripts. The image script drops from 33 KB to 16.5 KB. - The block editor and the Elementor and WPBakery editors, which draw layers without the page rendering them, enqueue the view scripts up front. - The shared FormLayerManager behind CTA, Form and Poll stays in the player bundle. The views only add their layers to it. - whenLayerRuntime() lets a player or image mount a layer whose view script loads after it started (deferred or delayed JS). - The registry only registers built file: scripts (with an *.asset.php), sets their translations, and prefixes core handles with godam-layer-. - assets/src/layers is left out of the release; it ships from assets/build/layers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(layers): note why the core layer loader includes variable paths PHPCS (WordPressVIPMinimum) flags both includes. Each path is built by the plugin: the core loader from its layer folder and fixed type list, the registry from a registered layer.json's built *.asset.php. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(layers): give layer.json stylesheets and translations, like block.json - Support `style`, `viewStyle` and `editorStyle` (a handle or `file:./x.css`, string or array). `file:` styles register with their path and `-rtl.css` twin; `style` + `viewStyle` load wherever view scripts do, `style` + `editorStyle` with the editor scripts, and image types' view styles reach the block editor canvas through `enqueue_block_assets`. - Translate `title`, `description` and variation strings with the type's `textdomain` and gettext context, as core does for block.json, falling back to the same string without a context. The editor prefers these over the untranslated layer.json strings its script imports. - Point script translations at GoDAM's languages folder only for GoDAM's own layer.json files; add-ons use WordPress's default lookup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(blocks): register the player styles on init so they reach the editor canvas The block editor collects its canvas styles before `admin_enqueue_scripts`, where `godam-player-wrapper-style` was registered. `godam-player-style` (tied to godam/image with wp_enqueue_block_style) had an unregistered dependency there, so WordPress dropped it, and with it every stylesheet that depends on it. The Woo product-hotspot popup rendered unstyled in the godam/image preview: its product image filled the canvas. Register both handles on `init` through GoDAM_Player::register_player_styles(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Date the Layer API deprecations to 2.8.0 develop is at 2.8.0 and 2.7.0 is already released, so the Layer API ships in 2.8.0. The old add-on hooks are deprecated since 2.8.0 and removed two minor releases later, in 2.10.0. Updates the PHP and JS notices, the developer guide and the tests that assert the notice text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix four Layer API review findings - Image hotspots drawn twice: when the hotspot view script registers after an image frame rendered, the host calls the shared onReady once per late layer. onReady now redraws into a fresh wrapper and removes the previous one, and destroy() clears it. - Form layers that couldn't be saved: FormProviderPanel saved the form ID under `form_id` when a provider set no idField, but the save check only read idField. Both now use getFormIdField(). - The `color` attribute format let extra CSS through, e.g. `rgb(0);position:fixed)`. It now uses rtgodam_sanitize_css_color(). - Variation icons: any string was drawn as an <img>, so a Dashicon name such as the guide's `icon: 'feedback'` was a broken image. Variations now split `icon` / `iconUrl` the way layer types do. The Jest asset mock now returns a URL-shaped path, as the build does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Address PR #2345 review feedback - Form save check: a layer whose form provider isn't registered (its add-on is inactive) no longer blocks saving the attachment; a layer without form_type is checked as the default provider, as PHP reads it. - Late form scripts: the Form view registers the core providers before its runtime, so layers waiting for the runtime pass validate. Layers whose variation registers later (an add-on provider) are mounted then, through the new whenLayerRuntimeVariation(). - custom_css: saved as written again, as on develop. wp_strip_all_tags() cut CSS at any `<` (`@media (width<=600px)`, `content: "<"`); only `</style` is removed now. - An add-on's validate() or usesIconFont() that throws is logged and treated as false, so the other layers still mount. - Variation attributes are compared by value in the editor and runtime, so array or object values match saved layers. - FormProviderPanel doesn't fetch a preview for an unavailable provider. - The godam_player_render_layer deprecation is reported before output buffering, so a WP_DEBUG notice isn't printed into the layer markup. - phpcs:ignore for the runtime script's asset include. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Import Layer API classes and load core layers from RTGODAM_PATH - Layer_Type_Registry (and REST_API\Jetpack in the form providers) is imported with `use` instead of written as a fully qualified name. - The core layer files (ad, cta, form, hotspot, poll) find their layer.json and class-form-providers.php through Core_Layer_Types::get_layers_dir(), built on RTGODAM_PATH, instead of __DIR__. get_layers_dir() now returns the folder register() is loading from, so a type's PHP, layer.json and the view.js it names come from the same build or source folder. - untrailingslashit() stub for the PHPUnit run. Add-on examples in docs/layer-api.md and layer-functions.php keep __DIR__, which is right for code that lives in another plugin. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Mark the form providers include as reviewed for VIP PHPCS rtBot flagged WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant on the require_once added in 08a8401. The path is the plugin's own core layer folder (RTGODAM_PATH based) plus a fixed filename. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: KMchaudhary <kuldipkumar.chaudhary@rtcamp.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d10b364 commit 105e176

144 files changed

Lines changed: 12170 additions & 2129 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.distignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ node_modules/
22
assets/src/blocks
33
assets/src/css
44
assets/src/js
5+
# Core layer types ship from assets/build/layers (layer.json, PHP and built scripts).
6+
assets/src/layers
57
assets/src/libs/jquery-ui-1.14.2.draggable
68
.browserslistrc
79
.eslintignore
@@ -45,3 +47,6 @@ phpunit.xml.dist
4547

4648
# Dashboard widget sample data: development and review only, never shipped.
4749
/inc/classes/class-dashboard-widget-preview.php
50+
51+
# Design docs and RFCs: for contributors, not shipped.
52+
/docs/

‎.eslintrc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"env": {
99
"browser": true
1010
},
11+
"settings": {
12+
// Provided at runtime by the `godam-layers` script (a webpack external).
13+
"import/core-modules": [ "@godam/layers" ]
14+
},
1115
"rules": {
1216
"jsdoc/check-indentation": "error",
1317
"@wordpress/dependency-group": "error"

‎assets/src/blocks/godam-image/render.php‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @var array $attributes Block attributes.
1313
*/
1414

15+
use RTGODAM\Inc\Layers\Layer_Type_Registry;
16+
1517
if ( ! defined( 'ABSPATH' ) ) {
1618
exit;
1719
}
@@ -73,16 +75,9 @@
7375
$godam_meta = get_post_meta( $godam_attachment_id, 'rtgodam_meta', true );
7476
$godam_all_layers = ( is_array( $godam_meta ) && ! empty( $godam_meta['layers'] ) ) ? $godam_meta['layers'] : array();
7577
do_action( 'rtgodam_after_attachment_lookup' );
76-
foreach ( $godam_all_layers as $godam_layer ) {
77-
if ( ! is_array( $godam_layer ) || empty( $godam_layer['type'] ) ) {
78-
continue;
79-
}
80-
if ( 'hotspot' === $godam_layer['type'] && ! empty( $godam_layer['hotspots'] ) ) {
81-
$godam_layers[] = $godam_layer;
82-
} elseif ( 'woo' === $godam_layer['type'] && ! empty( $godam_layer['productHotspots'] ) ) {
83-
$godam_layers[] = $godam_layer;
84-
}
85-
}
78+
// Layers of types that support images and have something to draw
79+
// (`supports.media` and `should_render` in the layer type registry).
80+
$godam_layers = Layer_Type_Registry::get_instance()->filter_layers_for_media( $godam_all_layers, 'image' );
8681
}
8782

8883
// A hotspot link becomes the `href` of a link on the front end, so only http, https

‎assets/src/js/godam-image-layers/render-image-frame.js‎

Lines changed: 60 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/**
22
* GoDAM Image Layers — shared frame renderer.
33
*
4-
* Draws hotspot and WooCommerce product-hotspot layers onto a single
5-
* `.godam-image__frame` element. Extracted so it can be reused by both the
4+
* Draws the saved layers of one `.godam-image__frame` element. Used by both the
65
* front-end bootstrap (frontend.js) and the block editor preview (the
76
* `godam/image` block's edit.js), which renders the same markup into the
87
* editor canvas and calls this against a ref.
98
*
10-
* It reuses the existing player-side managers unchanged except for a single
11-
* seam: `manager.computeContentRect` is overridden with an image-box
12-
* implementation (the managers' own version reads a `<video>`, which does not
13-
* exist here). A tiny "media host" adapter satisfies every other manager call
14-
* site; timeline/playback methods are no-ops.
9+
* Layers are mounted through the Layer API runtimes (#2294), exactly as on a
10+
* video, on a host whose media is the image. Only runtimes that declare
11+
* `supports.media: [ 'image' ]` run here (core: Hotspot, from its view script
12+
* `assets/build/layers/hotspot/view.js`; add-ons such as the Woo layer). A tiny "media host" adapter stands in for the
13+
* Video.js player; timeline and playback methods are no-ops.
1514
*
1615
* Note: importing this module is NOT side-effect-free — it initializes
1716
* `window.godamLayerRegistry` (see the layer-registry-init import below). It
@@ -25,9 +24,9 @@
2524
// Side-effect import: creates window.godamLayerRegistry and drains the queue the
2625
// Woo front-end script pushed to (image-only pages never boot the video player).
2726
import './../godam-player/utils/layer-registry-init.js';
28-
import HotspotLayerManager from './../godam-player/managers/layers/hotspotLayerManager.js';
29-
import { loadFontAwesome, hasHotspotsWithIcons, renderFontAwesomeIcons } from './../godam-player/utils/pluginLoader.js';
30-
import { resolveHotspotStyle } from './../godam-player/utils/hotspotStyle.js';
27+
import { createLayerHost } from './../godam-layers/runtime/host.js';
28+
import { layersUseIconFont, getLayerRuntime, whenLayerRuntime, runtimeUsesIconFont } from './../godam-layers/runtime/registry.js';
29+
import { loadFontAwesome, renderFontAwesomeIcons } from './../godam-player/utils/pluginLoader.js';
3130

3231
/**
3332
* Build a minimal "media host" adapter for the layer managers.
@@ -111,8 +110,6 @@ export function initImageFrame( frame ) {
111110
}
112111

113112
const instanceId = frame.dataset.instanceId || '';
114-
const hotspotLayers = layers.filter( ( layer ) => layer?.type === 'hotspot' );
115-
const wooLayers = layers.filter( ( layer ) => layer?.type === 'woo' );
116113
const adapter = makeImageAdapter( frame, img );
117114

118115
// Everything that must be undone on teardown — resize listener, ResizeObserver
@@ -148,120 +145,62 @@ export function initImageFrame( frame ) {
148145
return;
149146
}
150147

151-
let hotspotManager = null;
152-
let wooManager = null;
153-
154148
try {
155-
// Two `display:contents` group wrappers inside the SINGLE overlay. They
156-
// carry no box and form no stacking context, so all markers share the one
157-
// overlay's stacking context (the z-index fix), while giving each manager a
158-
// private scope for its index-based `.hotspot` reposition queries — hotspot
159-
// and Woo markers both use the `.hotspot` class, so they must not share a
160-
// query root.
161-
const hotspotGroup = document.createElement( 'div' );
162-
hotspotGroup.className = 'godam-image-hotspot-group';
163-
hotspotGroup.style.display = 'contents';
164-
const wooGroup = document.createElement( 'div' );
165-
wooGroup.className = 'godam-image-woo-group';
166-
wooGroup.style.display = 'contents';
167-
sharedEl.appendChild( hotspotGroup );
168-
sharedEl.appendChild( wooGroup );
149+
const host = createLayerHost( {
150+
player: adapter,
151+
media: { type: 'image', element: img },
152+
instanceId,
153+
} );
154+
cleanups.push( () => host.destroy() );
155+
156+
// One `display:contents` wrapper per layer inside the SINGLE overlay.
157+
// They carry no box and form no stacking context, so every marker
158+
// shares the one overlay's stacking context (the z-index fix) and
159+
// absolute markers resolve against the positioned frame, while each
160+
// layer gets a private scope for its manager's index-based `.hotspot`
161+
// reposition queries.
162+
const mountLayer = ( layer, runtime ) => {
163+
const layerEl = document.createElement( 'div' );
164+
layerEl.className = 'godam-image-layer-item';
165+
layerEl.dataset.layerType = String( layer?.type || '' );
166+
layerEl.style.display = 'contents';
167+
sharedEl.appendChild( layerEl );
168+
if ( ! host.mount( layer, layerEl, runtime ) ) {
169+
layerEl.remove();
170+
}
171+
};
169172

170-
// Merge every hotspot layer into ONE manager layer object so the manager's
171-
// index-based reposition stays consistent. Per-layer style is flattened
172-
// onto each hotspot (legacy per-hotspot fields) so distinct layer colours /
173-
// icons survive the merge.
174-
if ( hotspotLayers.length ) {
175-
const mergedHotspots = [];
176-
hotspotLayers.forEach( ( layer ) => {
177-
( layer.hotspots || [] ).forEach( ( hotspot ) => {
178-
const style = resolveHotspotStyle( layer, hotspot );
179-
// Button-style hotspots keep their own button fields; tag them so
180-
// the style-less merged layer still renders them as buttons.
181-
if ( style.styleType === 'button' ) {
182-
mergedHotspots.push( { ...hotspot, styleType: 'button' } );
173+
layers.forEach( ( layer ) => {
174+
const runtime = getLayerRuntime( layer?.type );
175+
if ( runtime ) {
176+
mountLayer( layer, runtime );
177+
return;
178+
}
179+
// Its view script may still load (deferred or delayed): mount it then.
180+
const stop = whenLayerRuntime( layer?.type, ( lateRuntime ) => {
181+
const mountLate = () => {
182+
if ( torn ) {
183183
return;
184184
}
185-
mergedHotspots.push( {
186-
...hotspot,
187-
icon: style.icon || '',
188-
customIconUrl: style.customIconUrl || null,
189-
backgroundColor: style.color,
190-
} );
191-
} );
192-
} );
193-
const mergedLayer = {
194-
id: 'godam-image-hotspots',
195-
type: 'hotspot',
196-
pauseOnHover: false,
197-
hotspots: mergedHotspots,
198-
};
199-
hotspotManager = new HotspotLayerManager( adapter, {}, instanceId );
200-
hotspotManager.computeContentRect = () => adapter.computeContentRect();
201-
hotspotManager.setupHotspotLayer( mergedLayer, hotspotGroup );
202-
const layerObj = hotspotManager.hotspotLayers[ hotspotManager.hotspotLayers.length - 1 ];
203-
// The analytics runtime is loaded on image pages now (see
204-
// godam-image/render.php), so plain-hotspot interaction emits are
205-
// captured like on video. Commerce impressions (Top Products) come from
206-
// the Woo layer's parent 'viewed' below; a plain hotspot has no product
207-
// row, so no impression beacon is fired here.
208-
hotspotManager.createHotspots( layerObj );
209-
}
210-
211-
// Woo layers — resolved from the shared registry (present only when the
212-
// godam-for-woo add-on is active). Each layer is set up INDIVIDUALLY on
213-
// its own container rather than merged into one: the Woo manager resolves
214-
// every marker's style (pulse / icon / custom icon + colours), behaviour
215-
// (clickBehaviour, tooltipDisplay, pauseOnHover) and analytics id from that
216-
// layer's own config, and its resize reposition queries `.hotspot` by index
217-
// WITHIN each layer's element against that layer's productHotspots. Merging
218-
// (the old behaviour) forced every product to inherit the first layer's
219-
// style/id, so a second layer's custom icon rendered as the first layer's
220-
// pulse. A single shared manager instance keeps the product cache/prefetch
221-
// shared across layers.
222-
if ( wooLayers.length ) {
223-
const WooManager = window.godamLayerRegistry?.getLayerManager?.( 'woo' );
224-
if ( WooManager ) {
225-
wooManager = new WooManager( adapter, {}, instanceId );
226-
wooManager.computeContentRect = () => adapter.computeContentRect();
227-
wooLayers.forEach( ( layer ) => {
228-
// A per-layer `display:contents` wrapper: it carries no box and
229-
// forms no stacking context, so all markers still share the ONE
230-
// overlay's stacking context (the z-index fix) and absolute
231-
// markers resolve against the positioned `.godam-image__frame`,
232-
// while giving each layer a private scope for the manager's
233-
// index-based `.hotspot` reposition queries.
234-
const layerEl = document.createElement( 'div' );
235-
layerEl.className = 'godam-image-woo-layer';
236-
layerEl.style.display = 'contents';
237-
wooGroup.appendChild( layerEl );
238-
wooManager.setupLayer( layer, layerEl );
239-
const layerObj = wooManager.wooLayers[ wooManager.wooLayers.length - 1 ];
240-
wooManager.createProductHotspots( layerObj );
241-
// A still image has no play/visibility transition, so fire the
242-
// parent-layer 'viewed' impression beacon once, now, on render.
243-
// This is the impressions signal the product rollup reads; without
244-
// it image Woo hotspots would always show 0 impressions. Guarded
245-
// no-op until the analytics runtime is loaded (it is a dependency
246-
// of this script, so it is present by now).
247-
if ( typeof wooManager.emitLayerVisible === 'function' ) {
248-
wooManager.emitLayerVisible( layerObj.layer );
185+
mountLayer( layer, lateRuntime );
186+
if ( runtimeUsesIconFont( lateRuntime, layer ) ) {
187+
renderFontAwesomeIcons( frame );
249188
}
250-
} );
251-
}
252-
}
189+
};
190+
if ( runtimeUsesIconFont( lateRuntime, layer ) ) {
191+
loadFontAwesome().then( mountLate ).catch( mountLate );
192+
} else {
193+
mountLate();
194+
}
195+
} );
196+
cleanups.push( stop );
197+
} );
198+
host.dispatch( 'onReady' );
253199

254200
// Reposition markers when the rendered image box changes. The listener
255201
// and observer are registered for teardown so redraws / unmounts in the
256202
// editor don't accumulate them (and don't pin a detached <img>).
257-
const reposition = () => {
258-
if ( hotspotManager && typeof hotspotManager.updateHotspotPositions === 'function' ) {
259-
hotspotManager.updateHotspotPositions();
260-
}
261-
if ( wooManager && typeof wooManager.updateProductHotspotPositions === 'function' ) {
262-
wooManager.updateProductHotspotPositions();
263-
}
264-
};
203+
const reposition = () => host.dispatch( 'onResize' );
265204
window.addEventListener( 'resize', reposition );
266205
cleanups.push( () => window.removeEventListener( 'resize', reposition ) );
267206
if ( 'ResizeObserver' in window ) {
@@ -274,11 +213,11 @@ export function initImageFrame( frame ) {
274213
// frame's document. In the block-editor iframe the global FA observer
275214
// never sees these nodes, so icon hotspots would otherwise render as
276215
// empty boxes; on the front end this is a harmless idempotent pass.
277-
if ( hasHotspotsWithIcons( layers ) ) {
216+
if ( layersUseIconFont( layers ) ) {
278217
renderFontAwesomeIcons( frame );
279218

280-
// Woo markers are appended asynchronously (createProductHotspots
281-
// resolves the products over REST first), so the pass above runs
219+
// Some markers are appended asynchronously (the Woo layer resolves
220+
// its products over REST first), so the pass above runs
282221
// before their `<i class="fa-solid ...">` nodes even exist. On the
283222
// front end FontAwesome's own dom.watch() observer catches them
284223
// late; inside the editor iframe it never does, leaving Woo product
@@ -313,7 +252,7 @@ export function initImageFrame( frame ) {
313252
const start = () => {
314253
// Load FontAwesome first when any hotspot uses an icon style, else the
315254
// icon glyphs render as empty boxes.
316-
if ( hasHotspotsWithIcons( layers ) ) {
255+
if ( layersUseIconFont( layers ) ) {
317256
loadFontAwesome().then( render ).catch( render );
318257
} else {
319258
render();

0 commit comments

Comments
 (0)