morphic-blocks 0.4.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.
@@ -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;
@@ -113,6 +113,16 @@ export interface MorphicCodeElementConfig {
113
113
  * `default` is used when no key fits; without it nothing is written.
114
114
  */
115
115
  types?: Record<string, string>;
116
+ /**
117
+ * The order of this language's operators, by the sign as it is written:
118
+ * `{ "**": { "rank": 80, "direction": "right" }, "//": 60 }`. Text views
119
+ * write brackets only where the order needs them, and `loadText()` reads
120
+ * `2 + 3 * 4` as the language means it. The common signs have an order
121
+ * without this setting (see the docs); an entry here changes one sign,
122
+ * `null` takes a sign's order away, and `false` turns the order off, so
123
+ * every value built from others is written in brackets.
124
+ */
125
+ operators?: false | Record<string, MorphicOperatorOrder | null>;
116
126
  /** Token highlighting for the codespace and preview showing this element. */
117
127
  highlighting?: MorphicHighlightDefinition;
118
128
  /**
@@ -121,6 +131,21 @@ export interface MorphicCodeElementConfig {
121
131
  */
122
132
  values?: MorphicValueFormat;
123
133
  }
134
+ /**
135
+ * Where an operator stands among the others: a rank (the higher, the tighter
136
+ * it binds), or the rank with more about it. `direction` says how the same
137
+ * rank reads without brackets: from the `"left"` (`10 - 4 - 3`, the default),
138
+ * from the `"right"` (`2 ** 3 ** 2`), or not at all (`"none"`: `1 < x < 5`
139
+ * needs brackets). `"any"` is for a sign whose grouping does not matter
140
+ * (`out << a << b`): no brackets are written between two of it, and text
141
+ * reads from the left. `prefix` is the rank of the sign in front of one
142
+ * value (`-x`, `not x`).
143
+ */
144
+ export type MorphicOperatorOrder = number | {
145
+ rank?: number;
146
+ direction?: "left" | "right" | "none" | "any";
147
+ prefix?: number;
148
+ };
124
149
  /**
125
150
  * Printed values in a language's own spelling, e.g. Python:
126
151
  * `{ "true": "True", "false": "False", "null": "None", "list": { "quote": "'" } }`.
@@ -204,9 +229,9 @@ export type MorphicDropdownImage = ({
204
229
  };
205
230
  /**
206
231
  * A field declared directly on a block (rendered at the `%FIELDNAME` token
207
- * position). Lets dropdown / text / number / checkbox fields live in the
208
- * definitions file instead of requiring a behavior's `onViewApplied`. Fields
209
- * outside these four (variables, colour, plugin/custom) still use
232
+ * position). Lets dropdown / reference / text / number / checkbox fields live
233
+ * in the definitions file instead of requiring a behavior's `onViewApplied`.
234
+ * Fields outside these (Blockly's variables, colour, plugin/custom) still use
210
235
  * `onViewApplied` — the `%FIELDNAME` token is skipped when no declaration
211
236
  * exists, leaving the behavior to supply the field.
212
237
  */
@@ -214,6 +239,22 @@ export type MorphicFieldDefinition = {
214
239
  type: "dropdown";
215
240
  options: MorphicDropdownOption[];
216
241
  default?: string;
242
+ }
243
+ /**
244
+ * A dropdown of the names the program declares: `to` names a block and one
245
+ * of its fields (`"class_define.NAME"`, or a list of them), and the field
246
+ * lists what that field holds on every such block. Renaming a declaration
247
+ * renames the references to it. `default` is the name a new block starts
248
+ * with; in a program that declares other names it takes the first of them.
249
+ * `scope` names a block (or several): the field then lists only what is
250
+ * declared inside the nearest such block around it, e.g. the fields of the
251
+ * class a method is in.
252
+ */
253
+ | {
254
+ type: "reference";
255
+ to: string | string[];
256
+ default?: string;
257
+ scope?: string | string[];
217
258
  } | {
218
259
  type: "text";
219
260
  default?: string;
@@ -227,10 +268,33 @@ export type MorphicFieldDefinition = {
227
268
  type: "checkbox";
228
269
  default?: boolean;
229
270
  };
271
+ /** How a repeating slot behaves. */
272
+ export interface MorphicRepeatConfig {
273
+ /**
274
+ * What is written between two entries: one text for every element, or a
275
+ * text per element name (`{ "python": ", ", "concept": " and " }`). `, ` by default.
276
+ */
277
+ separator?: string | Record<string, string>;
278
+ /** How many entries a new block starts with (1 by default). */
279
+ count?: number;
280
+ }
230
281
  export interface MorphicInputSlotDefinition {
231
282
  kind?: MorphicInputKind;
232
283
  name?: string;
233
284
  check?: string | string[];
285
+ /**
286
+ * Take only blocks whose check matches `check`. Without it, a block that has
287
+ * no check fits every slot (Blockly's rule); with it, such a block is
288
+ * refused, here and below the blocks already in the slot. Needs a `check`.
289
+ */
290
+ strict?: boolean;
291
+ /**
292
+ * Let the slot hold any number of values, e.g. the parameters of a function
293
+ * or the items of a list. The template names the slot once (`def f(%1):`);
294
+ * the entries are written one after the other with `separator` between
295
+ * them. Value slots only.
296
+ */
297
+ repeat?: MorphicRepeatConfig;
234
298
  align?: MorphicInputAlign;
235
299
  label?: string;
236
300
  /**
@@ -268,7 +332,27 @@ export interface MorphicPresetDefinition {
268
332
  * out keep what they show.
269
333
  */
270
334
  views: Record<string, string>;
335
+ /**
336
+ * The block set whose blocks the toolbox shows. Without it, the toolbox
337
+ * shows all of its blocks.
338
+ */
339
+ blockSet?: string;
340
+ /**
341
+ * Whether the toolbox groups its tiles under the categories. Default `true`;
342
+ * `false` shows them as one list, e.g. for a level with few blocks. Blocks
343
+ * keep their category colors either way.
344
+ */
345
+ categories?: boolean;
271
346
  }
347
+ /**
348
+ * Blocks for the toolbox, by name in `blockSets`: a list of block
349
+ * identifiers, or another set with blocks added at the end and removed.
350
+ */
351
+ export type MorphicBlockSet = string[] | {
352
+ extends: string;
353
+ add?: string[];
354
+ remove?: string[];
355
+ };
272
356
  /** Kinds of view: blocks, or the program as text. */
273
357
  export type MorphicViewKind = "workspace" | "codespace";
274
358
  /** A view in the definitions' `views`, by name. A bare kind is the short form. */
@@ -336,6 +420,13 @@ export interface MorphicBlockDefinition {
336
420
  shape?: MorphicBlockShape;
337
421
  previousStatement?: MorphicConnectionSpec;
338
422
  nextStatement?: MorphicConnectionSpec;
423
+ /**
424
+ * Let the block go only where the place above it (a slot, or the block it
425
+ * hangs below) has a check matching its own `previousStatement` or `output`
426
+ * check. Without it, a block with a check still fits every place that has
427
+ * none, so a class field could be dropped into ordinary code. Needs a check.
428
+ */
429
+ strict?: boolean;
339
430
  inputsInline?: boolean;
340
431
  tooltip?: string;
341
432
  helpUrl?: string;
@@ -411,6 +502,8 @@ export interface MorphicBlocksFormat {
411
502
  views?: Record<string, MorphicViewKind | MorphicViewDeclaration>;
412
503
  /** Named per-view mode configurations. Used as the mount default for `presets`. */
413
504
  presets?: MorphicPresetDefinition[];
505
+ /** Named lists of blocks for the toolbox, used by presets and `show({ blockSet })`. */
506
+ blockSets?: Record<string, MorphicBlockSet>;
414
507
  /**
415
508
  * How each code element's language is written, keyed by element name:
416
509
  * string quotes, empty slot defaults and highlighting.
@@ -468,6 +561,12 @@ export interface MorphicBehaviorProxy {
468
561
  * Use these where the value is emitted as a string (e.g. a text block).
469
562
  */
470
563
  quoted: Record<string, string>;
564
+ /**
565
+ * The generated code of each filled entry of a repeating slot, keyed by the
566
+ * slot's name: `proxy.lists.PARAMS` is `["a", "b"]`. `proxy.inputs.PARAMS`
567
+ * holds the same entries joined with `, `.
568
+ */
569
+ lists: Record<string, string[]>;
471
570
  }
472
571
  export type MorphicCodeBehavior = (proxy: MorphicBehaviorProxy) => string;
473
572
  export interface MorphicBlockBehavior {
@@ -500,7 +599,10 @@ export interface MorphicToolboxDisplayOptions {
500
599
  layout?: "stacked" | "tabs";
501
600
  /** A heading with the category's name above its tiles. Default `true`. */
502
601
  categoryHeadings?: boolean;
503
- /** A click on a heading collapses or expands its category. Default `true`. */
602
+ /**
603
+ * A click on a heading collapses or expands its category. Default `true`.
604
+ * Without headings, categories stay open.
605
+ */
504
606
  collapsible?: boolean;
505
607
  /**
506
608
  * Categories collapsed at the start: `true` for all, or their names.
@@ -646,6 +748,20 @@ export interface MorphicMountConfig {
646
748
  * them in the app's own development mode.
647
749
  */
648
750
  onWarning?: (message: string) => void;
751
+ /**
752
+ * Called when a value typed into a view is not taken (see
753
+ * `MorphicValueRejection`). The program is unchanged and the input stays
754
+ * open with the class `morphic-value-rejected`; what to tell the person
755
+ * is the app's to decide.
756
+ */
757
+ onValueRejected?: (rejected: MorphicRejectedValue) => void;
758
+ /**
759
+ * Where an image the definitions name is loaded from: a template's `<img>`,
760
+ * an image element's path, a dropdown picture. Return its address, or
761
+ * `undefined` to keep the path (next to the page), e.g. to show images an
762
+ * app keeps in memory: `assetUrl: (path) => blobUrls[path]`.
763
+ */
764
+ assetUrl?: (path: string) => string | undefined;
649
765
  toolbox?: MorphicToolboxConfig;
650
766
  toolboxLayout?: MorphicToolboxLayout;
651
767
  /**
@@ -748,6 +864,12 @@ export interface MorphicPlaceholderEditTarget {
748
864
  fieldType: "text" | "number" | "dropdown";
749
865
  /** Dropdown options as `[label, value]` pairs (only when `fieldType === "dropdown"`). */
750
866
  options?: [string, string][];
867
+ /**
868
+ * Set when the field's block is a whole value (a number, a name), in a
869
+ * slot or loose: its editor takes any value written as text, which then
870
+ * replaces the block.
871
+ */
872
+ value?: boolean;
751
873
  }
752
874
  /**
753
875
  * Range in the generated code occupied by a value slot. The codespace overlays
@@ -761,6 +883,8 @@ export interface MorphicPlaceholderRange {
761
883
  /** Exclusive 0-based character offset in `code`. */
762
884
  end: number;
763
885
  kind: "default" | "set";
886
+ /** Set when the range is a reference whose name nothing declares; text views mark it. */
887
+ missing?: boolean;
764
888
  /** Editing target when the slot contains an atomic single-field block. */
765
889
  edit?: MorphicPlaceholderEditTarget;
766
890
  /**
@@ -774,12 +898,65 @@ export interface MorphicPlaceholderRange {
774
898
  inputName: string;
775
899
  };
776
900
  }
901
+ /** What `loadText()` returns: how many blocks the text became, or the line that fits no block. */
902
+ export type MorphicTextLoadResult = {
903
+ ok: true;
904
+ blocks: number;
905
+ } | {
906
+ ok: false;
907
+ line: number;
908
+ message: string;
909
+ };
910
+ /**
911
+ * Why a typed value was not taken: no block writes the text (`"no-block"`),
912
+ * it names something nothing declares at that place (`"unknown-name"`), or
913
+ * the slot does not take the value (`"wrong-slot"`).
914
+ */
915
+ export type MorphicValueRejection = "no-block" | "unknown-name" | "wrong-slot";
916
+ /**
917
+ * What `loadValueText()` returns: how many blocks the text became, or why
918
+ * nothing changed. `name` is the unknown name; `message` says the reason in
919
+ * English, for logs or as a text to start from.
920
+ */
921
+ export type MorphicValueLoadResult = {
922
+ ok: true;
923
+ blocks: number;
924
+ } | {
925
+ ok: false;
926
+ reason: MorphicValueRejection;
927
+ name?: string;
928
+ message: string;
929
+ };
930
+ /** A typed value that was not taken, as `onValueRejected` gets it. */
931
+ export interface MorphicRejectedValue {
932
+ /** The view it was typed in. */
933
+ view: string;
934
+ /** The text as typed. */
935
+ text: string;
936
+ reason: MorphicValueRejection;
937
+ /** The name nothing declares, for `"unknown-name"`. */
938
+ name?: string;
939
+ /** The reason in English, for logs or as a text to start from. */
940
+ message: string;
941
+ }
942
+ /** Where the entries of a repeating slot end in generated text; text views put the slot's buttons there. */
943
+ export interface MorphicRepeatRange {
944
+ blockId: string;
945
+ /** The slot's name (the entries' inputs are `<name>_0`, `<name>_1`, …). */
946
+ slotName: string;
947
+ /** How many entries the slot has. */
948
+ count: number;
949
+ /** Character offset right after the last entry (or where the first would be). */
950
+ at: number;
951
+ }
777
952
  /** Result of `generateJavaScriptWithMetadata()`. */
778
953
  export interface MorphicCodeGenerationResult {
779
954
  code: string;
780
955
  metadata: MorphicCodeMetadata;
781
956
  /** Value-slot ranges with default/set classification. Empty if no value slots emitted. */
782
957
  placeholders: MorphicPlaceholderRange[];
958
+ /** Where each repeating slot's entries end in the text. Empty if the text has none. */
959
+ repeats?: MorphicRepeatRange[];
783
960
  /**
784
961
  * Line range (1-based, inclusive) of each group of connected blocks, a stack
785
962
  * in the workspace, in text order. Text views mark these lines with classes.
@@ -807,6 +984,19 @@ export interface MorphicCodeEditorTheme {
807
984
  gutterForeground?: string;
808
985
  /** Text selection highlight colour. */
809
986
  selectionBackground?: string;
987
+ /**
988
+ * Colour of the line that marks where dragged code would go
989
+ * (`morphic-drop-indicator-above` / `-below`). The app's CSS can set
990
+ * another look for these classes, or none.
991
+ */
992
+ dropIndicator?: string;
993
+ /**
994
+ * A CodeMirror theme (an extension, e.g. `oneDark` from
995
+ * `@codemirror/theme-one-dark`) for the editor's colours. The colours above
996
+ * then default to the theme's; any set here still win. Token colours stay
997
+ * with `highlighting`.
998
+ */
999
+ codemirror?: unknown;
810
1000
  }
811
1001
  /** Ways to delete blocks in a codespace, each on unless set to `false`. */
812
1002
  export interface MorphicCodespaceDeleteOptions {
@@ -816,6 +1006,8 @@ export interface MorphicCodespaceDeleteOptions {
816
1006
  keys?: boolean;
817
1007
  /** The toolbar button that deletes the blocks of the selected lines. */
818
1008
  toolbarButton?: boolean;
1009
+ /** Backspace in the emptied input of a value: the value leaves its slot, as when it is dragged out on blocks. */
1010
+ values?: boolean;
819
1011
  }
820
1012
  /** A view added with `engine.addView()`. */
821
1013
  export interface MorphicViewOptions {
@@ -883,11 +1075,12 @@ export interface MorphicCodeEditorOptions {
883
1075
  deleteKeys?: boolean;
884
1076
  /**
885
1077
  * If provided, a "grip" gutter (⋮⋮) appears on the start line of every block
886
- * id for which this returns `true`. Mousedown on the grip starts an HTML5
887
- * drag whose data transfer includes `morphic/block-id` set to the block id —
888
- * the host can then handle the drop (e.g. reorder).
1078
+ * id for which this returns `true`. A press on a grip goes to `onBlockGrab`,
1079
+ * and the host moves the block.
889
1080
  */
890
1081
  canDragBlock?: (blockId: string) => boolean;
1082
+ /** A grip was pressed, with the mouse's main button, a finger, or a pen. */
1083
+ onBlockGrab?: (blockId: string, press: PointerEvent, grip: HTMLElement) => void;
891
1084
  /**
892
1085
  * Token-level syntax highlighting rules. When set, the editor installs a
893
1086
  * CodeMirror `ViewPlugin` that emits `Decoration.mark` ranges for keywords,
@@ -900,7 +1093,25 @@ export interface MorphicCodeEditorOptions {
900
1093
  * write `newValue` to `edit.fieldName` on the Blockly block identified by
901
1094
  * `edit.blockId`. Re-codegen + re-render then refresh the codespace.
902
1095
  */
903
- onPlaceholderApply?: (edit: MorphicPlaceholderEditTarget, newValue: string) => void;
1096
+ /** `false` when the value was not taken; the editor then stays open. */
1097
+ onPlaceholderApply?: (edit: MorphicPlaceholderEditTarget, newValue: string) => boolean | void;
1098
+ /** A value typed into an empty slot; `false` when it was not taken. */
1099
+ onValueApply?: (slot: {
1100
+ parentBlockId: string;
1101
+ inputName: string;
1102
+ }, text: string) => boolean;
1103
+ /**
1104
+ * Backspace in the already empty input of a whole value: take the value
1105
+ * away. `false` when there was nothing to take away; the input then stays.
1106
+ */
1107
+ onValueRemove?: (edit: MorphicPlaceholderEditTarget) => boolean | void;
1108
+ /**
1109
+ * Called when a plus or minus button of a repeating slot is pressed, with
1110
+ * the block, the slot's name and +1 or -1. When set, the editor shows the
1111
+ * buttons after each repeating slot's last entry, on the line the pointer
1112
+ * is over and on selected lines.
1113
+ */
1114
+ onRepeat?: (blockId: string, slotName: string, delta: number) => void;
904
1115
  /**
905
1116
  * Whether value-slot underline markers are shown in this editor. The codespace
906
1117
  * uses them as a form-field affordance for inline editing; read-only display
@@ -1,4 +1,4 @@
1
- import type { MorphicBehaviorMap, MorphicBlockDefinition, MorphicCodeElementConfig, MorphicElementTypeEntry, MorphicModeDefinition, MorphicPresetDefinition, MorphicToolboxCategory } from "./types";
1
+ import type { MorphicBehaviorMap, MorphicBlockSet, MorphicBlockDefinition, MorphicCodeElementConfig, MorphicElementTypeEntry, MorphicModeDefinition, MorphicPresetDefinition, MorphicToolboxCategory } from "./types";
2
2
  /**
3
3
  * Inputs for a definitions validation pass. Everything is optional except the
4
4
  * definitions + elementTypes (available at construction); the cross-reference
@@ -14,6 +14,7 @@ export interface ValidateDefinitionsArgs {
14
14
  categories?: MorphicToolboxCategory[];
15
15
  /** Names of the views the developer declared (not the defaults). */
16
16
  views?: string[];
17
+ blockSets?: Record<string, MorphicBlockSet>;
17
18
  }
18
19
  export interface DefinitionValidationResult {
19
20
  /** Structural problems that guarantee wrong output — the caller should throw. */