@kubohiroya/turbowarp-plus-editor-code-views 0.21.2 → 0.21.3
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 +5 -0
- package/dist/code-popout.d.ts +10 -0
- package/dist/code-popout.js +402 -0
- package/dist/context-menu-icons.d.ts +13 -0
- package/dist/context-menu-icons.js +200 -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/package.json +27 -3
- package/src/code-popout.tsx +399 -0
- package/src/context-menu-icons.css +14 -0
- package/src/context-menu-icons.ts +209 -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/trash.svg +3 -0
- package/tsconfig.json +87 -1
package/README.ja.md
CHANGED
|
@@ -137,3 +137,8 @@ getInfo() {
|
|
|
137
137
|
- 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
|
|
138
138
|
- ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
|
|
139
139
|
|
|
140
|
+
|
|
141
|
+
## Editor の実装との責務分担
|
|
142
|
+
|
|
143
|
+
Editor 独自実装の移管元とライセンス確認は [EDITOR-MIGRATION.md](EDITOR-MIGRATION.md) に記録しています。
|
|
144
|
+
追加した公開 API は GUI モジュールを import せず、ホストから必要な値・データ・コールバックを受け取ります。
|
|
@@ -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,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare function createContextMenuIcons({ enabled, addonMessages, styles }: {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
addonMessages: Record<string, string>;
|
|
5
|
+
styles: Record<string, string>;
|
|
6
|
+
}): {
|
|
7
|
+
renderMenuIcon: (action: any) => React.ReactSVGElement;
|
|
8
|
+
renderContextMenuIcon: (action: any) => React.ReactSVGElement;
|
|
9
|
+
attachAssetContextMenuIcons: (document: any, getMessages: any) => {
|
|
10
|
+
dispose: () => any;
|
|
11
|
+
};
|
|
12
|
+
installBlockContextMenuIcons: (ScratchBlocks: any, showIcons?: boolean) => void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export function createContextMenuIcons({ enabled, addonMessages, styles }) {
|
|
4
|
+
const paths = {
|
|
5
|
+
open: 'M13 4h7v7m0-7-9 9m-1-8H4v15h15v-6',
|
|
6
|
+
duplicate: 'M8 8V4h12v12h-4M4 8h12v12H4z',
|
|
7
|
+
copy: 'M8 4h12v12M4 8h12v12H4zM7 12h6m-6 4h4',
|
|
8
|
+
cut: 'M8 8 20 20M8 16 20 4M8 8a3 3 0 1 0-6 0 3 3 0 0 0 6 0Zm0 8a3 3 0 1 0-6 0 3 3 0 0 0 6 0Z',
|
|
9
|
+
paste: 'M9 5H5v16h14V5h-4M9 3h6v5H9zM8 12h8m-8 4h8',
|
|
10
|
+
export: 'M12 3v12m-4-4 4 4 4-4M4 16v5h16v-5',
|
|
11
|
+
import: 'M12 15V3m-4 4 4-4 4 4M4 16v5h16v-5',
|
|
12
|
+
edit: 'm4 16-1 5 5-1L21 7l-4-4L4 16Zm10-10 4 4',
|
|
13
|
+
delete: 'M4 6h16M9 6V3h6v3M6 6l1 15h10l1-15M10 10v7m4-7v7',
|
|
14
|
+
addComment: 'M5 4h14v13h-8l-5 4v-4H5zM9 10h6m-3-3v6',
|
|
15
|
+
removeComment: 'M5 4h14v13h-8l-5 4v-4H5zM9 10h6',
|
|
16
|
+
undo: 'm9 4-5 5 5 5M4 9h10a5 5 0 0 1 0 10h-3',
|
|
17
|
+
redo: 'm15 4 5 5-5 5m5-5H10a5 5 0 0 0 0 10h3',
|
|
18
|
+
restoreDeletion: 'M4 7h16M9 7V4h6v3M6 10l1 10h10l1-10m-7 3-2 2 2 2m-2-2h5',
|
|
19
|
+
findAndReplace: 'M10.5 4a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13zM15 15l5 5',
|
|
20
|
+
cleanup: 'M4 4v16M8 5h12M8 12h8m-8 7h10',
|
|
21
|
+
space: 'M4 4h16M4 20h16M12 7v10m-3-7 3-3 3 3m-6 4 3 3 3-3',
|
|
22
|
+
moveTop: 'M4 3h16m-8 18V7m-5 5 5-5 5 5',
|
|
23
|
+
moveBottom: 'M4 21h16M12 3v14m-5-5 5 5 5-5',
|
|
24
|
+
folderAdd: 'M3 5h7l2 2h9v13H3zM8 13h8m-4-4v8',
|
|
25
|
+
folderRemove: 'M3 5h7l2 2h9v13H3zM8 13h8',
|
|
26
|
+
folderMove: 'M3 5h7l2 2h9v13H3zM7 13h10m-3-3 3 3-3 3',
|
|
27
|
+
collapse: 'm6 3 6 6 6-6M6 21l6-6 6 6',
|
|
28
|
+
expand: 'm6 9 6-6 6 6M6 15l6 6 6-6',
|
|
29
|
+
paletteHorizontal: 'M3 4h18v16H3zM3 9h18M7 6.5h.01m4 0h.01m4 0h.01',
|
|
30
|
+
paletteVertical: 'M3 4h18v16H3zM9 4v16M6 8h.01m0 4h.01m0 4h.01',
|
|
31
|
+
switch: 'M3 8h17m-4-4 4 4-4 4M21 16H4m4-4-4 4 4 4',
|
|
32
|
+
locate: 'M12 2v4m0 12v4M2 12h4m12 0h4M12 6a6 6 0 1 0 0 12 6 6 0 0 0 0-12z',
|
|
33
|
+
help: 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM9 9a3 3 0 1 1 4.5 2.6c-1 .6-1.5 1.2-1.5 2.4m0 3h.01',
|
|
34
|
+
// A ladybug, the mark of debugging in the editor (breakpoints, the debugger).
|
|
35
|
+
debug: 'M5.5 14a6.5 6.5 0 1 0 13 0 6.5 6.5 0 1 0-13 0M9 8.2a3 3 0 0 1 6 0M12 7.5v13M10.3 5.8 8.8 3.6m4.9 2.2 1.5-2.2M9 12h.01M15 12h.01M9 16.5h.01M15 16.5h.01'
|
|
36
|
+
};
|
|
37
|
+
const messageActions = {
|
|
38
|
+
DUPLICATE: 'duplicate',
|
|
39
|
+
ADD_COMMENT: 'addComment',
|
|
40
|
+
REMOVE_COMMENT: 'removeComment',
|
|
41
|
+
DELETE: 'delete',
|
|
42
|
+
DELETE_BLOCK: 'delete',
|
|
43
|
+
DELETE_X_BLOCKS: 'delete',
|
|
44
|
+
DELETE_VARIABLE: 'delete',
|
|
45
|
+
DELETE_LIST: 'delete',
|
|
46
|
+
UNDO: 'undo',
|
|
47
|
+
REDO: 'redo',
|
|
48
|
+
CLEAN_UP: 'cleanup',
|
|
49
|
+
COLLAPSE_ALL: 'collapse',
|
|
50
|
+
EXPAND_ALL: 'expand',
|
|
51
|
+
EDIT_PROCEDURE: 'edit',
|
|
52
|
+
RENAME_VARIABLE: 'edit',
|
|
53
|
+
RENAME_LIST: 'edit',
|
|
54
|
+
SHOW_PROCEDURE_DEFINITION: 'locate',
|
|
55
|
+
PROCEDURES_TO_REPORTER: 'switch',
|
|
56
|
+
PROCEDURES_TO_STATEMENT: 'switch',
|
|
57
|
+
HELP: 'help'
|
|
58
|
+
};
|
|
59
|
+
const blockAddonActions = {
|
|
60
|
+
'editor-devtools/paste': 'paste',
|
|
61
|
+
'editor-devtools/make-space': 'space',
|
|
62
|
+
'editor-devtools/copy-all': 'copy',
|
|
63
|
+
'editor-devtools/copy-block': 'copy',
|
|
64
|
+
'editor-devtools/cut-block': 'cut',
|
|
65
|
+
'editor-devtools/swap': 'switch',
|
|
66
|
+
'blocks2image/export_all_to_SVG': 'export',
|
|
67
|
+
'blocks2image/export_all_to_PNG': 'export',
|
|
68
|
+
'blocks2image/export_selected_to_SVG': 'export',
|
|
69
|
+
'blocks2image/export_selected_to_PNG': 'export',
|
|
70
|
+
'swap-local-global/edit-variable-option': 'edit',
|
|
71
|
+
'swap-local-global/edit-list-option': 'edit',
|
|
72
|
+
'swap-local-global/to-local': 'switch',
|
|
73
|
+
'swap-local-global/to-global': 'switch'
|
|
74
|
+
};
|
|
75
|
+
const assetActions = {
|
|
76
|
+
'gui.spriteSelectorItem.contextMenuDuplicate': 'duplicate',
|
|
77
|
+
'gui.spriteSelectorItem.contextMenuExport': 'export',
|
|
78
|
+
'tw.spriteSelectorItem.rename': 'edit',
|
|
79
|
+
'gui.spriteSelectorItem.contextMenuDelete': 'delete'
|
|
80
|
+
};
|
|
81
|
+
const defaultAssetActions = new Map([
|
|
82
|
+
['duplicate', 'duplicate'], ['export', 'export'], ['rename', 'edit'], ['delete', 'delete'],
|
|
83
|
+
['Open', 'open'], ['開く', 'open']
|
|
84
|
+
]);
|
|
85
|
+
const assetAddonActions = {
|
|
86
|
+
'move-to-top-bottom/top': 'moveTop',
|
|
87
|
+
'move-to-top-bottom/bottom': 'moveBottom'
|
|
88
|
+
};
|
|
89
|
+
const folderActions = {
|
|
90
|
+
'sa-folders-create-folder': 'folderAdd',
|
|
91
|
+
'sa-folders-rename-folder': 'edit',
|
|
92
|
+
'sa-folders-remove-folder': 'folderRemove',
|
|
93
|
+
'sa-folders-remove-from-folder': 'folderRemove',
|
|
94
|
+
'sa-folders-add-to-folder': 'folderMove'
|
|
95
|
+
};
|
|
96
|
+
// The GUI loads addon translations into this same catalog before registering their menus.
|
|
97
|
+
const matchesMessage = (text, message, placeholder) => {
|
|
98
|
+
if (typeof message !== 'string')
|
|
99
|
+
return false;
|
|
100
|
+
if (text === message)
|
|
101
|
+
return true;
|
|
102
|
+
const [prefix, suffix] = message.split(placeholder);
|
|
103
|
+
return suffix !== undefined && text.length > prefix.length + suffix.length &&
|
|
104
|
+
text.startsWith(prefix) && text.endsWith(suffix);
|
|
105
|
+
};
|
|
106
|
+
const localizedAction = (text, messages, actions, placeholder) => {
|
|
107
|
+
const id = Object.keys(actions).find(key => matchesMessage(text, messages[key], placeholder));
|
|
108
|
+
return actions[id];
|
|
109
|
+
};
|
|
110
|
+
const renderMenuIcon = action => paths[action] ? React.createElement('svg', {
|
|
111
|
+
className: styles.icon, 'aria-hidden': 'true', focusable: 'false', viewBox: '0 0 24 24'
|
|
112
|
+
}, React.createElement('path', { d: paths[action] })) : null;
|
|
113
|
+
const renderContextMenuIcon = action => enabled ? renderMenuIcon(action) : null;
|
|
114
|
+
const svgNamespace = 'http://www.w3.org/2000/svg';
|
|
115
|
+
const installed = new WeakSet();
|
|
116
|
+
function createContextMenuIcon(document, action) {
|
|
117
|
+
if (!Object.hasOwn(paths, action))
|
|
118
|
+
return null;
|
|
119
|
+
const icon = document.createElementNS(svgNamespace, 'svg');
|
|
120
|
+
icon.setAttribute('class', styles.icon);
|
|
121
|
+
icon.setAttribute('aria-hidden', 'true');
|
|
122
|
+
icon.setAttribute('focusable', 'false');
|
|
123
|
+
icon.setAttribute('viewBox', '0 0 24 24');
|
|
124
|
+
const path = document.createElementNS(svgNamespace, 'path');
|
|
125
|
+
path.setAttribute('d', paths[action]);
|
|
126
|
+
icon.appendChild(path);
|
|
127
|
+
return icon;
|
|
128
|
+
}
|
|
129
|
+
function attachAssetContextMenuIcons(document, getMessages) {
|
|
130
|
+
if (!enabled || !getMessages)
|
|
131
|
+
return null;
|
|
132
|
+
const window = document.defaultView;
|
|
133
|
+
if (!window)
|
|
134
|
+
return null;
|
|
135
|
+
const onShow = ({ detail }) => {
|
|
136
|
+
const target = detail?.target;
|
|
137
|
+
if (!target?.matches('[class*="sprite-selector-item_sprite-selector-item_"]'))
|
|
138
|
+
return;
|
|
139
|
+
const menu = target.querySelector('nav.react-contextmenu');
|
|
140
|
+
if (!menu)
|
|
141
|
+
return;
|
|
142
|
+
const messages = getMessages() || {};
|
|
143
|
+
for (const item of menu.children) {
|
|
144
|
+
if (!item.classList.contains('react-contextmenu-item'))
|
|
145
|
+
continue;
|
|
146
|
+
if (item.hasAttribute('data-tw-plus-menu-icon'))
|
|
147
|
+
continue;
|
|
148
|
+
const text = item.textContent;
|
|
149
|
+
const folderClass = Object.keys(folderActions).find(name => item.classList.contains(name));
|
|
150
|
+
const action = item.classList.contains('sa-ctx-menu') ?
|
|
151
|
+
folderActions[folderClass] || localizedAction(text, addonMessages, assetAddonActions) :
|
|
152
|
+
localizedAction(text, messages, assetActions) || defaultAssetActions.get(text);
|
|
153
|
+
item.querySelector(`.${styles.icon}`)?.remove();
|
|
154
|
+
const icon = createContextMenuIcon(item.ownerDocument, action);
|
|
155
|
+
if (icon)
|
|
156
|
+
item.prepend(icon);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
window.addEventListener('REACT_CONTEXTMENU_SHOW', onShow);
|
|
160
|
+
return { dispose: () => window.removeEventListener('REACT_CONTEXTMENU_SHOW', onShow) };
|
|
161
|
+
}
|
|
162
|
+
function installBlockContextMenuIcons(ScratchBlocks, showIcons = enabled) {
|
|
163
|
+
if (!showIcons || installed.has(ScratchBlocks))
|
|
164
|
+
return;
|
|
165
|
+
const { ContextMenu, WidgetDiv } = ScratchBlocks;
|
|
166
|
+
const populate = ContextMenu.populate_;
|
|
167
|
+
const data = ScratchBlocks.Constants.Data;
|
|
168
|
+
const variableOptionCallback = data.VARIABLE_OPTION_CALLBACK_FACTORY;
|
|
169
|
+
const variableChoices = new WeakSet();
|
|
170
|
+
// Blockly uses the project's variable or list name as the label for these choices.
|
|
171
|
+
data.VARIABLE_OPTION_CALLBACK_FACTORY = function (...args) {
|
|
172
|
+
const callback = variableOptionCallback.apply(this, args);
|
|
173
|
+
variableChoices.add(callback);
|
|
174
|
+
return callback;
|
|
175
|
+
};
|
|
176
|
+
// Decorate the final labels before Blockly measures the menu, after addons have read the strings.
|
|
177
|
+
ContextMenu.populate_ = function (options, rtl) {
|
|
178
|
+
const document = WidgetDiv.DIV.ownerDocument;
|
|
179
|
+
const { Msg } = ScratchBlocks;
|
|
180
|
+
const block = ScratchBlocks.mainWorkspace?.currentGesture_?.targetBlock_;
|
|
181
|
+
const isArgument = block?.type === 'argument_reporter_string_number' ||
|
|
182
|
+
block?.type === 'argument_reporter_boolean';
|
|
183
|
+
const decorated = options.map(option => {
|
|
184
|
+
const { text } = option;
|
|
185
|
+
if (typeof text !== 'string')
|
|
186
|
+
return option;
|
|
187
|
+
const action = option.twPlusMenuIcon || (variableChoices.has(option.callback) && 'switch') ||
|
|
188
|
+
localizedAction(text, Msg, messageActions, '%1') ||
|
|
189
|
+
localizedAction(text, addonMessages, blockAddonActions, '{var}') ||
|
|
190
|
+
((isArgument || Object.keys(addonMessages).some(key => key.startsWith('block-switching/') &&
|
|
191
|
+
addonMessages[key] === text)) && 'switch');
|
|
192
|
+
const icon = createContextMenuIcon(document, action);
|
|
193
|
+
return icon ? { ...option, text: [icon, document.createTextNode(text)] } : option;
|
|
194
|
+
});
|
|
195
|
+
return populate.call(this, decorated, rtl);
|
|
196
|
+
};
|
|
197
|
+
installed.add(ScratchBlocks);
|
|
198
|
+
}
|
|
199
|
+
return { renderMenuIcon, renderContextMenuIcon, attachAssetContextMenuIcons, installBlockContextMenuIcons };
|
|
200
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SurfaceHost } from '@kubohiroya/turbowarp-plus-editor-windows/surfaces';
|
|
2
|
+
export declare function createDesktopCode({ host, layoutMenu }: {
|
|
3
|
+
host: SurfaceHost<any, any> | null;
|
|
4
|
+
layoutMenu: boolean;
|
|
5
|
+
}): {
|
|
6
|
+
desktopCodeEnabled: boolean;
|
|
7
|
+
openDesktopCode: () => any;
|
|
8
|
+
attachDesktopCode: (context: any) => {
|
|
9
|
+
dispose(): void;
|
|
10
|
+
};
|
|
11
|
+
};
|