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
@@ -1,8 +1,10 @@
1
1
  import * as Blockly from "blockly";
2
2
  import { type MorphicToolbarHandle } from "./toolbar";
3
- import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions, MorphicViewHandle, MorphicViewOptions } from "./types";
3
+ import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodespaceDeleteOptions, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicTextLoadResult, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions, MorphicViewHandle, MorphicViewOptions } from "./types";
4
4
  export declare class MorphicBlocks extends EventTarget {
5
5
  private readonly definitions;
6
+ /** The blocks as loaded; each mount resolves their images (`assetUrl`) from these. */
7
+ private readonly loadedDefinitions;
6
8
  private readonly behaviors;
7
9
  /** Element types with the mounted `code` settings folded in (see withCodeSettings). */
8
10
  private elementTypes;
@@ -21,18 +23,15 @@ export declare class MorphicBlocks extends EventTarget {
21
23
  /** Codespaces a tile dragged by touch or pen can be dropped on. */
22
24
  private readonly tileDropTargets;
23
25
  private codeEditor?;
24
- private codespace?;
25
- private previewEditor?;
26
26
  /** Views added with `addView()`, beside the ones `mount()` sets up. */
27
27
  private readonly views;
28
28
  /** The mode of each added workspace, read when its blocks are built. */
29
- private readonly viewWorkspaceModes;
29
+ private readonly viewWorkspaceElements;
30
30
  /** Numbers added views that are not given a name. */
31
31
  private viewCount;
32
32
  private selectionSync?;
33
33
  private toolboxDefinition?;
34
34
  /** Container the preview editor was mounted into. */
35
- private previewHost?;
36
35
  private blockCategoryIndex;
37
36
  private appliedWorkspaceClasses;
38
37
  private appliedToolboxFlyoutClasses;
@@ -40,7 +39,6 @@ export declare class MorphicBlocks extends EventTarget {
40
39
  /** Offscreen div created when no user-supplied workspaceContainer is present. */
41
40
  private headlessWorkspaceHost?;
42
41
  /** Teardown fn for codespace drag/drop listeners; set when codespace is mounted. */
43
- private codespaceDropTeardown?;
44
42
  /** Redraws the workspace whenever its container changes size. */
45
43
  private workspaceResizeObserver?;
46
44
  /** Preset most recently applied, at mount or via `applyPreset`. */
@@ -57,10 +55,18 @@ export declare class MorphicBlocks extends EventTarget {
57
55
  * runtime `mount()` call only carries DOM / runtime concerns. */
58
56
  private readonly formatModes?;
59
57
  private readonly formatPresets?;
58
+ private readonly formatViews?;
60
59
  private readonly formatCode?;
61
60
  /** The definitions still use the old top level `highlighting` map. */
62
61
  private readonly formatHasHighlighting;
63
62
  private readonly formatCategories?;
63
+ private readonly formatBlockSets?;
64
+ /** Every block set's blocks, resolved at mount. */
65
+ private blockSetLists;
66
+ /** The block set the toolbox shows; `undefined` shows the toolbox's own blocks. */
67
+ private activeBlockSet?;
68
+ /** Whether the toolbox groups its tiles under the categories; presets and show() switch it. */
69
+ private categoriesShown;
64
70
  /**
65
71
  * Build an engine from a whole definitions file plus its behaviors — the two
66
72
  * artifacts a developer authors. `blocks` and `elementTypes` configure the
@@ -81,6 +87,12 @@ export declare class MorphicBlocks extends EventTarget {
81
87
  * when the engine was disposed or mounted again meanwhile.
82
88
  */
83
89
  private mountViews;
90
+ /** Mark every view's container with the preset applied last. */
91
+ private applyPresetClasses;
92
+ /** Whether any text editor is set up, which selection sync needs. */
93
+ private hasTextEditor;
94
+ /** The first codespace that edits the program, if any. */
95
+ private get codespace();
84
96
  /** Presets declared at mount (empty when none were provided). */
85
97
  getPresets(): MorphicPresetDefinition[];
86
98
  /**
@@ -125,15 +137,10 @@ export declare class MorphicBlocks extends EventTarget {
125
137
  * `icon` or `python` is not a mode unless a mode is named after it.
126
138
  */
127
139
  getAvailableModes(): MorphicModeName[];
128
- /** Current workspace mode name, or `undefined` if not mounted. */
129
- getWorkspaceMode(): MorphicModeName | undefined;
130
- /**
131
- * Effective codespace mode name: the independent `codespaceMode` when set,
132
- * else the workspace mode. `undefined` if not mounted.
133
- */
134
- getCodespaceMode(): MorphicModeName | undefined;
135
- /** Preview mode name, or `undefined` when no preview mode is set. */
136
- getPreviewMode(): MorphicModeName | undefined;
140
+ /** The toolbox tiles' mode, or `undefined` if not mounted. */
141
+ getToolboxMode(): MorphicModeName | undefined;
142
+ /** The names of the views that are set up: the declared ones with a container, and added ones. */
143
+ getViewNames(): string[];
137
144
  /**
138
145
  * Change which elements a mode shows while the app runs, e.g. to let the
139
146
  * user pick what a toolbox tile shows. Every view using the mode is redrawn;
@@ -142,10 +149,11 @@ export declare class MorphicBlocks extends EventTarget {
142
149
  setModeElements(mode: MorphicModeName, elements: string[]): void;
143
150
  /** Mode definition by name, or `undefined`. */
144
151
  private modeDef;
145
- /** Source element rendered by the codespace (label + highlighting source). */
146
- getActivePrimarySourceElement(): string | undefined;
147
- /** Source element rendered by the preview (label + highlighting source). */
148
- getActivePreviewElement(): string | undefined;
152
+ /**
153
+ * The element the first codespace writes, else the one the program's
154
+ * workspace shows, e.g. for printed values in its language.
155
+ */
156
+ private codespaceElement;
149
157
  /**
150
158
  * Mount a toolbar into the developer-provided container, bound to one of the
151
159
  * three editor surfaces. When `items` is omitted, the default set for the
@@ -179,16 +187,36 @@ export declare class MorphicBlocks extends EventTarget {
179
187
  * since lines always go to a console the host passes in.
180
188
  */
181
189
  logToConsole?: boolean;
190
+ /**
191
+ * Values the program can use by name, e.g. `{ turtle, canvas }` for the
192
+ * host's own functions. Each name must be a JavaScript identifier;
193
+ * `console` has its own option.
194
+ */
195
+ globals?: Record<string, unknown>;
182
196
  }): MorphicRunResult;
183
197
  /** Whether the framework's internal clipboard has a copyable to paste. */
184
198
  hasClipboardContents(): boolean;
199
+ /** The ways to delete blocks in the codespace named `view`, each `true` unless the app turned it off. */
200
+ getDeleteOptions(view: string): Required<MorphicCodespaceDeleteOptions>;
201
+ /** Whether lines are selected in the codespace named `view`, so `deleteSelectedLines()` has something to delete. */
202
+ hasSelectedLines(view: string): boolean;
203
+ /**
204
+ * Delete the blocks of the lines selected in the codespace named `view`, as
205
+ * Delete or Backspace does there. Returns true when lines were selected.
206
+ */
207
+ deleteSelectedLines(view: string): boolean;
185
208
  /**
186
209
  * Copy the currently active block to the framework's internal clipboard and
187
- * mirror its generated code text to the system clipboard. Resolution order:
210
+ * its own text, as the view writes it, to the system clipboard. Resolution order:
188
211
  * (1) Blockly's selected block, (2) for codespace/preview, the deepest block
189
212
  * enclosing the active line. Returns true when something was copied.
190
213
  */
191
214
  copyActiveBlock(view: string): boolean;
215
+ /**
216
+ * A block's own text in a view: its part of the text the view shows (a
217
+ * workspace, the text its mode writes), without the indent it has there.
218
+ */
219
+ private blockTextIn;
192
220
  /**
193
221
  * Paste the last copied block at the active pane's natural location.
194
222
  * Workspace: Blockly's default (workspace centre, slightly offset).
@@ -209,34 +237,45 @@ export declare class MorphicBlocks extends EventTarget {
209
237
  * `zoomToFit()`. For a text view it scales the editor's font-size.
210
238
  */
211
239
  zoomPane(view: string, direction: "in" | "out" | "fit"): void;
240
+ /** Redraw the toolbars of the view named `view`, e.g. when its selected lines change. */
241
+ private refreshToolbars;
212
242
  private ensureToolbarBlocklyListener;
213
243
  private teardownToolbarBlocklyListener;
214
- setModes(modes: {
215
- workspaceMode?: MorphicModeName;
216
- toolboxMode?: MorphicModeName;
217
- /** Per-element block/text override for toolbox tiles. `null` clears it (all code elements render as blocks). */
218
- toolboxRender?: Record<string, "block" | "text"> | null;
219
- /** Independent codespace mode. `null` clears the override (falls back to workspaceMode). */
220
- codespaceMode?: MorphicModeName | null;
221
- /** Preview mode. `null` clears it (preview renders nothing unless the workspace mode declares a legacy `preview`). */
222
- previewMode?: MorphicModeName | null;
244
+ /**
245
+ * Switch what the toolbox and the views show, in the shape of a preset:
246
+ * `show({ toolbox: "pseudo", blockSet: "level-2-blocks", categories: false, views: { blocks: "pseudo", code: "python" } })`.
247
+ * Everything left out stays as it is; showing and hiding the views'
248
+ * containers stays the app's job.
249
+ */
250
+ show(shown: {
251
+ toolbox?: MorphicModeName;
252
+ views?: Record<string, string>;
253
+ /** The block set the toolbox shows; `null` shows all of the toolbox's blocks again. */
254
+ blockSet?: string | null;
255
+ /** Whether the toolbox groups its tiles under the categories; `false` shows one list. */
256
+ categories?: boolean;
223
257
  }): void;
258
+ /** Moved in 0.4.0: `show()` takes its place. */
259
+ setModes(_modes: unknown): never;
260
+ generateJavaScript(): string;
224
261
  /**
225
- * Resolve the highlight rules for the codespace or preview editor by
226
- * looking up that pane's source element name in the
227
- * `mountConfig.highlighting` registry. Returns `undefined` when no
228
- * matching entry is configured.
262
+ * The program as a code element writes it, the text a codespace showing
263
+ * that element has: `engine.generateText("python")`. Works without a view
264
+ * of that element.
229
265
  */
230
- private resolveHighlightRules;
231
- generateJavaScript(): string;
232
- generateJavaScriptWithMetadata(): MorphicCodeGenerationResult;
233
- mountCodeEditor(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
266
+ generateText(element: string): string;
234
267
  /**
235
- * Mounts the primary text editor (codespace) into the `codespaceContainer`
236
- * declared at `mount()`. Renders the workspace as text using the codespace
237
- * mode's source element. Read-only for now; Task 4 adds drops.
268
+ * Replace the program with the blocks that write `text` in a code element:
269
+ * `engine.loadText("print(1)", { element: "python" })`. The text must be
270
+ * valid and written with the blocks' templates; when a line fits no block,
271
+ * nothing changes and the result names the line. One undo brings the old
272
+ * program back.
238
273
  */
239
- mountCodespace(options?: MorphicCodeEditorOptions): Promise<void>;
274
+ loadText(text: string, options: {
275
+ element: string;
276
+ }): MorphicTextLoadResult;
277
+ generateJavaScriptWithMetadata(): MorphicCodeGenerationResult;
278
+ mountCodeEditor(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
240
279
  private attachCodespaceDropTarget;
241
280
  /** Innermost (smallest-range) block in metadata whose char range contains `charOffset`. */
242
281
  private findInnermostBlockAtChar;
@@ -325,11 +364,13 @@ export declare class MorphicBlocks extends EventTarget {
325
364
  private isDescendantOf;
326
365
  /**
327
366
  * Connect `source`'s output into the value input named `inputName` on
328
- * `parent`. A real (non-shadow) child currently in the slot is `unplug`'d
329
- * to top level; a shadow disconnects implicitly when the new block
330
- * connects. The Blockly connection-check (output-type vs slot `check`)
331
- * runs inside `connect`; an incompatible drop throws and is rolled back
332
- * by restoring the prior real child when there was one.
367
+ * `parent`. A real (non-shadow) child currently in the slot goes into the
368
+ * new block's only value input that takes it, as Blockly does on a
369
+ * workspace drop, or else to its own line below the others; a shadow
370
+ * disconnects implicitly when the new block connects. The Blockly
371
+ * connection-check (output-type vs slot `check`) runs inside `connect`; an
372
+ * incompatible drop throws and is rolled back by restoring the prior real
373
+ * child when there was one.
333
374
  */
334
375
  private connectIntoValueSlot;
335
376
  private computeBlockDepth;
@@ -340,17 +381,15 @@ export declare class MorphicBlocks extends EventTarget {
340
381
  private applyTopBlockOrder;
341
382
  /** Write every text view again, added views included. */
342
383
  private refreshTextViews;
384
+ /** The program as `element` writes it, as a codespace shows it. */
385
+ private generateElementText;
386
+ /** Highlight rules of `element`. */
387
+ private highlightRulesFor;
343
388
  /**
344
- * Mounts a read-only preview editor that renders the current mode's
345
- * `preview` element as text. Re-renders on workspace and mode changes.
346
- * If the active mode has no `preview` declared, the editor stays empty.
389
+ * Where nothing says what a view shows: the first code element of the first
390
+ * mode, else the first code element at all.
347
391
  */
348
- mountPreview(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
349
- private generatePreviewText;
350
- /** The program as `mode`'s source element writes it, as a preview shows it. */
351
- private generateModeText;
352
- /** Highlight rules of `mode`'s source element. */
353
- private highlightRulesFor;
392
+ private defaultElement;
354
393
  /**
355
394
  * Add a view beside the ones `mount()` sets up, e.g. a second preview in
356
395
  * another mode. Returns a handle to change its mode or remove it; a new
@@ -364,10 +403,19 @@ export declare class MorphicBlocks extends EventTarget {
364
403
  * workspace, so the text views follow a click in the mirror too.
365
404
  */
366
405
  private addWorkspaceView;
367
- /** The mode's classes and block font on an added workspace. */
406
+ /** The element's classes and block font on an added workspace. */
368
407
  private styleWorkspaceView;
369
- /** Draw every block of an added workspace in the view's mode. */
408
+ /**
409
+ * Draw every block of an added workspace in the view's mode, with events
410
+ * off: an editable view would take the redraw for an edit.
411
+ */
370
412
  private renderWorkspaceView;
413
+ /**
414
+ * Tiles dropped on an editable added workspace, with the mouse or by touch,
415
+ * add their block to the main workspace at the same spot; the view shows it
416
+ * with its next copy. Returns the teardown.
417
+ */
418
+ private attachWorkspaceTileDrop;
371
419
  /** Remove an added view and everything it set up. */
372
420
  private disposeView;
373
421
  /**
@@ -378,10 +426,10 @@ export declare class MorphicBlocks extends EventTarget {
378
426
  private linkAddedView;
379
427
  /** A view by name, built in or added; `undefined` when there is none. */
380
428
  private resolveView;
381
- /** The mode a view shows, by name (`workspace`, `codespace`, `preview`, or an added view's). */
382
- getViewMode(name: string): MorphicModeName | undefined;
383
- /** A text view's mode must exist and have a code element to render. */
384
- private checkTextViewMode;
429
+ /** The element a view shows, by name. */
430
+ getViewElement(name: string): string | undefined;
431
+ /** A view shows a code element. */
432
+ private checkViewElement;
385
433
  /**
386
434
  * Write `newValue` to `edit.fieldName` on the Blockly block identified by
387
435
  * `edit.blockId`. The change event listener will trigger codespace re-sync.
@@ -393,14 +441,26 @@ export declare class MorphicBlocks extends EventTarget {
393
441
  * codespace mirrors Blockly's full set of moveable blocks.
394
442
  */
395
443
  private canMoveInCodespace;
444
+ /**
445
+ * Delete the block a line belongs to, or of each line from `line` to
446
+ * `lastLine`: a block inside another one deleted goes with it, and all of
447
+ * it is one undo step.
448
+ */
396
449
  private deleteBlockAtCodespaceLine;
397
- private generateCodespaceText;
450
+ /**
451
+ * The block a line belongs to, as the workspace would delete it: the
452
+ * statement that starts on the line (the innermost, for nested one-line
453
+ * templates), else the innermost statement around it (a closing `}`), and a
454
+ * value only where it stands as its own group. Defaults (shadows) are never
455
+ * picked; deleting one changes nothing.
456
+ */
457
+ private blockOwningLine;
398
458
  showCodeEditor(): void;
399
459
  hideCodeEditor(): void;
400
460
  isCodeEditorVisible(): boolean;
401
461
  setCodeEditorTheme(theme: MorphicCodeEditorTheme): void;
402
- setCodespaceTheme(theme: MorphicCodeEditorTheme): void;
403
- setPreviewTheme(theme: MorphicCodeEditorTheme): void;
462
+ /** Colours of a text view, by name, e.g. on a light and dark switch. */
463
+ setViewTheme(view: string, theme: MorphicCodeEditorTheme): void;
404
464
  /**
405
465
  * Enable bidirectional selection sync between the Blockly workspace and every
406
466
  * currently-mounted editor (code editor, codespace, preview). Requires at
@@ -419,7 +479,19 @@ export declare class MorphicBlocks extends EventTarget {
419
479
  private registerBlocks;
420
480
  /** Build a newly created block with this engine's definition, mode and behavior. */
421
481
  private initManagedBlock;
422
- private validateContainers;
482
+ /** Take the state saved with a block: the placeholder slots it had dealt with, and how many entries its repeating slots have. */
483
+ private loadBlockState;
484
+ /** Whether the block in a slot is the slot's placeholder default (a real block the framework put there). */
485
+ private isSlotDefault;
486
+ /** Build a block again as its view shows it now, e.g. after a repeating slot got or lost an entry. */
487
+ private rebuildBlock;
488
+ /**
489
+ * Give a repeating slot one entry more or less (`delta` +1 or -1). The last
490
+ * entry goes; a block in it is set free first, so nothing is deleted. One
491
+ * undo takes the change back, and other views follow like any other edit.
492
+ */
493
+ changeRepeat(block: Blockly.Block, slotName: string, delta: number): void;
494
+ private validateViews;
423
495
  private createHeadlessHost;
424
496
  private resolveMode;
425
497
  private applyView;
@@ -429,7 +501,6 @@ export declare class MorphicBlocks extends EventTarget {
429
501
  * is styled again once it does.
430
502
  */
431
503
  private styleBlock;
432
- private applyTextViewClasses;
433
504
  private applyWorkspaceContainerClass;
434
505
  /**
435
506
  * Measure block text with the font the workspace mode's CSS draws it in.
@@ -446,6 +517,10 @@ export declare class MorphicBlocks extends EventTarget {
446
517
  private renderFlyoutBlocks;
447
518
  private refreshToolbox;
448
519
  private bindFlyoutWorkspace;
520
+ /** Each block with its image paths passed through the mount's `assetUrl`; as loaded without one. */
521
+ private resolveAssetUrls;
522
+ /** The blocks the toolbox shows: the active block set's, else the mount's `toolbox.blocks` (all when unset). */
523
+ private toolboxBlocks;
449
524
  private resolveToolboxDefinition;
450
525
  private createBehaviorContext;
451
526
  private resolveClassNames;
@@ -0,0 +1,9 @@
1
+ import type { MorphicBlockDefinition, MorphicElementTypeEntry } from "./types";
2
+ /** Where the app loads an image the definitions name; `undefined` keeps the path. */
3
+ export type MorphicAssetUrl = (path: string) => string | undefined;
4
+ /**
5
+ * A block with every image path it names passed through `assetUrl`: the
6
+ * `<img>` in its templates, its image elements, and its dropdown pictures.
7
+ * Returns the block itself when nothing changes.
8
+ */
9
+ export declare function withAssetUrls(block: MorphicBlockDefinition, elementTypes: Record<string, MorphicElementTypeEntry>, assetUrl: MorphicAssetUrl): MorphicBlockDefinition;
@@ -0,0 +1,12 @@
1
+ import type { MorphicBlockSet } from "./types";
2
+ export interface ResolvedBlockSets {
3
+ /** Each set's blocks, in the set's order (the toolbox's order when categories are off). */
4
+ lists: Map<string, string[]>;
5
+ errors: string[];
6
+ warnings: string[];
7
+ }
8
+ /**
9
+ * The blocks of every set in `blockSets`. A set is a list, or another set
10
+ * (`extends`) with blocks added at the end and blocks removed.
11
+ */
12
+ export declare function resolveBlockSets(blockSets: Record<string, MorphicBlockSet> | undefined, isBlock: (identifier: string) => boolean): ResolvedBlockSets;
@@ -18,7 +18,29 @@ export type MorphicManagedBlock = Blockly.BlockSvg & {
18
18
  * until a mode that shows the field again restores it.
19
19
  */
20
20
  __morphicFieldMemory?: Map<string, unknown>;
21
+ /** The state of every default (shadow) the block has had, by input name. */
22
+ __morphicShadowMemory?: Map<string, Blockly.serialization.blocks.State>;
23
+ /**
24
+ * Set while a block is being made. A state loaded then belongs to a saved
25
+ * block (a program, a paste, an undo), not to an edit of this one.
26
+ */
27
+ __morphicFresh?: boolean;
21
28
  };
29
+ /** The slots whose placeholder default a block has dealt with, to save with it; `undefined` when it has none. */
30
+ export declare function savedPlaceholderSlots(block: Blockly.Block): string[] | undefined;
31
+ /**
32
+ * Give a block that is being made from a saved one the placeholder slots it
33
+ * had dealt with. The placeholders it was just built with are not its own:
34
+ * the saved block had kept, replaced or deleted them, and what it held is
35
+ * loaded next. Returns whether the state was taken.
36
+ */
37
+ export declare function loadPlaceholderSlots(block: Blockly.Block, saved: unknown): boolean;
38
+ /**
39
+ * Make an input of a block new again: it gets its default like the input of
40
+ * a new block, not what an earlier input of that name held. For the entry a
41
+ * repeating slot gets with plus.
42
+ */
43
+ export declare function forgetInputDefaults(block: Blockly.Block, inputName: string): void;
22
44
  export interface MorphicApplyBlockViewParams {
23
45
  block: Blockly.BlockSvg;
24
46
  definition: MorphicBlockDefinition;
@@ -39,6 +61,17 @@ export interface MorphicApplyBlockViewParams {
39
61
  * stock Blockly types pass through. Defaults to identity when omitted.
40
62
  */
41
63
  resolveBlocklyType?: (ref: string) => string;
64
+ /**
65
+ * Element types with their `code` settings, for the names `%1:type` shows.
66
+ * Unlike `elementTypes`, it adds no slot defaults. Falls back to `elementTypes`.
67
+ */
68
+ codeSettings?: Record<string, MorphicElementTypeEntry>;
69
+ /**
70
+ * Called when a plus or minus button of a repeating slot is pressed, with
71
+ * the slot's name and +1 or -1. Without it the block has no such buttons
72
+ * (toolbox tiles, read only views).
73
+ */
74
+ onRepeat?: (slotName: string, delta: number) => void;
42
75
  }
43
76
  export declare function applyBlockView(params: MorphicApplyBlockViewParams): void;
44
77
  /**
@@ -63,10 +96,45 @@ export declare function applyBlockIdentifierClass(block: Blockly.BlockSvg, ident
63
96
  */
64
97
  export declare function applyBlockColorFromCSS(block: Blockly.BlockSvg): void;
65
98
  /**
66
- * Mark a text view (codespace, preview) with the mode it shows, so a mode's
67
- * CSS can style it like the workspace and the tiles.
99
+ * Mark a view's container with combinable classes, so app CSS can style it:
100
+ * `morphic-view` and `morphic-view-<name>`, `morphic-shows-<element>` for
101
+ * the element it shows, its kind's root class (`morphic-workspace-root`,
102
+ * `morphic-codespace-root`) and any others given (`morphic-read-only`).
103
+ */
104
+ export declare function applyViewClasses(root: Element, view: {
105
+ name?: string;
106
+ element?: string;
107
+ classNames: string[];
108
+ }): void;
109
+ /** Mark a view's container with the preset applied last, `morphic-preset-<name>`. */
110
+ export declare function applyPresetClass(root: Element, preset: string | undefined): void;
111
+ /**
112
+ * Mark a block's drawing: a workspace block with the element it shows
113
+ * (`morphic-shows-<element>`), a toolbox block with its mode
114
+ * (`morphic-mode-<mode>`), and both with the context.
115
+ */
116
+ export declare function decorateBlockRoot(block: Blockly.BlockSvg, shown: string, context: MorphicRenderContext): void;
117
+ /**
118
+ * The plus or minus button of a repeating slot: a small field that is pressed
119
+ * instead of edited. It is text, so app CSS styles it through its classes,
120
+ * `morphic-repeat-button` and `morphic-repeat-add` / `morphic-repeat-remove`.
121
+ */
122
+ export declare class MorphicRepeatButton extends Blockly.Field<string> {
123
+ readonly action: "add" | "remove";
124
+ private readonly press;
125
+ EDITABLE: boolean;
126
+ SERIALIZABLE: boolean;
127
+ CURSOR: string;
128
+ constructor(action: "add" | "remove", press: () => void);
129
+ initView(): void;
130
+ protected showEditor_(): void;
131
+ }
132
+ /**
133
+ * Give a block the declared fields its template leaves out, on a row that is
134
+ * not drawn. The template decides what a block shows, `fields` what it has,
135
+ * so text views, generated code and saved programs find every field whatever
136
+ * the workspace shows.
68
137
  */
69
- export declare function applyTextViewModeClass(root: Element, mode: MorphicModeName | undefined, rootClassName: string): void;
70
- export declare function decorateBlockRoot(block: Blockly.BlockSvg, mode: MorphicModeName, context: MorphicRenderContext): void;
138
+ export declare function keepOmittedFields(block: Blockly.BlockSvg, definition: MorphicBlockDefinition, elementName: string | undefined): void;
71
139
  export declare function captureFieldValues(block: Blockly.BlockSvg): Map<string, unknown>;
72
140
  export declare function restoreFieldValues(block: Blockly.BlockSvg, values: Map<string, unknown>): void;
@@ -0,0 +1,31 @@
1
+ import type { MorphicBehaviorMap } from "./types";
2
+ import { type DefinitionValidationResult } from "./validate-definitions";
3
+ import type { MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicElementTypeEntry, MorphicModeDefinition, MorphicPresetDefinition, MorphicViewDeclaration, MorphicViewKind } from "./types";
4
+ /** The views without a `views` section: one workspace, one codespace. */
5
+ export declare const DEFAULT_VIEWS: Record<string, MorphicViewKind>;
6
+ /** The views in their long form; a bare kind is the short form. */
7
+ export declare function normalizeViews(views: Record<string, MorphicViewKind | MorphicViewDeclaration> | undefined): Record<string, MorphicViewDeclaration>;
8
+ /** Each declared view's kind. */
9
+ export declare function checkViewDeclarations(views: Record<string, MorphicViewDeclaration>): string[];
10
+ /**
11
+ * The presets against the modes, views and elements: duplicate names, an
12
+ * unknown toolbox mode, views that show no code element. A view no one
13
+ * declared only warns, since a view added later may carry the name.
14
+ */
15
+ export declare function checkPresets(args: {
16
+ presets: MorphicPresetDefinition[] | undefined;
17
+ modes: MorphicModeDefinition[] | undefined;
18
+ views: Record<string, MorphicViewDeclaration>;
19
+ elementTypes: Record<string, MorphicElementTypeEntry>;
20
+ }): DefinitionValidationResult;
21
+ /** Settings that moved in an earlier version, named so an upgrade fails with directions. */
22
+ export declare function movedDefinitionErrors(presets: MorphicPresetDefinition[] | undefined, hasHighlighting: boolean): string[];
23
+ /**
24
+ * Every check `mount()` runs on a definitions file, without mounting: the
25
+ * blocks themselves, the definitions, the presets and the views. Errors stop
26
+ * a mount; warnings do not. With `modeStyles` (the modes that have a
27
+ * stylesheet), a mode without one warns too.
28
+ */
29
+ export declare function checkDefinitions(format: MorphicBlocksFormat | MorphicBlocksFormatJson, behaviors?: MorphicBehaviorMap, options?: {
30
+ modeStyles?: string[];
31
+ }): DefinitionValidationResult;
@@ -75,15 +75,17 @@ export declare class MorphicCodeEditor {
75
75
  private syntaxHighlightCompartment?;
76
76
  private highlightEffect?;
77
77
  /** The host's highlight colour; without one, the theme's selection colour is used. */
78
- private highlightColor?;
79
78
  private themeSelection;
80
79
  /** The lines highlighted now, drawn again when the colour changes. */
81
80
  private activeHighlight?;
82
81
  private metadataEffect?;
83
82
  private placeholderEffect?;
83
+ private repeatEffect?;
84
84
  private dropIndicatorEffect?;
85
85
  private valueSlotHighlightEffect?;
86
86
  private hoverHighlightEffect?;
87
+ /** Hands the code view the line ranges of the groups of blocks. */
88
+ private groupsEffect?;
87
89
  private editableHoverHighlightEffect?;
88
90
  private emptyClickListener?;
89
91
  private disposed;
@@ -107,6 +109,14 @@ export declare class MorphicCodeEditor {
107
109
  * before they act. Driven from `setHoverHighlight` (called by MorphicBlocks
108
110
  * on the codespace container's `mousemove`/`mouseleave`).
109
111
  */
112
+ /**
113
+ * Classes on the lines of each group of connected blocks, and on their
114
+ * gutter (line numbers, delete, grip): `morphic-group-start`, `-end`,
115
+ * `-line` (groups of more than one line), `-odd` / `-even` (every other
116
+ * group) and `-hover` (the group under the mouse). They change nothing
117
+ * visible; the app styles them.
118
+ */
119
+ private buildGroupExtensions;
110
120
  private buildHoverHighlightExtensions;
111
121
  /** Highlight the char range of the block currently under the mouse cursor. */
112
122
  setHoverHighlight(range: {
@@ -132,8 +142,6 @@ export declare class MorphicCodeEditor {
132
142
  highlightLines(spans: LineSpan | LineSpan[]): void;
133
143
  /** Remove all line highlights. */
134
144
  clearHighlight(): void;
135
- /** Update the highlight background colour; `undefined` follows the theme's selection colour. */
136
- setHighlightColor(color: string | undefined): void;
137
145
  /** Show a drop-position indicator above (or below) the given 1-based line. */
138
146
  showDropIndicator(line: number, position?: "above" | "below"): void;
139
147
  /** Hide the drop-position indicator if currently shown. */
@@ -164,6 +172,17 @@ export declare class MorphicCodeEditor {
164
172
  refresh(): void;
165
173
  /** Current editor text. Empty string when the editor is not mounted yet. */
166
174
  getValue(): string;
175
+ /** The 1-based lines the selection covers, or `null` when nothing is selected. */
176
+ getSelectedLines(): {
177
+ first: number;
178
+ last: number;
179
+ } | null;
180
+ /**
181
+ * Drop the selection, also the browser's: while the editor is not focused
182
+ * (e.g. a toolbar button was clicked), the browser keeps showing it on
183
+ * whatever text moves into its place.
184
+ */
185
+ clearSelection(): void;
167
186
  /** 1-based line number under the cursor. Returns 1 when no cursor / not mounted. */
168
187
  getCursorLine(): number;
169
188
  /**
@@ -1,5 +1,11 @@
1
1
  export declare const MODE_CLASS_PREFIX = "morphic-mode-";
2
2
  export declare const CONTEXT_CLASS_PREFIX = "morphic-context-";
3
+ /** On a view and its blocks: the element the view shows. */
4
+ export declare const SHOWS_CLASS_PREFIX = "morphic-shows-";
5
+ /** On a view: its name. */
6
+ export declare const VIEW_CLASS_PREFIX = "morphic-view-";
7
+ /** On a view: the preset applied last. */
8
+ export declare const PRESET_CLASS_PREFIX = "morphic-preset-";
3
9
  export declare const CATEGORY_CLASS_PREFIX = "morphic-category-";
4
10
  export declare const BLOCK_CLASS_PREFIX = "morphic-block-";
5
11
  export declare const ALIGN_LEFT = -1;