@kubohiroya/turbowarp-plus-editor-code-views 0.21.2 → 0.22.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.
Files changed (52) hide show
  1. package/EDITOR-MIGRATION.md +5 -0
  2. package/README.ja.md +16 -1
  3. package/dist/code-pane.d.ts +7 -1
  4. package/dist/code-pane.js +45 -3
  5. package/dist/code-popout.d.ts +10 -0
  6. package/dist/code-popout.js +402 -0
  7. package/dist/context-menu-icons.d.ts +15 -0
  8. package/dist/context-menu-icons.js +204 -0
  9. package/dist/desktop-code.d.ts +11 -0
  10. package/dist/desktop-code.js +47 -0
  11. package/dist/horizontal-block-palette.d.ts +39 -0
  12. package/dist/horizontal-block-palette.js +774 -0
  13. package/dist/palette-delete-preview.d.ts +3 -0
  14. package/dist/palette-delete-preview.js +73 -0
  15. package/dist/script-arrows.js +106 -12
  16. package/dist/script-layout-view.js +122 -6
  17. package/dist/script-layout-workspace.js +7 -2
  18. package/dist/script-layout.d.ts +1 -1
  19. package/dist/script-layout.js +3 -3
  20. package/dist/script-projection.d.ts +8 -0
  21. package/dist/script-projection.js +10 -0
  22. package/dist/script-view-menu.d.ts +1 -0
  23. package/dist/script-view-menu.js +4 -2
  24. package/dist/serial-script-editor.d.ts +4 -0
  25. package/dist/serial-script-editor.js +43 -0
  26. package/dist/workspace-overview.js +67 -25
  27. package/dist/workspace.d.ts +1 -0
  28. package/dist/workspace.js +3 -2
  29. package/package.json +27 -3
  30. package/src/code-pane.ts +41 -3
  31. package/src/code-popout.tsx +399 -0
  32. package/src/context-menu-icons.css +14 -0
  33. package/src/context-menu-icons.ts +215 -0
  34. package/src/desktop-code.ts +45 -0
  35. package/src/horizontal-block-palette.css +111 -0
  36. package/src/horizontal-block-palette.ts +765 -0
  37. package/src/palette-delete-preview.css +21 -0
  38. package/src/palette-delete-preview.ts +79 -0
  39. package/src/pin-off.svg +6 -0
  40. package/src/pin.svg +5 -0
  41. package/src/script-arrows.ts +98 -12
  42. package/src/script-layout-view.ts +114 -6
  43. package/src/script-layout-workspace.ts +7 -2
  44. package/src/script-layout.ts +4 -3
  45. package/src/script-projection.ts +12 -0
  46. package/src/script-view-menu.ts +5 -2
  47. package/src/serial-script-editor.ts +41 -0
  48. package/src/styles.css +80 -5
  49. package/src/trash.svg +3 -0
  50. package/src/workspace-overview.ts +63 -22
  51. package/src/workspace.ts +3 -2
  52. package/tsconfig.json +87 -1
@@ -1,3 +1,4 @@
1
+ import { getScriptProjection } from "./script-projection.js";
1
2
  const svgNamespace = 'http://www.w3.org/2000/svg';
2
3
  /** A cached overview drawn from public block geometry; offscreen culling never changes the project map. */
3
4
  export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight = 0, getCorner, watermark, getWatermark }) {
@@ -34,25 +35,46 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
34
35
  function refreshShapes() {
35
36
  const retained = new Set();
36
37
  const additions = document.createDocumentFragment();
37
- for (const block of workspace.getAllBlocks(false)) {
38
- const root = block.getSvgRoot();
39
- if (!root || !canvas.contains(root) || root.closest('[data-tw-plus-hidden]'))
40
- continue;
41
- const source = root.querySelector(':scope > .blocklyPath');
42
- const data = source?.getAttribute('d');
43
- if (!data)
44
- continue;
45
- retained.add(block.id);
46
- let path = paths.get(block.id);
38
+ const paint = (id, data, fill, transform) => {
39
+ retained.add(id);
40
+ let path = paths.get(id);
47
41
  if (!path) {
48
42
  path = element('path');
49
- paths.set(block.id, path);
43
+ paths.set(id, path);
50
44
  additions.append(path);
51
45
  }
52
- const position = block.getRelativeToSurfaceXY();
53
46
  attribute(path, 'd', data);
54
- attribute(path, 'fill', source.getAttribute('fill') || block.getColour());
55
- attribute(path, 'transform', `translate(${position.x} ${position.y})${block.RTL ? ' scale(-1 1)' : ''}`);
47
+ attribute(path, 'fill', fill);
48
+ attribute(path, 'transform', transform);
49
+ };
50
+ const projection = getScriptProjection(workspace);
51
+ if (projection) {
52
+ const inverse = projection.svg.getScreenCTM()?.inverse();
53
+ if (inverse)
54
+ for (const source of projection.svg.querySelectorAll('.blocklyPath')) {
55
+ if (source.closest('[data-tw-plus-hidden]'))
56
+ continue;
57
+ const id = source.closest('[data-id]')?.getAttribute('data-id');
58
+ const data = source.getAttribute('d');
59
+ const matrix = source.getScreenCTM();
60
+ if (!id || !data || !matrix)
61
+ continue;
62
+ const transform = inverse.multiply(matrix);
63
+ paint(id, data, source.getAttribute('fill') || view.getComputedStyle(source).fill, `matrix(${transform.a} ${transform.b} ${transform.c} ${transform.d} ${transform.e} ${transform.f})`);
64
+ }
65
+ }
66
+ else {
67
+ for (const block of workspace.getAllBlocks(false)) {
68
+ const root = block.getSvgRoot();
69
+ if (!root || !canvas.contains(root) || root.closest('[data-tw-plus-hidden]'))
70
+ continue;
71
+ const source = root.querySelector(':scope > .blocklyPath');
72
+ const data = source?.getAttribute('d');
73
+ if (!data)
74
+ continue;
75
+ const position = block.getRelativeToSurfaceXY();
76
+ paint(block.id, data, source.getAttribute('fill') || block.getColour(), `translate(${position.x} ${position.y})${block.RTL ? ' scale(-1 1)' : ''}`);
77
+ }
56
78
  }
57
79
  for (const [id, path] of paths)
58
80
  if (!retained.has(id)) {
@@ -73,12 +95,17 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
73
95
  return;
74
96
  }
75
97
  const scale = workspace.scale;
76
- const x = metrics.viewLeft / scale, y = metrics.viewTop / scale;
77
- const width = metrics.viewWidth / scale, height = metrics.viewHeight / scale;
98
+ const projection = getScriptProjection(workspace);
99
+ const x = projection ? projection.surface.scrollLeft / scale : metrics.viewLeft / scale;
100
+ const y = projection ? projection.surface.scrollTop / scale : metrics.viewTop / scale;
101
+ const width = (projection ? projection.surface.clientWidth : metrics.viewWidth) / scale;
102
+ const height = (projection ? projection.surface.clientHeight : metrics.viewHeight) / scale;
78
103
  const content = metrics.contentWidth > 0 && metrics.contentHeight > 0;
79
- const area = content ? { x: metrics.contentLeft / scale, y: metrics.contentTop / scale,
80
- width: metrics.contentWidth / scale, height: metrics.contentHeight / scale } : { x, y, width, height };
81
- if (!workspace.scrollbar && content) {
104
+ const box = projection?.svg.viewBox.baseVal;
105
+ const area = box ? { x: box.x, y: box.y, width: Math.max(box.width, width), height: Math.max(box.height, height) } :
106
+ content ? { x: metrics.contentLeft / scale, y: metrics.contentTop / scale,
107
+ width: metrics.contentWidth / scale, height: metrics.contentHeight / scale } : { x, y, width, height };
108
+ if (!projection && !workspace.scrollbar && content) {
82
109
  const right = Math.max(x + width, area.x + area.width), bottom = Math.max(y + height, area.y + area.height);
83
110
  area.x = Math.min(x, area.x);
84
111
  area.y = Math.min(y, area.y);
@@ -102,9 +129,9 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
102
129
  if (needsShapes)
103
130
  refreshShapes();
104
131
  map.toggleAttribute('hidden', false);
105
- const box = `${area.x} ${area.y} ${area.width} ${area.height}`;
106
- attribute(map, 'viewBox', box);
107
- attribute(clip, 'viewBox', box);
132
+ const viewBox = `${area.x} ${area.y} ${area.width} ${area.height}`;
133
+ attribute(map, 'viewBox', viewBox);
134
+ attribute(clip, 'viewBox', viewBox);
108
135
  for (const [name, value] of Object.entries(area))
109
136
  attribute(clip, name, value);
110
137
  for (const [name, value] of Object.entries({ x, y, width, height }))
@@ -144,9 +171,21 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
144
171
  const resize = new view.ResizeObserver(schedule);
145
172
  resize.observe(container);
146
173
  const events = new view.AbortController();
174
+ let projectedSVG;
175
+ container.addEventListener('scriptprojectionchange', () => {
176
+ const next = getScriptProjection(workspace)?.svg;
177
+ needsShapes ||= next !== projectedSVG;
178
+ projectedSVG = next;
179
+ schedule();
180
+ }, { signal: events.signal });
147
181
  view.addEventListener('resize', schedule, { signal: events.signal });
148
182
  view.addEventListener('scroll', schedule, { signal: events.signal, passive: true });
149
183
  const moveView = (left, top) => {
184
+ const projection = getScriptProjection(workspace);
185
+ if (projection) {
186
+ projection.surface.scrollTo(left * workspace.scale, top * workspace.scale);
187
+ return;
188
+ }
150
189
  const metrics = workspace.getMetrics();
151
190
  if (!metrics || !workspace.scrollbar)
152
191
  return;
@@ -156,14 +195,16 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
156
195
  let pointer;
157
196
  const point = (event, inverse) => new view.DOMPoint(event.clientX, event.clientY).matrixTransform(inverse);
158
197
  map.addEventListener('pointerdown', event => {
159
- if (!event.isPrimary || event.button !== 0 || pointer || !workspace.scrollbar)
198
+ const projection = getScriptProjection(workspace);
199
+ if (!event.isPrimary || event.button !== 0 || pointer || (!projection && !workspace.scrollbar))
160
200
  return;
161
201
  const matrix = map.getScreenCTM(), metrics = workspace.getMetrics();
162
202
  if (!matrix || !metrics)
163
203
  return;
164
204
  const inverse = matrix.inverse();
165
205
  pointer = { id: event.pointerId, x: event.clientX, y: event.clientY, start: point(event, inverse), inverse,
166
- left: metrics.viewLeft / workspace.scale, top: metrics.viewTop / workspace.scale,
206
+ left: projection ? projection.surface.scrollLeft / workspace.scale : metrics.viewLeft / workspace.scale,
207
+ top: projection ? projection.surface.scrollTop / workspace.scale : metrics.viewTop / workspace.scale,
167
208
  viewport: event.target === viewport, moved: false };
168
209
  map.setPointerCapture(event.pointerId);
169
210
  }, { signal: events.signal });
@@ -190,8 +231,9 @@ export function attachWorkspaceOverview({ workspace, ScratchBlocks, zoomHeight =
190
231
  move(event);
191
232
  if (!pointer.moved) {
192
233
  const current = point(event, pointer.inverse), metrics = workspace.getMetrics();
234
+ const projection = getScriptProjection(workspace);
193
235
  if (metrics)
194
- moveView(current.x - metrics.viewWidth / (2 * workspace.scale), current.y - metrics.viewHeight / (2 * workspace.scale));
236
+ moveView(current.x - (projection ? projection.surface.clientWidth : metrics.viewWidth) / (2 * workspace.scale), current.y - (projection ? projection.surface.clientHeight : metrics.viewHeight) / (2 * workspace.scale));
195
237
  ScratchBlocks.hideChaff(true);
196
238
  }
197
239
  }
@@ -100,6 +100,7 @@ export declare function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspa
100
100
  focusBlock?: string;
101
101
  }): {
102
102
  workspace: any;
103
+ flush(): void;
103
104
  showBlock(id: string): void;
104
105
  dispose(): void;
105
106
  };
package/dist/workspace.js CHANGED
@@ -163,7 +163,7 @@ function attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError }
163
163
  const labels = () => japanese() ? { fold: '折りたたみ', unfold: '折りたたみを解除', folded: '折りたたみ中' } :
164
164
  { fold: 'Fold', unfold: 'Unfold', folded: 'Folded' };
165
165
  const vmBlock = (id) => vm.editingTarget?.blocks._blocks[id];
166
- const isFolded = (block) => isHatBlock(block) && isFoldedDefinition(vmBlock(block.id));
166
+ const isFolded = (block) => isFoldedDefinition(vmBlock(block.id));
167
167
  let focus;
168
168
  // The GUI can attach the code area again; what an earlier attachment left on the workspace is not its own.
169
169
  const own = (name) => Object.prototype.hasOwnProperty.call(workspace, name) && !workspace[name][focusOverride] ?
@@ -364,7 +364,7 @@ function attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError }
364
364
  /** Paint the folded scripts of a read-only workspace, such as the preview of a code view. */
365
365
  function paintPreviewFolds(blocks, preview, target, label) {
366
366
  for (const top of preview.getTopBlocks(false)) {
367
- paintFold(top, isHatBlock(top) && isFoldedDefinition(target.blocks._blocks[top.id]), label);
367
+ paintFold(top, isFoldedDefinition(target.blocks._blocks[top.id]), label);
368
368
  }
369
369
  }
370
370
  export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, options, onError, presentationElements = null }) {
@@ -988,6 +988,7 @@ function attachProcedureEditor({ vm, blocks, workspace, options, onError, homeDo
988
988
  modal.appendChild(popup);
989
989
  return {
990
990
  workspace: editor,
991
+ flush() { blocks.Events.fireNow_(); },
991
992
  showBlock(id) {
992
993
  pendingBlock = id;
993
994
  reveal();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.21.2",
3
+ "version": "0.22.0",
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",
@@ -103,7 +103,30 @@
103
103
  "default": "./dist/dockview-unsaved.js"
104
104
  },
105
105
  "./styles.css": "./src/styles.css",
106
- "./package.json": "./package.json"
106
+ "./package.json": "./package.json",
107
+ "./horizontal-block-palette": {
108
+ "types": "./dist/horizontal-block-palette.d.ts",
109
+ "default": "./dist/horizontal-block-palette.js"
110
+ },
111
+ "./horizontal-block-palette.css": "./src/horizontal-block-palette.css",
112
+ "./context-menu-icons": {
113
+ "types": "./dist/context-menu-icons.d.ts",
114
+ "default": "./dist/context-menu-icons.js"
115
+ },
116
+ "./context-menu-icons.css": "./src/context-menu-icons.css",
117
+ "./palette-delete-preview": {
118
+ "types": "./dist/palette-delete-preview.d.ts",
119
+ "default": "./dist/palette-delete-preview.js"
120
+ },
121
+ "./palette-delete-preview.css": "./src/palette-delete-preview.css",
122
+ "./code-popout": {
123
+ "types": "./dist/code-popout.d.ts",
124
+ "default": "./dist/code-popout.js"
125
+ },
126
+ "./desktop-code": {
127
+ "types": "./dist/desktop-code.d.ts",
128
+ "default": "./dist/desktop-code.js"
129
+ }
107
130
  },
108
131
  "files": [
109
132
  "dist",
@@ -111,7 +134,8 @@
111
134
  "tsconfig.json",
112
135
  "README.md",
113
136
  "README.ja.md",
114
- "LICENSE"
137
+ "LICENSE",
138
+ "EDITOR-MIGRATION.md"
115
139
  ],
116
140
  "scripts": {
117
141
  "build": "tsc -p tsconfig.json",
package/src/code-pane.ts CHANGED
@@ -427,26 +427,64 @@ export function readScriptRelationGraph(targets: readonly RelationTarget[], Scra
427
427
  /**
428
428
  * Add “Show this function” at the top of the menu of every block that calls a function, in the code area and
429
429
  * in the script editors. `find` names the function a block calls, if any; `open` shows it.
430
+ * With `scriptEditor`, free-layout blocks first offer to edit their own containing script.
430
431
  */
431
- export function attachFunctionCallMenu(ScratchBlocks, {label, find, open}: {
432
+ export function attachFunctionCallMenu(ScratchBlocks, {label, find, open, scriptEditor}: {
432
433
  label(): string;
433
434
  find(blockId: string): string | undefined;
434
435
  open(functionId: string): void;
436
+ scriptEditor?: {vm: any; locale: string;
437
+ open(scriptId: string, blockId: string, section: 'functions' | 'calls'): void};
435
438
  }) {
436
439
  const prototype = ScratchBlocks.BlockSvg.prototype;
437
440
  const showContextMenu = prototype.showContextMenu_;
438
441
  const patched = function (event) {
439
442
  const functionId = this.isInFlyout ? undefined : find(this.id);
440
- if (!functionId) return showContextMenu.call(this, event);
443
+ const target = scriptEditor?.vm.editingTarget;
444
+ const layout = scriptEditor && getDockedScriptLayout(scriptEditor.vm);
445
+ const script = scriptEditor && !this.isInFlyout && !this.workspace.options.readOnly &&
446
+ this.workspace === ScratchBlocks.getMainWorkspace?.() && layout?.view.view === 'free' &&
447
+ layout.canArrange() && !getCodeSession(scriptEditor.vm).bridge?.focusedScript ? this.getRootBlock() : undefined;
448
+ const scriptId = script && target?.blocks._blocks[script.id] ? script.id : undefined;
449
+ if (!functionId && !scriptId) return showContextMenu.call(this, event);
450
+ const blockId = this.id;
451
+ const japanese = scriptEditor && /^ja(?:-|$)/i.test(scriptEditor.locale);
452
+ const scriptOptions = scriptId ? [
453
+ {section: 'functions' as const, text: japanese ?
454
+ 'このスクリプトを個別の画面で編集する' : 'Edit this script in a separate view'},
455
+ {section: 'calls' as const, text: japanese ?
456
+ 'このスクリプトの呼び出し関係を開く' : 'Open relationships for this script'}
457
+ ].map(({section, text}) => ({text, twPlusMenuIcon: section, separator: false, enabled: true, callback() {
458
+ if (scriptEditor.vm.editingTarget === target && target.blocks._blocks[scriptId]) {
459
+ scriptEditor.open(scriptId, blockId, section);
460
+ }
461
+ }})) : [];
441
462
  const owned = Object.prototype.hasOwnProperty.call(this, 'customContextMenu');
442
463
  const custom = this.customContextMenu;
443
464
  this.customContextMenu = function (options) {
444
465
  custom?.call(this, options);
445
- options.unshift({text: label(), enabled: true, callback: () => open(functionId)});
466
+ if (functionId) options.unshift({text: label(), enabled: true, callback: () => open(functionId)});
467
+ options.unshift(...scriptOptions);
468
+ };
469
+ const {ContextMenu} = ScratchBlocks;
470
+ const populate = scriptOptions.length ? ContextMenu.populate_ : undefined;
471
+ if (scriptOptions.length) ContextMenu.populate_ = function (options, rtl) {
472
+ // Addons reorder ContextMenu.show's options after customContextMenu. Prioritize the final list,
473
+ // in place so their separator indices and the host's icon decoration still use the same order.
474
+ for (const priority of [...scriptOptions].reverse()) {
475
+ const index = options.findIndex(option => option.callback === priority.callback);
476
+ if (index > 0) options.unshift(...options.splice(index, 1));
477
+ }
478
+ for (const option of scriptOptions) option.separator = false;
479
+ if (options.length > 2) options[2].separator = true;
480
+ const menu = populate.call(this, options, rtl);
481
+ menu.getChildAt(2)?.getElement().classList.add('tw-script-editor-menu-divider');
482
+ return menu;
446
483
  };
447
484
  try {
448
485
  return showContextMenu.call(this, event);
449
486
  } finally {
487
+ if (scriptOptions.length) ContextMenu.populate_ = populate;
450
488
  if (owned) this.customContextMenu = custom;
451
489
  else delete this.customContextMenu;
452
490
  }