morphic-blocks 0.0.1 → 0.2.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +74 -6
- package/definitions.schema.json +283 -0
- package/dist/code-editor-YFXMCcWg.js +4 -0
- package/dist/index.d.ts +10 -0
- package/dist/morphic/MorphicBlocks.d.ts +360 -0
- package/dist/morphic/behavior-runtime.d.ts +3 -0
- package/dist/morphic/block-font.d.ts +34 -0
- package/dist/morphic/block-namespace.d.ts +34 -0
- package/dist/morphic/block-view.d.ts +54 -0
- package/dist/morphic/code-editor.d.ts +172 -0
- package/dist/morphic/codegen.d.ts +4 -0
- package/dist/morphic/constants.d.ts +7 -0
- package/dist/morphic/definitions.d.ts +28 -0
- package/dist/morphic/element-types.d.ts +20 -0
- package/dist/morphic/resize.d.ts +35 -0
- package/dist/morphic/selection-sync.d.ts +51 -0
- package/dist/morphic/styles.d.ts +36 -0
- package/dist/morphic/syntax-highlight.d.ts +14 -0
- package/dist/morphic/template-codegen.d.ts +16 -0
- package/dist/morphic/template.d.ts +45 -0
- package/dist/morphic/toolbar.d.ts +48 -0
- package/dist/morphic/toolbox-canvas.d.ts +53 -0
- package/dist/morphic/toolbox.d.ts +3 -0
- package/dist/morphic/types.d.ts +697 -0
- package/dist/morphic/validate-definitions.d.ts +31 -0
- package/dist/morphic/view-resolver.d.ts +17 -0
- package/dist/morphic-blocks.js +4265 -0
- package/dist/morphic-blocks.umd.cjs +30 -0
- package/dist/toolbar-kxlfr8TT.js +4 -0
- package/package.json +58 -5
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import type * as Blockly from "blockly";
|
|
2
|
+
export type MorphicModeName = string;
|
|
3
|
+
/**
|
|
4
|
+
* The rendering type of a named element.
|
|
5
|
+
* - "text" — rendered as an HTML label; never shown in the workspace
|
|
6
|
+
* - "code" — template with %N placeholders; rendered as a Blockly block in the workspace or as text in the codespace
|
|
7
|
+
* - "image" — rendered as an <img> in the toolbox tile; never shown in the workspace
|
|
8
|
+
*/
|
|
9
|
+
export type MorphicElementType = "text" | "code" | "image";
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for a value slot's empty-state default. Keyed by slot
|
|
12
|
+
* `check` in `elementTypes[name].empty[check]`, or set per-slot via
|
|
13
|
+
* `inputSlot.default`. Both `shadow` and `placeholder` are optional; when
|
|
14
|
+
* both are configured, the placeholder is created first and Blockly's
|
|
15
|
+
* native shadow restoration brings the shadow up if the user removes the
|
|
16
|
+
* placeholder.
|
|
17
|
+
*/
|
|
18
|
+
export interface MorphicEmptyDefaultConfig {
|
|
19
|
+
/**
|
|
20
|
+
* Blockly block type used as a *shadow* — ghosted, immutable, auto-replaced
|
|
21
|
+
* when a real block connects, restored when the user disconnects.
|
|
22
|
+
* Standard examples: `"math_number"`, `"text"`, `"logic_boolean"`.
|
|
23
|
+
* The shadow's output type must be compatible with the slot's `check`,
|
|
24
|
+
* otherwise Blockly silently rejects the connection.
|
|
25
|
+
*/
|
|
26
|
+
shadow?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Blockly block type used as a *placeholder* — a real (non-shadow) block
|
|
29
|
+
* attached on render. Movable, editable, deletable. When both `shadow`
|
|
30
|
+
* and `placeholder` are configured, the placeholder takes priority on
|
|
31
|
+
* the visible slot.
|
|
32
|
+
*/
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Initial values applied to the chosen block's internal Blockly fields
|
|
36
|
+
* (e.g. `{ NUM: "0" }` for `math_number`, `{ TEXT: "text" }` for `text`).
|
|
37
|
+
* Plural because a single shadow/placeholder block can carry multiple
|
|
38
|
+
* fields (`{ MIN: "0", MAX: "100", STEP: "1" }` for a range picker).
|
|
39
|
+
*/
|
|
40
|
+
fieldValues?: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Optional per-element configuration. Used in place of a bare type string in
|
|
44
|
+
* `elementTypes` when the developer needs to declare extras (e.g. shadows /
|
|
45
|
+
* placeholders for empty value slots).
|
|
46
|
+
*/
|
|
47
|
+
export interface MorphicElementTypeConfig {
|
|
48
|
+
type: MorphicElementType;
|
|
49
|
+
/**
|
|
50
|
+
* Per-slot-check defaults for empty value inputs. Keys are the slot's
|
|
51
|
+
* `check` string (e.g. `"Number"`, `"String"`, or any developer-defined
|
|
52
|
+
* check name). Slots without a matching key get no default — the codespace
|
|
53
|
+
* renders a `[TYPE]` marker (e.g. `[NUMBER]`) and the workspace shows an empty
|
|
54
|
+
* socket. Per-slot overrides via `inputSlots[i].default` take priority.
|
|
55
|
+
*/
|
|
56
|
+
empty?: Record<string, MorphicEmptyDefaultConfig>;
|
|
57
|
+
/**
|
|
58
|
+
* String delimiter used to wrap framework-supplied literals (shadow values
|
|
59
|
+
* and empty-slot fallbacks) in `String`-checked value slots, so the
|
|
60
|
+
* codespace renders e.g. `print("hello")` rather than `print(hello)`. The
|
|
61
|
+
* quotes are emitted around the value but outside the editable marker, so
|
|
62
|
+
* inline editing targets only the inner text. Only applies to literals the
|
|
63
|
+
* framework supplies; user-attached real blocks render via their own
|
|
64
|
+
* templates (a `var_get` stays bare, a `text_value` quotes itself).
|
|
65
|
+
* Language-specific (`"\""` for most languages); omit to disable quoting.
|
|
66
|
+
*/
|
|
67
|
+
stringQuote?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Display size for `type: "image"` elements. Used when the element value is
|
|
70
|
+
* a file path (e.g. `"assets/icon.svg"`) and the framework auto-wraps it as
|
|
71
|
+
* an `<img>` tag. Accepted formats:
|
|
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.
|
|
77
|
+
*/
|
|
78
|
+
size?: number | string;
|
|
79
|
+
}
|
|
80
|
+
/** Either a bare type or a config object with extras. */
|
|
81
|
+
export type MorphicElementTypeEntry = MorphicElementType | MorphicElementTypeConfig;
|
|
82
|
+
export type MorphicConnectionSpec = boolean | string | string[];
|
|
83
|
+
/**
|
|
84
|
+
* How a statement block connects above and below, in one word:
|
|
85
|
+
* - "statement" connects above and below (`print`, `if`)
|
|
86
|
+
* - "start" connects below only (a "when program starts" block)
|
|
87
|
+
* - "end" connects above only (`return`, `stop`)
|
|
88
|
+
* - "standalone" connects neither way
|
|
89
|
+
*
|
|
90
|
+
* It only fills `previousStatement` / `nextStatement` where the block does not
|
|
91
|
+
* set them itself, so an explicit flag still wins (e.g. to add a connection
|
|
92
|
+
* type). Value blocks use `output` instead and take no shape.
|
|
93
|
+
*/
|
|
94
|
+
export type MorphicBlockShape = "statement" | "start" | "end" | "standalone";
|
|
95
|
+
export type MorphicInputKind = "value" | "statement";
|
|
96
|
+
export type MorphicInputAlign = "left" | "centre" | "right";
|
|
97
|
+
/**
|
|
98
|
+
* One choice in a declared `dropdown` field. The `value` is the source of
|
|
99
|
+
* truth: it is what the block *generates*, serializes, and executes. The
|
|
100
|
+
* optional `label` is a display-only override shown on the workspace block
|
|
101
|
+
* (e.g. show `÷` while generating `/`); when omitted the value is shown.
|
|
102
|
+
*
|
|
103
|
+
* `display` makes the *shown* text mode-aware without touching the value: it
|
|
104
|
+
* maps an **element name** (like `highlighting` is keyed) to the text shown
|
|
105
|
+
* when that element is rendered — so a Python source shows `True` while
|
|
106
|
+
* JavaScript shows `true`, both storing/executing the same value. Resolution
|
|
107
|
+
* when rendering element `E`: block text `display[E] ?? label ?? value`;
|
|
108
|
+
* codespace/preview text `display[E] ?? value` (`label` stays block-only).
|
|
109
|
+
* Only the representation axis — natural-language translation is separate.
|
|
110
|
+
*
|
|
111
|
+
* Forms:
|
|
112
|
+
* - `"=="` → value = label = `"=="`
|
|
113
|
+
* - `["-", "−"]` → value `"-"`, shown as `"−"` on the block
|
|
114
|
+
* - `{ value, label?, display? }` → explicit object form
|
|
115
|
+
*/
|
|
116
|
+
export type MorphicDropdownOption = string | [value: string, label: string] | {
|
|
117
|
+
value: string;
|
|
118
|
+
label?: string;
|
|
119
|
+
display?: Record<string, string>;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* A field declared directly on a block (rendered at the `%FIELDNAME` token
|
|
123
|
+
* position). Lets dropdown / text / number / checkbox fields live in the
|
|
124
|
+
* definitions file instead of requiring a behavior's `onViewApplied`. Fields
|
|
125
|
+
* outside these four (variables, colour, plugin/custom) still use
|
|
126
|
+
* `onViewApplied` — the `%FIELDNAME` token is skipped when no declaration
|
|
127
|
+
* exists, leaving the behavior to supply the field.
|
|
128
|
+
*/
|
|
129
|
+
export type MorphicFieldDefinition = {
|
|
130
|
+
type: "dropdown";
|
|
131
|
+
options: MorphicDropdownOption[];
|
|
132
|
+
default?: string;
|
|
133
|
+
} | {
|
|
134
|
+
type: "text";
|
|
135
|
+
default?: string;
|
|
136
|
+
} | {
|
|
137
|
+
type: "number";
|
|
138
|
+
default?: number;
|
|
139
|
+
min?: number;
|
|
140
|
+
max?: number;
|
|
141
|
+
precision?: number;
|
|
142
|
+
} | {
|
|
143
|
+
type: "checkbox";
|
|
144
|
+
default?: boolean;
|
|
145
|
+
};
|
|
146
|
+
export interface MorphicInputSlotDefinition {
|
|
147
|
+
kind?: MorphicInputKind;
|
|
148
|
+
name?: string;
|
|
149
|
+
check?: string | string[];
|
|
150
|
+
align?: MorphicInputAlign;
|
|
151
|
+
label?: string;
|
|
152
|
+
/**
|
|
153
|
+
* Block-level override for this slot's empty-state default. Highest
|
|
154
|
+
* priority: when set, takes precedence over the elementType-level
|
|
155
|
+
* `empty[check]` lookup. Lets a specific block (e.g. a tutorial's first
|
|
156
|
+
* `print` block) supply richer defaults than its language's generic ones.
|
|
157
|
+
*/
|
|
158
|
+
default?: MorphicEmptyDefaultConfig;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Named visual parts of a Morphic Block.
|
|
162
|
+
* Keys are element names (e.g. "icon", "block", "code", "text") — free-form, not enforced.
|
|
163
|
+
* Values are template strings (same syntax as before: %1 placeholders, <img> tags, plain text).
|
|
164
|
+
* The "block" element is used as the Blockly workspace template.
|
|
165
|
+
*/
|
|
166
|
+
export type MorphicBlockElements = Record<string, string>;
|
|
167
|
+
/**
|
|
168
|
+
* A named per-view mode configuration. Presence of a view key means that view
|
|
169
|
+
* is part of the preset: `toolbox` is required, and at least one editing
|
|
170
|
+
* space (`workspace` or `codespace`) must be set; `preview` is optional.
|
|
171
|
+
*/
|
|
172
|
+
/**
|
|
173
|
+
* A preset's toolbox: either a bare mode name (all code elements render as
|
|
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
|
+
};
|
|
181
|
+
export interface MorphicPresetDefinition {
|
|
182
|
+
name: string;
|
|
183
|
+
/** Display label (e.g. for buttons). Falls back to `name`. */
|
|
184
|
+
label?: string;
|
|
185
|
+
toolbox: MorphicPresetToolbox;
|
|
186
|
+
workspace?: MorphicModeName;
|
|
187
|
+
codespace?: MorphicModeName;
|
|
188
|
+
preview?: MorphicModeName;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Declares which elements are visible for a given mode.
|
|
192
|
+
* CSS (one file per mode) controls how those elements look.
|
|
193
|
+
* A mode's source element (rendered in editing spaces and previews) is the
|
|
194
|
+
* first element in `elements` whose type is "code".
|
|
195
|
+
*/
|
|
196
|
+
export interface MorphicModeDefinition {
|
|
197
|
+
name: string;
|
|
198
|
+
/** Element names that are visible in this mode (e.g. ["icon", "text"]). */
|
|
199
|
+
elements: string[];
|
|
200
|
+
}
|
|
201
|
+
export interface MorphicBlockDefinition {
|
|
202
|
+
identifier: string;
|
|
203
|
+
/**
|
|
204
|
+
* Named visual elements of this Morphic Block, keyed by element name.
|
|
205
|
+
*
|
|
206
|
+
* The reserved key `default` is an optional fallback template: it is used for
|
|
207
|
+
* every `code` element the block does not list itself, so a template shared by
|
|
208
|
+
* all languages can be written once. A listed element always wins.
|
|
209
|
+
*/
|
|
210
|
+
elements: MorphicBlockElements;
|
|
211
|
+
/**
|
|
212
|
+
* Input slot definitions keyed by placeholder index ("1", "2", …).
|
|
213
|
+
* Applies to the workspace ("block") element template.
|
|
214
|
+
*/
|
|
215
|
+
inputSlots?: Record<string, MorphicInputSlotDefinition>;
|
|
216
|
+
/**
|
|
217
|
+
* Inline field definitions keyed by `%FIELDNAME` token name (uppercase, e.g.
|
|
218
|
+
* "OP", "NUM", "TEXT"). Rendered at the token position on the workspace block
|
|
219
|
+
* and read by text/preview views and codegen. Types outside the built-in four
|
|
220
|
+
* (dropdown / text / number / checkbox) are supplied by a behavior's
|
|
221
|
+
* `onViewApplied` instead.
|
|
222
|
+
*/
|
|
223
|
+
fields?: Record<string, MorphicFieldDefinition>;
|
|
224
|
+
color?: number | string;
|
|
225
|
+
output?: MorphicConnectionSpec;
|
|
226
|
+
/**
|
|
227
|
+
* One word for how the block connects above and below. Optional: without it
|
|
228
|
+
* the `previousStatement` / `nextStatement` flags decide, and a block with no
|
|
229
|
+
* flags is standalone. See {@link MorphicBlockShape}.
|
|
230
|
+
*/
|
|
231
|
+
shape?: MorphicBlockShape;
|
|
232
|
+
previousStatement?: MorphicConnectionSpec;
|
|
233
|
+
nextStatement?: MorphicConnectionSpec;
|
|
234
|
+
inputsInline?: boolean;
|
|
235
|
+
tooltip?: string;
|
|
236
|
+
helpUrl?: string;
|
|
237
|
+
/** Category name this block belongs to. */
|
|
238
|
+
category?: string;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Token-level highlighting rules for a code element. Keyed by element name
|
|
242
|
+
* (e.g. "python", "javascript", "concept") so a mode's source element already
|
|
243
|
+
* names the language — no separate `language` field is needed on modes.
|
|
244
|
+
*
|
|
245
|
+
* The framework applies these as `Decoration.mark` ranges via a CodeMirror
|
|
246
|
+
* `ViewPlugin`. Highlighting deliberately does NOT install a CodeMirror
|
|
247
|
+
* language (no bracket matching, no auto-indent, no autocomplete) — Code
|
|
248
|
+
* Blocks treats text as a rendered view, not a freeform editing surface.
|
|
249
|
+
*/
|
|
250
|
+
export interface MorphicHighlightDefinition {
|
|
251
|
+
/** Words to highlight as keywords (exact match against `[A-Za-z_]\w*` tokens). */
|
|
252
|
+
keywords?: string[];
|
|
253
|
+
/** String delimiters (e.g. `["\"", "'"]`). Span until matching close on same line. */
|
|
254
|
+
strings?: string[];
|
|
255
|
+
/** Line-comment marker (e.g. `"#"`, `"//"`). Highlights from the marker to end of line. */
|
|
256
|
+
comment?: string;
|
|
257
|
+
/** Highlight integer/decimal numeric literals. Defaults to true; pass `false` to disable. */
|
|
258
|
+
numbers?: boolean;
|
|
259
|
+
/** Per-token-class color overrides. Each is optional; framework provides sensible defaults. */
|
|
260
|
+
colors?: {
|
|
261
|
+
keyword?: string;
|
|
262
|
+
string?: string;
|
|
263
|
+
number?: string;
|
|
264
|
+
comment?: string;
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** Top-level format for a definitions JSON file. */
|
|
268
|
+
/**
|
|
269
|
+
* A definitions file as TypeScript sees a JSON import: the same structure as
|
|
270
|
+
* `MorphicBlocksFormat`, but with every exact value relaxed to its general
|
|
271
|
+
* kind (`"code"` becomes any string, a `[value, label]` pair any string list),
|
|
272
|
+
* and maps tolerate the `undefined` entries TypeScript adds when a list mixes
|
|
273
|
+
* objects. TypeScript widens JSON this way, so the constructor accepts it
|
|
274
|
+
* directly and `mount()` validates the values at runtime instead.
|
|
275
|
+
*/
|
|
276
|
+
export type MorphicBlocksFormatJson = Widen<MorphicBlocksFormat>;
|
|
277
|
+
type Widen<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends readonly (infer Item)[] ? Widen<Item>[] : T extends object ? string extends keyof T ? {
|
|
278
|
+
[Key in keyof T]: Widen<T[Key]> | undefined;
|
|
279
|
+
} : {
|
|
280
|
+
[Key in keyof T]: Widen<T[Key]>;
|
|
281
|
+
} : T;
|
|
282
|
+
export interface MorphicBlocksFormat {
|
|
283
|
+
/** JSON Schema reference for editor tooling. Ignored by the framework. */
|
|
284
|
+
$schema?: string;
|
|
285
|
+
/** Definitions format version. Current: 1. */
|
|
286
|
+
version?: number;
|
|
287
|
+
/**
|
|
288
|
+
* Global element type registry.
|
|
289
|
+
* Maps each element name to either a bare type ("text" | "code" | "image")
|
|
290
|
+
* or a config object (`{ type, empty? }`). Declared once here; per-block
|
|
291
|
+
* elements remain plain name→content strings.
|
|
292
|
+
*/
|
|
293
|
+
elementTypes?: Record<string, MorphicElementTypeEntry>;
|
|
294
|
+
/** Explicit mode definitions — which elements are visible per mode. */
|
|
295
|
+
modes?: MorphicModeDefinition[];
|
|
296
|
+
/** Named per-view mode configurations. Used as the mount default for `presets`. */
|
|
297
|
+
presets?: MorphicPresetDefinition[];
|
|
298
|
+
/**
|
|
299
|
+
* Per-element highlight rules, keyed by element name. The codespace and
|
|
300
|
+
* preview modes' source elements look up entries here for their editors.
|
|
301
|
+
*/
|
|
302
|
+
highlighting?: Record<string, MorphicHighlightDefinition>;
|
|
303
|
+
/** Optional category metadata. Blocks reference categories by name. */
|
|
304
|
+
categories?: MorphicToolboxCategory[];
|
|
305
|
+
/** Flat array of block definitions. */
|
|
306
|
+
blocks: MorphicBlockDefinition[];
|
|
307
|
+
}
|
|
308
|
+
export type MorphicRenderContext = "workspace" | "toolbox";
|
|
309
|
+
/** One line a program printed during `runJavaScript`, with its console level. */
|
|
310
|
+
export interface MorphicRunOutputLine {
|
|
311
|
+
level: "log" | "warn" | "error";
|
|
312
|
+
/** The printed arguments, converted to text and joined with spaces. */
|
|
313
|
+
text: string;
|
|
314
|
+
}
|
|
315
|
+
/** What `runJavaScript` returns (and the `morphic-run` event carries). */
|
|
316
|
+
export interface MorphicRunResult {
|
|
317
|
+
/** The JavaScript that was generated and run. */
|
|
318
|
+
code: string;
|
|
319
|
+
/** The program's return value. */
|
|
320
|
+
result: unknown;
|
|
321
|
+
/** The error it threw, or `null`. */
|
|
322
|
+
error: Error | null;
|
|
323
|
+
/** Every line it printed, in order, with its level. */
|
|
324
|
+
output: MorphicRunOutputLine[];
|
|
325
|
+
}
|
|
326
|
+
export interface MorphicBehaviorContext {
|
|
327
|
+
Blockly: typeof Blockly;
|
|
328
|
+
workspace: Blockly.WorkspaceSvg;
|
|
329
|
+
mode: MorphicModeName;
|
|
330
|
+
context: MorphicRenderContext;
|
|
331
|
+
definition: MorphicBlockDefinition;
|
|
332
|
+
}
|
|
333
|
+
export interface MorphicBehaviorProxy {
|
|
334
|
+
blockId: string;
|
|
335
|
+
blockType: string;
|
|
336
|
+
mode: MorphicModeName;
|
|
337
|
+
context: MorphicRenderContext;
|
|
338
|
+
inputs: Record<string, string>;
|
|
339
|
+
/**
|
|
340
|
+
* Plain field values, keyed by field name, exactly as the field holds them:
|
|
341
|
+
* a text field holding `x` gives `x`, a number field `42` gives `42`. Use
|
|
342
|
+
* these for identifiers and operators (`let ${proxy.fields.VAR} = …`).
|
|
343
|
+
*/
|
|
344
|
+
fields: Record<string, string>;
|
|
345
|
+
/**
|
|
346
|
+
* The same values as quoted, escaped string literals. Always quoted, even
|
|
347
|
+
* when the value looks like a number: `x` gives `"x"`, `42` gives `"42"`.
|
|
348
|
+
* Use these where the value is emitted as a string (e.g. a text block).
|
|
349
|
+
*/
|
|
350
|
+
quoted: Record<string, string>;
|
|
351
|
+
}
|
|
352
|
+
export type MorphicCodeBehavior = (proxy: MorphicBehaviorProxy) => string;
|
|
353
|
+
export interface MorphicBlockBehavior {
|
|
354
|
+
init?: (block: Blockly.BlockSvg, context: MorphicBehaviorContext) => void;
|
|
355
|
+
onViewApplied?: (block: Blockly.BlockSvg, context: MorphicBehaviorContext) => void;
|
|
356
|
+
generate?: MorphicCodeBehavior;
|
|
357
|
+
}
|
|
358
|
+
export type MorphicBehaviorDefinition = MorphicBlockBehavior | MorphicCodeBehavior;
|
|
359
|
+
export type MorphicBehaviorMap = Record<string, MorphicBehaviorDefinition>;
|
|
360
|
+
export interface MorphicStyleBundle {
|
|
361
|
+
href?: string;
|
|
362
|
+
cssText?: string;
|
|
363
|
+
}
|
|
364
|
+
export interface MorphicToolboxCategory {
|
|
365
|
+
name: string;
|
|
366
|
+
color?: string;
|
|
367
|
+
/** Explicit block list. If omitted the framework derives it from block definitions whose `category` matches this name. */
|
|
368
|
+
blocks?: string[];
|
|
369
|
+
}
|
|
370
|
+
export type MorphicToolboxLayout = "flyout" | "category";
|
|
371
|
+
export interface MorphicToolboxConfig {
|
|
372
|
+
kind?: "flyoutToolbox" | "categoryToolbox";
|
|
373
|
+
blocks?: string[];
|
|
374
|
+
categories?: MorphicToolboxCategory[];
|
|
375
|
+
}
|
|
376
|
+
/** Options for the custom HTML toolbox canvas (mountToolbox). */
|
|
377
|
+
export interface MorphicToolboxCanvasOptions {
|
|
378
|
+
/** Render a "Mode: <name>" header at the top of the toolbox. Defaults to true. */
|
|
379
|
+
modeLabel?: boolean;
|
|
380
|
+
/** Show only a subset of blocks. Defaults to all blocks in definitions. */
|
|
381
|
+
blocks?: string[];
|
|
382
|
+
/**
|
|
383
|
+
* Category grouping for the toolbox canvas.
|
|
384
|
+
* If omitted, falls back to categories from mount config.
|
|
385
|
+
* If neither is provided, blocks render as a flat list.
|
|
386
|
+
*/
|
|
387
|
+
categories?: MorphicToolboxCategory[];
|
|
388
|
+
}
|
|
389
|
+
export interface MorphicJavaScriptConfig {
|
|
390
|
+
statementPrefix?: string | null;
|
|
391
|
+
statementSuffix?: string | null;
|
|
392
|
+
infiniteLoopTrap?: string | null;
|
|
393
|
+
reservedWords?: string;
|
|
394
|
+
}
|
|
395
|
+
export interface MorphicMountConfig {
|
|
396
|
+
/**
|
|
397
|
+
* Container for the Blockly workspace. Optional — may be omitted when only a
|
|
398
|
+
* codespace is used. At least one of `workspaceContainer` or `codespaceContainer`
|
|
399
|
+
* must be provided. When omitted, Blockly runs headless (offscreen) so the block
|
|
400
|
+
* model stays available.
|
|
401
|
+
*/
|
|
402
|
+
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
|
+
*/
|
|
407
|
+
codespaceContainer?: HTMLElement;
|
|
408
|
+
/**
|
|
409
|
+
* Container for the custom HTML toolbox. `mount()` sets the toolbox up in it,
|
|
410
|
+
* which implies `canvasToolbox`.
|
|
411
|
+
*/
|
|
412
|
+
toolboxContainer?: HTMLElement;
|
|
413
|
+
/** Container for the read-only preview. `mount()` sets the preview up in it. */
|
|
414
|
+
previewContainer?: HTMLElement;
|
|
415
|
+
/**
|
|
416
|
+
* Container for the generated JavaScript view, a developer tool. `mount()`
|
|
417
|
+
* sets it up hidden; `showCodeEditor()` reveals it.
|
|
418
|
+
*/
|
|
419
|
+
codeEditorContainer?: HTMLElement;
|
|
420
|
+
/** Toolbar containers per pane, set up once the text editors are ready. */
|
|
421
|
+
toolbarContainers?: Partial<Record<MorphicToolbarPane, HTMLElement>>;
|
|
422
|
+
/** Theme for the codespace and code editor set up by `mount()`. */
|
|
423
|
+
editorTheme?: MorphicCodeEditorTheme;
|
|
424
|
+
/** Theme for the preview set up by `mount()`. Defaults to `editorTheme`. */
|
|
425
|
+
previewTheme?: MorphicCodeEditorTheme;
|
|
426
|
+
/**
|
|
427
|
+
* Link block and code selection across views. On by default when `mount()`
|
|
428
|
+
* sets up more than one view; `false` turns it off, an object configures it.
|
|
429
|
+
*/
|
|
430
|
+
selectionSync?: boolean | MorphicSelectionSyncOptions;
|
|
431
|
+
/** Mode definitions — drives automatic element visibility CSS. */
|
|
432
|
+
modes?: MorphicModeDefinition[];
|
|
433
|
+
/**
|
|
434
|
+
* Named per-view mode configurations. Validated at mount. When provided,
|
|
435
|
+
* the initial modes are derived from `preset` (or the first preset).
|
|
436
|
+
*/
|
|
437
|
+
presets?: MorphicPresetDefinition[];
|
|
438
|
+
/** Name of the preset applied at mount. Defaults to the first preset. */
|
|
439
|
+
preset?: string;
|
|
440
|
+
/**
|
|
441
|
+
* Called after a preset is applied — at mount and on every `applyPreset` —
|
|
442
|
+
* so the host can lay out pane visibility (a view is shown iff its key is
|
|
443
|
+
* present on the preset).
|
|
444
|
+
*/
|
|
445
|
+
onPresetApplied?: (preset: MorphicPresetDefinition) => void;
|
|
446
|
+
toolbox?: MorphicToolboxConfig;
|
|
447
|
+
toolboxLayout?: MorphicToolboxLayout;
|
|
448
|
+
/**
|
|
449
|
+
* When true, Blockly is injected without a built-in toolbox.
|
|
450
|
+
* Use this when calling `mountToolbox()` to avoid Blockly toolbox type conflicts.
|
|
451
|
+
*/
|
|
452
|
+
canvasToolbox?: boolean;
|
|
453
|
+
ui?: {
|
|
454
|
+
workspaceClassName?: string | string[];
|
|
455
|
+
toolboxClassName?: string | string[];
|
|
456
|
+
};
|
|
457
|
+
/**
|
|
458
|
+
* One stylesheet per mode, given by hand: mode name to CSS text or a link to
|
|
459
|
+
* a CSS file, e.g. `{ py: pyCss, js: "/modes/js.css" }`. CSS always contains
|
|
460
|
+
* a `{` and a link never does, so each value says which it is.
|
|
461
|
+
*/
|
|
462
|
+
modeStyles?: Record<MorphicModeName, string>;
|
|
463
|
+
/**
|
|
464
|
+
* A whole folder of mode stylesheets, e.g. the result of
|
|
465
|
+
* `import.meta.glob('./modes/*.css', { eager: true, query: '?url' })`.
|
|
466
|
+
* Each file styles the mode it is named after; values may be links (`?url`)
|
|
467
|
+
* or CSS text (`?raw`).
|
|
468
|
+
* The framework derives mode names from CSS filenames and loads the stylesheets automatically.
|
|
469
|
+
* Takes precedence over `modeStyles` for the same modes.
|
|
470
|
+
*/
|
|
471
|
+
modesFolder?: Record<string, unknown>;
|
|
472
|
+
baseStyle?: MorphicStyleBundle;
|
|
473
|
+
/** Options passed through to Blockly's `inject` (zoom, grid, trashcan, and so on). */
|
|
474
|
+
blockly?: Blockly.BlocklyOptions;
|
|
475
|
+
javascript?: MorphicJavaScriptConfig;
|
|
476
|
+
/**
|
|
477
|
+
* Per-element highlight rules, keyed by element name. The codespace and
|
|
478
|
+
* preview modes' source elements are used to look up entries here for
|
|
479
|
+
* their editors. Optional — when absent, editors render plain text.
|
|
480
|
+
*/
|
|
481
|
+
highlighting?: Record<string, MorphicHighlightDefinition>;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Resolved workspace template for a block in a given mode.
|
|
485
|
+
* The template comes from the block's "block" element (or first element as fallback).
|
|
486
|
+
* Used internally by block-view and view-resolver.
|
|
487
|
+
*/
|
|
488
|
+
export interface MorphicResolvedView {
|
|
489
|
+
mode: MorphicModeName;
|
|
490
|
+
template: string;
|
|
491
|
+
/** Element name the template came from. Used to look up per-element config (e.g. `empty` defaults). */
|
|
492
|
+
elementName?: string;
|
|
493
|
+
inputSlots?: Record<string, MorphicInputSlotDefinition>;
|
|
494
|
+
}
|
|
495
|
+
/** Line range a single block occupies in the generated code (1-based, inclusive). */
|
|
496
|
+
export interface MorphicCodeBlockPosition {
|
|
497
|
+
startLine: number;
|
|
498
|
+
endLine: number;
|
|
499
|
+
/**
|
|
500
|
+
* Character-offset range the block's rendered text occupies in the
|
|
501
|
+
* generated code (0-based, half-open: `[startChar, endChar)`). Provides
|
|
502
|
+
* inline precision so value children sharing a line (e.g. `1 + 2`) can
|
|
503
|
+
* still be distinguished. Used by the codespace to resolve value-slot drops.
|
|
504
|
+
*/
|
|
505
|
+
startChar?: number;
|
|
506
|
+
endChar?: number;
|
|
507
|
+
/**
|
|
508
|
+
* True when the block's whole rendered text is a single editable cell
|
|
509
|
+
* (atomic single-field block — `math_number`, `text`, `logic_boolean`, etc.).
|
|
510
|
+
* Used by the codespace's hover background so it can prefer the surrounding
|
|
511
|
+
* non-atomic wrapper (e.g. the whole `1 + 2` expression) instead of
|
|
512
|
+
* highlighting just the atomic value the cursor lands on.
|
|
513
|
+
*/
|
|
514
|
+
atomic?: boolean;
|
|
515
|
+
/**
|
|
516
|
+
* Body line range of each statement input declared on this block, keyed by
|
|
517
|
+
* the input name. Includes empty bodies — the start/end line points to the
|
|
518
|
+
* indented body line where children would render. Used by the codespace to
|
|
519
|
+
* resolve drops into empty `for`/`if` bodies.
|
|
520
|
+
*/
|
|
521
|
+
statementSlots?: Record<string, {
|
|
522
|
+
startLine: number;
|
|
523
|
+
endLine: number;
|
|
524
|
+
}>;
|
|
525
|
+
}
|
|
526
|
+
/** Maps Blockly block IDs to their positions in the generated code. */
|
|
527
|
+
export type MorphicCodeMetadata = Map<string, MorphicCodeBlockPosition>;
|
|
528
|
+
/**
|
|
529
|
+
* Edit target for a placeholder range, when its content is an atomic
|
|
530
|
+
* single-field block (shadow, placeholder, or user-attached value block with
|
|
531
|
+
* exactly one field and no nested children). Empty-fallback ranges and
|
|
532
|
+
* multi-field blocks have no `edit` and are not inline-editable.
|
|
533
|
+
*/
|
|
534
|
+
export interface MorphicPlaceholderEditTarget {
|
|
535
|
+
/** Blockly block id whose field produced the rendered text. */
|
|
536
|
+
blockId: string;
|
|
537
|
+
/** Field name on that block. */
|
|
538
|
+
fieldName: string;
|
|
539
|
+
/** Input UI form to use for editing. */
|
|
540
|
+
fieldType: "text" | "number" | "dropdown";
|
|
541
|
+
/** Dropdown options as `[label, value]` pairs (only when `fieldType === "dropdown"`). */
|
|
542
|
+
options?: [string, string][];
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Range in the generated code occupied by a value slot. The codespace overlays
|
|
546
|
+
* an always-on underline on every value position; `kind: "default"` adds dim
|
|
547
|
+
* italic styling for shadow targets and empty fallbacks (framework-supplied
|
|
548
|
+
* defaults), while `kind: "set"` marks user-attached or placeholder content.
|
|
549
|
+
*/
|
|
550
|
+
export interface MorphicPlaceholderRange {
|
|
551
|
+
/** Inclusive 0-based character offset in `code`. */
|
|
552
|
+
start: number;
|
|
553
|
+
/** Exclusive 0-based character offset in `code`. */
|
|
554
|
+
end: number;
|
|
555
|
+
kind: "default" | "set";
|
|
556
|
+
/** Editing target when the slot contains an atomic single-field block. */
|
|
557
|
+
edit?: MorphicPlaceholderEditTarget;
|
|
558
|
+
/**
|
|
559
|
+
* Set when the range is a *truly empty* value slot (rendered as a `[TYPE]`
|
|
560
|
+
* marker, with no block in it). Identifies the parent block and input so the
|
|
561
|
+
* codespace can resolve a drop into the empty slot — there's no child block
|
|
562
|
+
* to walk up from, unlike an occupied or shadow-filled slot.
|
|
563
|
+
*/
|
|
564
|
+
emptySlot?: {
|
|
565
|
+
parentBlockId: string;
|
|
566
|
+
inputName: string;
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
/** Result of `generateJavaScriptWithMetadata()`. */
|
|
570
|
+
export interface MorphicCodeGenerationResult {
|
|
571
|
+
code: string;
|
|
572
|
+
metadata: MorphicCodeMetadata;
|
|
573
|
+
/** Value-slot ranges with default/set classification. Empty if no value slots emitted. */
|
|
574
|
+
placeholders: MorphicPlaceholderRange[];
|
|
575
|
+
}
|
|
576
|
+
/** Theme configuration for the code editor. */
|
|
577
|
+
export interface MorphicCodeEditorTheme {
|
|
578
|
+
/** Editor text size (e.g. "14px"). */
|
|
579
|
+
fontSize?: string;
|
|
580
|
+
/** Editor font family (e.g. "monospace"). */
|
|
581
|
+
fontFamily?: string;
|
|
582
|
+
/** Line spacing multiplier (e.g. 1.5). */
|
|
583
|
+
lineHeight?: number;
|
|
584
|
+
/** Editor background colour. */
|
|
585
|
+
background?: string;
|
|
586
|
+
/** Default text colour. */
|
|
587
|
+
foreground?: string;
|
|
588
|
+
/** Line number column background colour. */
|
|
589
|
+
gutterBackground?: string;
|
|
590
|
+
/** Line number text colour. */
|
|
591
|
+
gutterForeground?: string;
|
|
592
|
+
/** Text selection highlight colour. */
|
|
593
|
+
selectionBackground?: string;
|
|
594
|
+
}
|
|
595
|
+
/** Options for `mountCodeEditor()`. */
|
|
596
|
+
export interface MorphicCodeEditorOptions {
|
|
597
|
+
/** Visual theme — the framework provides sensible defaults. */
|
|
598
|
+
theme?: MorphicCodeEditorTheme;
|
|
599
|
+
/** Raw CodeMirror extensions for power users. Appended after the built-in ones. */
|
|
600
|
+
extensions?: unknown[];
|
|
601
|
+
/**
|
|
602
|
+
* Called when the user requests to delete the block at a given 1-based line
|
|
603
|
+
* (via Delete/Backspace on an empty selection, or a click on the gutter ✕).
|
|
604
|
+
* When set, the editor installs a keymap and a delete gutter.
|
|
605
|
+
*/
|
|
606
|
+
onDelete?: (line: number) => void;
|
|
607
|
+
/**
|
|
608
|
+
* If provided, a "grip" gutter (⋮⋮) appears on the start line of every block
|
|
609
|
+
* id for which this returns `true`. Mousedown on the grip starts an HTML5
|
|
610
|
+
* drag whose data transfer includes `morphic/block-id` set to the block id —
|
|
611
|
+
* the host can then handle the drop (e.g. reorder).
|
|
612
|
+
*/
|
|
613
|
+
canDragBlock?: (blockId: string) => boolean;
|
|
614
|
+
/**
|
|
615
|
+
* Token-level syntax highlighting rules. When set, the editor installs a
|
|
616
|
+
* CodeMirror `ViewPlugin` that emits `Decoration.mark` ranges for keywords,
|
|
617
|
+
* strings, numbers, and line comments per these rules. Swappable at runtime
|
|
618
|
+
* via `setHighlightRules`.
|
|
619
|
+
*/
|
|
620
|
+
highlightRules?: MorphicHighlightDefinition;
|
|
621
|
+
/**
|
|
622
|
+
* Called when the user commits an inline placeholder edit. The host should
|
|
623
|
+
* write `newValue` to `edit.fieldName` on the Blockly block identified by
|
|
624
|
+
* `edit.blockId`. Re-codegen + re-render then refresh the codespace.
|
|
625
|
+
*/
|
|
626
|
+
onPlaceholderApply?: (edit: MorphicPlaceholderEditTarget, newValue: string) => void;
|
|
627
|
+
/**
|
|
628
|
+
* Whether value-slot underline markers are shown in this editor. The codespace
|
|
629
|
+
* uses them as a form-field affordance for inline editing; read-only display
|
|
630
|
+
* surfaces (e.g. the preview editor) should disable them. Default: `true`.
|
|
631
|
+
*/
|
|
632
|
+
showPlaceholderMarkers?: boolean;
|
|
633
|
+
}
|
|
634
|
+
/** Options for `enableSelectionSync()`. */
|
|
635
|
+
export interface MorphicSelectionSyncOptions {
|
|
636
|
+
/** CSS background colour for highlighted code lines. Defaults to a semi-transparent blue. */
|
|
637
|
+
highlightColor?: string;
|
|
638
|
+
/** Enable block → code direction. Defaults to true. */
|
|
639
|
+
blockToCode?: boolean;
|
|
640
|
+
/** Enable code → block direction. Defaults to true. */
|
|
641
|
+
codeToBlock?: boolean;
|
|
642
|
+
}
|
|
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
|
+
/** How items render their built-in icon + label pair. */
|
|
647
|
+
export type MorphicToolbarDisplay = "icon" | "label" | "both";
|
|
648
|
+
/**
|
|
649
|
+
* Runtime context passed to every toolbar item callback. Items use this to
|
|
650
|
+
* read pane state and request a re-render.
|
|
651
|
+
*/
|
|
652
|
+
export interface MorphicToolbarCtx {
|
|
653
|
+
engine: import("./MorphicBlocks").MorphicBlocks;
|
|
654
|
+
pane: MorphicToolbarPane;
|
|
655
|
+
/** Current rendered text for the bound pane. Empty for workspace pane unless
|
|
656
|
+
* the engine has a codespace/preview mounted to derive text from. */
|
|
657
|
+
getText: () => string;
|
|
658
|
+
/** Triggers the bound pane's re-render (codegen + redraw). */
|
|
659
|
+
refresh: () => void;
|
|
660
|
+
}
|
|
661
|
+
/** Output of an item's render — string is treated as text content. */
|
|
662
|
+
export type MorphicToolbarRender = HTMLElement | string;
|
|
663
|
+
/**
|
|
664
|
+
* One toolbar entry. Buttons supply `label` and/or `icon` plus `onClick`;
|
|
665
|
+
* custom widgets supply `render`. `visible` is evaluated on every refresh.
|
|
666
|
+
*/
|
|
667
|
+
export interface MorphicToolbarItem {
|
|
668
|
+
/** Stable id; reflected as `data-toolbar-id` for CSS targeting. */
|
|
669
|
+
id: string;
|
|
670
|
+
/** Lays out left of the spacer or right of it. Defaults to "left". */
|
|
671
|
+
align?: "left" | "right";
|
|
672
|
+
/** Plain-text label used in `label` / `both` display modes. */
|
|
673
|
+
label?: string;
|
|
674
|
+
/** Inline SVG string used in `icon` / `both` display modes. */
|
|
675
|
+
icon?: string;
|
|
676
|
+
/** Native `title` attribute / tooltip. */
|
|
677
|
+
title?: string;
|
|
678
|
+
/** Button click handler. Omit for non-interactive items (badges, labels). */
|
|
679
|
+
onClick?: (ctx: MorphicToolbarCtx) => void;
|
|
680
|
+
/** Optional predicate; item is omitted when this returns false on refresh. */
|
|
681
|
+
visible?: (ctx: MorphicToolbarCtx) => boolean;
|
|
682
|
+
/** Disable predicate; rendered with `disabled` and reduced opacity. */
|
|
683
|
+
disabled?: (ctx: MorphicToolbarCtx) => boolean;
|
|
684
|
+
/** Escape hatch — fully custom DOM. When set, label/icon/onClick are ignored. */
|
|
685
|
+
render?: (ctx: MorphicToolbarCtx) => MorphicToolbarRender;
|
|
686
|
+
}
|
|
687
|
+
/** Configuration for `engine.mountToolbar(container, config)`. */
|
|
688
|
+
export interface MorphicToolbarConfig {
|
|
689
|
+
/** Which pane this toolbar reflects. */
|
|
690
|
+
pane: MorphicToolbarPane;
|
|
691
|
+
/** Items to render. When omitted, the framework uses
|
|
692
|
+
* `toolbarItems.defaultsFor(pane)`. To render no items, pass `[]`. */
|
|
693
|
+
items?: MorphicToolbarItem[];
|
|
694
|
+
/** Display mode for items that have both icon and label. Defaults to "icon". */
|
|
695
|
+
display?: MorphicToolbarDisplay;
|
|
696
|
+
}
|
|
697
|
+
export {};
|