@kubohiroya/turbowarp-plus-editor-code-views 0.21.2 → 0.22.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/EDITOR-MIGRATION.md +5 -0
- package/README.ja.md +16 -1
- package/dist/code-pane.d.ts +7 -1
- package/dist/code-pane.js +45 -3
- package/dist/code-popout.d.ts +10 -0
- package/dist/code-popout.js +402 -0
- package/dist/context-menu-icons.d.ts +15 -0
- package/dist/context-menu-icons.js +204 -0
- package/dist/desktop-code.d.ts +11 -0
- package/dist/desktop-code.js +47 -0
- package/dist/horizontal-block-palette.d.ts +39 -0
- package/dist/horizontal-block-palette.js +774 -0
- package/dist/palette-delete-preview.d.ts +3 -0
- package/dist/palette-delete-preview.js +73 -0
- package/dist/script-arrows.js +106 -12
- package/dist/script-layout-view.js +122 -6
- package/dist/script-layout-workspace.js +7 -2
- package/dist/script-layout.d.ts +1 -1
- package/dist/script-layout.js +3 -3
- package/dist/script-projection.d.ts +8 -0
- package/dist/script-projection.js +10 -0
- package/dist/script-view-menu.d.ts +1 -0
- package/dist/script-view-menu.js +4 -2
- package/dist/serial-script-editor.d.ts +4 -0
- package/dist/serial-script-editor.js +43 -0
- package/dist/workspace-overview.js +67 -25
- package/dist/workspace.d.ts +1 -0
- package/dist/workspace.js +3 -2
- package/package.json +27 -3
- package/src/code-pane.ts +41 -3
- package/src/code-popout.tsx +399 -0
- package/src/context-menu-icons.css +14 -0
- package/src/context-menu-icons.ts +215 -0
- package/src/desktop-code.ts +45 -0
- package/src/horizontal-block-palette.css +111 -0
- package/src/horizontal-block-palette.ts +765 -0
- package/src/palette-delete-preview.css +21 -0
- package/src/palette-delete-preview.ts +79 -0
- package/src/pin-off.svg +6 -0
- package/src/pin.svg +5 -0
- package/src/script-arrows.ts +98 -12
- package/src/script-layout-view.ts +114 -6
- package/src/script-layout-workspace.ts +7 -2
- package/src/script-layout.ts +4 -3
- package/src/script-projection.ts +12 -0
- package/src/script-view-menu.ts +5 -2
- package/src/serial-script-editor.ts +41 -0
- package/src/styles.css +80 -5
- package/src/trash.svg +3 -0
- package/src/workspace-overview.ts +63 -22
- package/src/workspace.ts +3 -2
- package/tsconfig.json +87 -1
package/README.ja.md
CHANGED
|
@@ -10,6 +10,12 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
10
10
|
|
|
11
11
|
ホストが独立したワークスペースを表示する場合は、ルートまたは `./sprite-code-windows` の `fitScriptInWorkspace(workspace, scriptId, ScratchBlocks)` を使える。表示領域やブロックがまだない場合は `false` を返すので、レイアウト後に再試行する。
|
|
12
12
|
|
|
13
|
+
Z/N配置のミニマップは、表示中のスクリプトの並びとスクロール範囲を示す。スクリプトをクリックすると内容の編集画面を開き、閉じた後もZ/N配置を保つ。編集画面では先頭ブロックを固定し、作品に保存する自由配置の座標を動かさない。スクリプト間の余白は64単位以上、画面上で48px以上を確保する。背景の右クリックメニューで「すべて折りたたむ」「折りたたみをすべて解除」を利用できる。
|
|
14
|
+
|
|
15
|
+
Z/N配置内の矢印は、スクリプト群の右外側へ96px以上張り出し、経路を分けて上下に曲がる。左縁の▼/▶ボタンは各スクリプトの上端に並び、クリックやEnter/Spaceで個別に折りたたみを切り替えられる。横スクロール中も左縁に残り、縦スクロールとズームにはスクリプトと一緒に追従する。
|
|
16
|
+
|
|
17
|
+
これらの変更の切り戻しは、本変更を revert して再ビルドする。ホストのパッケージ固定を以前の版へ戻す場合は、機能移管で追加された API との互換性も確認する。自由配置へ切り替えるだけでも元の配置を表示できる。折りたたみは既存の作品内フラグを使用するため、「折りたたみをすべて解除」で解除できる。
|
|
18
|
+
|
|
13
19
|
## 入口
|
|
14
20
|
|
|
15
21
|
| import | 内容 |
|
|
@@ -47,6 +53,8 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
47
53
|
|
|
48
54
|
Editor は GUI の React 16 にこれらを描画するので、React 16.14 以降に対応する。
|
|
49
55
|
|
|
56
|
+
自由配置のブロックの右クリックメニューの先頭に「このスクリプトを個別の画面で編集する」、2番目に「このスクリプトの呼び出し関係を開く」を表示する。途中のブロックや関数呼び出しを選んだ場合も、そのブロックを含むスクリプト全体を開く。ホストは `attachFunctionCallMenu` の `scriptEditor: {vm, locale, open(scriptId, blockId, section)}` を既存の個別編集ペインへ接続する。`section` は先頭の項目では `functions`、2番目では `calls`。左側をハット・関数一覧または中央フォーカスした呼び出し関係にし、右側で編集する。Z/N表示、パレット、読み取り専用、個別編集中の画面には追加しない。既存の「この関数を表示」は呼び出し先を開く項目としてその下に残す。切り戻す場合は `scriptEditor` の接続を外す。
|
|
57
|
+
|
|
50
58
|
## スクリプト間の矢印
|
|
51
59
|
|
|
52
60
|
コードの背面に、関数呼び出しを紫系の実線、メッセージ送信を橙系の破線で表示する。「送って待つ」には待機の印を添える。通常配置、Z/N 配置、折り畳み、プレビュー、別窓で同じ表現を使う。関数の再帰はスクリプトの外へ回る線で示す。選択・ホバーによる強調は太さと不透明度で行い、関係種別の色・線種は変えない。
|
|
@@ -55,11 +63,13 @@ Editor は GUI の React 16 にこれらを描画するので、React 16.14 以
|
|
|
55
63
|
|
|
56
64
|
通常の線と矢印の先端は20%の濃さにし、選択・ホバー・ドラッグに関連する線を強調する。ドラッグ開始時に無関係な線だけが急に薄くなることはない。
|
|
57
65
|
|
|
66
|
+
矢印は、始点または終点のブロックの少なくとも一方が画面内にある場合に表示し、両方が画面外なら表示しない。画面外のブロックがネイティブの軽量化で非表示になっても接続位置を保持し、実際の接続位置へ向かう矢印を描く。スクロール・ズームでもこの表示判定を更新する。
|
|
67
|
+
|
|
58
68
|
関係は作品データから読み、折り畳みによって表示から消えた呼び出しも保持する。展開中は呼び出しブロックから、折り畳み中は見えているハットから結ぶ。同じハットと宛先へ重なる呼び出しは件数を表示する。別スプライトや一スクリプト表示の外にある接続先は、画面端の案内から辿れる。変数などで呼び出し先が変わるものは「実行時に決定」と示し、見えていない既定値を宛先とみなさない。
|
|
59
69
|
|
|
60
70
|
接続元または接続先を同じパネル内に描けない矢印は、線・矢印の先端・待機の印・件数を5%の濃さで表示する。ホバー・選択・ドラッグによる強調中も同じ濃さを維持する。
|
|
61
71
|
|
|
62
|
-
|
|
72
|
+
スクリプトをドラッグしている間は、そこへ入る・そこから出る矢印を太く濃くし、画面にある相手のスクリプト全体を薄い背景色で強調する。バウンディングボックスのボーダーは表示しない。関係しない矢印は薄くし、画面外の相手は接続先の案内を強調する。関数とメッセージの色・線種は維持する。折り畳み中も同じように働き、ドラッグの終了・矢印の非表示・画面の破棄で一時的な強調を解除する。移動中に Blockly がブロックを別の SVG へ移しても追従する。
|
|
63
73
|
|
|
64
74
|
`attachDockedScriptLayout` と `attachCodeWorkspace` のプレビュー・関数エディタには自動で付く。VM を持たない表示では、`attachScriptView` に `relations: {getTarget, getRelations, getTargetName, navigate}` を渡す。`getRelations` は親の `createCodeSession` が渡す `scriptRelations`、`getTarget` はその対象の `blocks` を使う。`getTargetName` は既存の対象一覧から名前を返し、同じメッセージを受信する別スプライトを案内で識別できるようにする。投影の受信後は `view.relations.update()` を呼ぶ。関係だけの更新は `relations`、接続先への移動は `relation-navigate`/`relation-focus` で既存の編集回収・対象切り替えを経由する。編集中の窓には履歴の更新のために XML を再読み込みさせない。
|
|
65
75
|
|
|
@@ -137,3 +147,8 @@ getInfo() {
|
|
|
137
147
|
- 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
|
|
138
148
|
- ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
|
|
139
149
|
|
|
150
|
+
|
|
151
|
+
## Editor の実装との責務分担
|
|
152
|
+
|
|
153
|
+
Editor 独自実装の移管元とライセンス確認は [EDITOR-MIGRATION.md](EDITOR-MIGRATION.md) に記録しています。
|
|
154
|
+
追加した公開 API は GUI モジュールを import せず、ホストから必要な値・データ・コールバックを受け取ります。
|
package/dist/code-pane.d.ts
CHANGED
|
@@ -89,11 +89,17 @@ export declare function readScriptRelationGraph(targets: readonly RelationTarget
|
|
|
89
89
|
/**
|
|
90
90
|
* Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
|
|
91
91
|
* in the script editors. `find` names the function a block calls, if any; `open` shows it.
|
|
92
|
+
* With `scriptEditor`, free-layout blocks first offer to edit their own containing script.
|
|
92
93
|
*/
|
|
93
|
-
export declare function attachFunctionCallMenu(ScratchBlocks: any, { label, find, open }: {
|
|
94
|
+
export declare function attachFunctionCallMenu(ScratchBlocks: any, { label, find, open, scriptEditor }: {
|
|
94
95
|
label(): string;
|
|
95
96
|
find(blockId: string): string | undefined;
|
|
96
97
|
open(functionId: string): void;
|
|
98
|
+
scriptEditor?: {
|
|
99
|
+
vm: any;
|
|
100
|
+
locale: string;
|
|
101
|
+
open(scriptId: string, blockId: string, section: 'functions' | 'calls'): void;
|
|
102
|
+
};
|
|
97
103
|
}): () => void;
|
|
98
104
|
/** Add a function definition of this name to a target through the code area's bridge. Resolves with the new hat's id. */
|
|
99
105
|
export declare function createFunction(vm: any, targetId: string, kind: 'procedure' | 'named', name: string, options?: {
|
package/dist/code-pane.js
CHANGED
|
@@ -440,24 +440,66 @@ export function readScriptRelationGraph(targets, ScratchBlocks) {
|
|
|
440
440
|
/**
|
|
441
441
|
* Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
|
|
442
442
|
* in the script editors. `find` names the function a block calls, if any; `open` shows it.
|
|
443
|
+
* With `scriptEditor`, free-layout blocks first offer to edit their own containing script.
|
|
443
444
|
*/
|
|
444
|
-
export function attachFunctionCallMenu(ScratchBlocks, { label, find, open }) {
|
|
445
|
+
export function attachFunctionCallMenu(ScratchBlocks, { label, find, open, scriptEditor }) {
|
|
445
446
|
const prototype = ScratchBlocks.BlockSvg.prototype;
|
|
446
447
|
const showContextMenu = prototype.showContextMenu_;
|
|
447
448
|
const patched = function (event) {
|
|
448
449
|
const functionId = this.isInFlyout ? undefined : find(this.id);
|
|
449
|
-
|
|
450
|
+
const target = scriptEditor?.vm.editingTarget;
|
|
451
|
+
const layout = scriptEditor && getDockedScriptLayout(scriptEditor.vm);
|
|
452
|
+
const script = scriptEditor && !this.isInFlyout && !this.workspace.options.readOnly &&
|
|
453
|
+
this.workspace === ScratchBlocks.getMainWorkspace?.() && layout?.view.view === 'free' &&
|
|
454
|
+
layout.canArrange() && !getCodeSession(scriptEditor.vm).bridge?.focusedScript ? this.getRootBlock() : undefined;
|
|
455
|
+
const scriptId = script && target?.blocks._blocks[script.id] ? script.id : undefined;
|
|
456
|
+
if (!functionId && !scriptId)
|
|
450
457
|
return showContextMenu.call(this, event);
|
|
458
|
+
const blockId = this.id;
|
|
459
|
+
const japanese = scriptEditor && /^ja(?:-|$)/i.test(scriptEditor.locale);
|
|
460
|
+
const scriptOptions = scriptId ? [
|
|
461
|
+
{ section: 'functions', text: japanese ?
|
|
462
|
+
'このスクリプトを個別の画面で編集する' : 'Edit this script in a separate view' },
|
|
463
|
+
{ section: 'calls', text: japanese ?
|
|
464
|
+
'このスクリプトの呼び出し関係を開く' : 'Open relationships for this script' }
|
|
465
|
+
].map(({ section, text }) => ({ text, twPlusMenuIcon: section, separator: false, enabled: true, callback() {
|
|
466
|
+
if (scriptEditor.vm.editingTarget === target && target.blocks._blocks[scriptId]) {
|
|
467
|
+
scriptEditor.open(scriptId, blockId, section);
|
|
468
|
+
}
|
|
469
|
+
} })) : [];
|
|
451
470
|
const owned = Object.prototype.hasOwnProperty.call(this, 'customContextMenu');
|
|
452
471
|
const custom = this.customContextMenu;
|
|
453
472
|
this.customContextMenu = function (options) {
|
|
454
473
|
custom?.call(this, options);
|
|
455
|
-
|
|
474
|
+
if (functionId)
|
|
475
|
+
options.unshift({ text: label(), enabled: true, callback: () => open(functionId) });
|
|
476
|
+
options.unshift(...scriptOptions);
|
|
456
477
|
};
|
|
478
|
+
const { ContextMenu } = ScratchBlocks;
|
|
479
|
+
const populate = scriptOptions.length ? ContextMenu.populate_ : undefined;
|
|
480
|
+
if (scriptOptions.length)
|
|
481
|
+
ContextMenu.populate_ = function (options, rtl) {
|
|
482
|
+
// Addons reorder ContextMenu.show's options after customContextMenu. Prioritize the final list,
|
|
483
|
+
// in place so their separator indices and the host's icon decoration still use the same order.
|
|
484
|
+
for (const priority of [...scriptOptions].reverse()) {
|
|
485
|
+
const index = options.findIndex(option => option.callback === priority.callback);
|
|
486
|
+
if (index > 0)
|
|
487
|
+
options.unshift(...options.splice(index, 1));
|
|
488
|
+
}
|
|
489
|
+
for (const option of scriptOptions)
|
|
490
|
+
option.separator = false;
|
|
491
|
+
if (options.length > 2)
|
|
492
|
+
options[2].separator = true;
|
|
493
|
+
const menu = populate.call(this, options, rtl);
|
|
494
|
+
menu.getChildAt(2)?.getElement().classList.add('tw-script-editor-menu-divider');
|
|
495
|
+
return menu;
|
|
496
|
+
};
|
|
457
497
|
try {
|
|
458
498
|
return showContextMenu.call(this, event);
|
|
459
499
|
}
|
|
460
500
|
finally {
|
|
501
|
+
if (scriptOptions.length)
|
|
502
|
+
ContextMenu.populate_ = populate;
|
|
461
503
|
if (owned)
|
|
462
504
|
this.customContextMenu = custom;
|
|
463
505
|
else
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare function startCodePopout({ ScratchBlocks, PopupLayout, attachWorkspaceNavigation, installBlockContextMenuIcons, readStartupFlags: readWebStartupFeatureFlags }: {
|
|
3
|
+
ScratchBlocks: any;
|
|
4
|
+
PopupLayout: React.ComponentType<any>;
|
|
5
|
+
attachWorkspaceNavigation: (...args: any[]) => any;
|
|
6
|
+
installBlockContextMenuIcons: (...args: any[]) => void;
|
|
7
|
+
readStartupFlags(search: string): {
|
|
8
|
+
contextMenuIcons: boolean;
|
|
9
|
+
};
|
|
10
|
+
}): void;
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import ReactDOM from 'react-dom';
|
|
4
|
+
import { transferMime } from '@kubohiroya/turbowarp-plus-editor-windows/transfer';
|
|
5
|
+
import { installProcedureEditor } from '@kubohiroya/turbowarp-plus-editor-code-views/procedure-editor';
|
|
6
|
+
import { createCodeExtensionPalette, createFieldActionDialog } from '@kubohiroya/turbowarp-plus-editor-code-views/code-extensions';
|
|
7
|
+
import { encodeEdit, fitScriptInWorkspace } from '@kubohiroya/turbowarp-plus-editor-code-views';
|
|
8
|
+
import { installScopedVariableReferences } from '@kubohiroya/turbowarp-plus-editor-code-views/variable-references';
|
|
9
|
+
import { TargetCombobox } from '@kubohiroya/turbowarp-plus-editor-code-views/target-combobox';
|
|
10
|
+
import { attachScriptView } from '@kubohiroya/turbowarp-plus-editor-code-views/script-layout';
|
|
11
|
+
export function startCodePopout({ ScratchBlocks, PopupLayout, attachWorkspaceNavigation, installBlockContextMenuIcons, readStartupFlags: readWebStartupFeatureFlags }) {
|
|
12
|
+
const ticket = location.hash.slice(1);
|
|
13
|
+
const mount = document.getElementById('mount');
|
|
14
|
+
const workspaceArea = document.getElementById('workspace-area');
|
|
15
|
+
const dropZone = document.getElementById('drop-zone');
|
|
16
|
+
const picker = document.getElementById('target-picker');
|
|
17
|
+
const renameError = document.getElementById('rename-error');
|
|
18
|
+
const status = document.getElementById('status');
|
|
19
|
+
const undo = document.querySelector('#undo');
|
|
20
|
+
const redo = document.querySelector('#redo');
|
|
21
|
+
const popupLayout = document.getElementById('popup-layout');
|
|
22
|
+
const dock = document.querySelector('#dock');
|
|
23
|
+
const motion = `<category name="Motion" colour="#4c97ff" secondaryColour="#3373cc">
|
|
24
|
+
<block type="motion_movesteps"><value name="STEPS"><shadow type="math_number"><field name="NUM">10</field></shadow></value></block>
|
|
25
|
+
<block type="motion_turnright"><value name="DEGREES"><shadow type="math_number"><field name="NUM">15</field></shadow></value></block>
|
|
26
|
+
</category>`;
|
|
27
|
+
const toolbox = (isStage, extensions = '') => `<xml>${isStage ? '' : motion}
|
|
28
|
+
<category name="Looks" colour="#9966ff" secondaryColour="#774dcb">
|
|
29
|
+
${isStage ? '<block type="looks_nextbackdrop"></block><block type="looks_backdropnumbername"></block>' :
|
|
30
|
+
'<block type="looks_show"></block><block type="looks_hide"></block>'}
|
|
31
|
+
</category>
|
|
32
|
+
<category name="My Blocks" colour="#ff6680" secondaryColour="#ff4d6a" custom="PROCEDURE"></category>${extensions}</xml>`;
|
|
33
|
+
const workspace = ScratchBlocks.inject(mount, {
|
|
34
|
+
media: 'static/blocks-media/default/', toolbox: toolbox(false), scrollbars: true, comments: true, zoom: { controls: true }
|
|
35
|
+
});
|
|
36
|
+
let navigation;
|
|
37
|
+
const fieldDialog = createFieldActionDialog(document);
|
|
38
|
+
let fieldRequest = 0;
|
|
39
|
+
let pendingField;
|
|
40
|
+
const extensionPalette = createCodeExtensionPalette(ScratchBlocks, (field, name) => {
|
|
41
|
+
const block = field.sourceBlock_;
|
|
42
|
+
if (!editable || blockedSnapshot || !document.hasFocus() || !block || block.isInFlyout)
|
|
43
|
+
return;
|
|
44
|
+
drain();
|
|
45
|
+
pendingField = { request: ++fieldRequest, blockId: block.id, name };
|
|
46
|
+
const { left, bottom } = field.getSvgRoot().getBoundingClientRect();
|
|
47
|
+
port.postMessage({ type: 'field-editor', targetId, lease, ...pendingField, anchor: { left, bottom } });
|
|
48
|
+
});
|
|
49
|
+
function cancelFieldEditor() {
|
|
50
|
+
pendingField = undefined;
|
|
51
|
+
fieldDialog.close();
|
|
52
|
+
}
|
|
53
|
+
let stageToolbox = false;
|
|
54
|
+
let extensionCategories = '';
|
|
55
|
+
let extensionSignature = '';
|
|
56
|
+
let blockedSnapshot;
|
|
57
|
+
let port;
|
|
58
|
+
let targetId;
|
|
59
|
+
let targets = [];
|
|
60
|
+
let locale;
|
|
61
|
+
let backpackDrop = false;
|
|
62
|
+
let lease;
|
|
63
|
+
let editable = false;
|
|
64
|
+
let history = { canUndo: false, canRedo: false, blocks: {}, scriptRelations: [] };
|
|
65
|
+
let group;
|
|
66
|
+
let edits = [];
|
|
67
|
+
const scriptView = attachScriptView({ workspace, ScratchBlocks, getLocale: () => locale,
|
|
68
|
+
mount: workspaceArea, canShowBlock: () => editable && !blockedSnapshot,
|
|
69
|
+
hasWorkspaceMenu: () => !mount.inert,
|
|
70
|
+
relations: {
|
|
71
|
+
getTarget: () => targetId ? { id: targetId, blocks: { _blocks: history.blocks } } : undefined,
|
|
72
|
+
getRelations: () => history.scriptRelations,
|
|
73
|
+
getTargetName: id => targets.find(candidate => candidate.id === id)?.name,
|
|
74
|
+
navigate: endpoint => port?.postMessage({ type: 'relation-navigate', ...endpoint })
|
|
75
|
+
} });
|
|
76
|
+
installScopedVariableReferences(ScratchBlocks, workspace, () => locale);
|
|
77
|
+
const procedureEditor = installProcedureEditor({
|
|
78
|
+
blocks: ScratchBlocks, workspace, getLocale: () => locale, canEdit: () => editable && !blockedSnapshot && document.hasFocus(),
|
|
79
|
+
commit(apply) {
|
|
80
|
+
drain();
|
|
81
|
+
apply();
|
|
82
|
+
ScratchBlocks.Events.fireNow_();
|
|
83
|
+
flush();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
function buttons() {
|
|
87
|
+
undo.disabled = !editable || !history.canUndo;
|
|
88
|
+
redo.disabled = !editable || !history.canRedo;
|
|
89
|
+
}
|
|
90
|
+
function setEditable(value) {
|
|
91
|
+
editable = value;
|
|
92
|
+
if (!value)
|
|
93
|
+
cancelFieldEditor();
|
|
94
|
+
updateWorkspaceAccess();
|
|
95
|
+
buttons();
|
|
96
|
+
renderTarget();
|
|
97
|
+
}
|
|
98
|
+
function updateWorkspaceAccess() {
|
|
99
|
+
mount.inert = !editable || Boolean(blockedSnapshot);
|
|
100
|
+
dropZone.hidden = editable || !backpackDrop || Boolean(blockedSnapshot);
|
|
101
|
+
status.textContent = blockedSnapshot ?
|
|
102
|
+
(/^ja(?:-|$)/i.test(locale) ? 'この別窓で表示できない拡張ブロックがあります' : 'This view cannot display some extension blocks') :
|
|
103
|
+
(editable ? 'Editing' : 'Read only');
|
|
104
|
+
}
|
|
105
|
+
function updateExtensions(extensions) {
|
|
106
|
+
const signature = JSON.stringify(extensions);
|
|
107
|
+
if (signature === extensionSignature)
|
|
108
|
+
return;
|
|
109
|
+
extensionSignature = signature;
|
|
110
|
+
extensionCategories = extensionPalette.update(extensions);
|
|
111
|
+
const target = targets.find(candidate => candidate.id === targetId);
|
|
112
|
+
stageToolbox = Boolean(target?.isStage);
|
|
113
|
+
workspace.updateToolbox(toolbox(stageToolbox, extensionCategories));
|
|
114
|
+
}
|
|
115
|
+
function renderTarget() {
|
|
116
|
+
const target = targets.find(candidate => candidate.id === targetId);
|
|
117
|
+
const name = target?.name;
|
|
118
|
+
if (target && target.isStage !== stageToolbox) {
|
|
119
|
+
workspace.updateToolbox(toolbox(target.isStage, extensionCategories));
|
|
120
|
+
stageToolbox = target.isStage;
|
|
121
|
+
}
|
|
122
|
+
const code = /^ja(?:-|$)/i.test(locale) ? 'コード' : 'Code';
|
|
123
|
+
document.title = name ? `${code} · ${name}` : code;
|
|
124
|
+
ReactDOM.render(React.createElement(TargetCombobox, { id: "code-target", value: targetId, targets: targets, locale: locale, disabled: !editable, onSelect: nextTargetId => {
|
|
125
|
+
if (!editable || nextTargetId === targetId)
|
|
126
|
+
return;
|
|
127
|
+
renameError.hidden = true;
|
|
128
|
+
drain();
|
|
129
|
+
port.postMessage({ type: 'select-target', targetId, lease, nextTargetId });
|
|
130
|
+
}, onRename: name => {
|
|
131
|
+
if (!editable)
|
|
132
|
+
return;
|
|
133
|
+
renameError.hidden = true;
|
|
134
|
+
drain();
|
|
135
|
+
setEditable(false);
|
|
136
|
+
port.postMessage({ type: 'rename-target', targetId, lease, name });
|
|
137
|
+
} }), picker);
|
|
138
|
+
}
|
|
139
|
+
function renderLayout(canDuplicate) {
|
|
140
|
+
ReactDOM.render(React.createElement(PopupLayout, { window: window, locale: locale, onDock: () => dock.click(), onDuplicate: canDuplicate ? () => {
|
|
141
|
+
drain();
|
|
142
|
+
port.postMessage({ type: 'duplicate-view' });
|
|
143
|
+
} : undefined }), popupLayout);
|
|
144
|
+
}
|
|
145
|
+
function snapshot(message) {
|
|
146
|
+
history = { ...history, ...message };
|
|
147
|
+
if (message.extensions)
|
|
148
|
+
updateExtensions(message.extensions);
|
|
149
|
+
if (message.dynamicShapes)
|
|
150
|
+
extensionPalette.setDynamicShapes(message.dynamicShapes);
|
|
151
|
+
if (message.xml) {
|
|
152
|
+
extensionPalette.setNativeFieldGraphics(message.nativeFieldGraphics ?? []);
|
|
153
|
+
cancelFieldEditor();
|
|
154
|
+
procedureEditor.cancel();
|
|
155
|
+
ScratchBlocks.Events.disable();
|
|
156
|
+
try {
|
|
157
|
+
const xml = ScratchBlocks.Xml.textToDom(message.xml);
|
|
158
|
+
blockedSnapshot = extensionPalette.supports(xml) ? undefined : message.xml;
|
|
159
|
+
if (blockedSnapshot)
|
|
160
|
+
workspace.clear();
|
|
161
|
+
else
|
|
162
|
+
ScratchBlocks.Xml.clearWorkspaceAndLoadFromXml(xml, workspace);
|
|
163
|
+
workspace.clearUndo();
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
ScratchBlocks.Events.enable();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
updateWorkspaceAccess();
|
|
170
|
+
buttons();
|
|
171
|
+
scriptView.relations?.update();
|
|
172
|
+
}
|
|
173
|
+
function flush() {
|
|
174
|
+
if (lease === undefined || !edits.length || workspace.isDragging())
|
|
175
|
+
return;
|
|
176
|
+
port.postMessage({ type: 'edits', targetId, lease, events: edits });
|
|
177
|
+
edits = [];
|
|
178
|
+
}
|
|
179
|
+
function drain() {
|
|
180
|
+
workspace.cancelCurrentGesture();
|
|
181
|
+
ScratchBlocks.hideChaff();
|
|
182
|
+
if (document.activeElement?.matches('.scratchCommentTextarea'))
|
|
183
|
+
document.activeElement.blur();
|
|
184
|
+
ScratchBlocks.Events.fireNow_();
|
|
185
|
+
flush();
|
|
186
|
+
}
|
|
187
|
+
function release(token = lease) {
|
|
188
|
+
if (token === undefined || token !== lease)
|
|
189
|
+
return;
|
|
190
|
+
procedureEditor.cancel();
|
|
191
|
+
drain();
|
|
192
|
+
setEditable(false);
|
|
193
|
+
port.postMessage({ type: 'release', lease });
|
|
194
|
+
lease = undefined;
|
|
195
|
+
edits = [];
|
|
196
|
+
}
|
|
197
|
+
function focus() {
|
|
198
|
+
if (targetId)
|
|
199
|
+
port.postMessage({ type: 'focus' });
|
|
200
|
+
}
|
|
201
|
+
function step(forward) {
|
|
202
|
+
if (!editable || !(forward ? history.canRedo : history.canUndo))
|
|
203
|
+
return;
|
|
204
|
+
drain();
|
|
205
|
+
setEditable(false);
|
|
206
|
+
port.postMessage({ type: 'step', targetId, lease, redo: forward });
|
|
207
|
+
}
|
|
208
|
+
function receive({ data: message }) {
|
|
209
|
+
try {
|
|
210
|
+
switch (message.type) {
|
|
211
|
+
case 'snapshot':
|
|
212
|
+
targetId = message.targetId;
|
|
213
|
+
targets = message.targets;
|
|
214
|
+
locale = message.locale;
|
|
215
|
+
scriptView.labels();
|
|
216
|
+
backpackDrop = message.backpackDrop;
|
|
217
|
+
snapshot(message);
|
|
218
|
+
dock.disabled = false;
|
|
219
|
+
if (message.layoutMenu) {
|
|
220
|
+
dock.hidden = true;
|
|
221
|
+
popupLayout.hidden = false;
|
|
222
|
+
navigation ??= attachWorkspaceNavigation(workspace, ScratchBlocks, { horizontalZoom: true, minimap: true });
|
|
223
|
+
renderLayout(message.canDuplicate);
|
|
224
|
+
}
|
|
225
|
+
setEditable(false);
|
|
226
|
+
if (document.hasFocus())
|
|
227
|
+
focus();
|
|
228
|
+
break;
|
|
229
|
+
case 'can-duplicate':
|
|
230
|
+
renderLayout(message.available);
|
|
231
|
+
break;
|
|
232
|
+
case 'target':
|
|
233
|
+
targetId = message.targetId;
|
|
234
|
+
renameError.hidden = true;
|
|
235
|
+
snapshot(message);
|
|
236
|
+
renderTarget();
|
|
237
|
+
break;
|
|
238
|
+
case 'relation-focus':
|
|
239
|
+
scriptView.setView('free');
|
|
240
|
+
requestAnimationFrame(() => {
|
|
241
|
+
const block = workspace.getBlockById(message.blockId) ?? workspace.getBlockById(message.scriptId);
|
|
242
|
+
if (block)
|
|
243
|
+
fitScriptInWorkspace(workspace, message.scriptId, ScratchBlocks);
|
|
244
|
+
});
|
|
245
|
+
break;
|
|
246
|
+
case 'rename-result':
|
|
247
|
+
if (lease === message.lease) {
|
|
248
|
+
renameError.textContent = /^ja(?:-|$)/i.test(locale) ?
|
|
249
|
+
'この名前は使用できません' : 'This name is unavailable';
|
|
250
|
+
renameError.hidden = message.accepted;
|
|
251
|
+
if (document.hasFocus())
|
|
252
|
+
setEditable(true);
|
|
253
|
+
else
|
|
254
|
+
release();
|
|
255
|
+
}
|
|
256
|
+
break;
|
|
257
|
+
case 'extensions':
|
|
258
|
+
updateExtensions(message.extensions);
|
|
259
|
+
extensionPalette.setDynamicShapes(message.dynamicShapes);
|
|
260
|
+
if (blockedSnapshot)
|
|
261
|
+
snapshot({ ...history, xml: blockedSnapshot, extensions: undefined, dynamicShapes: undefined });
|
|
262
|
+
break;
|
|
263
|
+
case 'targets':
|
|
264
|
+
targets = message.targets;
|
|
265
|
+
renderTarget();
|
|
266
|
+
break;
|
|
267
|
+
case 'grant':
|
|
268
|
+
if (!document.hasFocus()) {
|
|
269
|
+
port.postMessage({ type: 'release', lease: message.lease });
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
snapshot(message);
|
|
273
|
+
lease = message.lease;
|
|
274
|
+
setEditable(true);
|
|
275
|
+
break;
|
|
276
|
+
case 'revoke':
|
|
277
|
+
release(message.lease);
|
|
278
|
+
break;
|
|
279
|
+
case 'flush':
|
|
280
|
+
// Send queued edits without cancelling a drag, then confirm on the same ordered port.
|
|
281
|
+
// Comment text is committed on `change`, so commit what is being typed while keeping
|
|
282
|
+
// the focus and caret in the textarea.
|
|
283
|
+
if (document.activeElement?.matches('.scratchCommentTextarea')) {
|
|
284
|
+
document.activeElement.dispatchEvent(new Event('change'));
|
|
285
|
+
}
|
|
286
|
+
ScratchBlocks.Events.fireNow_();
|
|
287
|
+
flush();
|
|
288
|
+
port.postMessage({ type: 'flushed', request: message.request });
|
|
289
|
+
break;
|
|
290
|
+
case 'field-panel':
|
|
291
|
+
if (editable && document.hasFocus() && lease === message.lease && pendingField?.request === message.request) {
|
|
292
|
+
const selection = pendingField;
|
|
293
|
+
fieldDialog.show(message.panel, action => {
|
|
294
|
+
if (pendingField !== selection || !editable || !document.hasFocus())
|
|
295
|
+
return;
|
|
296
|
+
pendingField = undefined;
|
|
297
|
+
port.postMessage({ type: 'field-action', targetId, lease, ...selection, action });
|
|
298
|
+
}, locale);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
case 'history':
|
|
302
|
+
case 'update':
|
|
303
|
+
case 'relations':
|
|
304
|
+
snapshot(message);
|
|
305
|
+
break;
|
|
306
|
+
case 'step-result':
|
|
307
|
+
if (lease === message.lease) {
|
|
308
|
+
snapshot(message);
|
|
309
|
+
if (document.hasFocus())
|
|
310
|
+
setEditable(true);
|
|
311
|
+
else
|
|
312
|
+
release();
|
|
313
|
+
}
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
setEditable(false);
|
|
319
|
+
port.postMessage({ type: 'failed' });
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
workspace.addChangeListener(event => {
|
|
324
|
+
if (lease === undefined)
|
|
325
|
+
return;
|
|
326
|
+
if (event.type === 'endDrag') {
|
|
327
|
+
queueMicrotask(flush);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const edit = encodeEdit(event);
|
|
331
|
+
if (!edit)
|
|
332
|
+
return;
|
|
333
|
+
if (edits.length && group !== event.group)
|
|
334
|
+
flush();
|
|
335
|
+
group = event.group;
|
|
336
|
+
edits.push(edit);
|
|
337
|
+
queueMicrotask(flush);
|
|
338
|
+
});
|
|
339
|
+
workspace.undo = step;
|
|
340
|
+
workspace.hasUndoStack = () => editable && history.canUndo;
|
|
341
|
+
workspace.hasRedoStack = () => editable && history.canRedo;
|
|
342
|
+
undo.addEventListener('click', () => step(false));
|
|
343
|
+
redo.addEventListener('click', () => step(true));
|
|
344
|
+
dock.addEventListener('click', () => {
|
|
345
|
+
release();
|
|
346
|
+
port.postMessage({ type: 'dock' });
|
|
347
|
+
});
|
|
348
|
+
function acceptsBackpack(event) {
|
|
349
|
+
return !blockedSnapshot && backpackDrop && event.dataTransfer?.types.includes(transferMime);
|
|
350
|
+
}
|
|
351
|
+
workspaceArea.addEventListener('dragover', event => {
|
|
352
|
+
if (!acceptsBackpack(event))
|
|
353
|
+
return;
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
event.dataTransfer.dropEffect = 'copy';
|
|
356
|
+
});
|
|
357
|
+
workspaceArea.addEventListener('drop', event => {
|
|
358
|
+
if (!acceptsBackpack(event))
|
|
359
|
+
return;
|
|
360
|
+
event.preventDefault();
|
|
361
|
+
const matrix = workspace.getCanvas().getScreenCTM();
|
|
362
|
+
if (!matrix)
|
|
363
|
+
return;
|
|
364
|
+
const point = workspace.getParentSvg().createSVGPoint();
|
|
365
|
+
point.x = event.clientX;
|
|
366
|
+
point.y = event.clientY;
|
|
367
|
+
const { x, y } = point.matrixTransform(matrix.inverse());
|
|
368
|
+
port.postMessage({ type: 'backpack-drop', targetId, position: { x, y },
|
|
369
|
+
raw: event.dataTransfer.getData(transferMime) });
|
|
370
|
+
});
|
|
371
|
+
window.addEventListener('focus', focus);
|
|
372
|
+
window.addEventListener('blur', () => release());
|
|
373
|
+
window.addEventListener('resize', () => ScratchBlocks.svgResize(workspace));
|
|
374
|
+
window.addEventListener('pagehide', () => {
|
|
375
|
+
release();
|
|
376
|
+
scriptView.dispose();
|
|
377
|
+
navigation?.dispose();
|
|
378
|
+
port?.postMessage('stage2/disconnect');
|
|
379
|
+
workspace.dispose();
|
|
380
|
+
}, { once: true });
|
|
381
|
+
for (const name of ['keydown', 'keyup']) {
|
|
382
|
+
window.addEventListener(name, event => {
|
|
383
|
+
if (!editable)
|
|
384
|
+
event.stopImmediatePropagation();
|
|
385
|
+
}, true);
|
|
386
|
+
}
|
|
387
|
+
function connect(event) {
|
|
388
|
+
const parent = window.opener;
|
|
389
|
+
if (!parent || event.source !== parent || event.origin !== location.origin ||
|
|
390
|
+
event.data?.type !== 'stage2/connect' || event.data.ticket !== ticket || event.ports.length !== 1)
|
|
391
|
+
return;
|
|
392
|
+
window.removeEventListener('message', connect);
|
|
393
|
+
const { contextMenuIcons } = readWebStartupFeatureFlags(parent.location.search);
|
|
394
|
+
installBlockContextMenuIcons(ScratchBlocks, contextMenuIcons);
|
|
395
|
+
[port] = event.ports;
|
|
396
|
+
port.addEventListener('message', receive);
|
|
397
|
+
port.start();
|
|
398
|
+
port.postMessage('stage2/connected');
|
|
399
|
+
}
|
|
400
|
+
window.addEventListener('message', connect);
|
|
401
|
+
window.opener?.postMessage({ type: 'stage2/ready', ticket }, location.origin);
|
|
402
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare function createContextMenuIcons({ enabled, addonMessages, styles, renderSectionIcon, createSectionIcon }: {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
addonMessages: Record<string, string>;
|
|
5
|
+
styles: Record<string, string>;
|
|
6
|
+
renderSectionIcon?(section: 'functions' | 'calls', className: string): React.ReactNode;
|
|
7
|
+
createSectionIcon?(document: Document, section: 'functions' | 'calls', className: string): SVGSVGElement;
|
|
8
|
+
}): {
|
|
9
|
+
renderMenuIcon: (action: any) => React.ReactNode;
|
|
10
|
+
renderContextMenuIcon: (action: any) => React.ReactNode;
|
|
11
|
+
attachAssetContextMenuIcons: (document: any, getMessages: any) => {
|
|
12
|
+
dispose: () => any;
|
|
13
|
+
};
|
|
14
|
+
installBlockContextMenuIcons: (ScratchBlocks: any, showIcons?: boolean) => void;
|
|
15
|
+
};
|