@kubohiroya/turbowarp-plus-editor-code-views 0.19.0 → 0.21.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 CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消し履歴を共有するタブと窓のコード、別窓に切り離したブロックパレット、スクリプトの整列とスクリプト表示、関数の編集、拡張が自分で描くブロックの入力欄の編集を持つ。どの関数も VM・Blockly のワークスペース・`ScratchBlocks` をホストから受け取り、GUI のモジュールを import しない。
6
6
 
7
+ ## 選択したスクリプトの表示
8
+
9
+ 関数・ハットなどを選んで開くと、そのスクリプトの描画範囲を24pxの余白付きでパネルへ収める。自動ズームは50〜125%に制限し、ワークスペース固有の制限が厳しければそちらに従う。下限倍率ではスクリプトの上端を余白の下に置き、長いスクリプトも先頭から読めるようにする。通常の編集・手動ズームでは再フィットしない。
10
+
11
+ ホストが独立したワークスペースを表示する場合は、ルートまたは `./sprite-code-windows` の `fitScriptInWorkspace(workspace, scriptId, ScratchBlocks)` を使える。表示領域やブロックがまだない場合は `false` を返すので、レイアウト後に再試行する。
12
+
7
13
  ## 入口
8
14
 
9
15
  | import | 内容 |
@@ -45,15 +51,31 @@ Editor は GUI の React 16 にこれらを描画するので、React 16.14 以
45
51
 
46
52
  コードの背面に、関数呼び出しを紫系の実線、メッセージ送信を橙系の破線で表示する。「送って待つ」には待機の印を添える。通常配置、Z/N 配置、折り畳み、プレビュー、別窓で同じ表現を使う。関数の再帰はスクリプトの外へ回る線で示す。選択・ホバーによる強調は太さと不透明度で行い、関係種別の色・線種は変えない。
47
53
 
54
+ 関数呼び出しとメッセージ送受信は、常に呼び出し元・送信元の右から出て、呼び出し先・受信先の左へ入る。左右の配置が逆でも接続する側を入れ替えない。自己呼び出しもスクリプトの外を回って左側へ戻る。
55
+
56
+ 通常の線と矢印の先端は20%の濃さにし、選択・ホバー・ドラッグに関連する線を強調する。ドラッグ開始時に無関係な線だけが急に薄くなることはない。
57
+
48
58
  関係は作品データから読み、折り畳みによって表示から消えた呼び出しも保持する。展開中は呼び出しブロックから、折り畳み中は見えているハットから結ぶ。同じハットと宛先へ重なる呼び出しは件数を表示する。別スプライトや一スクリプト表示の外にある接続先は、画面端の案内から辿れる。変数などで呼び出し先が変わるものは「実行時に決定」と示し、見えていない既定値を宛先とみなさない。
49
59
 
60
+ 接続元または接続先を同じパネル内に描けない矢印は、線・矢印の先端・待機の印・件数を5%の濃さで表示する。ホバー・選択・ドラッグによる強調中も同じ濃さを維持する。
61
+
50
62
  スクリプトをドラッグしている間は、そこへ入る・そこから出る矢印を太く濃くし、画面にある相手のスクリプト全体を枠で強調する。関係しない矢印は薄くし、画面外の相手は接続先の案内を強調する。関数とメッセージの色・線種は維持する。折り畳み中も同じように働き、ドラッグの終了・矢印の非表示・画面の破棄で一時的な枠を解除する。移動中に Blockly がブロックを別の SVG へ移しても追従する。
51
63
 
52
64
  `attachDockedScriptLayout` と `attachCodeWorkspace` のプレビュー・関数エディタには自動で付く。VM を持たない表示では、`attachScriptView` に `relations: {getTarget, getRelations, getTargetName, navigate}` を渡す。`getRelations` は親の `createCodeSession` が渡す `scriptRelations`、`getTarget` はその対象の `blocks` を使う。`getTargetName` は既存の対象一覧から名前を返し、同じメッセージを受信する別スプライトを案内で識別できるようにする。投影の受信後は `view.relations.update()` を呼ぶ。関係だけの更新は `relations`、接続先への移動は `relation-navigate`/`relation-focus` で既存の編集回収・対象切り替えを経由する。編集中の窓には履歴の更新のために XML を再読み込みさせない。
53
65
 
54
66
  色は共通の CSS 変数 `--tw-script-function-stroke` と `--tw-script-message-stroke` に定義する。明暗テーマでは同じ種別のまま明度を変え、線種でも区別できる。既存の関数呼び出し図とミニマップも関数用の変数を使う。
55
67
 
56
- コード画面に常設の凡例や表示スイッチは置かない。選択・ホバー・ドラッグ中の関係に必要な接続先だけを短く表示する。呼び出し関係の図へ渡す `readScriptRelationGraph(targets, ScratchBlocks)` は、同じ関係データと、接続先スクリプトのハットの文言・対象名・位置を返す。図の向きと関数・メッセージの表示は、呼び出し関係パネルの左端カラム下端で操作する。
68
+ 自由配置パネルの左端カラム下端に、呼び出し関係パネルと同じ「関数」「メッセージ」の表示ボタンを置く。両方とも既定で有効で、独立して切り替えられる。選択はコードペインごとに保持し、作業領域の保存・復元、非アクティブなプレビュー、Z/N表示にも反映する。非表示の種類は接続先の案内とドラッグ中の強調からも外し、両方を非表示にすると矢印の座標計算を止める。ホストはコードペインアダプターの `relations.getVisibility(paneId)`・`setVisibility(paneId, value)`・`subscribe(listener)` を既存のツールバーへ接続する。
69
+
70
+ 矢印の描画層はブロックのキャンバスと分け、線の変更でミニマップの全ブロックを再生成しない。表示領域の寸法は1描画につき1回だけ読み、線の座標を測ってから必要なDOM更新をまとめる。スクロールでは内部の線とブロックの接続点を再利用し、画面端へ伸びる線だけを更新する。無関係なスクリプトの編集でも変化しない線は作り直さない。
71
+
72
+ ミニマップの描画と入力は `attachWorkspaceOverview` が所有し、ホストは workspace・配置・サムネイル取得の口を渡す。画面外のブロックを隠すネイティブの表示変更では形状を再走査せず、スクロール中は表示範囲の枠だけを更新する。ブロック編集時も既存のパスを保ち、必要な形・位置・色だけを書き換える。矢印の輪郭は同じパス形状で共有し、粗い走査と交点の精密化を組み合わせ、スクロール中は測定済みの接続点を使う。
73
+
74
+ 自由配置とコードのプレビューは2%まで縮小できる。通常の拡大・縮小ボタンやホイールを使い、リセットでは従来の初期倍率へ戻る。最小倍率はコード画面の接続中だけ変更し、Blockly の他の用途の既定値は変えない。
75
+
76
+ 選択・ホバー・ドラッグ中の関係に必要な接続先だけを短く表示する。呼び出し関係の図へ渡す `readScriptRelationGraph(targets, ScratchBlocks)` は、同じ関係データと、接続先スクリプトのハットの文言・対象名・位置を返す。図の向きは呼び出し関係パネルの左端カラム下端で操作する。
77
+
78
+ 切り戻す場合は、code-views と workbench の固定依存を導入前の版へ戻して再ビルドする。追加した `relationVisibility` は作業領域の任意設定であり、作品のコード・座標・編集履歴は変更しない。
57
79
 
58
80
  既定は表示する。作品の保存形式は変えない。完全に切り戻す場合は導入前のパッケージ固定と結線へ戻す。関連: [Editor #683](https://github.com/kubohiroya/turbowarp-plus-editor/issues/683)。
59
81
 
package/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  The code views of TurboWarp Plus Editor: code in tabs and windows that share one project and one undo history, the block palette detached into its own window, script layout and script views, the function editor, and editing of block fields that extensions draw themselves. Every function takes the VM, the Blockly workspace and `ScratchBlocks` from the host; the package imports no GUI module.
6
6
 
7
+ ## Opening a selected script
8
+
9
+ Opening a function or hat fits its rendered script into the pane with 24px of padding. Automatic zoom stays between 50% and 125%, respecting stricter workspace limits. At the lower limit, the script starts below the top padding so a long script remains readable from its beginning. Ordinary edits and manual zoom do not refit it.
10
+
11
+ Hosts with a separate workspace can call `fitScriptInWorkspace(workspace, scriptId, ScratchBlocks)` from the root or `./sprite-code-windows` export. It returns `false` if the viewport or block is not ready, so the host can retry after layout.
12
+
7
13
  ## Entry points
8
14
 
9
15
  | Import | Contents |
@@ -2,6 +2,7 @@ import { getCodeSession } from './workspace.ts';
2
2
  import type { AttachPreviewMinimap, AttachWorkspaceNavigation } from './workspace.ts';
3
3
  import { type ScriptView } from './script-layout-view.ts';
4
4
  import { type RelationTarget } from './script-relations.ts';
5
+ import type { ScriptRelationVisibility } from './script-arrows.ts';
5
6
  type Pane = {
6
7
  id: string;
7
8
  targetId: string;
@@ -15,10 +16,17 @@ export type SavedCodePane = {
15
16
  x: number;
16
17
  y: number;
17
18
  scriptView?: ScriptView;
19
+ relationVisibility?: ScriptRelationVisibility;
18
20
  };
19
21
  /** Read a saved code area back, or nothing if it does not have the shape `serialize` writes. */
20
22
  export declare function readSavedCodePane(value: Record<string, unknown>): SavedCodePane | undefined;
21
23
  export declare function createCodePaneAdapter(vm: any, locale: string, attachMinimap?: AttachPreviewMinimap): {
24
+ /** Independent arrow filters belong to a code pane, including when its workspace is parked. */
25
+ relations: {
26
+ getVisibility: (paneId: string) => ScriptRelationVisibility;
27
+ setVisibility(paneId: string, value: ScriptRelationVisibility): void;
28
+ subscribe(listener: () => void): () => boolean;
29
+ };
22
30
  /**
23
31
  * Make a code view show one script, a hat or a function, or every script again without `scriptId`.
24
32
  * The code area shows it while it is in that view, and the view's preview shows it otherwise.
package/dist/code-pane.js CHANGED
@@ -1,26 +1,32 @@
1
1
  import { getCodeSession } from "./workspace.js";
2
- import { getDockedScriptLayout } from "./script-layout-view.js";
2
+ import { getDockedScriptLayout, setDockedRelationVisibility } from "./script-layout-view.js";
3
3
  import { readScriptRelations } from "./script-relations.js";
4
4
  const scriptViews = ['free', 'z', 'n'];
5
5
  const finite = (value, limit) => typeof value === 'number' && Number.isFinite(value) && Math.abs(value) <= limit;
6
6
  /** Read a saved code area back, or nothing if it does not have the shape `serialize` writes. */
7
7
  export function readSavedCodePane(value) {
8
- const { scale, x, y, scriptView } = value;
8
+ const { scale, x, y, scriptView, relationVisibility } = value;
9
9
  if (!finite(scale, 100) || scale <= 0 || !finite(x, 1e7) || !finite(y, 1e7))
10
10
  return;
11
- return { scale, x, y, ...(scriptViews.includes(scriptView) && { scriptView: scriptView }) };
11
+ const visibility = relationVisibility;
12
+ return { scale, x, y, ...(scriptViews.includes(scriptView) && { scriptView: scriptView }),
13
+ ...(visibility && typeof visibility.function === 'boolean' && typeof visibility.message === 'boolean' &&
14
+ { relationVisibility: { function: visibility.function, message: visibility.message } }) };
12
15
  }
13
16
  export function createCodePaneAdapter(vm, locale, attachMinimap) {
14
17
  const session = getCodeSession(vm);
15
18
  const label = /^ja(?:-|$)/i.test(locale) ? 'このペインでコードを編集' : 'Edit code in this pane';
16
19
  const views = new Map();
20
+ const visibilities = new Map();
21
+ const visibilityListeners = new Set();
22
+ const getVisibility = (paneId) => ({ ...visibilities.get(paneId) ?? { function: true, message: true } });
17
23
  // Views that show one script, a hat or a function, by pane id; `onLost` runs when that script is deleted.
18
24
  const focused = new Map();
19
25
  // The code pane the code area is in, which shows its script there.
20
26
  let current;
21
- const applyFocus = (paneId) => {
27
+ const applyFocus = (paneId, refit = false) => {
22
28
  const focus = paneId === undefined ? undefined : focused.get(paneId);
23
- session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()));
29
+ session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()), refit);
24
30
  };
25
31
  let pending;
26
32
  // The GUI can attach the code area again, with a new bridge; the view it is in still shows its script.
@@ -62,6 +68,25 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
62
68
  };
63
69
  }
64
70
  return {
71
+ /** Independent arrow filters belong to a code pane, including when its workspace is parked. */
72
+ relations: {
73
+ getVisibility,
74
+ setVisibility(paneId, value) {
75
+ const before = getVisibility(paneId);
76
+ if (before.function === value.function && before.message === value.message)
77
+ return;
78
+ visibilities.set(paneId, { ...value });
79
+ if (current === paneId)
80
+ setDockedRelationVisibility(vm, value);
81
+ for (const listener of visibilityListeners)
82
+ listener();
83
+ session.changed();
84
+ },
85
+ subscribe(listener) {
86
+ visibilityListeners.add(listener);
87
+ return () => visibilityListeners.delete(listener);
88
+ }
89
+ },
65
90
  /**
66
91
  * Make a code view show one script, a hat or a function, or every script again without `scriptId`.
67
92
  * The code area shows it while it is in that view, and the view's preview shows it otherwise.
@@ -72,7 +97,7 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
72
97
  else
73
98
  focused.delete(paneId);
74
99
  if (current === paneId)
75
- applyFocus(paneId);
100
+ applyFocus(paneId, true);
76
101
  // Previews of other views redraw with their script.
77
102
  session.changed();
78
103
  },
@@ -81,8 +106,10 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
81
106
  forgetPane(paneId) {
82
107
  focused.delete(paneId);
83
108
  views.delete(paneId);
109
+ visibilities.delete(paneId);
84
110
  if (current === paneId) {
85
111
  current = undefined;
112
+ setDockedRelationVisibility(vm, { function: true, message: true });
86
113
  applyFocus(undefined);
87
114
  }
88
115
  },
@@ -113,7 +140,9 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
113
140
  return;
114
141
  const scriptView = presentation ? presentation.scriptView : views.get(pane.id) ??
115
142
  getDockedScriptLayout(vm)?.view.view;
116
- return { scale: shown.scale, x: shown.x, y: shown.y, ...(scriptView && { scriptView }) };
143
+ const relationVisibility = visibilities.get(pane.id) ?? shown.relationVisibility;
144
+ return { scale: shown.scale, x: shown.x, y: shown.y, ...(scriptView && { scriptView }),
145
+ ...(relationVisibility && { relationVisibility: { ...relationVisibility } }) };
117
146
  },
118
147
  /** A presentation from a saved code area. The preview draws it without the chrome of the code area. */
119
148
  deserialize(pane, value) {
@@ -124,7 +153,14 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
124
153
  return;
125
154
  if (saved.scriptView)
126
155
  views.set(pane.id, saved.scriptView);
127
- return { scale: saved.scale, x: saved.x, y: saved.y, visible: true, ...(saved.scriptView && { scriptView: saved.scriptView }) };
156
+ if (saved.relationVisibility)
157
+ visibilities.set(pane.id, { ...saved.relationVisibility });
158
+ else
159
+ visibilities.delete(pane.id);
160
+ for (const listener of visibilityListeners)
161
+ listener();
162
+ return { scale: saved.scale, x: saved.x, y: saved.y, visible: true, ...(saved.scriptView && { scriptView: saved.scriptView }),
163
+ ...(saved.relationVisibility && { relationVisibility: { ...saved.relationVisibility } }) };
128
164
  },
129
165
  restore(pane, presentation) {
130
166
  if (pending)
@@ -133,6 +169,12 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
133
169
  if (pane.section !== 0)
134
170
  return;
135
171
  current = pane.id;
172
+ if (!visibilities.has(pane.id) && presentation?.relationVisibility) {
173
+ visibilities.set(pane.id, { ...presentation.relationVisibility });
174
+ for (const listener of visibilityListeners)
175
+ listener();
176
+ }
177
+ setDockedRelationVisibility(vm, getVisibility(pane.id));
136
178
  applyFocus(pane.id);
137
179
  getDockedScriptLayout(vm)?.view.setView(views.get(pane.id) ?? 'free');
138
180
  if (!presentation)
@@ -140,6 +182,9 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
140
182
  const frame = requestAnimationFrame(() => {
141
183
  pending = undefined;
142
184
  session.bridge?.restorePane(pane.targetId, presentation);
185
+ // A picked script owns its initial viewport, including after a saved pane is restored.
186
+ if (focused.has(pane.id))
187
+ applyFocus(pane.id, true);
143
188
  });
144
189
  pending = { frame, pane, presentation };
145
190
  },
@@ -187,8 +232,10 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
187
232
  shownScript = focused.get(pane.id)?.scriptId;
188
233
  preview = bridge?.preview(blocks, chrome, { target, presentation, attachMinimap, watermark, locale, dropElement: surface,
189
234
  scriptView: views.get(pane.id), onScriptViewChange: view => views.set(pane.id, view),
235
+ relationVisibility: getVisibility(pane.id),
190
236
  scriptId: focused.get(pane.id)?.scriptId });
191
237
  }
238
+ preview?.setRelationVisibility(getVisibility(pane.id));
192
239
  preview?.update();
193
240
  }
194
241
  const unsubscribe = session.subscribe(update);
@@ -0,0 +1,3 @@
1
+ export declare const minimumCodeScale = 0.02;
2
+ /** The code area and its previews share a lower zoom limit without changing Blockly's other workspaces. */
3
+ export declare function attachCodeZoomRange(workspace: object): () => void;
@@ -0,0 +1,30 @@
1
+ export const minimumCodeScale = 0.02;
2
+ const ranges = new WeakMap();
3
+ /** The code area and its previews share a lower zoom limit without changing Blockly's other workspaces. */
4
+ export function attachCodeZoomRange(workspace) {
5
+ const options = workspace.options?.zoomOptions;
6
+ if (!options)
7
+ return () => { };
8
+ let range = ranges.get(workspace);
9
+ if (!range) {
10
+ range = { users: 0, previous: options.minScale, minimum: Math.min(options.minScale || 1, minimumCodeScale) };
11
+ ranges.set(workspace, range);
12
+ options.minScale = range.minimum;
13
+ }
14
+ range.users++;
15
+ let disposed = false;
16
+ return () => {
17
+ if (disposed)
18
+ return;
19
+ disposed = true;
20
+ if (--range.users)
21
+ return;
22
+ if (options.minScale === range.minimum) {
23
+ if (range.previous === undefined)
24
+ delete options.minScale;
25
+ else
26
+ options.minScale = range.previous;
27
+ }
28
+ ranges.delete(workspace);
29
+ };
30
+ }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export { attachDockedCodeExtensions } from './code-extensions.ts';
2
2
  export type { NativeFieldEditSession } from './native-extension-field.ts';
3
3
  export type { FieldActionPanel, ProgrammaticExtensionField } from './programmatic-extension-field.ts';
4
4
  export { encodeEdit } from './protocol.ts';
5
+ export { fitScriptInWorkspace } from './script-fit.ts';
5
6
  export { readScriptRelations } from './script-relations.ts';
6
7
  export type { ScriptEndpoint, ScriptRelation } from './script-relations.ts';
7
8
  export declare function refreshExternalBlockViews(vm: any, targets: any): void;
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ import { decodeEdit, isCodeEdit, isCommentEdit } from "./protocol.js";
9
9
  import { readScriptRelations } from "./script-relations.js";
10
10
  export { attachDockedCodeExtensions } from "./code-extensions.js";
11
11
  export { encodeEdit } from "./protocol.js";
12
+ export { fitScriptInWorkspace } from "./script-fit.js";
12
13
  export { readScriptRelations } from "./script-relations.js";
13
14
  // A drop must stay within one target's block and comment history.
14
15
  const supportsSharedHistory = script => !script.variables.length && !script.extensionIDs?.length &&
@@ -1,4 +1,8 @@
1
1
  import { type RelationTarget, type ScriptEndpoint, type ScriptRelation } from './script-relations.ts';
2
+ export type ScriptRelationVisibility = {
3
+ function: boolean;
4
+ message: boolean;
5
+ };
2
6
  export interface ScriptRelationContext {
3
7
  vm?: {
4
8
  runtime?: {
@@ -16,10 +20,11 @@ export interface ScriptRelationContext {
16
20
  getLocale?(): string;
17
21
  getTargetName?(id: string): string | undefined;
18
22
  navigate?(endpoint: ScriptEndpoint): void;
23
+ initialVisibility?: ScriptRelationVisibility;
19
24
  }
20
25
  export declare function registerScriptRelationNavigation(vm: object, navigate: (endpoint: ScriptEndpoint) => void): () => void;
21
26
  /** One renderer and semantic stylesheet serve live code, folded scripts, previews and serial layouts. */
22
- export declare function attachScriptArrows({ workspace, vm, getTarget, getRelations, getLocale, getTargetName, navigate }: ScriptRelationContext & {
27
+ export declare function attachScriptArrows({ workspace, vm, getTarget, getRelations, getLocale, getTargetName, navigate, initialVisibility }: ScriptRelationContext & {
23
28
  workspace: {
24
29
  getInjectionDiv(): HTMLElement;
25
30
  getCanvas(): SVGElement;
@@ -41,6 +46,11 @@ export declare function attachScriptArrows({ workspace, vm, getTarget, getRelati
41
46
  }): {
42
47
  update: () => void;
43
48
  readonly enabled: boolean;
49
+ readonly visibility: {
50
+ function: boolean;
51
+ message: boolean;
52
+ };
53
+ setVisibility(value: ScriptRelationVisibility): void;
44
54
  setEnabled(value: boolean): void;
45
55
  setProjection(svg: SVGSVGElement | null): void;
46
56
  dispose(): void;