Building blocks
Drawing on a block in the editor
Drawing on a block in the editor (overlays)
A block's preview is server-rendered and knows nothing of its children, and no JS runs in it. When a block needs something drawn on the canvas from the block store — pins on a map from its child cards, numbers on a timeline — give it its own editor script and draw through the bridge:
// block.json
"editorScript": "file:./editor.js"// editor.js — plain JS, no build step
window.gcbLiteEditor.overlay( 'gcb/touchpoint-zoom', ( ctx ) => {
const layer = ctx.element.querySelector( '.pins' );
layer.querySelectorAll( '.pin' ).forEach( ( n ) => n.remove() ); // idempotent
ctx.children.forEach( ( card ) => { /* draw from card.attributes */ } );
// ctx.select( id ), ctx.update( id, attrs ), ctx.selectedId, ctx.doc, ctx.rerender()
} );The render runs for each instance of the block on the canvas, and again when
its attributes, its children's attributes or the selection change, or when its
own or a child's preview is re-rendered — batched to one pass per frame. GCB's
editor bundle always loads first (your editorScript is added beside it, and
made to depend on it), and a block's own files are versioned by their modified
time, so an edit is never served stale.
A text/textarea/richtext sub-field of one row of a repeater field can be
typed on the canvas like a top-level field. Tag it with the row and the
sub-field as well as the field:
<span data-gcb-field="locations" data-gcb-row="<?php echo esc_attr($row['_id']); ?>"
data-gcb-subfield="area" data-gcb-field-type="text"><?php echo esc_html($row['area']); ?></span>data-gcb-row is the row's _id (stable when rows are dragged), or its
index when a row has none. Only those words are masked from the preview's
refetch, so moving a pin or adding a row still refreshes the preview.
Render the element even when the value is empty, or there is nothing to
click into.
The full editor bridge is in JavaScript API.