@kubohiroya/turbowarp-plus-editor-workbench 0.15.0 → 0.17.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/NOTICE +26 -0
- package/README.ja.md +33 -0
- package/README.md +33 -0
- package/dist/backpack-script.d.ts +2 -0
- package/dist/backpack-script.js +23 -0
- package/dist/backpack.d.ts +60 -0
- package/dist/backpack.js +226 -0
- package/dist/browser-tab-title.d.ts +2 -0
- package/dist/browser-tab-title.js +5 -0
- package/dist/busy-cursor.d.ts +22 -0
- package/dist/busy-cursor.js +107 -0
- package/dist/debugger-breakpoints.d.ts +91 -0
- package/dist/debugger-breakpoints.js +310 -0
- package/dist/edit-menu.d.ts +57 -0
- package/dist/edit-menu.js +99 -0
- package/dist/info-menu.d.ts +39 -0
- package/dist/info-menu.js +157 -0
- package/dist/project-file-dialogs.d.ts +70 -0
- package/dist/project-file-dialogs.js +625 -0
- package/dist/project-file-menu.d.ts +43 -0
- package/dist/project-file-menu.js +90 -0
- package/dist/project-load-progress-view.d.ts +6 -0
- package/dist/project-load-progress-view.js +66 -0
- package/dist/project-load-progress.d.ts +26 -0
- package/dist/project-load-progress.js +104 -0
- package/dist/project-search-box.d.ts +43 -0
- package/dist/project-search-box.js +437 -0
- package/dist/project-size-approval.d.ts +10 -0
- package/dist/project-size-approval.js +64 -0
- package/dist/stage-controls-view.d.ts +45 -0
- package/dist/stage-controls-view.js +127 -0
- package/dist/stage-controls.d.ts +98 -0
- package/dist/stage-controls.js +110 -0
- package/package.json +101 -28
- package/src/backpack-script.ts +21 -0
- package/src/backpack.tsx +260 -0
- package/src/browser-tab-title.ts +6 -0
- package/src/busy-cursor.ts +108 -0
- package/src/debugger-breakpoints.ts +413 -0
- package/src/debugger-panel.css +13 -0
- package/src/edit-menu.css +42 -0
- package/src/edit-menu.tsx +139 -0
- package/src/info-menu.css +172 -0
- package/src/info-menu.tsx +221 -0
- package/src/project-file-dialogs.ts +654 -0
- package/src/project-file-menu.css +94 -0
- package/src/project-file-menu.tsx +137 -0
- package/src/project-load-progress-view.tsx +75 -0
- package/src/project-load-progress.css +57 -0
- package/src/project-load-progress.ts +118 -0
- package/src/project-search-box.tsx +469 -0
- package/src/project-search.css +116 -0
- package/src/project-size-approval.ts +74 -0
- package/src/stage-controls-view.tsx +178 -0
- package/src/stage-controls.css +192 -0
- package/src/stage-controls.ts +124 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Breakpoints marked on blocks instead of placed as blocks.
|
|
3
|
+
*
|
|
4
|
+
* The editor add-on's debugger stops a project when a "breakpoint" block runs. That block is part
|
|
5
|
+
* of the project, so it is saved and shipped unless someone remembers to take it out. Here a block
|
|
6
|
+
* is only marked in the editor: the mark lives in this session, never in the project, and the
|
|
7
|
+
* project stops right before a marked block runs.
|
|
8
|
+
*
|
|
9
|
+
* Stopping is the add-on's own pause (its hidden `sa-pause` block), so its Threads tab, Step and
|
|
10
|
+
* Continue work unchanged. Without the add-on nothing can pause, and no menu item is offered.
|
|
11
|
+
*
|
|
12
|
+
* The VM is not changed. Two of its methods are wrapped, and only while at least one mark exists:
|
|
13
|
+
*
|
|
14
|
+
* - `getOpcodeFunction`: the block functions the interpreter caches. When a thread arrives at a
|
|
15
|
+
* marked block, the first function the block runs pauses instead, and the frame waits the way it
|
|
16
|
+
* waits for a reporter. Continuing runs the block from its start. The mark is kept in the
|
|
17
|
+
* frame's execution context, which the VM clears when the thread moves to another block, so a
|
|
18
|
+
* block that waits in place or a loop that comes back to its head stops only on arrival.
|
|
19
|
+
* - `_pushThread` and `_restartThread`: compiled scripts run no per-block functions, so a script
|
|
20
|
+
* that starts in a sprite with a mark is not compiled. Sprites without a mark compile as before.
|
|
21
|
+
* A script that was already running compiled keeps running until it starts again.
|
|
22
|
+
*/
|
|
23
|
+
export declare const breakpointClass = "tw-plus-breakpoint";
|
|
24
|
+
export declare const breakpointDotClass = "tw-plus-breakpoint-dot";
|
|
25
|
+
export interface Breakpoints {
|
|
26
|
+
/** Whether a pause is possible: the debugger add-on is on and has registered its pause. */
|
|
27
|
+
canPause(): boolean;
|
|
28
|
+
has(blocks: object, blockId: string): boolean;
|
|
29
|
+
set(blocks: object, blockId: string, marked: boolean): void;
|
|
30
|
+
toggle(blocks: object, blockId: string): boolean;
|
|
31
|
+
/** The marked blocks of one block container that still exist. */
|
|
32
|
+
ids(blocks: object): string[];
|
|
33
|
+
/** Whether a script in this container is running compiled, so a new mark misses it until it starts again. */
|
|
34
|
+
runsCompiled(blocks: object): boolean;
|
|
35
|
+
clear(): void;
|
|
36
|
+
subscribe(listener: () => void): () => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The breakpoints of one VM. Every caller gets the same instance, so the menu and the runtime
|
|
40
|
+
* agree on what is marked.
|
|
41
|
+
* @param {object} vm The VM.
|
|
42
|
+
* @returns {object} The breakpoints.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getBreakpoints(vm: object): Breakpoints;
|
|
45
|
+
interface ScratchBlocksLike {
|
|
46
|
+
ContextMenu: {
|
|
47
|
+
show(event: unknown, items: MenuItem[], rtl: boolean): void;
|
|
48
|
+
};
|
|
49
|
+
getMainWorkspace?(): WorkspaceLike | null;
|
|
50
|
+
mainWorkspace?: WorkspaceLike | null;
|
|
51
|
+
}
|
|
52
|
+
interface MenuItem {
|
|
53
|
+
text: string;
|
|
54
|
+
enabled: boolean;
|
|
55
|
+
callback(): void;
|
|
56
|
+
separator?: boolean;
|
|
57
|
+
}
|
|
58
|
+
interface BlockSvgLike {
|
|
59
|
+
id: string;
|
|
60
|
+
isShadow?(): boolean;
|
|
61
|
+
previousConnection?: unknown;
|
|
62
|
+
workspace?: WorkspaceLike;
|
|
63
|
+
getSvgRoot?(): Element | null;
|
|
64
|
+
}
|
|
65
|
+
interface WorkspaceLike {
|
|
66
|
+
isFlyout?: boolean;
|
|
67
|
+
currentGesture_?: {
|
|
68
|
+
targetBlock_?: BlockSvgLike | null;
|
|
69
|
+
flyout_?: unknown;
|
|
70
|
+
} | null;
|
|
71
|
+
getBlockById(id: string): BlockSvgLike | null;
|
|
72
|
+
getParentSvg?(): Element | null;
|
|
73
|
+
addChangeListener?(listener: (event: unknown) => void): unknown;
|
|
74
|
+
removeChangeListener?(listener: unknown): void;
|
|
75
|
+
}
|
|
76
|
+
interface BreakpointMenuOptions {
|
|
77
|
+
vm: object;
|
|
78
|
+
getBlockly(): Promise<ScratchBlocksLike> | ScratchBlocksLike;
|
|
79
|
+
getLocale?(): string | undefined;
|
|
80
|
+
}
|
|
81
|
+
export declare const breakpointMenuText: (locale: string | undefined, marked: boolean, runsCompiled: boolean) => "ブレークポイントを外す" | "ブレークポイントを設定(次に始めたときから止まる)" | "ブレークポイントを設定" | "Remove breakpoint" | "Add breakpoint (stops from the next start)" | "Add breakpoint";
|
|
82
|
+
/**
|
|
83
|
+
* Offer "Add breakpoint" on the context menu of a command block in the code area, and outline the
|
|
84
|
+
* marked blocks of the sprite being edited.
|
|
85
|
+
* @param {object} options The VM, a way to get ScratchBlocks, and the GUI's language.
|
|
86
|
+
* @returns {object} Something to dispose.
|
|
87
|
+
*/
|
|
88
|
+
export declare function attachBreakpointMenu(options: BreakpointMenuOptions): {
|
|
89
|
+
dispose(): void;
|
|
90
|
+
};
|
|
91
|
+
export {};
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Breakpoints marked on blocks instead of placed as blocks.
|
|
3
|
+
*
|
|
4
|
+
* The editor add-on's debugger stops a project when a "breakpoint" block runs. That block is part
|
|
5
|
+
* of the project, so it is saved and shipped unless someone remembers to take it out. Here a block
|
|
6
|
+
* is only marked in the editor: the mark lives in this session, never in the project, and the
|
|
7
|
+
* project stops right before a marked block runs.
|
|
8
|
+
*
|
|
9
|
+
* Stopping is the add-on's own pause (its hidden `sa-pause` block), so its Threads tab, Step and
|
|
10
|
+
* Continue work unchanged. Without the add-on nothing can pause, and no menu item is offered.
|
|
11
|
+
*
|
|
12
|
+
* The VM is not changed. Two of its methods are wrapped, and only while at least one mark exists:
|
|
13
|
+
*
|
|
14
|
+
* - `getOpcodeFunction`: the block functions the interpreter caches. When a thread arrives at a
|
|
15
|
+
* marked block, the first function the block runs pauses instead, and the frame waits the way it
|
|
16
|
+
* waits for a reporter. Continuing runs the block from its start. The mark is kept in the
|
|
17
|
+
* frame's execution context, which the VM clears when the thread moves to another block, so a
|
|
18
|
+
* block that waits in place or a loop that comes back to its head stops only on arrival.
|
|
19
|
+
* - `_pushThread` and `_restartThread`: compiled scripts run no per-block functions, so a script
|
|
20
|
+
* that starts in a sprite with a mark is not compiled. Sprites without a mark compile as before.
|
|
21
|
+
* A script that was already running compiled keeps running until it starts again.
|
|
22
|
+
*/
|
|
23
|
+
const PROMISE_WAIT = 1;
|
|
24
|
+
const pauseProcedure = 'sa-pause';
|
|
25
|
+
const hitKey = Symbol('tw-plus-breakpoint-hit');
|
|
26
|
+
export const breakpointClass = 'tw-plus-breakpoint';
|
|
27
|
+
export const breakpointDotClass = 'tw-plus-breakpoint-dot';
|
|
28
|
+
const svgNamespace = 'http://www.w3.org/2000/svg';
|
|
29
|
+
const breakpointsByVm = new WeakMap();
|
|
30
|
+
const ownMethod = (object, key) => Object.prototype.hasOwnProperty.call(object, key) ? { value: object[key] } : null;
|
|
31
|
+
const restoreMethod = (object, key, saved) => {
|
|
32
|
+
if (saved)
|
|
33
|
+
object[key] = saved.value;
|
|
34
|
+
else
|
|
35
|
+
delete object[key];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The breakpoints of one VM. Every caller gets the same instance, so the menu and the runtime
|
|
39
|
+
* agree on what is marked.
|
|
40
|
+
* @param {object} vm The VM.
|
|
41
|
+
* @returns {object} The breakpoints.
|
|
42
|
+
*/
|
|
43
|
+
export function getBreakpoints(vm) {
|
|
44
|
+
let breakpoints = breakpointsByVm.get(vm);
|
|
45
|
+
if (!breakpoints) {
|
|
46
|
+
breakpoints = createBreakpoints(vm);
|
|
47
|
+
breakpointsByVm.set(vm, breakpoints);
|
|
48
|
+
}
|
|
49
|
+
return breakpoints;
|
|
50
|
+
}
|
|
51
|
+
function createBreakpoints(vm) {
|
|
52
|
+
const runtime = vm.runtime;
|
|
53
|
+
const marks = new Map();
|
|
54
|
+
const listeners = new Set();
|
|
55
|
+
const gated = new WeakMap();
|
|
56
|
+
let installed = null;
|
|
57
|
+
const notify = () => {
|
|
58
|
+
for (const listener of [...listeners])
|
|
59
|
+
listener();
|
|
60
|
+
};
|
|
61
|
+
const marked = (blocks, blockId) => Boolean(blocks && blockId && marks.get(blocks)?.has(blockId));
|
|
62
|
+
const pause = (thread) => {
|
|
63
|
+
const block = runtime.getAddonBlock?.(pauseProcedure);
|
|
64
|
+
if (!block)
|
|
65
|
+
return false;
|
|
66
|
+
block.callback({}, { thread });
|
|
67
|
+
return thread.status === PROMISE_WAIT;
|
|
68
|
+
};
|
|
69
|
+
const gate = (blockFunction) => {
|
|
70
|
+
let wrapped = gated.get(blockFunction);
|
|
71
|
+
if (wrapped)
|
|
72
|
+
return wrapped;
|
|
73
|
+
wrapped = function (args, util) {
|
|
74
|
+
const thread = util?.thread;
|
|
75
|
+
if (thread && !thread.isCompiled && marked(thread.blockContainer, thread.peekStack())) {
|
|
76
|
+
const context = util.stackFrame;
|
|
77
|
+
if (context && !context[hitKey]) {
|
|
78
|
+
context[hitKey] = true;
|
|
79
|
+
if (pause(thread)) {
|
|
80
|
+
// The interpreter keeps the frame and runs the block again from its start
|
|
81
|
+
// when the thread runs again, as it does after a reporter it waited for.
|
|
82
|
+
thread.peekStackFrame().waitingReporter = true;
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return blockFunction.call(this, args, util);
|
|
88
|
+
};
|
|
89
|
+
gated.set(blockFunction, wrapped);
|
|
90
|
+
return wrapped;
|
|
91
|
+
};
|
|
92
|
+
const withoutCompiler = (blocks, start) => {
|
|
93
|
+
const options = runtime.compilerOptions;
|
|
94
|
+
if (!options?.enabled || !blocks || !marks.get(blocks)?.size)
|
|
95
|
+
return start();
|
|
96
|
+
runtime.compilerOptions = { ...options, enabled: false };
|
|
97
|
+
try {
|
|
98
|
+
return start();
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
runtime.compilerOptions = options;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const install = () => {
|
|
105
|
+
if (installed)
|
|
106
|
+
return;
|
|
107
|
+
const saved = {
|
|
108
|
+
getOpcodeFunction: ownMethod(runtime, 'getOpcodeFunction'),
|
|
109
|
+
_pushThread: ownMethod(runtime, '_pushThread'),
|
|
110
|
+
_restartThread: ownMethod(runtime, '_restartThread')
|
|
111
|
+
};
|
|
112
|
+
const getOpcodeFunction = runtime.getOpcodeFunction;
|
|
113
|
+
const pushThread = runtime._pushThread;
|
|
114
|
+
const restartThread = runtime._restartThread;
|
|
115
|
+
runtime.getOpcodeFunction = function (opcode) {
|
|
116
|
+
const blockFunction = getOpcodeFunction.call(this, opcode);
|
|
117
|
+
return typeof blockFunction === 'function' ? gate(blockFunction) : blockFunction;
|
|
118
|
+
};
|
|
119
|
+
runtime._pushThread = function (id, target, options) {
|
|
120
|
+
return withoutCompiler(target?.blocks, () => pushThread.call(this, id, target, options));
|
|
121
|
+
};
|
|
122
|
+
runtime._restartThread = function (thread) {
|
|
123
|
+
return withoutCompiler(thread?.blockContainer, () => restartThread.call(this, thread));
|
|
124
|
+
};
|
|
125
|
+
// The interpreter caches each block's function. Dropping the caches makes it look them up again.
|
|
126
|
+
runtime.resetAllCaches?.();
|
|
127
|
+
installed = () => {
|
|
128
|
+
restoreMethod(runtime, 'getOpcodeFunction', saved.getOpcodeFunction);
|
|
129
|
+
restoreMethod(runtime, '_pushThread', saved._pushThread);
|
|
130
|
+
restoreMethod(runtime, '_restartThread', saved._restartThread);
|
|
131
|
+
runtime.resetAllCaches?.();
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
const uninstall = () => {
|
|
135
|
+
const restore = installed;
|
|
136
|
+
installed = null;
|
|
137
|
+
restore?.();
|
|
138
|
+
};
|
|
139
|
+
const update = () => {
|
|
140
|
+
for (const [blocks, ids] of marks) {
|
|
141
|
+
if (!ids.size)
|
|
142
|
+
marks.delete(blocks);
|
|
143
|
+
}
|
|
144
|
+
if (marks.size)
|
|
145
|
+
install();
|
|
146
|
+
else
|
|
147
|
+
uninstall();
|
|
148
|
+
notify();
|
|
149
|
+
};
|
|
150
|
+
const clear = () => {
|
|
151
|
+
if (!marks.size)
|
|
152
|
+
return;
|
|
153
|
+
marks.clear();
|
|
154
|
+
update();
|
|
155
|
+
};
|
|
156
|
+
// A project load replaces every block container, so the marks have nothing left to point at.
|
|
157
|
+
runtime.on?.('RUNTIME_DISPOSED', clear);
|
|
158
|
+
const breakpoints = {
|
|
159
|
+
canPause: () => Boolean(runtime.getAddonBlock?.(pauseProcedure)),
|
|
160
|
+
has: (blocks, blockId) => marked(blocks, blockId),
|
|
161
|
+
set(blocks, blockId, on) {
|
|
162
|
+
if (marked(blocks, blockId) === on)
|
|
163
|
+
return;
|
|
164
|
+
if (on) {
|
|
165
|
+
let ids = marks.get(blocks);
|
|
166
|
+
if (!ids)
|
|
167
|
+
marks.set(blocks, ids = new Set());
|
|
168
|
+
ids.add(blockId);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
marks.get(blocks)?.delete(blockId);
|
|
172
|
+
}
|
|
173
|
+
update();
|
|
174
|
+
},
|
|
175
|
+
toggle(blocks, blockId) {
|
|
176
|
+
const on = !marked(blocks, blockId);
|
|
177
|
+
breakpoints.set(blocks, blockId, on);
|
|
178
|
+
return on;
|
|
179
|
+
},
|
|
180
|
+
ids(blocks) {
|
|
181
|
+
const ids = marks.get(blocks);
|
|
182
|
+
if (!ids)
|
|
183
|
+
return [];
|
|
184
|
+
return [...ids].filter(id => blocks.getBlock?.(id));
|
|
185
|
+
},
|
|
186
|
+
runsCompiled: blocks => runtime.threads.some(thread => thread.isCompiled && thread.blockContainer === blocks),
|
|
187
|
+
clear,
|
|
188
|
+
subscribe(listener) {
|
|
189
|
+
listeners.add(listener);
|
|
190
|
+
return () => listeners.delete(listener);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
return breakpoints;
|
|
194
|
+
}
|
|
195
|
+
const japanese = (locale) => /^ja(?:-|$)/i.test(locale || '');
|
|
196
|
+
export const breakpointMenuText = (locale, marked, runsCompiled) => {
|
|
197
|
+
if (japanese(locale)) {
|
|
198
|
+
if (marked)
|
|
199
|
+
return 'ブレークポイントを外す';
|
|
200
|
+
return runsCompiled ? 'ブレークポイントを設定(次に始めたときから止まる)' : 'ブレークポイントを設定';
|
|
201
|
+
}
|
|
202
|
+
if (marked)
|
|
203
|
+
return 'Remove breakpoint';
|
|
204
|
+
return runsCompiled ? 'Add breakpoint (stops from the next start)' : 'Add breakpoint';
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Offer "Add breakpoint" on the context menu of a command block in the code area, and outline the
|
|
208
|
+
* marked blocks of the sprite being edited.
|
|
209
|
+
* @param {object} options The VM, a way to get ScratchBlocks, and the GUI's language.
|
|
210
|
+
* @returns {object} Something to dispose.
|
|
211
|
+
*/
|
|
212
|
+
export function attachBreakpointMenu(options) {
|
|
213
|
+
const vm = options.vm;
|
|
214
|
+
// Without a runtime there is nothing to pause.
|
|
215
|
+
if (!vm?.runtime)
|
|
216
|
+
return { dispose() { } };
|
|
217
|
+
const breakpoints = getBreakpoints(vm);
|
|
218
|
+
let disposed = false;
|
|
219
|
+
let detach = () => { };
|
|
220
|
+
const editingBlocks = () => vm.editingTarget?.blocks;
|
|
221
|
+
const connect = (ScratchBlocks) => {
|
|
222
|
+
const workspace = () => ScratchBlocks.getMainWorkspace?.() || ScratchBlocks.mainWorkspace || null;
|
|
223
|
+
const menu = ScratchBlocks.ContextMenu;
|
|
224
|
+
const show = menu.show;
|
|
225
|
+
let active = true;
|
|
226
|
+
const patched = function (event, items, rtl) {
|
|
227
|
+
const main = workspace();
|
|
228
|
+
const gesture = main?.currentGesture_;
|
|
229
|
+
const block = gesture && !gesture.flyout_ ? gesture.targetBlock_ : null;
|
|
230
|
+
const blocks = editingBlocks();
|
|
231
|
+
if (active && block && blocks && block.previousConnection && !block.isShadow?.() &&
|
|
232
|
+
block.workspace === main && breakpoints.canPause()) {
|
|
233
|
+
const on = breakpoints.has(blocks, block.id);
|
|
234
|
+
items = [...items, {
|
|
235
|
+
text: breakpointMenuText(options.getLocale?.(), on, !on && breakpoints.runsCompiled(blocks)),
|
|
236
|
+
enabled: true,
|
|
237
|
+
separator: true,
|
|
238
|
+
callback: () => breakpoints.toggle(blocks, block.id)
|
|
239
|
+
}];
|
|
240
|
+
}
|
|
241
|
+
return show.call(this, event, items, rtl);
|
|
242
|
+
};
|
|
243
|
+
menu.show = patched;
|
|
244
|
+
let frame = 0;
|
|
245
|
+
const refresh = () => {
|
|
246
|
+
frame = 0;
|
|
247
|
+
const main = workspace();
|
|
248
|
+
const root = main?.getParentSvg?.();
|
|
249
|
+
if (!main || !root)
|
|
250
|
+
return;
|
|
251
|
+
for (const element of root.querySelectorAll(`.${breakpointClass}`))
|
|
252
|
+
element.classList.remove(breakpointClass);
|
|
253
|
+
for (const dot of root.querySelectorAll(`.${breakpointDotClass}`))
|
|
254
|
+
dot.remove();
|
|
255
|
+
const blocks = editingBlocks();
|
|
256
|
+
if (!blocks)
|
|
257
|
+
return;
|
|
258
|
+
for (const id of breakpoints.ids(blocks)) {
|
|
259
|
+
const element = main.getBlockById(id)?.getSvgRoot?.();
|
|
260
|
+
if (!element)
|
|
261
|
+
continue;
|
|
262
|
+
element.classList.add(breakpointClass);
|
|
263
|
+
// A dot left of the block, like the gutter mark of a code editor. The outline alone is
|
|
264
|
+
// hard to see on the orange and yellow categories.
|
|
265
|
+
const dot = element.ownerDocument.createElementNS(svgNamespace, 'circle');
|
|
266
|
+
dot.setAttribute('class', breakpointDotClass);
|
|
267
|
+
dot.setAttribute('cx', '-9');
|
|
268
|
+
dot.setAttribute('cy', '12');
|
|
269
|
+
dot.setAttribute('r', '6');
|
|
270
|
+
element.append(dot);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
const schedule = () => {
|
|
274
|
+
if (frame)
|
|
275
|
+
return;
|
|
276
|
+
const view = globalThis.requestAnimationFrame ? globalThis : null;
|
|
277
|
+
frame = view ? view.requestAnimationFrame(refresh) : setTimeout(refresh, 0);
|
|
278
|
+
};
|
|
279
|
+
const unsubscribe = breakpoints.subscribe(schedule);
|
|
280
|
+
const main = workspace();
|
|
281
|
+
const changeListener = main?.addChangeListener?.(schedule);
|
|
282
|
+
// Switching sprites reloads the workspace, and the new block elements have no outline yet.
|
|
283
|
+
vm.on?.('workspaceUpdate', schedule);
|
|
284
|
+
schedule();
|
|
285
|
+
detach = () => {
|
|
286
|
+
// Another patch may have wrapped ours since. Then ours stays in the chain and passes everything through.
|
|
287
|
+
active = false;
|
|
288
|
+
if (menu.show === patched)
|
|
289
|
+
menu.show = show;
|
|
290
|
+
unsubscribe();
|
|
291
|
+
if (changeListener)
|
|
292
|
+
main?.removeChangeListener?.(changeListener);
|
|
293
|
+
vm.off?.('workspaceUpdate', schedule);
|
|
294
|
+
if (frame && globalThis.cancelAnimationFrame)
|
|
295
|
+
globalThis.cancelAnimationFrame(frame);
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
Promise.resolve().then(() => options.getBlockly?.()).then(ScratchBlocks => {
|
|
299
|
+
if (!disposed && ScratchBlocks?.ContextMenu)
|
|
300
|
+
connect(ScratchBlocks);
|
|
301
|
+
}, () => {
|
|
302
|
+
// Without ScratchBlocks there is no code area to mark blocks in.
|
|
303
|
+
});
|
|
304
|
+
return {
|
|
305
|
+
dispose() {
|
|
306
|
+
disposed = true;
|
|
307
|
+
detach();
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { SearchProject } from '@kubohiroya/turbowarp-plus-editor-project-model/project-search';
|
|
3
|
+
interface Workspace {
|
|
4
|
+
options: {
|
|
5
|
+
readOnly: boolean;
|
|
6
|
+
};
|
|
7
|
+
getParentSvg(): SVGElement;
|
|
8
|
+
hasUndoStack(): boolean;
|
|
9
|
+
hasRedoStack(): boolean;
|
|
10
|
+
isDragging(): boolean;
|
|
11
|
+
isVisible(): boolean;
|
|
12
|
+
targetWorkspace?: Workspace;
|
|
13
|
+
isFlyout: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface WorkspaceContext {
|
|
16
|
+
vm: object;
|
|
17
|
+
workspace: Workspace;
|
|
18
|
+
canUse(): boolean;
|
|
19
|
+
ScratchBlocks: {
|
|
20
|
+
getMainWorkspace(): Workspace;
|
|
21
|
+
selected?: {
|
|
22
|
+
workspace: Workspace;
|
|
23
|
+
isDeletable(): boolean;
|
|
24
|
+
isMovable(): boolean;
|
|
25
|
+
};
|
|
26
|
+
clipboardXml_?: unknown;
|
|
27
|
+
clipboardSource_?: Workspace;
|
|
28
|
+
onKeyDown_(event: Pick<KeyboardEvent, 'target' | 'keyCode' | 'ctrlKey' | 'shiftKey' | 'preventDefault'>): void;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
interface MenuProps {
|
|
32
|
+
vm: {
|
|
33
|
+
runtime?: SearchProject;
|
|
34
|
+
};
|
|
35
|
+
locale: string;
|
|
36
|
+
MenuItem: React.ComponentType<{
|
|
37
|
+
className?: string;
|
|
38
|
+
children: React.ReactNode;
|
|
39
|
+
}>;
|
|
40
|
+
MenuSection: React.ComponentType<{
|
|
41
|
+
children: React.ReactNode;
|
|
42
|
+
}>;
|
|
43
|
+
renderIcon(command: Command | 'restoreDeletion'): React.ReactNode;
|
|
44
|
+
getRearrangement?(): {
|
|
45
|
+
canArrange(): boolean;
|
|
46
|
+
open(locale: string): void;
|
|
47
|
+
} | undefined;
|
|
48
|
+
onRequestClose(): void;
|
|
49
|
+
}
|
|
50
|
+
export declare function attachEditMenuWorkspace(context: WorkspaceContext): {
|
|
51
|
+
dispose(): void;
|
|
52
|
+
};
|
|
53
|
+
declare const groups: readonly [readonly [readonly ["undo", "Undo", "元に戻す"], readonly ["redo", "Redo", "繰り返し"]], readonly [readonly ["cut", "Cut", "切り取り"], readonly ["copy", "Copy", "コピー"], readonly ["paste", "Paste", "貼り付け"]], readonly [readonly ["findAndReplace", "Find and Replace", "検索と置換"], readonly ["cleanup", "Rearrange Code Blocks", "コードブロックの再配置"]]];
|
|
54
|
+
type Command = (typeof groups)[number][number][0];
|
|
55
|
+
export declare function renderEditMenu(props: MenuProps): React.JSX.Element;
|
|
56
|
+
export declare function renderEditMenuRestore(label: React.ReactNode, renderIcon: MenuProps['renderIcon']): React.JSX.Element;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { isMacPlatform, projectReplaceOpener, projectReplaceShortcut } from '@kubohiroya/turbowarp-plus-editor-project-model/project-search-connections';
|
|
3
|
+
const workspaces = new WeakMap();
|
|
4
|
+
export function attachEditMenuWorkspace(context) {
|
|
5
|
+
workspaces.set(context.vm, context);
|
|
6
|
+
return { dispose() {
|
|
7
|
+
if (workspaces.get(context.vm) === context)
|
|
8
|
+
workspaces.delete(context.vm);
|
|
9
|
+
} };
|
|
10
|
+
}
|
|
11
|
+
const groups = [
|
|
12
|
+
[['undo', 'Undo', '元に戻す'], ['redo', 'Redo', '繰り返し']],
|
|
13
|
+
[['cut', 'Cut', '切り取り'], ['copy', 'Copy', 'コピー'], ['paste', 'Paste', '貼り付け']],
|
|
14
|
+
[['findAndReplace', 'Find and Replace', '検索と置換'],
|
|
15
|
+
['cleanup', 'Rearrange Code Blocks', 'コードブロックの再配置']]
|
|
16
|
+
];
|
|
17
|
+
const shortcutKeys = { undo: 'Z', redo: 'Y', cut: 'X', copy: 'C', paste: 'V' };
|
|
18
|
+
function shortcutFor(command, mac) {
|
|
19
|
+
if (command === 'cleanup')
|
|
20
|
+
return;
|
|
21
|
+
if (command === 'findAndReplace') {
|
|
22
|
+
const { label, macLabel, keys, macKeys } = projectReplaceShortcut;
|
|
23
|
+
return { label: mac ? macLabel : label, keys: mac ? macKeys : keys };
|
|
24
|
+
}
|
|
25
|
+
if (mac && command === 'redo')
|
|
26
|
+
return { label: '⇧⌘Z', keys: 'Meta+Shift+Z' };
|
|
27
|
+
const key = shortcutKeys[command];
|
|
28
|
+
return mac ? { label: `⌘${key}`, keys: `Meta+${key}` } : { label: `Ctrl+${key}`, keys: `Control+${key}` };
|
|
29
|
+
}
|
|
30
|
+
function canRun(context, command) {
|
|
31
|
+
if (!context?.canUse())
|
|
32
|
+
return false;
|
|
33
|
+
const { workspace, ScratchBlocks } = context;
|
|
34
|
+
if (ScratchBlocks.getMainWorkspace() !== workspace || workspace.options.readOnly ||
|
|
35
|
+
!workspace.isVisible() || workspace.isDragging() || workspace.getParentSvg().closest('[inert]'))
|
|
36
|
+
return false;
|
|
37
|
+
if (command === 'undo')
|
|
38
|
+
return workspace.hasUndoStack();
|
|
39
|
+
if (command === 'redo')
|
|
40
|
+
return workspace.hasRedoStack();
|
|
41
|
+
if (command === 'paste') {
|
|
42
|
+
const source = ScratchBlocks.clipboardSource_;
|
|
43
|
+
return Boolean(ScratchBlocks.clipboardXml_) && (source?.isFlyout ? source.targetWorkspace : source) === workspace;
|
|
44
|
+
}
|
|
45
|
+
const block = ScratchBlocks.selected;
|
|
46
|
+
return Boolean(block?.isDeletable() && block.isMovable() && (block.workspace === workspace ||
|
|
47
|
+
(command === 'copy' && block.workspace.isFlyout && block.workspace.targetWorkspace === workspace)));
|
|
48
|
+
}
|
|
49
|
+
export function renderEditMenu(props) {
|
|
50
|
+
return React.createElement(EditMenu, { ...props });
|
|
51
|
+
}
|
|
52
|
+
export function renderEditMenuRestore(label, renderIcon) {
|
|
53
|
+
return React.createElement("span", { className: "tw-edit-menu__restore" },
|
|
54
|
+
renderIcon('restoreDeletion'),
|
|
55
|
+
label);
|
|
56
|
+
}
|
|
57
|
+
function EditMenu({ vm, locale, MenuItem, MenuSection, renderIcon, onRequestClose, getRearrangement }) {
|
|
58
|
+
const japanese = /^ja(?:-|$)/i.test(locale);
|
|
59
|
+
const mac = isMacPlatform();
|
|
60
|
+
const context = workspaces.get(vm);
|
|
61
|
+
const openReplace = projectReplaceOpener(vm.runtime);
|
|
62
|
+
const rearrangement = getRearrangement?.();
|
|
63
|
+
return React.createElement(React.Fragment, null, groups.map(group => React.createElement(MenuSection, { key: group[0][0] }, group.map(([command, english, translated]) => {
|
|
64
|
+
if (command === 'cleanup' && !getRearrangement)
|
|
65
|
+
return null;
|
|
66
|
+
const shortcut = shortcutFor(command, mac);
|
|
67
|
+
const enabled = command === 'cleanup' ? rearrangement?.canArrange() :
|
|
68
|
+
command === 'findAndReplace' ? Boolean(openReplace) : canRun(context, command);
|
|
69
|
+
return React.createElement(MenuItem, { key: command },
|
|
70
|
+
React.createElement("button", { className: "tw-edit-menu__command", type: "button", disabled: !enabled, "aria-keyshortcuts": shortcut?.keys, onClick: event => {
|
|
71
|
+
if (command === 'cleanup') {
|
|
72
|
+
const current = getRearrangement?.();
|
|
73
|
+
if (!current?.canArrange())
|
|
74
|
+
return;
|
|
75
|
+
onRequestClose();
|
|
76
|
+
current.open(locale);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (command === 'findAndReplace') {
|
|
80
|
+
if (!openReplace)
|
|
81
|
+
return;
|
|
82
|
+
onRequestClose();
|
|
83
|
+
openReplace();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const current = workspaces.get(vm);
|
|
87
|
+
if (!canRun(current, command))
|
|
88
|
+
return;
|
|
89
|
+
// Keep cut, copy and paste on the same ScratchBlocks clipboard.
|
|
90
|
+
current.ScratchBlocks.onKeyDown_({ target: event.currentTarget,
|
|
91
|
+
keyCode: shortcutKeys[command].charCodeAt(0), ctrlKey: true, shiftKey: false,
|
|
92
|
+
preventDefault: () => event.preventDefault() });
|
|
93
|
+
onRequestClose();
|
|
94
|
+
} },
|
|
95
|
+
renderIcon(command),
|
|
96
|
+
React.createElement("span", null, japanese ? translated : english),
|
|
97
|
+
shortcut && React.createElement("kbd", { className: "tw-edit-menu__shortcut", "aria-hidden": "true" }, shortcut.label)));
|
|
98
|
+
}))));
|
|
99
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare const projectTitleFieldClassName = "tw-info-menu__project-title-field";
|
|
3
|
+
export declare const projectTitleGroupClassName = "tw-info-menu__project-title-group";
|
|
4
|
+
type MenuAction = {
|
|
5
|
+
/** An image URL, or an inline icon such as an SVG drawn with `currentColor`. */
|
|
6
|
+
icon: string | React.ReactNode;
|
|
7
|
+
onClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
8
|
+
};
|
|
9
|
+
type RuntimeItemProps = {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
className?: string;
|
|
12
|
+
closeOnSelect?: boolean;
|
|
13
|
+
onClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
14
|
+
};
|
|
15
|
+
export interface ProjectSettingsMenuProps {
|
|
16
|
+
leading?: boolean;
|
|
17
|
+
displaySettings?: {
|
|
18
|
+
items: React.ReactNode;
|
|
19
|
+
open: boolean;
|
|
20
|
+
onOpen(): void;
|
|
21
|
+
onClose(): void;
|
|
22
|
+
};
|
|
23
|
+
renderRuntimeSettings?: (Item: React.ComponentType<RuntimeItemProps>) => React.ReactNode;
|
|
24
|
+
addons?: MenuAction;
|
|
25
|
+
advanced?: MenuAction;
|
|
26
|
+
/** Opens the AI agent settings; listed right after the advanced settings. */
|
|
27
|
+
aiAgents?: MenuAction;
|
|
28
|
+
}
|
|
29
|
+
interface ProjectInfoMenuProps extends ProjectSettingsMenuProps {
|
|
30
|
+
locale: string;
|
|
31
|
+
version: string;
|
|
32
|
+
logo: string;
|
|
33
|
+
openAbout?(): void;
|
|
34
|
+
openPrivacy?(): void;
|
|
35
|
+
sourceUrl?: string;
|
|
36
|
+
githubConnection?: MenuAction;
|
|
37
|
+
}
|
|
38
|
+
export declare function ProjectInfoMenu({ locale, version, logo, leading, displaySettings, renderRuntimeSettings, addons, advanced, githubConnection, aiAgents, openAbout, openPrivacy, sourceUrl }: ProjectInfoMenuProps): React.JSX.Element;
|
|
39
|
+
export {};
|