@kubohiroya/turbowarp-plus-editor-code-views 0.7.0 → 0.7.2

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.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Record a workspace as one that shows a target's code.
3
+ * @param {object} workspace The workspace.
4
+ * @returns {() => void} A function that removes the record.
5
+ */
6
+ export declare const registerCodeWorkspace: (workspace: object) => () => void;
7
+ /**
8
+ * Whether a workspace shows a target's code: it was recorded here, or it sends its events to
9
+ * `vm.blockListener` directly.
10
+ * @param {object} vm The VM.
11
+ * @param {object} workspace The workspace.
12
+ * @returns {boolean} Whether it is a code workspace.
13
+ */
14
+ export declare const isCodeWorkspace: (vm: {
15
+ blockListener?: unknown;
16
+ }, workspace: {
17
+ listeners_?: unknown[];
18
+ } | null | undefined) => boolean;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The workspaces that show a target's code and send their edits to the VM.
3
+ *
4
+ * Without the code area's own attachment the GUI adds `vm.blockListener` to the workspace, so a
5
+ * module can recognise it by that listener. With `attachCodeWorkspace` the GUI adds the
6
+ * attachment's own listener instead, which wraps `vm.blockListener`; the attachment records its
7
+ * workspace here so other modules still recognise it.
8
+ */
9
+ const codeWorkspaces = new WeakSet();
10
+ /**
11
+ * Record a workspace as one that shows a target's code.
12
+ * @param {object} workspace The workspace.
13
+ * @returns {() => void} A function that removes the record.
14
+ */
15
+ export const registerCodeWorkspace = (workspace) => {
16
+ codeWorkspaces.add(workspace);
17
+ return () => {
18
+ codeWorkspaces.delete(workspace);
19
+ };
20
+ };
21
+ /**
22
+ * Whether a workspace shows a target's code: it was recorded here, or it sends its events to
23
+ * `vm.blockListener` directly.
24
+ * @param {object} vm The VM.
25
+ * @param {object} workspace The workspace.
26
+ * @returns {boolean} Whether it is a code workspace.
27
+ */
28
+ export const isCodeWorkspace = (vm, workspace) => !!workspace && (codeWorkspaces.has(workspace) ||
29
+ (!!vm.blockListener && Array.isArray(workspace.listeners_) && workspace.listeners_.includes(vm.blockListener)));
@@ -90,7 +90,7 @@ export function attachTargetUndoHistory({ vm, getBlockly, onError = error => con
90
90
  const ready = Promise.resolve().then(getBlockly).then(ScratchBlocks => {
91
91
  if (!disposed)
92
92
  installed = installTargetUndoHistory(vm, ScratchBlocks);
93
- }, onError);
93
+ }).catch(onError);
94
94
  return {
95
95
  ready,
96
96
  dispose() {
@@ -1,3 +1,4 @@
1
+ import { isCodeWorkspace as isRecordedCodeWorkspace } from "./code-workspace-registry.js";
1
2
  import { applyPlan, planMerge, planScopeChange, refreshEditor, undoPlan } from "./variable-merge-plan.js";
2
3
  export { applyPlan, findVariable, planMerge, planScopeChange, refreshEditor, undoPlan } from "./variable-merge-plan.js";
3
4
  const MESSAGES = {
@@ -326,8 +327,10 @@ export function installVariableMerge(vm, ScratchBlocks, { document: fallbackDocu
326
327
  const main = workspace?.targetWorkspace ?? workspace;
327
328
  if (!main)
328
329
  return false;
330
+ if (isRecordedCodeWorkspace(vm, main))
331
+ return true;
329
332
  if (vm.blockListener && Array.isArray(main.listeners_))
330
- return main.listeners_.includes(vm.blockListener);
333
+ return false;
331
334
  return main === ScratchBlocks.getMainWorkspace?.();
332
335
  };
333
336
  const documentOf = (workspace) => {
@@ -474,7 +477,7 @@ export function attachVariableMerge({ vm, getBlockly, document, locale, onError
474
477
  if (disposed || !ScratchBlocks)
475
478
  return;
476
479
  remove = installVariableMerge(vm, ScratchBlocks, { document, locale, onError });
477
- });
480
+ }).catch(onError ?? (error => console.error(error)));
478
481
  return {
479
482
  ready,
480
483
  dispose() {
package/dist/workspace.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { attachBlockDrag } from "./block-drag.js";
2
+ import { registerCodeWorkspace } from "./code-workspace-registry.js";
2
3
  import { workspaceXML } from "./history.js";
3
4
  import { isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition } from "./procedure-edits.js";
4
5
  import { decodeEdit, encodeEdit, isCommentEdit } from "./protocol.js";
@@ -375,6 +376,7 @@ function paintPreviewFolds(blocks, preview, target, label) {
375
376
  }
376
377
  export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, options, onError, presentationElements = null }) {
377
378
  const session = getCodeSession(vm);
379
+ const unregisterCodeWorkspace = registerCodeWorkspace(workspace);
378
380
  const homeDocument = workspace.getInjectionDiv().ownerDocument;
379
381
  const histories = new WeakMap();
380
382
  const signature = target => target.blocks.toXML(target.comments);
@@ -645,6 +647,7 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
645
647
  },
646
648
  dispose() {
647
649
  blockDrag.dispose();
650
+ unregisterCodeWorkspace();
648
651
  shown?.disconnect();
649
652
  visibility.dispose();
650
653
  if (pendingFrame !== null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
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",
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The workspaces that show a target's code and send their edits to the VM.
3
+ *
4
+ * Without the code area's own attachment the GUI adds `vm.blockListener` to the workspace, so a
5
+ * module can recognise it by that listener. With `attachCodeWorkspace` the GUI adds the
6
+ * attachment's own listener instead, which wraps `vm.blockListener`; the attachment records its
7
+ * workspace here so other modules still recognise it.
8
+ */
9
+ const codeWorkspaces = new WeakSet<object>();
10
+
11
+ /**
12
+ * Record a workspace as one that shows a target's code.
13
+ * @param {object} workspace The workspace.
14
+ * @returns {() => void} A function that removes the record.
15
+ */
16
+ export const registerCodeWorkspace = (workspace: object) => {
17
+ codeWorkspaces.add(workspace);
18
+ return () => {
19
+ codeWorkspaces.delete(workspace);
20
+ };
21
+ };
22
+
23
+ /**
24
+ * Whether a workspace shows a target's code: it was recorded here, or it sends its events to
25
+ * `vm.blockListener` directly.
26
+ * @param {object} vm The VM.
27
+ * @param {object} workspace The workspace.
28
+ * @returns {boolean} Whether it is a code workspace.
29
+ */
30
+ export const isCodeWorkspace = (vm: {blockListener?: unknown}, workspace: {listeners_?: unknown[]} | null | undefined) =>
31
+ !!workspace && (codeWorkspaces.has(workspace) ||
32
+ (!!vm.blockListener && Array.isArray(workspace.listeners_) && workspace.listeners_.includes(vm.blockListener)));
@@ -133,7 +133,7 @@ export function attachTargetUndoHistory({vm, getBlockly, onError = error => cons
133
133
  let installed: {dispose(): void} | undefined;
134
134
  const ready = Promise.resolve().then(getBlockly).then(ScratchBlocks => {
135
135
  if (!disposed) installed = installTargetUndoHistory(vm, ScratchBlocks);
136
- }, onError);
136
+ }).catch(onError);
137
137
  return {
138
138
  ready,
139
139
  dispose() {
@@ -1,3 +1,4 @@
1
+ import {isCodeWorkspace as isRecordedCodeWorkspace} from './code-workspace-registry.ts';
1
2
  import {applyPlan, planMerge, planScopeChange, refreshEditor, undoPlan,
2
3
  type Plan, type Refusal, type VmLike} from './variable-merge-plan.ts';
3
4
 
@@ -380,7 +381,8 @@ export function installVariableMerge (vm: VmLike & {blockListener?: unknown}, Sc
380
381
  const isCodeWorkspace = (workspace: WorkspaceLike) => {
381
382
  const main = workspace?.targetWorkspace ?? workspace;
382
383
  if (!main) return false;
383
- if (vm.blockListener && Array.isArray(main.listeners_)) return main.listeners_.includes(vm.blockListener);
384
+ if (isRecordedCodeWorkspace(vm, main)) return true;
385
+ if (vm.blockListener && Array.isArray(main.listeners_)) return false;
384
386
  return main === ScratchBlocks.getMainWorkspace?.();
385
387
  };
386
388
  const documentOf = (workspace: WorkspaceLike) => {
@@ -527,7 +529,7 @@ export function attachVariableMerge ({vm, getBlockly, document, locale, onError}
527
529
  const ready = Promise.resolve().then(() => getBlockly()).then(ScratchBlocks => {
528
530
  if (disposed || !ScratchBlocks) return;
529
531
  remove = installVariableMerge(vm, ScratchBlocks, {document, locale, onError});
530
- });
532
+ }).catch(onError ?? (error => console.error(error)));
531
533
  return {
532
534
  ready,
533
535
  dispose () {
package/src/workspace.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {attachBlockDrag} from './block-drag.ts';
2
+ import {registerCodeWorkspace} from './code-workspace-registry.ts';
2
3
  import {workspaceXML, type CodeTarget} from './history.ts';
3
4
  import {isFoldedDefinition, removeObsoleteProcedureInputs, setFoldedDefinition} from './procedure-edits.ts';
4
5
  import {decodeEdit, encodeEdit, isCommentEdit} from './protocol.ts';
@@ -416,6 +417,7 @@ function paintPreviewFolds(blocks, preview, target, label: string) {
416
417
 
417
418
  export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, options, onError, presentationElements = null as PresentationElements | null}) {
418
419
  const session = getCodeSession(vm);
420
+ const unregisterCodeWorkspace = registerCodeWorkspace(workspace);
419
421
  const homeDocument: Document = workspace.getInjectionDiv().ownerDocument;
420
422
  const histories = new WeakMap();
421
423
  const signature = target => target.blocks.toXML(target.comments);
@@ -678,6 +680,7 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
678
680
  },
679
681
  dispose() {
680
682
  blockDrag.dispose();
683
+ unregisterCodeWorkspace();
681
684
  shown?.disconnect();
682
685
  visibility.dispose();
683
686
  if (pendingFrame !== null) cancelAnimationFrame(pendingFrame);