@digital-gravy/etch-public-api 0.6.0 → 0.7.0
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 +27 -0
- package/dist/index.d.cts +22 -5
- package/dist/index.d.ts +22 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -128,6 +128,33 @@ To discard in-memory changes and restore the original block:
|
|
|
128
128
|
etch.blocks.exitComponentEditMode({ revert: true });
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
+
### Component props
|
|
132
|
+
|
|
133
|
+
A component **instance** exposes its bound props as block attributes — set them
|
|
134
|
+
with `setAttribute` / `update`, read them with `getAttribute`. Unlike an HTML
|
|
135
|
+
block's free-form attributes, a component's props are a **closed schema**: the
|
|
136
|
+
key must be a property declared by the component definition, or the call throws
|
|
137
|
+
`INVALID_ARGUMENT`.
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const [cardId] = etch.blocks.find({ type: "etch/component" });
|
|
141
|
+
|
|
142
|
+
// Set a prop the component declares
|
|
143
|
+
etch.blocks.setAttribute(cardId, "title", "Hello world");
|
|
144
|
+
|
|
145
|
+
// Read it back
|
|
146
|
+
etch.blocks.getAttribute(cardId, "title"); // "Hello world"
|
|
147
|
+
|
|
148
|
+
// Or patch several props at once
|
|
149
|
+
etch.blocks.update(cardId, { attributes: { title: "Hi", variant: "primary" } });
|
|
150
|
+
|
|
151
|
+
// An undeclared key is rejected
|
|
152
|
+
etch.blocks.setAttribute(cardId, "notAProp", "x"); // ✗ throws INVALID_ARGUMENT
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
> Values are stored as-is. For typed props such as classes or repeater/group
|
|
156
|
+
> data, pass the value in the component's internal format.
|
|
157
|
+
|
|
131
158
|
### Error handling
|
|
132
159
|
|
|
133
160
|
Methods throw a typed `EtchApiError` with a `code`, rather than returning
|
package/dist/index.d.cts
CHANGED
|
@@ -256,8 +256,10 @@ interface BlockPatch {
|
|
|
256
256
|
/** Hide or show the block on the canvas. */
|
|
257
257
|
hidden?: boolean;
|
|
258
258
|
/**
|
|
259
|
-
* Merge
|
|
260
|
-
*
|
|
259
|
+
* Merge attributes — a key set to `undefined` removes that attribute. Valid
|
|
260
|
+
* on HTML blocks (free-form HTML attributes) and on component instances,
|
|
261
|
+
* whose attributes are their bound props — a closed schema, so each key must
|
|
262
|
+
* be a property declared by the component (an unknown key is rejected).
|
|
261
263
|
*/
|
|
262
264
|
attributes?: Record<string, string | undefined>;
|
|
263
265
|
/** Replace the text content. Only valid on text blocks. */
|
|
@@ -299,11 +301,26 @@ interface EtchBlocksApi {
|
|
|
299
301
|
setText(blockId: string, text: string): void;
|
|
300
302
|
/** Rename a block (sets its label / display name). */
|
|
301
303
|
rename(blockId: string, name: string): void;
|
|
302
|
-
/**
|
|
304
|
+
/**
|
|
305
|
+
* Read an attribute, or `undefined` when it is not set. Works on HTML blocks
|
|
306
|
+
* (HTML attributes) and component instances (bound prop values). Throws
|
|
307
|
+
* `WRONG_BLOCK_TYPE` for a block that has no attributes (e.g. a text block).
|
|
308
|
+
*/
|
|
303
309
|
getAttribute(blockId: string, key: string): string | undefined;
|
|
304
|
-
/**
|
|
310
|
+
/**
|
|
311
|
+
* Set an attribute; omit `value` for a valueless (boolean) attribute. Works
|
|
312
|
+
* on HTML blocks and component instances. On a component, `key` must be a
|
|
313
|
+
* declared property — props are a closed schema — otherwise
|
|
314
|
+
* `INVALID_ARGUMENT` is thrown (or `OPERATION_FAILED` if the component
|
|
315
|
+
* definition has not loaded yet). Throws `WRONG_BLOCK_TYPE` for a block with
|
|
316
|
+
* no attributes.
|
|
317
|
+
*/
|
|
305
318
|
setAttribute(blockId: string, key: string, value?: string): void;
|
|
306
|
-
/**
|
|
319
|
+
/**
|
|
320
|
+
* Remove an attribute. Works on HTML blocks and component instances. On a
|
|
321
|
+
* component, `key` must be a declared property (see {@link setAttribute}).
|
|
322
|
+
* Throws `WRONG_BLOCK_TYPE` for a block with no attributes.
|
|
323
|
+
*/
|
|
307
324
|
removeAttribute(blockId: string, key: string): void;
|
|
308
325
|
/** Add a CSS class to the block. */
|
|
309
326
|
addClass(blockId: string, className: string): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -256,8 +256,10 @@ interface BlockPatch {
|
|
|
256
256
|
/** Hide or show the block on the canvas. */
|
|
257
257
|
hidden?: boolean;
|
|
258
258
|
/**
|
|
259
|
-
* Merge
|
|
260
|
-
*
|
|
259
|
+
* Merge attributes — a key set to `undefined` removes that attribute. Valid
|
|
260
|
+
* on HTML blocks (free-form HTML attributes) and on component instances,
|
|
261
|
+
* whose attributes are their bound props — a closed schema, so each key must
|
|
262
|
+
* be a property declared by the component (an unknown key is rejected).
|
|
261
263
|
*/
|
|
262
264
|
attributes?: Record<string, string | undefined>;
|
|
263
265
|
/** Replace the text content. Only valid on text blocks. */
|
|
@@ -299,11 +301,26 @@ interface EtchBlocksApi {
|
|
|
299
301
|
setText(blockId: string, text: string): void;
|
|
300
302
|
/** Rename a block (sets its label / display name). */
|
|
301
303
|
rename(blockId: string, name: string): void;
|
|
302
|
-
/**
|
|
304
|
+
/**
|
|
305
|
+
* Read an attribute, or `undefined` when it is not set. Works on HTML blocks
|
|
306
|
+
* (HTML attributes) and component instances (bound prop values). Throws
|
|
307
|
+
* `WRONG_BLOCK_TYPE` for a block that has no attributes (e.g. a text block).
|
|
308
|
+
*/
|
|
303
309
|
getAttribute(blockId: string, key: string): string | undefined;
|
|
304
|
-
/**
|
|
310
|
+
/**
|
|
311
|
+
* Set an attribute; omit `value` for a valueless (boolean) attribute. Works
|
|
312
|
+
* on HTML blocks and component instances. On a component, `key` must be a
|
|
313
|
+
* declared property — props are a closed schema — otherwise
|
|
314
|
+
* `INVALID_ARGUMENT` is thrown (or `OPERATION_FAILED` if the component
|
|
315
|
+
* definition has not loaded yet). Throws `WRONG_BLOCK_TYPE` for a block with
|
|
316
|
+
* no attributes.
|
|
317
|
+
*/
|
|
305
318
|
setAttribute(blockId: string, key: string, value?: string): void;
|
|
306
|
-
/**
|
|
319
|
+
/**
|
|
320
|
+
* Remove an attribute. Works on HTML blocks and component instances. On a
|
|
321
|
+
* component, `key` must be a declared property (see {@link setAttribute}).
|
|
322
|
+
* Throws `WRONG_BLOCK_TYPE` for a block with no attributes.
|
|
323
|
+
*/
|
|
307
324
|
removeAttribute(blockId: string, key: string): void;
|
|
308
325
|
/** Add a CSS class to the block. */
|
|
309
326
|
addClass(blockId: string, className: string): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digital-gravy/etch-public-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
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
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|