@kubohiroya/turbowarp-plus-editor-code-views 0.0.0-stage → 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.
Files changed (108) hide show
  1. package/LICENSE +373 -0
  2. package/README.ja.md +38 -0
  3. package/README.md +37 -2
  4. package/dist/asset-edit-session.d.ts +24 -0
  5. package/dist/asset-edit-session.js +58 -0
  6. package/dist/block-argument-text.d.ts +39 -0
  7. package/dist/block-argument-text.js +170 -0
  8. package/dist/block-drag.d.ts +17 -0
  9. package/dist/block-drag.js +184 -0
  10. package/dist/code-extensions.d.ts +38 -0
  11. package/dist/code-extensions.js +418 -0
  12. package/dist/code-pane.d.ts +63 -0
  13. package/dist/code-pane.js +363 -0
  14. package/dist/custom-extension-fields.d.ts +45 -0
  15. package/dist/custom-extension-fields.js +144 -0
  16. package/dist/detached-block-palette-view.d.ts +10 -0
  17. package/dist/detached-block-palette-view.js +62 -0
  18. package/dist/detached-block-palette.d.ts +41 -0
  19. package/dist/detached-block-palette.js +197 -0
  20. package/dist/dockview-unsaved.d.ts +45 -0
  21. package/dist/dockview-unsaved.js +123 -0
  22. package/dist/history.d.ts +57 -0
  23. package/dist/history.js +85 -0
  24. package/dist/index.d.ts +28 -0
  25. package/dist/index.js +729 -0
  26. package/dist/native-extension-field.d.ts +26 -0
  27. package/dist/native-extension-field.js +204 -0
  28. package/dist/native-field-svg.d.ts +20 -0
  29. package/dist/native-field-svg.js +156 -0
  30. package/dist/palette-category-reorder.d.ts +4 -0
  31. package/dist/palette-category-reorder.js +167 -0
  32. package/dist/palette-category-search.d.ts +26 -0
  33. package/dist/palette-category-search.js +74 -0
  34. package/dist/palette-dropdown-resize.d.ts +19 -0
  35. package/dist/palette-dropdown-resize.js +135 -0
  36. package/dist/parent-field-layout.d.ts +2 -0
  37. package/dist/parent-field-layout.js +90 -0
  38. package/dist/parent-field-mirror.d.ts +3 -0
  39. package/dist/parent-field-mirror.js +290 -0
  40. package/dist/parent-field-resources.d.ts +9 -0
  41. package/dist/parent-field-resources.js +199 -0
  42. package/dist/preview-chrome.d.ts +9 -0
  43. package/dist/preview-chrome.js +78 -0
  44. package/dist/procedure-editor.d.ts +9 -0
  45. package/dist/procedure-editor.js +122 -0
  46. package/dist/procedure-edits.d.ts +16 -0
  47. package/dist/procedure-edits.js +47 -0
  48. package/dist/programmatic-extension-field.d.ts +20 -0
  49. package/dist/programmatic-extension-field.js +69 -0
  50. package/dist/protocol.d.ts +15 -0
  51. package/dist/protocol.js +36 -0
  52. package/dist/script-layout-dialog.d.ts +10 -0
  53. package/dist/script-layout-dialog.js +93 -0
  54. package/dist/script-layout-view.d.ts +68 -0
  55. package/dist/script-layout-view.js +164 -0
  56. package/dist/script-layout-workspace.d.ts +45 -0
  57. package/dist/script-layout-workspace.js +55 -0
  58. package/dist/script-layout.d.ts +17 -0
  59. package/dist/script-layout.js +25 -0
  60. package/dist/script-view-menu.d.ts +46 -0
  61. package/dist/script-view-menu.js +161 -0
  62. package/dist/sprite-code-windows.d.ts +31 -0
  63. package/dist/sprite-code-windows.js +180 -0
  64. package/dist/stage-code-prewarm.d.ts +52 -0
  65. package/dist/stage-code-prewarm.js +216 -0
  66. package/dist/target-combobox.d.ts +10 -0
  67. package/dist/target-combobox.js +92 -0
  68. package/dist/variable-references.d.ts +1 -0
  69. package/dist/variable-references.js +87 -0
  70. package/dist/workspace.d.ts +103 -0
  71. package/dist/workspace.js +1102 -0
  72. package/package.json +100 -4
  73. package/src/asset-edit-session.ts +72 -0
  74. package/src/block-argument-text.ts +172 -0
  75. package/src/block-drag.ts +190 -0
  76. package/src/code-extensions.ts +394 -0
  77. package/src/code-pane.ts +353 -0
  78. package/src/custom-extension-fields.ts +154 -0
  79. package/src/detached-block-palette-view.tsx +97 -0
  80. package/src/detached-block-palette.ts +218 -0
  81. package/src/dockview-unsaved.ts +134 -0
  82. package/src/history.ts +114 -0
  83. package/src/index.ts +680 -0
  84. package/src/native-extension-field.ts +201 -0
  85. package/src/native-field-svg.ts +140 -0
  86. package/src/palette-category-reorder.ts +155 -0
  87. package/src/palette-category-search.ts +82 -0
  88. package/src/palette-dropdown-resize.ts +132 -0
  89. package/src/parent-field-layout.ts +88 -0
  90. package/src/parent-field-mirror.ts +260 -0
  91. package/src/parent-field-resources.ts +179 -0
  92. package/src/preview-chrome.ts +82 -0
  93. package/src/procedure-editor.ts +123 -0
  94. package/src/procedure-edits.ts +46 -0
  95. package/src/programmatic-extension-field.ts +76 -0
  96. package/src/protocol.ts +47 -0
  97. package/src/script-layout-dialog.ts +104 -0
  98. package/src/script-layout-view.ts +198 -0
  99. package/src/script-layout-workspace.ts +95 -0
  100. package/src/script-layout.ts +40 -0
  101. package/src/script-view-menu.ts +213 -0
  102. package/src/sprite-code-windows.tsx +182 -0
  103. package/src/stage-code-prewarm.ts +242 -0
  104. package/src/styles.css +595 -0
  105. package/src/target-combobox.tsx +106 -0
  106. package/src/variable-references.ts +85 -0
  107. package/src/workspace.ts +1124 -0
  108. package/tsconfig.json +23 -0
@@ -0,0 +1,418 @@
1
+ import { customField, getCustomFields, programmaticField, registerCustomFields, reserveAttachedNativeField } from "./custom-extension-fields.js";
2
+ import { openNativeField } from "./native-extension-field.js";
3
+ import { createFieldActionDialog, readFieldActionPanel, runFieldAction } from "./programmatic-extension-field.js";
4
+ export { createFieldActionDialog } from "./programmatic-extension-field.js";
5
+ const safeImage = (source) => typeof source === 'string' && /^(?:data:image\/|media:\/\/)/i.test(source);
6
+ function menuOptions(options) {
7
+ const valid = Array.isArray(options) ? options.filter(pair => Array.isArray(pair) && pair.length === 2 &&
8
+ (typeof pair[0] === 'string' || safeImage(pair[0]?.src)) && typeof pair[1] === 'string') : [];
9
+ return valid.length ? valid : [['', '']];
10
+ }
11
+ function projectDefinition(json, runtime, target) {
12
+ return JSON.parse(JSON.stringify(json, (key, value) => {
13
+ if (key === 'src')
14
+ return safeImage(value) ? value : '';
15
+ if (key === 'options') {
16
+ if (typeof value !== 'function')
17
+ return menuOptions(value);
18
+ // The VM's menu callbacks implicitly use the active editing target.
19
+ if (runtime.getEditingTarget() === target) {
20
+ try {
21
+ return menuOptions(value());
22
+ }
23
+ catch {
24
+ return menuOptions(null);
25
+ }
26
+ }
27
+ return menuOptions(null);
28
+ }
29
+ return typeof value === 'function' ? undefined : value;
30
+ }));
31
+ }
32
+ const dynamicTypes = new Set(['command', 'reporter', 'Boolean', 'hat', 'event']);
33
+ const isBranched = info => (info?.blockType === 'conditional' || info?.blockType === 'loop') &&
34
+ [undefined, 0, 1, 2].includes(info.branchCount) && (typeof info.text === 'string' ||
35
+ Array.isArray(info.text) && info.text.length > 0 && info.text.every(line => typeof line === 'string'));
36
+ const supportsDynamicInfo = info => isBranched(info) || (dynamicTypes.has(info?.blockType) &&
37
+ typeof info.text === 'string' && !info.branchCount);
38
+ function convertDynamicShape(runtime, category, opcode, info) {
39
+ return runtime._convertBlockForScratchBlocks({ ...info, opcode, isDynamic: true }, category).json;
40
+ }
41
+ export function attachDockedCodeExtensions({ vm, ScratchBlocks, document, locale, onError: onFieldError = (_error) => { } }) {
42
+ const { runtime } = vm;
43
+ const definitions = ScratchBlocks.Blocks;
44
+ const installed = new Map();
45
+ const fieldRegistrations = new Map();
46
+ let disposed = false;
47
+ const dialog = document && createFieldActionDialog(document);
48
+ let fieldRequest = 0;
49
+ let closeNativeEditor;
50
+ const invalidateFieldAction = () => {
51
+ fieldRequest++;
52
+ const close = closeNativeEditor;
53
+ closeNativeEditor = undefined;
54
+ close?.();
55
+ };
56
+ function openDockedNative(field, name) {
57
+ const block = field.sourceBlock_;
58
+ if (!document || disposed || !block || block.isInFlyout)
59
+ return;
60
+ invalidateFieldAction();
61
+ const request = fieldRequest;
62
+ const target = runtime.getEditingTarget();
63
+ const workspace = block.workspace;
64
+ const vmBlock = target?.blocks.getBlock(block.id);
65
+ const value = vmBlock?.fields?.[name]?.value;
66
+ const native = customField(runtime, block.type, name)?.native;
67
+ if (!native || typeof value !== 'string' || field.getValue() !== value)
68
+ return;
69
+ const sameTarget = () => vm.editingTarget === target && runtime.getEditingTarget() === target &&
70
+ runtime.getTargetById(target.id) === target;
71
+ const stillInstalled = () => sameTarget() && field.sourceBlock_ === block &&
72
+ workspace.getBlockById(block.id)?.getField(name) === field;
73
+ const current = () => !disposed && request === fieldRequest && stillInstalled() && document.hasFocus() &&
74
+ !workspace.getParentSvg().parentElement.inert && target.blocks.getBlock(block.id) === vmBlock &&
75
+ vmBlock.fields[name]?.value === value && customField(runtime, block.type, name)?.native === native;
76
+ if (!current() || !reserveAttachedNativeField(field, native))
77
+ return;
78
+ const setValue = field.setValue.bind(field);
79
+ closeNativeEditor = openNativeField({
80
+ blocks: ScratchBlocks, field, document, parentDocument: true, asyncCommit: native.asyncCommit === true,
81
+ valid: current,
82
+ commit(next) {
83
+ // Deliver the normal Blockly change before replacing the retired field.
84
+ setValue(next);
85
+ ScratchBlocks.Events.fireNow_();
86
+ },
87
+ onAttachedClose() {
88
+ queueMicrotask(() => {
89
+ if (!stillInstalled())
90
+ return;
91
+ ScratchBlocks.Events.fireNow_();
92
+ if (!stillInstalled())
93
+ return;
94
+ // The GUI clears the workspace history on refresh, even for the same target.
95
+ const undo = workspace.undoStack_.slice();
96
+ const redo = workspace.redoStack_.slice();
97
+ ScratchBlocks.Events.disable();
98
+ try {
99
+ vm.refreshWorkspace();
100
+ }
101
+ finally {
102
+ if (sameTarget()) {
103
+ workspace.undoStack_ = undo;
104
+ workspace.redoStack_ = redo;
105
+ }
106
+ ScratchBlocks.Events.enable();
107
+ }
108
+ });
109
+ },
110
+ onError: onFieldError
111
+ });
112
+ }
113
+ function openProgrammatic(field, name) {
114
+ const block = field.sourceBlock_;
115
+ if (!dialog || disposed || !block || block.isInFlyout)
116
+ return;
117
+ const editor = programmaticField(runtime, block.type, name);
118
+ if (!editor)
119
+ return;
120
+ const request = ++fieldRequest;
121
+ const target = runtime.getEditingTarget();
122
+ try {
123
+ const value = field.getValue();
124
+ const panel = readFieldActionPanel(editor, value);
125
+ const current = () => !disposed && request === fieldRequest && field.sourceBlock_ === block &&
126
+ field.getValue() === value && runtime.getEditingTarget() === target && document.hasFocus() &&
127
+ !block.workspace?.getParentSvg?.()?.parentElement?.inert &&
128
+ programmaticField(runtime, block.type, name) === editor;
129
+ if (panel)
130
+ dialog.show(panel, action => {
131
+ if (!current())
132
+ return;
133
+ runFieldAction(editor, value, action, next => {
134
+ if (current() && typeof next === 'string')
135
+ field.setValue(next);
136
+ }, error => {
137
+ if (current())
138
+ onFieldError(error);
139
+ });
140
+ }, locale);
141
+ }
142
+ catch (error) {
143
+ onFieldError(error);
144
+ }
145
+ }
146
+ function install(category) {
147
+ fieldRegistrations.get(category.id)?.();
148
+ const fields = getCustomFields(category);
149
+ if (fields.length) {
150
+ fieldRegistrations.set(category.id, registerCustomFields(ScratchBlocks, fields.map(({ declaration }) => declaration), openProgrammatic, name => fields.find(field => field.declaration.name === name)?.native, undefined, openDockedNative));
151
+ }
152
+ else
153
+ fieldRegistrations.delete(category.id);
154
+ for (const entry of category.blocks ?? []) {
155
+ if (!entry.info?.isDynamic)
156
+ continue;
157
+ const type = entry.json?.type;
158
+ const native = definitions[type];
159
+ if (!native?.domToMutation || installed.get(type)?.replacement === native)
160
+ continue;
161
+ const mutations = new WeakMap();
162
+ const replacement = { ...native,
163
+ domToMutation(mutation) {
164
+ let info;
165
+ let json;
166
+ try {
167
+ info = JSON.parse(mutation.getAttribute('blockInfo'));
168
+ if (isBranched(info)) {
169
+ json = convertDynamicShape(runtime, category, entry.info.opcode, info);
170
+ }
171
+ }
172
+ catch {
173
+ // Leave invalid metadata to the native definition.
174
+ }
175
+ if (!json)
176
+ return native.domToMutation.call(this, mutation);
177
+ // Native init supplies the docked theme. Apply the VM's layout and connections.
178
+ const { colour, colourSecondary, colourTertiary, extensions, ...layout } = json;
179
+ this.jsonInit(layout);
180
+ if (info.color1 || info.color2 || info.color3)
181
+ this.setColour(info.color1, info.color2, info.color3);
182
+ mutations.set(this, mutation.cloneNode(true));
183
+ },
184
+ mutationToDom() {
185
+ return mutations.get(this)?.cloneNode(true) ?? native.mutationToDom.call(this);
186
+ } };
187
+ definitions[type] = replacement;
188
+ installed.set(type, { native, replacement });
189
+ }
190
+ }
191
+ // The docked GUI registers its factory first and also replays existing categories on mount.
192
+ vm.on('EXTENSION_ADDED', install);
193
+ vm.on('BLOCKSINFO_UPDATE', install);
194
+ vm.on('workspaceUpdate', invalidateFieldAction);
195
+ document?.defaultView?.addEventListener('blur', invalidateFieldAction);
196
+ queueMicrotask(() => {
197
+ if (!disposed)
198
+ for (const category of runtime._blockInfo ?? [])
199
+ install(category);
200
+ });
201
+ return { dispose() {
202
+ disposed = true;
203
+ invalidateFieldAction();
204
+ dialog?.close();
205
+ vm.removeListener('EXTENSION_ADDED', install);
206
+ vm.removeListener('BLOCKSINFO_UPDATE', install);
207
+ vm.removeListener('workspaceUpdate', invalidateFieldAction);
208
+ document?.defaultView?.removeEventListener('blur', invalidateFieldAction);
209
+ for (const unregister of fieldRegistrations.values())
210
+ unregister();
211
+ for (const [type, { native, replacement }] of installed) {
212
+ if (definitions[type] === replacement)
213
+ definitions[type] = native;
214
+ }
215
+ } };
216
+ }
217
+ // getBlocksJSON omits menu shadows and does not distinguish dynamic shapes.
218
+ export function getCodeExtensions(runtime, target) {
219
+ const definitions = [];
220
+ const dynamicDefaults = [];
221
+ const customFields = [];
222
+ for (const category of runtime._blockInfo ?? []) {
223
+ for (const { declaration, definition } of getCustomFields(category)) {
224
+ customFields.push(declaration);
225
+ definitions.push(projectDefinition(definition, runtime, target));
226
+ }
227
+ for (const entry of [...(category.menus ?? []), ...(category.blocks ?? [])]) {
228
+ const json = entry.json;
229
+ if (!json?.type?.startsWith(`${category.id}_`))
230
+ continue;
231
+ if (!entry.info?.isDynamic)
232
+ definitions.push(projectDefinition(json, runtime, target));
233
+ else if (supportsDynamicInfo(entry.info)) {
234
+ dynamicDefaults.push({ mutation: JSON.stringify(entry.info), json: projectDefinition(json, runtime, target) });
235
+ }
236
+ }
237
+ }
238
+ let categories = [];
239
+ try {
240
+ // Add-ons can attach methods to the palette array. Send only data across the message port.
241
+ categories = Array.from(runtime.getBlocksXML?.(target) ?? [], ({ id, xml }) => ({ id, xml }));
242
+ }
243
+ catch {
244
+ // Invalid palette metadata must not prevent existing blocks from loading.
245
+ }
246
+ return { definitions, categories, dynamicDefaults, customFields };
247
+ }
248
+ export function getCodeDynamicShapes(runtime, target) {
249
+ const dynamic = new Map();
250
+ for (const category of runtime._blockInfo ?? []) {
251
+ for (const entry of category.blocks ?? []) {
252
+ if (entry.info?.isDynamic && entry.json?.type)
253
+ dynamic.set(entry.json.type, { category, opcode: entry.info.opcode });
254
+ }
255
+ }
256
+ if (!dynamic.size)
257
+ return [];
258
+ const shapes = new Map();
259
+ for (const block of Object.values(target.blocks._blocks)) {
260
+ const registration = dynamic.get(block.opcode);
261
+ const info = block.mutation?.blockInfo;
262
+ if (!registration || !supportsDynamicInfo(info))
263
+ continue;
264
+ const mutation = JSON.stringify(info);
265
+ const key = shapeKey(block.opcode, mutation);
266
+ if (shapes.has(key))
267
+ continue;
268
+ try {
269
+ const json = convertDynamicShape(runtime, registration.category, registration.opcode, info);
270
+ shapes.set(key, { mutation, json: projectDefinition(json, runtime, target) });
271
+ }
272
+ catch {
273
+ // An invalid or unsupported mutation remains in the parent VM.
274
+ }
275
+ }
276
+ return [...shapes.values()];
277
+ }
278
+ const shapeKey = (type, mutation) => `${type}\n${mutation}`;
279
+ const paletteTags = new Set(['block', 'shadow', 'field', 'value', 'statement', 'next', 'mutation', 'sep']);
280
+ const blockArgs = (json) => Object.entries(json).filter(([key]) => /^args\d+$/.test(key))
281
+ .flatMap(([, args]) => Array.isArray(args) ? args : []);
282
+ export function createCodeExtensionPalette(blocks, openProgrammatic) {
283
+ const installed = new Set();
284
+ const dynamicTypes = new Set();
285
+ const defaults = new Map();
286
+ let shapes = new Map();
287
+ let unregisterFields;
288
+ let nativeGraphics = new Map();
289
+ function canInstall(json) {
290
+ const registry = blocks.Field?.TYPE_MAP_;
291
+ const mixins = blocks.Extensions?.ALL_;
292
+ const customFieldPrefix = `field_${json.type.split('_')[0]}_`;
293
+ if (blockArgs(json).some(field => typeof field?.type !== 'string' || (field.type.startsWith('field_') &&
294
+ (registry ? !Object.hasOwn(registry, field.type) : field.type.startsWith(customFieldPrefix)))))
295
+ return false;
296
+ const extensions = Array.isArray(json.extensions) ? json.extensions : [json.extensions].filter(Boolean);
297
+ return !mixins || ![...extensions, json.mutator].filter(Boolean).some(name => !Object.hasOwn(mixins, name));
298
+ }
299
+ function shape(type, raw) {
300
+ if (!raw)
301
+ return;
302
+ try {
303
+ const key = shapeKey(type, JSON.stringify(JSON.parse(raw)));
304
+ return shapes.get(key) ?? defaults.get(key);
305
+ }
306
+ catch {
307
+ return;
308
+ }
309
+ }
310
+ function supportsBlock(block) {
311
+ const type = block.getAttribute('type');
312
+ if (!Object.hasOwn(blocks.Blocks, type))
313
+ return false;
314
+ if (!dynamicTypes.has(type))
315
+ return true;
316
+ const mutation = Array.from(block.children).find(child => child.localName === 'mutation');
317
+ const json = shape(type, mutation?.getAttribute('blockInfo'));
318
+ if (!json)
319
+ return false;
320
+ const inputs = new Map(blockArgs(json).map(arg => [arg.name, arg.type]));
321
+ return Array.from(block.children).every(child => {
322
+ if (child.localName === 'next')
323
+ return Object.hasOwn(json, 'nextStatement');
324
+ const inputType = inputs.get(child.getAttribute('name'));
325
+ // VM workspace XML uses <value> for statement inputs as well.
326
+ if (child.localName === 'value')
327
+ return inputType === 'input_value' || inputType === 'input_statement';
328
+ if (child.localName === 'statement')
329
+ return inputType === 'input_statement';
330
+ return true;
331
+ });
332
+ }
333
+ function registerDynamic(type) {
334
+ if (dynamicTypes.has(type) || Object.hasOwn(blocks.Blocks, type))
335
+ return;
336
+ blocks.Blocks[type] = {
337
+ init() { },
338
+ domToMutation(mutation) {
339
+ const json = shape(type, mutation.getAttribute('blockInfo'));
340
+ if (!json)
341
+ throw new Error(`Unsupported dynamic block: ${type}`);
342
+ this.jsonInit(json);
343
+ this.plusExtensionMutation = mutation.cloneNode(true);
344
+ },
345
+ mutationToDom() {
346
+ return this.plusExtensionMutation?.cloneNode(true) ?? null;
347
+ }
348
+ };
349
+ installed.add(type);
350
+ dynamicTypes.add(type);
351
+ }
352
+ return {
353
+ update({ definitions, categories, dynamicDefaults, customFields }) {
354
+ unregisterFields?.();
355
+ unregisterFields = registerCustomFields(blocks, customFields, openProgrammatic, undefined, (blockId, name, value) => {
356
+ const graphic = nativeGraphics.get(blockId)?.get(name);
357
+ return graphic?.value === value ? graphic : undefined;
358
+ });
359
+ for (const type of installed)
360
+ delete blocks.Blocks[type];
361
+ installed.clear();
362
+ dynamicTypes.clear();
363
+ defaults.clear();
364
+ for (const json of definitions) {
365
+ if (Object.hasOwn(blocks.Blocks, json.type) || !canInstall(json))
366
+ continue;
367
+ blocks.defineBlocksWithJsonArray([json]);
368
+ installed.add(json.type);
369
+ }
370
+ for (const { mutation, json } of dynamicDefaults) {
371
+ if (!canInstall(json))
372
+ continue;
373
+ defaults.set(shapeKey(json.type, mutation), json);
374
+ registerDynamic(json.type);
375
+ }
376
+ return categories.map(({ xml }) => {
377
+ let category;
378
+ try {
379
+ category = blocks.Xml.textToDom(`<xml>${xml}</xml>`).firstElementChild;
380
+ }
381
+ catch {
382
+ return '';
383
+ }
384
+ if (category?.localName !== 'category')
385
+ return '';
386
+ if (!safeImage(category.getAttribute('iconURI')))
387
+ category.removeAttribute('iconURI');
388
+ category.removeAttribute('showStatusButton');
389
+ for (const child of Array.from(category.children)) {
390
+ if ([child, ...child.querySelectorAll('*')].some(node => !paletteTags.has(node.localName) ||
391
+ (node.matches('block, shadow') && !supportsBlock(node))))
392
+ child.remove();
393
+ }
394
+ return category.querySelector('block') ? category.outerHTML : '';
395
+ }).join('');
396
+ },
397
+ setNativeFieldGraphics(graphics) {
398
+ nativeGraphics = new Map();
399
+ for (const graphic of graphics) {
400
+ if (!nativeGraphics.has(graphic.blockId))
401
+ nativeGraphics.set(graphic.blockId, new Map());
402
+ nativeGraphics.get(graphic.blockId).set(graphic.name, graphic);
403
+ }
404
+ },
405
+ setDynamicShapes(next) {
406
+ shapes = new Map();
407
+ for (const { mutation, json } of next) {
408
+ if (!canInstall(json))
409
+ continue;
410
+ shapes.set(shapeKey(json.type, mutation), json);
411
+ registerDynamic(json.type);
412
+ }
413
+ },
414
+ supports(xml) {
415
+ return Array.from(xml.querySelectorAll('block, shadow')).every(supportsBlock);
416
+ }
417
+ };
418
+ }
@@ -0,0 +1,63 @@
1
+ import { getCodeSession } from './workspace.ts';
2
+ import type { AttachPreviewMinimap, AttachWorkspaceNavigation } from './workspace.ts';
3
+ type Pane = {
4
+ id: string;
5
+ targetId: string;
6
+ section: number;
7
+ };
8
+ type Bridge = NonNullable<ReturnType<typeof getCodeSession>['bridge']>;
9
+ type Presentation = NonNullable<ReturnType<Bridge['capturePane']>>;
10
+ export declare function createCodePaneAdapter(vm: any, locale: string, attachMinimap?: AttachPreviewMinimap): {
11
+ /**
12
+ * Make a code view show one script, a hat or a function, or every script again without `scriptId`.
13
+ * The code area shows it while it is in that view, and the view's preview shows it otherwise.
14
+ */
15
+ focusPane(paneId: string, scriptId?: string, onLost?: () => void): void;
16
+ focusedScript: (paneId: string) => string;
17
+ /** Forget a view that has closed. */
18
+ forgetPane(paneId: string): void;
19
+ ownsClick: (element: EventTarget) => boolean;
20
+ watchEditingCostume: (image: HTMLImageElement) => () => void;
21
+ capture(pane: Pane): import("./workspace.ts").Presentation;
22
+ restore(pane: Pane, presentation?: Presentation): void;
23
+ renderInactive(container: HTMLElement, pane: Pane, presentation?: Presentation): () => void;
24
+ };
25
+ /**
26
+ * Mount an editor of one custom block definition in `element`, for the functions section.
27
+ * It follows the code area: when the GUI replaces its workspace, the editor is rebuilt on the new one.
28
+ */
29
+ export declare function mountProcedureEditor(vm: any, locale: string, element: HTMLElement, targetId: string, definitionId: string, { editable, attachNavigation, focusBlock }: {
30
+ editable: boolean;
31
+ attachNavigation?: AttachWorkspaceNavigation;
32
+ focusBlock?: string;
33
+ }): (() => void) & {
34
+ /** Show one block of the script, such as a call picked in the list of a function's uses. */
35
+ showBlock(id: string): void;
36
+ };
37
+ export interface HatScript {
38
+ id: string;
39
+ label: string;
40
+ x: number;
41
+ y: number;
42
+ thumbnail?: SVGSVGElement;
43
+ }
44
+ /**
45
+ * Read the scripts of a target that start with a hat, such as “when green flag clicked”, leaving out `skip`
46
+ * (its function definitions). A script is a hat script when its top block has nothing to attach above it or
47
+ * to plug it into; its label is how ScratchBlocks spells that block.
48
+ */
49
+ export declare function readHatScripts(target: any, ScratchBlocks: any, skip?: ReadonlySet<string>): HatScript[];
50
+ /**
51
+ * Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
52
+ * in the script editors. `find` names the function a block calls, if any; `open` shows it.
53
+ */
54
+ export declare function attachFunctionCallMenu(ScratchBlocks: any, { label, find, open }: {
55
+ label(): string;
56
+ find(blockId: string): string | undefined;
57
+ open(functionId: string): void;
58
+ }): () => void;
59
+ /** Add a function definition of this name to a target through the code area's bridge. Resolves with the new hat's id. */
60
+ export declare function createFunction(vm: any, targetId: string, kind: 'procedure' | 'named', name: string, options?: {
61
+ loadNamedFunctions?(): Promise<void>;
62
+ }): Promise<string>;
63
+ export {};