morphic-blocks 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,4 +13,8 @@ import type { MorphicBlockDefinition, MorphicCodeGenerationResult, MorphicElemen
13
13
  * covers the first-line-emitted-from-its-template through the last; a block
14
14
  * whose template wraps children (e.g. `if`) covers its children's lines too.
15
15
  */
16
- export declare function generateTextFromWorkspace(workspace: Blockly.Workspace, mode: MorphicModeName, definitions: ReadonlyMap<string, MorphicBlockDefinition>, elementTypes: Record<string, MorphicElementTypeEntry>, modeDefs: MorphicModeDefinition[], elementOverride?: string): MorphicCodeGenerationResult;
16
+ export declare function generateTextFromWorkspace(workspace: Blockly.Workspace, mode: MorphicModeName, definitions: ReadonlyMap<string, MorphicBlockDefinition>, elementTypes: Record<string, MorphicElementTypeEntry>, modeDefs: MorphicModeDefinition[], elementOverride?: string,
17
+ /** `emptyStatements: false` leaves empty statement slots empty (toolbox tiles). */
18
+ options?: {
19
+ emptyStatements?: boolean;
20
+ }): MorphicCodeGenerationResult;
@@ -13,13 +13,23 @@ export interface MorphicPlaceholderToken {
13
13
  kind: "placeholder";
14
14
  index: number;
15
15
  }
16
+ /** `%1:type`: the name of the type of the value in slot 1, from the element's `code.types`. */
17
+ export interface MorphicValueTypeToken {
18
+ kind: "valueType";
19
+ index: number;
20
+ }
16
21
  export interface MorphicFieldToken {
17
22
  kind: "field";
18
23
  name: string;
19
24
  }
20
- export type MorphicTemplateToken = MorphicImageToken | MorphicTextToken | MorphicPlaceholderToken | MorphicFieldToken;
25
+ export type MorphicTemplateToken = MorphicImageToken | MorphicTextToken | MorphicPlaceholderToken | MorphicValueTypeToken | MorphicFieldToken;
21
26
  export declare function parseTemplate(template: string): MorphicTemplateToken[];
22
27
  export declare function normalizeTemplateText(text: string): string;
28
+ /**
29
+ * A name as a CSS class part. The name is kept as written (`icon-tiles`,
30
+ * `iconTiles`, `icon_tiles` all work), so the app's CSS uses the same name as
31
+ * its definitions; only characters a class cannot hold become `-`.
32
+ */
23
33
  export declare function toModeClassToken(mode: string): string;
24
34
  /**
25
35
  * Converts parsed template tokens into an HTML string for toolbox canvas rendering.
@@ -1,14 +1,17 @@
1
- import type { MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarItem, MorphicToolbarPane, MorphicRunResult } from "./types";
1
+ import type { MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarItem, MorphicViewKind, MorphicRunResult } from "./types";
2
2
  /** Detail of the `morphic-run` event: the same result `runJavaScript` returns. */
3
3
  export type MorphicRunEventDetail = MorphicRunResult;
4
4
  export interface MorphicToolbarHandle {
5
5
  refresh: () => void;
6
6
  dispose: () => void;
7
- pane: MorphicToolbarPane;
7
+ /** The kind of view the toolbar acts on. */
8
+ kind: MorphicViewKind;
9
+ /** The name of the view the toolbar acts on. */
10
+ view: string;
8
11
  }
9
12
  export declare const toolbarItems: {
10
- /** "Mode: <name>" label for the pane's active mode. */
11
- modeLabel(): MorphicToolbarItem;
13
+ /** The name of the element the view shows, e.g. "python". */
14
+ viewLabel(): MorphicToolbarItem;
12
15
  /**
13
16
  * Block-aware copy. Resolves the active block via selection-sync or cursor
14
17
  * line, then copies it to the framework's internal clipboard. Mirrors the
@@ -21,6 +24,8 @@ export declare const toolbarItems: {
21
24
  zoomIn(): MorphicToolbarItem;
22
25
  zoomOut(): MorphicToolbarItem;
23
26
  zoomFit(): MorphicToolbarItem;
27
+ /** Delete the blocks of the lines selected in a codespace; disabled while none are. */
28
+ deleteSelection(): MorphicToolbarItem;
24
29
  clear(): MorphicToolbarItem;
25
30
  /**
26
31
  * Run the workspace's generated JavaScript and dispatch a "morphic-run"
@@ -35,14 +40,11 @@ export declare const toolbarItems: {
35
40
  load(): MorphicToolbarItem;
36
41
  /** Reset the workspace to empty. With confirm. Not in defaults. */
37
42
  reset(): MorphicToolbarItem;
38
- /**
39
- * Toggle the preview pane's host container visibility. Not in defaults — the
40
- * developer decides whether the user controls this. Looks for an element
41
- * with `data-morphic-preview-host` and flips its `hidden` attribute.
42
- */
43
- togglePreview(): MorphicToolbarItem;
44
43
  readOnlyBadge(): MorphicToolbarItem;
45
44
  spacer(): MorphicToolbarItem;
46
- defaultsFor(pane: MorphicToolbarPane): MorphicToolbarItem[];
45
+ /** Items for a view that shows the program but cannot change it. */
46
+ readOnlyDefaults(): MorphicToolbarItem[];
47
+ /** The default items of a view of this kind; a view that shows the program without changing it gets `readOnlyDefaults()`. */
48
+ defaultsFor(kind: MorphicViewKind, readOnly?: boolean): MorphicToolbarItem[];
47
49
  };
48
50
  export declare function renderToolbar(container: HTMLElement, config: MorphicToolbarConfig, ctx: MorphicToolbarCtx): MorphicToolbarHandle;
@@ -1,6 +1,18 @@
1
1
  import * as Blockly from "blockly";
2
- import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeDefinition, MorphicModeName, MorphicToolboxCanvasOptions } from "./types";
2
+ import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeDefinition, MorphicCodeElementConfig, MorphicModeName, MorphicToolboxCanvasOptions } from "./types";
3
3
  export declare const DRAG_DATA_KEY = "morphic/block-type";
4
+ /**
5
+ * Where a tile dragged by touch or pen can land. HTML drag and drop, which
6
+ * mouse drags use, does not start from a finger in most browsers.
7
+ */
8
+ export interface TileDropTarget {
9
+ element: HTMLElement;
10
+ /** The tile is over the target at this point. */
11
+ over?(x: number, y: number): void;
12
+ /** The tile left the target or the drag ended. */
13
+ leave?(): void;
14
+ drop(blockType: string, x: number, y: number): void;
15
+ }
4
16
  export declare class MorphicToolboxCanvas {
5
17
  private readonly container;
6
18
  private readonly workspaceContainer;
@@ -12,8 +24,7 @@ export declare class MorphicToolboxCanvas {
12
24
  private readonly options;
13
25
  private readonly modes;
14
26
  private currentMode;
15
- /** Per-element block/text override for code elements in tiles (from the active preset). */
16
- private renderOverride?;
27
+ private readonly code;
17
28
  private previewWorkspace?;
18
29
  private readonly onPreviewWorkspace?;
19
30
  private previewContainer?;
@@ -21,6 +32,18 @@ export declare class MorphicToolboxCanvas {
21
32
  private previewBase?;
22
33
  private readonly onDragOver;
23
34
  private readonly onDrop;
35
+ /** Targets besides the workspace, such as codespaces, for touch drags. */
36
+ private readonly dropTargets;
37
+ /** Names of the collapsed categories; kept across redraws. */
38
+ private readonly collapsedCategories;
39
+ /** Each drawn category's parts, to collapse or expand without a redraw. */
40
+ private categoryParts;
41
+ /** The category shown with the `"tabs"` layout; the first one until chosen. */
42
+ private activeCategory?;
43
+ private readonly onScroll;
44
+ private readonly resizeObserver;
45
+ /** Adds a tile's block when the tile is chosen with the keyboard. */
46
+ private readonly onChoose?;
24
47
  constructor(params: {
25
48
  container: HTMLElement;
26
49
  workspaceContainer: HTMLElement;
@@ -30,22 +53,69 @@ export declare class MorphicToolboxCanvas {
30
53
  behaviors: MorphicBehaviorMap;
31
54
  elementTypes?: Record<string, MorphicElementTypeEntry>;
32
55
  mode: MorphicModeName;
33
- render?: Record<string, "block" | "text">;
34
56
  modes?: MorphicModeDefinition[];
57
+ /** Code element settings; their highlighting colours code shown as text. */
58
+ code?: Record<string, MorphicCodeElementConfig>;
35
59
  options?: MorphicToolboxCanvasOptions;
36
60
  /** Called once with the hidden workspace used to draw block previews. */
37
61
  onPreviewWorkspace?: (workspace: Blockly.WorkspaceSvg) => void;
62
+ dropTargets?: () => TileDropTarget[];
63
+ onChoose?: (blockType: string) => void;
38
64
  });
39
- rerender(mode: MorphicModeName, render?: Record<string, "block" | "text">): void;
65
+ rerender(mode: MorphicModeName): void;
40
66
  dispose(): void;
67
+ /**
68
+ * Draw the tiles and publish the width of the widest block or code shown
69
+ * as text as `--morphic-toolbox-block-width` on the container. The layout
70
+ * stays the app's: one that wants every tile to fit gives the container
71
+ * `min-width: calc(var(--morphic-toolbox-block-width) + <padding>)` and
72
+ * lets its column grow to it (`minmax(250px, min-content)`). Only blocks and
73
+ * code count, neither of which wraps, so long descriptions do not widen it.
74
+ */
41
75
  private render;
76
+ /** Widest block or code text on a tile in the current render, in pixels. */
77
+ private widestBlock;
78
+ private renderTiles;
79
+ /** Collapse or expand a category, without drawing the tiles again. */
80
+ private toggleCategory;
81
+ /** Mark the container while tiles wait above or below its visible part. */
82
+ private showScrollHints;
83
+ /**
84
+ * Hide the tiles of collapsed categories and tell their headings; with
85
+ * tabs, show only the chosen category.
86
+ */
87
+ private showCategories;
88
+ private showCategoryState;
42
89
  private createTile;
90
+ /**
91
+ * What a screen reader calls the tile: what the tile shows, in the mode's
92
+ * order. Code is read as the codespace would write it, text as it is, and
93
+ * images by their alt text. The identifier when the mode shows nothing.
94
+ */
95
+ private tileName;
96
+ /** The target under a point: a codespace, else the workspace. */
97
+ private dropTargetAt;
98
+ /**
99
+ * Drag a tile with a finger or pen: a copy of the tile follows the pointer
100
+ * and the block is made where it is let go.
101
+ */
102
+ private attachTouchDrag;
43
103
  private ensurePreviewWorkspace;
44
104
  /**
45
105
  * The tile shows a copy of the rendered block outside Blockly, so its text
46
106
  * takes the font the tile's CSS gives that element. Measure with that font.
47
107
  */
48
108
  private syncPreviewFont;
109
+ /**
110
+ * A block for one code element of a tile, built like a workspace block,
111
+ * slot defaults included, in the hidden tile workspace. Not drawn yet.
112
+ */
113
+ private createPreviewBlock;
114
+ /**
115
+ * A code element shown as text reads exactly like the codespace would
116
+ * write the block: field values, slot defaults and all.
117
+ */
118
+ private createCodeText;
49
119
  private createBlockPreviewSvg;
50
120
  private createBlockAtPosition;
51
121
  private resolveBlockIds;