@kubohiroya/turbowarp-plus-editor-code-views 0.18.0 → 0.19.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.
- package/README.ja.md +17 -1
- package/dist/code-pane.d.ts +14 -0
- package/dist/code-pane.js +36 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +59 -2
- package/dist/script-arrows.d.ts +47 -0
- package/dist/script-arrows.js +448 -0
- package/dist/script-layout-view.d.ts +24 -1
- package/dist/script-layout-view.js +15 -4
- package/dist/script-relations.d.ts +47 -0
- package/dist/script-relations.js +155 -0
- package/dist/sprite-code-windows.d.ts +1 -1
- package/dist/sprite-code-windows.js +1 -1
- package/dist/workspace.d.ts +3 -2
- package/dist/workspace.js +24 -2
- package/package.json +1 -1
- package/src/code-pane.ts +32 -1
- package/src/index.ts +55 -2
- package/src/script-arrows.ts +440 -0
- package/src/script-layout-view.ts +17 -4
- package/src/script-relations.ts +179 -0
- package/src/sprite-code-windows.tsx +1 -1
- package/src/styles.css +145 -0
- package/src/workspace.ts +26 -6
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
const namedPrefix = 'kubohiroyanamedfunctions_';
|
|
2
|
+
function program(target) {
|
|
3
|
+
const blocks = target.blocks._blocks;
|
|
4
|
+
const roots = new Map();
|
|
5
|
+
function endpoint(id) {
|
|
6
|
+
const path = new Set();
|
|
7
|
+
let current = id;
|
|
8
|
+
let root;
|
|
9
|
+
while (true) {
|
|
10
|
+
if (roots.has(current)) {
|
|
11
|
+
root = roots.get(current);
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
if (path.has(current) || !blocks[current])
|
|
15
|
+
break;
|
|
16
|
+
path.add(current);
|
|
17
|
+
const parent = blocks[current].parent;
|
|
18
|
+
if (!parent) {
|
|
19
|
+
root = current;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
current = parent;
|
|
23
|
+
}
|
|
24
|
+
for (const member of path)
|
|
25
|
+
roots.set(member, root);
|
|
26
|
+
return root ? { targetId: target.id, blockId: id, scriptId: root } : undefined;
|
|
27
|
+
}
|
|
28
|
+
return { target, blocks, endpoint };
|
|
29
|
+
}
|
|
30
|
+
/** Read the active literal only; an obscured menu/text shadow is never a possible destination. */
|
|
31
|
+
function inputLiteral(block, blocks, name) {
|
|
32
|
+
const input = block.inputs?.[name];
|
|
33
|
+
const value = input?.block ? blocks[input.block] : undefined;
|
|
34
|
+
if (!value)
|
|
35
|
+
return undefined;
|
|
36
|
+
const field = value.opcode === 'text' ? value.fields?.TEXT :
|
|
37
|
+
value.opcode === 'event_broadcast_menu' ? value.fields?.BROADCAST_OPTION : undefined;
|
|
38
|
+
return field?.value === undefined || field.value === null ? undefined : String(field.value);
|
|
39
|
+
}
|
|
40
|
+
function append(map, key, value) {
|
|
41
|
+
const entries = map.get(key);
|
|
42
|
+
if (entries)
|
|
43
|
+
entries.push(value);
|
|
44
|
+
else
|
|
45
|
+
map.set(key, [value]);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Extract possible static calls without executing the project or modifying its blocks.
|
|
49
|
+
* Broadcasts fan out; conflicting function definitions and computed names stay unresolved.
|
|
50
|
+
*/
|
|
51
|
+
export function readScriptRelations(targets) {
|
|
52
|
+
const seenTargets = new Set();
|
|
53
|
+
const programs = targets.filter(target => {
|
|
54
|
+
if (target.isOriginal === false || seenTargets.has(target.id))
|
|
55
|
+
return false;
|
|
56
|
+
seenTargets.add(target.id);
|
|
57
|
+
return true;
|
|
58
|
+
}).map(program);
|
|
59
|
+
const procedures = new Map();
|
|
60
|
+
const functions = new Map();
|
|
61
|
+
const receivers = new Map();
|
|
62
|
+
for (const { target, blocks, endpoint } of programs) {
|
|
63
|
+
const own = new Map();
|
|
64
|
+
procedures.set(target.id, own);
|
|
65
|
+
for (const block of Object.values(blocks)) {
|
|
66
|
+
if (block.parent || block.topLevel === false || block.shadow)
|
|
67
|
+
continue;
|
|
68
|
+
const destination = endpoint(block.id);
|
|
69
|
+
if (!destination)
|
|
70
|
+
continue;
|
|
71
|
+
if (block.opcode === 'procedures_definition') {
|
|
72
|
+
const prototype = blocks[block.inputs?.custom_block?.block ?? ''];
|
|
73
|
+
const code = prototype?.opcode === 'procedures_prototype' ? prototype.mutation?.proccode : undefined;
|
|
74
|
+
if (typeof code === 'string')
|
|
75
|
+
append(own, code, destination);
|
|
76
|
+
}
|
|
77
|
+
else if (block.opcode === `${namedPrefix}defineFunction`) {
|
|
78
|
+
// A named definition's NAME is required to be literal, including its shadow connection.
|
|
79
|
+
const input = block.inputs?.NAME;
|
|
80
|
+
if (input?.block && input.block !== input.shadow)
|
|
81
|
+
continue;
|
|
82
|
+
const name = inputLiteral(block, blocks, 'NAME')?.trim();
|
|
83
|
+
const scope = block.fields?.SCOPE?.value;
|
|
84
|
+
if (name && (scope === undefined || scope === null || scope === 'global' || scope === 'sprite')) {
|
|
85
|
+
append(functions, name, { endpoint: destination, scope: scope === 'sprite' ? 'sprite' : 'global' });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (block.opcode === 'event_whenbroadcastreceived') {
|
|
89
|
+
const name = block.fields?.BROADCAST_OPTION?.value;
|
|
90
|
+
if (name !== undefined && name !== null)
|
|
91
|
+
append(receivers, String(name).toUpperCase(), destination);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const relations = [];
|
|
96
|
+
for (const { target, blocks, endpoint } of programs) {
|
|
97
|
+
for (const block of Object.values(blocks)) {
|
|
98
|
+
let kind;
|
|
99
|
+
let name;
|
|
100
|
+
let wait = true;
|
|
101
|
+
let destinations = [];
|
|
102
|
+
let hasInput = true;
|
|
103
|
+
if (block.opcode === 'procedures_call') {
|
|
104
|
+
kind = 'function';
|
|
105
|
+
name = typeof block.mutation?.proccode === 'string' ? block.mutation.proccode : undefined;
|
|
106
|
+
destinations = procedures.get(target.id)?.get(name) ?? [];
|
|
107
|
+
hasInput = name !== undefined;
|
|
108
|
+
}
|
|
109
|
+
else if (block.opcode === 'event_broadcast' || block.opcode === 'event_broadcastandwait') {
|
|
110
|
+
kind = 'message';
|
|
111
|
+
wait = block.opcode === 'event_broadcastandwait';
|
|
112
|
+
name = inputLiteral(block, blocks, 'BROADCAST_INPUT');
|
|
113
|
+
destinations = name === undefined ? [] : receivers.get(name.toUpperCase()) ?? [];
|
|
114
|
+
hasInput = Boolean(block.inputs?.BROADCAST_INPUT?.block);
|
|
115
|
+
}
|
|
116
|
+
else if (block.opcode === `${namedPrefix}callFunction` || block.opcode === `${namedPrefix}startFunction` ||
|
|
117
|
+
block.opcode === `${namedPrefix}callFunctionFromMenu`) {
|
|
118
|
+
kind = 'function';
|
|
119
|
+
wait = block.opcode !== `${namedPrefix}startFunction`;
|
|
120
|
+
if (block.opcode === `${namedPrefix}callFunctionFromMenu`) {
|
|
121
|
+
const field = block.fields?.FUNCTION?.value;
|
|
122
|
+
name = field === undefined || field === null ? undefined : String(field);
|
|
123
|
+
hasInput = name !== undefined;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
name = inputLiteral(block, blocks, 'NAME');
|
|
127
|
+
hasInput = Boolean(block.inputs?.NAME?.block);
|
|
128
|
+
}
|
|
129
|
+
name = name?.trim();
|
|
130
|
+
destinations = (functions.get(name) ?? []).filter(definition => definition.scope === 'global' ||
|
|
131
|
+
definition.endpoint.targetId === target.id).map(definition => definition.endpoint);
|
|
132
|
+
}
|
|
133
|
+
else
|
|
134
|
+
continue;
|
|
135
|
+
const source = endpoint(block.id);
|
|
136
|
+
if (!source || block.shadow)
|
|
137
|
+
continue;
|
|
138
|
+
const relation = { kind, source, label: name ?? '', wait };
|
|
139
|
+
if (name === undefined) {
|
|
140
|
+
relations.push({ ...relation, unresolved: hasInput ? 'dynamic' : 'missing' });
|
|
141
|
+
}
|
|
142
|
+
else if (!destinations.length) {
|
|
143
|
+
relations.push({ ...relation, unresolved: 'missing' });
|
|
144
|
+
}
|
|
145
|
+
else if (kind === 'function' && destinations.length > 1) {
|
|
146
|
+
relations.push({ ...relation, unresolved: 'ambiguous' });
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
for (const destination of destinations)
|
|
150
|
+
relations.push({ ...relation, destination });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return relations;
|
|
155
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { createCodePaneAdapter } from './code-pane.ts';
|
|
3
3
|
export { attachCodeWorkspace } from './workspace.ts';
|
|
4
|
-
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane } from './code-pane.ts';
|
|
4
|
+
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane, readScriptRelationGraph } from './code-pane.ts';
|
|
5
5
|
export type { SavedCodePane } from './code-pane.ts';
|
|
6
6
|
export declare function CodePaneCostumeThumbnail({ adapter }: {
|
|
7
7
|
adapter: ReturnType<typeof createCodePaneAdapter>;
|
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
3
|
import { getCodeSession } from "./workspace.js";
|
|
4
4
|
export { attachCodeWorkspace } from "./workspace.js";
|
|
5
|
-
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane } from "./code-pane.js";
|
|
5
|
+
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane, readScriptRelationGraph } from "./code-pane.js";
|
|
6
6
|
export function CodePaneCostumeThumbnail({ adapter }) {
|
|
7
7
|
const image = React.useRef(null);
|
|
8
8
|
React.useLayoutEffect(() => adapter.watchEditingCostume(image.current), [adapter]);
|
package/dist/workspace.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { type CodeTarget } from './history.ts';
|
|
|
2
2
|
import { captureCodePreviewChrome } from './preview-chrome.ts';
|
|
3
3
|
import type { PresentationElements } from './preview-chrome.ts';
|
|
4
4
|
import { type ScriptView } from './script-layout-view.ts';
|
|
5
|
+
import type { ScriptEndpoint } from './script-relations.ts';
|
|
5
6
|
export type AttachPreviewMinimap = (workspace: any, ScratchBlocks: any, watermark: HTMLElement) => {
|
|
6
7
|
dispose(): void;
|
|
7
8
|
};
|
|
@@ -25,8 +26,8 @@ interface CodeSession {
|
|
|
25
26
|
presentation?: Presentation;
|
|
26
27
|
} | null;
|
|
27
28
|
bridge: ReturnType<typeof attachCodeWorkspace> | null;
|
|
28
|
-
subscribe(listener: () => void): () => void;
|
|
29
|
-
changed(): void;
|
|
29
|
+
subscribe(listener: (navigation?: ScriptEndpoint) => void): () => void;
|
|
30
|
+
changed(navigation?: ScriptEndpoint): void;
|
|
30
31
|
}
|
|
31
32
|
export declare function getCodeSession(vm: object): CodeSession;
|
|
32
33
|
type MeasuredField = {
|
package/dist/workspace.js
CHANGED
|
@@ -5,6 +5,7 @@ import { isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition
|
|
|
5
5
|
import { decodeEdit, encodeEdit, isCommentEdit } from "./protocol.js";
|
|
6
6
|
import { captureCodePreviewChrome } from "./preview-chrome.js";
|
|
7
7
|
import { attachScriptView, getDockedScriptLayout } from "./script-layout-view.js";
|
|
8
|
+
import { attachScriptArrows, registerScriptRelationNavigation } from "./script-arrows.js";
|
|
8
9
|
const sessions = new WeakMap();
|
|
9
10
|
export function getCodeSession(vm) {
|
|
10
11
|
let session = sessions.get(vm);
|
|
@@ -17,9 +18,9 @@ export function getCodeSession(vm) {
|
|
|
17
18
|
listeners.add(listener);
|
|
18
19
|
return () => listeners.delete(listener);
|
|
19
20
|
},
|
|
20
|
-
changed() {
|
|
21
|
+
changed(navigation) {
|
|
21
22
|
for (const listener of listeners)
|
|
22
|
-
listener();
|
|
23
|
+
listener(navigation);
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
26
|
sessions.set(vm, session);
|
|
@@ -566,6 +567,7 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
566
567
|
const minimap = attachMinimap?.(preview, blocks, watermark);
|
|
567
568
|
const scriptView = attachScriptView({ workspace: preview, ScratchBlocks: blocks, mount: chrome.parentElement,
|
|
568
569
|
getLocale: () => locale ?? workspace.getInjectionDiv().ownerDocument.documentElement.lang,
|
|
570
|
+
relations: { vm, getTarget: () => target },
|
|
569
571
|
initialView: view.scriptView ?? presentation?.scriptView,
|
|
570
572
|
onChange: next => {
|
|
571
573
|
if (presentation)
|
|
@@ -647,6 +649,7 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
647
649
|
},
|
|
648
650
|
dispose() {
|
|
649
651
|
blockDrag.dispose();
|
|
652
|
+
unregisterRelationNavigation();
|
|
650
653
|
unregisterCodeWorkspace();
|
|
651
654
|
shown?.disconnect();
|
|
652
655
|
visibility.dispose();
|
|
@@ -676,6 +679,22 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
676
679
|
shown?.observe(injection);
|
|
677
680
|
const blockDrag = attachBlockDrag({ vm, workspace, ScratchBlocks: blocks, onError, prepareImport: bridge.prepareImport });
|
|
678
681
|
const visibility = attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError });
|
|
682
|
+
const unregisterRelationNavigation = registerScriptRelationNavigation(vm, endpoint => {
|
|
683
|
+
if (vm.runtime.getTargetById(endpoint.targetId)?.blocks._blocks[endpoint.blockId] === undefined)
|
|
684
|
+
return;
|
|
685
|
+
if (vm.editingTarget?.id !== endpoint.targetId)
|
|
686
|
+
vm.setEditingTarget(endpoint.targetId);
|
|
687
|
+
view.requestAnimationFrame(() => {
|
|
688
|
+
if (session.bridge !== bridge)
|
|
689
|
+
return;
|
|
690
|
+
// Let a target switch restore that pane's mode first. Normal views keep showing all scripts.
|
|
691
|
+
if (visibility.focusedScript) {
|
|
692
|
+
visibility.focus(endpoint.scriptId);
|
|
693
|
+
session.changed(endpoint);
|
|
694
|
+
}
|
|
695
|
+
workspace.centerOnBlock(endpoint.scriptId);
|
|
696
|
+
});
|
|
697
|
+
});
|
|
679
698
|
visibility.paint();
|
|
680
699
|
session.bridge = bridge;
|
|
681
700
|
vm.runtime.on('PROJECT_CHANGED', changed);
|
|
@@ -970,6 +989,8 @@ function attachProcedureEditor({ vm, blocks, workspace, options, onError, homeDo
|
|
|
970
989
|
};
|
|
971
990
|
document.addEventListener('pointerdown', releaseFocus, true);
|
|
972
991
|
vm.runtime.on('PROJECT_CHANGED', scheduleLoad);
|
|
992
|
+
const relations = attachScriptArrows({ workspace: editor, vm, getTarget: () => target,
|
|
993
|
+
getLocale: () => locale });
|
|
973
994
|
load();
|
|
974
995
|
// In a modal dialog the rest of the page is inert, including the text and dropdown editors
|
|
975
996
|
// ScratchBlocks keeps in the body. Lend them to the dialog, which covers the viewport so their
|
|
@@ -986,6 +1007,7 @@ function attachProcedureEditor({ vm, blocks, workspace, options, onError, homeDo
|
|
|
986
1007
|
},
|
|
987
1008
|
dispose() {
|
|
988
1009
|
toolboxListeners.delete(refreshPalette);
|
|
1010
|
+
relations.dispose();
|
|
989
1011
|
navigation?.dispose();
|
|
990
1012
|
element.removeAttribute('data-code-hover-surface');
|
|
991
1013
|
element.removeAttribute('data-hover-palette');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-code-views",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Code views for TurboWarp Plus Editor: code in tabs and windows, the detached block palette, script layout and block field editing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
package/src/code-pane.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {getCodeSession} from './workspace.ts';
|
|
2
2
|
import type {AttachPreviewMinimap, AttachWorkspaceNavigation} from './workspace.ts';
|
|
3
3
|
import {getDockedScriptLayout, type ScriptView} from './script-layout-view.ts';
|
|
4
|
+
import {readScriptRelations, type RelationTarget} from './script-relations.ts';
|
|
4
5
|
|
|
5
6
|
type Pane = {id: string; targetId: string; section: number};
|
|
6
7
|
type Bridge = NonNullable<ReturnType<typeof getCodeSession>['bridge']>;
|
|
@@ -41,7 +42,9 @@ export function createCodePaneAdapter(vm, locale: string, attachMinimap?: Attach
|
|
|
41
42
|
let pending: {frame: number; pane: Pane; presentation: Presentation} | undefined;
|
|
42
43
|
// The GUI can attach the code area again, with a new bridge; the view it is in still shows its script.
|
|
43
44
|
let attached = session.bridge;
|
|
44
|
-
session.subscribe(
|
|
45
|
+
session.subscribe(navigation => {
|
|
46
|
+
const focus = current === undefined ? undefined : focused.get(current);
|
|
47
|
+
if (navigation && focus) focus.scriptId = navigation.scriptId;
|
|
45
48
|
if (session.bridge === attached) return;
|
|
46
49
|
attached = session.bridge;
|
|
47
50
|
if (current !== undefined) applyFocus(current);
|
|
@@ -351,6 +354,34 @@ export function readHatScripts(target, ScratchBlocks, skip: ReadonlySet<string>
|
|
|
351
354
|
}
|
|
352
355
|
}
|
|
353
356
|
|
|
357
|
+
/** Supply the calls panel with the same relations and native hat labels as the code workspace. */
|
|
358
|
+
export function readScriptRelationGraph(targets: readonly RelationTarget[], ScratchBlocks) {
|
|
359
|
+
const scriptRelations = readScriptRelations(targets);
|
|
360
|
+
const used = new Map<string, Set<string>>();
|
|
361
|
+
for (const relation of scriptRelations) {
|
|
362
|
+
for (const endpoint of [relation.source, relation.destination]) {
|
|
363
|
+
if (!endpoint) continue;
|
|
364
|
+
if (!used.has(endpoint.targetId)) used.set(endpoint.targetId, new Set());
|
|
365
|
+
used.get(endpoint.targetId).add(endpoint.scriptId);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const scripts: {targetId: string; scriptId: string; label: string; targetName?: string; isStage?: boolean; x?: number; y?: number}[] = [];
|
|
369
|
+
for (const target of targets) {
|
|
370
|
+
if (target.isOriginal === false || !used.has(target.id)) continue;
|
|
371
|
+
const hats = new Map(readHatScripts(target, ScratchBlocks).map(hat => [hat.id, hat]));
|
|
372
|
+
for (const scriptId of used.get(target.id)) {
|
|
373
|
+
const block = target.blocks._blocks[scriptId];
|
|
374
|
+
if (!block) continue;
|
|
375
|
+
const prototype = target.blocks._blocks[block.inputs?.custom_block?.block ?? ''];
|
|
376
|
+
const label = hats.get(scriptId)?.label ?? String(prototype?.mutation?.proccode ??
|
|
377
|
+
block.fields?.BROADCAST_OPTION?.value ?? block.opcode);
|
|
378
|
+
scripts.push({targetId: target.id, scriptId, label, targetName: target.getName?.(),
|
|
379
|
+
isStage: Boolean(target.isStage), x: block.x ?? 0, y: block.y ?? 0});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return {scriptRelations, scripts};
|
|
383
|
+
}
|
|
384
|
+
|
|
354
385
|
/**
|
|
355
386
|
* Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
|
|
356
387
|
* in the script editors. `find` names the function a block calls, if any; `open` shows it.
|
package/src/index.ts
CHANGED
|
@@ -6,11 +6,14 @@ import {readFieldActionPanel, runFieldAction, type FieldActionPanel, type Progra
|
|
|
6
6
|
import {capture, createHistory, restore, workspaceXML} from './history.ts';
|
|
7
7
|
import {removeObsoleteProcedureInputs} from './procedure-edits.ts';
|
|
8
8
|
import {decodeEdit, isCodeEdit, isCommentEdit} from './protocol.ts';
|
|
9
|
+
import {readScriptRelations, type ScriptEndpoint} from './script-relations.ts';
|
|
9
10
|
|
|
10
11
|
export {attachDockedCodeExtensions} from './code-extensions.ts';
|
|
11
12
|
export type {NativeFieldEditSession} from './native-extension-field.ts';
|
|
12
13
|
export type {FieldActionPanel, ProgrammaticExtensionField} from './programmatic-extension-field.ts';
|
|
13
14
|
export {encodeEdit} from './protocol.ts';
|
|
15
|
+
export {readScriptRelations} from './script-relations.ts';
|
|
16
|
+
export type {ScriptEndpoint, ScriptRelation} from './script-relations.ts';
|
|
14
17
|
|
|
15
18
|
// A drop must stay within one target's block and comment history.
|
|
16
19
|
const supportsSharedHistory = script => !script.variables.length && !script.extensionIDs?.length &&
|
|
@@ -63,6 +66,8 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
63
66
|
document: Document;
|
|
64
67
|
close(): void;
|
|
65
68
|
duplicate?: () => void;
|
|
69
|
+
relationFocus?: ScriptEndpoint;
|
|
70
|
+
relationSignature?: string;
|
|
66
71
|
closeNativeField?: () => void;
|
|
67
72
|
fieldAction?: {request: number; blockId: string; name: string; value: string;
|
|
68
73
|
editor: ProgrammaticExtensionField; panel: FieldActionPanel; submitted?: boolean};
|
|
@@ -80,6 +85,7 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
80
85
|
let lease = 0;
|
|
81
86
|
let projecting = false;
|
|
82
87
|
let disposed = false;
|
|
88
|
+
let relationsPending = false;
|
|
83
89
|
let canDuplicate = false;
|
|
84
90
|
let flushRequest = 0;
|
|
85
91
|
const flushes = new Set<{request: number; waiting: Set<CodeView>; resolve(): void}>();
|
|
@@ -95,10 +101,27 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
95
101
|
const dynamicShapesFor = target => getCodeDynamicShapes(vm.runtime, target);
|
|
96
102
|
const projection = (target, xml = workspaceXML(vm, target)) => ({
|
|
97
103
|
...histories.get(target).state, xml, dynamicShapes: dynamicShapesFor(target),
|
|
104
|
+
blocks: target.blocks._blocks,
|
|
105
|
+
scriptRelations: readScriptRelations(vm.runtime.targets.filter(selectable)),
|
|
98
106
|
nativeFieldGraphics: vm.editingTarget === target ? projectNativeFieldGraphics(vm.runtime, target, workspace) : []
|
|
99
107
|
});
|
|
100
108
|
const extensionsFor = target => getCodeExtensions(vm.runtime, target);
|
|
101
|
-
const send = (view, message) =>
|
|
109
|
+
const send = (view, message) => {
|
|
110
|
+
if (message.scriptRelations) view.relationSignature = JSON.stringify(message.scriptRelations);
|
|
111
|
+
view.port.postMessage(message);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
function updateRelations() {
|
|
115
|
+
if (relationsPending || !views.size) return;
|
|
116
|
+
relationsPending = true;
|
|
117
|
+
queueMicrotask(() => {
|
|
118
|
+
relationsPending = false;
|
|
119
|
+
if (disposed || !views.size) return;
|
|
120
|
+
const scriptRelations = readScriptRelations(vm.runtime.targets.filter(selectable));
|
|
121
|
+
const signature = JSON.stringify(scriptRelations);
|
|
122
|
+
for (const view of views) if (view.relationSignature !== signature) send(view, {type: 'relations', scriptRelations});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
102
125
|
|
|
103
126
|
function updateCatalog() {
|
|
104
127
|
const targets = vm.runtime.targets.filter(selectable).map(target => {
|
|
@@ -143,8 +166,11 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
143
166
|
const data = projection(target, xml);
|
|
144
167
|
for (const view of views) {
|
|
145
168
|
if (view.target !== target) continue;
|
|
146
|
-
|
|
169
|
+
// The editing source must keep its live fields and queued edits; only other views reload XML.
|
|
170
|
+
send(view, view === source ? {type: 'history', ...histories.get(target).state,
|
|
171
|
+
blocks: data.blocks, scriptRelations: data.scriptRelations} : {type: 'update', ...data});
|
|
147
172
|
}
|
|
173
|
+
updateRelations();
|
|
148
174
|
}
|
|
149
175
|
|
|
150
176
|
function flushParent() {
|
|
@@ -196,6 +222,11 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
196
222
|
view.target = target;
|
|
197
223
|
if (!histories.has(target)) histories.set(target, createHistory(target, vm.runtime.getTargetForStage()));
|
|
198
224
|
send(view, {type: 'target', targetId: target.id, extensions: extensionsFor(target), ...projection(target)});
|
|
225
|
+
if (view.relationFocus?.targetId === target.id) {
|
|
226
|
+
const {blockId, scriptId} = view.relationFocus;
|
|
227
|
+
view.relationFocus = undefined;
|
|
228
|
+
send(view, {type: 'relation-focus', blockId, scriptId});
|
|
229
|
+
}
|
|
199
230
|
}
|
|
200
231
|
|
|
201
232
|
function grant() {
|
|
@@ -470,6 +501,25 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
470
501
|
accept: supportsSharedHistory, commit: apply => queueDrop(target, apply)});
|
|
471
502
|
return;
|
|
472
503
|
}
|
|
504
|
+
if (message.type === 'relation-navigate') {
|
|
505
|
+
if (view.draining || !viewFocused(view) || !exists(view.target)) return;
|
|
506
|
+
const endpoint = {targetId: message.targetId, blockId: message.blockId, scriptId: message.scriptId};
|
|
507
|
+
const matches = candidate => candidate && candidate.targetId === endpoint.targetId &&
|
|
508
|
+
candidate.blockId === endpoint.blockId && candidate.scriptId === endpoint.scriptId;
|
|
509
|
+
// Navigation is limited to an existing endpoint related to this window's target.
|
|
510
|
+
if (!readScriptRelations(vm.runtime.targets.filter(selectable)).some(relation =>
|
|
511
|
+
(relation.source.targetId === view.target.id || relation.destination?.targetId === view.target.id) &&
|
|
512
|
+
(matches(relation.source) || matches(relation.destination)))) return;
|
|
513
|
+
const target = vm.runtime.getTargetById(endpoint.targetId);
|
|
514
|
+
if (!selectable(target)) return;
|
|
515
|
+
if (target === view.target) send(view, {type: 'relation-focus', blockId: endpoint.blockId, scriptId: endpoint.scriptId});
|
|
516
|
+
else {
|
|
517
|
+
view.relationFocus = endpoint;
|
|
518
|
+
pending = view;
|
|
519
|
+
retarget(view, target);
|
|
520
|
+
}
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
473
523
|
if (!owns(view, message)) return;
|
|
474
524
|
if (message.type === 'field-editor' || message.type === 'field-action') {
|
|
475
525
|
handleFieldAction(view, message);
|
|
@@ -561,6 +611,7 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
561
611
|
}
|
|
562
612
|
|
|
563
613
|
function targetsUpdated() {
|
|
614
|
+
updateRelations();
|
|
564
615
|
if (views.size) updateCatalog();
|
|
565
616
|
for (const view of views) if (!exists(view.target)) view.close();
|
|
566
617
|
for (const target of histories.keys()) if (!exists(target)) histories.delete(target);
|
|
@@ -648,6 +699,7 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
648
699
|
vm.on('workspaceUpdate', flushWorkspaceProjection);
|
|
649
700
|
vm.on('EXTENSION_ADDED', extensionsUpdated);
|
|
650
701
|
vm.on('BLOCKSINFO_UPDATE', extensionsUpdated);
|
|
702
|
+
vm.runtime.on?.('PROJECT_CHANGED', updateRelations);
|
|
651
703
|
|
|
652
704
|
return {attach, flush, setCanDuplicate(available) {
|
|
653
705
|
if (available === canDuplicate) return;
|
|
@@ -670,6 +722,7 @@ export function createCodeSession({vm, workspace, ScratchBlocks, document, local
|
|
|
670
722
|
parent.removeEventListener('focus', parentFocus);
|
|
671
723
|
vm.removeListener('targetsUpdate', targetsUpdated);
|
|
672
724
|
vm.removeListener('workspaceUpdate', flushWorkspaceProjection);
|
|
725
|
+
vm.runtime.removeListener?.('PROJECT_CHANGED', updateRelations);
|
|
673
726
|
vm.removeListener('EXTERNAL_BLOCKS_RELOADED', externalBlocksReloaded);
|
|
674
727
|
vm.removeListener('EXTENSION_ADDED', extensionsUpdated);
|
|
675
728
|
vm.removeListener('BLOCKSINFO_UPDATE', extensionsUpdated);
|