morphic-blocks 0.1.0 → 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/README.md +12 -16
- package/definitions.schema.json +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/morphic/MorphicBlocks.d.ts +60 -8
- package/dist/morphic/block-font.d.ts +34 -0
- package/dist/morphic/block-view.d.ts +9 -0
- package/dist/morphic/code-editor.d.ts +1 -0
- package/dist/morphic/codegen.d.ts +1 -1
- package/dist/morphic/definitions.d.ts +27 -1
- package/dist/morphic/styles.d.ts +16 -2
- package/dist/morphic/toolbar.d.ts +3 -6
- package/dist/morphic/toolbox-canvas.d.ts +10 -0
- package/dist/morphic/types.d.ts +102 -27
- package/dist/morphic-blocks.js +1753 -1461
- package/dist/morphic-blocks.umd.cjs +25 -22
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -23,11 +23,12 @@ block-based and text-based programming.
|
|
|
23
23
|
## Install
|
|
24
24
|
|
|
25
25
|
```sh
|
|
26
|
-
npm i morphic-blocks
|
|
26
|
+
npm i morphic-blocks
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
additionally use CodeMirror — install those only if
|
|
29
|
+
Blockly is a dependency, so it is installed for you. The code editor,
|
|
30
|
+
codespace, and preview views additionally use CodeMirror — install those only if
|
|
31
|
+
you need them:
|
|
31
32
|
|
|
32
33
|
```sh
|
|
33
34
|
npm i @codemirror/state @codemirror/view @codemirror/lang-javascript
|
|
@@ -40,24 +41,19 @@ import { MorphicBlocks } from "morphic-blocks";
|
|
|
40
41
|
import definitions from "./definitions.json";
|
|
41
42
|
import { behaviors } from "./behaviors";
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
definitions.elementTypes,
|
|
47
|
-
);
|
|
44
|
+
// Modes, presets and categories all come from the definitions file, passed as
|
|
45
|
+
// imported; mount() validates it.
|
|
46
|
+
const engine = new MorphicBlocks(definitions, behaviors);
|
|
48
47
|
|
|
48
|
+
// One call sets up every view it gets a container for.
|
|
49
49
|
engine.mount({
|
|
50
50
|
workspaceContainer: document.getElementById("workspace")!,
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
toolboxContainer: document.getElementById("toolbox")!,
|
|
52
|
+
preset: "conceptual", // a preset from definitions.json; without presets the first mode is used
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// switch representation at runtime — the same blocks re-render
|
|
60
|
-
engine.setModes({ workspaceMode: "python" });
|
|
55
|
+
// switch representation at runtime; the same blocks re-render
|
|
56
|
+
engine.applyPreset("python");
|
|
61
57
|
```
|
|
62
58
|
|
|
63
59
|
## Features
|
package/definitions.schema.json
CHANGED
|
@@ -249,7 +249,12 @@
|
|
|
249
249
|
"properties": {
|
|
250
250
|
"identifier": { "type": "string", "minLength": 1 },
|
|
251
251
|
"category": { "type": "string" },
|
|
252
|
+
"shape": {
|
|
253
|
+
"description": "How a statement block connects: statement (above and below), start (below only), end (above only), standalone (neither). Fills previousStatement / nextStatement where unset; explicit flags win. Value blocks use output instead.",
|
|
254
|
+
"enum": ["statement", "start", "end", "standalone"]
|
|
255
|
+
},
|
|
252
256
|
"elements": {
|
|
257
|
+
"description": "Element name to content. The reserved key \"default\" is an optional fallback template, used for every code element the block does not list; a listed element always wins.",
|
|
253
258
|
"type": "object",
|
|
254
259
|
"minProperties": 1,
|
|
255
260
|
"additionalProperties": { "type": "string" }
|
package/dist/index.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export { makeResizable } from "./morphic/resize";
|
|
|
7
7
|
export { validateDefinitions } from "./morphic/validate-definitions";
|
|
8
8
|
export type { ValidateDefinitionsArgs, DefinitionValidationResult, } from "./morphic/validate-definitions";
|
|
9
9
|
export type { MorphicResizeOptions, MorphicResizeHandle, } from "./morphic/resize";
|
|
10
|
-
export type { MorphicElementType, MorphicElementTypeConfig, MorphicElementTypeEntry, MorphicEmptyDefaultConfig, MorphicHighlightDefinition, MorphicJavaScriptConfig, MorphicBehaviorContext, MorphicBehaviorDefinition, MorphicBehaviorMap, MorphicBehaviorProxy, MorphicBlockBehavior, MorphicBlockDefinition, MorphicBlocksFormat, MorphicCodeBehavior, MorphicCodeBlockPosition, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicCodeMetadata, MorphicConnectionSpec, MorphicDropdownOption, MorphicFieldDefinition, MorphicInputAlign, MorphicInputKind, MorphicInputSlotDefinition, MorphicModeName,
|
|
10
|
+
export type { MorphicElementType, MorphicElementTypeConfig, MorphicElementTypeEntry, MorphicEmptyDefaultConfig, MorphicHighlightDefinition, MorphicJavaScriptConfig, MorphicBehaviorContext, MorphicBehaviorDefinition, MorphicBehaviorMap, MorphicBehaviorProxy, MorphicBlockBehavior, MorphicBlockDefinition, MorphicBlockShape, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeBehavior, MorphicCodeBlockPosition, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicCodeMetadata, MorphicConnectionSpec, MorphicDropdownOption, MorphicFieldDefinition, MorphicInputAlign, MorphicInputKind, MorphicInputSlotDefinition, MorphicModeName, MorphicMountConfig, MorphicPlaceholderEditTarget, MorphicPlaceholderRange, MorphicRenderContext, MorphicResolvedView, MorphicStyleBundle, MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarDisplay, MorphicToolbarItem, MorphicToolbarPane, MorphicToolbarRender, MorphicToolboxCanvasOptions, MorphicToolboxCategory, MorphicToolboxConfig, MorphicToolboxLayout, MorphicBlockElements, MorphicModeDefinition, MorphicPresetDefinition, MorphicRunOutputLine, MorphicRunResult, MorphicPresetToolbox, MorphicSelectionSyncOptions, } from "./morphic/types";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as Blockly from "blockly";
|
|
2
2
|
import { type MorphicToolbarHandle } from "./toolbar";
|
|
3
|
-
import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicPresetDefinition, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions } from "./types";
|
|
3
|
+
import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions } from "./types";
|
|
4
4
|
export declare class MorphicBlocks extends EventTarget {
|
|
5
5
|
private readonly definitions;
|
|
6
6
|
private readonly behaviors;
|
|
@@ -29,6 +29,17 @@ export declare class MorphicBlocks extends EventTarget {
|
|
|
29
29
|
private headlessWorkspaceHost?;
|
|
30
30
|
/** Teardown fn for codespace drag/drop listeners; set when codespace is mounted. */
|
|
31
31
|
private codespaceDropTeardown?;
|
|
32
|
+
/** Redraws the workspace whenever its container changes size. */
|
|
33
|
+
private workspaceResizeObserver?;
|
|
34
|
+
/** Preset most recently applied, at mount or via `applyPreset`. */
|
|
35
|
+
private activePreset?;
|
|
36
|
+
/** The workspace's theme and font before any mode's CSS font was applied. */
|
|
37
|
+
private baseTheme?;
|
|
38
|
+
private baseFont?;
|
|
39
|
+
/** Counts mounts, so async view setup can tell it was superseded. */
|
|
40
|
+
private mountGeneration;
|
|
41
|
+
/** Options of the active selection sync, reused when an editor is replaced. */
|
|
42
|
+
private selectionSyncOptions?;
|
|
32
43
|
/** Format-level fields remembered from the constructor and used as `mount()`
|
|
33
44
|
* defaults, so the developer hands the whole definitions file in once and the
|
|
34
45
|
* runtime `mount()` call only carries DOM / runtime concerns. */
|
|
@@ -42,10 +53,29 @@ export declare class MorphicBlocks extends EventTarget {
|
|
|
42
53
|
* engine; `modes` / `presets` / `highlighting` / `categories` are remembered
|
|
43
54
|
* as defaults for `mount()` / `mountToolbox()` (a call may still override them).
|
|
44
55
|
*/
|
|
45
|
-
constructor(
|
|
46
|
-
|
|
56
|
+
constructor(formatInput: MorphicBlocksFormat | MorphicBlocksFormatJson, behaviors?: MorphicBehaviorMap);
|
|
57
|
+
/**
|
|
58
|
+
* Set the editor up. Everything given a container is mounted: workspace,
|
|
59
|
+
* toolbox, codespace, preview, code editor and toolbars. The workspace is
|
|
60
|
+
* ready as soon as this returns; the returned promise settles once the text
|
|
61
|
+
* editors, which load in the background, are ready too.
|
|
62
|
+
*/
|
|
63
|
+
mount(inputConfig: MorphicMountConfig): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Set up the text editors and what depends on them (toolbars, selection
|
|
66
|
+
* sync). Runs after the synchronous part of `mount()`; bails out quietly
|
|
67
|
+
* when the engine was disposed or mounted again meanwhile.
|
|
68
|
+
*/
|
|
69
|
+
private mountViews;
|
|
47
70
|
/** Presets declared at mount (empty when none were provided). */
|
|
48
71
|
getPresets(): MorphicPresetDefinition[];
|
|
72
|
+
/**
|
|
73
|
+
* The preset most recently applied, at mount or via `applyPreset`, so a host
|
|
74
|
+
* can mark the active preset button without tracking it itself. `undefined`
|
|
75
|
+
* when no preset has been applied. Lower level `setModes` calls do not
|
|
76
|
+
* change it.
|
|
77
|
+
*/
|
|
78
|
+
getActivePreset(): MorphicPresetDefinition | undefined;
|
|
49
79
|
/**
|
|
50
80
|
* Apply a preset by name or index: sets the per-view modes derived from the
|
|
51
81
|
* preset and notifies `onPresetApplied` so the host can update pane
|
|
@@ -62,6 +92,11 @@ export declare class MorphicBlocks extends EventTarget {
|
|
|
62
92
|
private validatePresets;
|
|
63
93
|
dispose(): void;
|
|
64
94
|
mountToolbox(container: HTMLElement, options?: MorphicToolboxCanvasOptions): void;
|
|
95
|
+
/**
|
|
96
|
+
* The underlying Blockly workspace, for the rare case that needs Blockly
|
|
97
|
+
* directly (e.g. creating blocks from code). Everyday use goes through the
|
|
98
|
+
* engine. `undefined` before `mount`.
|
|
99
|
+
*/
|
|
65
100
|
getWorkspace(): Blockly.WorkspaceSvg | undefined;
|
|
66
101
|
/**
|
|
67
102
|
* The declared mode names (`modes[].name` from the mount config). Empty until
|
|
@@ -111,11 +146,13 @@ export declare class MorphicBlocks extends EventTarget {
|
|
|
111
146
|
warn?: (...a: unknown[]) => void;
|
|
112
147
|
error?: (...a: unknown[]) => void;
|
|
113
148
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Also print the program's lines to the browser console. Default `false`:
|
|
151
|
+
* lines are only collected in `output`. Ignored when `console` is given,
|
|
152
|
+
* since lines always go to a console the host passes in.
|
|
153
|
+
*/
|
|
154
|
+
logToConsole?: boolean;
|
|
155
|
+
}): MorphicRunResult;
|
|
119
156
|
/** Whether the framework's internal clipboard has a copyable to paste. */
|
|
120
157
|
hasClipboardContents(): boolean;
|
|
121
158
|
/**
|
|
@@ -282,16 +319,31 @@ export declare class MorphicBlocks extends EventTarget {
|
|
|
282
319
|
* least one of those editors to be mounted first.
|
|
283
320
|
*/
|
|
284
321
|
enableSelectionSync(options?: MorphicSelectionSyncOptions): void;
|
|
322
|
+
/**
|
|
323
|
+
* Rebuild an active selection sync after an editor was (re)mounted, so it
|
|
324
|
+
* links the current editors rather than a disposed one.
|
|
325
|
+
*/
|
|
326
|
+
private refreshSelectionSync;
|
|
285
327
|
/** Disable selection sync and clear any active highlights. */
|
|
286
328
|
disableSelectionSync(): void;
|
|
287
329
|
private readonly onWorkspaceChange;
|
|
288
330
|
private readonly onFlyoutChange;
|
|
289
331
|
private registerBlocks;
|
|
332
|
+
/** Build a newly created block with this engine's definition, mode and behavior. */
|
|
333
|
+
private initManagedBlock;
|
|
290
334
|
private validateContainers;
|
|
291
335
|
private createHeadlessHost;
|
|
292
336
|
private resolveMode;
|
|
293
337
|
private applyView;
|
|
294
338
|
private applyWorkspaceContainerClass;
|
|
339
|
+
/**
|
|
340
|
+
* Measure block text with the font the workspace mode's CSS draws it in.
|
|
341
|
+
* The probe sits in the workspace container with Blockly's renderer and
|
|
342
|
+
* base theme classes, so it gets Blockly's own font unless the mode's CSS
|
|
343
|
+
* overrides it, exactly like real block text.
|
|
344
|
+
*/
|
|
345
|
+
private syncWorkspaceFont;
|
|
346
|
+
private readonly onStylesLoaded;
|
|
295
347
|
private applyFlyoutClass;
|
|
296
348
|
private renderWorkspaceBlocks;
|
|
297
349
|
private renderFlyoutBlocks;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as Blockly from "blockly";
|
|
2
|
+
/**
|
|
3
|
+
* Blockly sizes a block by measuring its text with the font its renderer
|
|
4
|
+
* constants name, not with the font the page's CSS draws. When a mode's CSS
|
|
5
|
+
* sets another font for block text (a monospace font for a syntax mode, say),
|
|
6
|
+
* the drawn text is wider than the measured one and neighbouring parts
|
|
7
|
+
* overlap. These helpers read the font the CSS really applies and hand it to
|
|
8
|
+
* Blockly through a theme, so measuring and drawing agree.
|
|
9
|
+
*/
|
|
10
|
+
export interface MorphicBlockFont {
|
|
11
|
+
family: string;
|
|
12
|
+
/** In points, as Blockly's theme expects. */
|
|
13
|
+
size: number;
|
|
14
|
+
weight: string;
|
|
15
|
+
}
|
|
16
|
+
/** The font a workspace's renderer measures with right now. */
|
|
17
|
+
export declare function measuredFont(workspace: Blockly.WorkspaceSvg): MorphicBlockFont;
|
|
18
|
+
/**
|
|
19
|
+
* Read the font the page's CSS gives block text placed inside `parent`,
|
|
20
|
+
* wrapped in elements carrying `wrapperClasses` (outermost first). A hidden
|
|
21
|
+
* probe is added and removed again. Values the CSS leaves open come from
|
|
22
|
+
* `fallback`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readCssFont(parent: HTMLElement, wrapperClasses: string[][], fallback: MorphicBlockFont): MorphicBlockFont;
|
|
25
|
+
export declare const sameFont: (a: MorphicBlockFont, b: MorphicBlockFont) => boolean;
|
|
26
|
+
/** `base` with its block text font replaced by `font`. */
|
|
27
|
+
export declare function themeWithFont(base: Blockly.Theme, font: MorphicBlockFont): Blockly.Theme;
|
|
28
|
+
/**
|
|
29
|
+
* Give `workspace` the theme for `font`, or `base` itself when the font is the
|
|
30
|
+
* one `base` measures with anyway. Returns whether the theme changed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function applyFont(workspace: Blockly.WorkspaceSvg, base: Blockly.Theme, baseFont: MorphicBlockFont, font: MorphicBlockFont): boolean;
|
|
33
|
+
/** Measure every block's text again, e.g. after a font changed or loaded. */
|
|
34
|
+
export declare function remeasureBlocks(workspace: Blockly.WorkspaceSvg): void;
|
|
@@ -3,6 +3,15 @@ import type { MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeName,
|
|
|
3
3
|
export type MorphicManagedBlock = Blockly.BlockSvg & {
|
|
4
4
|
__morphicMode?: string;
|
|
5
5
|
__morphicContext?: MorphicRenderContext;
|
|
6
|
+
/**
|
|
7
|
+
* Input names whose configured `placeholder` has already been handled. A
|
|
8
|
+
* placeholder is a *real* block the user may edit or delete, so it is
|
|
9
|
+
* attached once when the block is first rendered and never re-created —
|
|
10
|
+
* otherwise any re-render (notably a mode switch) would resurrect a value
|
|
11
|
+
* the user deliberately removed. Shadows are exempt: Blockly owns their
|
|
12
|
+
* lifecycle and restores them by design.
|
|
13
|
+
*/
|
|
14
|
+
__morphicPlaceholderSlots?: Set<string>;
|
|
6
15
|
};
|
|
7
16
|
export interface MorphicApplyBlockViewParams {
|
|
8
17
|
block: Blockly.BlockSvg;
|
|
@@ -80,6 +80,7 @@ export declare class MorphicCodeEditor {
|
|
|
80
80
|
private hoverHighlightEffect?;
|
|
81
81
|
private editableHoverHighlightEffect?;
|
|
82
82
|
private emptyClickListener?;
|
|
83
|
+
private disposed;
|
|
83
84
|
constructor(container: HTMLElement, workspace: Blockly.WorkspaceSvg, generateWithMetadata: () => MorphicCodeGenerationResult, options?: MorphicCodeEditorOptions);
|
|
84
85
|
mount(): Promise<void>;
|
|
85
86
|
private buildDeleteExtensions;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Blockly from "blockly";
|
|
2
2
|
import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicCodeGenerationResult, MorphicJavaScriptConfig } from "./types";
|
|
3
3
|
export declare function generateJavaScriptFromWorkspace(workspace: Blockly.WorkspaceSvg, definitions: ReadonlyMap<string, MorphicBlockDefinition>, behaviors: MorphicBehaviorMap, config?: MorphicJavaScriptConfig): string;
|
|
4
4
|
export declare function generateJavaScriptWithMetadataFromWorkspace(workspace: Blockly.WorkspaceSvg, definitions: ReadonlyMap<string, MorphicBlockDefinition>, behaviors: MorphicBehaviorMap, config?: MorphicJavaScriptConfig): MorphicCodeGenerationResult;
|
|
@@ -1,2 +1,28 @@
|
|
|
1
|
-
import type { MorphicBlockDefinition } from "./types";
|
|
1
|
+
import type { MorphicBlockDefinition, MorphicBlockShape, MorphicElementTypeEntry } from "./types";
|
|
2
|
+
export declare const BLOCK_SHAPES: MorphicBlockShape[];
|
|
3
|
+
/**
|
|
4
|
+
* Turn each block's optional `shape` into the connection flags it stands for,
|
|
5
|
+
* once, at load time, so rendering and codegen only ever see the flags. A flag
|
|
6
|
+
* the block sets itself is kept, which is how a connection type is added
|
|
7
|
+
* (`"shape": "statement", "previousStatement": "Action"`). `shape` stays on the
|
|
8
|
+
* block so validation can report contradictions. Returns new block objects.
|
|
9
|
+
*/
|
|
10
|
+
export declare function applyBlockShapes(blocks: MorphicBlockDefinition[]): MorphicBlockDefinition[];
|
|
11
|
+
/**
|
|
12
|
+
* Reserved element key: a block's fallback template, used for every `code`
|
|
13
|
+
* element the block does not list itself. A listed element always wins.
|
|
14
|
+
*/
|
|
15
|
+
export declare const DEFAULT_ELEMENT = "default";
|
|
16
|
+
/**
|
|
17
|
+
* Resolve each block's optional `default` element once, at load time: it is
|
|
18
|
+
* copied into every `code` element (declared in `elementTypes`) that the block
|
|
19
|
+
* does not list, then removed. Every later consumer (workspace, codespace,
|
|
20
|
+
* preview, toolbox, codegen, validation) therefore sees complete blocks and
|
|
21
|
+
* needs no knowledge of `default`.
|
|
22
|
+
*
|
|
23
|
+
* Returns new block objects; the caller's definitions are not mutated. A
|
|
24
|
+
* `default` that fills nothing (no `code` elements are declared) is kept, so
|
|
25
|
+
* validation reports it as an undeclared element instead of it vanishing.
|
|
26
|
+
*/
|
|
27
|
+
export declare function expandDefaultElements(blocks: MorphicBlockDefinition[], elementTypes: Record<string, MorphicElementTypeEntry>): MorphicBlockDefinition[];
|
|
2
28
|
export declare function createDefinitionMap(definitions: MorphicBlockDefinition[] | MorphicBlockDefinition): Map<string, MorphicBlockDefinition>;
|
package/dist/morphic/styles.d.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
-
import type { MorphicModeDefinition,
|
|
1
|
+
import type { MorphicModeDefinition, MorphicModeName, MorphicStyleBundle, MorphicToolboxCategory } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* One mode's stylesheet, a link or the CSS itself. Hosts pass `modesFolder` or
|
|
4
|
+
* the `modeStyles` map; the engine merges both into a list of these.
|
|
5
|
+
*/
|
|
6
|
+
export interface MorphicModeStyle {
|
|
7
|
+
mode: MorphicModeName;
|
|
8
|
+
href?: string;
|
|
9
|
+
cssText?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Injects the framework's stylesheets into the page. Every helper first checks
|
|
13
|
+
* the page itself for an identical stylesheet, so engines that are created
|
|
14
|
+
* again and again (a remounted component, React's double mount in development,
|
|
15
|
+
* several editors on one page) never stack up copies of the same CSS.
|
|
16
|
+
*/
|
|
2
17
|
export declare class MorphicStyleManager {
|
|
3
|
-
private readonly loadedStyleKeys;
|
|
4
18
|
/**
|
|
5
19
|
* Injects visibility CSS derived from mode definitions.
|
|
6
20
|
* Hides all morphic elements by default, then shows only those listed
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import type { MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarItem, MorphicToolbarPane } from "./types";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
result: unknown;
|
|
5
|
-
error: Error | null;
|
|
6
|
-
}
|
|
1
|
+
import type { MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarItem, MorphicToolbarPane, MorphicRunResult } from "./types";
|
|
2
|
+
/** Detail of the `morphic-run` event: the same result `runJavaScript` returns. */
|
|
3
|
+
export type MorphicRunEventDetail = MorphicRunResult;
|
|
7
4
|
export interface MorphicToolbarHandle {
|
|
8
5
|
refresh: () => void;
|
|
9
6
|
dispose: () => void;
|
|
@@ -15,7 +15,10 @@ export declare class MorphicToolboxCanvas {
|
|
|
15
15
|
/** Per-element block/text override for code elements in tiles (from the active preset). */
|
|
16
16
|
private renderOverride?;
|
|
17
17
|
private previewWorkspace?;
|
|
18
|
+
private readonly onPreviewWorkspace?;
|
|
18
19
|
private previewContainer?;
|
|
20
|
+
/** The preview workspace's own theme and font, before any tile font. */
|
|
21
|
+
private previewBase?;
|
|
19
22
|
private readonly onDragOver;
|
|
20
23
|
private readonly onDrop;
|
|
21
24
|
constructor(params: {
|
|
@@ -30,12 +33,19 @@ export declare class MorphicToolboxCanvas {
|
|
|
30
33
|
render?: Record<string, "block" | "text">;
|
|
31
34
|
modes?: MorphicModeDefinition[];
|
|
32
35
|
options?: MorphicToolboxCanvasOptions;
|
|
36
|
+
/** Called once with the hidden workspace used to draw block previews. */
|
|
37
|
+
onPreviewWorkspace?: (workspace: Blockly.WorkspaceSvg) => void;
|
|
33
38
|
});
|
|
34
39
|
rerender(mode: MorphicModeName, render?: Record<string, "block" | "text">): void;
|
|
35
40
|
dispose(): void;
|
|
36
41
|
private render;
|
|
37
42
|
private createTile;
|
|
38
43
|
private ensurePreviewWorkspace;
|
|
44
|
+
/**
|
|
45
|
+
* The tile shows a copy of the rendered block outside Blockly, so its text
|
|
46
|
+
* takes the font the tile's CSS gives that element. Measure with that font.
|
|
47
|
+
*/
|
|
48
|
+
private syncPreviewFont;
|
|
39
49
|
private createBlockPreviewSvg;
|
|
40
50
|
private createBlockAtPosition;
|
|
41
51
|
private resolveBlockIds;
|
package/dist/morphic/types.d.ts
CHANGED
|
@@ -80,6 +80,18 @@ export interface MorphicElementTypeConfig {
|
|
|
80
80
|
/** Either a bare type or a config object with extras. */
|
|
81
81
|
export type MorphicElementTypeEntry = MorphicElementType | MorphicElementTypeConfig;
|
|
82
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";
|
|
83
95
|
export type MorphicInputKind = "value" | "statement";
|
|
84
96
|
export type MorphicInputAlign = "left" | "centre" | "right";
|
|
85
97
|
/**
|
|
@@ -189,9 +201,11 @@ export interface MorphicModeDefinition {
|
|
|
189
201
|
export interface MorphicBlockDefinition {
|
|
190
202
|
identifier: string;
|
|
191
203
|
/**
|
|
192
|
-
* Named visual elements of this Morphic Block.
|
|
193
|
-
*
|
|
194
|
-
*
|
|
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.
|
|
195
209
|
*/
|
|
196
210
|
elements: MorphicBlockElements;
|
|
197
211
|
/**
|
|
@@ -209,6 +223,12 @@ export interface MorphicBlockDefinition {
|
|
|
209
223
|
fields?: Record<string, MorphicFieldDefinition>;
|
|
210
224
|
color?: number | string;
|
|
211
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;
|
|
212
232
|
previousStatement?: MorphicConnectionSpec;
|
|
213
233
|
nextStatement?: MorphicConnectionSpec;
|
|
214
234
|
inputsInline?: boolean;
|
|
@@ -245,6 +265,20 @@ export interface MorphicHighlightDefinition {
|
|
|
245
265
|
};
|
|
246
266
|
}
|
|
247
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;
|
|
248
282
|
export interface MorphicBlocksFormat {
|
|
249
283
|
/** JSON Schema reference for editor tooling. Ignored by the framework. */
|
|
250
284
|
$schema?: string;
|
|
@@ -272,6 +306,23 @@ export interface MorphicBlocksFormat {
|
|
|
272
306
|
blocks: MorphicBlockDefinition[];
|
|
273
307
|
}
|
|
274
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
|
+
}
|
|
275
326
|
export interface MorphicBehaviorContext {
|
|
276
327
|
Blockly: typeof Blockly;
|
|
277
328
|
workspace: Blockly.WorkspaceSvg;
|
|
@@ -285,7 +336,18 @@ export interface MorphicBehaviorProxy {
|
|
|
285
336
|
mode: MorphicModeName;
|
|
286
337
|
context: MorphicRenderContext;
|
|
287
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
|
+
*/
|
|
288
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>;
|
|
289
351
|
}
|
|
290
352
|
export type MorphicCodeBehavior = (proxy: MorphicBehaviorProxy) => string;
|
|
291
353
|
export interface MorphicBlockBehavior {
|
|
@@ -295,11 +357,6 @@ export interface MorphicBlockBehavior {
|
|
|
295
357
|
}
|
|
296
358
|
export type MorphicBehaviorDefinition = MorphicBlockBehavior | MorphicCodeBehavior;
|
|
297
359
|
export type MorphicBehaviorMap = Record<string, MorphicBehaviorDefinition>;
|
|
298
|
-
export interface MorphicModeStyle {
|
|
299
|
-
mode: MorphicModeName;
|
|
300
|
-
href?: string;
|
|
301
|
-
cssText?: string;
|
|
302
|
-
}
|
|
303
360
|
export interface MorphicStyleBundle {
|
|
304
361
|
href?: string;
|
|
305
362
|
cssText?: string;
|
|
@@ -344,10 +401,33 @@ export interface MorphicMountConfig {
|
|
|
344
401
|
*/
|
|
345
402
|
workspaceContainer?: HTMLElement;
|
|
346
403
|
/**
|
|
347
|
-
* Container for the primary text editor (codespace). Optional
|
|
348
|
-
* when a `
|
|
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.
|
|
349
406
|
*/
|
|
350
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;
|
|
351
431
|
/** Mode definitions — drives automatic element visibility CSS. */
|
|
352
432
|
modes?: MorphicModeDefinition[];
|
|
353
433
|
/**
|
|
@@ -370,34 +450,28 @@ export interface MorphicMountConfig {
|
|
|
370
450
|
* Use this when calling `mountToolbox()` to avoid Blockly toolbox type conflicts.
|
|
371
451
|
*/
|
|
372
452
|
canvasToolbox?: boolean;
|
|
373
|
-
workspaceMode?: MorphicModeName;
|
|
374
|
-
toolboxMode?: MorphicModeName;
|
|
375
|
-
/**
|
|
376
|
-
* Independent mode for the codespace. When set, the codespace renders the
|
|
377
|
-
* source element of this mode instead of following `workspaceMode`.
|
|
378
|
-
*/
|
|
379
|
-
codespaceMode?: MorphicModeName;
|
|
380
|
-
/**
|
|
381
|
-
* Mode for the preview editor. The preview renders the source element of
|
|
382
|
-
* this mode. When unset, the preview falls back to the active workspace
|
|
383
|
-
* mode's `preview` element (legacy behavior).
|
|
384
|
-
*/
|
|
385
|
-
previewMode?: MorphicModeName;
|
|
386
453
|
ui?: {
|
|
387
454
|
workspaceClassName?: string | string[];
|
|
388
455
|
toolboxClassName?: string | string[];
|
|
389
456
|
};
|
|
390
|
-
modeStyles?: MorphicModeStyle[];
|
|
391
457
|
/**
|
|
392
|
-
*
|
|
393
|
-
*
|
|
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`).
|
|
394
468
|
* The framework derives mode names from CSS filenames and loads the stylesheets automatically.
|
|
395
469
|
* Takes precedence over `modeStyles` for the same modes.
|
|
396
470
|
*/
|
|
397
471
|
modesFolder?: Record<string, unknown>;
|
|
398
472
|
baseStyle?: MorphicStyleBundle;
|
|
473
|
+
/** Options passed through to Blockly's `inject` (zoom, grid, trashcan, and so on). */
|
|
399
474
|
blockly?: Blockly.BlocklyOptions;
|
|
400
|
-
blocklyOptions?: Blockly.BlocklyOptions;
|
|
401
475
|
javascript?: MorphicJavaScriptConfig;
|
|
402
476
|
/**
|
|
403
477
|
* Per-element highlight rules, keyed by element name. The codespace and
|
|
@@ -620,3 +694,4 @@ export interface MorphicToolbarConfig {
|
|
|
620
694
|
/** Display mode for items that have both icon and label. Defaults to "icon". */
|
|
621
695
|
display?: MorphicToolbarDisplay;
|
|
622
696
|
}
|
|
697
|
+
export {};
|