@kubohiroya/turbowarp-plus-editor-code-views 0.17.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 +19 -1
- package/README.md +3 -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/palette-category-groups.d.ts +3 -2
- package/dist/palette-category-groups.js +46 -16
- package/dist/palette-category-reorder.d.ts +5 -0
- package/dist/palette-category-reorder.js +1 -1
- 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 +5 -2
- package/src/code-pane.ts +32 -1
- package/src/index.ts +55 -2
- package/src/palette-category-groups.ts +46 -19
- package/src/palette-category-reorder.ts +6 -1
- 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
package/README.ja.md
CHANGED
|
@@ -18,7 +18,7 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
18
18
|
| `./target-combobox` | コードの窓の対象の選択 |
|
|
19
19
|
| `./detached-block-palette` | 別窓のブロックパレット。パレットの `grouping` を渡すと、パレットと同じくグループを子ブロックパレットで開き、固定できる |
|
|
20
20
|
| `./palette-category-reorder`・`./palette-category-search`・`./palette-dropdown-resize` | パレットのカテゴリの並べ替えと検索、パレットのドロップダウンの大きさの変更 |
|
|
21
|
-
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`prepareGroupedToolboxTree(tree, {catalog, visible})` は、機能拡張が宣言したグループの入れ子化と表示するグループの絞り込みを含めて、これを一度に行う。`hasCategoryGroups(tree)` は木が category を入れ子にしているかを返す。`attachPaletteCategoryGroups(menu, options)` で子ブロックパレットを開く。その `selectionTarget(id)` は、自分のブロックを持たないグループの代わりに選ぶ category を返し、`syncButtons(openId)` はグループのアイコンの状態を付け直す。`reorder: true`
|
|
21
|
+
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`prepareGroupedToolboxTree(tree, {catalog, visible})` は、機能拡張が宣言したグループの入れ子化と表示するグループの絞り込みを含めて、これを一度に行う。`hasCategoryGroups(tree)` は木が category を入れ子にしているかを返す。`attachPaletteCategoryGroups(menu, options)` で子ブロックパレットを開く。その `selectionTarget(id)` は、自分のブロックを持たないグループの代わりに選ぶ category を返し、`syncButtons(openId)` はグループのアイコンの状態を付け直す。`reorder: true` では、グループの要素を長押しして、同じグループの要素の中で並べ替えられる(子ブロックパレットの中でも、固定したグループの要素を広げた場所でも)(別のグループや階層へは動かさない)。順番はパレットを描き直しても保ち(`order` を渡せば、プロジェクトと一緒に保存するパレットの設定に保つ)、`prepare(tree, options)` はツールボックスをその順番で描かせ、`arrange(categories, rank)` がツールボックスの category をそれに合わせて、グループの後にその要素が続くように並べる。子ブロックパレットの右上にはピンがあり、押すとグループの要素を、そのグループを表示していたパレットのそのグループの直後に広げて固定する。固定したグループのアイコンにはピンが付き、押すと元のようにたたむ。固定は同じ `order` に保つので、それを渡したパレット同士は揃う。`splitExtensionCategories(tree, catalog, {nested: true})` は、グループを宣言した機能拡張をこのグループにする |
|
|
22
22
|
| `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はエディターの画面状態として保存し、選ぶ画面を持つ。ストアの `order`(`createPaletteOrder`)は、パレットのカテゴリーと各グループの要素を並べ替えた順番を保ち、同じ設定と一緒に保存する |
|
|
23
23
|
| `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する。テキストの引数で Shift+Enter を押すと、改行を入れてダイアログで編集を続ける |
|
|
24
24
|
| `./block-number-input` | 数値の引数に入力した全角の数字・記号を半角にする |
|
|
@@ -41,10 +41,28 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
41
41
|
|
|
42
42
|
Editor は GUI の React 16 にこれらを描画するので、React 16.14 以降に対応する。
|
|
43
43
|
|
|
44
|
+
## スクリプト間の矢印
|
|
45
|
+
|
|
46
|
+
コードの背面に、関数呼び出しを紫系の実線、メッセージ送信を橙系の破線で表示する。「送って待つ」には待機の印を添える。通常配置、Z/N 配置、折り畳み、プレビュー、別窓で同じ表現を使う。関数の再帰はスクリプトの外へ回る線で示す。選択・ホバーによる強調は太さと不透明度で行い、関係種別の色・線種は変えない。
|
|
47
|
+
|
|
48
|
+
関係は作品データから読み、折り畳みによって表示から消えた呼び出しも保持する。展開中は呼び出しブロックから、折り畳み中は見えているハットから結ぶ。同じハットと宛先へ重なる呼び出しは件数を表示する。別スプライトや一スクリプト表示の外にある接続先は、画面端の案内から辿れる。変数などで呼び出し先が変わるものは「実行時に決定」と示し、見えていない既定値を宛先とみなさない。
|
|
49
|
+
|
|
50
|
+
スクリプトをドラッグしている間は、そこへ入る・そこから出る矢印を太く濃くし、画面にある相手のスクリプト全体を枠で強調する。関係しない矢印は薄くし、画面外の相手は接続先の案内を強調する。関数とメッセージの色・線種は維持する。折り畳み中も同じように働き、ドラッグの終了・矢印の非表示・画面の破棄で一時的な枠を解除する。移動中に Blockly がブロックを別の SVG へ移しても追従する。
|
|
51
|
+
|
|
52
|
+
`attachDockedScriptLayout` と `attachCodeWorkspace` のプレビュー・関数エディタには自動で付く。VM を持たない表示では、`attachScriptView` に `relations: {getTarget, getRelations, getTargetName, navigate}` を渡す。`getRelations` は親の `createCodeSession` が渡す `scriptRelations`、`getTarget` はその対象の `blocks` を使う。`getTargetName` は既存の対象一覧から名前を返し、同じメッセージを受信する別スプライトを案内で識別できるようにする。投影の受信後は `view.relations.update()` を呼ぶ。関係だけの更新は `relations`、接続先への移動は `relation-navigate`/`relation-focus` で既存の編集回収・対象切り替えを経由する。編集中の窓には履歴の更新のために XML を再読み込みさせない。
|
|
53
|
+
|
|
54
|
+
色は共通の CSS 変数 `--tw-script-function-stroke` と `--tw-script-message-stroke` に定義する。明暗テーマでは同じ種別のまま明度を変え、線種でも区別できる。既存の関数呼び出し図とミニマップも関数用の変数を使う。
|
|
55
|
+
|
|
56
|
+
コード画面に常設の凡例や表示スイッチは置かない。選択・ホバー・ドラッグ中の関係に必要な接続先だけを短く表示する。呼び出し関係の図へ渡す `readScriptRelationGraph(targets, ScratchBlocks)` は、同じ関係データと、接続先スクリプトのハットの文言・対象名・位置を返す。図の向きと関数・メッセージの表示は、呼び出し関係パネルの左端カラム下端で操作する。
|
|
57
|
+
|
|
58
|
+
既定は表示する。作品の保存形式は変えない。完全に切り戻す場合は導入前のパッケージ固定と結線へ戻す。関連: [Editor #683](https://github.com/kubohiroya/turbowarp-plus-editor/issues/683)。
|
|
59
|
+
|
|
44
60
|
## 検証
|
|
45
61
|
|
|
46
62
|
`npm test` でビルドし、単体テストを実行し、React 16.14 より新しい API を使っていないことを確かめる。
|
|
47
63
|
|
|
64
|
+
`npm run test:browser` は、最小のエディター実行環境(`@kubohiroya/turbowarp-plus-editor-test-runtime`)で、Editor と同じ VM と Scratch Blocks を使い、パレットのグループとスクリプト間の矢印を Chromium で操作して確かめる。矢印は `node --test test/script-arrows.browser.ts` で限定実行でき、折り畳み・実際の関数エディタ・Z/N 配置・テーマ・別スプライトへの移動・キー入力の隔離をヘッドレスで検証する。`TWPE_CHROME_EXECUTABLE` を指定すると、Playwright のものの代わりにその Chromium を使う。
|
|
65
|
+
|
|
48
66
|
## ライセンス
|
|
49
67
|
|
|
50
68
|
MPL-2.0。turbowarp-plus-editor の `packages/editor-views` と `packages/editor-ui` から移したコードで、同じ作者が書いたもの。移すときに GPL-3.0 から MPL-2.0 にした。由来の監査は [turbowarp-plus-packages の移行計画](https://github.com/kubohiroya/turbowarp-plus-packages/blob/main/docs/editor-migration-plan.ja.md)の「由来の監査(7b)」にある。
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ The code views of TurboWarp Plus Editor: code in tabs and windows that share one
|
|
|
19
19
|
| `./detached-block-palette` | The block palette in its own window. Given the palette's `grouping`, it opens groups in child palettes and pins them as the palette does |
|
|
20
20
|
| `./palette-category-visibility`, `./palette-visibility-dialog` | Choose which block groups the palette shows for each sprite and the stage. The setting is saved with the editor's screen state, with a screen to choose. The store's `order` (`createPaletteOrder`) keeps the order the palette's categories and each group's members were dragged into, saved with the same settings |
|
|
21
21
|
| `./palette-category-reorder`, `./palette-category-search`, `./palette-dropdown-resize` | Reordering and searching palette categories, and resizing palette dropdowns |
|
|
22
|
-
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `prepareGroupedToolboxTree(tree, {catalog, visible})` does both in one step, also nesting the groups extensions declare and keeping only the groups to show, and `hasCategoryGroups(tree)` tells whether a tree nests categories. `attachPaletteCategoryGroups(menu, options)` opens the child palettes; its `selectionTarget(id)` gives the category to select for a group without blocks of its own, and `syncButtons(openId)` marks the groups' icons. With `reorder: true`, a long press on a member of a
|
|
22
|
+
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `prepareGroupedToolboxTree(tree, {catalog, visible})` does both in one step, also nesting the groups extensions declare and keeping only the groups to show, and `hasCategoryGroups(tree)` tells whether a tree nests categories. `attachPaletteCategoryGroups(menu, options)` opens the child palettes; its `selectionTarget(id)` gives the category to select for a group without blocks of its own, and `syncButtons(openId)` marks the groups' icons. With `reorder: true`, a long press on a member of a group drags it among the members of the same group, in the group's child palette or where a pinned group shows its members (not to another group or level); the order is kept while the palette is drawn again (in the `order` given, such as the palette settings saved with the project), `prepare(tree, options)` draws the toolbox in it, and `arrange(categories, rank)` orders the toolbox's categories to match, each group followed by its members. A child palette has a pin at its top right: pinning the group shows its members in the palette that showed the group, right after it, and the pinned group's icon shows a pin that folds them back. The pins are kept in the same `order`, so palettes given it follow each other. `splitExtensionCategories(tree, catalog, {nested: true})` makes an extension that declares groups such a group |
|
|
23
23
|
| `./block-argument-text` | Shortens long text arguments and edits them in a dialog. Shift+Enter in a text argument continues in the dialog with a line break |
|
|
24
24
|
| `./block-number-input` | Converts full-width digits and signs typed into number arguments to half-width |
|
|
25
25
|
| `./clean-up-comments` | Keeps comments with their scripts in "Clean up blocks": block comments keep their offset from the block, scripts with their comments do not cover one another, and workspace comments that cover a script move beside the scripts (`installCleanUpComments(ScratchBlocks)`) |
|
|
@@ -45,6 +45,8 @@ React 16.14 or later is supported, because the Editor mounts these views in the
|
|
|
45
45
|
|
|
46
46
|
Run `npm test` to build the package, run the unit tests, and check that the sources use no React API newer than 16.14.
|
|
47
47
|
|
|
48
|
+
Run `npm run test:browser` to drive the palette's groups in Chromium with the Editor's own VM and Scratch Blocks, through the minimal editor runtime (`@kubohiroya/turbowarp-plus-editor-test-runtime`): child palettes, pins, reordering and the detached palette. `TWPE_CHROME_EXECUTABLE` names a Chromium to use instead of Playwright's own.
|
|
49
|
+
|
|
48
50
|
## License
|
|
49
51
|
|
|
50
52
|
MPL-2.0. This code moved here from `packages/editor-views` and `packages/editor-ui` in turbowarp-plus-editor. The same author wrote it, and it changed from GPL-3.0 to MPL-2.0 in the move. The provenance audit is in the "由来の監査(7b)" section of the [migration plan in turbowarp-plus-packages](https://github.com/kubohiroya/turbowarp-plus-packages/blob/main/docs/editor-migration-plan.ja.md).
|
package/dist/code-pane.d.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 { type ScriptView } from './script-layout-view.ts';
|
|
4
|
+
import { type RelationTarget } from './script-relations.ts';
|
|
4
5
|
type Pane = {
|
|
5
6
|
id: string;
|
|
6
7
|
targetId: string;
|
|
@@ -64,6 +65,19 @@ export interface HatScript {
|
|
|
64
65
|
* to plug it into; its label is how ScratchBlocks spells that block.
|
|
65
66
|
*/
|
|
66
67
|
export declare function readHatScripts(target: any, ScratchBlocks: any, skip?: ReadonlySet<string>): HatScript[];
|
|
68
|
+
/** Supply the calls panel with the same relations and native hat labels as the code workspace. */
|
|
69
|
+
export declare function readScriptRelationGraph(targets: readonly RelationTarget[], ScratchBlocks: any): {
|
|
70
|
+
scriptRelations: import("./script-relations.ts").ScriptRelation[];
|
|
71
|
+
scripts: {
|
|
72
|
+
targetId: string;
|
|
73
|
+
scriptId: string;
|
|
74
|
+
label: string;
|
|
75
|
+
targetName?: string;
|
|
76
|
+
isStage?: boolean;
|
|
77
|
+
x?: number;
|
|
78
|
+
y?: number;
|
|
79
|
+
}[];
|
|
80
|
+
};
|
|
67
81
|
/**
|
|
68
82
|
* Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
|
|
69
83
|
* in the script editors. `find` names the function a block calls, if any; `open` shows it.
|
package/dist/code-pane.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getCodeSession } from "./workspace.js";
|
|
2
2
|
import { getDockedScriptLayout } from "./script-layout-view.js";
|
|
3
|
+
import { readScriptRelations } from "./script-relations.js";
|
|
3
4
|
const scriptViews = ['free', 'z', 'n'];
|
|
4
5
|
const finite = (value, limit) => typeof value === 'number' && Number.isFinite(value) && Math.abs(value) <= limit;
|
|
5
6
|
/** Read a saved code area back, or nothing if it does not have the shape `serialize` writes. */
|
|
@@ -24,7 +25,10 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
|
|
|
24
25
|
let pending;
|
|
25
26
|
// The GUI can attach the code area again, with a new bridge; the view it is in still shows its script.
|
|
26
27
|
let attached = session.bridge;
|
|
27
|
-
session.subscribe(
|
|
28
|
+
session.subscribe(navigation => {
|
|
29
|
+
const focus = current === undefined ? undefined : focused.get(current);
|
|
30
|
+
if (navigation && focus)
|
|
31
|
+
focus.scriptId = navigation.scriptId;
|
|
28
32
|
if (session.bridge === attached)
|
|
29
33
|
return;
|
|
30
34
|
attached = session.bridge;
|
|
@@ -355,6 +359,37 @@ export function readHatScripts(target, ScratchBlocks, skip = new Set()) {
|
|
|
355
359
|
workspace.dispose();
|
|
356
360
|
}
|
|
357
361
|
}
|
|
362
|
+
/** Supply the calls panel with the same relations and native hat labels as the code workspace. */
|
|
363
|
+
export function readScriptRelationGraph(targets, ScratchBlocks) {
|
|
364
|
+
const scriptRelations = readScriptRelations(targets);
|
|
365
|
+
const used = new Map();
|
|
366
|
+
for (const relation of scriptRelations) {
|
|
367
|
+
for (const endpoint of [relation.source, relation.destination]) {
|
|
368
|
+
if (!endpoint)
|
|
369
|
+
continue;
|
|
370
|
+
if (!used.has(endpoint.targetId))
|
|
371
|
+
used.set(endpoint.targetId, new Set());
|
|
372
|
+
used.get(endpoint.targetId).add(endpoint.scriptId);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const scripts = [];
|
|
376
|
+
for (const target of targets) {
|
|
377
|
+
if (target.isOriginal === false || !used.has(target.id))
|
|
378
|
+
continue;
|
|
379
|
+
const hats = new Map(readHatScripts(target, ScratchBlocks).map(hat => [hat.id, hat]));
|
|
380
|
+
for (const scriptId of used.get(target.id)) {
|
|
381
|
+
const block = target.blocks._blocks[scriptId];
|
|
382
|
+
if (!block)
|
|
383
|
+
continue;
|
|
384
|
+
const prototype = target.blocks._blocks[block.inputs?.custom_block?.block ?? ''];
|
|
385
|
+
const label = hats.get(scriptId)?.label ?? String(prototype?.mutation?.proccode ??
|
|
386
|
+
block.fields?.BROADCAST_OPTION?.value ?? block.opcode);
|
|
387
|
+
scripts.push({ targetId: target.id, scriptId, label, targetName: target.getName?.(),
|
|
388
|
+
isStage: Boolean(target.isStage), x: block.x ?? 0, y: block.y ?? 0 });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return { scriptRelations, scripts };
|
|
392
|
+
}
|
|
358
393
|
/**
|
|
359
394
|
* Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
|
|
360
395
|
* in the script editors. `find` names the function a block calls, if any; `open` shows it.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ export { attachDockedCodeExtensions } from './code-extensions.ts';
|
|
|
2
2
|
export type { NativeFieldEditSession } from './native-extension-field.ts';
|
|
3
3
|
export type { FieldActionPanel, ProgrammaticExtensionField } from './programmatic-extension-field.ts';
|
|
4
4
|
export { encodeEdit } from './protocol.ts';
|
|
5
|
+
export { readScriptRelations } from './script-relations.ts';
|
|
6
|
+
export type { ScriptEndpoint, ScriptRelation } from './script-relations.ts';
|
|
5
7
|
export declare function refreshExternalBlockViews(vm: any, targets: any): void;
|
|
6
8
|
export declare function createCodeSession({ vm, workspace, ScratchBlocks, document, locale, canUse, onError, receiveBackpackDrop }: {
|
|
7
9
|
vm: any;
|
package/dist/index.js
CHANGED
|
@@ -6,8 +6,10 @@ import { readFieldActionPanel, runFieldAction } from "./programmatic-extension-f
|
|
|
6
6
|
import { capture, createHistory, restore, workspaceXML } from "./history.js";
|
|
7
7
|
import { removeObsoleteProcedureInputs } from "./procedure-edits.js";
|
|
8
8
|
import { decodeEdit, isCodeEdit, isCommentEdit } from "./protocol.js";
|
|
9
|
+
import { readScriptRelations } from "./script-relations.js";
|
|
9
10
|
export { attachDockedCodeExtensions } from "./code-extensions.js";
|
|
10
11
|
export { encodeEdit } from "./protocol.js";
|
|
12
|
+
export { readScriptRelations } from "./script-relations.js";
|
|
11
13
|
// A drop must stay within one target's block and comment history.
|
|
12
14
|
const supportsSharedHistory = script => !script.variables.length && !script.extensionIDs?.length &&
|
|
13
15
|
!Object.keys(script.extensionURLs).length;
|
|
@@ -61,6 +63,7 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
61
63
|
let lease = 0;
|
|
62
64
|
let projecting = false;
|
|
63
65
|
let disposed = false;
|
|
66
|
+
let relationsPending = false;
|
|
64
67
|
let canDuplicate = false;
|
|
65
68
|
let flushRequest = 0;
|
|
66
69
|
const flushes = new Set();
|
|
@@ -75,10 +78,31 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
75
78
|
const dynamicShapesFor = target => getCodeDynamicShapes(vm.runtime, target);
|
|
76
79
|
const projection = (target, xml = workspaceXML(vm, target)) => ({
|
|
77
80
|
...histories.get(target).state, xml, dynamicShapes: dynamicShapesFor(target),
|
|
81
|
+
blocks: target.blocks._blocks,
|
|
82
|
+
scriptRelations: readScriptRelations(vm.runtime.targets.filter(selectable)),
|
|
78
83
|
nativeFieldGraphics: vm.editingTarget === target ? projectNativeFieldGraphics(vm.runtime, target, workspace) : []
|
|
79
84
|
});
|
|
80
85
|
const extensionsFor = target => getCodeExtensions(vm.runtime, target);
|
|
81
|
-
const send = (view, message) =>
|
|
86
|
+
const send = (view, message) => {
|
|
87
|
+
if (message.scriptRelations)
|
|
88
|
+
view.relationSignature = JSON.stringify(message.scriptRelations);
|
|
89
|
+
view.port.postMessage(message);
|
|
90
|
+
};
|
|
91
|
+
function updateRelations() {
|
|
92
|
+
if (relationsPending || !views.size)
|
|
93
|
+
return;
|
|
94
|
+
relationsPending = true;
|
|
95
|
+
queueMicrotask(() => {
|
|
96
|
+
relationsPending = false;
|
|
97
|
+
if (disposed || !views.size)
|
|
98
|
+
return;
|
|
99
|
+
const scriptRelations = readScriptRelations(vm.runtime.targets.filter(selectable));
|
|
100
|
+
const signature = JSON.stringify(scriptRelations);
|
|
101
|
+
for (const view of views)
|
|
102
|
+
if (view.relationSignature !== signature)
|
|
103
|
+
send(view, { type: 'relations', scriptRelations });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
82
106
|
function updateCatalog() {
|
|
83
107
|
const targets = vm.runtime.targets.filter(selectable).map(target => {
|
|
84
108
|
const asset = target.getCostumes?.()[target.currentCostume]?.asset;
|
|
@@ -126,8 +150,11 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
126
150
|
for (const view of views) {
|
|
127
151
|
if (view.target !== target)
|
|
128
152
|
continue;
|
|
129
|
-
|
|
153
|
+
// The editing source must keep its live fields and queued edits; only other views reload XML.
|
|
154
|
+
send(view, view === source ? { type: 'history', ...histories.get(target).state,
|
|
155
|
+
blocks: data.blocks, scriptRelations: data.scriptRelations } : { type: 'update', ...data });
|
|
130
156
|
}
|
|
157
|
+
updateRelations();
|
|
131
158
|
}
|
|
132
159
|
function flushParent() {
|
|
133
160
|
for (const target of dirty) {
|
|
@@ -184,6 +211,11 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
184
211
|
if (!histories.has(target))
|
|
185
212
|
histories.set(target, createHistory(target, vm.runtime.getTargetForStage()));
|
|
186
213
|
send(view, { type: 'target', targetId: target.id, extensions: extensionsFor(target), ...projection(target) });
|
|
214
|
+
if (view.relationFocus?.targetId === target.id) {
|
|
215
|
+
const { blockId, scriptId } = view.relationFocus;
|
|
216
|
+
view.relationFocus = undefined;
|
|
217
|
+
send(view, { type: 'relation-focus', blockId, scriptId });
|
|
218
|
+
}
|
|
187
219
|
}
|
|
188
220
|
function grant() {
|
|
189
221
|
if (disposed || owner)
|
|
@@ -490,6 +522,28 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
490
522
|
accept: supportsSharedHistory, commit: apply => queueDrop(target, apply) });
|
|
491
523
|
return;
|
|
492
524
|
}
|
|
525
|
+
if (message.type === 'relation-navigate') {
|
|
526
|
+
if (view.draining || !viewFocused(view) || !exists(view.target))
|
|
527
|
+
return;
|
|
528
|
+
const endpoint = { targetId: message.targetId, blockId: message.blockId, scriptId: message.scriptId };
|
|
529
|
+
const matches = candidate => candidate && candidate.targetId === endpoint.targetId &&
|
|
530
|
+
candidate.blockId === endpoint.blockId && candidate.scriptId === endpoint.scriptId;
|
|
531
|
+
// Navigation is limited to an existing endpoint related to this window's target.
|
|
532
|
+
if (!readScriptRelations(vm.runtime.targets.filter(selectable)).some(relation => (relation.source.targetId === view.target.id || relation.destination?.targetId === view.target.id) &&
|
|
533
|
+
(matches(relation.source) || matches(relation.destination))))
|
|
534
|
+
return;
|
|
535
|
+
const target = vm.runtime.getTargetById(endpoint.targetId);
|
|
536
|
+
if (!selectable(target))
|
|
537
|
+
return;
|
|
538
|
+
if (target === view.target)
|
|
539
|
+
send(view, { type: 'relation-focus', blockId: endpoint.blockId, scriptId: endpoint.scriptId });
|
|
540
|
+
else {
|
|
541
|
+
view.relationFocus = endpoint;
|
|
542
|
+
pending = view;
|
|
543
|
+
retarget(view, target);
|
|
544
|
+
}
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
493
547
|
if (!owns(view, message))
|
|
494
548
|
return;
|
|
495
549
|
if (message.type === 'field-editor' || message.type === 'field-action') {
|
|
@@ -594,6 +648,7 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
594
648
|
}
|
|
595
649
|
}
|
|
596
650
|
function targetsUpdated() {
|
|
651
|
+
updateRelations();
|
|
597
652
|
if (views.size)
|
|
598
653
|
updateCatalog();
|
|
599
654
|
for (const view of views)
|
|
@@ -694,6 +749,7 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
694
749
|
vm.on('workspaceUpdate', flushWorkspaceProjection);
|
|
695
750
|
vm.on('EXTENSION_ADDED', extensionsUpdated);
|
|
696
751
|
vm.on('BLOCKSINFO_UPDATE', extensionsUpdated);
|
|
752
|
+
vm.runtime.on?.('PROJECT_CHANGED', updateRelations);
|
|
697
753
|
return { attach, flush, setCanDuplicate(available) {
|
|
698
754
|
if (available === canDuplicate)
|
|
699
755
|
return;
|
|
@@ -720,6 +776,7 @@ export function createCodeSession({ vm, workspace, ScratchBlocks, document, loca
|
|
|
720
776
|
parent.removeEventListener('focus', parentFocus);
|
|
721
777
|
vm.removeListener('targetsUpdate', targetsUpdated);
|
|
722
778
|
vm.removeListener('workspaceUpdate', flushWorkspaceProjection);
|
|
779
|
+
vm.runtime.removeListener?.('PROJECT_CHANGED', updateRelations);
|
|
723
780
|
vm.removeListener('EXTERNAL_BLOCKS_RELOADED', externalBlocksReloaded);
|
|
724
781
|
vm.removeListener('EXTENSION_ADDED', extensionsUpdated);
|
|
725
782
|
vm.removeListener('BLOCKSINFO_UPDATE', extensionsUpdated);
|
|
@@ -56,8 +56,9 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
56
56
|
/** Called when a child palette opens or the last one closes. */
|
|
57
57
|
onChange?(): void;
|
|
58
58
|
/**
|
|
59
|
-
* Lets a long press on a member of a
|
|
60
|
-
*
|
|
59
|
+
* Lets a long press on a member of a group drag it to another place among the members of the same group, as the
|
|
60
|
+
* palette's own categories can be reordered: in the group's child palette, or where a pinned group shows its
|
|
61
|
+
* members. Members do not move to another group or level.
|
|
61
62
|
*/
|
|
62
63
|
reorder?: boolean;
|
|
63
64
|
/** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
|
|
@@ -236,8 +236,9 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
236
236
|
const closeFrom = (depth) => {
|
|
237
237
|
let changed = false;
|
|
238
238
|
while (stack.length > depth) {
|
|
239
|
-
const { anchor, popup,
|
|
240
|
-
reorder
|
|
239
|
+
const { anchor, popup, reorders } = stack.pop();
|
|
240
|
+
for (const reorder of reorders)
|
|
241
|
+
reorder.dispose();
|
|
241
242
|
anchor.setAttribute('aria-expanded', 'false');
|
|
242
243
|
popup.remove();
|
|
243
244
|
changed = true;
|
|
@@ -359,36 +360,34 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
359
360
|
popup.append(pinButton(false, () => pinFrom(depth)));
|
|
360
361
|
if (groups.withOwnContents.has(group.id_))
|
|
361
362
|
popup.append(createEntry(group, depth, true));
|
|
362
|
-
|
|
363
|
+
// The entries of each group's members shown here: the group's own, and those of pinned member groups.
|
|
364
|
+
const shown = new Map([[group.id_, []]]);
|
|
363
365
|
// A pinned member group is followed by its own members, here rather than in a child palette of its own.
|
|
364
|
-
const add = (member,
|
|
366
|
+
const add = (member, groupId) => {
|
|
365
367
|
const entry = createEntry(member, depth + 1);
|
|
366
|
-
|
|
367
|
-
entries.push(entry);
|
|
368
|
+
shown.get(groupId).push(entry);
|
|
368
369
|
popup.append(entry);
|
|
369
370
|
if (!isPinned(member.id_))
|
|
370
371
|
return;
|
|
372
|
+
shown.set(member.id_, []);
|
|
371
373
|
for (const id of groups.members.get(member.id_) ?? []) {
|
|
372
374
|
const inner = categoryById(id);
|
|
373
375
|
if (inner)
|
|
374
|
-
add(inner,
|
|
376
|
+
add(inner, member.id_);
|
|
375
377
|
}
|
|
376
378
|
};
|
|
377
379
|
for (const member of members)
|
|
378
|
-
add(member,
|
|
380
|
+
add(member, group.id_);
|
|
379
381
|
(menu.parentElement ?? doc.body).append(popup);
|
|
380
382
|
place(popup, anchor);
|
|
381
383
|
anchor.setAttribute('aria-expanded', 'true');
|
|
382
|
-
// The group's own blocks stay first;
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
386
|
-
quietly(() => memberOrder().setMembers(group.id_, order));
|
|
387
|
-
groups.members.set(group.id_, order);
|
|
384
|
+
// The group's own blocks stay first; the members of each group shown here can be dragged among themselves.
|
|
385
|
+
const reorders = options.reorder ? [...shown].filter(([, entries]) => entries.length > 1).map(([groupId, entries]) => attachPaletteCategoryReorder(popup, () => entries, (from, to) => {
|
|
386
|
+
moveMember(groupId, from, to);
|
|
388
387
|
open(group, anchor, depth, false);
|
|
389
388
|
options.onReorder?.();
|
|
390
|
-
}, { horizontal: options.horizontal, area: entry => entry }) :
|
|
391
|
-
stack.push({ id: group.id_, anchor, popup,
|
|
389
|
+
}, { horizontal: options.horizontal, area: entry => entry })) : [];
|
|
390
|
+
stack.push({ id: group.id_, anchor, popup, reorders });
|
|
392
391
|
options.onChange?.();
|
|
393
392
|
if (focus)
|
|
394
393
|
popup.firstElementChild?.focus();
|
|
@@ -428,6 +427,34 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
428
427
|
else
|
|
429
428
|
applyVisibility();
|
|
430
429
|
}
|
|
430
|
+
/** Moves a member of the group, as dragged, and keeps the order. */
|
|
431
|
+
function moveMember(groupId, from, to) {
|
|
432
|
+
const order = [...(groups.members.get(groupId) ?? [])];
|
|
433
|
+
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
434
|
+
quietly(() => memberOrder().setMembers(groupId, order));
|
|
435
|
+
groups.members.set(groupId, order);
|
|
436
|
+
}
|
|
437
|
+
// Where the palette shows a pinned group's members, they can be dragged among themselves.
|
|
438
|
+
let inlineReorders = [];
|
|
439
|
+
function attachInlineReorders() {
|
|
440
|
+
for (const reorder of inlineReorders)
|
|
441
|
+
reorder.dispose();
|
|
442
|
+
inlineReorders = [];
|
|
443
|
+
if (!options.reorder)
|
|
444
|
+
return;
|
|
445
|
+
for (const [id, members] of groups.members) {
|
|
446
|
+
if (members.length < 2 || !isPinned(id) || !inPalette(id))
|
|
447
|
+
continue;
|
|
448
|
+
const items = () => (groups.members.get(id) ?? []).map(member => categoryById(member)?.item_)
|
|
449
|
+
.filter((item) => Boolean(item));
|
|
450
|
+
inlineReorders.push(attachPaletteCategoryReorder(menu, items, (from, to) => {
|
|
451
|
+
moveMember(id, from, to);
|
|
452
|
+
close();
|
|
453
|
+
applyVisibility();
|
|
454
|
+
options.onReorder?.();
|
|
455
|
+
}, { horizontal: options.horizontal, ends: false }));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
431
458
|
// Shows the categories every group of which is pinned open, hides the others, and marks the groups shown.
|
|
432
459
|
function applyVisibility() {
|
|
433
460
|
for (const category of options.categories()) {
|
|
@@ -457,6 +484,7 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
457
484
|
else if (!pinned)
|
|
458
485
|
button?.remove();
|
|
459
486
|
}
|
|
487
|
+
attachInlineReorders();
|
|
460
488
|
markSelection();
|
|
461
489
|
options.onChange?.();
|
|
462
490
|
}
|
|
@@ -634,6 +662,8 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
634
662
|
close,
|
|
635
663
|
dispose() {
|
|
636
664
|
close();
|
|
665
|
+
for (const reorder of inlineReorders)
|
|
666
|
+
reorder.dispose();
|
|
637
667
|
unwatch?.();
|
|
638
668
|
events.abort();
|
|
639
669
|
}
|
|
@@ -3,6 +3,11 @@ export interface PaletteCategoryReorderOptions {
|
|
|
3
3
|
horizontal?(): boolean;
|
|
4
4
|
/** The element an item takes up, which a drop over it lands on. By default, the row that holds the item. */
|
|
5
5
|
area?(item: HTMLElement): Element;
|
|
6
|
+
/**
|
|
7
|
+
* Whether a drop before the first item or after the last one lands at that end. On by default; off, a drop
|
|
8
|
+
* anywhere but over an item moves nothing, as when the items share the menu with others.
|
|
9
|
+
*/
|
|
10
|
+
ends?: boolean;
|
|
6
11
|
}
|
|
7
12
|
/**
|
|
8
13
|
* Lets a long press on an item of the menu drag it to another place among the items. Used for the palette's
|
|
@@ -40,7 +40,7 @@ export function attachPaletteCategoryReorder(menu, getItems, onDrop, options = {
|
|
|
40
40
|
if (!items.length || !menu.contains(hit))
|
|
41
41
|
return;
|
|
42
42
|
const item = items.find(candidate => area(candidate).contains(hit));
|
|
43
|
-
if (item)
|
|
43
|
+
if (item || options.ends === false)
|
|
44
44
|
return item;
|
|
45
45
|
const first = items[0];
|
|
46
46
|
const last = items[items.length - 1];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type RelationTarget, type ScriptEndpoint, type ScriptRelation } from './script-relations.ts';
|
|
2
|
+
export interface ScriptRelationContext {
|
|
3
|
+
vm?: {
|
|
4
|
+
runtime?: {
|
|
5
|
+
targets?: readonly RelationTarget[];
|
|
6
|
+
on?(name: string, listener: () => void): void;
|
|
7
|
+
removeListener?(name: string, listener: () => void): void;
|
|
8
|
+
};
|
|
9
|
+
on?(name: string, listener: () => void): void;
|
|
10
|
+
removeListener?(name: string, listener: () => void): void;
|
|
11
|
+
setEditingTarget?(id: string): void;
|
|
12
|
+
};
|
|
13
|
+
getTarget(): RelationTarget | undefined;
|
|
14
|
+
/** A child window receives the parent's graph rather than interpreting a partial project. */
|
|
15
|
+
getRelations?(): readonly ScriptRelation[];
|
|
16
|
+
getLocale?(): string;
|
|
17
|
+
getTargetName?(id: string): string | undefined;
|
|
18
|
+
navigate?(endpoint: ScriptEndpoint): void;
|
|
19
|
+
}
|
|
20
|
+
export declare function registerScriptRelationNavigation(vm: object, navigate: (endpoint: ScriptEndpoint) => void): () => void;
|
|
21
|
+
/** One renderer and semantic stylesheet serve live code, folded scripts, previews and serial layouts. */
|
|
22
|
+
export declare function attachScriptArrows({ workspace, vm, getTarget, getRelations, getLocale, getTargetName, navigate }: ScriptRelationContext & {
|
|
23
|
+
workspace: {
|
|
24
|
+
getInjectionDiv(): HTMLElement;
|
|
25
|
+
getCanvas(): SVGElement;
|
|
26
|
+
getParentSvg(): SVGElement;
|
|
27
|
+
getBlockById?(id: string): {
|
|
28
|
+
width?: number;
|
|
29
|
+
height?: number;
|
|
30
|
+
getSvgRoot(): SVGElement | null;
|
|
31
|
+
} | null;
|
|
32
|
+
addChangeListener?(listener: (event: {
|
|
33
|
+
type?: string;
|
|
34
|
+
}) => void): void;
|
|
35
|
+
removeChangeListener?(listener: (event: {
|
|
36
|
+
type?: string;
|
|
37
|
+
}) => void): void;
|
|
38
|
+
centerOnBlock(id: string): void;
|
|
39
|
+
RTL?: boolean;
|
|
40
|
+
};
|
|
41
|
+
}): {
|
|
42
|
+
update: () => void;
|
|
43
|
+
readonly enabled: boolean;
|
|
44
|
+
setEnabled(value: boolean): void;
|
|
45
|
+
setProjection(svg: SVGSVGElement | null): void;
|
|
46
|
+
dispose(): void;
|
|
47
|
+
};
|