@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/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 {
@@ -791,6 +855,68 @@
791
855
  background: var(--ui-black-transparent, #80808040);
792
856
  }
793
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
+
794
920
  .tw-palette-visibility-dont-ask {
795
921
  display: flex;
796
922
  align-items: center;
@@ -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
+ }