@kubohiroya/turbowarp-plus-editor-code-views 0.26.1 → 0.26.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/README.ja.md CHANGED
@@ -23,6 +23,7 @@ Z/N配置内の矢印は、スクリプト群の右外側へ96px以上張り
23
23
  - Ctrl+クリック(macOS では ⌘+クリック)で、クリックしたスクリプト全体を選択に加えたり外したりします。スクリプトは実行しません。macOS の Ctrl+クリックは右クリックとして扱われるため使いません。
24
24
  - 背景を Shift+ドラッグすると矩形を描き、重なったスクリプトを選択に加えます。Shift+Alt+ドラッグでは選択から外します。
25
25
  - 選択中のスクリプトをドラッグすると、ほかの選択中のスクリプトも同じだけ動きます。パレットに落とすと、選択中のスクリプトをすべて削除します。どちらも1回の「元に戻す」で戻ります。
26
+ - 選択中のスクリプトをスプライト一覧のスプライトや背景に落とすと、選択中のスクリプトをすべてそこへコピーします。ドラッグしたスクリプトとの位置関係は保ちます。元のスクリプトは動きません。
26
27
  - Delete・Backspace キー、またはブロックの右クリックメニューの「選択した N 個のスクリプトを削除」で、まとめて削除します。呼び出されている関数の定義が消える場合は、Scratch と同じく警告を出して削除全体を取り消します。
27
28
  - Esc、背景のクリック、選択していないスクリプトを押すこと、スプライトやステージの切り替えで、選択を解除します。
28
29
 
package/README.md CHANGED
@@ -17,6 +17,7 @@ In the free layout, several scripts can be selected and then moved or deleted to
17
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
18
  - Shift+drag on the background draws a rectangle and adds the scripts it touches. Shift+Alt+drag removes them.
19
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
+ - Dropping a selected script on a sprite or the stage in the sprite list copies every selected script there, keeping their places relative to the dragged one. The selected scripts stay where they were.
20
21
  - 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
22
  - Escape, a click on the background, pressing an unselected script or switching sprites clears the selection.
22
23
 
@@ -22,7 +22,7 @@ const messages = {
22
22
  hiddenInUse: (count) => `Hidden and in use: ${count}`, otherBlocks: 'Other blocks', cancel: 'Cancel', confirm: 'Confirm', close: 'Close'
23
23
  },
24
24
  ja: {
25
- title: 'パレット内のブロックグループの表示・非表示の設定', sprite: (name) => `スプライト「${name}」`, stage: '背景',
25
+ title: 'パレット内のブロックグループの表示・非表示の設定', sprite: (name) => `スプライト「${name}」`, stage: 'ステージ',
26
26
  newSprite: (name) => `新しいスプライト「${name}」`,
27
27
  quick: 'まとめて選ぶ', copyFrom: '設定のコピー元', copy: 'コピー', chooseSprite: 'スプライトを選ぶ',
28
28
  sets: '設定のショートカット', addSet: '設定のショートカットを追加', setName: 'ショートカットの名前', add: '追加',
@@ -4,6 +4,7 @@
4
4
  * - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
5
5
  * - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
6
6
  * - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
7
+ * - Dropping a selected script on a sprite or the stage in the sprite list copies every selected script there.
7
8
  * - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
8
9
  * - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
9
10
  *
@@ -35,6 +36,49 @@ export declare const isToggleClick: (event: {
35
36
  export declare const rectFromCorners: (a: Point, b: Point) => Rect;
36
37
  /** Whether two rectangles overlap, touching edges included. */
37
38
  export declare const intersects: (a: Rect, b: Rect) => boolean;
39
+ /** A block as the VM stores it. */
40
+ interface VMBlock {
41
+ id: string;
42
+ opcode?: string;
43
+ next?: string | null;
44
+ topLevel?: boolean;
45
+ x?: number;
46
+ y?: number;
47
+ inputs?: Record<string, {
48
+ block?: string | null;
49
+ shadow?: string | null;
50
+ }>;
51
+ comment?: string;
52
+ }
53
+ interface SelectionVM {
54
+ on(name: string, listener: () => void): void;
55
+ removeListener(name: string, listener: () => void): void;
56
+ editingTarget?: {
57
+ id: string;
58
+ blocks: {
59
+ _blocks: Record<string, VMBlock>;
60
+ };
61
+ } | null;
62
+ shareBlocksToTarget?(blocks: unknown, targetId: string, fromTargetId?: string): Promise<unknown>;
63
+ }
64
+ /** Copies of a script's blocks, its top block first. Comments stay behind, as in a single-script drop. */
65
+ export declare function copyScript(blocks: Record<string, VMBlock>, topId: string): VMBlock[];
66
+ /** A group drop outside the blocks area: the dragged script and copies of the others. */
67
+ export interface OutsideDrop {
68
+ /** The target the scripts came from. */
69
+ source: string;
70
+ /** The dragged script's top opcode and number of blocks, which its copy keeps under new ids. */
71
+ opcode: string;
72
+ size: number;
73
+ origin: Point;
74
+ scripts: VMBlock[][];
75
+ }
76
+ /**
77
+ * Add the other selected scripts to the blocks the GUI copies to a sprite, keeping their places
78
+ * relative to the dragged script, which the GUI moves into the receiving sprite's view.
79
+ * @returns Whether the payload is the dragged script's and the scripts were added.
80
+ */
81
+ export declare function addCompanions(payload: unknown, drop: OutsideDrop): boolean;
38
82
  /** Lets scripts in a free-layout workspace be selected, moved and deleted together. */
39
83
  export declare function attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, onError, getLocale }: {
40
84
  workspace: any;
@@ -42,10 +86,7 @@ export declare function attachScriptSelection({ workspace, ScratchBlocks: blocks
42
86
  onError?: (error: unknown) => void;
43
87
  getLocale?: () => string;
44
88
  /** 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
- };
89
+ vm?: SelectionVM;
49
90
  }): {
50
91
  /** The ids of the selected scripts' top blocks. */
51
92
  readonly selected: string[];
@@ -5,6 +5,7 @@
5
5
  * - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
6
6
  * - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
7
7
  * - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
8
+ * - Dropping a selected script on a sprite or the stage in the sprite list copies every selected script there.
8
9
  * - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
9
10
  * - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
10
11
  *
@@ -29,6 +30,44 @@ export const intersects = (a, b) => a.left <= b.right && b.left <= a.right && a.
29
30
  const labels = (locale, count) => /^ja(?:-|$)/i.test(locale) ?
30
31
  { remove: `選択した${count}個のスクリプトを削除` } :
31
32
  { remove: count === 1 ? 'Delete 1 selected script' : `Delete ${count} selected scripts` };
33
+ /** Copies of a script's blocks, its top block first. Comments stay behind, as in a single-script drop. */
34
+ export function copyScript(blocks, topId) {
35
+ const copies = [];
36
+ const seen = new Set();
37
+ const pending = [topId];
38
+ while (pending.length) {
39
+ const id = pending.shift();
40
+ if (!id || seen.has(id) || !blocks[id])
41
+ continue;
42
+ seen.add(id);
43
+ const copy = JSON.parse(JSON.stringify(blocks[id]));
44
+ delete copy.comment;
45
+ copies.push(copy);
46
+ pending.push(copy.next);
47
+ for (const input of Object.values(copy.inputs ?? {}))
48
+ pending.push(input.block, input.shadow);
49
+ }
50
+ return copies;
51
+ }
52
+ /**
53
+ * Add the other selected scripts to the blocks the GUI copies to a sprite, keeping their places
54
+ * relative to the dragged script, which the GUI moves into the receiving sprite's view.
55
+ * @returns Whether the payload is the dragged script's and the scripts were added.
56
+ */
57
+ export function addCompanions(payload, drop) {
58
+ const list = Array.isArray(payload) ? payload :
59
+ Array.isArray(payload?.blocks) ? payload.blocks : null;
60
+ const top = list?.find(block => block.topLevel);
61
+ if (!top || top.opcode !== drop.opcode || list.length !== drop.size)
62
+ return false;
63
+ const dx = (top.x ?? drop.origin.x) - drop.origin.x;
64
+ const dy = (top.y ?? drop.origin.y) - drop.origin.y;
65
+ for (const script of drop.scripts) {
66
+ const [head] = script;
67
+ list.push({ ...head, x: (head.x ?? 0) + dx, y: (head.y ?? 0) + dy }, ...script.slice(1));
68
+ }
69
+ return true;
70
+ }
32
71
  /** Procedure codes called by a block but defined nowhere: the calls a deletion would leave behind. */
33
72
  function orphanCalls(workspace) {
34
73
  const defined = new Set();
@@ -92,7 +131,7 @@ function installHooks(blocks) {
92
131
  };
93
132
  }
94
133
  }
95
- function createSelection({ workspace, blocks, onError, getLocale }) {
134
+ function createSelection({ workspace, blocks, onError, getLocale, vm }) {
96
135
  const svg = workspace.getParentSvg();
97
136
  const injection = workspace.getInjectionDiv();
98
137
  const document = injection.ownerDocument;
@@ -100,6 +139,8 @@ function createSelection({ workspace, blocks, onError, getLocale }) {
100
139
  const mac = isMacPlatform(window.navigator);
101
140
  const selected = new Set();
102
141
  let companions = [];
142
+ /** The last group drop outside the blocks area, which the GUI may copy to a sprite. */
143
+ let outsideDrop;
103
144
  let marquee;
104
145
  let pendingClear;
105
146
  const readOnly = () => Boolean(workspace.options?.readOnly);
@@ -146,6 +187,17 @@ function createSelection({ workspace, blocks, onError, getLocale }) {
146
187
  selected.add(id);
147
188
  paint();
148
189
  }
190
+ function rememberOutsideDrop(dragged, ids) {
191
+ // Read the VM before it hears of these moves, so the copies keep the places they had.
192
+ const source = vm?.editingTarget;
193
+ const records = source?.blocks?._blocks;
194
+ const head = records?.[dragged];
195
+ if (!head)
196
+ return;
197
+ outsideDrop = { source: source.id, opcode: head.opcode, size: copyScript(records, dragged).length,
198
+ origin: { x: head.x ?? 0, y: head.y ?? 0 },
199
+ scripts: ids.map(id => copyScript(records, id)).filter(script => script.length) };
200
+ }
149
201
  function toggle(block) {
150
202
  if (selected.has(block.id))
151
203
  selected.delete(block.id);
@@ -312,6 +364,7 @@ function createSelection({ workspace, blocks, onError, getLocale }) {
312
364
  deleteSelected,
313
365
  dragStarted(dragger) {
314
366
  companions = [];
367
+ outsideDrop = undefined;
315
368
  const dragged = dragger.draggingBlock_;
316
369
  if (!selected.has(dragged.id) || dragged.getParent()) {
317
370
  // Dragging an unselected script, or a part pulled out of a selected one, moves only that.
@@ -352,10 +405,18 @@ function createSelection({ workspace, blocks, onError, getLocale }) {
352
405
  }
353
406
  else {
354
407
  // A drop outside the blocks area is undone as one group by Blockly, these moves included.
408
+ if (dragger.wasOutside_)
409
+ rememberOutsideDrop(dragger.draggingBlock_.id, moving.map(({ block }) => block.id));
355
410
  for (const { block } of moving)
356
411
  block.moveBy(offset.x, offset.y);
357
412
  }
358
413
  },
414
+ /** The group drop the GUI is copying to a sprite, once. */
415
+ takeOutsideDrop() {
416
+ const drop = outsideDrop;
417
+ outsideDrop = undefined;
418
+ return drop;
419
+ },
359
420
  blockMenuOption(block) {
360
421
  if (readOnly() || !selected.has(block.getRootBlock().id))
361
422
  return undefined;
@@ -379,10 +440,29 @@ function createSelection({ workspace, blocks, onError, getLocale }) {
379
440
  export function attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, onError = (error) => console.error(error), getLocale = () => blocks.ScratchMsgs?.currentLocale_ ?? '' }) {
380
441
  installHooks(blocks);
381
442
  owners.get(workspace)?.dispose();
382
- const selection = createSelection({ workspace, blocks, onError, getLocale });
443
+ const selection = createSelection({ workspace, blocks, onError, getLocale, vm });
383
444
  owners.set(workspace, selection);
384
445
  const clearOnLoad = () => selection.clear();
385
446
  vm?.on('workspaceUpdate', clearOnLoad);
447
+ // The GUI copies a script dropped on the sprite list with the source target as the third
448
+ // argument; add the rest of the selection to that copy.
449
+ const share = vm?.shareBlocksToTarget;
450
+ const hadOwnShare = Boolean(vm) && Object.prototype.hasOwnProperty.call(vm, 'shareBlocksToTarget');
451
+ const sharing = share && function (payload, targetId, fromTargetId) {
452
+ if (fromTargetId && fromTargetId !== targetId) {
453
+ const drop = selection.takeOutsideDrop();
454
+ try {
455
+ if (drop?.source === fromTargetId)
456
+ addCompanions(payload, drop);
457
+ }
458
+ catch (error) {
459
+ onError(error);
460
+ }
461
+ }
462
+ return share.call(this, payload, targetId, fromTargetId);
463
+ };
464
+ if (sharing)
465
+ vm.shareBlocksToTarget = sharing;
386
466
  return {
387
467
  /** The ids of the selected scripts' top blocks. */
388
468
  get selected() {
@@ -395,6 +475,12 @@ export function attachScriptSelection({ workspace, ScratchBlocks: blocks, vm, on
395
475
  deleteSelected: selection.deleteSelected,
396
476
  dispose() {
397
477
  vm?.removeListener('workspaceUpdate', clearOnLoad);
478
+ if (sharing && vm.shareBlocksToTarget === sharing) {
479
+ if (hadOwnShare)
480
+ vm.shareBlocksToTarget = share;
481
+ else
482
+ delete vm.shareBlocksToTarget;
483
+ }
398
484
  if (owners.get(workspace) !== selection)
399
485
  return;
400
486
  owners.delete(workspace);
@@ -60,24 +60,24 @@ export interface SharedCopy {
60
60
  }
61
61
  declare const messages: {
62
62
  en: {
63
- copiedToSprite: (name: string) => string;
64
- copiedToStage: string;
63
+ copiedToSprite: (name: string, count?: number) => string;
64
+ copiedToStage: (count?: number) => string;
65
65
  undo: string;
66
66
  close: string;
67
67
  removedFromSprite: (name: string) => string;
68
68
  removedFromStage: string;
69
69
  };
70
70
  ja: {
71
- copiedToSprite: (name: string) => string;
72
- copiedToStage: string;
71
+ copiedToSprite: (name: string, count?: number) => string;
72
+ copiedToStage: (count?: number) => string;
73
73
  undo: string;
74
74
  close: string;
75
75
  removedFromSprite: (name: string) => string;
76
76
  removedFromStage: string;
77
77
  };
78
78
  'ja-hira': {
79
- copiedToSprite: (name: string) => string;
80
- copiedToStage: string;
79
+ copiedToSprite: (name: string, count?: number) => string;
80
+ copiedToStage: (count?: number) => string;
81
81
  undo: string;
82
82
  close: string;
83
83
  removedFromSprite: (name: string) => string;
@@ -10,25 +10,28 @@
10
10
  */
11
11
  const messages = {
12
12
  en: {
13
- copiedToSprite: (name) => `Copied the script to “${name}”.`,
14
- copiedToStage: 'Copied the script to the Stage.',
13
+ copiedToSprite: (name, count = 1) => count > 1 ?
14
+ `Copied ${count} scripts to “${name}”.` : `Copied the script to “${name}”.`,
15
+ copiedToStage: (count = 1) => count > 1 ? `Copied ${count} scripts to the Stage.` : 'Copied the script to the Stage.',
15
16
  undo: 'Undo', close: 'Close',
16
17
  removedFromSprite: (name) => `Removed the copy from “${name}”.`,
17
18
  removedFromStage: 'Removed the copy from the Stage.'
18
19
  },
19
20
  ja: {
20
- copiedToSprite: (name) => `スプライト「${name}」にスクリプトをコピーしました。`,
21
- copiedToStage: '背景にスクリプトをコピーしました。',
21
+ copiedToSprite: (name, count = 1) => count > 1 ?
22
+ `スプライト「${name}」に${count}個のスクリプトをコピーしました。` : `スプライト「${name}」にスクリプトをコピーしました。`,
23
+ copiedToStage: (count = 1) => count > 1 ? `ステージに${count}個のスクリプトをコピーしました。` : 'ステージにスクリプトをコピーしました。',
22
24
  undo: '元に戻す', close: '閉じる',
23
25
  removedFromSprite: (name) => `スプライト「${name}」からコピーを取り消しました。`,
24
- removedFromStage: '背景からコピーを取り消しました。'
26
+ removedFromStage: 'ステージからコピーを取り消しました。'
25
27
  },
26
28
  'ja-hira': {
27
- copiedToSprite: (name) => `スプライト「${name}」にスクリプトをコピーしました。`,
28
- copiedToStage: 'はいけいにスクリプトをコピーしました。',
29
+ copiedToSprite: (name, count = 1) => count > 1 ?
30
+ `スプライト「${name}」に${count}このスクリプトをコピーしました。` : `スプライト「${name}」にスクリプトをコピーしました。`,
31
+ copiedToStage: (count = 1) => count > 1 ? `ステージに${count}このスクリプトをコピーしました。` : 'ステージにスクリプトをコピーしました。',
29
32
  undo: 'もとにもどす', close: 'とじる',
30
33
  removedFromSprite: (name) => `スプライト「${name}」からコピーをとりけしました。`,
31
- removedFromStage: 'はいけいからコピーをとりけしました。'
34
+ removedFromStage: 'ステージからコピーをとりけしました。'
32
35
  }
33
36
  };
34
37
  /** The notice's wording for a locale: ja-hira, ja, and English for anything else. */
@@ -126,7 +129,8 @@ export function attachSpriteCopyNotice({ vm, document, locale, timeout = 10000,
126
129
  notice.setAttribute('aria-live', 'polite');
127
130
  const message = document.createElement('span');
128
131
  message.className = 'tw-sprite-copy-notice-text';
129
- message.textContent = target.isStage ? words.copiedToStage : words.copiedToSprite(name);
132
+ const count = copy.roots.length;
133
+ message.textContent = target.isStage ? words.copiedToStage(count) : words.copiedToSprite(name, count);
130
134
  const undo = document.createElement('button');
131
135
  undo.type = 'button';
132
136
  undo.className = 'tw-sprite-copy-notice-undo';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.26.1",
3
+ "version": "0.26.3",
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",
@@ -64,7 +64,7 @@ const messages = {
64
64
  hiddenInUse: (count: number) => `Hidden and in use: ${count}`, otherBlocks: 'Other blocks', cancel: 'Cancel', confirm: 'Confirm', close: 'Close'
65
65
  },
66
66
  ja: {
67
- title: 'パレット内のブロックグループの表示・非表示の設定', sprite: (name: string) => `スプライト「${name}」`, stage: '背景',
67
+ title: 'パレット内のブロックグループの表示・非表示の設定', sprite: (name: string) => `スプライト「${name}」`, stage: 'ステージ',
68
68
  newSprite: (name: string) => `新しいスプライト「${name}」`,
69
69
  quick: 'まとめて選ぶ', copyFrom: '設定のコピー元', copy: 'コピー', chooseSprite: 'スプライトを選ぶ',
70
70
  sets: '設定のショートカット', addSet: '設定のショートカットを追加', setName: 'ショートカットの名前', add: '追加',
@@ -5,6 +5,7 @@
5
5
  * - Ctrl+click (⌘+click on macOS, where Ctrl+click is the context menu) adds or removes the clicked script.
6
6
  * - Shift+drag on the background draws a rectangle; scripts it touches are added. Shift+Alt+drag removes them.
7
7
  * - Dragging a selected script moves the others by the same distance; dropping it on the palette deletes them all.
8
+ * - Dropping a selected script on a sprite or the stage in the sprite list copies every selected script there.
8
9
  * - Delete or Backspace, or the block context menu, deletes every selected script as one undoable edit.
9
10
  * - Escape, a plain click on the background or a plain press on an unselected script clears the selection.
10
11
  *
@@ -58,6 +59,72 @@ const labels = (locale: string, count: number) => /^ja(?:-|$)/i.test(locale) ?
58
59
  {remove: `選択した${count}個のスクリプトを削除`} :
59
60
  {remove: count === 1 ? 'Delete 1 selected script' : `Delete ${count} selected scripts`};
60
61
 
62
+ /** A block as the VM stores it. */
63
+ interface VMBlock {
64
+ id: string;
65
+ opcode?: string;
66
+ next?: string | null;
67
+ topLevel?: boolean;
68
+ x?: number;
69
+ y?: number;
70
+ inputs?: Record<string, {block?: string | null; shadow?: string | null}>;
71
+ comment?: string;
72
+ }
73
+ interface SelectionVM {
74
+ on(name: string, listener: () => void): void;
75
+ removeListener(name: string, listener: () => void): void;
76
+ editingTarget?: {id: string; blocks: {_blocks: Record<string, VMBlock>}} | null;
77
+ shareBlocksToTarget?(blocks: unknown, targetId: string, fromTargetId?: string): Promise<unknown>;
78
+ }
79
+
80
+ /** Copies of a script's blocks, its top block first. Comments stay behind, as in a single-script drop. */
81
+ export function copyScript(blocks: Record<string, VMBlock>, topId: string): VMBlock[] {
82
+ const copies: VMBlock[] = [];
83
+ const seen = new Set<string>();
84
+ const pending: Array<string | null | undefined> = [topId];
85
+ while (pending.length) {
86
+ const id = pending.shift();
87
+ if (!id || seen.has(id) || !blocks[id]) continue;
88
+ seen.add(id);
89
+ const copy = JSON.parse(JSON.stringify(blocks[id])) as VMBlock;
90
+ delete copy.comment;
91
+ copies.push(copy);
92
+ pending.push(copy.next);
93
+ for (const input of Object.values(copy.inputs ?? {})) pending.push(input.block, input.shadow);
94
+ }
95
+ return copies;
96
+ }
97
+
98
+ /** A group drop outside the blocks area: the dragged script and copies of the others. */
99
+ export interface OutsideDrop {
100
+ /** The target the scripts came from. */
101
+ source: string;
102
+ /** The dragged script's top opcode and number of blocks, which its copy keeps under new ids. */
103
+ opcode: string;
104
+ size: number;
105
+ origin: Point;
106
+ scripts: VMBlock[][];
107
+ }
108
+
109
+ /**
110
+ * Add the other selected scripts to the blocks the GUI copies to a sprite, keeping their places
111
+ * relative to the dragged script, which the GUI moves into the receiving sprite's view.
112
+ * @returns Whether the payload is the dragged script's and the scripts were added.
113
+ */
114
+ export function addCompanions(payload: unknown, drop: OutsideDrop) {
115
+ const list = Array.isArray(payload) ? payload as VMBlock[] :
116
+ Array.isArray((payload as {blocks?: unknown})?.blocks) ? (payload as {blocks: VMBlock[]}).blocks : null;
117
+ const top = list?.find(block => block.topLevel);
118
+ if (!top || top.opcode !== drop.opcode || list.length !== drop.size) return false;
119
+ const dx = (top.x ?? drop.origin.x) - drop.origin.x;
120
+ const dy = (top.y ?? drop.origin.y) - drop.origin.y;
121
+ for (const script of drop.scripts) {
122
+ const [head] = script;
123
+ list.push({...head, x: (head.x ?? 0) + dx, y: (head.y ?? 0) + dy}, ...script.slice(1));
124
+ }
125
+ return true;
126
+ }
127
+
61
128
  /** Procedure codes called by a block but defined nowhere: the calls a deletion would leave behind. */
62
129
  function orphanCalls(workspace): Set<string> {
63
130
  const defined = new Set<string>();
@@ -117,7 +184,7 @@ function installHooks(blocks) {
117
184
  }
118
185
  }
119
186
 
120
- function createSelection({workspace, blocks, onError, getLocale}) {
187
+ function createSelection({workspace, blocks, onError, getLocale, vm}: {workspace; blocks; onError; getLocale; vm?: SelectionVM}) {
121
188
  const svg: SVGSVGElement = workspace.getParentSvg();
122
189
  const injection: HTMLElement = workspace.getInjectionDiv();
123
190
  const document = injection.ownerDocument;
@@ -125,6 +192,8 @@ function createSelection({workspace, blocks, onError, getLocale}) {
125
192
  const mac = isMacPlatform(window.navigator);
126
193
  const selected = new Set<string>();
127
194
  let companions: {block: SelectableBlock; start: Point}[] = [];
195
+ /** The last group drop outside the blocks area, which the GUI may copy to a sprite. */
196
+ let outsideDrop: OutsideDrop | undefined;
128
197
  let marquee: {start: Point; element: HTMLDivElement; remove: boolean; abort: AbortController} | undefined;
129
198
  let pendingClear: {start: Point} | undefined;
130
199
 
@@ -172,6 +241,17 @@ function createSelection({workspace, blocks, onError, getLocale}) {
172
241
  paint();
173
242
  }
174
243
 
244
+ function rememberOutsideDrop(dragged: string, ids: string[]) {
245
+ // Read the VM before it hears of these moves, so the copies keep the places they had.
246
+ const source = vm?.editingTarget;
247
+ const records = source?.blocks?._blocks;
248
+ const head = records?.[dragged];
249
+ if (!head) return;
250
+ outsideDrop = {source: source.id, opcode: head.opcode, size: copyScript(records, dragged).length,
251
+ origin: {x: head.x ?? 0, y: head.y ?? 0},
252
+ scripts: ids.map(id => copyScript(records, id)).filter(script => script.length)};
253
+ }
254
+
175
255
  function toggle(block: SelectableBlock) {
176
256
  if (selected.has(block.id)) selected.delete(block.id);
177
257
  else selected.add(block.id);
@@ -325,6 +405,7 @@ function createSelection({workspace, blocks, onError, getLocale}) {
325
405
  deleteSelected,
326
406
  dragStarted(dragger) {
327
407
  companions = [];
408
+ outsideDrop = undefined;
328
409
  const dragged = dragger.draggingBlock_ as SelectableBlock;
329
410
  if (!selected.has(dragged.id) || dragged.getParent()) {
330
411
  // Dragging an unselected script, or a part pulled out of a selected one, moves only that.
@@ -357,9 +438,16 @@ function createSelection({workspace, blocks, onError, getLocale}) {
357
438
  rollBackOrphans(before);
358
439
  } else {
359
440
  // A drop outside the blocks area is undone as one group by Blockly, these moves included.
441
+ if (dragger.wasOutside_) rememberOutsideDrop(dragger.draggingBlock_.id, moving.map(({block}) => block.id));
360
442
  for (const {block} of moving) block.moveBy(offset.x, offset.y);
361
443
  }
362
444
  },
445
+ /** The group drop the GUI is copying to a sprite, once. */
446
+ takeOutsideDrop() {
447
+ const drop = outsideDrop;
448
+ outsideDrop = undefined;
449
+ return drop;
450
+ },
363
451
  blockMenuOption(block: SelectableBlock) {
364
452
  if (readOnly() || !selected.has(block.getRootBlock().id)) return undefined;
365
453
  const count = deletable().length;
@@ -382,14 +470,30 @@ export function attachScriptSelection({workspace, ScratchBlocks: blocks, vm, onE
382
470
  getLocale = () => blocks.ScratchMsgs?.currentLocale_ ?? ''}: {
383
471
  workspace; ScratchBlocks; onError?: (error: unknown) => void; getLocale?: () => string;
384
472
  /** 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};
473
+ vm?: SelectionVM;
386
474
  }) {
387
475
  installHooks(blocks);
388
476
  owners.get(workspace)?.dispose();
389
- const selection = createSelection({workspace, blocks, onError, getLocale});
477
+ const selection = createSelection({workspace, blocks, onError, getLocale, vm});
390
478
  owners.set(workspace, selection);
391
479
  const clearOnLoad = () => selection.clear();
392
480
  vm?.on('workspaceUpdate', clearOnLoad);
481
+ // The GUI copies a script dropped on the sprite list with the source target as the third
482
+ // argument; add the rest of the selection to that copy.
483
+ const share = vm?.shareBlocksToTarget;
484
+ const hadOwnShare = Boolean(vm) && Object.prototype.hasOwnProperty.call(vm, 'shareBlocksToTarget');
485
+ const sharing = share && function (this: SelectionVM, payload: unknown, targetId: string, fromTargetId?: string) {
486
+ if (fromTargetId && fromTargetId !== targetId) {
487
+ const drop = selection.takeOutsideDrop();
488
+ try {
489
+ if (drop?.source === fromTargetId) addCompanions(payload, drop);
490
+ } catch (error) {
491
+ onError(error);
492
+ }
493
+ }
494
+ return share.call(this, payload, targetId, fromTargetId);
495
+ };
496
+ if (sharing) vm.shareBlocksToTarget = sharing;
393
497
  return {
394
498
  /** The ids of the selected scripts' top blocks. */
395
499
  get selected() {
@@ -402,6 +506,10 @@ export function attachScriptSelection({workspace, ScratchBlocks: blocks, vm, onE
402
506
  deleteSelected: selection.deleteSelected,
403
507
  dispose() {
404
508
  vm?.removeListener('workspaceUpdate', clearOnLoad);
509
+ if (sharing && vm.shareBlocksToTarget === sharing) {
510
+ if (hadOwnShare) vm.shareBlocksToTarget = share;
511
+ else delete vm.shareBlocksToTarget;
512
+ }
405
513
  if (owners.get(workspace) !== selection) return;
406
514
  owners.delete(workspace);
407
515
  selection.dispose();
@@ -61,25 +61,28 @@ export interface SharedCopy {
61
61
 
62
62
  const messages = {
63
63
  en: {
64
- copiedToSprite: (name: string) => `Copied the script to “${name}”.`,
65
- copiedToStage: 'Copied the script to the Stage.',
64
+ copiedToSprite: (name: string, count = 1) => count > 1 ?
65
+ `Copied ${count} scripts to “${name}”.` : `Copied the script to “${name}”.`,
66
+ copiedToStage: (count = 1) => count > 1 ? `Copied ${count} scripts to the Stage.` : 'Copied the script to the Stage.',
66
67
  undo: 'Undo', close: 'Close',
67
68
  removedFromSprite: (name: string) => `Removed the copy from “${name}”.`,
68
69
  removedFromStage: 'Removed the copy from the Stage.'
69
70
  },
70
71
  ja: {
71
- copiedToSprite: (name: string) => `スプライト「${name}」にスクリプトをコピーしました。`,
72
- copiedToStage: '背景にスクリプトをコピーしました。',
72
+ copiedToSprite: (name: string, count = 1) => count > 1 ?
73
+ `スプライト「${name}」に${count}個のスクリプトをコピーしました。` : `スプライト「${name}」にスクリプトをコピーしました。`,
74
+ copiedToStage: (count = 1) => count > 1 ? `ステージに${count}個のスクリプトをコピーしました。` : 'ステージにスクリプトをコピーしました。',
73
75
  undo: '元に戻す', close: '閉じる',
74
76
  removedFromSprite: (name: string) => `スプライト「${name}」からコピーを取り消しました。`,
75
- removedFromStage: '背景からコピーを取り消しました。'
77
+ removedFromStage: 'ステージからコピーを取り消しました。'
76
78
  },
77
79
  'ja-hira': {
78
- copiedToSprite: (name: string) => `スプライト「${name}」にスクリプトをコピーしました。`,
79
- copiedToStage: 'はいけいにスクリプトをコピーしました。',
80
+ copiedToSprite: (name: string, count = 1) => count > 1 ?
81
+ `スプライト「${name}」に${count}このスクリプトをコピーしました。` : `スプライト「${name}」にスクリプトをコピーしました。`,
82
+ copiedToStage: (count = 1) => count > 1 ? `ステージに${count}このスクリプトをコピーしました。` : 'ステージにスクリプトをコピーしました。',
80
83
  undo: 'もとにもどす', close: 'とじる',
81
84
  removedFromSprite: (name: string) => `スプライト「${name}」からコピーをとりけしました。`,
82
- removedFromStage: 'はいけいからコピーをとりけしました。'
85
+ removedFromStage: 'ステージからコピーをとりけしました。'
83
86
  }
84
87
  };
85
88
  export type SpriteCopyNoticeMessages = typeof messages.en;
@@ -176,7 +179,8 @@ export function attachSpriteCopyNotice({vm, document, locale, timeout = 10000,
176
179
  notice.setAttribute('aria-live', 'polite');
177
180
  const message = document.createElement('span');
178
181
  message.className = 'tw-sprite-copy-notice-text';
179
- message.textContent = target.isStage ? words.copiedToStage : words.copiedToSprite(name);
182
+ const count = copy.roots.length;
183
+ message.textContent = target.isStage ? words.copiedToStage(count) : words.copiedToSprite(name, count);
180
184
  const undo = document.createElement('button');
181
185
  undo.type = 'button';
182
186
  undo.className = 'tw-sprite-copy-notice-undo';