@digital-gravy/etch-public-api 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -55,22 +55,68 @@ async function whenEtchReady(timeoutMs = 10_000) {
55
55
  const etch = await whenEtchReady();
56
56
  ```
57
57
 
58
- ### Reading and mutating
58
+ ### Reading and mutating blocks
59
59
 
60
60
  ```ts
61
61
  // Read
62
- const textIds = etch.blocks.find({ type: "text" });
62
+ const textIds = etch.blocks.find({ type: "etch/text" });
63
63
  const json = etch.blocks.getJson(textIds[0]);
64
64
 
65
65
  // Mutate (routes through the same guarded paths as the UI; undo/redo works)
66
66
  etch.blocks.setText(textIds[0], "Hello world");
67
67
  etch.blocks.addClass(textIds[0], "lead");
68
68
 
69
+ // Persist (blocks/styles wait for save; stylesheets/components/fields persist immediately)
70
+ await etch.saveAsync();
71
+ ```
72
+
73
+ ### Styles
74
+
75
+ ```ts
76
+ // Create a class or id rule
69
77
  const styleId = etch.styles.create(".lead", "font-size: 1.25rem;");
78
+
79
+ // Find existing styles by selector type
80
+ const classStyles = etch.styles.list({ type: "class" });
81
+ const myStyle = etch.styles.list().find((s) => s.selector === ".lead");
82
+ console.log(myStyle?.id); // the id you pass to blocks.addClass etc.
83
+
84
+ // Global CSS custom properties
70
85
  etch.styles.setVariable("--brand", "#0af");
86
+ etch.styles.getVariable("--brand"); // "#0af"
87
+ ```
71
88
 
72
- // Persist (blocks/styles wait for save; stylesheets/components/fields persist immediately)
89
+ > **Note:** The `collection` field on style objects is an internal implementation
90
+ > detail. Always omit the `collection` argument — the default collection is the
91
+ > only one supported via the public API.
92
+
93
+ ### Component edit mode
94
+
95
+ Use `blocks.enterComponentEditMode` to open a component's internal block tree
96
+ for direct inspection or mutation, then save and exit when done:
97
+
98
+ ```ts
99
+ // Find a component block
100
+ const [compId] = etch.blocks.find({ type: "etch/component" });
101
+
102
+ // Enter edit mode — the component's block tree becomes accessible
103
+ etch.blocks.enterComponentEditMode(compId);
104
+
105
+ // Inspect or mutate the component's children
106
+ const tree = etch.blocks.getTree();
107
+
108
+ // Persist the component definition to the backend
109
+ await etch.blocks.saveComponentEditModeAsync();
110
+
111
+ // Save the page, then exit edit mode
73
112
  await etch.saveAsync();
113
+ etch.blocks.exitComponentEditMode();
114
+ ```
115
+
116
+ To discard in-memory changes and restore the original block:
117
+
118
+ ```ts
119
+ etch.blocks.exitComponentEditMode({ revert: true });
74
120
  ```
75
121
 
76
122
  ### Error handling
@@ -142,10 +188,33 @@ etch.blocks.create({
142
188
  version: 1,
143
189
  context: {},
144
190
  children: [],
145
- tag: "div",
191
+ tag: "div", // ✗ type error
146
192
  });
147
193
  ```
148
194
 
195
+ ### Special element attributes
196
+
197
+ Some block types recognise **special attributes** in addition to standard HTML:
198
+
199
+ **`etch/dynamic-image`** — rendered as `<img>`:
200
+ - `mediaId` — WordPress attachment ID. Etch fetches the media object and uses its URL as `src`, overriding any explicit `src`. Supports dynamic expressions (e.g. `{post.featured_image_id}`).
201
+ - `useSrcSet` — `"true"` to generate a responsive `srcset` from the media (requires `mediaId`).
202
+ - `maximumSize` — WordPress image size slug (e.g. `"large"`, `"full"`) used when resolving the image. Defaults to `"full"`.
203
+
204
+ ```ts
205
+ etch.blocks.setAttribute(imgBlockId, "mediaId", "{post.featured_image_id}");
206
+ etch.blocks.setAttribute(imgBlockId, "useSrcSet", "true");
207
+ ```
208
+
209
+ **`etch/svg`** — inline SVG:
210
+ - `src` — URL of an external `.svg` file. Etch fetches and inlines the SVG at render time. Supports dynamic expressions.
211
+ - `stripColors` — `"true"` to strip `fill` and `stroke` colour declarations from the fetched SVG, so CSS can drive its colours instead.
212
+
213
+ ```ts
214
+ etch.blocks.setAttribute(svgBlockId, "src", "/icons/logo.svg");
215
+ etch.blocks.setAttribute(svgBlockId, "stripColors", "true");
216
+ ```
217
+
149
218
  ### Types only
150
219
 
151
220
  Every contract type is exported and dependency-free, so you can use them
@@ -167,7 +236,15 @@ is imported.
167
236
  - `getEtch(options?)` / `isEtchAvailable()` — acquire the API from the page.
168
237
  - `EtchApiError` / `isEtchApiError()` / `EtchApiErrorCode` — typed errors.
169
238
  - `ETCH_API_VERSION` — the contract version this package targets (`0.x`).
170
- - The full contract as exported types (`Etch`, `EtchBlocksApi`, `PublicBlockJson`, …).
239
+ - The full contract as exported types:
240
+ - **Blocks** — `Etch`, `EtchBlocksApi`, `EtchBlockJson`, `PublicBlockJson`, `FindBlocksPredicate`, `BlockPatch`, …
241
+ - **Styles** — `EtchStylesApi`, `StyleSummary`, `StyleListFilter`, `StyleSelectorType`, `StylePatch`
242
+ - **Stylesheets** — `EtchStylesheetsApi`, `StylesheetSummary`, `StylesheetInput`, `StylesheetPatch`
243
+ - **Components** — `EtchComponentsApi`, `PublicComponentSummary`, `PublicComponentJson`, `ComponentPatch`, `ComponentProperty`, …
244
+ - **Loops** — `EtchLoopsApi`, `EtchLoop`, `EtchLoopConfig`, `BlockLoopBinding`, …
245
+ - **Navigation** — `EtchNavigationApi`, `NavigationPlace`, `PostSummary`, `TemplateSummary`
246
+ - **Fields** — `EtchFieldsApi`, `CustomField`, `CustomFieldGroup`, …
247
+ - **UI / History** — `EtchUiApi`, `EtchHistoryApi`, `ColorScheme`
171
248
 
172
249
  ## Versioning
173
250
 
package/dist/index.d.cts CHANGED
@@ -1,18 +1,5 @@
1
1
  /**
2
- * The public, dependency-free contract for the Etch builder scripting API.
3
- *
4
- * Every type here is self-contained (no imports from the Etch source), so this
5
- * file can ship as the published `.d.ts` and be the single artifact that is
6
- * versioned deliberately. It mirrors the runtime exposed on `window.etch`.
7
- *
8
- * Method signatures take/return plain serializable values (block ids and JSON),
9
- * never internal reactive class instances. Loose internal shapes (block JSON,
10
- * loop query args, custom-field definitions) are modelled as **extensible**
11
- * objects with an index signature, matching the runtime's permissive parsing.
12
- *
13
- * Several string fields use the `'literal' | (string & {})` idiom: the known
14
- * values appear in autocomplete while any other string is still accepted (for
15
- * loop parameter expressions, plugin-added values, etc.).
2
+ * Block JSON shapes (read and write) and the `etch.blocks` API surface.
16
3
  */
17
4
  /** A block type identifier, always namespaced under `etch/` (e.g. `etch/text`). */
18
5
  type EtchBlockType = `etch/${string}`;
@@ -112,6 +99,16 @@ interface EtchDynamicElementBlockJson extends EtchBlockCommon {
112
99
  *
113
100
  * The global `styles` applied to the block are **read-only** (see
114
101
  * {@link EtchElementBlockJson}).
102
+ *
103
+ * **Special attributes** (in addition to standard `<img>` attributes):
104
+ * - `mediaId` — WordPress attachment post ID. When set, Etch fetches the media
105
+ * object and uses its URL as `src`, overriding any explicit `src` attribute.
106
+ * Supports dynamic expressions (`{post.featured_image_id}`).
107
+ * - `useSrcSet` — `"true"` to generate a responsive `srcset` from the media
108
+ * (only effective when `mediaId` is set).
109
+ * - `maximumSize` — maximum WordPress image size slug (e.g. `"large"`,
110
+ * `"full"`) used when building `srcset` / resolving the image URL. Defaults
111
+ * to `"full"` when omitted.
115
112
  */
116
113
  interface EtchDynamicImageBlockJson extends EtchBlockCommon {
117
114
  type: "etch/dynamic-image";
@@ -123,6 +120,13 @@ interface EtchDynamicImageBlockJson extends EtchBlockCommon {
123
120
  *
124
121
  * The global `styles` applied to the block are **read-only** (see
125
122
  * {@link EtchElementBlockJson}).
123
+ *
124
+ * **Special attributes** (in addition to standard SVG attributes):
125
+ * - `src` — URL of an external `.svg` file. When set, Etch fetches the file and
126
+ * inlines its contents at render time. Supports dynamic expressions.
127
+ * - `stripColors` — `"true"` to strip `fill` and `stroke` colour declarations
128
+ * from the fetched SVG so CSS can drive its colours instead. Only effective
129
+ * when `src` is set.
126
130
  */
127
131
  interface EtchSvgBlockJson extends EtchBlockCommon {
128
132
  type: "etch/svg";
@@ -307,7 +311,39 @@ interface EtchBlocksApi {
307
311
  removeClass(blockId: string, className: string): void;
308
312
  /** Whether the block currently has the given CSS class. */
309
313
  hasClass(blockId: string, className: string): boolean;
314
+ /**
315
+ * Enter component edit (focus) mode on an `etch/component` block. The block
316
+ * is replaced by a focus wrapper whose children are the component's block
317
+ * tree, allowing direct inspection and mutation. Throws `WRONG_BLOCK_TYPE`
318
+ * for non-component blocks and `OPERATION_FAILED` if already in component
319
+ * edit mode or the component definition is not loaded.
320
+ */
321
+ enterComponentEditMode(blockId: string): void;
322
+ /**
323
+ * Exit component edit (focus) mode. Does nothing when not in component edit
324
+ * mode. When `revert` is `true` the original component block is restored and
325
+ * any in-memory edits to the component definition are discarded; when
326
+ * omitted (or `false`) the focus wrapper is replaced by the component block
327
+ * as-is.
328
+ */
329
+ exitComponentEditMode(options?: {
330
+ revert?: boolean;
331
+ }): void;
332
+ /** Whether the builder is currently in component edit (focus) mode. */
333
+ isInComponentEditMode(): boolean;
334
+ /**
335
+ * Persist the component currently being edited and save the page. The
336
+ * builder stays in component edit mode after the call; follow with
337
+ * `exitComponentEditMode()` when done. Throws `OPERATION_FAILED` when not
338
+ * in component edit mode.
339
+ */
340
+ saveComponentEditModeAsync(): Promise<void>;
310
341
  }
342
+
343
+ /**
344
+ * Loop definitions, WordPress query argument shapes, and the `etch.loops` API
345
+ * surface.
346
+ */
311
347
  /**
312
348
  * A loop parameter reference, e.g. `$count`. Resolved to a concrete value
313
349
  * before the query runs.
@@ -500,32 +536,93 @@ interface EtchLoopsApi {
500
536
  /** Bind (or update the binding of) a loop on an `etch/loop` block. */
501
537
  setForBlock(blockId: string, loop: BlockLoopBinding): void;
502
538
  }
539
+
540
+ /**
541
+ * Global style (CSS rule) shapes and the `etch.styles` API surface.
542
+ */
543
+ /**
544
+ * The selector type inferred from a style's selector string.
545
+ * - `class` — `.my-class`
546
+ * - `id` — `#my-id`
547
+ * - `tag` — `div`, `h1`, etc.
548
+ * - `element` — etch element selector (`:where([data-etch-element="..."])`)
549
+ * - `attribute` — `[data-foo]`, `[type="submit"]`, etc.
550
+ * - `custom` — anything else (pseudo-classes, combinators, etc.)
551
+ */
552
+ type StyleSelectorType = "class" | "id" | "tag" | "element" | "attribute" | "custom";
553
+ /** A style entry as returned by `styles.list()`. */
554
+ interface StyleSummary {
555
+ /** Stable id of the style rule. */
556
+ id: string;
557
+ /** The CSS selector the rule targets. */
558
+ selector: string;
559
+ /**
560
+ * The selector type inferred from the selector string, or `undefined` when
561
+ * the selector is invalid.
562
+ */
563
+ type: StyleSelectorType | undefined;
564
+ /**
565
+ * The collection this style belongs to.
566
+ *
567
+ * @reserved Collections are an internal implementation detail. Always
568
+ * `"default"` for styles created via the public API. Do not rely on this
569
+ * value — it may change without notice.
570
+ */
571
+ collection: string;
572
+ /** The CSS declarations (rule body). */
573
+ css: string;
574
+ }
575
+ /** Filter accepted by `styles.list()`. All provided fields are AND-matched. */
576
+ interface StyleListFilter {
577
+ /** Restrict to styles whose inferred selector type matches. */
578
+ type?: StyleSelectorType;
579
+ /**
580
+ * Restrict to styles in this collection.
581
+ *
582
+ * @reserved Collections are an internal implementation detail. Omit this
583
+ * filter — filtering by collection is not supported for external use.
584
+ */
585
+ collection?: string;
586
+ }
503
587
  /** Patch accepted by `styles.update()`. Omitted fields keep their current value. */
504
588
  interface StylePatch {
505
589
  /** The CSS selector the rule targets. */
506
590
  selector?: string;
507
591
  /** The CSS declarations (rule body). */
508
592
  css?: string;
509
- /** The collection/folder the style belongs to. */
593
+ /**
594
+ * @reserved Collections are an internal implementation detail. Do not set
595
+ * this field — passing a value here has no guaranteed effect.
596
+ */
510
597
  collection?: string;
511
598
  }
512
599
  /** Global style (CSS) definitions, including `:root` global CSS variables. */
513
600
  interface EtchStylesApi {
601
+ /**
602
+ * All styles as a flat array, optionally filtered by selector `type`.
603
+ * Useful for resolving selector names to their ids before passing those ids
604
+ * to other methods.
605
+ */
606
+ list(filter?: StyleListFilter): StyleSummary[];
514
607
  /** Create a style rule for `selector`; returns its id. */
515
- create(selector: string, css?: string, collection?: string): string;
516
- /** Patch a style rule's selector, css, or collection. */
608
+ create(selector: string, css?: string): string;
609
+ /** Patch a style rule's selector or css. */
517
610
  update(styleId: string, patch: StylePatch): void;
518
611
  /** Delete a style rule by id. */
519
612
  delete(styleId: string): void;
520
613
  /** All global CSS custom properties as a `name -> value` record. */
521
- listVariables(collection?: string): Record<string, string>;
614
+ listVariables(): Record<string, string>;
522
615
  /** Read one global CSS custom property, or `undefined` when unset. */
523
- getVariable(name: string, collection?: string): string | undefined;
616
+ getVariable(name: string): string | undefined;
524
617
  /** Set a global CSS custom property (e.g. `('--brand', '#0af')`). */
525
- setVariable(name: string, value: string, collection?: string): void;
618
+ setVariable(name: string, value: string): void;
526
619
  /** Remove a global CSS custom property. */
527
- removeVariable(name: string, collection?: string): void;
620
+ removeVariable(name: string): void;
528
621
  }
622
+
623
+ /**
624
+ * Global stylesheet shapes and the `etch.stylesheets` API surface.
625
+ */
529
626
  /** Type of a global stylesheet. */
530
627
  type StylesheetType = "default" | "@custom-media";
531
628
  /** A global stylesheet entry. */
@@ -581,6 +678,12 @@ interface EtchStylesheetsApi {
581
678
  /** Add (or look up) a `@custom-media` definition, e.g. `('--sm', '(max-width: 600px)')`. */
582
679
  addCustomMediaAsync(name: string, query: string): Promise<void>;
583
680
  }
681
+
682
+ /**
683
+ * Reusable-component property model, component JSON shapes, and the
684
+ * `etch.components` API surface.
685
+ */
686
+
584
687
  /** Fields shared by every component property. */
585
688
  interface ComponentPropertyBase {
586
689
  /** Display name of the property. */
@@ -764,7 +867,11 @@ interface PublicComponentSummary {
764
867
  id: number;
765
868
  /** Display name. */
766
869
  name: string;
767
- /** Stable key used to reference the component in markup. */
870
+ /**
871
+ * Stable key used to reference the component in markup (e.g. `<Card />`).
872
+ * Always **PascalCase**: starts with an uppercase letter, letters and digits
873
+ * only (no dashes, underscores, or spaces).
874
+ */
768
875
  key: string;
769
876
  /** Optional human-readable description. */
770
877
  description?: string;
@@ -780,7 +887,11 @@ interface PublicComponentJson extends PublicComponentSummary {
780
887
  interface ComponentPatch {
781
888
  /** New display name. */
782
889
  name?: string;
783
- /** New reference key. */
890
+ /**
891
+ * New reference key. It is **converted to PascalCase** before it is stored
892
+ * (e.g. `my card` → `MyCard`), so the key actually saved may differ from the
893
+ * string passed here.
894
+ */
784
895
  key?: string;
785
896
  /** New description. */
786
897
  description?: string;
@@ -807,6 +918,10 @@ interface EtchComponentsApi {
807
918
  /** Delete a component by id. */
808
919
  deleteAsync(componentId: number): Promise<void>;
809
920
  }
921
+
922
+ /**
923
+ * Builder navigation shapes and the `etch.navigation` API surface.
924
+ */
810
925
  /**
811
926
  * A navigable area of the builder UI:
812
927
  * - `builder` — the canvas/editor
@@ -859,6 +974,10 @@ interface EtchNavigationApi {
859
974
  /** List available templates. */
860
975
  listTemplatesAsync(): Promise<TemplateSummary[]>;
861
976
  }
977
+
978
+ /**
979
+ * Custom field group/value shapes and the `etch.fields` API surface.
980
+ */
862
981
  /** Custom field type. Open-ended for future field types. */
863
982
  type CustomFieldType = "text" | "textarea" | "number" | "boolean" | (string & {});
864
983
  /** A custom field definition (extensible). */
@@ -975,6 +1094,10 @@ interface EtchFieldsApi {
975
1094
  /** Clear one field value on a post. */
976
1095
  deleteValueAsync(postId: number, fieldKey: string): Promise<void>;
977
1096
  }
1097
+
1098
+ /**
1099
+ * Builder chrome controls (`etch.ui`) and undo/redo (`etch.history`).
1100
+ */
978
1101
  /** Canvas color scheme. */
979
1102
  type ColorScheme = "light" | "dark";
980
1103
  /** Builder app/chrome controls: color scheme, interface visibility, exit. */
@@ -1005,6 +1128,12 @@ interface EtchHistoryApi {
1005
1128
  /** Whether there is a mutation to redo. */
1006
1129
  canRedo(): boolean;
1007
1130
  }
1131
+
1132
+ /**
1133
+ * The root {@link Etch} interface exposed on `window.etch`, tying every API
1134
+ * namespace together, plus connection/versioning options.
1135
+ */
1136
+
1008
1137
  /** Options for negotiating an API instance (see the runtime `connect`). */
1009
1138
  interface ConnectOptions {
1010
1139
  /**
@@ -1126,4 +1255,4 @@ declare function isEtchApiError(value: unknown): value is EtchApiError;
1126
1255
  */
1127
1256
  declare const ETCH_API_VERSION = "0.x";
1128
1257
 
1129
- export { type ArrayComponentProperty, type BlockLoopBinding, type BlockPatch, type BooleanComponentProperty, type BooleanParam, type ClassComponentProperty, type ColorScheme, type ComponentPatch, type ComponentProperty, type ComponentPropertyBase, type ConditionComponentProperty, type ConnectOptions, type CustomField, type CustomFieldAssignment, type CustomFieldGroup, type CustomFieldType, ETCH_API_VERSION, type Etch, EtchApiError, type EtchApiErrorCode, type EtchBlockCommon, type EtchBlockContext, type EtchBlockJson, type EtchBlockOptions, type EtchBlockScript, type EtchBlockType, type EtchBlockTypeName, type EtchBlocksApi, type EtchComponentBlockJson, type EtchComponentsApi, type EtchConditionBlockJson, type EtchDynamicElementBlockJson, type EtchDynamicImageBlockJson, type EtchElementBlockJson, type EtchFieldsApi, type EtchHistoryApi, type EtchHtmlAttributes, type EtchLoop, type EtchLoopBlockJson, type EtchLoopConfig, type EtchLoopObj, type EtchLoopsApi, type EtchNavigationApi, type EtchPassthroughBlockJson, type EtchPostContentBlockJson, type EtchRawHtmlBlockJson, type EtchSlotContentBlockJson, type EtchSlotPlaceholderBlockJson, type EtchStylesApi, type EtchStylesheetsApi, type EtchSvgBlockJson, type EtchTextBlockJson, type EtchUiApi, type FindBlocksPredicate, type GroupComponentProperty, type GutenbergBlock, type LoopParamRef, type MetaQueryItem, type NavigationPlace, type NumberComponentProperty, type NumericParam, type ObjectComponentProperty, type PostCustomFieldGroupEntry, type PostCustomFieldValueEntry, type PostCustomFieldValueResponse, type PostCustomFieldValuesResponse, type PostSummary, type PublicBlockJson, type PublicComponentJson, type PublicComponentSummary, type RepeaterComponentProperty, type ResolvedCustomField, type SelectOptionsString, type StringComponentProperty, type StylePatch, type StylesheetInput, type StylesheetPatch, type StylesheetSummary, type StylesheetType, type TaxQueryItem, type TemplateSummary, type WpQueryArgs, type WpTermsArgs, type WpUsersArgs, getEtch, isEtchApiError, isEtchAvailable };
1258
+ export { type ArrayComponentProperty, type BlockLoopBinding, type BlockPatch, type BooleanComponentProperty, type BooleanParam, type ClassComponentProperty, type ColorScheme, type ComponentPatch, type ComponentProperty, type ComponentPropertyBase, type ConditionComponentProperty, type ConnectOptions, type CustomField, type CustomFieldAssignment, type CustomFieldGroup, type CustomFieldType, ETCH_API_VERSION, type Etch, EtchApiError, type EtchApiErrorCode, type EtchBlockCommon, type EtchBlockContext, type EtchBlockJson, type EtchBlockOptions, type EtchBlockScript, type EtchBlockType, type EtchBlockTypeName, type EtchBlocksApi, type EtchComponentBlockJson, type EtchComponentsApi, type EtchConditionBlockJson, type EtchDynamicElementBlockJson, type EtchDynamicImageBlockJson, type EtchElementBlockJson, type EtchFieldsApi, type EtchHistoryApi, type EtchHtmlAttributes, type EtchLoop, type EtchLoopBlockJson, type EtchLoopConfig, type EtchLoopObj, type EtchLoopsApi, type EtchNavigationApi, type EtchPassthroughBlockJson, type EtchPostContentBlockJson, type EtchRawHtmlBlockJson, type EtchSlotContentBlockJson, type EtchSlotPlaceholderBlockJson, type EtchStylesApi, type EtchStylesheetsApi, type EtchSvgBlockJson, type EtchTextBlockJson, type EtchUiApi, type FindBlocksPredicate, type GroupComponentProperty, type GutenbergBlock, type LoopParamRef, type MetaQueryItem, type NavigationPlace, type NumberComponentProperty, type NumericParam, type ObjectComponentProperty, type PostCustomFieldGroupEntry, type PostCustomFieldValueEntry, type PostCustomFieldValueResponse, type PostCustomFieldValuesResponse, type PostSummary, type PublicBlockJson, type PublicComponentJson, type PublicComponentSummary, type RepeaterComponentProperty, type ResolvedCustomField, type SelectOptionsString, type StringComponentProperty, type StyleListFilter, type StylePatch, type StyleSelectorType, type StyleSummary, type StylesheetInput, type StylesheetPatch, type StylesheetSummary, type StylesheetType, type TaxQueryItem, type TemplateSummary, type WpQueryArgs, type WpTermsArgs, type WpUsersArgs, getEtch, isEtchApiError, isEtchAvailable };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,5 @@
1
1
  /**
2
- * The public, dependency-free contract for the Etch builder scripting API.
3
- *
4
- * Every type here is self-contained (no imports from the Etch source), so this
5
- * file can ship as the published `.d.ts` and be the single artifact that is
6
- * versioned deliberately. It mirrors the runtime exposed on `window.etch`.
7
- *
8
- * Method signatures take/return plain serializable values (block ids and JSON),
9
- * never internal reactive class instances. Loose internal shapes (block JSON,
10
- * loop query args, custom-field definitions) are modelled as **extensible**
11
- * objects with an index signature, matching the runtime's permissive parsing.
12
- *
13
- * Several string fields use the `'literal' | (string & {})` idiom: the known
14
- * values appear in autocomplete while any other string is still accepted (for
15
- * loop parameter expressions, plugin-added values, etc.).
2
+ * Block JSON shapes (read and write) and the `etch.blocks` API surface.
16
3
  */
17
4
  /** A block type identifier, always namespaced under `etch/` (e.g. `etch/text`). */
18
5
  type EtchBlockType = `etch/${string}`;
@@ -112,6 +99,16 @@ interface EtchDynamicElementBlockJson extends EtchBlockCommon {
112
99
  *
113
100
  * The global `styles` applied to the block are **read-only** (see
114
101
  * {@link EtchElementBlockJson}).
102
+ *
103
+ * **Special attributes** (in addition to standard `<img>` attributes):
104
+ * - `mediaId` — WordPress attachment post ID. When set, Etch fetches the media
105
+ * object and uses its URL as `src`, overriding any explicit `src` attribute.
106
+ * Supports dynamic expressions (`{post.featured_image_id}`).
107
+ * - `useSrcSet` — `"true"` to generate a responsive `srcset` from the media
108
+ * (only effective when `mediaId` is set).
109
+ * - `maximumSize` — maximum WordPress image size slug (e.g. `"large"`,
110
+ * `"full"`) used when building `srcset` / resolving the image URL. Defaults
111
+ * to `"full"` when omitted.
115
112
  */
116
113
  interface EtchDynamicImageBlockJson extends EtchBlockCommon {
117
114
  type: "etch/dynamic-image";
@@ -123,6 +120,13 @@ interface EtchDynamicImageBlockJson extends EtchBlockCommon {
123
120
  *
124
121
  * The global `styles` applied to the block are **read-only** (see
125
122
  * {@link EtchElementBlockJson}).
123
+ *
124
+ * **Special attributes** (in addition to standard SVG attributes):
125
+ * - `src` — URL of an external `.svg` file. When set, Etch fetches the file and
126
+ * inlines its contents at render time. Supports dynamic expressions.
127
+ * - `stripColors` — `"true"` to strip `fill` and `stroke` colour declarations
128
+ * from the fetched SVG so CSS can drive its colours instead. Only effective
129
+ * when `src` is set.
126
130
  */
127
131
  interface EtchSvgBlockJson extends EtchBlockCommon {
128
132
  type: "etch/svg";
@@ -307,7 +311,39 @@ interface EtchBlocksApi {
307
311
  removeClass(blockId: string, className: string): void;
308
312
  /** Whether the block currently has the given CSS class. */
309
313
  hasClass(blockId: string, className: string): boolean;
314
+ /**
315
+ * Enter component edit (focus) mode on an `etch/component` block. The block
316
+ * is replaced by a focus wrapper whose children are the component's block
317
+ * tree, allowing direct inspection and mutation. Throws `WRONG_BLOCK_TYPE`
318
+ * for non-component blocks and `OPERATION_FAILED` if already in component
319
+ * edit mode or the component definition is not loaded.
320
+ */
321
+ enterComponentEditMode(blockId: string): void;
322
+ /**
323
+ * Exit component edit (focus) mode. Does nothing when not in component edit
324
+ * mode. When `revert` is `true` the original component block is restored and
325
+ * any in-memory edits to the component definition are discarded; when
326
+ * omitted (or `false`) the focus wrapper is replaced by the component block
327
+ * as-is.
328
+ */
329
+ exitComponentEditMode(options?: {
330
+ revert?: boolean;
331
+ }): void;
332
+ /** Whether the builder is currently in component edit (focus) mode. */
333
+ isInComponentEditMode(): boolean;
334
+ /**
335
+ * Persist the component currently being edited and save the page. The
336
+ * builder stays in component edit mode after the call; follow with
337
+ * `exitComponentEditMode()` when done. Throws `OPERATION_FAILED` when not
338
+ * in component edit mode.
339
+ */
340
+ saveComponentEditModeAsync(): Promise<void>;
310
341
  }
342
+
343
+ /**
344
+ * Loop definitions, WordPress query argument shapes, and the `etch.loops` API
345
+ * surface.
346
+ */
311
347
  /**
312
348
  * A loop parameter reference, e.g. `$count`. Resolved to a concrete value
313
349
  * before the query runs.
@@ -500,32 +536,93 @@ interface EtchLoopsApi {
500
536
  /** Bind (or update the binding of) a loop on an `etch/loop` block. */
501
537
  setForBlock(blockId: string, loop: BlockLoopBinding): void;
502
538
  }
539
+
540
+ /**
541
+ * Global style (CSS rule) shapes and the `etch.styles` API surface.
542
+ */
543
+ /**
544
+ * The selector type inferred from a style's selector string.
545
+ * - `class` — `.my-class`
546
+ * - `id` — `#my-id`
547
+ * - `tag` — `div`, `h1`, etc.
548
+ * - `element` — etch element selector (`:where([data-etch-element="..."])`)
549
+ * - `attribute` — `[data-foo]`, `[type="submit"]`, etc.
550
+ * - `custom` — anything else (pseudo-classes, combinators, etc.)
551
+ */
552
+ type StyleSelectorType = "class" | "id" | "tag" | "element" | "attribute" | "custom";
553
+ /** A style entry as returned by `styles.list()`. */
554
+ interface StyleSummary {
555
+ /** Stable id of the style rule. */
556
+ id: string;
557
+ /** The CSS selector the rule targets. */
558
+ selector: string;
559
+ /**
560
+ * The selector type inferred from the selector string, or `undefined` when
561
+ * the selector is invalid.
562
+ */
563
+ type: StyleSelectorType | undefined;
564
+ /**
565
+ * The collection this style belongs to.
566
+ *
567
+ * @reserved Collections are an internal implementation detail. Always
568
+ * `"default"` for styles created via the public API. Do not rely on this
569
+ * value — it may change without notice.
570
+ */
571
+ collection: string;
572
+ /** The CSS declarations (rule body). */
573
+ css: string;
574
+ }
575
+ /** Filter accepted by `styles.list()`. All provided fields are AND-matched. */
576
+ interface StyleListFilter {
577
+ /** Restrict to styles whose inferred selector type matches. */
578
+ type?: StyleSelectorType;
579
+ /**
580
+ * Restrict to styles in this collection.
581
+ *
582
+ * @reserved Collections are an internal implementation detail. Omit this
583
+ * filter — filtering by collection is not supported for external use.
584
+ */
585
+ collection?: string;
586
+ }
503
587
  /** Patch accepted by `styles.update()`. Omitted fields keep their current value. */
504
588
  interface StylePatch {
505
589
  /** The CSS selector the rule targets. */
506
590
  selector?: string;
507
591
  /** The CSS declarations (rule body). */
508
592
  css?: string;
509
- /** The collection/folder the style belongs to. */
593
+ /**
594
+ * @reserved Collections are an internal implementation detail. Do not set
595
+ * this field — passing a value here has no guaranteed effect.
596
+ */
510
597
  collection?: string;
511
598
  }
512
599
  /** Global style (CSS) definitions, including `:root` global CSS variables. */
513
600
  interface EtchStylesApi {
601
+ /**
602
+ * All styles as a flat array, optionally filtered by selector `type`.
603
+ * Useful for resolving selector names to their ids before passing those ids
604
+ * to other methods.
605
+ */
606
+ list(filter?: StyleListFilter): StyleSummary[];
514
607
  /** Create a style rule for `selector`; returns its id. */
515
- create(selector: string, css?: string, collection?: string): string;
516
- /** Patch a style rule's selector, css, or collection. */
608
+ create(selector: string, css?: string): string;
609
+ /** Patch a style rule's selector or css. */
517
610
  update(styleId: string, patch: StylePatch): void;
518
611
  /** Delete a style rule by id. */
519
612
  delete(styleId: string): void;
520
613
  /** All global CSS custom properties as a `name -> value` record. */
521
- listVariables(collection?: string): Record<string, string>;
614
+ listVariables(): Record<string, string>;
522
615
  /** Read one global CSS custom property, or `undefined` when unset. */
523
- getVariable(name: string, collection?: string): string | undefined;
616
+ getVariable(name: string): string | undefined;
524
617
  /** Set a global CSS custom property (e.g. `('--brand', '#0af')`). */
525
- setVariable(name: string, value: string, collection?: string): void;
618
+ setVariable(name: string, value: string): void;
526
619
  /** Remove a global CSS custom property. */
527
- removeVariable(name: string, collection?: string): void;
620
+ removeVariable(name: string): void;
528
621
  }
622
+
623
+ /**
624
+ * Global stylesheet shapes and the `etch.stylesheets` API surface.
625
+ */
529
626
  /** Type of a global stylesheet. */
530
627
  type StylesheetType = "default" | "@custom-media";
531
628
  /** A global stylesheet entry. */
@@ -581,6 +678,12 @@ interface EtchStylesheetsApi {
581
678
  /** Add (or look up) a `@custom-media` definition, e.g. `('--sm', '(max-width: 600px)')`. */
582
679
  addCustomMediaAsync(name: string, query: string): Promise<void>;
583
680
  }
681
+
682
+ /**
683
+ * Reusable-component property model, component JSON shapes, and the
684
+ * `etch.components` API surface.
685
+ */
686
+
584
687
  /** Fields shared by every component property. */
585
688
  interface ComponentPropertyBase {
586
689
  /** Display name of the property. */
@@ -764,7 +867,11 @@ interface PublicComponentSummary {
764
867
  id: number;
765
868
  /** Display name. */
766
869
  name: string;
767
- /** Stable key used to reference the component in markup. */
870
+ /**
871
+ * Stable key used to reference the component in markup (e.g. `<Card />`).
872
+ * Always **PascalCase**: starts with an uppercase letter, letters and digits
873
+ * only (no dashes, underscores, or spaces).
874
+ */
768
875
  key: string;
769
876
  /** Optional human-readable description. */
770
877
  description?: string;
@@ -780,7 +887,11 @@ interface PublicComponentJson extends PublicComponentSummary {
780
887
  interface ComponentPatch {
781
888
  /** New display name. */
782
889
  name?: string;
783
- /** New reference key. */
890
+ /**
891
+ * New reference key. It is **converted to PascalCase** before it is stored
892
+ * (e.g. `my card` → `MyCard`), so the key actually saved may differ from the
893
+ * string passed here.
894
+ */
784
895
  key?: string;
785
896
  /** New description. */
786
897
  description?: string;
@@ -807,6 +918,10 @@ interface EtchComponentsApi {
807
918
  /** Delete a component by id. */
808
919
  deleteAsync(componentId: number): Promise<void>;
809
920
  }
921
+
922
+ /**
923
+ * Builder navigation shapes and the `etch.navigation` API surface.
924
+ */
810
925
  /**
811
926
  * A navigable area of the builder UI:
812
927
  * - `builder` — the canvas/editor
@@ -859,6 +974,10 @@ interface EtchNavigationApi {
859
974
  /** List available templates. */
860
975
  listTemplatesAsync(): Promise<TemplateSummary[]>;
861
976
  }
977
+
978
+ /**
979
+ * Custom field group/value shapes and the `etch.fields` API surface.
980
+ */
862
981
  /** Custom field type. Open-ended for future field types. */
863
982
  type CustomFieldType = "text" | "textarea" | "number" | "boolean" | (string & {});
864
983
  /** A custom field definition (extensible). */
@@ -975,6 +1094,10 @@ interface EtchFieldsApi {
975
1094
  /** Clear one field value on a post. */
976
1095
  deleteValueAsync(postId: number, fieldKey: string): Promise<void>;
977
1096
  }
1097
+
1098
+ /**
1099
+ * Builder chrome controls (`etch.ui`) and undo/redo (`etch.history`).
1100
+ */
978
1101
  /** Canvas color scheme. */
979
1102
  type ColorScheme = "light" | "dark";
980
1103
  /** Builder app/chrome controls: color scheme, interface visibility, exit. */
@@ -1005,6 +1128,12 @@ interface EtchHistoryApi {
1005
1128
  /** Whether there is a mutation to redo. */
1006
1129
  canRedo(): boolean;
1007
1130
  }
1131
+
1132
+ /**
1133
+ * The root {@link Etch} interface exposed on `window.etch`, tying every API
1134
+ * namespace together, plus connection/versioning options.
1135
+ */
1136
+
1008
1137
  /** Options for negotiating an API instance (see the runtime `connect`). */
1009
1138
  interface ConnectOptions {
1010
1139
  /**
@@ -1126,4 +1255,4 @@ declare function isEtchApiError(value: unknown): value is EtchApiError;
1126
1255
  */
1127
1256
  declare const ETCH_API_VERSION = "0.x";
1128
1257
 
1129
- export { type ArrayComponentProperty, type BlockLoopBinding, type BlockPatch, type BooleanComponentProperty, type BooleanParam, type ClassComponentProperty, type ColorScheme, type ComponentPatch, type ComponentProperty, type ComponentPropertyBase, type ConditionComponentProperty, type ConnectOptions, type CustomField, type CustomFieldAssignment, type CustomFieldGroup, type CustomFieldType, ETCH_API_VERSION, type Etch, EtchApiError, type EtchApiErrorCode, type EtchBlockCommon, type EtchBlockContext, type EtchBlockJson, type EtchBlockOptions, type EtchBlockScript, type EtchBlockType, type EtchBlockTypeName, type EtchBlocksApi, type EtchComponentBlockJson, type EtchComponentsApi, type EtchConditionBlockJson, type EtchDynamicElementBlockJson, type EtchDynamicImageBlockJson, type EtchElementBlockJson, type EtchFieldsApi, type EtchHistoryApi, type EtchHtmlAttributes, type EtchLoop, type EtchLoopBlockJson, type EtchLoopConfig, type EtchLoopObj, type EtchLoopsApi, type EtchNavigationApi, type EtchPassthroughBlockJson, type EtchPostContentBlockJson, type EtchRawHtmlBlockJson, type EtchSlotContentBlockJson, type EtchSlotPlaceholderBlockJson, type EtchStylesApi, type EtchStylesheetsApi, type EtchSvgBlockJson, type EtchTextBlockJson, type EtchUiApi, type FindBlocksPredicate, type GroupComponentProperty, type GutenbergBlock, type LoopParamRef, type MetaQueryItem, type NavigationPlace, type NumberComponentProperty, type NumericParam, type ObjectComponentProperty, type PostCustomFieldGroupEntry, type PostCustomFieldValueEntry, type PostCustomFieldValueResponse, type PostCustomFieldValuesResponse, type PostSummary, type PublicBlockJson, type PublicComponentJson, type PublicComponentSummary, type RepeaterComponentProperty, type ResolvedCustomField, type SelectOptionsString, type StringComponentProperty, type StylePatch, type StylesheetInput, type StylesheetPatch, type StylesheetSummary, type StylesheetType, type TaxQueryItem, type TemplateSummary, type WpQueryArgs, type WpTermsArgs, type WpUsersArgs, getEtch, isEtchApiError, isEtchAvailable };
1258
+ export { type ArrayComponentProperty, type BlockLoopBinding, type BlockPatch, type BooleanComponentProperty, type BooleanParam, type ClassComponentProperty, type ColorScheme, type ComponentPatch, type ComponentProperty, type ComponentPropertyBase, type ConditionComponentProperty, type ConnectOptions, type CustomField, type CustomFieldAssignment, type CustomFieldGroup, type CustomFieldType, ETCH_API_VERSION, type Etch, EtchApiError, type EtchApiErrorCode, type EtchBlockCommon, type EtchBlockContext, type EtchBlockJson, type EtchBlockOptions, type EtchBlockScript, type EtchBlockType, type EtchBlockTypeName, type EtchBlocksApi, type EtchComponentBlockJson, type EtchComponentsApi, type EtchConditionBlockJson, type EtchDynamicElementBlockJson, type EtchDynamicImageBlockJson, type EtchElementBlockJson, type EtchFieldsApi, type EtchHistoryApi, type EtchHtmlAttributes, type EtchLoop, type EtchLoopBlockJson, type EtchLoopConfig, type EtchLoopObj, type EtchLoopsApi, type EtchNavigationApi, type EtchPassthroughBlockJson, type EtchPostContentBlockJson, type EtchRawHtmlBlockJson, type EtchSlotContentBlockJson, type EtchSlotPlaceholderBlockJson, type EtchStylesApi, type EtchStylesheetsApi, type EtchSvgBlockJson, type EtchTextBlockJson, type EtchUiApi, type FindBlocksPredicate, type GroupComponentProperty, type GutenbergBlock, type LoopParamRef, type MetaQueryItem, type NavigationPlace, type NumberComponentProperty, type NumericParam, type ObjectComponentProperty, type PostCustomFieldGroupEntry, type PostCustomFieldValueEntry, type PostCustomFieldValueResponse, type PostCustomFieldValuesResponse, type PostSummary, type PublicBlockJson, type PublicComponentJson, type PublicComponentSummary, type RepeaterComponentProperty, type ResolvedCustomField, type SelectOptionsString, type StringComponentProperty, type StyleListFilter, type StylePatch, type StyleSelectorType, type StyleSummary, type StylesheetInput, type StylesheetPatch, type StylesheetSummary, type StylesheetType, type TaxQueryItem, type TemplateSummary, type WpQueryArgs, type WpTermsArgs, type WpUsersArgs, getEtch, isEtchApiError, isEtchAvailable };
package/package.json CHANGED
@@ -1,43 +1,46 @@
1
1
  {
2
- "name": "@digital-gravy/etch-public-api",
3
- "version": "0.3.1",
4
- "description": "MIT-licensed typed client and contract for the Etch builder scripting API (window.etch). Etch itself is a separate proprietary product governed by its own commercial terms.",
5
- "license": "MIT",
6
- "type": "module",
7
- "main": "./dist/index.cjs",
8
- "module": "./dist/index.js",
9
- "types": "./dist/index.d.ts",
10
- "exports": {
11
- ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.js",
14
- "require": "./dist/index.cjs"
15
- }
16
- },
17
- "files": ["dist", "README.md"],
18
- "sideEffects": false,
19
- "scripts": {
20
- "build": "tsup",
21
- "dev": "tsup --watch",
22
- "test": "vitest run",
23
- "test:watch": "vitest",
24
- "typecheck": "tsc --noEmit",
25
- "prepublishOnly": "bun run build"
26
- },
27
- "keywords": [
28
- "etch",
29
- "etchwp",
30
- "wordpress",
31
- "builder",
32
- "scripting",
33
- "api"
34
- ],
35
- "publishConfig": {
36
- "access": "public"
37
- },
38
- "devDependencies": {
39
- "tsup": "^8.3.5",
40
- "typescript": "^5.7.2",
41
- "vitest": "^3.0.5"
42
- }
2
+ "name": "@digital-gravy/etch-public-api",
3
+ "version": "0.3.3",
4
+ "description": "MIT-licensed typed client and contract for the Etch builder scripting API (window.etch). Etch itself is a separate proprietary product governed by its own commercial terms.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md"
20
+ ],
21
+ "sideEffects": false,
22
+ "scripts": {
23
+ "build": "tsup",
24
+ "dev": "tsup --watch",
25
+ "test": "vitest run",
26
+ "test:watch": "vitest",
27
+ "typecheck": "tsc --noEmit",
28
+ "prepublishOnly": "bun run build"
29
+ },
30
+ "keywords": [
31
+ "etch",
32
+ "etchwp",
33
+ "wordpress",
34
+ "builder",
35
+ "scripting",
36
+ "api"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "devDependencies": {
42
+ "tsup": "^8.3.5",
43
+ "typescript": "^5.7.2",
44
+ "vitest": "^3.0.5"
45
+ }
43
46
  }