morphic-blocks 0.5.0 → 0.6.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.
@@ -192,7 +192,8 @@
192
192
  "properties": {
193
193
  "lineButtons": { "type": "boolean" },
194
194
  "keys": { "type": "boolean" },
195
- "toolbarButton": { "type": "boolean" }
195
+ "toolbarButton": { "type": "boolean" },
196
+ "values": { "type": "boolean" }
196
197
  },
197
198
  "additionalProperties": false
198
199
  }
package/dist/index.d.ts CHANGED
@@ -11,4 +11,4 @@ export { checkDefinitions } from "./morphic/check-definitions";
11
11
  export { resolveBlockSets, type ResolvedBlockSets } from "./morphic/block-sets";
12
12
  export type { ValidateDefinitionsArgs, DefinitionValidationResult, } from "./morphic/validate-definitions";
13
13
  export type { MorphicResizeOptions, MorphicResizeHandle, } from "./morphic/resize";
14
- export type { MorphicElementType, MorphicElementTypeConfig, MorphicElementTypeEntry, MorphicEmptyDefaultConfig, MorphicHighlightDefinition, MorphicJavaScriptConfig, MorphicBehaviorContext, MorphicBehaviorDefinition, MorphicBehaviorMap, MorphicBehaviorProxy, MorphicBlockBehavior, MorphicBlockDefinition, MorphicBlockShape, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeElementConfig, MorphicOperatorOrder, MorphicValueFormat, MorphicViewHandle, MorphicViewDeclaration, MorphicViewKind, MorphicViewOptions, MorphicCodeBehavior, MorphicCodeBlockPosition, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodespaceDeleteOptions, MorphicCodeGenerationResult, MorphicCodeMetadata, MorphicConnectionSpec, MorphicDropdownImage, MorphicElementRenderContext, MorphicElementRenderer, MorphicDropdownOption, MorphicFieldDefinition, MorphicInputAlign, MorphicInputKind, MorphicInputSlotDefinition, MorphicModeName, MorphicMountConfig, MorphicPlaceholderEditTarget, MorphicPlaceholderRange, MorphicRepeatConfig, MorphicRepeatRange, MorphicTextLoadResult, MorphicRenderContext, MorphicResolvedView, MorphicStyleBundle, MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarDisplay, MorphicToolbarItem, MorphicToolbarRender, MorphicToolboxCanvasOptions, MorphicToolboxCategory, MorphicToolboxConfig, MorphicToolboxLayout, MorphicToolboxModeLabel, MorphicBlockElements, MorphicModeDefinition, MorphicTileRender, MorphicPresetDefinition, MorphicBlockSet, MorphicRunOutputLine, MorphicRunResult, MorphicSelectionSyncOptions, } from "./morphic/types";
14
+ export type { MorphicElementType, MorphicElementTypeConfig, MorphicElementTypeEntry, MorphicEmptyDefaultConfig, MorphicHighlightDefinition, MorphicJavaScriptConfig, MorphicBehaviorContext, MorphicBehaviorDefinition, MorphicBehaviorMap, MorphicBehaviorProxy, MorphicBlockBehavior, MorphicBlockDefinition, MorphicBlockShape, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeElementConfig, MorphicOperatorOrder, MorphicValueFormat, MorphicViewHandle, MorphicViewDeclaration, MorphicViewKind, MorphicViewOptions, MorphicCodeBehavior, MorphicCodeBlockPosition, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodespaceDeleteOptions, MorphicCodeGenerationResult, MorphicCodeMetadata, MorphicConnectionSpec, MorphicDropdownImage, MorphicElementRenderContext, MorphicElementRenderer, MorphicDropdownOption, MorphicFieldDefinition, MorphicInputAlign, MorphicInputKind, MorphicInputSlotDefinition, MorphicModeName, MorphicMountConfig, MorphicPlaceholderEditTarget, MorphicPlaceholderRange, MorphicRepeatConfig, MorphicRepeatRange, MorphicTextLoadResult, MorphicRejectedValue, MorphicValueLoadResult, MorphicValueRejection, MorphicRenderContext, MorphicResolvedView, MorphicStyleBundle, MorphicToolbarConfig, MorphicToolbarCtx, MorphicToolbarDisplay, MorphicToolbarItem, MorphicToolbarRender, MorphicToolboxCanvasOptions, MorphicToolboxCategory, MorphicToolboxConfig, MorphicToolboxLayout, MorphicToolboxModeLabel, MorphicBlockElements, MorphicModeDefinition, MorphicTileRender, MorphicPresetDefinition, MorphicBlockSet, MorphicRunOutputLine, MorphicRunResult, 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, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodespaceDeleteOptions, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicTextLoadResult, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions, MorphicViewHandle, MorphicViewOptions } from "./types";
3
+ import type { MorphicBehaviorMap, MorphicBlocksFormat, MorphicBlocksFormatJson, MorphicCodeEditorOptions, MorphicCodeEditorTheme, MorphicCodespaceDeleteOptions, MorphicCodeGenerationResult, MorphicModeName, MorphicMountConfig, MorphicTextLoadResult, MorphicValueLoadResult, MorphicPresetDefinition, MorphicRunResult, MorphicSelectionSyncOptions, MorphicToolbarConfig, MorphicToolboxCanvasOptions, MorphicViewHandle, MorphicViewOptions } from "./types";
4
4
  export declare class MorphicBlocks extends EventTarget {
5
5
  private readonly definitions;
6
6
  /** The blocks as loaded; each mount resolves their images (`assetUrl`) from these. */
@@ -25,6 +25,8 @@ export declare class MorphicBlocks extends EventTarget {
25
25
  private codeEditor?;
26
26
  /** Views added with `addView()`, beside the ones `mount()` sets up. */
27
27
  private readonly views;
28
+ /** Starts the move of a block whose grip was pressed, per codespace. */
29
+ private readonly codeGrabbers;
28
30
  /** The mode of each added workspace, read when its blocks are built. */
29
31
  private readonly viewWorkspaceElements;
30
32
  /** Numbers added views that are not given a name. */
@@ -274,9 +276,45 @@ export declare class MorphicBlocks extends EventTarget {
274
276
  loadText(text: string, options: {
275
277
  element: string;
276
278
  }): MorphicTextLoadResult;
279
+ /**
280
+ * Put the value that `text` writes in a code element into a value slot of
281
+ * a block: `engine.loadValueText("x + 1", { element: "python", block, slot: "TIMES" })`.
282
+ * The text becomes the blocks that write it (a name, a number, an operator
283
+ * with its parts) and takes the place of what the slot held; one undo
284
+ * brings that back. When no block writes the text, a name in it is declared
285
+ * nowhere the slot can see, or the slot does not take the value, nothing
286
+ * changes and the result says which.
287
+ *
288
+ * Without `slot`, `block` is a value itself and the text takes its place:
289
+ * in the slot it sits in, or, when it lies loose, where it lies (no slot
290
+ * decides then what fits, and a name counts where nothing surrounds it).
291
+ */
292
+ loadValueText(text: string, options: {
293
+ element: string;
294
+ block: Blockly.Block | string;
295
+ slot?: string;
296
+ }): MorphicValueLoadResult;
297
+ /** `loadValueText()`, or with `check` only its answer: nothing changes then. */
298
+ private readValueText;
299
+ /** Every name the program declares, as the text reader knows names: `<block>.<FIELD>=<name>`. */
300
+ private declaredNameKeys;
301
+ /**
302
+ * The first name in blocks read from text that nothing declares where they
303
+ * are meant to go: `inside` is the block whose slot takes them. Each
304
+ * reference is asked as its menu would be, scope included.
305
+ */
306
+ private unknownNameIn;
277
307
  generateJavaScriptWithMetadata(): MorphicCodeGenerationResult;
278
308
  mountCodeEditor(container: HTMLElement, options?: MorphicCodeEditorOptions): Promise<void>;
279
309
  private attachCodespaceDropTarget;
310
+ /** What a held tile shows over a codespace: the block as the view writes it, in the view's font. */
311
+ private tileTextLook;
312
+ /**
313
+ * Where the text of a block shows for a drop in a codespace: in the value's
314
+ * place, on the empty line of a body, or on a line of its own, indented
315
+ * like the block it goes beside.
316
+ */
317
+ private dropPreviewPlace;
280
318
  /** Innermost (smallest-range) block in metadata whose char range contains `charOffset`. */
281
319
  private findInnermostBlockAtChar;
282
320
  /**
@@ -411,9 +449,9 @@ export declare class MorphicBlocks extends EventTarget {
411
449
  */
412
450
  private renderWorkspaceView;
413
451
  /**
414
- * Tiles dropped on an editable added workspace, with the mouse or by touch,
415
- * add their block to the main workspace at the same spot; the view shows it
416
- * with its next copy. Returns the teardown.
452
+ * Tiles dragged to an added workspace show as their block there and, let
453
+ * go over an editable one, add it to the main workspace at the same spot;
454
+ * the view shows it with its next copy. Returns the teardown.
417
455
  */
418
456
  private attachWorkspaceTileDrop;
419
457
  /** Remove an added view and everything it set up. */
@@ -435,6 +473,34 @@ export declare class MorphicBlocks extends EventTarget {
435
473
  * `edit.blockId`. The change event listener will trigger codespace re-sync.
436
474
  */
437
475
  private applyPlaceholderEdit;
476
+ /**
477
+ * Where a value typed over a value block goes: the slot the block sits in,
478
+ * or, for a block that lies loose, the block's own place.
479
+ */
480
+ private valuePlaceOf;
481
+ /**
482
+ * Take the value an input in a codespace stands for away, as dragging it
483
+ * out of its slot does on blocks: the slot is empty again, or shows its own
484
+ * default. `false` when there is nothing to take away (a slot's default is
485
+ * no value of the program).
486
+ */
487
+ private removeValue;
488
+ /**
489
+ * Read a value typed in a view into its place (a slot, or where a loose
490
+ * value lies), as the view's element writes it. With `others`, text the
491
+ * view's element does not know is tried in the other code elements, in
492
+ * their order: blocks may show words (`Variable x`) where a person types
493
+ * the code they know (`x`). A value that is not taken changes nothing and
494
+ * goes to the app's `onValueRejected`; `false` then.
495
+ */
496
+ private typeValue;
497
+ /**
498
+ * What a workspace view does with text typed into a field: for the one
499
+ * field of a block that is a whole value (in a slot or loose), read it as a
500
+ * value for that place, as the view's element writes it, or else as another
501
+ * code element does. Nothing for any other field.
502
+ */
503
+ private typedValueTaker;
438
504
  /**
439
505
  * Whether a codespace grip may move the block: statement blocks
440
506
  * (previousConnection) and value blocks (outputConnection), so the
@@ -12,10 +12,42 @@ export interface DropIndicator {
12
12
  line: number;
13
13
  position: "above" | "below";
14
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";
15
+ /**
16
+ * Where the text of a block that is dragged in is shown, as it would stand
17
+ * after the drop: on a line of its own above or below a line, indented like
18
+ * `indentLine` (the first line of the block it goes beside); on the empty
19
+ * line of a body, which holds the body's indentation; or in a value's place.
20
+ */
21
+ export type DropPreviewPlace = {
22
+ kind: "line";
23
+ line: number;
24
+ position: "above" | "below";
25
+ indentLine?: number;
26
+ } | {
27
+ kind: "fill";
28
+ line: number;
29
+ } | {
30
+ kind: "value";
31
+ from: number;
32
+ to: number;
33
+ };
34
+ /**
35
+ * The place code is dragged away from: the lines of a block that has lines of
36
+ * its own (1-based, inclusive), or the characters of a value inside a line.
37
+ */
38
+ export type DragSource = {
39
+ lines: {
40
+ from: number;
41
+ to: number;
42
+ };
43
+ } | {
44
+ range: {
45
+ from: number;
46
+ to: number;
47
+ };
48
+ };
17
49
  export declare function getActiveGripDragSourceId(): string | undefined;
18
- /** Mark a block id as the active grip-drag source (e.g. from a placeholder-range drag). */
50
+ /** Say which block is moved, or that none is any more. */
19
51
  export declare function setActiveGripDragSourceId(id: string | undefined): void;
20
52
  export declare class MorphicCodeEditor {
21
53
  /** Use JavaScript language support; only the code editor does. */
@@ -50,6 +82,7 @@ export declare class MorphicCodeEditor {
50
82
  private findPlaceholderAtPos;
51
83
  /** DOM root of the active inline placeholder editor (input/select), if open. */
52
84
  private placeholderEditorEl?;
85
+ private placeholderEditorList?;
53
86
  private placeholderEditorScrollHandler?;
54
87
  private placeholderEditorOutsideHandler?;
55
88
  private closePlaceholderEditor;
@@ -59,6 +92,15 @@ export declare class MorphicCodeEditor {
59
92
  * read-only; this overlay is a separate DOM element positioned via
60
93
  * `coordsAtPos`. Closed on Enter/blur (apply), Escape (cancel), or doc
61
94
  * change / scroll.
95
+ *
96
+ * A whole value (a number, a name) and an empty slot are typed as text:
97
+ * any value the view's language writes, `x + 1` as well as `42`. A value
98
+ * that is not taken stays in the input, marked with
99
+ * `morphic-value-rejected`, until it is changed or Escape closes it.
100
+ *
101
+ * An emptied input follows the blocks: Enter leaves a text empty, makes a
102
+ * number 0, and leaves a name as it was. Taking the value away is a second
103
+ * step, Backspace in the already empty input.
62
104
  */
63
105
  private openPlaceholderEditor;
64
106
  /** Called when the user's cursor line changes in the editor. */
@@ -82,6 +124,8 @@ export declare class MorphicCodeEditor {
82
124
  private placeholderEffect?;
83
125
  private repeatEffect?;
84
126
  private dropIndicatorEffect?;
127
+ private dropPreviewEffect?;
128
+ private dragSourceEffect?;
85
129
  private valueSlotHighlightEffect?;
86
130
  private hoverHighlightEffect?;
87
131
  /** Hands the code view the line ranges of the groups of blocks. */
@@ -96,6 +140,22 @@ export declare class MorphicCodeEditor {
96
140
  private buildDeleteExtensions;
97
141
  private buildGripExtensions;
98
142
  private buildDropIndicatorExtensions;
143
+ /**
144
+ * The text of a block that is dragged in, shown where it would stand: the
145
+ * lines after it move down and the rest of a line moves aside, so the view
146
+ * reads as it would after the drop. It is a widget, not text of the
147
+ * program, and goes with the next change.
148
+ */
149
+ private buildDropPreviewExtensions;
150
+ /**
151
+ * The place code is dragged away from. The lines of a block close up, with
152
+ * their line numbers and markers, so the code reads as it would without the
153
+ * block; a value inside a line fades. Both are classes on what is there
154
+ * (`morphic-drag-source`, `morphic-drag-source-value`), nothing is taken out
155
+ * of the page: the grip that is dragged has to stay in it for the drag to
156
+ * go on. Gone with the next change of the text.
157
+ */
158
+ private buildDragSourceExtensions;
99
159
  /**
100
160
  * Highlight the char range of the value slot a dragged block would land in.
101
161
  * Distinct from `dropIndicator` (a horizontal line above/below a line) so the
@@ -146,6 +206,17 @@ export declare class MorphicCodeEditor {
146
206
  showDropIndicator(line: number, position?: "above" | "below"): void;
147
207
  /** Hide the drop-position indicator if currently shown. */
148
208
  hideDropIndicator(): void;
209
+ /** Show the text of a block that is dragged in where it would stand after the drop. */
210
+ showDropPreview(text: string, place: DropPreviewPlace): void;
211
+ /**
212
+ * Whether a point of the page is on the text shown by `showDropPreview()`:
213
+ * in the band of lines it takes up.
214
+ */
215
+ isOnDropPreview(x: number, y: number): boolean;
216
+ /** Take the text shown by `showDropPreview()` away. */
217
+ hideDropPreview(): void;
218
+ /** Mark the place code is dragged away from, or take the mark away (`null`). */
219
+ setDragSource(source: DragSource | null): void;
149
220
  /** Outline the char range of the value slot the drop would land in. */
150
221
  showValueSlotHighlight(from: number, to: number): void;
151
222
  /** Clear the value-slot highlight if currently shown. */
@@ -59,6 +59,12 @@ export declare class MorphicReference extends MorphicDropdown {
59
59
  protected doClassValidation_(newValue?: string): string | null;
60
60
  protected doValueUpdate_(newValue: string): void;
61
61
  }
62
+ /**
63
+ * The names a reference field would list at a place where no block is yet:
64
+ * `inside` is the block the reference would sit in. What `declaredNames()`
65
+ * gives a reference that is on the workspace.
66
+ */
67
+ export declare function declaredNamesAt(workspace: Blockly.Workspace, to: string | string[], scope: string | string[] | undefined, inside: Blockly.Block | null): string[];
62
68
  /** Let the references of one edit keep the names they have, declared or not. */
63
69
  export declare function keepNamesOf(group: string): void;
64
70
  /**
@@ -51,6 +51,24 @@ export declare function ensureTileHighlightStyles(elementName: string, colors?:
51
51
  * select text or open the phone's menu. Apps with a sideways toolbox set
52
52
  * `touch-action: pan-x` on their tiles.
53
53
  */
54
+ /**
55
+ * How the text of a dragged tile looks over a codespace: as code, in the
56
+ * font the codespace hands over, coloured like the element. `:where()` gives
57
+ * the rules no weight, so any rule of the app wins.
58
+ */
59
+ export declare function ensureDragTextStyles(elementName: string, colors?: MorphicHighlightDefinition["colors"]): void;
60
+ /**
61
+ * A typed value that was not taken stays in its input, marked. `:where()`
62
+ * gives the rule no weight, so any rule of the app wins.
63
+ */
64
+ export declare function ensureRejectedValueStyles(): void;
65
+ /**
66
+ * Code that is dragged in a codespace: the line that marks its new place, in
67
+ * the colour of the view's theme, its old place, where its lines close up
68
+ * and a value fades, and the cursors of the grip. `:where()` gives the rules
69
+ * no weight, so any rule of the app wins.
70
+ */
71
+ export declare function ensureCodeDragStyles(): void;
54
72
  export declare function ensureTileTouchStyles(): void;
55
73
  /**
56
74
  * The toolbox's scroll hints: no scrollbar, and a fade at each edge where more
@@ -1,5 +1,5 @@
1
1
  import type * as Blockly from "blockly";
2
- import type { MorphicBlockDefinition, MorphicCodeElementConfig } from "./types";
2
+ import type { MorphicBlockDefinition, MorphicCodeElementConfig, MorphicInputSlotDefinition } from "./types";
3
3
  /**
4
4
  * Text to blocks: read the text of a code element back into the blocks that
5
5
  * write it. There is no parser per language. Each block's template is its
@@ -25,10 +25,35 @@ export type MorphicTextParseResult = {
25
25
  line: number;
26
26
  message: string;
27
27
  };
28
+ /** Where one typed value is meant to go: the slot, and the names the program declares. */
29
+ export interface MorphicValuePlace {
30
+ /** The slot's definition; without one, any value fits. */
31
+ slot?: MorphicInputSlotDefinition;
32
+ /** Every name the program declares, as `<block>.<FIELD>=<name>`. */
33
+ names: ReadonlySet<string>;
34
+ }
35
+ /** What reading one value gives: its block (with the blocks in it), or why the text is none. */
36
+ export type MorphicValueParseResult = {
37
+ ok: true;
38
+ block: BlockState;
39
+ blocks: number;
40
+ } | {
41
+ ok: false;
42
+ reason: "no-block" | "wrong-slot";
43
+ message: string;
44
+ };
28
45
  /**
29
46
  * Read `source`, written in the code element `element`, into blocks.
30
47
  * `settings` are the element's `code` settings: its keywords are no names,
31
48
  * its comment sign and empty statement are read as it writes them.
32
49
  */
33
50
  export declare function parseTextToBlocks(source: string, element: string, definitions: ReadonlyMap<string, MorphicBlockDefinition>, settings?: MorphicCodeElementConfig): MorphicTextParseResult;
51
+ /**
52
+ * Read `source` as one value written in the code element `element`, for a
53
+ * slot: `x + 1` gives the operator's block with a name and a number in it.
54
+ * The slot's check and strict rule decide what fits, as they do for a block
55
+ * dragged there, and the order of operators groups the parts. Brackets
56
+ * around the whole value are never needed: the blocks hold the structure.
57
+ */
58
+ export declare function parseValueToBlocks(source: string, element: string, definitions: ReadonlyMap<string, MorphicBlockDefinition>, settings: MorphicCodeElementConfig | undefined, place: MorphicValuePlace): MorphicValueParseResult;
34
59
  export {};
@@ -0,0 +1,160 @@
1
+ import * as Blockly from "blockly";
2
+ /**
3
+ * Tiles dragged to a workspace. The HTML toolbox drags its tiles itself, so
4
+ * Blockly's own drag, which shows where a block would go and connects it
5
+ * when it is let go near a connection, never runs. What it would do is done
6
+ * here: over a workspace the tile becomes the block, its place is shown as
7
+ * Blockly shows it, and the block let go goes there.
8
+ */
9
+ /** A code element a target shows as text, and the font it writes it in. */
10
+ export interface TileTextPreview {
11
+ element: string;
12
+ font?: string;
13
+ }
14
+ /** Where a dragged tile can land. */
15
+ export interface TileDropTarget {
16
+ element: HTMLElement;
17
+ /**
18
+ * What the tile turns into over the target, which is what the target
19
+ * shows: `"block"` when the target draws the block itself, or the code
20
+ * element the block is written in as text. Without it the tile stays as
21
+ * it is.
22
+ */
23
+ preview?: () => "block" | TileTextPreview | undefined;
24
+ /**
25
+ * Whether the target takes the tile. One that does not (a read only view)
26
+ * still shows what the tile would be there, and the toolbox marks the drag
27
+ * as refused (`morphic-drag-refused`). Without it the target takes the tile.
28
+ */
29
+ takes?: () => boolean;
30
+ /**
31
+ * The tile is over the target at this point. `text` is the block as the
32
+ * target's code element writes it, for a target that shows text. Such a
33
+ * target says whether it shows the text at the block's place: `true`, and
34
+ * the text beside the pointer is not shown as well; `false`, and the target
35
+ * takes the block, but not at this spot (`morphic-drag-no-place`).
36
+ */
37
+ over?(x: number, y: number, blockType: string, text?: string): boolean | void;
38
+ /** The tile left the target or the drag ended. */
39
+ leave?(): void;
40
+ drop(blockType: string, x: number, y: number): void;
41
+ }
42
+ /** A connection of a block and the one on the workspace it would take. */
43
+ export interface SnapCandidate {
44
+ local: Blockly.RenderedConnection;
45
+ neighbour: Blockly.RenderedConnection;
46
+ }
47
+ /**
48
+ * The connection in reach of a block lying loose on the workspace: the
49
+ * nearest one within Blockly's snap radius that the workspace's connection
50
+ * checker lets it take (so checks and strict slots decide, as in a drag).
51
+ * The block must be drawn; its connections have no place before.
52
+ */
53
+ export declare function nearestConnection(block: Blockly.BlockSvg): SnapCandidate | null;
54
+ /**
55
+ * A connection a block would take, told by its place in its block: the
56
+ * connections of a block come in the same order in every copy of the program,
57
+ * so a place found in one view holds in another.
58
+ */
59
+ export interface SnapPlace {
60
+ /** The block on the workspace. */
61
+ blockId: string;
62
+ /** Which of that block's connections. */
63
+ index: number;
64
+ /** Which of the new block's connections goes there. */
65
+ local: number;
66
+ }
67
+ /**
68
+ * Add a block to a workspace at a place (in workspace coordinates) and
69
+ * connect it: to `shown`, the connection a preview showed as its place, else
70
+ * to the nearest one in reach; without one it stays loose at the place.
71
+ * Adding and connecting are one undo step.
72
+ */
73
+ export declare function addBlockAt(workspace: Blockly.WorkspaceSvg, type: string, at: Blockly.utils.Coordinate, shown?: SnapPlace): Blockly.BlockSvg;
74
+ /** A point of the page in the coordinates of a workspace. */
75
+ export declare function workspacePoint(workspace: Blockly.WorkspaceSvg, clientX: number, clientY: number): Blockly.utils.Coordinate;
76
+ /**
77
+ * A layer for what follows the pointer in a drag. It stays inside `parent`,
78
+ * so the styles that reach the parent's content reach it too, but the browser
79
+ * draws it on top of the page (as a popover), where nothing around the parent
80
+ * cuts it off. `onTop` is false in a browser without that: the layer then
81
+ * stays where the parent's edge ends it.
82
+ */
83
+ export declare function openDragLayer(parent: HTMLElement): {
84
+ layer: HTMLElement;
85
+ onTop: boolean;
86
+ };
87
+ /**
88
+ * The state of a workspace without the blocks that only show a held tile:
89
+ * what a copy of the program is made from.
90
+ */
91
+ export declare function saveProgram(workspace: Blockly.Workspace): {
92
+ [key: string]: unknown;
93
+ };
94
+ /**
95
+ * What a dragged tile shows over a workspace: its block, drawn as the view
96
+ * draws blocks, under the pointer, and where it would go, shown as Blockly
97
+ * shows it in its own drags: a gray copy at the place while the blocks after
98
+ * it make room, or, where the block would take a value's place, that value
99
+ * faded. Nothing of it is part of the program: it is made with events off and
100
+ * Blockly never moves it, so it is no undo step, no copy or save of the
101
+ * program has it (`saveProgram`), and it goes when the tile leaves or is let
102
+ * go.
103
+ */
104
+ export declare class TileBlockPreview {
105
+ private readonly view;
106
+ private readonly program;
107
+ private block;
108
+ private type;
109
+ private previewer;
110
+ private shown;
111
+ private replaced;
112
+ private raised;
113
+ /**
114
+ * @param view The workspace the block is shown in.
115
+ * @param program The workspace that holds the program, when `view` is a copy of it.
116
+ */
117
+ constructor(view: Blockly.WorkspaceSvg, program?: () => Blockly.WorkspaceSvg | null | undefined);
118
+ /**
119
+ * Show the block of a Blockly type at a place, in workspace coordinates,
120
+ * and where it would go; `placed: false` shows the block alone, for a view
121
+ * that takes none.
122
+ */
123
+ show(type: string, at: Blockly.utils.Coordinate, placed?: boolean): void;
124
+ /** The connection shown as the block's place, for the block that is added in its stead. */
125
+ place(): SnapPlace | undefined;
126
+ /** Take the block and what shows its place away. */
127
+ hide(): void;
128
+ /**
129
+ * The block, not drawn yet. In a copy of the program it is made as the
130
+ * program makes it: the program gives a block its slot defaults, so the
131
+ * block is made there, off the record, and its state loaded into the view,
132
+ * which is how the view gets every block it shows.
133
+ */
134
+ private make;
135
+ /**
136
+ * Show the block on top of the page. Inside the workspace its edge cuts a
137
+ * block held near it, and the next view covers the rest. Blockly needs the
138
+ * block where it is, so what is shown on top is a copy of its drawing, in a
139
+ * layer inside the workspace (the view's styles reach it), and the block
140
+ * itself is hidden. Without a place on top of the page the block stays as
141
+ * it is, cut like a block Blockly drags.
142
+ */
143
+ private raise;
144
+ /** Keep the layer where the workspace is, and as far scrolled and zoomed. */
145
+ private alignLayer;
146
+ /**
147
+ * The place to show: the nearest connection, but the one shown stays until
148
+ * another is clearly nearer, as in a Blockly drag. Showing a place moves
149
+ * the blocks around it, and without this the place would flicker between two.
150
+ */
151
+ private placeFor;
152
+ private showPlace;
153
+ }
154
+ /**
155
+ * A workspace as a place for tiles. Over `view` the tile shows as its block;
156
+ * let go, the block is added to the program (the view itself, or the
157
+ * workspace a copy of the program forwards to) at the same place. A read
158
+ * only view shows the block and takes nothing.
159
+ */
160
+ export declare function workspaceDropTarget(element: HTMLElement, view: Blockly.WorkspaceSvg, program: () => Blockly.WorkspaceSvg | null | undefined, blocklyType: (blockType: string) => string): TileDropTarget;
@@ -1,18 +1,8 @@
1
1
  import * as Blockly from "blockly";
2
- import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeDefinition, MorphicCodeElementConfig, MorphicModeName, MorphicToolboxCanvasOptions } from "./types";
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
- }
2
+ import { type TileDropTarget } from "./tile-drop";
3
+ import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicElementTypeEntry, MorphicModeDefinition, MorphicCodeElementConfig, MorphicHighlightDefinition, MorphicModeName, MorphicToolboxCanvasOptions } from "./types";
4
+ /** Text as HTML with its tokens wrapped in the classes the codespace uses. */
5
+ export declare function highlightedHtml(text: string, rules: MorphicHighlightDefinition): string;
16
6
  export declare class MorphicToolboxCanvas {
17
7
  private readonly container;
18
8
  private readonly workspaceContainer;
@@ -30,10 +20,11 @@ export declare class MorphicToolboxCanvas {
30
20
  private previewContainer?;
31
21
  /** The preview workspace's own theme and font, before any tile font. */
32
22
  private previewBase?;
33
- private readonly onDragOver;
34
- private readonly onDrop;
35
23
  /** Targets besides the workspace, such as codespaces, for touch drags. */
36
24
  private readonly dropTargets;
25
+ private readonly workspaceTarget;
26
+ /** Ends the drag of a tile that is going on, without a drop. */
27
+ private endDrag;
37
28
  /** Names of the collapsed categories; kept across redraws. */
38
29
  private readonly collapsedCategories;
39
30
  /** Each drawn category's parts, to collapse or expand without a redraw. */
@@ -101,13 +92,32 @@ export declare class MorphicToolboxCanvas {
101
92
  * images by their alt text. The identifier when the mode shows nothing.
102
93
  */
103
94
  private tileName;
104
- /** The target under a point: a codespace, else the workspace. */
95
+ /** The target under a point: a codespace or an added workspace, else the workspace. */
105
96
  private dropTargetAt;
106
97
  /**
107
- * Drag a tile with a finger or pen: a copy of the tile follows the pointer
108
- * and the block is made where it is let go.
98
+ * The target for a dragged tile at a point, and the point in it. A view
99
+ * that was just left stays the target a little past its edge: views lie
100
+ * side by side with a divider between them, and there the tile would show
101
+ * as a tile again for a moment, or flicker when it is held on the line.
102
+ * Over the toolbox, or over another view, the old one is left at once.
103
+ */
104
+ private heldTargetAt;
105
+ /**
106
+ * Drag a tile: a copy of the tile follows the pointer and the block is made
107
+ * where it is let go. The mouse drags once it moves; a finger or pen once it
108
+ * moves sideways or is held, since up and down scrolls the toolbox.
109
+ */
110
+ private attachDrag;
111
+ /**
112
+ * The layer a dragged tile is shown in. It stays inside the toolbox, so the
113
+ * toolbox's and the mode's styles reach the copy, but the browser draws it
114
+ * on top of the page (as a popover), where nothing around the toolbox cuts
115
+ * it off. A browser without that keeps it in the toolbox, which then drops
116
+ * the fade at its edges for as long as the drag lasts.
109
117
  */
110
- private attachTouchDrag;
118
+ private createDragLayer;
119
+ /** The block as a code element writes it, as what follows the pointer over a codespace. */
120
+ private createDragText;
111
121
  private ensurePreviewWorkspace;
112
122
  /**
113
123
  * The tile shows a copy of the rendered block outside Blockly, so its text
@@ -125,7 +135,6 @@ export declare class MorphicToolboxCanvas {
125
135
  */
126
136
  private createCodeText;
127
137
  private createBlockPreviewSvg;
128
- private createBlockAtPosition;
129
138
  private resolveBlockIds;
130
139
  private resolveCategoryBlockIds;
131
140
  }
@@ -0,0 +1,23 @@
1
+ import * as Blockly from "blockly";
2
+ /**
3
+ * Typed values on blocks. A number in a slot is a field that takes numbers
4
+ * only; to put `x + 1` there, a person would have to drag three blocks. Here
5
+ * the number's editor takes any text: a number changes the field as before,
6
+ * anything else is handed on to be read as a value that replaces the block.
7
+ */
8
+ /**
9
+ * Hands a typed text on for the value a field's block is. `check` only asks
10
+ * whether the text would be taken (and tells the app when not). Returns
11
+ * `false` when it is not taken.
12
+ */
13
+ export type TypedValueTaker = (text: string, check: boolean) => boolean;
14
+ /** A number as a number field takes it. */
15
+ export declare const NUMBER_TEXT: RegExp;
16
+ /**
17
+ * Let the number fields of an editable workspace take typed values.
18
+ * `takerFor` says what to do with text typed into a field, or nothing where
19
+ * the field's block is no value in a slot. Fields are prepared when the
20
+ * workspace is pressed, which is before any editor opens, so Blockly's own
21
+ * number blocks take part like the app's. Returns the teardown.
22
+ */
23
+ export declare function watchTypedValues(workspace: Blockly.WorkspaceSvg, takerFor: (field: Blockly.Field) => TypedValueTaker | undefined): () => void;