@kubohiroya/turbowarp-plus-editor-code-views 0.6.0 → 0.7.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/README.ja.md +6 -1
- package/README.md +6 -1
- package/dist/block-argument-text.d.ts +12 -1
- package/dist/block-argument-text.js +54 -9
- package/dist/clean-up-comments.d.ts +70 -0
- package/dist/clean-up-comments.js +226 -0
- package/dist/sprite-copy-notice.d.ts +106 -0
- package/dist/sprite-copy-notice.js +240 -0
- package/dist/sprite-restore-history.d.ts +48 -0
- package/dist/sprite-restore-history.js +62 -0
- package/dist/target-undo-history.d.ts +77 -0
- package/dist/target-undo-history.js +102 -0
- package/dist/variable-merge-plan.d.ts +199 -0
- package/dist/variable-merge-plan.js +392 -0
- package/dist/variable-merge.d.ts +236 -0
- package/dist/variable-merge.js +486 -0
- package/package.json +21 -1
- package/src/block-argument-text.ts +55 -9
- package/src/clean-up-comments.ts +305 -0
- package/src/sprite-copy-notice.ts +282 -0
- package/src/sprite-restore-history.ts +85 -0
- package/src/styles.css +126 -0
- package/src/target-undo-history.ts +145 -0
- package/src/variable-merge-plan.ts +498 -0
- package/src/variable-merge.ts +539 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes a script copied by dropping it on a sprite or the stage in the sprite list visible, and
|
|
3
|
+
* lets the user take that copy back in one action.
|
|
4
|
+
*
|
|
5
|
+
* The GUI copies a dragged script when the pointer is released over a tile of the sprite list: its
|
|
6
|
+
* target pane listens for the VM's BLOCK_DRAG_END and calls
|
|
7
|
+
* `vm.shareBlocksToTarget(blocks, targetId, editingTargetId)`. That is the only caller that passes
|
|
8
|
+
* the source target, so a call with a source different from the receiving target is a drop on the
|
|
9
|
+
* sprite list. Pasting and backpack drops pass no source and are left alone.
|
|
10
|
+
*/
|
|
11
|
+
const messages = {
|
|
12
|
+
en: {
|
|
13
|
+
copiedToSprite: (name) => `Copied the script to “${name}”.`,
|
|
14
|
+
copiedToStage: 'Copied the script to the Stage.',
|
|
15
|
+
undo: 'Undo', close: 'Close',
|
|
16
|
+
removedFromSprite: (name) => `Removed the copy from “${name}”.`,
|
|
17
|
+
removedFromStage: 'Removed the copy from the Stage.'
|
|
18
|
+
},
|
|
19
|
+
ja: {
|
|
20
|
+
copiedToSprite: (name) => `スプライト「${name}」にスクリプトをコピーしました。`,
|
|
21
|
+
copiedToStage: '背景にスクリプトをコピーしました。',
|
|
22
|
+
undo: '元に戻す', close: '閉じる',
|
|
23
|
+
removedFromSprite: (name) => `スプライト「${name}」からコピーを取り消しました。`,
|
|
24
|
+
removedFromStage: '背景からコピーを取り消しました。'
|
|
25
|
+
},
|
|
26
|
+
'ja-hira': {
|
|
27
|
+
copiedToSprite: (name) => `スプライト「${name}」にスクリプトをコピーしました。`,
|
|
28
|
+
copiedToStage: 'はいけいにスクリプトをコピーしました。',
|
|
29
|
+
undo: 'もとにもどす', close: 'とじる',
|
|
30
|
+
removedFromSprite: (name) => `スプライト「${name}」からコピーをとりけしました。`,
|
|
31
|
+
removedFromStage: 'はいけいからコピーをとりけしました。'
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/** The notice's wording for a locale: ja-hira, ja, and English for anything else. */
|
|
35
|
+
export function spriteCopyNoticeMessages(locale) {
|
|
36
|
+
if (/^ja-hira$/i.test(locale ?? ''))
|
|
37
|
+
return messages['ja-hira'];
|
|
38
|
+
return /^ja(?:-|$)/i.test(locale ?? '') ? messages.ja : messages.en;
|
|
39
|
+
}
|
|
40
|
+
/** Marks the tile of the target that received a copy, for as long as the notice is shown. */
|
|
41
|
+
export const RECEIVED_COPY_ATTRIBUTE = 'data-tw-received-copy';
|
|
42
|
+
function defaultTargetElement(target, document) {
|
|
43
|
+
if (target.isStage)
|
|
44
|
+
return document.querySelector('[class*="stage-selector_stage-selector_"]');
|
|
45
|
+
const name = target.getName();
|
|
46
|
+
for (const label of document.querySelectorAll('[class*="sprite-selector-item_sprite-name_"]')) {
|
|
47
|
+
if (label.textContent === name)
|
|
48
|
+
return label.closest('[class*="sprite-selector-item_sprite-selector-item_"]');
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Remove what a drop copied into a target: its scripts, and the variables it created that no
|
|
54
|
+
* script refers to any more. Scripts the user already deleted are skipped.
|
|
55
|
+
* @returns Whether anything was removed.
|
|
56
|
+
*/
|
|
57
|
+
export function removeSharedCopy(vm, copy) {
|
|
58
|
+
const { target } = copy;
|
|
59
|
+
if (vm.runtime.getTargetById(target.id) !== target)
|
|
60
|
+
return false;
|
|
61
|
+
let removed = false;
|
|
62
|
+
for (const id of copy.roots) {
|
|
63
|
+
if (!target.blocks._blocks[id])
|
|
64
|
+
continue;
|
|
65
|
+
target.blocks.deleteBlock(id);
|
|
66
|
+
removed = true;
|
|
67
|
+
}
|
|
68
|
+
const referenced = new Set();
|
|
69
|
+
for (const each of vm.runtime.targets) {
|
|
70
|
+
if (each.isOriginal === false)
|
|
71
|
+
continue;
|
|
72
|
+
for (const id of Object.keys(each.blocks.getAllVariableAndListReferences()))
|
|
73
|
+
referenced.add(id);
|
|
74
|
+
}
|
|
75
|
+
let stageChanged = false;
|
|
76
|
+
for (const { owner, id } of copy.variables) {
|
|
77
|
+
if (referenced.has(id) || !Object.prototype.hasOwnProperty.call(owner.variables, id))
|
|
78
|
+
continue;
|
|
79
|
+
owner.deleteVariable(id);
|
|
80
|
+
removed = true;
|
|
81
|
+
if (owner.isStage)
|
|
82
|
+
stageChanged = true;
|
|
83
|
+
}
|
|
84
|
+
if (removed) {
|
|
85
|
+
// Only the code area that shows the target (or the stage's variables) has to be redrawn.
|
|
86
|
+
if (vm.editingTarget === target || stageChanged)
|
|
87
|
+
vm.emitWorkspaceUpdate?.();
|
|
88
|
+
vm.emitTargetsUpdate?.();
|
|
89
|
+
}
|
|
90
|
+
return removed;
|
|
91
|
+
}
|
|
92
|
+
const svgNS = 'http://www.w3.org/2000/svg';
|
|
93
|
+
function closeIcon(document) {
|
|
94
|
+
const svg = document.createElementNS(svgNS, 'svg');
|
|
95
|
+
svg.setAttribute('viewBox', '0 0 16 16');
|
|
96
|
+
svg.setAttribute('width', '12');
|
|
97
|
+
svg.setAttribute('height', '12');
|
|
98
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
99
|
+
const path = document.createElementNS(svgNS, 'path');
|
|
100
|
+
path.setAttribute('d', 'M3 3 L13 13 M13 3 L3 13');
|
|
101
|
+
path.setAttribute('stroke', 'currentColor');
|
|
102
|
+
path.setAttribute('stroke-width', '2');
|
|
103
|
+
path.setAttribute('stroke-linecap', 'round');
|
|
104
|
+
svg.append(path);
|
|
105
|
+
return svg;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Show a notice naming the sprite (or the stage) that a drop on the sprite list copied a script
|
|
109
|
+
* into, mark its tile, and offer to undo the copy.
|
|
110
|
+
* @returns `dispose`, which removes the notice and stops watching the VM.
|
|
111
|
+
*/
|
|
112
|
+
export function attachSpriteCopyNotice({ vm, document, locale, timeout = 10000, findTargetElement = defaultTargetElement }) {
|
|
113
|
+
const hadOwn = Object.prototype.hasOwnProperty.call(vm, 'shareBlocksToTarget');
|
|
114
|
+
const original = vm.shareBlocksToTarget;
|
|
115
|
+
let disposed = false;
|
|
116
|
+
let hide;
|
|
117
|
+
const text = () => spriteCopyNoticeMessages(typeof locale === 'function' ? locale() : locale);
|
|
118
|
+
function show(copy) {
|
|
119
|
+
hide?.();
|
|
120
|
+
const words = text();
|
|
121
|
+
const { target } = copy;
|
|
122
|
+
const name = target.isStage ? '' : target.getName();
|
|
123
|
+
const notice = document.createElement('div');
|
|
124
|
+
notice.className = 'tw-sprite-copy-notice';
|
|
125
|
+
notice.setAttribute('role', 'status');
|
|
126
|
+
notice.setAttribute('aria-live', 'polite');
|
|
127
|
+
const message = document.createElement('span');
|
|
128
|
+
message.className = 'tw-sprite-copy-notice-text';
|
|
129
|
+
message.textContent = target.isStage ? words.copiedToStage : words.copiedToSprite(name);
|
|
130
|
+
const undo = document.createElement('button');
|
|
131
|
+
undo.type = 'button';
|
|
132
|
+
undo.className = 'tw-sprite-copy-notice-undo';
|
|
133
|
+
undo.textContent = words.undo;
|
|
134
|
+
const close = document.createElement('button');
|
|
135
|
+
close.type = 'button';
|
|
136
|
+
close.className = 'tw-sprite-copy-notice-close';
|
|
137
|
+
close.title = words.close;
|
|
138
|
+
close.setAttribute('aria-label', words.close);
|
|
139
|
+
close.append(closeIcon(document));
|
|
140
|
+
notice.append(message, undo, close);
|
|
141
|
+
document.body.append(notice);
|
|
142
|
+
let tile;
|
|
143
|
+
try {
|
|
144
|
+
tile = findTargetElement(target, document);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
tile = null;
|
|
148
|
+
}
|
|
149
|
+
tile?.setAttribute(RECEIVED_COPY_ATTRIBUTE, '');
|
|
150
|
+
let timer;
|
|
151
|
+
let wait = timeout;
|
|
152
|
+
const view = document.defaultView;
|
|
153
|
+
const schedule = () => {
|
|
154
|
+
if (timer !== undefined)
|
|
155
|
+
view.clearTimeout(timer);
|
|
156
|
+
timer = view.setTimeout(() => hide?.(), wait);
|
|
157
|
+
};
|
|
158
|
+
// Keep the notice while the pointer or the keyboard focus is on it. The listeners go with
|
|
159
|
+
// the notice, which is removed when it is dismissed.
|
|
160
|
+
notice.addEventListener('mouseenter', () => {
|
|
161
|
+
if (timer !== undefined)
|
|
162
|
+
view.clearTimeout(timer);
|
|
163
|
+
timer = undefined;
|
|
164
|
+
});
|
|
165
|
+
notice.addEventListener('mouseleave', schedule);
|
|
166
|
+
notice.addEventListener('focusin', () => {
|
|
167
|
+
if (timer !== undefined)
|
|
168
|
+
view.clearTimeout(timer);
|
|
169
|
+
timer = undefined;
|
|
170
|
+
});
|
|
171
|
+
notice.addEventListener('focusout', event => {
|
|
172
|
+
if (!notice.contains(event.relatedTarget))
|
|
173
|
+
schedule();
|
|
174
|
+
});
|
|
175
|
+
close.addEventListener('click', () => hide?.());
|
|
176
|
+
undo.addEventListener('click', () => {
|
|
177
|
+
if (!removeSharedCopy(vm, copy)) {
|
|
178
|
+
hide?.();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
tile?.removeAttribute(RECEIVED_COPY_ATTRIBUTE);
|
|
182
|
+
undo.remove();
|
|
183
|
+
message.textContent = target.isStage ? words.removedFromStage : words.removedFromSprite(name);
|
|
184
|
+
close.focus();
|
|
185
|
+
wait = Math.min(timeout, 3000);
|
|
186
|
+
schedule();
|
|
187
|
+
});
|
|
188
|
+
const dismiss = () => {
|
|
189
|
+
if (timer !== undefined)
|
|
190
|
+
view.clearTimeout(timer);
|
|
191
|
+
tile?.removeAttribute(RECEIVED_COPY_ATTRIBUTE);
|
|
192
|
+
notice.remove();
|
|
193
|
+
if (hide === dismiss)
|
|
194
|
+
hide = undefined;
|
|
195
|
+
};
|
|
196
|
+
hide = dismiss;
|
|
197
|
+
schedule();
|
|
198
|
+
}
|
|
199
|
+
const patched = function (blocks, targetId, fromTargetId) {
|
|
200
|
+
const target = !disposed && fromTargetId && fromTargetId !== targetId ?
|
|
201
|
+
vm.runtime.getTargetById(targetId) : undefined;
|
|
202
|
+
if (!target)
|
|
203
|
+
return original.call(this, blocks, targetId, fromTargetId);
|
|
204
|
+
const stage = vm.runtime.getTargetForStage();
|
|
205
|
+
const blocksBefore = new Set(Object.keys(target.blocks._blocks));
|
|
206
|
+
// Resolving variable conflicts runs synchronously, before the returned promise.
|
|
207
|
+
const owners = [...new Set([target, stage].filter(Boolean))];
|
|
208
|
+
const variablesBefore = new Map(owners.map(owner => [owner, new Set(Object.keys(owner.variables))]));
|
|
209
|
+
const result = original.call(this, blocks, targetId, fromTargetId);
|
|
210
|
+
Promise.resolve(result).then(() => {
|
|
211
|
+
if (disposed || vm.runtime.getTargetById(target.id) !== target)
|
|
212
|
+
return;
|
|
213
|
+
const created = Object.keys(target.blocks._blocks).filter(id => !blocksBefore.has(id));
|
|
214
|
+
const createdSet = new Set(created);
|
|
215
|
+
const roots = created.filter(id => {
|
|
216
|
+
const block = target.blocks._blocks[id];
|
|
217
|
+
return block.topLevel || !block.parent || !createdSet.has(block.parent);
|
|
218
|
+
});
|
|
219
|
+
if (!roots.length)
|
|
220
|
+
return;
|
|
221
|
+
const variables = owners.flatMap(owner => Object.keys(owner.variables)
|
|
222
|
+
.filter(id => !variablesBefore.get(owner).has(id)).map(id => ({ owner, id })));
|
|
223
|
+
show({ target, roots, variables });
|
|
224
|
+
}, () => { });
|
|
225
|
+
return result;
|
|
226
|
+
};
|
|
227
|
+
vm.shareBlocksToTarget = patched;
|
|
228
|
+
return {
|
|
229
|
+
dispose() {
|
|
230
|
+
disposed = true;
|
|
231
|
+
hide?.();
|
|
232
|
+
if (vm.shareBlocksToTarget !== patched)
|
|
233
|
+
return;
|
|
234
|
+
if (hadOwn)
|
|
235
|
+
vm.shareBlocksToTarget = original;
|
|
236
|
+
else
|
|
237
|
+
delete vm.shareBlocksToTarget;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Let "Restore Sprite" go back through more than the last deleted sprite.
|
|
3
|
+
*
|
|
4
|
+
* The GUI keeps one restore function in `scratchGui.restoreDeletion` and replaces it on every deletion,
|
|
5
|
+
* so only the last deleted sprite can be restored. This keeps the restore functions of earlier sprite
|
|
6
|
+
* deletions, and when the menu has used one and cleared the slot, it puts the previous one back.
|
|
7
|
+
* Nothing is added to the project: each restore function holds the deleted sprite itself.
|
|
8
|
+
*/
|
|
9
|
+
export declare const RESTORE_UPDATE = "scratch-gui/restore-deletion/RESTORE_UPDATE";
|
|
10
|
+
/** How many deleted sprites can be restored, the one in the GUI's slot included. */
|
|
11
|
+
export declare const spriteRestoreLimit = 10;
|
|
12
|
+
type RestoreFunction = () => unknown;
|
|
13
|
+
interface RestoreDeletion {
|
|
14
|
+
restoreFun: RestoreFunction | null;
|
|
15
|
+
deletedItem: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RestoreStore {
|
|
18
|
+
getState(): {
|
|
19
|
+
scratchGui: {
|
|
20
|
+
restoreDeletion: RestoreDeletion;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
dispatch(action: {
|
|
24
|
+
type: string;
|
|
25
|
+
state: RestoreDeletion;
|
|
26
|
+
}): unknown;
|
|
27
|
+
subscribe(listener: () => void): () => void;
|
|
28
|
+
}
|
|
29
|
+
interface ProjectEvents {
|
|
30
|
+
runtime: {
|
|
31
|
+
on(event: 'PROJECT_LOADED', listener: () => void): unknown;
|
|
32
|
+
removeListener(event: 'PROJECT_LOADED', listener: () => void): unknown;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Keep up to `limit` sprite restore functions for the GUI's store. Pass `vm` to forget the earlier ones
|
|
37
|
+
* when another project is loaded; the one in the GUI's slot stays, as it did before.
|
|
38
|
+
* @returns Something to dispose, plus `count()` for the number of sprites that can be restored.
|
|
39
|
+
*/
|
|
40
|
+
export declare function attachSpriteRestoreHistory({ store, vm, limit }: {
|
|
41
|
+
store: RestoreStore;
|
|
42
|
+
vm?: ProjectEvents;
|
|
43
|
+
limit?: number;
|
|
44
|
+
}): {
|
|
45
|
+
count: () => number;
|
|
46
|
+
dispose(): void;
|
|
47
|
+
};
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Let "Restore Sprite" go back through more than the last deleted sprite.
|
|
3
|
+
*
|
|
4
|
+
* The GUI keeps one restore function in `scratchGui.restoreDeletion` and replaces it on every deletion,
|
|
5
|
+
* so only the last deleted sprite can be restored. This keeps the restore functions of earlier sprite
|
|
6
|
+
* deletions, and when the menu has used one and cleared the slot, it puts the previous one back.
|
|
7
|
+
* Nothing is added to the project: each restore function holds the deleted sprite itself.
|
|
8
|
+
*/
|
|
9
|
+
export const RESTORE_UPDATE = 'scratch-gui/restore-deletion/RESTORE_UPDATE';
|
|
10
|
+
const sprite = 'Sprite';
|
|
11
|
+
/** How many deleted sprites can be restored, the one in the GUI's slot included. */
|
|
12
|
+
export const spriteRestoreLimit = 10;
|
|
13
|
+
/**
|
|
14
|
+
* Keep up to `limit` sprite restore functions for the GUI's store. Pass `vm` to forget the earlier ones
|
|
15
|
+
* when another project is loaded; the one in the GUI's slot stays, as it did before.
|
|
16
|
+
* @returns Something to dispose, plus `count()` for the number of sprites that can be restored.
|
|
17
|
+
*/
|
|
18
|
+
export function attachSpriteRestoreHistory({ store, vm, limit = spriteRestoreLimit }) {
|
|
19
|
+
const read = () => store.getState().scratchGui.restoreDeletion;
|
|
20
|
+
// Oldest first. The last one is the one in the GUI's slot, or the next to put back there.
|
|
21
|
+
const stack = [];
|
|
22
|
+
let last = read();
|
|
23
|
+
if (last.restoreFun && last.deletedItem === sprite)
|
|
24
|
+
stack.push(last.restoreFun);
|
|
25
|
+
const update = () => {
|
|
26
|
+
const current = read();
|
|
27
|
+
if (current.restoreFun === last.restoreFun && current.deletedItem === last.deletedItem)
|
|
28
|
+
return;
|
|
29
|
+
const previous = last;
|
|
30
|
+
last = current;
|
|
31
|
+
if (current.restoreFun) {
|
|
32
|
+
if (current.deletedItem === sprite && !stack.includes(current.restoreFun)) {
|
|
33
|
+
stack.push(current.restoreFun);
|
|
34
|
+
if (stack.length > Math.max(1, limit))
|
|
35
|
+
stack.splice(0, stack.length - Math.max(1, limit));
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// The menu restored what the slot held and cleared it.
|
|
40
|
+
if (previous.restoreFun && previous.restoreFun === stack.at(-1))
|
|
41
|
+
stack.pop();
|
|
42
|
+
const next = stack.at(-1);
|
|
43
|
+
if (next)
|
|
44
|
+
store.dispatch({ type: RESTORE_UPDATE, state: { restoreFun: next, deletedItem: sprite } });
|
|
45
|
+
};
|
|
46
|
+
const unsubscribe = store.subscribe(update);
|
|
47
|
+
const projectLoaded = () => {
|
|
48
|
+
const { restoreFun } = read();
|
|
49
|
+
stack.length = 0;
|
|
50
|
+
if (restoreFun && read().deletedItem === sprite)
|
|
51
|
+
stack.push(restoreFun);
|
|
52
|
+
};
|
|
53
|
+
vm?.runtime.on('PROJECT_LOADED', projectLoaded);
|
|
54
|
+
return {
|
|
55
|
+
count: () => stack.length,
|
|
56
|
+
dispose() {
|
|
57
|
+
unsubscribe();
|
|
58
|
+
vm?.runtime.removeListener('PROJECT_LOADED', projectLoaded);
|
|
59
|
+
stack.length = 0;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the code area's undo history for each sprite and the stage.
|
|
3
|
+
*
|
|
4
|
+
* The GUI reloads the code area for every editing target change and calls `clearUndo()` right after,
|
|
5
|
+
* so switching to another sprite and back loses what could be undone. This keeps the stacks the code
|
|
6
|
+
* area had for the target it showed, and gives them back when that target is shown again.
|
|
7
|
+
*
|
|
8
|
+
* Undo events find blocks by ID in the reloaded code area, so a history is given back only when the
|
|
9
|
+
* target's code is still what it was when the history was put aside. Anything that changed the code
|
|
10
|
+
* meanwhile, such as an edit in a code window, a script dropped onto the sprite or a renamed
|
|
11
|
+
* variable, drops that history, as the GUI always did.
|
|
12
|
+
*/
|
|
13
|
+
interface UndoEvent {
|
|
14
|
+
group?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface UndoWorkspace {
|
|
17
|
+
undoStack_: UndoEvent[];
|
|
18
|
+
redoStack_: UndoEvent[];
|
|
19
|
+
listeners_?: unknown[];
|
|
20
|
+
clearUndo(): void;
|
|
21
|
+
}
|
|
22
|
+
interface HistoryTarget {
|
|
23
|
+
id: string;
|
|
24
|
+
isStage?: boolean;
|
|
25
|
+
blocks: {
|
|
26
|
+
toXML(comments: Record<string, unknown>): string;
|
|
27
|
+
};
|
|
28
|
+
comments: Record<string, {
|
|
29
|
+
blockId: string | null;
|
|
30
|
+
toXML(): string;
|
|
31
|
+
}>;
|
|
32
|
+
variables: Record<string, {
|
|
33
|
+
type?: string;
|
|
34
|
+
toXML(local?: boolean): string;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
export interface UndoHistoryVM {
|
|
38
|
+
editingTarget: HistoryTarget | null;
|
|
39
|
+
blockListener: unknown;
|
|
40
|
+
runtime: {
|
|
41
|
+
getTargetById(id: string): HistoryTarget | undefined;
|
|
42
|
+
getTargetForStage(): HistoryTarget | undefined;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export interface UndoHistoryBlocks {
|
|
46
|
+
Workspace: {
|
|
47
|
+
prototype: Pick<UndoWorkspace, 'clearUndo'>;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* What a target's undo events depend on: its blocks and comments, and the variables they name.
|
|
52
|
+
* Broadcast messages are left out, because the VM removes unused ones whenever it reloads the code area.
|
|
53
|
+
*/
|
|
54
|
+
export declare function historySignature(vm: Pick<UndoHistoryVM, 'runtime'>, target: HistoryTarget): string;
|
|
55
|
+
/**
|
|
56
|
+
* Patch `clearUndo()` of the given ScratchBlocks so the code area connected to `vm` keeps one undo
|
|
57
|
+
* history per target. Only the workspace that sends its events to `vm.blockListener` is affected;
|
|
58
|
+
* the palette, previews and code windows keep the original behaviour.
|
|
59
|
+
* @returns Something to dispose, which puts the original `clearUndo()` back.
|
|
60
|
+
*/
|
|
61
|
+
export declare function installTargetUndoHistory(vm: UndoHistoryVM, ScratchBlocks: UndoHistoryBlocks): {
|
|
62
|
+
dispose(): void;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Keep the code area's undo history per target once ScratchBlocks has loaded. This takes the context
|
|
66
|
+
* the GUI hands to the host's `attachToWorkspace`.
|
|
67
|
+
* @returns Something to dispose.
|
|
68
|
+
*/
|
|
69
|
+
export declare function attachTargetUndoHistory({ vm, getBlockly, onError }: {
|
|
70
|
+
vm: UndoHistoryVM;
|
|
71
|
+
getBlockly(): Promise<UndoHistoryBlocks>;
|
|
72
|
+
onError?: (error: unknown) => void;
|
|
73
|
+
}): {
|
|
74
|
+
ready: Promise<void>;
|
|
75
|
+
dispose(): void;
|
|
76
|
+
};
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the code area's undo history for each sprite and the stage.
|
|
3
|
+
*
|
|
4
|
+
* The GUI reloads the code area for every editing target change and calls `clearUndo()` right after,
|
|
5
|
+
* so switching to another sprite and back loses what could be undone. This keeps the stacks the code
|
|
6
|
+
* area had for the target it showed, and gives them back when that target is shown again.
|
|
7
|
+
*
|
|
8
|
+
* Undo events find blocks by ID in the reloaded code area, so a history is given back only when the
|
|
9
|
+
* target's code is still what it was when the history was put aside. Anything that changed the code
|
|
10
|
+
* meanwhile, such as an edit in a code window, a script dropped onto the sprite or a renamed
|
|
11
|
+
* variable, drops that history, as the GUI always did.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* What a target's undo events depend on: its blocks and comments, and the variables they name.
|
|
15
|
+
* Broadcast messages are left out, because the VM removes unused ones whenever it reloads the code area.
|
|
16
|
+
*/
|
|
17
|
+
export function historySignature(vm, target) {
|
|
18
|
+
const stage = vm.runtime.getTargetForStage();
|
|
19
|
+
const variables = (owner, local) => owner ?
|
|
20
|
+
Object.entries(owner.variables).filter(([, variable]) => variable.type !== 'broadcast_msg')
|
|
21
|
+
.map(([id, variable]) => `${id}:${variable.toXML(local)}`).join('') : '';
|
|
22
|
+
const workspaceComments = Object.values(target.comments).filter(comment => comment.blockId === null)
|
|
23
|
+
.map(comment => comment.toXML()).join('');
|
|
24
|
+
return [
|
|
25
|
+
target.blocks.toXML(target.comments),
|
|
26
|
+
workspaceComments,
|
|
27
|
+
variables(stage, false),
|
|
28
|
+
target.isStage ? '' : variables(target, true)
|
|
29
|
+
].join('\n');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Patch `clearUndo()` of the given ScratchBlocks so the code area connected to `vm` keeps one undo
|
|
33
|
+
* history per target. Only the workspace that sends its events to `vm.blockListener` is affected;
|
|
34
|
+
* the palette, previews and code windows keep the original behaviour.
|
|
35
|
+
* @returns Something to dispose, which puts the original `clearUndo()` back.
|
|
36
|
+
*/
|
|
37
|
+
export function installTargetUndoHistory(vm, ScratchBlocks) {
|
|
38
|
+
const prototype = ScratchBlocks.Workspace.prototype;
|
|
39
|
+
const clearUndo = prototype.clearUndo;
|
|
40
|
+
const histories = new WeakMap();
|
|
41
|
+
// The target each code area showed when its history was last cleared.
|
|
42
|
+
const shown = new WeakMap();
|
|
43
|
+
const exists = (target) => vm.runtime.getTargetById(target.id) === target;
|
|
44
|
+
const patched = function () {
|
|
45
|
+
if (!this.listeners_?.includes(vm.blockListener))
|
|
46
|
+
return clearUndo.call(this);
|
|
47
|
+
const previous = shown.get(this);
|
|
48
|
+
const next = vm.editingTarget;
|
|
49
|
+
if (previous && previous !== next && exists(previous) &&
|
|
50
|
+
(this.undoStack_.length || this.redoStack_.length)) {
|
|
51
|
+
histories.set(previous, {
|
|
52
|
+
workspace: this,
|
|
53
|
+
signature: historySignature(vm, previous),
|
|
54
|
+
undo: this.undoStack_.slice(),
|
|
55
|
+
redo: this.redoStack_.slice()
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
clearUndo.call(this);
|
|
59
|
+
if (!next) {
|
|
60
|
+
shown.delete(this);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
shown.set(this, next);
|
|
64
|
+
// Reloading the same target means its code changed under the code area: start over, as before.
|
|
65
|
+
if (next === previous)
|
|
66
|
+
return;
|
|
67
|
+
const stored = histories.get(next);
|
|
68
|
+
histories.delete(next);
|
|
69
|
+
if (stored?.workspace === this && stored.signature === historySignature(vm, next)) {
|
|
70
|
+
this.undoStack_.push(...stored.undo);
|
|
71
|
+
this.redoStack_.push(...stored.redo);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
prototype.clearUndo = patched;
|
|
75
|
+
return {
|
|
76
|
+
dispose() {
|
|
77
|
+
if (prototype.clearUndo === patched)
|
|
78
|
+
prototype.clearUndo = clearUndo;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Keep the code area's undo history per target once ScratchBlocks has loaded. This takes the context
|
|
84
|
+
* the GUI hands to the host's `attachToWorkspace`.
|
|
85
|
+
* @returns Something to dispose.
|
|
86
|
+
*/
|
|
87
|
+
export function attachTargetUndoHistory({ vm, getBlockly, onError = error => console.error(error) }) {
|
|
88
|
+
let disposed = false;
|
|
89
|
+
let installed;
|
|
90
|
+
const ready = Promise.resolve().then(getBlockly).then(ScratchBlocks => {
|
|
91
|
+
if (!disposed)
|
|
92
|
+
installed = installTargetUndoHistory(vm, ScratchBlocks);
|
|
93
|
+
}, onError);
|
|
94
|
+
return {
|
|
95
|
+
ready,
|
|
96
|
+
dispose() {
|
|
97
|
+
disposed = true;
|
|
98
|
+
installed?.dispose();
|
|
99
|
+
installed = undefined;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|