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,360 @@
|
|
|
1
|
+
import * as Blockly from "blockly";
|
|
2
|
+
import { type MorphicToolbarHandle } from "./toolbar";
|
|
3
|
+
import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions } from "./types";
|
|
4
|
+
export declare class MorphicBlocks extends EventTarget {
|
|
5
|
+
private readonly definitions;
|
|
6
|
+
private readonly behaviors;
|
|
7
|
+
private readonly elementTypes;
|
|
8
|
+
private readonly styles;
|
|
9
|
+
private readonly registeredBlockTypes;
|
|
10
|
+
private readonly toolbars;
|
|
11
|
+
/** Blockly events listener installed when the first toolbar mounts. */
|
|
12
|
+
private toolbarsBlocklyListener?;
|
|
13
|
+
/** Latest internal clipboard contents — set by `copyActiveBlock`, used by `pasteActiveBlock`. */
|
|
14
|
+
private lastCopyData?;
|
|
15
|
+
private mountConfig?;
|
|
16
|
+
private workspace?;
|
|
17
|
+
private flyoutWorkspace?;
|
|
18
|
+
private toolboxCanvas?;
|
|
19
|
+
private codeEditor?;
|
|
20
|
+
private codespace?;
|
|
21
|
+
private previewEditor?;
|
|
22
|
+
private selectionSync?;
|
|
23
|
+
private toolboxDefinition?;
|
|
24
|
+
private blockCategoryIndex;
|
|
25
|
+
private appliedWorkspaceClasses;
|
|
26
|
+
private appliedToolboxFlyoutClasses;
|
|
27
|
+
private appliedToolboxShellClasses;
|
|
28
|
+
/** Offscreen div created when no user-supplied workspaceContainer is present. */
|
|
29
|
+
private headlessWorkspaceHost?;
|
|
30
|
+
/** Teardown fn for codespace drag/drop listeners; set when codespace is mounted. */
|
|
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?;
|
|
43
|
+
/** Format-level fields remembered from the constructor and used as `mount()`
|
|
44
|
+
* defaults, so the developer hands the whole definitions file in once and the
|
|
45
|
+
* runtime `mount()` call only carries DOM / runtime concerns. */
|
|
46
|
+
private readonly formatModes?;
|
|
47
|
+
private readonly formatPresets?;
|
|
48
|
+
private readonly formatHighlighting?;
|
|
49
|
+
private readonly formatCategories?;
|
|
50
|
+
/**
|
|
51
|
+
* Build an engine from a whole definitions file plus its behaviors — the two
|
|
52
|
+
* artifacts a developer authors. `blocks` and `elementTypes` configure the
|
|
53
|
+
* engine; `modes` / `presets` / `highlighting` / `categories` are remembered
|
|
54
|
+
* as defaults for `mount()` / `mountToolbox()` (a call may still override them).
|
|
55
|
+
*/
|
|
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;
|
|
70
|
+
/** Presets declared at mount (empty when none were provided). */
|
|
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;
|
|
79
|
+
/**
|
|
80
|
+
* Apply a preset by name or index: sets the per-view modes derived from the
|
|
81
|
+
* preset and notifies `onPresetApplied` so the host can update pane
|
|
82
|
+
* visibility. Returns the applied preset.
|
|
83
|
+
*/
|
|
84
|
+
applyPreset(nameOrIndex: string | number): MorphicPresetDefinition;
|
|
85
|
+
/** Validate preset definitions against modes, element types, and containers. */
|
|
86
|
+
/**
|
|
87
|
+
* Static validation of the block definitions against the mount config. Warns
|
|
88
|
+
* on degraded / dead config and throws (listing every structural problem at
|
|
89
|
+
* once) on breakage that guarantees wrong output. See `validate-definitions`.
|
|
90
|
+
*/
|
|
91
|
+
private validateDefinitions;
|
|
92
|
+
private validatePresets;
|
|
93
|
+
dispose(): void;
|
|
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
|
+
*/
|
|
100
|
+
getWorkspace(): Blockly.WorkspaceSvg | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* The declared mode names (`modes[].name` from the mount config). Empty until
|
|
103
|
+
* `mount()` runs. Note: this is *modes*, not element names — an element like
|
|
104
|
+
* `icon` or `python` is not a mode unless a mode is named after it.
|
|
105
|
+
*/
|
|
106
|
+
getAvailableModes(): MorphicModeName[];
|
|
107
|
+
/** Current workspace mode name, or `undefined` if not mounted. */
|
|
108
|
+
getWorkspaceMode(): MorphicModeName | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Effective codespace mode name: the independent `codespaceMode` when set,
|
|
111
|
+
* else the workspace mode. `undefined` if not mounted.
|
|
112
|
+
*/
|
|
113
|
+
getCodespaceMode(): MorphicModeName | undefined;
|
|
114
|
+
/** Preview mode name, or `undefined` when no preview mode is set. */
|
|
115
|
+
getPreviewMode(): MorphicModeName | undefined;
|
|
116
|
+
/** Mode definition by name, or `undefined`. */
|
|
117
|
+
private modeDef;
|
|
118
|
+
/** Source element rendered by the codespace (label + highlighting source). */
|
|
119
|
+
getActivePrimarySourceElement(): string | undefined;
|
|
120
|
+
/** Source element rendered by the preview (label + highlighting source). */
|
|
121
|
+
getActivePreviewElement(): string | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Mount a toolbar into the developer-provided container, bound to one of the
|
|
124
|
+
* three editor surfaces. When `items` is omitted, the default set for the
|
|
125
|
+
* pane is rendered. Returns a handle for `refresh()` / `dispose()`.
|
|
126
|
+
*
|
|
127
|
+
* The toolbar's stateful items (undo/redo enabled-state, language label)
|
|
128
|
+
* refresh automatically when:
|
|
129
|
+
* - any Blockly event fires on the workspace (covers undo/redo,
|
|
130
|
+
* drops, edits — both workspace- and codespace-originated)
|
|
131
|
+
* - `setModes` is called (covers language label changes)
|
|
132
|
+
*/
|
|
133
|
+
mountToolbar(container: HTMLElement, config: MorphicToolbarConfig): MorphicToolbarHandle;
|
|
134
|
+
private toolbarTextFor;
|
|
135
|
+
private toolbarRefreshFor;
|
|
136
|
+
/**
|
|
137
|
+
* Generate JavaScript from the current workspace and execute it.
|
|
138
|
+
* Pass an optional `console` to capture `console.log` / `.warn` / `.error`
|
|
139
|
+
* calls — useful when routing output to a panel rather than the browser
|
|
140
|
+
* devtools. Dispatches a `morphic-run` CustomEvent with the result so
|
|
141
|
+
* additional listeners can react.
|
|
142
|
+
*/
|
|
143
|
+
runJavaScript(options?: {
|
|
144
|
+
console?: {
|
|
145
|
+
log: (...a: unknown[]) => void;
|
|
146
|
+
warn?: (...a: unknown[]) => void;
|
|
147
|
+
error?: (...a: unknown[]) => void;
|
|
148
|
+
};
|
|
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;
|
|
156
|
+
/** Whether the framework's internal clipboard has a copyable to paste. */
|
|
157
|
+
hasClipboardContents(): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Copy the currently active block to the framework's internal clipboard and
|
|
160
|
+
* mirror its generated code text to the system clipboard. Resolution order:
|
|
161
|
+
* (1) Blockly's selected block, (2) for codespace/preview, the deepest block
|
|
162
|
+
* enclosing the active line. Returns true when something was copied.
|
|
163
|
+
*/
|
|
164
|
+
copyActiveBlock(pane: "workspace" | "codespace" | "preview"): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Paste the last copied block at the active pane's natural location.
|
|
167
|
+
* Workspace: Blockly's default (workspace centre, slightly offset).
|
|
168
|
+
* Codespace/preview: the block lands at the workspace level too — the text
|
|
169
|
+
* view re-renders to include it. (Cursor-position-aware paste into a
|
|
170
|
+
* specific slot is deferred; the current behaviour matches Ctrl+V in
|
|
171
|
+
* standard Blockly.) Returns true if something was pasted.
|
|
172
|
+
*/
|
|
173
|
+
pasteActiveBlock(_pane: "workspace" | "codespace" | "preview"): boolean;
|
|
174
|
+
private resolveActiveBlock;
|
|
175
|
+
/** Serialize the workspace to a plain object (Blockly's native format). */
|
|
176
|
+
serializeWorkspace(): unknown;
|
|
177
|
+
/** Restore the workspace from a previously serialized state. */
|
|
178
|
+
loadWorkspace(state: unknown): void;
|
|
179
|
+
/**
|
|
180
|
+
* Adjust the pane's zoom level. Direction is `"in" | "out" | "fit"`. For
|
|
181
|
+
* the workspace pane this maps to `workspace.zoomCenter(±1)` /
|
|
182
|
+
* `zoomToFit()`. For codespace/preview it scales the editor's font-size.
|
|
183
|
+
*/
|
|
184
|
+
zoomPane(pane: "workspace" | "codespace" | "preview", direction: "in" | "out" | "fit"): void;
|
|
185
|
+
private ensureToolbarBlocklyListener;
|
|
186
|
+
private teardownToolbarBlocklyListener;
|
|
187
|
+
setModes(modes: {
|
|
188
|
+
workspaceMode?: MorphicModeName;
|
|
189
|
+
toolboxMode?: MorphicModeName;
|
|
190
|
+
/** Per-element block/text override for toolbox tiles. `null` clears it (all code elements render as blocks). */
|
|
191
|
+
toolboxRender?: Record<string, "block" | "text"> | null;
|
|
192
|
+
/** Independent codespace mode. `null` clears the override (falls back to workspaceMode). */
|
|
193
|
+
codespaceMode?: MorphicModeName | null;
|
|
194
|
+
/** Preview mode. `null` clears it (preview renders nothing unless the workspace mode declares a legacy `preview`). */
|
|
195
|
+
previewMode?: MorphicModeName | null;
|
|
196
|
+
}): void;
|
|
197
|
+
/**
|
|
198
|
+
* Resolve the highlight rules for the codespace or preview editor by
|
|
199
|
+
* looking up that pane's source element name in the
|
|
200
|
+
* `mountConfig.highlighting` registry. Returns `undefined` when no
|
|
201
|
+
* matching entry is configured.
|
|
202
|
+
*/
|
|
203
|
+
private resolveHighlightRules;
|
|
204
|
+
generateJavaScript(): string;
|
|
205
|
+
generateJavaScriptWithMetadata(): MorphicCodeGenerationResult;
|
|
206
|
+
mountCodeEditor(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
|
|
207
|
+
/**
|
|
208
|
+
* Mounts the primary text editor (codespace) into the `codespaceContainer`
|
|
209
|
+
* declared at `mount()`. Renders the workspace as text using the codespace
|
|
210
|
+
* mode's source element. Read-only for now; Task 4 adds drops.
|
|
211
|
+
*/
|
|
212
|
+
mountCodespace(options?: MorphicCodeEditorOptions): Promise<void>;
|
|
213
|
+
private attachCodespaceDropTarget;
|
|
214
|
+
/** Innermost (smallest-range) block in metadata whose char range contains `charOffset`. */
|
|
215
|
+
private findInnermostBlockAtChar;
|
|
216
|
+
/**
|
|
217
|
+
* Hover-background target: the smallest *non-atomic* block whose range
|
|
218
|
+
* contains the cursor. Atomic single-field blocks (numbers, strings,
|
|
219
|
+
* booleans) are skipped so hovering on `3` highlights the surrounding
|
|
220
|
+
* `1 + 2` expression instead of just the digit. Falls back to the smallest
|
|
221
|
+
* block found when only atomic blocks contain the cursor (e.g. a top-level
|
|
222
|
+
* orphan number on its own line).
|
|
223
|
+
*/
|
|
224
|
+
private findHoverBlockAtChar;
|
|
225
|
+
/** Innermost placeholder with an editable `edit` target whose range contains `charOffset`. */
|
|
226
|
+
private findInnermostEditablePlaceholderAtChar;
|
|
227
|
+
/**
|
|
228
|
+
* Reusable drop-result applicator for the right-click drag path: same set
|
|
229
|
+
* of target kinds as the HTML5 drop handler, but the source is always an
|
|
230
|
+
* existing workspace block (no toolbox-spawn case).
|
|
231
|
+
*/
|
|
232
|
+
private applyRightDragMove;
|
|
233
|
+
/**
|
|
234
|
+
* Resolve a codespace drop into both a visual indicator and a typed target.
|
|
235
|
+
*
|
|
236
|
+
* `target.kind === "top"` means the drop becomes a top-level block at
|
|
237
|
+
* `target.index`. `target.kind === "statement"` means it should be wired into
|
|
238
|
+
* an existing block's statement chain — `targetBlockId` is the block under
|
|
239
|
+
* the cursor and `position` says whether to land before or after it.
|
|
240
|
+
*
|
|
241
|
+
* Above/below is decided by upper/lower-half of the cursor's line; cursor
|
|
242
|
+
* past the last rendered line always resolves to "after all" at top level.
|
|
243
|
+
*/
|
|
244
|
+
private computeCodespaceDrop;
|
|
245
|
+
/**
|
|
246
|
+
* Deepest statement-input slot whose body range contains `line`. Used for
|
|
247
|
+
* empty/whitespace bodies where no child block anchors the line.
|
|
248
|
+
*/
|
|
249
|
+
private findInnermostStatementSlotAtLine;
|
|
250
|
+
/**
|
|
251
|
+
* Append `source` to the chain in `parent`'s statement input named `inputName`.
|
|
252
|
+
* Connects to the slot directly when the slot is empty; otherwise walks to
|
|
253
|
+
* the chain's tail and connects there.
|
|
254
|
+
*/
|
|
255
|
+
private connectIntoSlot;
|
|
256
|
+
/**
|
|
257
|
+
* Resolve a char-offset in the codespace to the value slot the drop should
|
|
258
|
+
* land in. Two cases, both yielding the parent block + input name:
|
|
259
|
+
*
|
|
260
|
+
* 1. Empty slot — the offset is inside a placeholder range with a
|
|
261
|
+
* shadow/placeholder block (`edit.blockId`). The shadow's parent
|
|
262
|
+
* connection identifies the slot.
|
|
263
|
+
* 2. Occupied slot — the offset is inside an inner value block's char
|
|
264
|
+
* range (innermost wins by smallest range). The block's own parent
|
|
265
|
+
* connection identifies the slot; on drop, the child is replaced.
|
|
266
|
+
*
|
|
267
|
+
* Excludes the active grip-drag source so dragging a value block onto its
|
|
268
|
+
* own rendered range is a no-op rather than a self-replace.
|
|
269
|
+
*/
|
|
270
|
+
private findValueSlotDropAtChar;
|
|
271
|
+
/** Find the value-input that holds `child`, returning its parent + input name. */
|
|
272
|
+
private resolveValueParent;
|
|
273
|
+
/**
|
|
274
|
+
* Place a freshly-orphaned block at a clear workspace coordinate, below the
|
|
275
|
+
* current bottom-most top block. Keeps replaced value children visible in
|
|
276
|
+
* the codespace instead of overlapping the parent's slot position.
|
|
277
|
+
*/
|
|
278
|
+
private placeOrphanBelowTops;
|
|
279
|
+
/** True when `candidate` is `ancestor` itself or anywhere in its subtree. */
|
|
280
|
+
private isDescendantOf;
|
|
281
|
+
/**
|
|
282
|
+
* Connect `source`'s output into the value input named `inputName` on
|
|
283
|
+
* `parent`. A real (non-shadow) child currently in the slot is `unplug`'d
|
|
284
|
+
* to top level; a shadow disconnects implicitly when the new block
|
|
285
|
+
* connects. The Blockly connection-check (output-type vs slot `check`)
|
|
286
|
+
* runs inside `connect`; an incompatible drop throws and is rolled back
|
|
287
|
+
* by restoring the prior real child when there was one.
|
|
288
|
+
*/
|
|
289
|
+
private connectIntoValueSlot;
|
|
290
|
+
private computeBlockDepth;
|
|
291
|
+
/** Place `block` at top-level `index`, re-spacing all top blocks. */
|
|
292
|
+
private placeAtTopIndex;
|
|
293
|
+
/** Connect `source` into `target`'s statement chain before/after `target`. */
|
|
294
|
+
private connectStatement;
|
|
295
|
+
private applyTopBlockOrder;
|
|
296
|
+
/**
|
|
297
|
+
* Mounts a read-only preview editor that renders the current mode's
|
|
298
|
+
* `preview` element as text. Re-renders on workspace and mode changes.
|
|
299
|
+
* If the active mode has no `preview` declared, the editor stays empty.
|
|
300
|
+
*/
|
|
301
|
+
mountPreview(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
|
|
302
|
+
private generatePreviewText;
|
|
303
|
+
/**
|
|
304
|
+
* Write `newValue` to `edit.fieldName` on the Blockly block identified by
|
|
305
|
+
* `edit.blockId`. The change event listener will trigger codespace re-sync.
|
|
306
|
+
*/
|
|
307
|
+
private applyPlaceholderEdit;
|
|
308
|
+
private deleteBlockAtCodespaceLine;
|
|
309
|
+
private generateCodespaceText;
|
|
310
|
+
showCodeEditor(): void;
|
|
311
|
+
hideCodeEditor(): void;
|
|
312
|
+
isCodeEditorVisible(): boolean;
|
|
313
|
+
setCodeEditorTheme(theme: MorphicCodeEditorTheme): void;
|
|
314
|
+
setCodespaceTheme(theme: MorphicCodeEditorTheme): void;
|
|
315
|
+
setPreviewTheme(theme: MorphicCodeEditorTheme): void;
|
|
316
|
+
/**
|
|
317
|
+
* Enable bidirectional selection sync between the Blockly workspace and every
|
|
318
|
+
* currently-mounted editor (code editor, codespace, preview). Requires at
|
|
319
|
+
* least one of those editors to be mounted first.
|
|
320
|
+
*/
|
|
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;
|
|
327
|
+
/** Disable selection sync and clear any active highlights. */
|
|
328
|
+
disableSelectionSync(): void;
|
|
329
|
+
private readonly onWorkspaceChange;
|
|
330
|
+
private readonly onFlyoutChange;
|
|
331
|
+
private registerBlocks;
|
|
332
|
+
/** Build a newly created block with this engine's definition, mode and behavior. */
|
|
333
|
+
private initManagedBlock;
|
|
334
|
+
private validateContainers;
|
|
335
|
+
private createHeadlessHost;
|
|
336
|
+
private resolveMode;
|
|
337
|
+
private applyView;
|
|
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;
|
|
347
|
+
private applyFlyoutClass;
|
|
348
|
+
private renderWorkspaceBlocks;
|
|
349
|
+
private renderFlyoutBlocks;
|
|
350
|
+
private refreshToolbox;
|
|
351
|
+
private bindFlyoutWorkspace;
|
|
352
|
+
private resolveToolboxDefinition;
|
|
353
|
+
private createBehaviorContext;
|
|
354
|
+
private resolveClassNames;
|
|
355
|
+
private replaceUserClasses;
|
|
356
|
+
private buildBlockColorMap;
|
|
357
|
+
private resolveToolboxKind;
|
|
358
|
+
private createCategoryIndex;
|
|
359
|
+
private deferApplyView;
|
|
360
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { MorphicBehaviorDefinition, MorphicBlockBehavior, MorphicCodeBehavior } from "./types";
|
|
2
|
+
export declare function getLifecycleBehavior(behavior: MorphicBehaviorDefinition | undefined): MorphicBlockBehavior | undefined;
|
|
3
|
+
export declare function getCodeBehavior(behavior: MorphicBehaviorDefinition | undefined): MorphicCodeBehavior | undefined;
|
|
@@ -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;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Morphic blocks are registered with Blockly under a namespaced type
|
|
3
|
+
* (`morphic:<identifier>`) so a developer's identifier can never collide with
|
|
4
|
+
* a Blockly built-in (e.g. naming a block `logic_boolean` would otherwise
|
|
5
|
+
* clobber Blockly's stock block and break shadows / connection checks).
|
|
6
|
+
*
|
|
7
|
+
* The namespace is an internal Blockly-type detail: `definitions.json` and
|
|
8
|
+
* `behaviors.ts` are written with clean identifiers, and the framework
|
|
9
|
+
* translates at the boundaries (registration, `newBlock`, toolbox, drag
|
|
10
|
+
* payloads, codegen, shadow/placeholder resolution).
|
|
11
|
+
*/
|
|
12
|
+
export declare const MORPHIC_TYPE_PREFIX = "morphic:";
|
|
13
|
+
/** Clean identifier → namespaced Blockly type (`text_print` → `morphic:text_print`). */
|
|
14
|
+
export declare function toBlocklyType(identifier: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Namespaced Blockly type → clean identifier (`morphic:text_print` →
|
|
17
|
+
* `text_print`). Returns the input unchanged when it isn't namespaced (e.g. a
|
|
18
|
+
* Blockly stock type such as `math_number`), so it is safe to call on any
|
|
19
|
+
* `block.type`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function toCleanId(blocklyType: string): string;
|
|
22
|
+
/** Whether a Blockly type is a namespaced morphic type. */
|
|
23
|
+
export declare function isMorphicType(blocklyType: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Resolve a developer-facing block reference — from a toolbox entry, a
|
|
26
|
+
* `shadow`/`placeholder` default, or a drag payload — to its registered Blockly
|
|
27
|
+
* type. A reference is morphic *if and only if* its clean id is in the
|
|
28
|
+
* definitions map: in that case it is namespaced (`text_print` →
|
|
29
|
+
* `morphic:text_print`); otherwise it is treated as a Blockly stock type and
|
|
30
|
+
* passes through unchanged (`math_number` stays `math_number`).
|
|
31
|
+
*/
|
|
32
|
+
export declare function resolveBlocklyType(ref: string, definitions: {
|
|
33
|
+
has(key: string): boolean;
|
|
34
|
+
}): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as Blockly from "blockly";
|
|
2
|
+
import type { MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeName, MorphicRenderContext, MorphicResolvedView } from "./types";
|
|
3
|
+
export type MorphicManagedBlock = Blockly.BlockSvg & {
|
|
4
|
+
__morphicMode?: string;
|
|
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>;
|
|
15
|
+
};
|
|
16
|
+
export interface MorphicApplyBlockViewParams {
|
|
17
|
+
block: Blockly.BlockSvg;
|
|
18
|
+
definition: MorphicBlockDefinition;
|
|
19
|
+
view: MorphicResolvedView;
|
|
20
|
+
mode: MorphicModeName;
|
|
21
|
+
context: MorphicRenderContext;
|
|
22
|
+
/**
|
|
23
|
+
* Global element type registry. When provided, value inputs with a
|
|
24
|
+
* configured shadow get a Blockly shadow block attached via
|
|
25
|
+
* `setShadowState`. Pass only for the engine's main editing workspace —
|
|
26
|
+
* toolbox-canvas SVG previews and Blockly's flyout don't benefit from
|
|
27
|
+
* shadows and skip this step by omitting elementTypes.
|
|
28
|
+
*/
|
|
29
|
+
elementTypes?: Record<string, MorphicElementTypeEntry>;
|
|
30
|
+
/**
|
|
31
|
+
* Translates a developer-facing block reference (a `shadow`/`placeholder`
|
|
32
|
+
* default) to its registered Blockly type. Morphic refs are namespaced;
|
|
33
|
+
* stock Blockly types pass through. Defaults to identity when omitted.
|
|
34
|
+
*/
|
|
35
|
+
resolveBlocklyType?: (ref: string) => string;
|
|
36
|
+
}
|
|
37
|
+
export declare function applyBlockView(params: MorphicApplyBlockViewParams): void;
|
|
38
|
+
export declare function applyRootModeClasses(root: Element, mode: MorphicModeName, context: MorphicRenderContext, rootClassName: string): void;
|
|
39
|
+
export declare function getManagedBlockMode(block: Blockly.Block): string | undefined;
|
|
40
|
+
export declare function applyBlockCategoryClass(block: Blockly.BlockSvg, categoryToken?: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Stamps a stable `morphic-block-{identifier}` class on the block SVG root.
|
|
43
|
+
* This persists across mode switches and lets mode CSS files target individual
|
|
44
|
+
* blocks with rules like `.morphic-block-log_message { --morphic-block-color: #f97316; }`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function applyBlockIdentifierClass(block: Blockly.BlockSvg, identifier: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Reads the CSS custom property `--morphic-block-color` from the block SVG root
|
|
49
|
+
* (after mode and identifier classes have been applied) and calls `block.setColour()`
|
|
50
|
+
* when a value is found. This lets mode CSS files drive block colours declaratively.
|
|
51
|
+
*/
|
|
52
|
+
export declare function applyBlockColorFromCSS(block: Blockly.BlockSvg): void;
|
|
53
|
+
export declare function captureFieldValues(block: Blockly.BlockSvg): Map<string, unknown>;
|
|
54
|
+
export declare function restoreFieldValues(block: Blockly.BlockSvg, values: Map<string, unknown>): void;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type * as Blockly from "blockly";
|
|
2
|
+
import type { MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodeGenerationResult, MorphicHighlightDefinition, MorphicPlaceholderRange } from "./types";
|
|
3
|
+
/** A single span of 1-based lines. */
|
|
4
|
+
export interface LineSpan {
|
|
5
|
+
fromLine: number;
|
|
6
|
+
toLine: number;
|
|
7
|
+
}
|
|
8
|
+
/** One or more line spans to highlight, or `null` to clear. */
|
|
9
|
+
export type HighlightRange = LineSpan | LineSpan[] | null;
|
|
10
|
+
/** Drop-indicator position relative to a line. */
|
|
11
|
+
export interface DropIndicator {
|
|
12
|
+
line: number;
|
|
13
|
+
position: "above" | "below";
|
|
14
|
+
}
|
|
15
|
+
/** Data-transfer key carrying a Blockly block id during a codespace drag. */
|
|
16
|
+
export declare const BLOCK_ID_DRAG_KEY = "morphic/block-id";
|
|
17
|
+
export declare function getActiveGripDragSourceId(): string | undefined;
|
|
18
|
+
/** Mark a block id as the active grip-drag source (e.g. from a placeholder-range drag). */
|
|
19
|
+
export declare function setActiveGripDragSourceId(id: string | undefined): void;
|
|
20
|
+
export declare class MorphicCodeEditor {
|
|
21
|
+
private editorView?;
|
|
22
|
+
private container;
|
|
23
|
+
private options;
|
|
24
|
+
private workspace;
|
|
25
|
+
private generateWithMetadata;
|
|
26
|
+
private visible;
|
|
27
|
+
private syncTimer?;
|
|
28
|
+
private changeListener?;
|
|
29
|
+
private lastCode;
|
|
30
|
+
/** Latest metadata from the most recent code generation. */
|
|
31
|
+
metadata: MorphicCodeGenerationResult["metadata"];
|
|
32
|
+
/**
|
|
33
|
+
* Latest placeholder ranges. Sorted by `start` ascending. Used to suppress
|
|
34
|
+
* block-line highlighting when the cursor lands inside a value-slot range
|
|
35
|
+
* (a placeholder is its own selectable target).
|
|
36
|
+
*/
|
|
37
|
+
private placeholders;
|
|
38
|
+
/** Read-only view of the latest placeholder ranges. */
|
|
39
|
+
getPlaceholders(): readonly MorphicPlaceholderRange[];
|
|
40
|
+
/**
|
|
41
|
+
* Find the placeholder that covers `pos`. End is inclusive so a click that
|
|
42
|
+
* lands just past a single-digit marker still resolves; when ranges nest
|
|
43
|
+
* (e.g. "0 < 0" outer with two inner "0" markers) the narrowest match wins
|
|
44
|
+
* so clicks resolve to the editable inner range. Returns `null` when the
|
|
45
|
+
* editor has placeholder markers disabled (e.g. the preview editor) so
|
|
46
|
+
* clicks fall through to the block-line selection path instead.
|
|
47
|
+
*/
|
|
48
|
+
private findPlaceholderAtPos;
|
|
49
|
+
/** DOM root of the active inline placeholder editor (input/select), if open. */
|
|
50
|
+
private placeholderEditorEl?;
|
|
51
|
+
private placeholderEditorScrollHandler?;
|
|
52
|
+
private placeholderEditorOutsideHandler?;
|
|
53
|
+
private closePlaceholderEditor;
|
|
54
|
+
/**
|
|
55
|
+
* Open an inline editor (input/select) over the placeholder range and call
|
|
56
|
+
* `onPlaceholderApply` when the user commits. Read-only CodeMirror stays
|
|
57
|
+
* read-only; this overlay is a separate DOM element positioned via
|
|
58
|
+
* `coordsAtPos`. Closed on Enter/blur (apply), Escape (cancel), or doc
|
|
59
|
+
* change / scroll.
|
|
60
|
+
*/
|
|
61
|
+
private openPlaceholderEditor;
|
|
62
|
+
/** Called when the user's cursor line changes in the editor. */
|
|
63
|
+
onCursorLine?: (line: number) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Called when the user clicks in the empty area below the last rendered
|
|
66
|
+
* line. CodeMirror snaps the cursor to end-of-doc on such clicks, which
|
|
67
|
+
* isn't enough to detect "user wants to deselect" — the cursor often lands
|
|
68
|
+
* on a line that's still inside the currently selected block's range.
|
|
69
|
+
*/
|
|
70
|
+
onEmptyClick?: () => void;
|
|
71
|
+
private cm?;
|
|
72
|
+
private themeCompartment?;
|
|
73
|
+
private syntaxHighlightCompartment?;
|
|
74
|
+
private highlightEffect?;
|
|
75
|
+
private highlightColor;
|
|
76
|
+
private metadataEffect?;
|
|
77
|
+
private placeholderEffect?;
|
|
78
|
+
private dropIndicatorEffect?;
|
|
79
|
+
private valueSlotHighlightEffect?;
|
|
80
|
+
private hoverHighlightEffect?;
|
|
81
|
+
private editableHoverHighlightEffect?;
|
|
82
|
+
private emptyClickListener?;
|
|
83
|
+
private disposed;
|
|
84
|
+
constructor(container: HTMLElement, workspace: Blockly.WorkspaceSvg, generateWithMetadata: () => MorphicCodeGenerationResult, options?: MorphicCodeEditorOptions);
|
|
85
|
+
mount(): Promise<void>;
|
|
86
|
+
private buildDeleteExtensions;
|
|
87
|
+
private buildGripExtensions;
|
|
88
|
+
private buildDropIndicatorExtensions;
|
|
89
|
+
/**
|
|
90
|
+
* Highlight the char range of the value slot a dragged block would land in.
|
|
91
|
+
* Distinct from `dropIndicator` (a horizontal line above/below a line) so the
|
|
92
|
+
* user can tell at a glance that the drop will be value-typed, not statement.
|
|
93
|
+
*/
|
|
94
|
+
private buildValueSlotHighlightExtensions;
|
|
95
|
+
/**
|
|
96
|
+
* Hover-time hint: when the mouse moves over a block in the codespace, the
|
|
97
|
+
* innermost block under the cursor gets a subtle background tint so the
|
|
98
|
+
* user can see what "click to edit" or "right-click drag" will target
|
|
99
|
+
* before they act. Driven from `setHoverHighlight` (called by MorphicBlocks
|
|
100
|
+
* on the codespace container's `mousemove`/`mouseleave`).
|
|
101
|
+
*/
|
|
102
|
+
private buildHoverHighlightExtensions;
|
|
103
|
+
/** Highlight the char range of the block currently under the mouse cursor. */
|
|
104
|
+
setHoverHighlight(range: {
|
|
105
|
+
from: number;
|
|
106
|
+
to: number;
|
|
107
|
+
} | null): void;
|
|
108
|
+
/**
|
|
109
|
+
* Editable-hover layer: a single outline around the innermost editable
|
|
110
|
+
* placeholder under the cursor. Mirrors the structure of the value-slot /
|
|
111
|
+
* hover layers above — separate from the placeholder marks themselves so
|
|
112
|
+
* we don't double-render outlines for nested wrappers.
|
|
113
|
+
*/
|
|
114
|
+
private buildEditableHoverHighlightExtensions;
|
|
115
|
+
/** Outline the innermost editable placeholder under the cursor (one only). */
|
|
116
|
+
setEditableHoverHighlight(range: {
|
|
117
|
+
from: number;
|
|
118
|
+
to: number;
|
|
119
|
+
} | null): void;
|
|
120
|
+
show(): void;
|
|
121
|
+
hide(): void;
|
|
122
|
+
isVisible(): boolean;
|
|
123
|
+
/** Highlight one or more ranges of 1-based lines in the editor. */
|
|
124
|
+
highlightLines(spans: LineSpan | LineSpan[]): void;
|
|
125
|
+
/** Remove all line highlights. */
|
|
126
|
+
clearHighlight(): void;
|
|
127
|
+
/** Update the highlight background colour. */
|
|
128
|
+
setHighlightColor(color: string): void;
|
|
129
|
+
/** Show a drop-position indicator above (or below) the given 1-based line. */
|
|
130
|
+
showDropIndicator(line: number, position?: "above" | "below"): void;
|
|
131
|
+
/** Hide the drop-position indicator if currently shown. */
|
|
132
|
+
hideDropIndicator(): void;
|
|
133
|
+
/** Outline the char range of the value slot the drop would land in. */
|
|
134
|
+
showValueSlotHighlight(from: number, to: number): void;
|
|
135
|
+
/** Clear the value-slot highlight if currently shown. */
|
|
136
|
+
hideValueSlotHighlight(): void;
|
|
137
|
+
/** Return the 1-based line number at the given client coordinates, or null. */
|
|
138
|
+
getLineAtCoords(x: number, y: number): number | null;
|
|
139
|
+
/** Return the 0-based char offset at the given client coordinates, or null. */
|
|
140
|
+
charAtCoords(x: number, y: number): number | null;
|
|
141
|
+
/** Total line count of the editor's current document. */
|
|
142
|
+
getLineCount(): number;
|
|
143
|
+
/** Whether the given clientY is past the bottom of the last rendered line. */
|
|
144
|
+
isBelowLastLine(clientY: number): boolean;
|
|
145
|
+
/** Whether clientY is in the lower half of the given 1-based line's bbox. */
|
|
146
|
+
isInLowerHalfOfLine(line: number, clientY: number): boolean;
|
|
147
|
+
setTheme(theme: MorphicCodeEditorTheme): void;
|
|
148
|
+
/**
|
|
149
|
+
* Swap the active token-highlight rules. Pass `undefined` to clear. Used by
|
|
150
|
+
* MorphicBlocks on `setModes()` so the codespace and preview re-tokenize for
|
|
151
|
+
* the new mode's source element.
|
|
152
|
+
*/
|
|
153
|
+
setHighlightRules(rules: MorphicHighlightDefinition | undefined): void;
|
|
154
|
+
dispose(): void;
|
|
155
|
+
/** Force a regeneration now (e.g. after the underlying template source changes). */
|
|
156
|
+
refresh(): void;
|
|
157
|
+
/** Current editor text. Empty string when the editor is not mounted yet. */
|
|
158
|
+
getValue(): string;
|
|
159
|
+
/** 1-based line number under the cursor. Returns 1 when no cursor / not mounted. */
|
|
160
|
+
getCursorLine(): number;
|
|
161
|
+
/**
|
|
162
|
+
* Adjust the editor's zoom by reconfiguring the theme compartment with a
|
|
163
|
+
* scaled font-size. `"fit"` resets to the theme's configured default.
|
|
164
|
+
* One step = 1.2× / 0.83× — matches CodeMirror's natural rhythm.
|
|
165
|
+
*/
|
|
166
|
+
adjustZoom(direction: "in" | "out" | "fit"): void;
|
|
167
|
+
private zoomFactor;
|
|
168
|
+
private syncNow;
|
|
169
|
+
private scheduleDebouncedSync;
|
|
170
|
+
private attachSyncListener;
|
|
171
|
+
private detachSyncListener;
|
|
172
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import * as Blockly from "blockly";
|
|
2
|
+
import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicCodeGenerationResult, MorphicJavaScriptConfig } from "./types";
|
|
3
|
+
export declare function generateJavaScriptFromWorkspace(workspace: Blockly.WorkspaceSvg, definitions: ReadonlyMap<string, MorphicBlockDefinition>, behaviors: MorphicBehaviorMap, config?: MorphicJavaScriptConfig): string;
|
|
4
|
+
export declare function generateJavaScriptWithMetadataFromWorkspace(workspace: Blockly.WorkspaceSvg, definitions: ReadonlyMap<string, MorphicBlockDefinition>, behaviors: MorphicBehaviorMap, config?: MorphicJavaScriptConfig): MorphicCodeGenerationResult;
|