morphic-blocks 0.3.0 → 0.5.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.
Files changed (45) hide show
  1. package/README.md +26 -21
  2. package/definitions.schema.json +147 -14
  3. package/dist/cli.js +6 -17
  4. package/dist/copy-media-CllRyMqQ.js +21 -0
  5. package/dist/copy-media.d.ts +8 -0
  6. package/dist/esbuild.d.ts +3 -0
  7. package/dist/esbuild.js +10 -0
  8. package/dist/index.d.ts +4 -1
  9. package/dist/morphic/MorphicBlocks.d.ts +141 -66
  10. package/dist/morphic/asset-urls.d.ts +9 -0
  11. package/dist/morphic/block-sets.d.ts +12 -0
  12. package/dist/morphic/block-view.d.ts +72 -4
  13. package/dist/morphic/check-definitions.d.ts +31 -0
  14. package/dist/morphic/code-editor.d.ts +22 -3
  15. package/dist/morphic/constants.d.ts +6 -0
  16. package/dist/morphic/dropdown-fields.d.ts +72 -0
  17. package/dist/morphic/element-types.d.ts +1 -1
  18. package/dist/morphic/operators.d.ts +73 -0
  19. package/dist/morphic/parent-field.d.ts +7 -0
  20. package/dist/morphic/repeat-slots.d.ts +31 -0
  21. package/dist/morphic/resize.d.ts +7 -2
  22. package/dist/morphic/strict-slots.d.ts +10 -0
  23. package/dist/morphic/styles.d.ts +5 -0
  24. package/dist/morphic/template-codegen.d.ts +16 -0
  25. package/dist/morphic/template.d.ts +42 -1
  26. package/dist/morphic/text-to-blocks.d.ts +34 -0
  27. package/dist/morphic/toolbar.d.ts +8 -11
  28. package/dist/morphic/toolbox-canvas.d.ts +34 -11
  29. package/dist/morphic/types.d.ts +350 -67
  30. package/dist/morphic/validate-definitions.d.ts +4 -1
  31. package/dist/morphic/value-type.d.ts +13 -0
  32. package/dist/morphic/view-resolver.d.ts +5 -0
  33. package/dist/morphic-blocks.js +4432 -2511
  34. package/dist/morphic-blocks.umd.cjs +43 -23
  35. package/dist/plugin-h3QH8ods.js +19 -0
  36. package/dist/plugin.d.ts +9 -0
  37. package/dist/rollup.d.ts +3 -0
  38. package/dist/rollup.js +10 -0
  39. package/dist/rspack.d.ts +3 -0
  40. package/dist/rspack.js +10 -0
  41. package/dist/vite.d.ts +3 -0
  42. package/dist/vite.js +10 -0
  43. package/dist/webpack.d.ts +3 -0
  44. package/dist/webpack.js +10 -0
  45. package/package.json +24 -3
@@ -5,8 +5,24 @@ export type MorphicModeName = string;
5
5
  * - "text" — rendered as an HTML label; never shown in the workspace
6
6
  * - "code" — template with %N placeholders; rendered as a Blockly block in the workspace or as text in the codespace
7
7
  * - "image" — rendered as an <img> in the toolbox tile; never shown in the workspace
8
+ * - "custom" — drawn on the toolbox tile by the app's renderer (`toolbox.custom`); never shown in the workspace
8
9
  */
9
- export type MorphicElementType = "text" | "code" | "image";
10
+ export type MorphicElementType = "text" | "code" | "image" | "custom";
11
+ /** What a renderer of a `custom` element is drawing. */
12
+ export interface MorphicElementRenderContext {
13
+ /** The block's identifier. */
14
+ block: string;
15
+ /** The element's name. */
16
+ element: string;
17
+ /** The toolbox mode. */
18
+ mode: string;
19
+ }
20
+ /**
21
+ * Draws a `custom` element on a toolbox tile from the block's content for it:
22
+ * a button, a player, anything. A string is shown as text. Called again on
23
+ * every redraw of the toolbox.
24
+ */
25
+ export type MorphicElementRenderer = (content: string, context: MorphicElementRenderContext) => Node | string;
10
26
  /**
11
27
  * Configuration for a value slot's empty-state default. Keyed by slot
12
28
  * `check` in `code[element].empty[check]`, or set per-slot via
@@ -90,6 +106,23 @@ export interface MorphicCodeElementConfig {
90
106
  * leave empty bodies empty.
91
107
  */
92
108
  emptyStatement?: string;
109
+ /**
110
+ * Names written for `%1:type`, keyed by the value's type (its block's
111
+ * `output` check, or the slot's `check` while empty), e.g. Java:
112
+ * `{ "Number": "int", "String": "String", "Boolean": "boolean", "default": "var" }`.
113
+ * `default` is used when no key fits; without it nothing is written.
114
+ */
115
+ types?: Record<string, string>;
116
+ /**
117
+ * The order of this language's operators, by the sign as it is written:
118
+ * `{ "**": { "rank": 80, "direction": "right" }, "//": 60 }`. Text views
119
+ * write brackets only where the order needs them, and `loadText()` reads
120
+ * `2 + 3 * 4` as the language means it. The common signs have an order
121
+ * without this setting (see the docs); an entry here changes one sign,
122
+ * `null` takes a sign's order away, and `false` turns the order off, so
123
+ * every value built from others is written in brackets.
124
+ */
125
+ operators?: false | Record<string, MorphicOperatorOrder | null>;
93
126
  /** Token highlighting for the codespace and preview showing this element. */
94
127
  highlighting?: MorphicHighlightDefinition;
95
128
  /**
@@ -98,6 +131,21 @@ export interface MorphicCodeElementConfig {
98
131
  */
99
132
  values?: MorphicValueFormat;
100
133
  }
134
+ /**
135
+ * Where an operator stands among the others: a rank (the higher, the tighter
136
+ * it binds), or the rank with more about it. `direction` says how the same
137
+ * rank reads without brackets: from the `"left"` (`10 - 4 - 3`, the default),
138
+ * from the `"right"` (`2 ** 3 ** 2`), or not at all (`"none"`: `1 < x < 5`
139
+ * needs brackets). `"any"` is for a sign whose grouping does not matter
140
+ * (`out << a << b`): no brackets are written between two of it, and text
141
+ * reads from the left. `prefix` is the rank of the sign in front of one
142
+ * value (`-x`, `not x`).
143
+ */
144
+ export type MorphicOperatorOrder = number | {
145
+ rank?: number;
146
+ direction?: "left" | "right" | "none" | "any";
147
+ prefix?: number;
148
+ };
101
149
  /**
102
150
  * Printed values in a language's own spelling, e.g. Python:
103
151
  * `{ "true": "True", "false": "False", "null": "None", "list": { "quote": "'" } }`.
@@ -153,17 +201,37 @@ export type MorphicInputAlign = "left" | "centre" | "right";
153
201
  * - `"=="` → value = label = `"=="`
154
202
  * - `["-", "−"]` → value `"-"`, shown as `"−"` on the block
155
203
  * - `{ value, label?, display? }` → explicit object form
204
+ *
205
+ * `label` can also be an image ({@link MorphicDropdownImage}): the block shows
206
+ * the picture, text views still write the value.
156
207
  */
157
208
  export type MorphicDropdownOption = string | [value: string, label: string] | {
158
209
  value: string;
159
- label?: string;
210
+ label?: string | MorphicDropdownImage;
160
211
  display?: Record<string, string>;
161
212
  };
213
+ /**
214
+ * A picture shown as a dropdown choice on the block: an image file (`src`) or
215
+ * a swatch the framework draws in a CSS color (`color`, e.g. `"#e53935"`).
216
+ * `alt` (what screen readers say) defaults to the option's value; the size
217
+ * defaults to 16 × 16.
218
+ */
219
+ export type MorphicDropdownImage = ({
220
+ src: string;
221
+ color?: never;
222
+ } | {
223
+ color: string;
224
+ src?: never;
225
+ }) & {
226
+ alt?: string;
227
+ width?: number;
228
+ height?: number;
229
+ };
162
230
  /**
163
231
  * A field declared directly on a block (rendered at the `%FIELDNAME` token
164
- * position). Lets dropdown / text / number / checkbox fields live in the
165
- * definitions file instead of requiring a behavior's `onViewApplied`. Fields
166
- * outside these four (variables, colour, plugin/custom) still use
232
+ * position). Lets dropdown / reference / text / number / checkbox fields live
233
+ * in the definitions file instead of requiring a behavior's `onViewApplied`.
234
+ * Fields outside these (Blockly's variables, colour, plugin/custom) still use
167
235
  * `onViewApplied` — the `%FIELDNAME` token is skipped when no declaration
168
236
  * exists, leaving the behavior to supply the field.
169
237
  */
@@ -171,6 +239,22 @@ export type MorphicFieldDefinition = {
171
239
  type: "dropdown";
172
240
  options: MorphicDropdownOption[];
173
241
  default?: string;
242
+ }
243
+ /**
244
+ * A dropdown of the names the program declares: `to` names a block and one
245
+ * of its fields (`"class_define.NAME"`, or a list of them), and the field
246
+ * lists what that field holds on every such block. Renaming a declaration
247
+ * renames the references to it. `default` is the name a new block starts
248
+ * with; in a program that declares other names it takes the first of them.
249
+ * `scope` names a block (or several): the field then lists only what is
250
+ * declared inside the nearest such block around it, e.g. the fields of the
251
+ * class a method is in.
252
+ */
253
+ | {
254
+ type: "reference";
255
+ to: string | string[];
256
+ default?: string;
257
+ scope?: string | string[];
174
258
  } | {
175
259
  type: "text";
176
260
  default?: string;
@@ -184,10 +268,33 @@ export type MorphicFieldDefinition = {
184
268
  type: "checkbox";
185
269
  default?: boolean;
186
270
  };
271
+ /** How a repeating slot behaves. */
272
+ export interface MorphicRepeatConfig {
273
+ /**
274
+ * What is written between two entries: one text for every element, or a
275
+ * text per element name (`{ "python": ", ", "concept": " and " }`). `, ` by default.
276
+ */
277
+ separator?: string | Record<string, string>;
278
+ /** How many entries a new block starts with (1 by default). */
279
+ count?: number;
280
+ }
187
281
  export interface MorphicInputSlotDefinition {
188
282
  kind?: MorphicInputKind;
189
283
  name?: string;
190
284
  check?: string | string[];
285
+ /**
286
+ * Take only blocks whose check matches `check`. Without it, a block that has
287
+ * no check fits every slot (Blockly's rule); with it, such a block is
288
+ * refused, here and below the blocks already in the slot. Needs a `check`.
289
+ */
290
+ strict?: boolean;
291
+ /**
292
+ * Let the slot hold any number of values, e.g. the parameters of a function
293
+ * or the items of a list. The template names the slot once (`def f(%1):`);
294
+ * the entries are written one after the other with `separator` between
295
+ * them. Value slots only.
296
+ */
297
+ repeat?: MorphicRepeatConfig;
191
298
  align?: MorphicInputAlign;
192
299
  label?: string;
193
300
  /**
@@ -210,29 +317,59 @@ export type MorphicBlockElements = Record<string, string>;
210
317
  * is part of the preset: `toolbox` is required, and at least one editing
211
318
  * space (`workspace` or `codespace`) must be set; `preview` is optional.
212
319
  */
213
- /**
214
- * A preset's toolbox: either a bare mode name (all code elements render as
215
- * blocks) or an object that additionally overrides, per element, whether a
216
- * `code` element renders as a draggable block or as source text in the tile.
217
- */
218
- export type MorphicPresetToolbox = MorphicModeName | {
219
- mode: MorphicModeName;
220
- render?: Record<string, "block" | "text">;
221
- };
320
+ /** How a mode's code element is drawn on a tile: as a block, or as its text. */
321
+ export type MorphicTileRender = "block-based" | "text-based";
222
322
  export interface MorphicPresetDefinition {
223
323
  name: string;
224
324
  /** Display label (e.g. for buttons). Falls back to `name`. */
225
325
  label?: string;
226
- toolbox: MorphicPresetToolbox;
227
- workspace?: MorphicModeName;
228
- codespace?: MorphicModeName;
229
- preview?: MorphicModeName;
326
+ /** The mode of the toolbox tiles; the mode's `render` says how they are drawn. */
327
+ toolbox: MorphicModeName;
328
+ /**
329
+ * The code element each view shows, by view name, e.g. `{ "blocks": "concept", "code": "python" }`.
330
+ * The views it lists are the ones the preset shows; showing and hiding
331
+ * their containers is the app's job (`onPresetApplied`). Views it leaves
332
+ * out keep what they show.
333
+ */
334
+ views: Record<string, string>;
335
+ /**
336
+ * The block set whose blocks the toolbox shows. Without it, the toolbox
337
+ * shows all of its blocks.
338
+ */
339
+ blockSet?: string;
340
+ /**
341
+ * Whether the toolbox groups its tiles under the categories. Default `true`;
342
+ * `false` shows them as one list, e.g. for a level with few blocks. Blocks
343
+ * keep their category colors either way.
344
+ */
345
+ categories?: boolean;
346
+ }
347
+ /**
348
+ * Blocks for the toolbox, by name in `blockSets`: a list of block
349
+ * identifiers, or another set with blocks added at the end and removed.
350
+ */
351
+ export type MorphicBlockSet = string[] | {
352
+ extends: string;
353
+ add?: string[];
354
+ remove?: string[];
355
+ };
356
+ /** Kinds of view: blocks, or the program as text. */
357
+ export type MorphicViewKind = "workspace" | "codespace";
358
+ /** A view in the definitions' `views`, by name. A bare kind is the short form. */
359
+ export interface MorphicViewDeclaration {
360
+ kind: MorphicViewKind;
361
+ /**
362
+ * Whether the program can be changed in the view. Default `true`. A
363
+ * codespace that cannot be edited shows the program as text, read only.
364
+ */
365
+ editable?: boolean;
230
366
  /**
231
- * Modes of views added with `addView()`, by view name. Applying the preset
232
- * switches each listed view that exists; the app shows or hides views, as
233
- * for the built-in ones.
367
+ * Right to left (`true`) or left to right (`false`). Left out, a workspace
368
+ * follows `blockly.rtl` and a codespace the page around it.
234
369
  */
235
- views?: Record<string, MorphicModeName>;
370
+ rtl?: boolean;
371
+ /** How blocks are deleted in a codespace. */
372
+ delete?: MorphicCodespaceDeleteOptions;
236
373
  }
237
374
  /**
238
375
  * Declares which elements are visible for a given mode.
@@ -244,6 +381,11 @@ export interface MorphicModeDefinition {
244
381
  name: string;
245
382
  /** Element names that are visible in this mode (e.g. ["icon", "text"]). */
246
383
  elements: string[];
384
+ /**
385
+ * How the mode's code elements are drawn on toolbox tiles, by element name:
386
+ * `"block-based"` (the default) or `"text-based"`, e.g. `{ "python": "text-based" }`.
387
+ */
388
+ render?: Record<string, MorphicTileRender>;
247
389
  }
248
390
  export interface MorphicBlockDefinition {
249
391
  identifier: string;
@@ -278,6 +420,13 @@ export interface MorphicBlockDefinition {
278
420
  shape?: MorphicBlockShape;
279
421
  previousStatement?: MorphicConnectionSpec;
280
422
  nextStatement?: MorphicConnectionSpec;
423
+ /**
424
+ * Let the block go only where the place above it (a slot, or the block it
425
+ * hangs below) has a check matching its own `previousStatement` or `output`
426
+ * check. Without it, a block with a check still fits every place that has
427
+ * none, so a class field could be dropped into ordinary code. Needs a check.
428
+ */
429
+ strict?: boolean;
281
430
  inputsInline?: boolean;
282
431
  tooltip?: string;
283
432
  helpUrl?: string;
@@ -344,8 +493,17 @@ export interface MorphicBlocksFormat {
344
493
  elementTypes?: Record<string, MorphicElementTypeEntry>;
345
494
  /** Explicit mode definitions — which elements are visible per mode. */
346
495
  modes?: MorphicModeDefinition[];
496
+ /**
497
+ * The views, by name: `{ "blocks": "workspace", "read": { "kind": "codespace", "editable": false } }`.
498
+ * `mount()` sets up each one it gets a container for. The first workspace
499
+ * with a container holds the program. Default: a `workspace` and a
500
+ * `codespace` view.
501
+ */
502
+ views?: Record<string, MorphicViewKind | MorphicViewDeclaration>;
347
503
  /** Named per-view mode configurations. Used as the mount default for `presets`. */
348
504
  presets?: MorphicPresetDefinition[];
505
+ /** Named lists of blocks for the toolbox, used by presets and `show({ blockSet })`. */
506
+ blockSets?: Record<string, MorphicBlockSet>;
349
507
  /**
350
508
  * How each code element's language is written, keyed by element name:
351
509
  * string quotes, empty slot defaults and highlighting.
@@ -377,14 +535,18 @@ export interface MorphicRunResult {
377
535
  export interface MorphicBehaviorContext {
378
536
  Blockly: typeof Blockly;
379
537
  workspace: Blockly.WorkspaceSvg;
380
- mode: MorphicModeName;
538
+ /** The code element the block is drawn from. */
539
+ element: string;
540
+ /** The toolbox mode, for blocks drawn in the toolbox. */
541
+ mode?: MorphicModeName;
381
542
  context: MorphicRenderContext;
382
543
  definition: MorphicBlockDefinition;
383
544
  }
384
545
  export interface MorphicBehaviorProxy {
385
546
  blockId: string;
386
547
  blockType: string;
387
- mode: MorphicModeName;
548
+ /** The code element the block is drawn from in its view. */
549
+ element: string;
388
550
  context: MorphicRenderContext;
389
551
  inputs: Record<string, string>;
390
552
  /**
@@ -399,6 +561,12 @@ export interface MorphicBehaviorProxy {
399
561
  * Use these where the value is emitted as a string (e.g. a text block).
400
562
  */
401
563
  quoted: Record<string, string>;
564
+ /**
565
+ * The generated code of each filled entry of a repeating slot, keyed by the
566
+ * slot's name: `proxy.lists.PARAMS` is `["a", "b"]`. `proxy.inputs.PARAMS`
567
+ * holds the same entries joined with `, `.
568
+ */
569
+ lists: Record<string, string[]>;
402
570
  }
403
571
  export type MorphicCodeBehavior = (proxy: MorphicBehaviorProxy) => string;
404
572
  export interface MorphicBlockBehavior {
@@ -419,7 +587,54 @@ export interface MorphicToolboxCategory {
419
587
  blocks?: string[];
420
588
  }
421
589
  export type MorphicToolboxLayout = "flyout" | "category";
422
- export interface MorphicToolboxConfig {
590
+ /** How the HTML toolbox shows categories and scrolls; part of both toolbox option sets. */
591
+ export interface MorphicToolboxDisplayOptions {
592
+ /**
593
+ * `"stacked"` (default): every category under its heading. `"tabs"`: a list
594
+ * of the category names and the tiles of the chosen one, the first at the
595
+ * start; `collapsed` does not apply. The container gets
596
+ * `morphic-toolbox-stacked` or `morphic-toolbox-tabs`; where the list sits
597
+ * is the app's CSS. Without categories, tiles are a flat list either way.
598
+ */
599
+ layout?: "stacked" | "tabs";
600
+ /** A heading with the category's name above its tiles. Default `true`. */
601
+ categoryHeadings?: boolean;
602
+ /**
603
+ * A click on a heading collapses or expands its category. Default `true`.
604
+ * Without headings, categories stay open.
605
+ */
606
+ collapsible?: boolean;
607
+ /**
608
+ * Categories collapsed at the start: `true` for all, or their names.
609
+ * Default: all open. The toolbox keeps the state across redraws.
610
+ */
611
+ collapsed?: boolean | string[];
612
+ /** Called when a category is collapsed or expanded, e.g. to remember it. */
613
+ onCategoryToggle?: (name: string, collapsed: boolean) => void;
614
+ /**
615
+ * Show the container's scrollbar. Default `false`: hidden, and the edges
616
+ * fade where more tiles wait. Either way the container gets the classes
617
+ * `morphic-toolbox`, `morphic-toolbox-more-above` and
618
+ * `morphic-toolbox-more-below` (while tiles are hidden there) for the
619
+ * app's own styles; the defaults have no weight (`:where()`), so any app
620
+ * rule wins. Works when the container is the element that scrolls.
621
+ */
622
+ scrollbar?: boolean;
623
+ /**
624
+ * How each `custom` element is drawn on the tiles, by element name.
625
+ * Pressing a button, link or input inside one does not drag the tile, and
626
+ * Enter or Space there does not add the block.
627
+ */
628
+ custom?: Record<string, MorphicElementRenderer>;
629
+ /**
630
+ * The element whose text names each tile for screen readers (its
631
+ * `aria-label`), e.g. a `spoken` text element no mode lists; a function
632
+ * gets the toolbox mode and returns the element's name. A block without
633
+ * that element is named by what its tile shows, as without this option.
634
+ */
635
+ label?: string | ((mode: string) => string);
636
+ }
637
+ export interface MorphicToolboxConfig extends MorphicToolboxDisplayOptions {
423
638
  kind?: "flyoutToolbox" | "categoryToolbox";
424
639
  /** Show only these blocks, in the HTML toolbox and in Blockly's own. */
425
640
  blocks?: string[];
@@ -440,7 +655,7 @@ export interface MorphicToolboxConfig {
440
655
  */
441
656
  export type MorphicToolboxModeLabel = boolean | string | ((mode: string) => string);
442
657
  /** Options for the custom HTML toolbox canvas (mountToolbox). */
443
- export interface MorphicToolboxCanvasOptions {
658
+ export interface MorphicToolboxCanvasOptions extends MorphicToolboxDisplayOptions {
444
659
  /** Header of the toolbox; see {@link MorphicToolboxModeLabel}. Defaults to "Mode: <name>". */
445
660
  modeLabel?: MorphicToolboxModeLabel;
446
661
  /**
@@ -479,35 +694,34 @@ export interface MorphicJavaScriptConfig {
479
694
  }
480
695
  export interface MorphicMountConfig {
481
696
  /**
482
- * Container for the Blockly workspace. Optional — may be omitted when only a
483
- * codespace is used. At least one of `workspaceContainer` or `codespaceContainer`
484
- * must be provided. When omitted, Blockly runs headless (offscreen) so the block
485
- * model stays available.
697
+ * The views' containers, by view name. A view without one is not set up,
698
+ * so the same definitions serve a full page and a small embed. At least one
699
+ * view needs a container. Without a workspace, Blockly runs headless
700
+ * (offscreen) so the block model stays available.
486
701
  */
702
+ containers?: Record<string, HTMLElement>;
703
+ /** Short for `containers.workspace`. */
487
704
  workspaceContainer?: HTMLElement;
488
- /**
489
- * Container for the primary text editor (codespace). Optional, but required
490
- * when a preset shows the codespace. `mount()` sets the codespace up in it.
491
- */
705
+ /** Short for `containers.codespace`. */
492
706
  codespaceContainer?: HTMLElement;
493
707
  /**
494
708
  * Container for the custom HTML toolbox. `mount()` sets the toolbox up in it,
495
709
  * which implies `canvasToolbox`.
496
710
  */
497
711
  toolboxContainer?: HTMLElement;
498
- /** Container for the read-only preview. `mount()` sets the preview up in it. */
499
- previewContainer?: HTMLElement;
500
712
  /**
501
713
  * Container for the generated JavaScript view, a developer tool. `mount()`
502
714
  * sets it up hidden; `showCodeEditor()` reveals it.
503
715
  */
504
716
  codeEditorContainer?: HTMLElement;
505
- /** Toolbar containers per pane, set up once the text editors are ready. */
506
- toolbarContainers?: Partial<Record<MorphicToolbarPane, HTMLElement>>;
507
- /** Theme for the codespace and code editor set up by `mount()`. */
717
+ /** Toolbar containers, by view name, set up once the views are ready. */
718
+ toolbars?: Record<string, HTMLElement>;
719
+ /** Theme for the text views and the code editor. */
508
720
  editorTheme?: MorphicCodeEditorTheme;
509
- /** Theme for the preview set up by `mount()`. Defaults to `editorTheme`. */
510
- previewTheme?: MorphicCodeEditorTheme;
721
+ /** Themes of single text views, by view name, in place of `editorTheme`. */
722
+ themes?: Record<string, MorphicCodeEditorTheme>;
723
+ /** The views, in place of the definitions' `views`. */
724
+ views?: Record<string, MorphicViewKind | MorphicViewDeclaration>;
511
725
  /**
512
726
  * Link block and code selection across views. On by default when `mount()`
513
727
  * sets up more than one view; `false` turns it off, an object configures it.
@@ -524,8 +738,8 @@ export interface MorphicMountConfig {
524
738
  preset?: string;
525
739
  /**
526
740
  * Called after a preset is applied — at mount and on every `applyPreset` —
527
- * so the host can lay out pane visibility (a view is shown iff its key is
528
- * present on the preset).
741
+ * so the host can lay out pane visibility (the preset shows the views its
742
+ * `views` lists).
529
743
  */
530
744
  onPresetApplied?: (preset: MorphicPresetDefinition) => void;
531
745
  /**
@@ -534,6 +748,13 @@ export interface MorphicMountConfig {
534
748
  * them in the app's own development mode.
535
749
  */
536
750
  onWarning?: (message: string) => void;
751
+ /**
752
+ * Where an image the definitions name is loaded from: a template's `<img>`,
753
+ * an image element's path, a dropdown picture. Return its address, or
754
+ * `undefined` to keep the path (next to the page), e.g. to show images an
755
+ * app keeps in memory: `assetUrl: (path) => blobUrls[path]`.
756
+ */
757
+ assetUrl?: (path: string) => string | undefined;
537
758
  toolbox?: MorphicToolboxConfig;
538
759
  toolboxLayout?: MorphicToolboxLayout;
539
760
  /**
@@ -564,6 +785,7 @@ export interface MorphicMountConfig {
564
785
  /**
565
786
  * Options passed through to Blockly's `inject` (zoom, grid, trashcan, and so
566
787
  * on). `media` defaults to `"blockly-media/"` next to the page, filled by
788
+ * the bundler plugin (`morphic-blocks/vite` and others) or
567
789
  * `npx morphic-blocks copy-media`, so Blockly never loads from another server
568
790
  * unless the host sets it.
569
791
  */
@@ -648,6 +870,8 @@ export interface MorphicPlaceholderRange {
648
870
  /** Exclusive 0-based character offset in `code`. */
649
871
  end: number;
650
872
  kind: "default" | "set";
873
+ /** Set when the range is a reference whose name nothing declares; text views mark it. */
874
+ missing?: boolean;
651
875
  /** Editing target when the slot contains an atomic single-field block. */
652
876
  edit?: MorphicPlaceholderEditTarget;
653
877
  /**
@@ -661,12 +885,41 @@ export interface MorphicPlaceholderRange {
661
885
  inputName: string;
662
886
  };
663
887
  }
888
+ /** What `loadText()` returns: how many blocks the text became, or the line that fits no block. */
889
+ export type MorphicTextLoadResult = {
890
+ ok: true;
891
+ blocks: number;
892
+ } | {
893
+ ok: false;
894
+ line: number;
895
+ message: string;
896
+ };
897
+ /** Where the entries of a repeating slot end in generated text; text views put the slot's buttons there. */
898
+ export interface MorphicRepeatRange {
899
+ blockId: string;
900
+ /** The slot's name (the entries' inputs are `<name>_0`, `<name>_1`, …). */
901
+ slotName: string;
902
+ /** How many entries the slot has. */
903
+ count: number;
904
+ /** Character offset right after the last entry (or where the first would be). */
905
+ at: number;
906
+ }
664
907
  /** Result of `generateJavaScriptWithMetadata()`. */
665
908
  export interface MorphicCodeGenerationResult {
666
909
  code: string;
667
910
  metadata: MorphicCodeMetadata;
668
911
  /** Value-slot ranges with default/set classification. Empty if no value slots emitted. */
669
912
  placeholders: MorphicPlaceholderRange[];
913
+ /** Where each repeating slot's entries end in the text. Empty if the text has none. */
914
+ repeats?: MorphicRepeatRange[];
915
+ /**
916
+ * Line range (1-based, inclusive) of each group of connected blocks, a stack
917
+ * in the workspace, in text order. Text views mark these lines with classes.
918
+ */
919
+ groups?: {
920
+ startLine: number;
921
+ endLine: number;
922
+ }[];
670
923
  }
671
924
  /** Theme configuration for the code editor. */
672
925
  export interface MorphicCodeEditorTheme {
@@ -686,25 +939,44 @@ export interface MorphicCodeEditorTheme {
686
939
  gutterForeground?: string;
687
940
  /** Text selection highlight colour. */
688
941
  selectionBackground?: string;
942
+ /**
943
+ * A CodeMirror theme (an extension, e.g. `oneDark` from
944
+ * `@codemirror/theme-one-dark`) for the editor's colours. The colours above
945
+ * then default to the theme's; any set here still win. Token colours stay
946
+ * with `highlighting`.
947
+ */
948
+ codemirror?: unknown;
949
+ }
950
+ /** Ways to delete blocks in a codespace, each on unless set to `false`. */
951
+ export interface MorphicCodespaceDeleteOptions {
952
+ /** The ✕ beside each line where a block starts. */
953
+ lineButtons?: boolean;
954
+ /** Delete and Backspace: the block of the cursor's line, or of every selected line. */
955
+ keys?: boolean;
956
+ /** The toolbar button that deletes the blocks of the selected lines. */
957
+ toolbarButton?: boolean;
689
958
  }
690
- /** Kinds of view `addView()` adds beside the ones `mount()` sets up. */
691
- export type MorphicViewKind = "preview" | "codespace" | "workspace";
692
959
  /** A view added with `engine.addView()`. */
693
960
  export interface MorphicViewOptions {
694
961
  kind: MorphicViewKind;
695
962
  /** Element the view renders into. */
696
963
  container: HTMLElement;
697
- /** Mode the view shows; a preview renders its source element. */
698
- mode: MorphicModeName;
699
- /** Unique among the engine's added views. */
964
+ /** The code element the view shows, e.g. `"python"`. */
965
+ element: string;
966
+ /** Unique among the engine's views. */
700
967
  name?: string;
701
968
  /**
702
- * An added workspace shows the program as blocks in its own mode and follows
703
- * every change of the main workspace. It is read only: `false` is the only
704
- * value for now, and the default.
969
+ * Whether the program can be changed in the view. Default `true`. An added
970
+ * workspace follows every change of the one that holds the program and
971
+ * sends its own edits there; a codespace that cannot be edited shows the
972
+ * program as text, read only.
705
973
  */
706
- editable?: false;
707
- /** Colours of a text view, like `previewTheme`. */
974
+ editable?: boolean;
975
+ /** Right to left or left to right; left out, the view follows the program's workspace or the page. */
976
+ rtl?: boolean;
977
+ /** How blocks are deleted in a codespace. */
978
+ delete?: MorphicCodespaceDeleteOptions;
979
+ /** Colours of a text view, like `editorTheme`. */
708
980
  theme?: MorphicCodeEditorTheme;
709
981
  /** A toolbar for this view, removed with it. Items default to its kind's. */
710
982
  toolbar?: {
@@ -720,8 +992,10 @@ export interface MorphicViewHandle {
720
992
  readonly name: string;
721
993
  /** Settles once the view is ready; text views load their editor in the background. */
722
994
  readonly ready: Promise<void>;
723
- getMode(): MorphicModeName;
724
- setMode(mode: MorphicModeName): void;
995
+ /** The code element the view shows. */
996
+ getElement(): string;
997
+ /** Show another code element. */
998
+ show(element: string): void;
725
999
  /** Colours of a text view, e.g. when the app switches between light and dark. No effect on a workspace. */
726
1000
  setTheme(theme: MorphicCodeEditorTheme): void;
727
1001
  /** Removes the view. `mount()` again removes every added view too. */
@@ -735,10 +1009,17 @@ export interface MorphicCodeEditorOptions {
735
1009
  extensions?: unknown[];
736
1010
  /**
737
1011
  * Called when the user requests to delete the block at a given 1-based line
738
- * (via Delete/Backspace on an empty selection, or a click on the gutter ✕).
1012
+ * (via Delete/Backspace, or a click on the gutter ✕), or the blocks of the
1013
+ * lines `line` to `lastLine` when several lines are selected.
739
1014
  * When set, the editor installs a keymap and a delete gutter.
740
1015
  */
741
- onDelete?: (line: number) => void;
1016
+ onDelete?: (line: number, lastLine?: number) => void;
1017
+ /** Called when the selected lines change (see `getSelectedLines()`). */
1018
+ onLineSelection?: () => void;
1019
+ /** With `onDelete`: the ✕ beside each line where a block starts. Default `true`. */
1020
+ deleteButtons?: boolean;
1021
+ /** With `onDelete`: Delete and Backspace delete. Default `true`. */
1022
+ deleteKeys?: boolean;
742
1023
  /**
743
1024
  * If provided, a "grip" gutter (⋮⋮) appears on the start line of every block
744
1025
  * id for which this returns `true`. Mousedown on the grip starts an HTML5
@@ -759,6 +1040,13 @@ export interface MorphicCodeEditorOptions {
759
1040
  * `edit.blockId`. Re-codegen + re-render then refresh the codespace.
760
1041
  */
761
1042
  onPlaceholderApply?: (edit: MorphicPlaceholderEditTarget, newValue: string) => void;
1043
+ /**
1044
+ * Called when a plus or minus button of a repeating slot is pressed, with
1045
+ * the block, the slot's name and +1 or -1. When set, the editor shows the
1046
+ * buttons after each repeating slot's last entry, on the line the pointer
1047
+ * is over and on selected lines.
1048
+ */
1049
+ onRepeat?: (blockId: string, slotName: string, delta: number) => void;
762
1050
  /**
763
1051
  * Whether value-slot underline markers are shown in this editor. The codespace
764
1052
  * uses them as a form-field affordance for inline editing; read-only display
@@ -768,16 +1056,11 @@ export interface MorphicCodeEditorOptions {
768
1056
  }
769
1057
  /** Options for `enableSelectionSync()`. */
770
1058
  export interface MorphicSelectionSyncOptions {
771
- /** CSS background colour for highlighted code lines. Defaults to each editor's theme `selectionBackground`, a little lighter. */
772
- highlightColor?: string;
773
1059
  /** Enable block → code direction. Defaults to true. */
774
1060
  blockToCode?: boolean;
775
1061
  /** Enable code → block direction. Defaults to true. */
776
1062
  codeToBlock?: boolean;
777
1063
  }
778
- /** Surface a toolbar binds to. Drives stateful items (undo enabled-state,
779
- * language label source, what "clear" affects). */
780
- export type MorphicToolbarPane = "workspace" | "codespace" | "preview";
781
1064
  /** How items render their built-in icon + label pair. */
782
1065
  export type MorphicToolbarDisplay = "icon" | "label" | "both";
783
1066
  /**
@@ -787,7 +1070,9 @@ export type MorphicToolbarDisplay = "icon" | "label" | "both";
787
1070
  export interface MorphicToolbarCtx {
788
1071
  engine: import("./MorphicBlocks").MorphicBlocks;
789
1072
  /** The kind of view the toolbar acts on. */
790
- pane: MorphicToolbarPane;
1073
+ kind: MorphicViewKind;
1074
+ /** Whether the view shows the program without changing it. */
1075
+ readOnly: boolean;
791
1076
  /** The name of the view the toolbar acts on; pass it to engine methods such as `zoomPane`. */
792
1077
  view: string;
793
1078
  /** Current rendered text for the bound pane. Empty for workspace pane unless
@@ -824,10 +1109,8 @@ export interface MorphicToolbarItem {
824
1109
  }
825
1110
  /** Configuration for `engine.mountToolbar(container, config)`. */
826
1111
  export interface MorphicToolbarConfig {
827
- /** The view this toolbar acts on, by name: `workspace`, `codespace`, `preview`, or an added view's. */
828
- view?: string;
829
- /** Same as `view` for the views `mount()` sets up. One of `view` and `pane` is required. */
830
- pane?: MorphicToolbarPane;
1112
+ /** The view this toolbar acts on, by name. */
1113
+ view: string;
831
1114
  /** Items to render. When omitted, the framework uses
832
1115
  * `toolbarItems.defaultsFor(<kind of view>)`. To render no items, pass `[]`. */
833
1116
  items?: MorphicToolbarItem[];