morphic-blocks 0.0.1 → 0.1.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.
@@ -0,0 +1,51 @@
1
+ import * as Blockly from "blockly";
2
+ import type { MorphicCodeEditor } from "./code-editor";
3
+ import type { MorphicSelectionSyncOptions } from "./types";
4
+ /**
5
+ * Bidirectional selection sync between a Blockly workspace and one or more
6
+ * `MorphicCodeEditor` instances (code editor, codespace, preview).
7
+ *
8
+ * - Block → Code: selecting a block highlights its **own** lines (excluding
9
+ * children) in every attached editor. Each editor's spans are computed from
10
+ * its own metadata — the same block can map to different line ranges when
11
+ * editors render different languages.
12
+ * - Code → Block: moving the cursor in any editor selects the corresponding
13
+ * block and re-broadcasts highlights to the other editors.
14
+ *
15
+ * A guard flag prevents circular updates.
16
+ */
17
+ export declare class MorphicSelectionSync {
18
+ private workspace;
19
+ private editors;
20
+ private blockToCode;
21
+ private codeToBlock;
22
+ private blockListener?;
23
+ private cursorCallbacks;
24
+ private emptyClickCallbacks;
25
+ /** Prevents circular updates. */
26
+ private guard;
27
+ constructor(workspace: Blockly.WorkspaceSvg, editors: MorphicCodeEditor | MorphicCodeEditor[], options?: MorphicSelectionSyncOptions);
28
+ /** Start listening for selection events in both directions. */
29
+ enable(): void;
30
+ /** Stop listening and clear highlights / selection. */
31
+ disable(): void;
32
+ private attachBlockListener;
33
+ private detachBlockListener;
34
+ /** Update every editor's highlight for the given block (or clear if undefined). */
35
+ private broadcastHighlight;
36
+ private attachCursorListeners;
37
+ private detachCursorListeners;
38
+ /**
39
+ * Clear the workspace selection and every editor's line highlight. Public
40
+ * so consumers can reset selection state on view transitions (e.g. when
41
+ * switching between workspace and codespace presentations).
42
+ */
43
+ clearAll(): void;
44
+ /**
45
+ * Compute the line spans that belong to a block *excluding* its children
46
+ * within the given metadata.
47
+ */
48
+ private computeOwnSpans;
49
+ /** Scan metadata to find which block contains the given line. */
50
+ private findBlockAtLine;
51
+ }
@@ -0,0 +1,22 @@
1
+ import type { MorphicModeDefinition, MorphicModeStyle, MorphicModeName, MorphicStyleBundle, MorphicToolboxCategory } from "./types";
2
+ export declare class MorphicStyleManager {
3
+ private readonly loadedStyleKeys;
4
+ /**
5
+ * Injects visibility CSS derived from mode definitions.
6
+ * Hides all morphic elements by default, then shows only those listed
7
+ * in each mode's `elements` array. Developer CSS only needs to handle styling.
8
+ */
9
+ ensureModeVisibilityStyles(modes: MorphicModeDefinition[]): void;
10
+ ensureStyles(baseStyle: MorphicStyleBundle | undefined, modeStyles: MorphicModeStyle[]): void;
11
+ /**
12
+ * Inject the framework-shipped toolbar layout CSS once per document. The
13
+ * stylesheet itself lives at `toolbar.css`; we import it as a string via
14
+ * Vite's `?inline` query so consumers don't need to add a CSS import.
15
+ * Everything visual inherits from the parent by default; CSS variables let
16
+ * the host theme without overriding class selectors.
17
+ */
18
+ ensureToolbarStyles(): Promise<void>;
19
+ ensureCategoryStyles(categories: MorphicToolboxCategory[]): void;
20
+ validateModeCoverage(modeStyles: MorphicModeStyle[], definitionModes: MorphicModeName[]): void;
21
+ private ensureStyleBundle;
22
+ }
@@ -0,0 +1,14 @@
1
+ import type { MorphicHighlightDefinition } from "./types";
2
+ type Extension = import("@codemirror/state").Extension;
3
+ /**
4
+ * Build a set of CodeMirror extensions that token-highlight the visible
5
+ * viewport according to the given rules. Implementation deliberately uses a
6
+ * `ViewPlugin` + `Decoration.mark` instead of a `StreamLanguage` so we don't
7
+ * accidentally pull in language services (bracket matching, indent, etc.) —
8
+ * the codespace is a rendered view of the block model, not a freeform editor.
9
+ *
10
+ * Returns an empty array when `rules` is falsy, so callers can pass the
11
+ * result through a `Compartment` unconditionally.
12
+ */
13
+ export declare function buildHighlightExtensions(cmView: typeof import("@codemirror/view"), cmState: typeof import("@codemirror/state"), rules: MorphicHighlightDefinition | undefined): Extension[];
14
+ export {};
@@ -0,0 +1,16 @@
1
+ import * as Blockly from "blockly";
2
+ import type { MorphicBlockDefinition, MorphicCodeGenerationResult, MorphicElementTypeEntry, MorphicModeDefinition, MorphicModeName } from "./types";
3
+ /**
4
+ * Generates plain text for a workspace using the template that the active
5
+ * mode resolves to for each block, plus a block-id → line-range map.
6
+ *
7
+ * Authors control whitespace and indentation through the element content itself.
8
+ * Example:
9
+ * "if ( %1 ) { %2 }" → single-line output
10
+ * "if ( %1 ) {\n %2\n}" → multi-line, author adds newlines and indent
11
+ *
12
+ * Line numbers in the metadata are 1-based and inclusive. A block's range
13
+ * covers the first-line-emitted-from-its-template through the last; a block
14
+ * whose template wraps children (e.g. `if`) covers its children's lines too.
15
+ */
16
+ export declare function generateTextFromWorkspace(workspace: Blockly.Workspace, mode: MorphicModeName, definitions: ReadonlyMap<string, MorphicBlockDefinition>, elementTypes: Record<string, MorphicElementTypeEntry>, modeDefs: MorphicModeDefinition[], elementOverride?: string): MorphicCodeGenerationResult;
@@ -0,0 +1,45 @@
1
+ export interface MorphicImageToken {
2
+ kind: "image";
3
+ src: string;
4
+ alt: string;
5
+ width: number;
6
+ height: number;
7
+ }
8
+ export interface MorphicTextToken {
9
+ kind: "text";
10
+ value: string;
11
+ }
12
+ export interface MorphicPlaceholderToken {
13
+ kind: "placeholder";
14
+ index: number;
15
+ }
16
+ export interface MorphicFieldToken {
17
+ kind: "field";
18
+ name: string;
19
+ }
20
+ export type MorphicTemplateToken = MorphicImageToken | MorphicTextToken | MorphicPlaceholderToken | MorphicFieldToken;
21
+ export declare function parseTemplate(template: string): MorphicTemplateToken[];
22
+ export declare function normalizeTemplateText(text: string): string;
23
+ export declare function toModeClassToken(mode: string): string;
24
+ /**
25
+ * Converts parsed template tokens into an HTML string for toolbox canvas rendering.
26
+ * - Text tokens become escaped text nodes.
27
+ * - Image tokens become <img> elements.
28
+ * - Placeholder tokens become <span class="morphic-toolbox-slot" data-slot-index="N">.
29
+ */
30
+ export declare function renderTemplateAsHtml(tokens: MorphicTemplateToken[]): string;
31
+ /**
32
+ * Normalise the value of a `type: "image"` element so it always becomes an
33
+ * `<img>` tag. Behaviour:
34
+ * - Already-formed `<img …>` HTML → returned unchanged.
35
+ * - Recognised image path (.svg, .png, .jpg, .jpeg, .gif, .webp) → wrapped
36
+ * as `<img src="value" width="W" height="H">` with the supplied size.
37
+ * - Anything else → returned unchanged (treated as plain text downstream).
38
+ *
39
+ * Use the size from `resolveImageSize(elementTypes[name])` so per-element
40
+ * `size` overrides take effect.
41
+ */
42
+ export declare function normalizeImageValue(value: string, size: {
43
+ width: number;
44
+ height: number;
45
+ }): string;
@@ -0,0 +1,51 @@
1
+ import type { MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarItem, MorphicToolbarPane } from "./types";
2
+ export interface MorphicRunEventDetail {
3
+ code: string;
4
+ result: unknown;
5
+ error: Error | null;
6
+ }
7
+ export interface MorphicToolbarHandle {
8
+ refresh: () => void;
9
+ dispose: () => void;
10
+ pane: MorphicToolbarPane;
11
+ }
12
+ export declare const toolbarItems: {
13
+ /** "Mode: <name>" label for the pane's active mode. */
14
+ modeLabel(): MorphicToolbarItem;
15
+ /**
16
+ * Block-aware copy. Resolves the active block via selection-sync or cursor
17
+ * line, then copies it to the framework's internal clipboard. Mirrors the
18
+ * generated code text to the system clipboard as a best-effort.
19
+ */
20
+ copy(): MorphicToolbarItem;
21
+ paste(): MorphicToolbarItem;
22
+ undo(): MorphicToolbarItem;
23
+ redo(): MorphicToolbarItem;
24
+ zoomIn(): MorphicToolbarItem;
25
+ zoomOut(): MorphicToolbarItem;
26
+ zoomFit(): MorphicToolbarItem;
27
+ clear(): MorphicToolbarItem;
28
+ /**
29
+ * Run the workspace's generated JavaScript and dispatch a "morphic-run"
30
+ * event on the engine. Not in any default set — hosts typically wire a
31
+ * global Run button via `engine.runJavaScript()` instead. Kept here for
32
+ * cases where a toolbar Run button is desired.
33
+ */
34
+ runCode(): MorphicToolbarItem;
35
+ /** Serialize the workspace and download as JSON. Not in defaults. */
36
+ save(): MorphicToolbarItem;
37
+ /** Load a workspace JSON file via a file picker. Not in defaults. */
38
+ load(): MorphicToolbarItem;
39
+ /** Reset the workspace to empty. With confirm. Not in defaults. */
40
+ reset(): MorphicToolbarItem;
41
+ /**
42
+ * Toggle the preview pane's host container visibility. Not in defaults — the
43
+ * developer decides whether the user controls this. Looks for an element
44
+ * with `data-morphic-preview-host` and flips its `hidden` attribute.
45
+ */
46
+ togglePreview(): MorphicToolbarItem;
47
+ readOnlyBadge(): MorphicToolbarItem;
48
+ spacer(): MorphicToolbarItem;
49
+ defaultsFor(pane: MorphicToolbarPane): MorphicToolbarItem[];
50
+ };
51
+ export declare function renderToolbar(container: HTMLElement, config: MorphicToolbarConfig, ctx: MorphicToolbarCtx): MorphicToolbarHandle;
@@ -0,0 +1,43 @@
1
+ import * as Blockly from "blockly";
2
+ import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeDefinition, MorphicModeName, MorphicToolboxCanvasOptions } from "./types";
3
+ export declare const DRAG_DATA_KEY = "morphic/block-type";
4
+ export declare class MorphicToolboxCanvas {
5
+ private readonly container;
6
+ private readonly workspaceContainer;
7
+ private readonly workspace;
8
+ private readonly definitions;
9
+ private readonly blockColors;
10
+ private readonly behaviors;
11
+ private readonly elementTypes;
12
+ private readonly options;
13
+ private readonly modes;
14
+ private currentMode;
15
+ /** Per-element block/text override for code elements in tiles (from the active preset). */
16
+ private renderOverride?;
17
+ private previewWorkspace?;
18
+ private previewContainer?;
19
+ private readonly onDragOver;
20
+ private readonly onDrop;
21
+ constructor(params: {
22
+ container: HTMLElement;
23
+ workspaceContainer: HTMLElement;
24
+ workspace: Blockly.WorkspaceSvg;
25
+ definitions: Map<string, MorphicBlockDefinition>;
26
+ blockColors: Map<string, string>;
27
+ behaviors: MorphicBehaviorMap;
28
+ elementTypes?: Record<string, MorphicElementTypeEntry>;
29
+ mode: MorphicModeName;
30
+ render?: Record<string, "block" | "text">;
31
+ modes?: MorphicModeDefinition[];
32
+ options?: MorphicToolboxCanvasOptions;
33
+ });
34
+ rerender(mode: MorphicModeName, render?: Record<string, "block" | "text">): void;
35
+ dispose(): void;
36
+ private render;
37
+ private createTile;
38
+ private ensurePreviewWorkspace;
39
+ private createBlockPreviewSvg;
40
+ private createBlockAtPosition;
41
+ private resolveBlockIds;
42
+ private resolveCategoryBlockIds;
43
+ }
@@ -0,0 +1,3 @@
1
+ import type * as Blockly from "blockly";
2
+ import type { MorphicBlockDefinition, MorphicToolboxConfig } from "./types";
3
+ export declare function buildToolboxDefinition(toolbox: MorphicToolboxConfig, definitions: ReadonlyMap<string, MorphicBlockDefinition>): NonNullable<Blockly.BlocklyOptions["toolbox"]>;