morphic-blocks 0.2.1 → 0.4.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 +93 -53
- package/definitions.schema.json +112 -26
- package/dist/cli.js +25 -0
- package/dist/copy-media-CllRyMqQ.js +21 -0
- package/dist/copy-media.d.ts +8 -0
- package/dist/esbuild.d.ts +3 -0
- package/dist/esbuild.js +10 -0
- package/dist/index.d.ts +2 -1
- package/dist/morphic/MorphicBlocks.d.ts +180 -59
- package/dist/morphic/block-view.d.ts +36 -0
- package/dist/morphic/code-editor.d.ts +30 -4
- package/dist/morphic/constants.d.ts +6 -0
- package/dist/morphic/element-types.d.ts +11 -3
- package/dist/morphic/resize.d.ts +7 -2
- package/dist/morphic/styles.d.ts +26 -3
- package/dist/morphic/syntax-highlight.d.ts +13 -0
- package/dist/morphic/template-codegen.d.ts +5 -1
- package/dist/morphic/template.d.ts +11 -1
- package/dist/morphic/toolbar.d.ts +13 -11
- package/dist/morphic/toolbox-canvas.d.ts +75 -5
- package/dist/morphic/types.d.ts +351 -74
- package/dist/morphic/validate-definitions.d.ts +5 -3
- package/dist/morphic/value-format.d.ts +8 -0
- package/dist/morphic/value-type.d.ts +13 -0
- package/dist/morphic/view-resolver.d.ts +5 -0
- package/dist/morphic-blocks.js +3671 -2420
- package/dist/morphic-blocks.umd.cjs +39 -30
- package/dist/plugin-h3QH8ods.js +19 -0
- package/dist/plugin.d.ts +9 -0
- package/dist/rollup.d.ts +3 -0
- package/dist/rollup.js +10 -0
- package/dist/rspack.d.ts +3 -0
- package/dist/rspack.js +10 -0
- package/dist/vite.d.ts +3 -0
- package/dist/vite.js +10 -0
- package/dist/webpack.d.ts +3 -0
- package/dist/webpack.js +10 -0
- package/package.json +42 -8
package/dist/morphic/types.d.ts
CHANGED
|
@@ -5,11 +5,27 @@ 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
|
-
* `check` in `
|
|
28
|
+
* `check` in `code[element].empty[check]`, or set per-slot via
|
|
13
29
|
* `inputSlot.default`. Both `shadow` and `placeholder` are optional; when
|
|
14
30
|
* both are configured, the placeholder is created first and Blockly's
|
|
15
31
|
* native shadow restoration brings the shadow up if the user removes the
|
|
@@ -41,11 +57,30 @@ export interface MorphicEmptyDefaultConfig {
|
|
|
41
57
|
}
|
|
42
58
|
/**
|
|
43
59
|
* Optional per-element configuration. Used in place of a bare type string in
|
|
44
|
-
* `elementTypes` when
|
|
45
|
-
*
|
|
60
|
+
* `elementTypes` when an image element needs its display `size`. How a code
|
|
61
|
+
* element's language is written lives in the `code` section instead.
|
|
46
62
|
*/
|
|
47
63
|
export interface MorphicElementTypeConfig {
|
|
48
64
|
type: MorphicElementType;
|
|
65
|
+
/**
|
|
66
|
+
* Display size for `type: "image"` elements. Used when the element value is
|
|
67
|
+
* a file path (e.g. `"assets/icon.svg"`) and the framework auto-wraps it as
|
|
68
|
+
* an `<img>` tag. Accepted formats:
|
|
69
|
+
* - number: square, e.g. `32` → 32×32
|
|
70
|
+
* - `"32"`: square
|
|
71
|
+
* - `"32x32"`: explicit width × height
|
|
72
|
+
* Defaults to 16×16 if omitted. Ignored for non-image element types and for
|
|
73
|
+
* values that are already `<img>` HTML.
|
|
74
|
+
*/
|
|
75
|
+
size?: number | string;
|
|
76
|
+
}
|
|
77
|
+
/** Either a bare type or a config object with extras. */
|
|
78
|
+
/**
|
|
79
|
+
* How one code element's language is written, in the `code` section keyed by
|
|
80
|
+
* element name: the literals the framework writes into empty slots and how
|
|
81
|
+
* the codespace and preview colour it.
|
|
82
|
+
*/
|
|
83
|
+
export interface MorphicCodeElementConfig {
|
|
49
84
|
/**
|
|
50
85
|
* Per-slot-check defaults for empty value inputs. Keys are the slot's
|
|
51
86
|
* `check` string (e.g. `"Number"`, `"String"`, or any developer-defined
|
|
@@ -66,18 +101,47 @@ export interface MorphicElementTypeConfig {
|
|
|
66
101
|
*/
|
|
67
102
|
stringQuote?: string;
|
|
68
103
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* - number: square, e.g. `32` → 32×32
|
|
73
|
-
* - `"32"`: square
|
|
74
|
-
* - `"32x32"`: explicit width × height
|
|
75
|
-
* Defaults to 16×16 if omitted. Ignored for non-image element types and for
|
|
76
|
-
* values that are already `<img>` HTML.
|
|
104
|
+
* Written by the codespace and preview into an empty statement slot, for
|
|
105
|
+
* languages where an empty body is invalid (`"pass"` for Python). Omit to
|
|
106
|
+
* leave empty bodies empty.
|
|
77
107
|
*/
|
|
78
|
-
|
|
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
|
+
/** Token highlighting for the codespace and preview showing this element. */
|
|
117
|
+
highlighting?: MorphicHighlightDefinition;
|
|
118
|
+
/**
|
|
119
|
+
* How `runJavaScript()` writes printed values while the codespace shows this
|
|
120
|
+
* element. Programs run as JavaScript, so values otherwise read like it.
|
|
121
|
+
*/
|
|
122
|
+
values?: MorphicValueFormat;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Printed values in a language's own spelling, e.g. Python:
|
|
126
|
+
* `{ "true": "True", "false": "False", "null": "None", "list": { "quote": "'" } }`.
|
|
127
|
+
* Each entry replaces JavaScript's text for that value; left out, it stays.
|
|
128
|
+
*/
|
|
129
|
+
export interface MorphicValueFormat {
|
|
130
|
+
true?: string;
|
|
131
|
+
false?: string;
|
|
132
|
+
null?: string;
|
|
133
|
+
undefined?: string;
|
|
134
|
+
NaN?: string;
|
|
135
|
+
Infinity?: string;
|
|
136
|
+
"-Infinity"?: string;
|
|
137
|
+
/** Lists: brackets, separator and quotes around texts in them. Left out, lists read `1,2,3`. */
|
|
138
|
+
list?: {
|
|
139
|
+
open?: string;
|
|
140
|
+
close?: string;
|
|
141
|
+
separator?: string;
|
|
142
|
+
quote?: string;
|
|
143
|
+
};
|
|
79
144
|
}
|
|
80
|
-
/** Either a bare type or a config object with extras. */
|
|
81
145
|
export type MorphicElementTypeEntry = MorphicElementType | MorphicElementTypeConfig;
|
|
82
146
|
export type MorphicConnectionSpec = boolean | string | string[];
|
|
83
147
|
/**
|
|
@@ -112,12 +176,32 @@ export type MorphicInputAlign = "left" | "centre" | "right";
|
|
|
112
176
|
* - `"=="` → value = label = `"=="`
|
|
113
177
|
* - `["-", "−"]` → value `"-"`, shown as `"−"` on the block
|
|
114
178
|
* - `{ value, label?, display? }` → explicit object form
|
|
179
|
+
*
|
|
180
|
+
* `label` can also be an image ({@link MorphicDropdownImage}): the block shows
|
|
181
|
+
* the picture, text views still write the value.
|
|
115
182
|
*/
|
|
116
183
|
export type MorphicDropdownOption = string | [value: string, label: string] | {
|
|
117
184
|
value: string;
|
|
118
|
-
label?: string;
|
|
185
|
+
label?: string | MorphicDropdownImage;
|
|
119
186
|
display?: Record<string, string>;
|
|
120
187
|
};
|
|
188
|
+
/**
|
|
189
|
+
* A picture shown as a dropdown choice on the block: an image file (`src`) or
|
|
190
|
+
* a swatch the framework draws in a CSS color (`color`, e.g. `"#e53935"`).
|
|
191
|
+
* `alt` (what screen readers say) defaults to the option's value; the size
|
|
192
|
+
* defaults to 16 × 16.
|
|
193
|
+
*/
|
|
194
|
+
export type MorphicDropdownImage = ({
|
|
195
|
+
src: string;
|
|
196
|
+
color?: never;
|
|
197
|
+
} | {
|
|
198
|
+
color: string;
|
|
199
|
+
src?: never;
|
|
200
|
+
}) & {
|
|
201
|
+
alt?: string;
|
|
202
|
+
width?: number;
|
|
203
|
+
height?: number;
|
|
204
|
+
};
|
|
121
205
|
/**
|
|
122
206
|
* A field declared directly on a block (rendered at the `%FIELDNAME` token
|
|
123
207
|
* position). Lets dropdown / text / number / checkbox fields live in the
|
|
@@ -169,23 +253,39 @@ export type MorphicBlockElements = Record<string, string>;
|
|
|
169
253
|
* is part of the preset: `toolbox` is required, and at least one editing
|
|
170
254
|
* space (`workspace` or `codespace`) must be set; `preview` is optional.
|
|
171
255
|
*/
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
* blocks) or an object that additionally overrides, per element, whether a
|
|
175
|
-
* `code` element renders as a draggable block or as source text in the tile.
|
|
176
|
-
*/
|
|
177
|
-
export type MorphicPresetToolbox = MorphicModeName | {
|
|
178
|
-
mode: MorphicModeName;
|
|
179
|
-
render?: Record<string, "block" | "text">;
|
|
180
|
-
};
|
|
256
|
+
/** How a mode's code element is drawn on a tile: as a block, or as its text. */
|
|
257
|
+
export type MorphicTileRender = "block-based" | "text-based";
|
|
181
258
|
export interface MorphicPresetDefinition {
|
|
182
259
|
name: string;
|
|
183
260
|
/** Display label (e.g. for buttons). Falls back to `name`. */
|
|
184
261
|
label?: string;
|
|
185
|
-
toolbox
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
262
|
+
/** The mode of the toolbox tiles; the mode's `render` says how they are drawn. */
|
|
263
|
+
toolbox: MorphicModeName;
|
|
264
|
+
/**
|
|
265
|
+
* The code element each view shows, by view name, e.g. `{ "blocks": "concept", "code": "python" }`.
|
|
266
|
+
* The views it lists are the ones the preset shows; showing and hiding
|
|
267
|
+
* their containers is the app's job (`onPresetApplied`). Views it leaves
|
|
268
|
+
* out keep what they show.
|
|
269
|
+
*/
|
|
270
|
+
views: Record<string, string>;
|
|
271
|
+
}
|
|
272
|
+
/** Kinds of view: blocks, or the program as text. */
|
|
273
|
+
export type MorphicViewKind = "workspace" | "codespace";
|
|
274
|
+
/** A view in the definitions' `views`, by name. A bare kind is the short form. */
|
|
275
|
+
export interface MorphicViewDeclaration {
|
|
276
|
+
kind: MorphicViewKind;
|
|
277
|
+
/**
|
|
278
|
+
* Whether the program can be changed in the view. Default `true`. A
|
|
279
|
+
* codespace that cannot be edited shows the program as text, read only.
|
|
280
|
+
*/
|
|
281
|
+
editable?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Right to left (`true`) or left to right (`false`). Left out, a workspace
|
|
284
|
+
* follows `blockly.rtl` and a codespace the page around it.
|
|
285
|
+
*/
|
|
286
|
+
rtl?: boolean;
|
|
287
|
+
/** How blocks are deleted in a codespace. */
|
|
288
|
+
delete?: MorphicCodespaceDeleteOptions;
|
|
189
289
|
}
|
|
190
290
|
/**
|
|
191
291
|
* Declares which elements are visible for a given mode.
|
|
@@ -197,6 +297,11 @@ export interface MorphicModeDefinition {
|
|
|
197
297
|
name: string;
|
|
198
298
|
/** Element names that are visible in this mode (e.g. ["icon", "text"]). */
|
|
199
299
|
elements: string[];
|
|
300
|
+
/**
|
|
301
|
+
* How the mode's code elements are drawn on toolbox tiles, by element name:
|
|
302
|
+
* `"block-based"` (the default) or `"text-based"`, e.g. `{ "python": "text-based" }`.
|
|
303
|
+
*/
|
|
304
|
+
render?: Record<string, MorphicTileRender>;
|
|
200
305
|
}
|
|
201
306
|
export interface MorphicBlockDefinition {
|
|
202
307
|
identifier: string;
|
|
@@ -248,10 +353,14 @@ export interface MorphicBlockDefinition {
|
|
|
248
353
|
* Blocks treats text as a rendered view, not a freeform editing surface.
|
|
249
354
|
*/
|
|
250
355
|
export interface MorphicHighlightDefinition {
|
|
251
|
-
/** Words to highlight as keywords
|
|
356
|
+
/** Words to highlight as keywords, matched as whole words in any script (`if`, `اطبع`). */
|
|
252
357
|
keywords?: string[];
|
|
253
|
-
/**
|
|
254
|
-
|
|
358
|
+
/**
|
|
359
|
+
* String delimiters. A mark that opens and closes (`"\""`, `"'"`), or an
|
|
360
|
+
* `[open, close]` pair for quotes that differ (`["„", "“"]`, `["«", "»"]`).
|
|
361
|
+
* A string spans to its close on the same line.
|
|
362
|
+
*/
|
|
363
|
+
strings?: (string | [open: string, close: string])[];
|
|
255
364
|
/** Line-comment marker (e.g. `"#"`, `"//"`). Highlights from the marker to end of line. */
|
|
256
365
|
comment?: string;
|
|
257
366
|
/** Highlight integer/decimal numeric literals. Defaults to true; pass `false` to disable. */
|
|
@@ -287,19 +396,26 @@ export interface MorphicBlocksFormat {
|
|
|
287
396
|
/**
|
|
288
397
|
* Global element type registry.
|
|
289
398
|
* Maps each element name to either a bare type ("text" | "code" | "image")
|
|
290
|
-
* or a config object (`{ type,
|
|
399
|
+
* or a config object (`{ type, size? }`). Declared once here; per-block
|
|
291
400
|
* elements remain plain name→content strings.
|
|
292
401
|
*/
|
|
293
402
|
elementTypes?: Record<string, MorphicElementTypeEntry>;
|
|
294
403
|
/** Explicit mode definitions — which elements are visible per mode. */
|
|
295
404
|
modes?: MorphicModeDefinition[];
|
|
405
|
+
/**
|
|
406
|
+
* The views, by name: `{ "blocks": "workspace", "read": { "kind": "codespace", "editable": false } }`.
|
|
407
|
+
* `mount()` sets up each one it gets a container for. The first workspace
|
|
408
|
+
* with a container holds the program. Default: a `workspace` and a
|
|
409
|
+
* `codespace` view.
|
|
410
|
+
*/
|
|
411
|
+
views?: Record<string, MorphicViewKind | MorphicViewDeclaration>;
|
|
296
412
|
/** Named per-view mode configurations. Used as the mount default for `presets`. */
|
|
297
413
|
presets?: MorphicPresetDefinition[];
|
|
298
414
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
415
|
+
* How each code element's language is written, keyed by element name:
|
|
416
|
+
* string quotes, empty slot defaults and highlighting.
|
|
301
417
|
*/
|
|
302
|
-
|
|
418
|
+
code?: Record<string, MorphicCodeElementConfig>;
|
|
303
419
|
/** Optional category metadata. Blocks reference categories by name. */
|
|
304
420
|
categories?: MorphicToolboxCategory[];
|
|
305
421
|
/** Flat array of block definitions. */
|
|
@@ -326,14 +442,18 @@ export interface MorphicRunResult {
|
|
|
326
442
|
export interface MorphicBehaviorContext {
|
|
327
443
|
Blockly: typeof Blockly;
|
|
328
444
|
workspace: Blockly.WorkspaceSvg;
|
|
329
|
-
|
|
445
|
+
/** The code element the block is drawn from. */
|
|
446
|
+
element: string;
|
|
447
|
+
/** The toolbox mode, for blocks drawn in the toolbox. */
|
|
448
|
+
mode?: MorphicModeName;
|
|
330
449
|
context: MorphicRenderContext;
|
|
331
450
|
definition: MorphicBlockDefinition;
|
|
332
451
|
}
|
|
333
452
|
export interface MorphicBehaviorProxy {
|
|
334
453
|
blockId: string;
|
|
335
454
|
blockType: string;
|
|
336
|
-
|
|
455
|
+
/** The code element the block is drawn from in its view. */
|
|
456
|
+
element: string;
|
|
337
457
|
context: MorphicRenderContext;
|
|
338
458
|
inputs: Record<string, string>;
|
|
339
459
|
/**
|
|
@@ -368,15 +488,93 @@ export interface MorphicToolboxCategory {
|
|
|
368
488
|
blocks?: string[];
|
|
369
489
|
}
|
|
370
490
|
export type MorphicToolboxLayout = "flyout" | "category";
|
|
371
|
-
|
|
491
|
+
/** How the HTML toolbox shows categories and scrolls; part of both toolbox option sets. */
|
|
492
|
+
export interface MorphicToolboxDisplayOptions {
|
|
493
|
+
/**
|
|
494
|
+
* `"stacked"` (default): every category under its heading. `"tabs"`: a list
|
|
495
|
+
* of the category names and the tiles of the chosen one, the first at the
|
|
496
|
+
* start; `collapsed` does not apply. The container gets
|
|
497
|
+
* `morphic-toolbox-stacked` or `morphic-toolbox-tabs`; where the list sits
|
|
498
|
+
* is the app's CSS. Without categories, tiles are a flat list either way.
|
|
499
|
+
*/
|
|
500
|
+
layout?: "stacked" | "tabs";
|
|
501
|
+
/** A heading with the category's name above its tiles. Default `true`. */
|
|
502
|
+
categoryHeadings?: boolean;
|
|
503
|
+
/** A click on a heading collapses or expands its category. Default `true`. */
|
|
504
|
+
collapsible?: boolean;
|
|
505
|
+
/**
|
|
506
|
+
* Categories collapsed at the start: `true` for all, or their names.
|
|
507
|
+
* Default: all open. The toolbox keeps the state across redraws.
|
|
508
|
+
*/
|
|
509
|
+
collapsed?: boolean | string[];
|
|
510
|
+
/** Called when a category is collapsed or expanded, e.g. to remember it. */
|
|
511
|
+
onCategoryToggle?: (name: string, collapsed: boolean) => void;
|
|
512
|
+
/**
|
|
513
|
+
* Show the container's scrollbar. Default `false`: hidden, and the edges
|
|
514
|
+
* fade where more tiles wait. Either way the container gets the classes
|
|
515
|
+
* `morphic-toolbox`, `morphic-toolbox-more-above` and
|
|
516
|
+
* `morphic-toolbox-more-below` (while tiles are hidden there) for the
|
|
517
|
+
* app's own styles; the defaults have no weight (`:where()`), so any app
|
|
518
|
+
* rule wins. Works when the container is the element that scrolls.
|
|
519
|
+
*/
|
|
520
|
+
scrollbar?: boolean;
|
|
521
|
+
/**
|
|
522
|
+
* How each `custom` element is drawn on the tiles, by element name.
|
|
523
|
+
* Pressing a button, link or input inside one does not drag the tile, and
|
|
524
|
+
* Enter or Space there does not add the block.
|
|
525
|
+
*/
|
|
526
|
+
custom?: Record<string, MorphicElementRenderer>;
|
|
527
|
+
/**
|
|
528
|
+
* The element whose text names each tile for screen readers (its
|
|
529
|
+
* `aria-label`), e.g. a `spoken` text element no mode lists; a function
|
|
530
|
+
* gets the toolbox mode and returns the element's name. A block without
|
|
531
|
+
* that element is named by what its tile shows, as without this option.
|
|
532
|
+
*/
|
|
533
|
+
label?: string | ((mode: string) => string);
|
|
534
|
+
}
|
|
535
|
+
export interface MorphicToolboxConfig extends MorphicToolboxDisplayOptions {
|
|
372
536
|
kind?: "flyoutToolbox" | "categoryToolbox";
|
|
537
|
+
/** Show only these blocks, in the HTML toolbox and in Blockly's own. */
|
|
373
538
|
blocks?: string[];
|
|
374
539
|
categories?: MorphicToolboxCategory[];
|
|
540
|
+
/** Header of the HTML toolbox; see {@link MorphicToolboxModeLabel}. */
|
|
541
|
+
modeLabel?: MorphicToolboxModeLabel;
|
|
542
|
+
/** Colour code shown as text on tiles like the codespace does. Default `true`. */
|
|
543
|
+
highlight?: boolean;
|
|
544
|
+
/** Tiles can be dragged with a finger or pen. Default `true`. */
|
|
545
|
+
touch?: boolean;
|
|
546
|
+
/** Tiles can be reached with Tab and added with Enter or Space. Default `true`. */
|
|
547
|
+
keyboard?: boolean;
|
|
375
548
|
}
|
|
549
|
+
/**
|
|
550
|
+
* Header of the HTML toolbox: `true` shows "Mode: <name>" (the default),
|
|
551
|
+
* `false` none, a string that text, and a function the text it returns for
|
|
552
|
+
* the active mode (`(mode) => mode` shows the bare name).
|
|
553
|
+
*/
|
|
554
|
+
export type MorphicToolboxModeLabel = boolean | string | ((mode: string) => string);
|
|
376
555
|
/** Options for the custom HTML toolbox canvas (mountToolbox). */
|
|
377
|
-
export interface MorphicToolboxCanvasOptions {
|
|
378
|
-
/**
|
|
379
|
-
modeLabel?:
|
|
556
|
+
export interface MorphicToolboxCanvasOptions extends MorphicToolboxDisplayOptions {
|
|
557
|
+
/** Header of the toolbox; see {@link MorphicToolboxModeLabel}. Defaults to "Mode: <name>". */
|
|
558
|
+
modeLabel?: MorphicToolboxModeLabel;
|
|
559
|
+
/**
|
|
560
|
+
* Colour code shown as text on tiles (`render: "text"`) with its element's
|
|
561
|
+
* `code.<element>.highlighting`, as the codespace does. Default `true`.
|
|
562
|
+
*/
|
|
563
|
+
highlight?: boolean;
|
|
564
|
+
/**
|
|
565
|
+
* Tiles can be dragged with a finger or pen into the workspace and the
|
|
566
|
+
* codespace. Default `true`. A tile starts moving when dragged sideways (the
|
|
567
|
+
* toolbox still scrolls up and down) or after a long press.
|
|
568
|
+
*/
|
|
569
|
+
touch?: boolean;
|
|
570
|
+
/**
|
|
571
|
+
* Tiles can be reached with Tab and read by screen readers as buttons named
|
|
572
|
+
* after what they show (code as the codespace writes it, text, image alt
|
|
573
|
+
* text). Enter or Space adds the block after
|
|
574
|
+
* the selected one when it fits there, else as a new stack, and selects it,
|
|
575
|
+
* so pressing again builds a chain. Default `true`.
|
|
576
|
+
*/
|
|
577
|
+
keyboard?: boolean;
|
|
380
578
|
/** Show only a subset of blocks. Defaults to all blocks in definitions. */
|
|
381
579
|
blocks?: string[];
|
|
382
580
|
/**
|
|
@@ -394,35 +592,34 @@ export interface MorphicJavaScriptConfig {
|
|
|
394
592
|
}
|
|
395
593
|
export interface MorphicMountConfig {
|
|
396
594
|
/**
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
* model stays available.
|
|
595
|
+
* The views' containers, by view name. A view without one is not set up,
|
|
596
|
+
* so the same definitions serve a full page and a small embed. At least one
|
|
597
|
+
* view needs a container. Without a workspace, Blockly runs headless
|
|
598
|
+
* (offscreen) so the block model stays available.
|
|
401
599
|
*/
|
|
600
|
+
containers?: Record<string, HTMLElement>;
|
|
601
|
+
/** Short for `containers.workspace`. */
|
|
402
602
|
workspaceContainer?: HTMLElement;
|
|
403
|
-
/**
|
|
404
|
-
* Container for the primary text editor (codespace). Optional, but required
|
|
405
|
-
* when a preset shows the codespace. `mount()` sets the codespace up in it.
|
|
406
|
-
*/
|
|
603
|
+
/** Short for `containers.codespace`. */
|
|
407
604
|
codespaceContainer?: HTMLElement;
|
|
408
605
|
/**
|
|
409
606
|
* Container for the custom HTML toolbox. `mount()` sets the toolbox up in it,
|
|
410
607
|
* which implies `canvasToolbox`.
|
|
411
608
|
*/
|
|
412
609
|
toolboxContainer?: HTMLElement;
|
|
413
|
-
/** Container for the read-only preview. `mount()` sets the preview up in it. */
|
|
414
|
-
previewContainer?: HTMLElement;
|
|
415
610
|
/**
|
|
416
611
|
* Container for the generated JavaScript view, a developer tool. `mount()`
|
|
417
612
|
* sets it up hidden; `showCodeEditor()` reveals it.
|
|
418
613
|
*/
|
|
419
614
|
codeEditorContainer?: HTMLElement;
|
|
420
|
-
/** Toolbar containers
|
|
421
|
-
|
|
422
|
-
/** Theme for the
|
|
615
|
+
/** Toolbar containers, by view name, set up once the views are ready. */
|
|
616
|
+
toolbars?: Record<string, HTMLElement>;
|
|
617
|
+
/** Theme for the text views and the code editor. */
|
|
423
618
|
editorTheme?: MorphicCodeEditorTheme;
|
|
424
|
-
/**
|
|
425
|
-
|
|
619
|
+
/** Themes of single text views, by view name, in place of `editorTheme`. */
|
|
620
|
+
themes?: Record<string, MorphicCodeEditorTheme>;
|
|
621
|
+
/** The views, in place of the definitions' `views`. */
|
|
622
|
+
views?: Record<string, MorphicViewKind | MorphicViewDeclaration>;
|
|
426
623
|
/**
|
|
427
624
|
* Link block and code selection across views. On by default when `mount()`
|
|
428
625
|
* sets up more than one view; `false` turns it off, an object configures it.
|
|
@@ -439,10 +636,16 @@ export interface MorphicMountConfig {
|
|
|
439
636
|
preset?: string;
|
|
440
637
|
/**
|
|
441
638
|
* Called after a preset is applied — at mount and on every `applyPreset` —
|
|
442
|
-
* so the host can lay out pane visibility (
|
|
443
|
-
*
|
|
639
|
+
* so the host can lay out pane visibility (the preset shows the views its
|
|
640
|
+
* `views` lists).
|
|
444
641
|
*/
|
|
445
642
|
onPresetApplied?: (preset: MorphicPresetDefinition) => void;
|
|
643
|
+
/**
|
|
644
|
+
* Receives every framework warning (definition warnings, modes without
|
|
645
|
+
* CSS, missing Blockly media) instead of the browser console, e.g. to show
|
|
646
|
+
* them in the app's own development mode.
|
|
647
|
+
*/
|
|
648
|
+
onWarning?: (message: string) => void;
|
|
446
649
|
toolbox?: MorphicToolboxConfig;
|
|
447
650
|
toolboxLayout?: MorphicToolboxLayout;
|
|
448
651
|
/**
|
|
@@ -470,15 +673,20 @@ export interface MorphicMountConfig {
|
|
|
470
673
|
*/
|
|
471
674
|
modesFolder?: Record<string, unknown>;
|
|
472
675
|
baseStyle?: MorphicStyleBundle;
|
|
473
|
-
/**
|
|
676
|
+
/**
|
|
677
|
+
* Options passed through to Blockly's `inject` (zoom, grid, trashcan, and so
|
|
678
|
+
* on). `media` defaults to `"blockly-media/"` next to the page, filled by
|
|
679
|
+
* the bundler plugin (`morphic-blocks/vite` and others) or
|
|
680
|
+
* `npx morphic-blocks copy-media`, so Blockly never loads from another server
|
|
681
|
+
* unless the host sets it.
|
|
682
|
+
*/
|
|
474
683
|
blockly?: Blockly.BlocklyOptions;
|
|
475
684
|
javascript?: MorphicJavaScriptConfig;
|
|
476
685
|
/**
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
* their editors. Optional — when absent, editors render plain text.
|
|
686
|
+
* How each code element's language is written, keyed by element name.
|
|
687
|
+
* Defaults to the definitions' `code` section.
|
|
480
688
|
*/
|
|
481
|
-
|
|
689
|
+
code?: Record<string, MorphicCodeElementConfig>;
|
|
482
690
|
}
|
|
483
691
|
/**
|
|
484
692
|
* Resolved workspace template for a block in a given mode.
|
|
@@ -572,6 +780,14 @@ export interface MorphicCodeGenerationResult {
|
|
|
572
780
|
metadata: MorphicCodeMetadata;
|
|
573
781
|
/** Value-slot ranges with default/set classification. Empty if no value slots emitted. */
|
|
574
782
|
placeholders: MorphicPlaceholderRange[];
|
|
783
|
+
/**
|
|
784
|
+
* Line range (1-based, inclusive) of each group of connected blocks, a stack
|
|
785
|
+
* in the workspace, in text order. Text views mark these lines with classes.
|
|
786
|
+
*/
|
|
787
|
+
groups?: {
|
|
788
|
+
startLine: number;
|
|
789
|
+
endLine: number;
|
|
790
|
+
}[];
|
|
575
791
|
}
|
|
576
792
|
/** Theme configuration for the code editor. */
|
|
577
793
|
export interface MorphicCodeEditorTheme {
|
|
@@ -592,6 +808,60 @@ export interface MorphicCodeEditorTheme {
|
|
|
592
808
|
/** Text selection highlight colour. */
|
|
593
809
|
selectionBackground?: string;
|
|
594
810
|
}
|
|
811
|
+
/** Ways to delete blocks in a codespace, each on unless set to `false`. */
|
|
812
|
+
export interface MorphicCodespaceDeleteOptions {
|
|
813
|
+
/** The ✕ beside each line where a block starts. */
|
|
814
|
+
lineButtons?: boolean;
|
|
815
|
+
/** Delete and Backspace: the block of the cursor's line, or of every selected line. */
|
|
816
|
+
keys?: boolean;
|
|
817
|
+
/** The toolbar button that deletes the blocks of the selected lines. */
|
|
818
|
+
toolbarButton?: boolean;
|
|
819
|
+
}
|
|
820
|
+
/** A view added with `engine.addView()`. */
|
|
821
|
+
export interface MorphicViewOptions {
|
|
822
|
+
kind: MorphicViewKind;
|
|
823
|
+
/** Element the view renders into. */
|
|
824
|
+
container: HTMLElement;
|
|
825
|
+
/** The code element the view shows, e.g. `"python"`. */
|
|
826
|
+
element: string;
|
|
827
|
+
/** Unique among the engine's views. */
|
|
828
|
+
name?: string;
|
|
829
|
+
/**
|
|
830
|
+
* Whether the program can be changed in the view. Default `true`. An added
|
|
831
|
+
* workspace follows every change of the one that holds the program and
|
|
832
|
+
* sends its own edits there; a codespace that cannot be edited shows the
|
|
833
|
+
* program as text, read only.
|
|
834
|
+
*/
|
|
835
|
+
editable?: boolean;
|
|
836
|
+
/** Right to left or left to right; left out, the view follows the program's workspace or the page. */
|
|
837
|
+
rtl?: boolean;
|
|
838
|
+
/** How blocks are deleted in a codespace. */
|
|
839
|
+
delete?: MorphicCodespaceDeleteOptions;
|
|
840
|
+
/** Colours of a text view, like `editorTheme`. */
|
|
841
|
+
theme?: MorphicCodeEditorTheme;
|
|
842
|
+
/** A toolbar for this view, removed with it. Items default to its kind's. */
|
|
843
|
+
toolbar?: {
|
|
844
|
+
container: HTMLElement;
|
|
845
|
+
items?: MorphicToolbarItem[];
|
|
846
|
+
display?: MorphicToolbarDisplay;
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
/** Controls a view added with `engine.addView()`. */
|
|
850
|
+
export interface MorphicViewHandle {
|
|
851
|
+
readonly kind: MorphicViewKind;
|
|
852
|
+
/** The given name, or `view-1`, `view-2`, … when none was given. */
|
|
853
|
+
readonly name: string;
|
|
854
|
+
/** Settles once the view is ready; text views load their editor in the background. */
|
|
855
|
+
readonly ready: Promise<void>;
|
|
856
|
+
/** The code element the view shows. */
|
|
857
|
+
getElement(): string;
|
|
858
|
+
/** Show another code element. */
|
|
859
|
+
show(element: string): void;
|
|
860
|
+
/** Colours of a text view, e.g. when the app switches between light and dark. No effect on a workspace. */
|
|
861
|
+
setTheme(theme: MorphicCodeEditorTheme): void;
|
|
862
|
+
/** Removes the view. `mount()` again removes every added view too. */
|
|
863
|
+
dispose(): void;
|
|
864
|
+
}
|
|
595
865
|
/** Options for `mountCodeEditor()`. */
|
|
596
866
|
export interface MorphicCodeEditorOptions {
|
|
597
867
|
/** Visual theme — the framework provides sensible defaults. */
|
|
@@ -600,10 +870,17 @@ export interface MorphicCodeEditorOptions {
|
|
|
600
870
|
extensions?: unknown[];
|
|
601
871
|
/**
|
|
602
872
|
* Called when the user requests to delete the block at a given 1-based line
|
|
603
|
-
* (via Delete/Backspace
|
|
873
|
+
* (via Delete/Backspace, or a click on the gutter ✕), or the blocks of the
|
|
874
|
+
* lines `line` to `lastLine` when several lines are selected.
|
|
604
875
|
* When set, the editor installs a keymap and a delete gutter.
|
|
605
876
|
*/
|
|
606
|
-
onDelete?: (line: number) => void;
|
|
877
|
+
onDelete?: (line: number, lastLine?: number) => void;
|
|
878
|
+
/** Called when the selected lines change (see `getSelectedLines()`). */
|
|
879
|
+
onLineSelection?: () => void;
|
|
880
|
+
/** With `onDelete`: the ✕ beside each line where a block starts. Default `true`. */
|
|
881
|
+
deleteButtons?: boolean;
|
|
882
|
+
/** With `onDelete`: Delete and Backspace delete. Default `true`. */
|
|
883
|
+
deleteKeys?: boolean;
|
|
607
884
|
/**
|
|
608
885
|
* If provided, a "grip" gutter (⋮⋮) appears on the start line of every block
|
|
609
886
|
* id for which this returns `true`. Mousedown on the grip starts an HTML5
|
|
@@ -633,16 +910,11 @@ export interface MorphicCodeEditorOptions {
|
|
|
633
910
|
}
|
|
634
911
|
/** Options for `enableSelectionSync()`. */
|
|
635
912
|
export interface MorphicSelectionSyncOptions {
|
|
636
|
-
/** CSS background colour for highlighted code lines. Defaults to a semi-transparent blue. */
|
|
637
|
-
highlightColor?: string;
|
|
638
913
|
/** Enable block → code direction. Defaults to true. */
|
|
639
914
|
blockToCode?: boolean;
|
|
640
915
|
/** Enable code → block direction. Defaults to true. */
|
|
641
916
|
codeToBlock?: boolean;
|
|
642
917
|
}
|
|
643
|
-
/** Surface a toolbar binds to. Drives stateful items (undo enabled-state,
|
|
644
|
-
* language label source, what "clear" affects). */
|
|
645
|
-
export type MorphicToolbarPane = "workspace" | "codespace" | "preview";
|
|
646
918
|
/** How items render their built-in icon + label pair. */
|
|
647
919
|
export type MorphicToolbarDisplay = "icon" | "label" | "both";
|
|
648
920
|
/**
|
|
@@ -651,7 +923,12 @@ export type MorphicToolbarDisplay = "icon" | "label" | "both";
|
|
|
651
923
|
*/
|
|
652
924
|
export interface MorphicToolbarCtx {
|
|
653
925
|
engine: import("./MorphicBlocks").MorphicBlocks;
|
|
654
|
-
|
|
926
|
+
/** The kind of view the toolbar acts on. */
|
|
927
|
+
kind: MorphicViewKind;
|
|
928
|
+
/** Whether the view shows the program without changing it. */
|
|
929
|
+
readOnly: boolean;
|
|
930
|
+
/** The name of the view the toolbar acts on; pass it to engine methods such as `zoomPane`. */
|
|
931
|
+
view: string;
|
|
655
932
|
/** Current rendered text for the bound pane. Empty for workspace pane unless
|
|
656
933
|
* the engine has a codespace/preview mounted to derive text from. */
|
|
657
934
|
getText: () => string;
|
|
@@ -686,10 +963,10 @@ export interface MorphicToolbarItem {
|
|
|
686
963
|
}
|
|
687
964
|
/** Configuration for `engine.mountToolbar(container, config)`. */
|
|
688
965
|
export interface MorphicToolbarConfig {
|
|
689
|
-
/**
|
|
690
|
-
|
|
966
|
+
/** The view this toolbar acts on, by name. */
|
|
967
|
+
view: string;
|
|
691
968
|
/** Items to render. When omitted, the framework uses
|
|
692
|
-
* `toolbarItems.defaultsFor(
|
|
969
|
+
* `toolbarItems.defaultsFor(<kind of view>)`. To render no items, pass `[]`. */
|
|
693
970
|
items?: MorphicToolbarItem[];
|
|
694
971
|
/** Display mode for items that have both icon and label. Defaults to "icon". */
|
|
695
972
|
display?: MorphicToolbarDisplay;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { MorphicBehaviorMap, MorphicBlockDefinition,
|
|
1
|
+
import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicCodeElementConfig, MorphicElementTypeEntry, MorphicModeDefinition, MorphicPresetDefinition, MorphicToolboxCategory } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Inputs for a definitions validation pass. Everything is optional except the
|
|
4
4
|
* definitions + elementTypes (available at construction); the cross-reference
|
|
5
|
-
* data (modes /
|
|
5
|
+
* data (modes / code / categories) arrives from the mount config.
|
|
6
6
|
*/
|
|
7
7
|
export interface ValidateDefinitionsArgs {
|
|
8
8
|
definitions: ReadonlyMap<string, MorphicBlockDefinition>;
|
|
@@ -10,8 +10,10 @@ export interface ValidateDefinitionsArgs {
|
|
|
10
10
|
behaviors: MorphicBehaviorMap;
|
|
11
11
|
modes?: MorphicModeDefinition[];
|
|
12
12
|
presets?: MorphicPresetDefinition[];
|
|
13
|
-
|
|
13
|
+
code?: Record<string, MorphicCodeElementConfig>;
|
|
14
14
|
categories?: MorphicToolboxCategory[];
|
|
15
|
+
/** Names of the views the developer declared (not the defaults). */
|
|
16
|
+
views?: string[];
|
|
15
17
|
}
|
|
16
18
|
export interface DefinitionValidationResult {
|
|
17
19
|
/** Structural problems that guarantee wrong output — the caller should throw. */
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { MorphicValueFormat } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* A printed value the way the shown language writes it. Programs run as
|
|
4
|
+
* JavaScript, so without a format a value reads like JavaScript (`true`,
|
|
5
|
+
* `null`, `1,2,3`); a code element's `values` format rewrites the primitive
|
|
6
|
+
* values and lists (`True`, `None`, `[1, 2, 3]`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function formatValue(value: unknown, format: MorphicValueFormat | undefined, nested?: boolean): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type * as Blockly from "blockly";
|
|
2
|
+
import type { MorphicElementTypeEntry, MorphicInputSlotDefinition } from "./types";
|
|
3
|
+
/** Name prefix of the label a `%1:type` token becomes, followed by the slot number. */
|
|
4
|
+
export declare const VALUE_TYPE_FIELD = "MORPHIC_TYPE_";
|
|
5
|
+
/** Whether a field is the label of a `%1:type` token, not one of the block's own fields. */
|
|
6
|
+
export declare const isValueTypeLabel: (field: Blockly.Field) => boolean;
|
|
7
|
+
/**
|
|
8
|
+
* The name `%1:type` writes for the value in `slot`: the element's
|
|
9
|
+
* `code.types` entry for the value's type (its block's output check, or the
|
|
10
|
+
* slot's own check while the slot is empty and accepts one type), else
|
|
11
|
+
* `default`, else nothing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function valueTypeName(block: Blockly.Block, slot: MorphicInputSlotDefinition | undefined, elementEntry: MorphicElementTypeEntry | undefined): string;
|