@kubohiroya/turbowarp-plus-editor-code-views 0.5.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 +7 -2
- package/README.md +7 -2
- 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/palette-visibility-dialog.d.ts +7 -0
- package/dist/palette-visibility-dialog.js +9 -3
- 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/palette-visibility-dialog.tsx +18 -3
- package/src/sprite-copy-notice.ts +282 -0
- package/src/sprite-restore-history.ts +85 -0
- package/src/styles.css +134 -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,85 @@
|
|
|
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
|
+
|
|
10
|
+
export const RESTORE_UPDATE = 'scratch-gui/restore-deletion/RESTORE_UPDATE';
|
|
11
|
+
const sprite = 'Sprite';
|
|
12
|
+
|
|
13
|
+
/** How many deleted sprites can be restored, the one in the GUI's slot included. */
|
|
14
|
+
export const spriteRestoreLimit = 10;
|
|
15
|
+
|
|
16
|
+
type RestoreFunction = () => unknown;
|
|
17
|
+
interface RestoreDeletion {
|
|
18
|
+
restoreFun: RestoreFunction | null;
|
|
19
|
+
deletedItem: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RestoreStore {
|
|
23
|
+
getState(): {scratchGui: {restoreDeletion: RestoreDeletion}};
|
|
24
|
+
dispatch(action: {type: string; state: RestoreDeletion}): unknown;
|
|
25
|
+
subscribe(listener: () => void): () => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ProjectEvents {
|
|
29
|
+
runtime: {
|
|
30
|
+
on(event: 'PROJECT_LOADED', listener: () => void): unknown;
|
|
31
|
+
removeListener(event: 'PROJECT_LOADED', listener: () => void): unknown;
|
|
32
|
+
};
|
|
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 function attachSpriteRestoreHistory({store, vm, limit = spriteRestoreLimit}: {
|
|
41
|
+
store: RestoreStore;
|
|
42
|
+
vm?: ProjectEvents;
|
|
43
|
+
limit?: number;
|
|
44
|
+
}) {
|
|
45
|
+
const read = () => store.getState().scratchGui.restoreDeletion;
|
|
46
|
+
// Oldest first. The last one is the one in the GUI's slot, or the next to put back there.
|
|
47
|
+
const stack: RestoreFunction[] = [];
|
|
48
|
+
let last = read();
|
|
49
|
+
if (last.restoreFun && last.deletedItem === sprite) stack.push(last.restoreFun);
|
|
50
|
+
|
|
51
|
+
const update = () => {
|
|
52
|
+
const current = read();
|
|
53
|
+
if (current.restoreFun === last.restoreFun && current.deletedItem === last.deletedItem) return;
|
|
54
|
+
const previous = last;
|
|
55
|
+
last = current;
|
|
56
|
+
if (current.restoreFun) {
|
|
57
|
+
if (current.deletedItem === sprite && !stack.includes(current.restoreFun)) {
|
|
58
|
+
stack.push(current.restoreFun);
|
|
59
|
+
if (stack.length > Math.max(1, limit)) stack.splice(0, stack.length - Math.max(1, limit));
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// The menu restored what the slot held and cleared it.
|
|
64
|
+
if (previous.restoreFun && previous.restoreFun === stack.at(-1)) stack.pop();
|
|
65
|
+
const next = stack.at(-1);
|
|
66
|
+
if (next) store.dispatch({type: RESTORE_UPDATE, state: {restoreFun: next, deletedItem: sprite}});
|
|
67
|
+
};
|
|
68
|
+
const unsubscribe = store.subscribe(update);
|
|
69
|
+
|
|
70
|
+
const projectLoaded = () => {
|
|
71
|
+
const {restoreFun} = read();
|
|
72
|
+
stack.length = 0;
|
|
73
|
+
if (restoreFun && read().deletedItem === sprite) stack.push(restoreFun);
|
|
74
|
+
};
|
|
75
|
+
vm?.runtime.on('PROJECT_LOADED', projectLoaded);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
count: () => stack.length,
|
|
79
|
+
dispose() {
|
|
80
|
+
unsubscribe();
|
|
81
|
+
vm?.runtime.removeListener('PROJECT_LOADED', projectLoaded);
|
|
82
|
+
stack.length = 0;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
package/src/styles.css
CHANGED
|
@@ -331,6 +331,70 @@
|
|
|
331
331
|
overflow-wrap: anywhere;
|
|
332
332
|
}
|
|
333
333
|
|
|
334
|
+
/* Merging variables and changing their scope. */
|
|
335
|
+
|
|
336
|
+
.twPlusVariableMergeDialog {
|
|
337
|
+
box-sizing: border-box;
|
|
338
|
+
width: min(32rem, calc(100% - 2rem));
|
|
339
|
+
max-width: none;
|
|
340
|
+
padding: 1rem;
|
|
341
|
+
border: 0;
|
|
342
|
+
border-radius: 0.75rem;
|
|
343
|
+
background: Canvas;
|
|
344
|
+
color: CanvasText;
|
|
345
|
+
}
|
|
346
|
+
.twPlusVariableMergeDialog::backdrop {
|
|
347
|
+
background: #17212a80;
|
|
348
|
+
}
|
|
349
|
+
.twPlusVariableMergeDialog h2 {
|
|
350
|
+
margin-top: 0;
|
|
351
|
+
font-size: 1.1rem;
|
|
352
|
+
}
|
|
353
|
+
.twPlusVariableMergeDetails {
|
|
354
|
+
margin: 0.5rem 0;
|
|
355
|
+
padding-left: 1.25rem;
|
|
356
|
+
overflow-wrap: anywhere;
|
|
357
|
+
}
|
|
358
|
+
.twPlusVariableMergeWarning {
|
|
359
|
+
padding: 0.5rem 0.75rem;
|
|
360
|
+
border-left: 0.25rem solid #e6a100;
|
|
361
|
+
background: color-mix(in srgb, #e6a100 12%, Canvas);
|
|
362
|
+
}
|
|
363
|
+
.twPlusVariableMergeNotice {
|
|
364
|
+
position: fixed;
|
|
365
|
+
left: 50%;
|
|
366
|
+
bottom: 1.5rem;
|
|
367
|
+
z-index: 10000;
|
|
368
|
+
display: flex;
|
|
369
|
+
gap: 0.5rem;
|
|
370
|
+
align-items: center;
|
|
371
|
+
max-width: calc(100% - 2rem);
|
|
372
|
+
padding: 0.5rem 0.5rem 0.5rem 1rem;
|
|
373
|
+
border-radius: 0.5rem;
|
|
374
|
+
background: #1f2933;
|
|
375
|
+
color: #fff;
|
|
376
|
+
box-shadow: 0 0.25rem 1rem #0004;
|
|
377
|
+
transform: translateX(-50%);
|
|
378
|
+
}
|
|
379
|
+
.twPlusVariableMergeNotice button {
|
|
380
|
+
font: inherit;
|
|
381
|
+
color: inherit;
|
|
382
|
+
background: transparent;
|
|
383
|
+
border: 1px solid #fff8;
|
|
384
|
+
border-radius: 0.25rem;
|
|
385
|
+
padding: 0.25rem 0.5rem;
|
|
386
|
+
cursor: pointer;
|
|
387
|
+
}
|
|
388
|
+
.twPlusVariableMergeNotice .twPlusVariableMergeClose {
|
|
389
|
+
display: inline-flex;
|
|
390
|
+
border: 0;
|
|
391
|
+
padding: 0.25rem;
|
|
392
|
+
}
|
|
393
|
+
.twPlusVariableMergeNotice .twPlusVariableMergeClose svg {
|
|
394
|
+
width: 1rem;
|
|
395
|
+
height: 1rem;
|
|
396
|
+
}
|
|
397
|
+
|
|
334
398
|
/* The function editor. */
|
|
335
399
|
|
|
336
400
|
.twPlusProcedureDialog {
|
|
@@ -790,3 +854,73 @@
|
|
|
790
854
|
margin: 0.25rem 0;
|
|
791
855
|
background: var(--ui-black-transparent, #80808040);
|
|
792
856
|
}
|
|
857
|
+
|
|
858
|
+
/* The notice after a script dropped on the sprite list was copied into a sprite or the stage. */
|
|
859
|
+
.tw-sprite-copy-notice {
|
|
860
|
+
position: fixed;
|
|
861
|
+
left: 50%;
|
|
862
|
+
bottom: 1.5rem;
|
|
863
|
+
z-index: 1000;
|
|
864
|
+
display: flex;
|
|
865
|
+
align-items: center;
|
|
866
|
+
gap: 0.75rem;
|
|
867
|
+
max-width: calc(100vw - 2rem);
|
|
868
|
+
padding: 0.6rem 0.6rem 0.6rem 1rem;
|
|
869
|
+
border: 1px solid var(--ui-black-transparent, #d7dbe6);
|
|
870
|
+
border-radius: 0.5rem;
|
|
871
|
+
color: var(--text-primary, #243248);
|
|
872
|
+
background: var(--ui-primary, #f5f6f8);
|
|
873
|
+
box-shadow: 0 0.25rem 1rem rgba(0, 0, 0, 0.2);
|
|
874
|
+
font-size: 0.875rem;
|
|
875
|
+
transform: translateX(-50%);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
.tw-sprite-copy-notice-text {
|
|
879
|
+
overflow-wrap: anywhere;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
.tw-sprite-copy-notice-undo {
|
|
883
|
+
flex: none;
|
|
884
|
+
padding: 0.3rem 0.75rem;
|
|
885
|
+
border: 1px solid var(--looks-secondary, #855cd6);
|
|
886
|
+
border-radius: 0.35rem;
|
|
887
|
+
color: var(--looks-secondary, #855cd6);
|
|
888
|
+
background: transparent;
|
|
889
|
+
font: inherit;
|
|
890
|
+
font-weight: bold;
|
|
891
|
+
cursor: pointer;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
.tw-sprite-copy-notice-close {
|
|
895
|
+
flex: none;
|
|
896
|
+
display: inline-flex;
|
|
897
|
+
align-items: center;
|
|
898
|
+
justify-content: center;
|
|
899
|
+
width: 1.75rem;
|
|
900
|
+
height: 1.75rem;
|
|
901
|
+
padding: 0;
|
|
902
|
+
border: none;
|
|
903
|
+
border-radius: 0.35rem;
|
|
904
|
+
color: inherit;
|
|
905
|
+
background: transparent;
|
|
906
|
+
cursor: pointer;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
.tw-sprite-copy-notice-undo:hover,
|
|
910
|
+
.tw-sprite-copy-notice-close:hover {
|
|
911
|
+
background: color-mix(in srgb, currentColor 10%, transparent);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/* The tile of the sprite or stage that received the copy, while the notice is shown. */
|
|
915
|
+
[data-tw-received-copy] {
|
|
916
|
+
outline: 3px solid var(--looks-secondary, #855cd6);
|
|
917
|
+
outline-offset: 1px;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
.tw-palette-visibility-dont-ask {
|
|
921
|
+
display: flex;
|
|
922
|
+
align-items: center;
|
|
923
|
+
gap: 0.35rem;
|
|
924
|
+
margin-left: auto;
|
|
925
|
+
font-size: 0.85rem;
|
|
926
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
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
|
+
interface UndoEvent {
|
|
15
|
+
group?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface UndoWorkspace {
|
|
19
|
+
undoStack_: UndoEvent[];
|
|
20
|
+
redoStack_: UndoEvent[];
|
|
21
|
+
listeners_?: unknown[];
|
|
22
|
+
clearUndo(): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface HistoryTarget {
|
|
26
|
+
id: string;
|
|
27
|
+
isStage?: boolean;
|
|
28
|
+
blocks: {toXML(comments: Record<string, unknown>): string};
|
|
29
|
+
comments: Record<string, {blockId: string | null; toXML(): string}>;
|
|
30
|
+
variables: Record<string, {type?: string; toXML(local?: boolean): string}>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface UndoHistoryVM {
|
|
34
|
+
editingTarget: HistoryTarget | null;
|
|
35
|
+
blockListener: unknown;
|
|
36
|
+
runtime: {
|
|
37
|
+
getTargetById(id: string): HistoryTarget | undefined;
|
|
38
|
+
getTargetForStage(): HistoryTarget | undefined;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface UndoHistoryBlocks {
|
|
43
|
+
Workspace: {prototype: Pick<UndoWorkspace, 'clearUndo'>};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface StoredHistory {
|
|
47
|
+
workspace: UndoWorkspace;
|
|
48
|
+
signature: string;
|
|
49
|
+
undo: UndoEvent[];
|
|
50
|
+
redo: UndoEvent[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What a target's undo events depend on: its blocks and comments, and the variables they name.
|
|
55
|
+
* Broadcast messages are left out, because the VM removes unused ones whenever it reloads the code area.
|
|
56
|
+
*/
|
|
57
|
+
export function historySignature(vm: Pick<UndoHistoryVM, 'runtime'>, target: HistoryTarget) {
|
|
58
|
+
const stage = vm.runtime.getTargetForStage();
|
|
59
|
+
const variables = (owner: HistoryTarget | undefined, local: boolean) => owner ?
|
|
60
|
+
Object.entries(owner.variables).filter(([, variable]) => variable.type !== 'broadcast_msg')
|
|
61
|
+
.map(([id, variable]) => `${id}:${variable.toXML(local)}`).join('') : '';
|
|
62
|
+
const workspaceComments = Object.values(target.comments).filter(comment => comment.blockId === null)
|
|
63
|
+
.map(comment => comment.toXML()).join('');
|
|
64
|
+
return [
|
|
65
|
+
target.blocks.toXML(target.comments),
|
|
66
|
+
workspaceComments,
|
|
67
|
+
variables(stage, false),
|
|
68
|
+
target.isStage ? '' : variables(target, true)
|
|
69
|
+
].join('\n');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Patch `clearUndo()` of the given ScratchBlocks so the code area connected to `vm` keeps one undo
|
|
74
|
+
* history per target. Only the workspace that sends its events to `vm.blockListener` is affected;
|
|
75
|
+
* the palette, previews and code windows keep the original behaviour.
|
|
76
|
+
* @returns Something to dispose, which puts the original `clearUndo()` back.
|
|
77
|
+
*/
|
|
78
|
+
export function installTargetUndoHistory(vm: UndoHistoryVM, ScratchBlocks: UndoHistoryBlocks) {
|
|
79
|
+
const prototype = ScratchBlocks.Workspace.prototype;
|
|
80
|
+
const clearUndo = prototype.clearUndo;
|
|
81
|
+
const histories = new WeakMap<HistoryTarget, StoredHistory>();
|
|
82
|
+
// The target each code area showed when its history was last cleared.
|
|
83
|
+
const shown = new WeakMap<UndoWorkspace, HistoryTarget>();
|
|
84
|
+
const exists = (target: HistoryTarget) => vm.runtime.getTargetById(target.id) === target;
|
|
85
|
+
|
|
86
|
+
const patched = function (this: UndoWorkspace) {
|
|
87
|
+
if (!this.listeners_?.includes(vm.blockListener)) return clearUndo.call(this);
|
|
88
|
+
const previous = shown.get(this);
|
|
89
|
+
const next = vm.editingTarget;
|
|
90
|
+
if (previous && previous !== next && exists(previous) &&
|
|
91
|
+
(this.undoStack_.length || this.redoStack_.length)) {
|
|
92
|
+
histories.set(previous, {
|
|
93
|
+
workspace: this,
|
|
94
|
+
signature: historySignature(vm, previous),
|
|
95
|
+
undo: this.undoStack_.slice(),
|
|
96
|
+
redo: this.redoStack_.slice()
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
clearUndo.call(this);
|
|
100
|
+
if (!next) {
|
|
101
|
+
shown.delete(this);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
shown.set(this, next);
|
|
105
|
+
// Reloading the same target means its code changed under the code area: start over, as before.
|
|
106
|
+
if (next === previous) return;
|
|
107
|
+
const stored = histories.get(next);
|
|
108
|
+
histories.delete(next);
|
|
109
|
+
if (stored?.workspace === this && stored.signature === historySignature(vm, next)) {
|
|
110
|
+
this.undoStack_.push(...stored.undo);
|
|
111
|
+
this.redoStack_.push(...stored.redo);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
prototype.clearUndo = patched;
|
|
115
|
+
return {
|
|
116
|
+
dispose() {
|
|
117
|
+
if (prototype.clearUndo === patched) prototype.clearUndo = clearUndo;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Keep the code area's undo history per target once ScratchBlocks has loaded. This takes the context
|
|
124
|
+
* the GUI hands to the host's `attachToWorkspace`.
|
|
125
|
+
* @returns Something to dispose.
|
|
126
|
+
*/
|
|
127
|
+
export function attachTargetUndoHistory({vm, getBlockly, onError = error => console.error(error)}: {
|
|
128
|
+
vm: UndoHistoryVM;
|
|
129
|
+
getBlockly(): Promise<UndoHistoryBlocks>;
|
|
130
|
+
onError?: (error: unknown) => void;
|
|
131
|
+
}) {
|
|
132
|
+
let disposed = false;
|
|
133
|
+
let installed: {dispose(): void} | undefined;
|
|
134
|
+
const ready = Promise.resolve().then(getBlockly).then(ScratchBlocks => {
|
|
135
|
+
if (!disposed) installed = installTargetUndoHistory(vm, ScratchBlocks);
|
|
136
|
+
}, onError);
|
|
137
|
+
return {
|
|
138
|
+
ready,
|
|
139
|
+
dispose() {
|
|
140
|
+
disposed = true;
|
|
141
|
+
installed?.dispose();
|
|
142
|
+
installed = undefined;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|