@kubohiroya/turbowarp-plus-editor-code-views 0.25.0 → 0.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +12 -0
- package/README.md +12 -0
- package/dist/script-selection.d.ts +57 -0
- package/dist/script-selection.js +404 -0
- package/dist/workspace.js +3 -0
- package/package.json +8 -4
- package/src/script-selection.ts +410 -0
- package/src/styles.css +15 -0
- package/src/workspace.ts +3 -0
package/README.ja.md
CHANGED
|
@@ -16,6 +16,18 @@ Z/N配置内の矢印は、スクリプト群の右外側へ96px以上張り
|
|
|
16
16
|
|
|
17
17
|
これらの変更の切り戻しは、本変更を revert して再ビルドする。ホストのパッケージ固定を以前の版へ戻す場合は、機能移管で追加された API との互換性も確認する。自由配置へ切り替えるだけでも元の配置を表示できる。折りたたみは既存の作品内フラグを使用するため、「折りたたみをすべて解除」で解除できる。
|
|
18
18
|
|
|
19
|
+
## 複数のスクリプトの選択
|
|
20
|
+
|
|
21
|
+
自由配置では、複数のスクリプトを選んで、まとめて移動・削除できます。選択は画面上の状態で、作品には保存しません。
|
|
22
|
+
|
|
23
|
+
- Ctrl+クリック(macOS では ⌘+クリック)で、クリックしたスクリプト全体を選択に加えたり外したりします。スクリプトは実行しません。macOS の Ctrl+クリックは右クリックとして扱われるため使いません。
|
|
24
|
+
- 背景を Shift+ドラッグすると矩形を描き、重なったスクリプトを選択に加えます。Shift+Alt+ドラッグでは選択から外します。
|
|
25
|
+
- 選択中のスクリプトをドラッグすると、ほかの選択中のスクリプトも同じだけ動きます。パレットに落とすと、選択中のスクリプトをすべて削除します。どちらも1回の「元に戻す」で戻ります。
|
|
26
|
+
- Delete・Backspace キー、またはブロックの右クリックメニューの「選択した N 個のスクリプトを削除」で、まとめて削除します。呼び出されている関数の定義が消える場合は、Scratch と同じく警告を出して削除全体を取り消します。
|
|
27
|
+
- Esc、背景のクリック、選択していないスクリプトを押すこと、スプライトやステージの切り替えで、選択を解除します。
|
|
28
|
+
|
|
29
|
+
`attachCodeWorkspace` が自動で組み込みます。単独で使うときは `@kubohiroya/turbowarp-plus-editor-code-views/script-selection` の `attachScriptSelection` を呼びます。
|
|
30
|
+
|
|
19
31
|
## 関係に基づく再配置(Editor #727)
|
|
20
32
|
|
|
21
33
|
自由配置の背景を右クリックし「スクリプトを意味に応じて再配置する」を選ぶと、現在のスプライト/ステージの再配置をプレビューする。「再配置する」で確定し、取消では作品を変更しない。Undo/Redo各1回で座標と付属コメントを戻せる。別窓では編集権を持つ画面だけで実行でき、他スプライトの座標は動かさない。
|
package/README.md
CHANGED
|
@@ -10,6 +10,18 @@ Opening a function or hat adjusts the zoom to its rendered script and places the
|
|
|
10
10
|
|
|
11
11
|
Hosts with a separate workspace can call `fitScriptInWorkspace(workspace, scriptId, ScratchBlocks)` from the root or `./sprite-code-windows` export. It returns `false` if the viewport or block is not ready, so the host can retry after layout.
|
|
12
12
|
|
|
13
|
+
## Selecting several scripts
|
|
14
|
+
|
|
15
|
+
In the free layout, several scripts can be selected and then moved or deleted together. The selection is screen state and is not saved with the project.
|
|
16
|
+
|
|
17
|
+
- Ctrl+click (⌘+click on macOS) adds or removes the whole clicked script without running it. Ctrl+click is not used on macOS, where it opens the context menu.
|
|
18
|
+
- Shift+drag on the background draws a rectangle and adds the scripts it touches. Shift+Alt+drag removes them.
|
|
19
|
+
- Dragging a selected script moves the other selected scripts by the same distance; dropping it on the palette deletes them all. Either is undone in one step.
|
|
20
|
+
- Delete or Backspace, or "Delete N selected scripts" in the block context menu, deletes them together. If that removes the definition of a function still in use, the whole deletion is undone with Scratch's warning.
|
|
21
|
+
- Escape, a click on the background, pressing an unselected script or switching sprites clears the selection.
|
|
22
|
+
|
|
23
|
+
`attachCodeWorkspace` installs it. Import `attachScriptSelection` from `@kubohiroya/turbowarp-plus-editor-code-views/script-selection` to use it on its own.
|
|
24
|
+
|
|
13
25
|
## Rearranging scripts by relationships (Editor #727)
|
|
14
26
|
|
|
15
27
|
Right-click a free-layout background and choose **Rearrange scripts by relationships**. Preview the current sprite or stage, then confirm or cancel. One Undo/Redo restores the positions and attached comments. Other targets stay fixed. Editable popouts use their existing edit lease; read-only and serial views cannot run this operation.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select several scripts in the free layout and move or delete them together.
|
|
3
|
+
*
|
|
4
|
+
* - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
|
|
5
|
+
* - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
|
|
6
|
+
* - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
|
|
7
|
+
* - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
|
|
8
|
+
* - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
|
|
9
|
+
*
|
|
10
|
+
* The selection is screen state only; it is never saved with the project.
|
|
11
|
+
*/
|
|
12
|
+
type Point = {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
};
|
|
16
|
+
type Rect = {
|
|
17
|
+
left: number;
|
|
18
|
+
top: number;
|
|
19
|
+
right: number;
|
|
20
|
+
bottom: number;
|
|
21
|
+
};
|
|
22
|
+
export declare const isMacPlatform: (navigator: {
|
|
23
|
+
platform?: string;
|
|
24
|
+
userAgent?: string;
|
|
25
|
+
} | undefined) => boolean;
|
|
26
|
+
/** Whether a press toggles a script: ⌘ on macOS, Ctrl elsewhere. */
|
|
27
|
+
export declare const isToggleClick: (event: {
|
|
28
|
+
ctrlKey: boolean;
|
|
29
|
+
metaKey: boolean;
|
|
30
|
+
shiftKey: boolean;
|
|
31
|
+
altKey: boolean;
|
|
32
|
+
button: number;
|
|
33
|
+
}, mac: boolean) => boolean;
|
|
34
|
+
/** Normalise two corners into a rectangle. */
|
|
35
|
+
export declare const rectFromCorners: (a: Point, b: Point) => Rect;
|
|
36
|
+
/** Whether two rectangles overlap, touching edges included. */
|
|
37
|
+
export declare const intersects: (a: Rect, b: Rect) => boolean;
|
|
38
|
+
/** Lets scripts in a free-layout workspace be selected, moved and deleted together. */
|
|
39
|
+
export declare function attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, onError, getLocale }: {
|
|
40
|
+
workspace: any;
|
|
41
|
+
ScratchBlocks: any;
|
|
42
|
+
onError?: (error: unknown) => void;
|
|
43
|
+
getLocale?: () => string;
|
|
44
|
+
/** Clears the selection when the workspace loads another sprite or the stage. */
|
|
45
|
+
vm?: {
|
|
46
|
+
on(name: string, listener: () => void): void;
|
|
47
|
+
removeListener(name: string, listener: () => void): void;
|
|
48
|
+
};
|
|
49
|
+
}): {
|
|
50
|
+
/** The ids of the selected scripts' top blocks. */
|
|
51
|
+
readonly selected: string[];
|
|
52
|
+
select(ids: Iterable<string>): void;
|
|
53
|
+
clear: () => void;
|
|
54
|
+
deleteSelected: () => void;
|
|
55
|
+
dispose(): void;
|
|
56
|
+
};
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Select several scripts in the free layout and move or delete them together.
|
|
4
|
+
*
|
|
5
|
+
* - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
|
|
6
|
+
* - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
|
|
7
|
+
* - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
|
|
8
|
+
* - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
|
|
9
|
+
* - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
|
|
10
|
+
*
|
|
11
|
+
* The selection is screen state only; it is never saved with the project.
|
|
12
|
+
*/
|
|
13
|
+
const selectedClass = 'tw-plus-script-selected';
|
|
14
|
+
const marqueeClass = 'tw-plus-script-marquee';
|
|
15
|
+
const installed = new WeakSet();
|
|
16
|
+
/** The attachment that receives drags and context menus of a workspace's blocks. */
|
|
17
|
+
const owners = new WeakMap();
|
|
18
|
+
/** The workspace whose selection the keyboard acts on: the one last pressed in. */
|
|
19
|
+
let activeWorkspace = null;
|
|
20
|
+
export const isMacPlatform = (navigator) => /Mac|iPhone|iPad|iPod/.test(navigator?.platform || navigator?.userAgent || '');
|
|
21
|
+
/** Whether a press toggles a script: ⌘ on macOS, Ctrl elsewhere. */
|
|
22
|
+
export const isToggleClick = (event, mac) => event.button === 0 && !event.shiftKey && !event.altKey && (mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey);
|
|
23
|
+
/** Normalise two corners into a rectangle. */
|
|
24
|
+
export const rectFromCorners = (a, b) => ({
|
|
25
|
+
left: Math.min(a.x, b.x), top: Math.min(a.y, b.y), right: Math.max(a.x, b.x), bottom: Math.max(a.y, b.y)
|
|
26
|
+
});
|
|
27
|
+
/** Whether two rectangles overlap, touching edges included. */
|
|
28
|
+
export const intersects = (a, b) => a.left <= b.right && b.left <= a.right && a.top <= b.bottom && b.top <= a.bottom;
|
|
29
|
+
const labels = (locale, count) => /^ja(?:-|$)/i.test(locale) ?
|
|
30
|
+
{ remove: `選択した${count}個のスクリプトを削除` } :
|
|
31
|
+
{ remove: count === 1 ? 'Delete 1 selected script' : `Delete ${count} selected scripts` };
|
|
32
|
+
/** Procedure codes called by a block but defined nowhere: the calls a deletion would leave behind. */
|
|
33
|
+
function orphanCalls(workspace) {
|
|
34
|
+
const defined = new Set();
|
|
35
|
+
const called = new Set();
|
|
36
|
+
for (const block of workspace.getAllBlocks(false)) {
|
|
37
|
+
if (block.type === 'procedures_prototype')
|
|
38
|
+
defined.add(block.getProcCode?.() ?? '');
|
|
39
|
+
else if (block.type === 'procedures_call')
|
|
40
|
+
called.add(block.getProcCode?.() ?? '');
|
|
41
|
+
}
|
|
42
|
+
return new Set([...called].filter(code => !defined.has(code)));
|
|
43
|
+
}
|
|
44
|
+
function installHooks(blocks) {
|
|
45
|
+
const dragger = blocks.BlockDragger?.prototype;
|
|
46
|
+
if (dragger && !installed.has(dragger)) {
|
|
47
|
+
installed.add(dragger);
|
|
48
|
+
const { startBlockDrag, dragBlock, endBlockDrag } = dragger;
|
|
49
|
+
dragger.startBlockDrag = function (...args) {
|
|
50
|
+
const result = startBlockDrag.apply(this, args);
|
|
51
|
+
try {
|
|
52
|
+
owners.get(this.workspace_)?.dragStarted(this);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
owners.get(this.workspace_)?.onError(error);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
dragger.dragBlock = function (event, delta) {
|
|
60
|
+
const result = dragBlock.call(this, event, delta);
|
|
61
|
+
owners.get(this.workspace_)?.dragMoved(this, delta);
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
dragger.endBlockDrag = function (event, delta) {
|
|
65
|
+
const owner = owners.get(this.workspace_);
|
|
66
|
+
// Move or delete the other scripts in the drag's event group, which the original call closes.
|
|
67
|
+
try {
|
|
68
|
+
owner?.dragEnding(this, event, delta);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
owner?.onError(error);
|
|
72
|
+
}
|
|
73
|
+
return endBlockDrag.call(this, event, delta);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const block = blocks.BlockSvg?.prototype;
|
|
77
|
+
if (block?.showContextMenu_ && !installed.has(block)) {
|
|
78
|
+
installed.add(block);
|
|
79
|
+
const { showContextMenu_ } = block;
|
|
80
|
+
block.showContextMenu_ = function (event) {
|
|
81
|
+
const extra = owners.get(this.workspace)?.blockMenuOption(this);
|
|
82
|
+
if (!extra)
|
|
83
|
+
return showContextMenu_.call(this, event);
|
|
84
|
+
const { show } = blocks.ContextMenu;
|
|
85
|
+
blocks.ContextMenu.show = (menuEvent, options, rtl) => show.call(blocks.ContextMenu, menuEvent, [...options, extra], rtl);
|
|
86
|
+
try {
|
|
87
|
+
return showContextMenu_.call(this, event);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
blocks.ContextMenu.show = show;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function createSelection({ workspace, blocks, onError, getLocale }) {
|
|
96
|
+
const svg = workspace.getParentSvg();
|
|
97
|
+
const injection = workspace.getInjectionDiv();
|
|
98
|
+
const document = injection.ownerDocument;
|
|
99
|
+
const window = document.defaultView;
|
|
100
|
+
const mac = isMacPlatform(window.navigator);
|
|
101
|
+
const selected = new Set();
|
|
102
|
+
let companions = [];
|
|
103
|
+
let marquee;
|
|
104
|
+
let pendingClear;
|
|
105
|
+
const readOnly = () => Boolean(workspace.options?.readOnly);
|
|
106
|
+
const blockAt = (target) => {
|
|
107
|
+
const node = target?.closest?.('[data-id]');
|
|
108
|
+
const id = node?.getAttribute('data-id');
|
|
109
|
+
const block = id ? workspace.getBlockById(id) : null;
|
|
110
|
+
return block && !block.getSvgRoot()?.closest('[data-tw-plus-hidden]') ? block.getRootBlock() : undefined;
|
|
111
|
+
};
|
|
112
|
+
const isBackground = (target) => {
|
|
113
|
+
const element = target;
|
|
114
|
+
return element === svg || Boolean(element?.classList?.contains('blocklyMainBackground'));
|
|
115
|
+
};
|
|
116
|
+
const visibleTops = () => workspace.getTopBlocks(false).filter(block => {
|
|
117
|
+
const root = block.getSvgRoot();
|
|
118
|
+
return root && root.getClientRects().length && !root.closest('[data-tw-plus-hidden]');
|
|
119
|
+
});
|
|
120
|
+
function paint() {
|
|
121
|
+
for (const element of svg.querySelectorAll(`.${selectedClass}`)) {
|
|
122
|
+
const id = element.getAttribute('data-id');
|
|
123
|
+
if (!id || !selected.has(id))
|
|
124
|
+
element.classList.remove(selectedClass);
|
|
125
|
+
}
|
|
126
|
+
for (const id of selected)
|
|
127
|
+
workspace.getBlockById(id)?.getSvgRoot()?.classList.add(selectedClass);
|
|
128
|
+
injection.toggleAttribute('data-tw-plus-script-selection', selected.size > 0);
|
|
129
|
+
}
|
|
130
|
+
/** Keep only scripts that are still on the workspace and still start a stack. */
|
|
131
|
+
function prune() {
|
|
132
|
+
let changed = false;
|
|
133
|
+
for (const id of [...selected]) {
|
|
134
|
+
const block = workspace.getBlockById(id);
|
|
135
|
+
if (!block || block.getParent()) {
|
|
136
|
+
selected.delete(id);
|
|
137
|
+
changed = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (changed)
|
|
141
|
+
paint();
|
|
142
|
+
}
|
|
143
|
+
function set(ids) {
|
|
144
|
+
selected.clear();
|
|
145
|
+
for (const id of ids)
|
|
146
|
+
selected.add(id);
|
|
147
|
+
paint();
|
|
148
|
+
}
|
|
149
|
+
function toggle(block) {
|
|
150
|
+
if (selected.has(block.id))
|
|
151
|
+
selected.delete(block.id);
|
|
152
|
+
else
|
|
153
|
+
selected.add(block.id);
|
|
154
|
+
paint();
|
|
155
|
+
}
|
|
156
|
+
const clear = () => {
|
|
157
|
+
if (selected.size)
|
|
158
|
+
set([]);
|
|
159
|
+
};
|
|
160
|
+
function deletable() {
|
|
161
|
+
prune();
|
|
162
|
+
return [...selected].map(id => workspace.getBlockById(id))
|
|
163
|
+
.filter((block) => Boolean(block?.isDeletable()));
|
|
164
|
+
}
|
|
165
|
+
/** Undo an edit that left calls to a function without its definition, as Scratch does for one script. */
|
|
166
|
+
function rollBackOrphans(before) {
|
|
167
|
+
window.setTimeout(() => {
|
|
168
|
+
const after = orphanCalls(workspace);
|
|
169
|
+
if (![...after].some(code => !before.has(code)))
|
|
170
|
+
return;
|
|
171
|
+
window.alert(blocks.Msg?.PROCEDURE_USED ?? 'This function is in use.');
|
|
172
|
+
workspace.undo(false);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function deleteSelected() {
|
|
176
|
+
const targets = deletable();
|
|
177
|
+
if (!targets.length || readOnly())
|
|
178
|
+
return;
|
|
179
|
+
const before = orphanCalls(workspace);
|
|
180
|
+
blocks.hideChaff?.();
|
|
181
|
+
blocks.Events.setGroup(true);
|
|
182
|
+
try {
|
|
183
|
+
for (const block of targets)
|
|
184
|
+
block.dispose(false, true);
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
blocks.Events.setGroup(false);
|
|
188
|
+
}
|
|
189
|
+
set([]);
|
|
190
|
+
rollBackOrphans(before);
|
|
191
|
+
}
|
|
192
|
+
function startMarquee(event) {
|
|
193
|
+
const abort = new window.AbortController();
|
|
194
|
+
const element = document.createElement('div');
|
|
195
|
+
element.className = marqueeClass;
|
|
196
|
+
element.setAttribute('aria-hidden', 'true');
|
|
197
|
+
injection.append(element);
|
|
198
|
+
marquee = { start: { x: event.clientX, y: event.clientY }, element, remove: event.altKey, abort };
|
|
199
|
+
const place = (point) => {
|
|
200
|
+
const rect = rectFromCorners(marquee.start, point);
|
|
201
|
+
const origin = injection.getBoundingClientRect();
|
|
202
|
+
Object.assign(element.style, { left: `${rect.left - origin.left}px`, top: `${rect.top - origin.top}px`,
|
|
203
|
+
width: `${rect.right - rect.left}px`, height: `${rect.bottom - rect.top}px` });
|
|
204
|
+
return rect;
|
|
205
|
+
};
|
|
206
|
+
place(marquee.start);
|
|
207
|
+
const options = { capture: true, signal: abort.signal };
|
|
208
|
+
document.addEventListener('mousemove', move => {
|
|
209
|
+
move.preventDefault();
|
|
210
|
+
place({ x: move.clientX, y: move.clientY });
|
|
211
|
+
}, options);
|
|
212
|
+
document.addEventListener('mouseup', up => {
|
|
213
|
+
up.preventDefault();
|
|
214
|
+
up.stopPropagation();
|
|
215
|
+
finishMarquee(place({ x: up.clientX, y: up.clientY }));
|
|
216
|
+
}, options);
|
|
217
|
+
window.addEventListener('blur', () => finishMarquee(undefined), options);
|
|
218
|
+
}
|
|
219
|
+
function finishMarquee(rect) {
|
|
220
|
+
if (!marquee)
|
|
221
|
+
return;
|
|
222
|
+
const { element, remove, abort } = marquee;
|
|
223
|
+
marquee = undefined;
|
|
224
|
+
abort.abort();
|
|
225
|
+
element.remove();
|
|
226
|
+
if (!rect || (rect.right - rect.left < 3 && rect.bottom - rect.top < 3))
|
|
227
|
+
return;
|
|
228
|
+
const hits = visibleTops().filter(block => intersects(rect, block.getSvgRoot().getBoundingClientRect())).map(block => block.id);
|
|
229
|
+
if (remove)
|
|
230
|
+
for (const id of hits)
|
|
231
|
+
selected.delete(id);
|
|
232
|
+
else
|
|
233
|
+
for (const id of hits)
|
|
234
|
+
selected.add(id);
|
|
235
|
+
paint();
|
|
236
|
+
}
|
|
237
|
+
function onMouseDown(event) {
|
|
238
|
+
if (readOnly() || event.button !== 0)
|
|
239
|
+
return;
|
|
240
|
+
activeWorkspace = workspace;
|
|
241
|
+
prune();
|
|
242
|
+
if (isToggleClick(event, mac)) {
|
|
243
|
+
const block = blockAt(event.target);
|
|
244
|
+
if (!block)
|
|
245
|
+
return;
|
|
246
|
+
// Keep Blockly from starting a gesture, which would run the script on click.
|
|
247
|
+
event.preventDefault();
|
|
248
|
+
event.stopPropagation();
|
|
249
|
+
blocks.hideChaff?.();
|
|
250
|
+
toggle(block);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (event.shiftKey && isBackground(event.target)) {
|
|
254
|
+
event.preventDefault();
|
|
255
|
+
event.stopPropagation();
|
|
256
|
+
blocks.hideChaff?.();
|
|
257
|
+
startMarquee(event);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey)
|
|
261
|
+
return;
|
|
262
|
+
const block = blockAt(event.target);
|
|
263
|
+
if (block) {
|
|
264
|
+
if (!selected.has(block.id))
|
|
265
|
+
clear();
|
|
266
|
+
}
|
|
267
|
+
else if (isBackground(event.target)) {
|
|
268
|
+
// Dragging the background scrolls; only a click clears.
|
|
269
|
+
pendingClear = { start: { x: event.clientX, y: event.clientY } };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function onMouseUp(event) {
|
|
273
|
+
if (!pendingClear)
|
|
274
|
+
return;
|
|
275
|
+
const { start } = pendingClear;
|
|
276
|
+
pendingClear = undefined;
|
|
277
|
+
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) < 4)
|
|
278
|
+
clear();
|
|
279
|
+
}
|
|
280
|
+
function onKeyDown(event) {
|
|
281
|
+
if (activeWorkspace !== workspace || !selected.size || !svg.isConnected || !svg.getClientRects().length)
|
|
282
|
+
return;
|
|
283
|
+
const target = event.target;
|
|
284
|
+
if (target?.closest?.('input, textarea, select, [contenteditable=""], [contenteditable="true"]'))
|
|
285
|
+
return;
|
|
286
|
+
if (event.key === 'Escape') {
|
|
287
|
+
clear();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if ((event.key !== 'Delete' && event.key !== 'Backspace') || event.ctrlKey || event.metaKey || event.altKey)
|
|
291
|
+
return;
|
|
292
|
+
if (readOnly() || workspace.isDragging?.() || !deletable().length)
|
|
293
|
+
return;
|
|
294
|
+
// Blockly's own handler would delete only its selected block.
|
|
295
|
+
event.preventDefault();
|
|
296
|
+
event.stopImmediatePropagation();
|
|
297
|
+
deleteSelected();
|
|
298
|
+
}
|
|
299
|
+
const abort = new window.AbortController();
|
|
300
|
+
svg.addEventListener('mousedown', onMouseDown, { capture: true, signal: abort.signal });
|
|
301
|
+
document.addEventListener('mouseup', onMouseUp, { capture: true, signal: abort.signal });
|
|
302
|
+
window.addEventListener('keydown', onKeyDown, { capture: true, signal: abort.signal });
|
|
303
|
+
const listener = workspace.addChangeListener(() => prune());
|
|
304
|
+
return {
|
|
305
|
+
onError,
|
|
306
|
+
get ids() {
|
|
307
|
+
prune();
|
|
308
|
+
return [...selected];
|
|
309
|
+
},
|
|
310
|
+
set,
|
|
311
|
+
clear,
|
|
312
|
+
deleteSelected,
|
|
313
|
+
dragStarted(dragger) {
|
|
314
|
+
companions = [];
|
|
315
|
+
const dragged = dragger.draggingBlock_;
|
|
316
|
+
if (!selected.has(dragged.id) || dragged.getParent()) {
|
|
317
|
+
// Dragging an unselected script, or a part pulled out of a selected one, moves only that.
|
|
318
|
+
if (!selected.has(dragged.id))
|
|
319
|
+
clear();
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
prune();
|
|
323
|
+
companions = [...selected].filter(id => id !== dragged.id)
|
|
324
|
+
.map(id => workspace.getBlockById(id))
|
|
325
|
+
.filter((block) => Boolean(block && block.isMovable?.() !== false))
|
|
326
|
+
.map(block => ({ block, start: block.getRelativeToSurfaceXY() }));
|
|
327
|
+
},
|
|
328
|
+
dragMoved(dragger, delta) {
|
|
329
|
+
if (!companions.length)
|
|
330
|
+
return;
|
|
331
|
+
const offset = dragger.pixelsToWorkspaceUnits_(delta);
|
|
332
|
+
for (const { block, start } of companions)
|
|
333
|
+
block.translate(start.x + offset.x, start.y + offset.y);
|
|
334
|
+
},
|
|
335
|
+
dragEnding(dragger, event, delta) {
|
|
336
|
+
const moving = companions;
|
|
337
|
+
companions = [];
|
|
338
|
+
if (!moving.length)
|
|
339
|
+
return;
|
|
340
|
+
// The original call refreshes this state first; do the same so the decision matches.
|
|
341
|
+
dragger.dragBlock(event, delta);
|
|
342
|
+
const offset = dragger.pixelsToWorkspaceUnits_(delta);
|
|
343
|
+
for (const { block, start } of moving)
|
|
344
|
+
block.translate(start.x, start.y);
|
|
345
|
+
if (dragger.wouldDeleteBlock_) {
|
|
346
|
+
const before = orphanCalls(workspace);
|
|
347
|
+
for (const { block } of moving)
|
|
348
|
+
if (block.isDeletable())
|
|
349
|
+
block.dispose(false, true);
|
|
350
|
+
set([]);
|
|
351
|
+
rollBackOrphans(before);
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
// A drop outside the blocks area is undone as one group by Blockly, these moves included.
|
|
355
|
+
for (const { block } of moving)
|
|
356
|
+
block.moveBy(offset.x, offset.y);
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
blockMenuOption(block) {
|
|
360
|
+
if (readOnly() || !selected.has(block.getRootBlock().id))
|
|
361
|
+
return undefined;
|
|
362
|
+
const count = deletable().length;
|
|
363
|
+
if (!count)
|
|
364
|
+
return undefined;
|
|
365
|
+
return { text: labels(getLocale(), count).remove, enabled: true, callback: deleteSelected };
|
|
366
|
+
},
|
|
367
|
+
dispose() {
|
|
368
|
+
finishMarquee(undefined);
|
|
369
|
+
abort.abort();
|
|
370
|
+
workspace.removeChangeListener(listener);
|
|
371
|
+
set([]);
|
|
372
|
+
injection.removeAttribute('data-tw-plus-script-selection');
|
|
373
|
+
if (activeWorkspace === workspace)
|
|
374
|
+
activeWorkspace = null;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/** Lets scripts in a free-layout workspace be selected, moved and deleted together. */
|
|
379
|
+
export function attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, onError = (error) => console.error(error), getLocale = () => blocks.ScratchMsgs?.currentLocale_ ?? '' }) {
|
|
380
|
+
installHooks(blocks);
|
|
381
|
+
owners.get(workspace)?.dispose();
|
|
382
|
+
const selection = createSelection({ workspace, blocks, onError, getLocale });
|
|
383
|
+
owners.set(workspace, selection);
|
|
384
|
+
const clearOnLoad = () => selection.clear();
|
|
385
|
+
vm?.on('workspaceUpdate', clearOnLoad);
|
|
386
|
+
return {
|
|
387
|
+
/** The ids of the selected scripts' top blocks. */
|
|
388
|
+
get selected() {
|
|
389
|
+
return selection.ids;
|
|
390
|
+
},
|
|
391
|
+
select(ids) {
|
|
392
|
+
selection.set(ids);
|
|
393
|
+
},
|
|
394
|
+
clear: selection.clear,
|
|
395
|
+
deleteSelected: selection.deleteSelected,
|
|
396
|
+
dispose() {
|
|
397
|
+
vm?.removeListener('workspaceUpdate', clearOnLoad);
|
|
398
|
+
if (owners.get(workspace) !== selection)
|
|
399
|
+
return;
|
|
400
|
+
owners.delete(workspace);
|
|
401
|
+
selection.dispose();
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
package/dist/workspace.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { attachBlockDrag } from "./block-drag.js";
|
|
2
|
+
import { attachScriptSelection } from "./script-selection.js";
|
|
2
3
|
import { registerCodeWorkspace } from "./code-workspace-registry.js";
|
|
3
4
|
import { workspaceXML } from "./history.js";
|
|
4
5
|
import { isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition } from "./procedure-edits.js";
|
|
@@ -643,6 +644,7 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
643
644
|
},
|
|
644
645
|
dispose() {
|
|
645
646
|
blockDrag.dispose();
|
|
647
|
+
scriptSelection.dispose();
|
|
646
648
|
unregisterRelationNavigation();
|
|
647
649
|
unregisterCodeWorkspace();
|
|
648
650
|
shown?.disconnect();
|
|
@@ -672,6 +674,7 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
672
674
|
}) : null;
|
|
673
675
|
shown?.observe(injection);
|
|
674
676
|
const blockDrag = attachBlockDrag({ vm, workspace, ScratchBlocks: blocks, onError, prepareImport: bridge.prepareImport });
|
|
677
|
+
const scriptSelection = attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, onError });
|
|
675
678
|
const visibility = attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError });
|
|
676
679
|
const unregisterRelationNavigation = registerScriptRelationNavigation(vm, endpoint => {
|
|
677
680
|
if (vm.runtime.getTargetById(endpoint.targetId)?.blocks._blocks[endpoint.blockId] === undefined)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-code-views",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.1",
|
|
4
4
|
"description": "Code views for TurboWarp Plus Editor: code in tabs and windows, the detached block palette, script layout and block field editing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -102,6 +102,10 @@
|
|
|
102
102
|
"types": "./dist/dockview-unsaved.d.ts",
|
|
103
103
|
"default": "./dist/dockview-unsaved.js"
|
|
104
104
|
},
|
|
105
|
+
"./script-selection": {
|
|
106
|
+
"types": "./dist/script-selection.d.ts",
|
|
107
|
+
"default": "./dist/script-selection.js"
|
|
108
|
+
},
|
|
105
109
|
"./styles.css": "./src/styles.css",
|
|
106
110
|
"./package.json": "./package.json",
|
|
107
111
|
"./horizontal-block-palette": {
|
|
@@ -148,15 +152,15 @@
|
|
|
148
152
|
"matter-js": "0.20.0"
|
|
149
153
|
},
|
|
150
154
|
"peerDependencies": {
|
|
151
|
-
"@kubohiroya/turbowarp-extension-security-dialog": ">=0.9.0 <0.10.0 || 0.10.0 || 0.13.0",
|
|
155
|
+
"@kubohiroya/turbowarp-extension-security-dialog": ">=0.9.0 <0.10.0 || 0.10.0 || 0.13.0 || 0.13.1",
|
|
152
156
|
"@kubohiroya/turbowarp-plus-editor-windows": ">=0.2.0 <0.3.0",
|
|
153
157
|
"react": ">=16.14",
|
|
154
158
|
"react-dom": ">=16.14"
|
|
155
159
|
},
|
|
156
160
|
"devDependencies": {
|
|
157
|
-
"@kubohiroya/turbowarp-extension-security-dialog": "0.13.
|
|
161
|
+
"@kubohiroya/turbowarp-extension-security-dialog": "0.13.1",
|
|
158
162
|
"@kubohiroya/turbowarp-plus-editor-test-runtime": "0.3.2",
|
|
159
|
-
"@kubohiroya/turbowarp-plus-editor-windows": "0.2.
|
|
163
|
+
"@kubohiroya/turbowarp-plus-editor-windows": "0.2.2",
|
|
160
164
|
"@playwright/test": "1.64.1",
|
|
161
165
|
"@types/matter-js": "0.20.2",
|
|
162
166
|
"@types/react": "18.3.31",
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Select several scripts in the free layout and move or delete them together.
|
|
4
|
+
*
|
|
5
|
+
* - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
|
|
6
|
+
* - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
|
|
7
|
+
* - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
|
|
8
|
+
* - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
|
|
9
|
+
* - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
|
|
10
|
+
*
|
|
11
|
+
* The selection is screen state only; it is never saved with the project.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type Point = {x: number; y: number};
|
|
15
|
+
type Rect = {left: number; top: number; right: number; bottom: number};
|
|
16
|
+
|
|
17
|
+
interface SelectableBlock {
|
|
18
|
+
id: string;
|
|
19
|
+
type: string;
|
|
20
|
+
workspace: object;
|
|
21
|
+
getParent(): SelectableBlock | null;
|
|
22
|
+
getRootBlock(): SelectableBlock;
|
|
23
|
+
getSvgRoot(): SVGGElement | null;
|
|
24
|
+
getRelativeToSurfaceXY(): Point;
|
|
25
|
+
translate(x: number, y: number): void;
|
|
26
|
+
moveBy(dx: number, dy: number): void;
|
|
27
|
+
isDeletable(): boolean;
|
|
28
|
+
isMovable?(): boolean;
|
|
29
|
+
dispose(healStack?: boolean, animate?: boolean): void;
|
|
30
|
+
getDescendants(ordered: boolean): SelectableBlock[];
|
|
31
|
+
getProcCode?(): string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const selectedClass = 'tw-plus-script-selected';
|
|
35
|
+
const marqueeClass = 'tw-plus-script-marquee';
|
|
36
|
+
const installed = new WeakSet<object>();
|
|
37
|
+
/** The attachment that receives drags and context menus of a workspace's blocks. */
|
|
38
|
+
const owners = new WeakMap<object, ReturnType<typeof createSelection>>();
|
|
39
|
+
/** The workspace whose selection the keyboard acts on: the one last pressed in. */
|
|
40
|
+
let activeWorkspace: object | null = null;
|
|
41
|
+
|
|
42
|
+
export const isMacPlatform = (navigator: {platform?: string; userAgent?: string} | undefined) =>
|
|
43
|
+
/Mac|iPhone|iPad|iPod/.test(navigator?.platform || navigator?.userAgent || '');
|
|
44
|
+
|
|
45
|
+
/** Whether a press toggles a script: ⌘ on macOS, Ctrl elsewhere. */
|
|
46
|
+
export const isToggleClick = (event: {ctrlKey: boolean; metaKey: boolean; shiftKey: boolean; altKey: boolean; button: number},
|
|
47
|
+
mac: boolean) => event.button === 0 && !event.shiftKey && !event.altKey && (mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey);
|
|
48
|
+
|
|
49
|
+
/** Normalise two corners into a rectangle. */
|
|
50
|
+
export const rectFromCorners = (a: Point, b: Point): Rect => ({
|
|
51
|
+
left: Math.min(a.x, b.x), top: Math.min(a.y, b.y), right: Math.max(a.x, b.x), bottom: Math.max(a.y, b.y)
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** Whether two rectangles overlap, touching edges included. */
|
|
55
|
+
export const intersects = (a: Rect, b: Rect) => a.left <= b.right && b.left <= a.right && a.top <= b.bottom && b.top <= a.bottom;
|
|
56
|
+
|
|
57
|
+
const labels = (locale: string, count: number) => /^ja(?:-|$)/i.test(locale) ?
|
|
58
|
+
{remove: `選択した${count}個のスクリプトを削除`} :
|
|
59
|
+
{remove: count === 1 ? 'Delete 1 selected script' : `Delete ${count} selected scripts`};
|
|
60
|
+
|
|
61
|
+
/** Procedure codes called by a block but defined nowhere: the calls a deletion would leave behind. */
|
|
62
|
+
function orphanCalls(workspace): Set<string> {
|
|
63
|
+
const defined = new Set<string>();
|
|
64
|
+
const called = new Set<string>();
|
|
65
|
+
for (const block of workspace.getAllBlocks(false) as SelectableBlock[]) {
|
|
66
|
+
if (block.type === 'procedures_prototype') defined.add(block.getProcCode?.() ?? '');
|
|
67
|
+
else if (block.type === 'procedures_call') called.add(block.getProcCode?.() ?? '');
|
|
68
|
+
}
|
|
69
|
+
return new Set([...called].filter(code => !defined.has(code)));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function installHooks(blocks) {
|
|
73
|
+
const dragger = blocks.BlockDragger?.prototype;
|
|
74
|
+
if (dragger && !installed.has(dragger)) {
|
|
75
|
+
installed.add(dragger);
|
|
76
|
+
const {startBlockDrag, dragBlock, endBlockDrag} = dragger;
|
|
77
|
+
dragger.startBlockDrag = function (...args) {
|
|
78
|
+
const result = startBlockDrag.apply(this, args);
|
|
79
|
+
try {
|
|
80
|
+
owners.get(this.workspace_)?.dragStarted(this);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
owners.get(this.workspace_)?.onError(error);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
dragger.dragBlock = function (event, delta) {
|
|
87
|
+
const result = dragBlock.call(this, event, delta);
|
|
88
|
+
owners.get(this.workspace_)?.dragMoved(this, delta);
|
|
89
|
+
return result;
|
|
90
|
+
};
|
|
91
|
+
dragger.endBlockDrag = function (event, delta) {
|
|
92
|
+
const owner = owners.get(this.workspace_);
|
|
93
|
+
// Move or delete the other scripts in the drag's event group, which the original call closes.
|
|
94
|
+
try {
|
|
95
|
+
owner?.dragEnding(this, event, delta);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
owner?.onError(error);
|
|
98
|
+
}
|
|
99
|
+
return endBlockDrag.call(this, event, delta);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const block = blocks.BlockSvg?.prototype;
|
|
103
|
+
if (block?.showContextMenu_ && !installed.has(block)) {
|
|
104
|
+
installed.add(block);
|
|
105
|
+
const {showContextMenu_} = block;
|
|
106
|
+
block.showContextMenu_ = function (event) {
|
|
107
|
+
const extra = owners.get(this.workspace)?.blockMenuOption(this);
|
|
108
|
+
if (!extra) return showContextMenu_.call(this, event);
|
|
109
|
+
const {show} = blocks.ContextMenu;
|
|
110
|
+
blocks.ContextMenu.show = (menuEvent, options, rtl) => show.call(blocks.ContextMenu, menuEvent, [...options, extra], rtl);
|
|
111
|
+
try {
|
|
112
|
+
return showContextMenu_.call(this, event);
|
|
113
|
+
} finally {
|
|
114
|
+
blocks.ContextMenu.show = show;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function createSelection({workspace, blocks, onError, getLocale}) {
|
|
121
|
+
const svg: SVGSVGElement = workspace.getParentSvg();
|
|
122
|
+
const injection: HTMLElement = workspace.getInjectionDiv();
|
|
123
|
+
const document = injection.ownerDocument;
|
|
124
|
+
const window = document.defaultView;
|
|
125
|
+
const mac = isMacPlatform(window.navigator);
|
|
126
|
+
const selected = new Set<string>();
|
|
127
|
+
let companions: {block: SelectableBlock; start: Point}[] = [];
|
|
128
|
+
let marquee: {start: Point; element: HTMLDivElement; remove: boolean; abort: AbortController} | undefined;
|
|
129
|
+
let pendingClear: {start: Point} | undefined;
|
|
130
|
+
|
|
131
|
+
const readOnly = () => Boolean(workspace.options?.readOnly);
|
|
132
|
+
const blockAt = (target: EventTarget | null): SelectableBlock | undefined => {
|
|
133
|
+
const node = (target as Element | null)?.closest?.('[data-id]');
|
|
134
|
+
const id = node?.getAttribute('data-id');
|
|
135
|
+
const block = id ? workspace.getBlockById(id) as SelectableBlock | null : null;
|
|
136
|
+
return block && !block.getSvgRoot()?.closest('[data-tw-plus-hidden]') ? block.getRootBlock() : undefined;
|
|
137
|
+
};
|
|
138
|
+
const isBackground = (target: EventTarget | null) => {
|
|
139
|
+
const element = target as Element | null;
|
|
140
|
+
return element === svg || Boolean(element?.classList?.contains('blocklyMainBackground'));
|
|
141
|
+
};
|
|
142
|
+
const visibleTops = () => (workspace.getTopBlocks(false) as SelectableBlock[]).filter(block => {
|
|
143
|
+
const root = block.getSvgRoot();
|
|
144
|
+
return root && root.getClientRects().length && !root.closest('[data-tw-plus-hidden]');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
function paint() {
|
|
148
|
+
for (const element of svg.querySelectorAll(`.${selectedClass}`)) {
|
|
149
|
+
const id = element.getAttribute('data-id');
|
|
150
|
+
if (!id || !selected.has(id)) element.classList.remove(selectedClass);
|
|
151
|
+
}
|
|
152
|
+
for (const id of selected) workspace.getBlockById(id)?.getSvgRoot()?.classList.add(selectedClass);
|
|
153
|
+
injection.toggleAttribute('data-tw-plus-script-selection', selected.size > 0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Keep only scripts that are still on the workspace and still start a stack. */
|
|
157
|
+
function prune() {
|
|
158
|
+
let changed = false;
|
|
159
|
+
for (const id of [...selected]) {
|
|
160
|
+
const block = workspace.getBlockById(id) as SelectableBlock | null;
|
|
161
|
+
if (!block || block.getParent()) {
|
|
162
|
+
selected.delete(id);
|
|
163
|
+
changed = true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (changed) paint();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function set(ids: Iterable<string>) {
|
|
170
|
+
selected.clear();
|
|
171
|
+
for (const id of ids) selected.add(id);
|
|
172
|
+
paint();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function toggle(block: SelectableBlock) {
|
|
176
|
+
if (selected.has(block.id)) selected.delete(block.id);
|
|
177
|
+
else selected.add(block.id);
|
|
178
|
+
paint();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const clear = () => {
|
|
182
|
+
if (selected.size) set([]);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
function deletable(): SelectableBlock[] {
|
|
186
|
+
prune();
|
|
187
|
+
return [...selected].map(id => workspace.getBlockById(id) as SelectableBlock | null)
|
|
188
|
+
.filter((block): block is SelectableBlock => Boolean(block?.isDeletable()));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Undo an edit that left calls to a function without its definition, as Scratch does for one script. */
|
|
192
|
+
function rollBackOrphans(before: Set<string>) {
|
|
193
|
+
window.setTimeout(() => {
|
|
194
|
+
const after = orphanCalls(workspace);
|
|
195
|
+
if (![...after].some(code => !before.has(code))) return;
|
|
196
|
+
window.alert(blocks.Msg?.PROCEDURE_USED ?? 'This function is in use.');
|
|
197
|
+
workspace.undo(false);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function deleteSelected() {
|
|
202
|
+
const targets = deletable();
|
|
203
|
+
if (!targets.length || readOnly()) return;
|
|
204
|
+
const before = orphanCalls(workspace);
|
|
205
|
+
blocks.hideChaff?.();
|
|
206
|
+
blocks.Events.setGroup(true);
|
|
207
|
+
try {
|
|
208
|
+
for (const block of targets) block.dispose(false, true);
|
|
209
|
+
} finally {
|
|
210
|
+
blocks.Events.setGroup(false);
|
|
211
|
+
}
|
|
212
|
+
set([]);
|
|
213
|
+
rollBackOrphans(before);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function startMarquee(event: MouseEvent) {
|
|
217
|
+
const abort = new window.AbortController();
|
|
218
|
+
const element = document.createElement('div');
|
|
219
|
+
element.className = marqueeClass;
|
|
220
|
+
element.setAttribute('aria-hidden', 'true');
|
|
221
|
+
injection.append(element);
|
|
222
|
+
marquee = {start: {x: event.clientX, y: event.clientY}, element, remove: event.altKey, abort};
|
|
223
|
+
const place = (point: Point) => {
|
|
224
|
+
const rect = rectFromCorners(marquee.start, point);
|
|
225
|
+
const origin = injection.getBoundingClientRect();
|
|
226
|
+
Object.assign(element.style, {left: `${rect.left - origin.left}px`, top: `${rect.top - origin.top}px`,
|
|
227
|
+
width: `${rect.right - rect.left}px`, height: `${rect.bottom - rect.top}px`});
|
|
228
|
+
return rect;
|
|
229
|
+
};
|
|
230
|
+
place(marquee.start);
|
|
231
|
+
const options = {capture: true, signal: abort.signal};
|
|
232
|
+
document.addEventListener('mousemove', move => {
|
|
233
|
+
move.preventDefault();
|
|
234
|
+
place({x: move.clientX, y: move.clientY});
|
|
235
|
+
}, options);
|
|
236
|
+
document.addEventListener('mouseup', up => {
|
|
237
|
+
up.preventDefault();
|
|
238
|
+
up.stopPropagation();
|
|
239
|
+
finishMarquee(place({x: up.clientX, y: up.clientY}));
|
|
240
|
+
}, options);
|
|
241
|
+
window.addEventListener('blur', () => finishMarquee(undefined), options);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function finishMarquee(rect: Rect | undefined) {
|
|
245
|
+
if (!marquee) return;
|
|
246
|
+
const {element, remove, abort} = marquee;
|
|
247
|
+
marquee = undefined;
|
|
248
|
+
abort.abort();
|
|
249
|
+
element.remove();
|
|
250
|
+
if (!rect || (rect.right - rect.left < 3 && rect.bottom - rect.top < 3)) return;
|
|
251
|
+
const hits = visibleTops().filter(block => intersects(rect, block.getSvgRoot().getBoundingClientRect())).map(block => block.id);
|
|
252
|
+
if (remove) for (const id of hits) selected.delete(id);
|
|
253
|
+
else for (const id of hits) selected.add(id);
|
|
254
|
+
paint();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function onMouseDown(event: MouseEvent) {
|
|
258
|
+
if (readOnly() || event.button !== 0) return;
|
|
259
|
+
activeWorkspace = workspace;
|
|
260
|
+
prune();
|
|
261
|
+
if (isToggleClick(event, mac)) {
|
|
262
|
+
const block = blockAt(event.target);
|
|
263
|
+
if (!block) return;
|
|
264
|
+
// Keep Blockly from starting a gesture, which would run the script on click.
|
|
265
|
+
event.preventDefault();
|
|
266
|
+
event.stopPropagation();
|
|
267
|
+
blocks.hideChaff?.();
|
|
268
|
+
toggle(block);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (event.shiftKey && isBackground(event.target)) {
|
|
272
|
+
event.preventDefault();
|
|
273
|
+
event.stopPropagation();
|
|
274
|
+
blocks.hideChaff?.();
|
|
275
|
+
startMarquee(event);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey) return;
|
|
279
|
+
const block = blockAt(event.target);
|
|
280
|
+
if (block) {
|
|
281
|
+
if (!selected.has(block.id)) clear();
|
|
282
|
+
} else if (isBackground(event.target)) {
|
|
283
|
+
// Dragging the background scrolls; only a click clears.
|
|
284
|
+
pendingClear = {start: {x: event.clientX, y: event.clientY}};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function onMouseUp(event: MouseEvent) {
|
|
289
|
+
if (!pendingClear) return;
|
|
290
|
+
const {start} = pendingClear;
|
|
291
|
+
pendingClear = undefined;
|
|
292
|
+
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) < 4) clear();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
296
|
+
if (activeWorkspace !== workspace || !selected.size || !svg.isConnected || !svg.getClientRects().length) return;
|
|
297
|
+
const target = event.target as HTMLElement | null;
|
|
298
|
+
if (target?.closest?.('input, textarea, select, [contenteditable=""], [contenteditable="true"]')) return;
|
|
299
|
+
if (event.key === 'Escape') {
|
|
300
|
+
clear();
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if ((event.key !== 'Delete' && event.key !== 'Backspace') || event.ctrlKey || event.metaKey || event.altKey) return;
|
|
304
|
+
if (readOnly() || workspace.isDragging?.() || !deletable().length) return;
|
|
305
|
+
// Blockly's own handler would delete only its selected block.
|
|
306
|
+
event.preventDefault();
|
|
307
|
+
event.stopImmediatePropagation();
|
|
308
|
+
deleteSelected();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const abort = new window.AbortController();
|
|
312
|
+
svg.addEventListener('mousedown', onMouseDown, {capture: true, signal: abort.signal});
|
|
313
|
+
document.addEventListener('mouseup', onMouseUp, {capture: true, signal: abort.signal});
|
|
314
|
+
window.addEventListener('keydown', onKeyDown, {capture: true, signal: abort.signal});
|
|
315
|
+
const listener = workspace.addChangeListener(() => prune());
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
onError,
|
|
319
|
+
get ids() {
|
|
320
|
+
prune();
|
|
321
|
+
return [...selected];
|
|
322
|
+
},
|
|
323
|
+
set,
|
|
324
|
+
clear,
|
|
325
|
+
deleteSelected,
|
|
326
|
+
dragStarted(dragger) {
|
|
327
|
+
companions = [];
|
|
328
|
+
const dragged = dragger.draggingBlock_ as SelectableBlock;
|
|
329
|
+
if (!selected.has(dragged.id) || dragged.getParent()) {
|
|
330
|
+
// Dragging an unselected script, or a part pulled out of a selected one, moves only that.
|
|
331
|
+
if (!selected.has(dragged.id)) clear();
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
prune();
|
|
335
|
+
companions = [...selected].filter(id => id !== dragged.id)
|
|
336
|
+
.map(id => workspace.getBlockById(id) as SelectableBlock | null)
|
|
337
|
+
.filter((block): block is SelectableBlock => Boolean(block && block.isMovable?.() !== false))
|
|
338
|
+
.map(block => ({block, start: block.getRelativeToSurfaceXY()}));
|
|
339
|
+
},
|
|
340
|
+
dragMoved(dragger, delta: Point) {
|
|
341
|
+
if (!companions.length) return;
|
|
342
|
+
const offset = dragger.pixelsToWorkspaceUnits_(delta);
|
|
343
|
+
for (const {block, start} of companions) block.translate(start.x + offset.x, start.y + offset.y);
|
|
344
|
+
},
|
|
345
|
+
dragEnding(dragger, event, delta: Point) {
|
|
346
|
+
const moving = companions;
|
|
347
|
+
companions = [];
|
|
348
|
+
if (!moving.length) return;
|
|
349
|
+
// The original call refreshes this state first; do the same so the decision matches.
|
|
350
|
+
dragger.dragBlock(event, delta);
|
|
351
|
+
const offset = dragger.pixelsToWorkspaceUnits_(delta);
|
|
352
|
+
for (const {block, start} of moving) block.translate(start.x, start.y);
|
|
353
|
+
if (dragger.wouldDeleteBlock_) {
|
|
354
|
+
const before = orphanCalls(workspace);
|
|
355
|
+
for (const {block} of moving) if (block.isDeletable()) block.dispose(false, true);
|
|
356
|
+
set([]);
|
|
357
|
+
rollBackOrphans(before);
|
|
358
|
+
} else {
|
|
359
|
+
// A drop outside the blocks area is undone as one group by Blockly, these moves included.
|
|
360
|
+
for (const {block} of moving) block.moveBy(offset.x, offset.y);
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
blockMenuOption(block: SelectableBlock) {
|
|
364
|
+
if (readOnly() || !selected.has(block.getRootBlock().id)) return undefined;
|
|
365
|
+
const count = deletable().length;
|
|
366
|
+
if (!count) return undefined;
|
|
367
|
+
return {text: labels(getLocale(), count).remove, enabled: true, callback: deleteSelected};
|
|
368
|
+
},
|
|
369
|
+
dispose() {
|
|
370
|
+
finishMarquee(undefined);
|
|
371
|
+
abort.abort();
|
|
372
|
+
workspace.removeChangeListener(listener);
|
|
373
|
+
set([]);
|
|
374
|
+
injection.removeAttribute('data-tw-plus-script-selection');
|
|
375
|
+
if (activeWorkspace === workspace) activeWorkspace = null;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Lets scripts in a free-layout workspace be selected, moved and deleted together. */
|
|
381
|
+
export function attachScriptSelection({workspace, ScratchBlocks: blocks, vm, onError = (error: unknown) => console.error(error),
|
|
382
|
+
getLocale = () => blocks.ScratchMsgs?.currentLocale_ ?? ''}: {
|
|
383
|
+
workspace; ScratchBlocks; onError?: (error: unknown) => void; getLocale?: () => string;
|
|
384
|
+
/** Clears the selection when the workspace loads another sprite or the stage. */
|
|
385
|
+
vm?: {on(name: string, listener: () => void): void; removeListener(name: string, listener: () => void): void};
|
|
386
|
+
}) {
|
|
387
|
+
installHooks(blocks);
|
|
388
|
+
owners.get(workspace)?.dispose();
|
|
389
|
+
const selection = createSelection({workspace, blocks, onError, getLocale});
|
|
390
|
+
owners.set(workspace, selection);
|
|
391
|
+
const clearOnLoad = () => selection.clear();
|
|
392
|
+
vm?.on('workspaceUpdate', clearOnLoad);
|
|
393
|
+
return {
|
|
394
|
+
/** The ids of the selected scripts' top blocks. */
|
|
395
|
+
get selected() {
|
|
396
|
+
return selection.ids;
|
|
397
|
+
},
|
|
398
|
+
select(ids: Iterable<string>) {
|
|
399
|
+
selection.set(ids);
|
|
400
|
+
},
|
|
401
|
+
clear: selection.clear,
|
|
402
|
+
deleteSelected: selection.deleteSelected,
|
|
403
|
+
dispose() {
|
|
404
|
+
vm?.removeListener('workspaceUpdate', clearOnLoad);
|
|
405
|
+
if (owners.get(workspace) !== selection) return;
|
|
406
|
+
owners.delete(workspace);
|
|
407
|
+
selection.dispose();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}
|
package/src/styles.css
CHANGED
|
@@ -1352,3 +1352,18 @@
|
|
|
1352
1352
|
.tw-palette-group-item[data-palette-group-pinned] {
|
|
1353
1353
|
position: relative;
|
|
1354
1354
|
}
|
|
1355
|
+
|
|
1356
|
+
/* Scripts selected together in the free layout (script-selection.ts). */
|
|
1357
|
+
.blocklyBlockCanvas .tw-plus-script-selected {
|
|
1358
|
+
filter: drop-shadow(0 0 1.5px var(--tw-plus-script-selection, #4c97ff))
|
|
1359
|
+
drop-shadow(0 0 3px var(--tw-plus-script-selection, #4c97ff));
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
.tw-plus-script-marquee {
|
|
1363
|
+
position: absolute;
|
|
1364
|
+
z-index: 30;
|
|
1365
|
+
box-sizing: border-box;
|
|
1366
|
+
border: 1px solid var(--tw-plus-script-selection, #4c97ff);
|
|
1367
|
+
background: color-mix(in srgb, var(--tw-plus-script-selection, #4c97ff) 14%, transparent);
|
|
1368
|
+
pointer-events: none;
|
|
1369
|
+
}
|
package/src/workspace.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {attachBlockDrag} from './block-drag.ts';
|
|
2
|
+
import {attachScriptSelection} from './script-selection.ts';
|
|
2
3
|
import {registerCodeWorkspace} from './code-workspace-registry.ts';
|
|
3
4
|
import {workspaceXML, type CodeTarget} from './history.ts';
|
|
4
5
|
import {isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition} from './procedure-edits.ts';
|
|
@@ -676,6 +677,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
676
677
|
},
|
|
677
678
|
dispose() {
|
|
678
679
|
blockDrag.dispose();
|
|
680
|
+
scriptSelection.dispose();
|
|
679
681
|
unregisterRelationNavigation();
|
|
680
682
|
unregisterCodeWorkspace();
|
|
681
683
|
shown?.disconnect();
|
|
@@ -703,6 +705,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
703
705
|
shown?.observe(injection);
|
|
704
706
|
|
|
705
707
|
const blockDrag = attachBlockDrag({vm, workspace, ScratchBlocks: blocks, onError, prepareImport: bridge.prepareImport});
|
|
708
|
+
const scriptSelection = attachScriptSelection({workspace, ScratchBlocks: blocks, vm, onError});
|
|
706
709
|
const visibility = attachScriptVisibility({vm, blocks, workspace, homeDocument, onError});
|
|
707
710
|
const unregisterRelationNavigation = registerScriptRelationNavigation(vm, endpoint => {
|
|
708
711
|
if (vm.runtime.getTargetById(endpoint.targetId)?.blocks._blocks[endpoint.blockId] === undefined) return;
|