@kubohiroya/turbowarp-plus-editor-code-views 0.19.0 → 0.20.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 +6 -0
- package/README.md +6 -0
- package/dist/code-pane.js +6 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/script-fit.d.ts +34 -0
- package/dist/script-fit.js +41 -0
- package/dist/sprite-code-windows.d.ts +1 -0
- package/dist/sprite-code-windows.js +1 -0
- package/dist/workspace.d.ts +1 -1
- package/dist/workspace.js +19 -34
- package/package.json +1 -1
- package/src/code-pane.ts +5 -3
- package/src/index.ts +1 -0
- package/src/script-fit.ts +57 -0
- package/src/sprite-code-windows.tsx +1 -0
- package/src/workspace.ts +16 -35
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 | 内容 |
|
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 |
|
package/dist/code-pane.js
CHANGED
|
@@ -18,9 +18,9 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
|
|
|
18
18
|
const focused = new Map();
|
|
19
19
|
// The code pane the code area is in, which shows its script there.
|
|
20
20
|
let current;
|
|
21
|
-
const applyFocus = (paneId) => {
|
|
21
|
+
const applyFocus = (paneId, refit = false) => {
|
|
22
22
|
const focus = paneId === undefined ? undefined : focused.get(paneId);
|
|
23
|
-
session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()));
|
|
23
|
+
session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()), refit);
|
|
24
24
|
};
|
|
25
25
|
let pending;
|
|
26
26
|
// The GUI can attach the code area again, with a new bridge; the view it is in still shows its script.
|
|
@@ -72,7 +72,7 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
|
|
|
72
72
|
else
|
|
73
73
|
focused.delete(paneId);
|
|
74
74
|
if (current === paneId)
|
|
75
|
-
applyFocus(paneId);
|
|
75
|
+
applyFocus(paneId, true);
|
|
76
76
|
// Previews of other views redraw with their script.
|
|
77
77
|
session.changed();
|
|
78
78
|
},
|
|
@@ -140,6 +140,9 @@ export function createCodePaneAdapter(vm, locale, attachMinimap) {
|
|
|
140
140
|
const frame = requestAnimationFrame(() => {
|
|
141
141
|
pending = undefined;
|
|
142
142
|
session.bridge?.restorePane(pane.targetId, presentation);
|
|
143
|
+
// A picked script owns its initial viewport, including after a saved pane is restored.
|
|
144
|
+
if (focused.has(pane.id))
|
|
145
|
+
applyFocus(pane.id, true);
|
|
143
146
|
});
|
|
144
147
|
pending = { frame, pane, presentation };
|
|
145
148
|
},
|
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 &&
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type ScriptFitWorkspace = {
|
|
2
|
+
RTL?: boolean;
|
|
3
|
+
options?: {
|
|
4
|
+
zoomOptions?: {
|
|
5
|
+
minScale?: number;
|
|
6
|
+
maxScale?: number;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
scale: number;
|
|
10
|
+
getInjectionDiv(): HTMLElement;
|
|
11
|
+
getBlockById(id: string): {
|
|
12
|
+
getSvgRoot(): SVGGraphicsElement | null;
|
|
13
|
+
getRelativeToSurfaceXY(): {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
};
|
|
17
|
+
} | null;
|
|
18
|
+
getMetrics(): {
|
|
19
|
+
viewWidth: number;
|
|
20
|
+
viewHeight: number;
|
|
21
|
+
contentLeft: number;
|
|
22
|
+
contentTop: number;
|
|
23
|
+
} | null;
|
|
24
|
+
resize?(): void;
|
|
25
|
+
setScale(scale: number): void;
|
|
26
|
+
scrollbar?: {
|
|
27
|
+
set(x: number, y: number): void;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/** Fit a visible script once. Hidden or not-yet-rendered workspaces can retry after their layout settles. */
|
|
31
|
+
export declare function fitScriptInWorkspace(workspace: ScriptFitWorkspace, scriptId: string, blocks: {
|
|
32
|
+
svgResize(workspace: ScriptFitWorkspace): void;
|
|
33
|
+
}): boolean;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const padding = 24;
|
|
2
|
+
const positive = (value, fallback) => Number.isFinite(value) && value > 0 ? value : fallback;
|
|
3
|
+
/** Fit a visible script once. Hidden or not-yet-rendered workspaces can retry after their layout settles. */
|
|
4
|
+
export function fitScriptInWorkspace(workspace, scriptId, blocks) {
|
|
5
|
+
const injection = workspace.getInjectionDiv();
|
|
6
|
+
const block = workspace.getBlockById(scriptId);
|
|
7
|
+
if (!block || !injection.clientWidth || !injection.clientHeight || !workspace.scrollbar)
|
|
8
|
+
return false;
|
|
9
|
+
const root = block.getSvgRoot();
|
|
10
|
+
if (!root)
|
|
11
|
+
return false;
|
|
12
|
+
// A split can have its new DOM size while Blockly's cached SVG size still belongs to the old pane.
|
|
13
|
+
blocks.svgResize(workspace);
|
|
14
|
+
workspace.resize?.();
|
|
15
|
+
const bounds = root.getBBox();
|
|
16
|
+
const metrics = workspace.getMetrics();
|
|
17
|
+
if (!metrics?.viewWidth || !metrics.viewHeight || !(bounds.width > 0) || !(bounds.height > 0))
|
|
18
|
+
return false;
|
|
19
|
+
// SVG bounds include nested reporters and the hat's overhang, but exclude folded, hidden bodies.
|
|
20
|
+
const origin = block.getRelativeToSurfaceXY();
|
|
21
|
+
const left = origin.x + bounds.x;
|
|
22
|
+
const top = origin.y + bounds.y;
|
|
23
|
+
const zoom = workspace.options?.zoomOptions;
|
|
24
|
+
const workspaceMin = positive(zoom?.minScale, 0.01);
|
|
25
|
+
const workspaceMax = Math.max(workspaceMin, positive(zoom?.maxScale, Infinity));
|
|
26
|
+
const minScale = Math.min(Math.max(0.5, workspaceMin), workspaceMax);
|
|
27
|
+
const maxScale = Math.max(minScale, Math.min(1.25, workspaceMax));
|
|
28
|
+
const fit = Math.min(Math.max(1, metrics.viewWidth - padding * 2) / bounds.width, Math.max(1, metrics.viewHeight - padding * 2) / bounds.height);
|
|
29
|
+
const scale = Math.max(minScale, Math.min(maxScale, fit));
|
|
30
|
+
workspace.setScale(scale);
|
|
31
|
+
const scaled = workspace.getMetrics();
|
|
32
|
+
if (!scaled)
|
|
33
|
+
return false;
|
|
34
|
+
const width = bounds.width * workspace.scale;
|
|
35
|
+
const height = bounds.height * workspace.scale;
|
|
36
|
+
const x = width > scaled.viewWidth - padding * 2 && workspace.RTL ?
|
|
37
|
+
scaled.viewWidth - padding - width : Math.max(padding, (scaled.viewWidth - width) / 2);
|
|
38
|
+
const y = workspace.scale <= minScale ? padding : Math.max(padding, (scaled.viewHeight - height) / 2);
|
|
39
|
+
workspace.scrollbar.set(left * workspace.scale - scaled.contentLeft - x, top * workspace.scale - scaled.contentTop - y);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { createCodePaneAdapter } from './code-pane.ts';
|
|
3
3
|
export { attachCodeWorkspace } from './workspace.ts';
|
|
4
|
+
export { fitScriptInWorkspace } from './script-fit.ts';
|
|
4
5
|
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane, readScriptRelationGraph } from './code-pane.ts';
|
|
5
6
|
export type { SavedCodePane } from './code-pane.ts';
|
|
6
7
|
export declare function CodePaneCostumeThumbnail({ adapter }: {
|
|
@@ -2,6 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
3
|
import { getCodeSession } from "./workspace.js";
|
|
4
4
|
export { attachCodeWorkspace } from "./workspace.js";
|
|
5
|
+
export { fitScriptInWorkspace } from "./script-fit.js";
|
|
5
6
|
export { attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane, readScriptRelationGraph } from "./code-pane.js";
|
|
6
7
|
export function CodePaneCostumeThumbnail({ adapter }) {
|
|
7
8
|
const image = React.useRef(null);
|
package/dist/workspace.d.ts
CHANGED
|
@@ -79,7 +79,7 @@ export declare function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspa
|
|
|
79
79
|
* Show only one script in the code area, a hat or a function, or every script again without `id`.
|
|
80
80
|
* `onLost` runs when that script is deleted.
|
|
81
81
|
*/
|
|
82
|
-
focusScript(id?: string, onLost?: () => void): void;
|
|
82
|
+
focusScript(id?: string, onLost?: () => void, refit?: boolean): void;
|
|
83
83
|
readonly focusedScript: string;
|
|
84
84
|
createFunction(target: CodeTarget & {
|
|
85
85
|
id: string;
|
package/dist/workspace.js
CHANGED
|
@@ -6,6 +6,7 @@ import { decodeEdit, encodeEdit, isCommentEdit } from "./protocol.js";
|
|
|
6
6
|
import { captureCodePreviewChrome } from "./preview-chrome.js";
|
|
7
7
|
import { attachScriptView, getDockedScriptLayout } from "./script-layout-view.js";
|
|
8
8
|
import { attachScriptArrows, registerScriptRelationNavigation } from "./script-arrows.js";
|
|
9
|
+
import { fitScriptInWorkspace } from "./script-fit.js";
|
|
9
10
|
const sessions = new WeakMap();
|
|
10
11
|
export function getCodeSession(vm) {
|
|
11
12
|
let session = sessions.get(vm);
|
|
@@ -242,28 +243,17 @@ function attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError }
|
|
|
242
243
|
safePaint();
|
|
243
244
|
}
|
|
244
245
|
/**
|
|
245
|
-
*
|
|
246
|
-
*
|
|
246
|
+
* Fit the shown script once the code area has its size: its destination view may only be laid out
|
|
247
|
+
* after the script was picked. Later edits and manual zooms keep their viewport.
|
|
247
248
|
*/
|
|
248
249
|
let revealPending = false;
|
|
249
250
|
function reveal() {
|
|
250
251
|
if (!revealPending)
|
|
251
252
|
return;
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (!block || !injection.clientWidth || !injection.clientHeight)
|
|
255
|
-
return;
|
|
256
|
-
// The scroll area follows the shown scripts, which may have changed since the scrollbars were sized.
|
|
257
|
-
workspace.resize?.();
|
|
258
|
-
const metrics = workspace.getMetrics?.();
|
|
259
|
-
if (!metrics?.viewWidth || !metrics.viewHeight)
|
|
260
|
-
return;
|
|
261
|
-
revealPending = false;
|
|
262
|
-
const { x, y } = block.getRelativeToSurfaceXY();
|
|
263
|
-
const padding = 16;
|
|
264
|
-
workspace.scrollbar?.set(x * workspace.scale - metrics.contentLeft - padding, y * workspace.scale - metrics.contentTop - padding);
|
|
253
|
+
if (focus && fitScriptInWorkspace(workspace, focus.id, blocks))
|
|
254
|
+
revealPending = false;
|
|
265
255
|
}
|
|
266
|
-
function setFocus(next) {
|
|
256
|
+
function setFocus(next, refit = false) {
|
|
267
257
|
const changedScript = next?.id !== focus?.id;
|
|
268
258
|
focus = next && { id: next.id, onLost: next.onLost, extras: changedScript ? new Set() : focus?.extras ?? new Set() };
|
|
269
259
|
if (focus) {
|
|
@@ -288,11 +278,13 @@ function attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError }
|
|
|
288
278
|
safePaint();
|
|
289
279
|
if (workspace.rendered && workspace.isVisible?.() !== false)
|
|
290
280
|
workspace.resize?.();
|
|
291
|
-
if (focus && changedScript) {
|
|
281
|
+
if (focus && (changedScript || refit)) {
|
|
292
282
|
revealPending = true;
|
|
293
|
-
|
|
283
|
+
// The pane can still be moving from its previous size during this activation.
|
|
294
284
|
schedulePaint();
|
|
295
285
|
}
|
|
286
|
+
else if (!focus)
|
|
287
|
+
revealPending = false;
|
|
296
288
|
}
|
|
297
289
|
// Blocks dragged in from the palette, or pulled out of the shown script, stay in view. Only the end of a
|
|
298
290
|
// drag counts: a reload of the workspace also sends a create event for every block, after the load.
|
|
@@ -342,9 +334,9 @@ function attachScriptVisibility({ vm, blocks, workspace, homeDocument, onError }
|
|
|
342
334
|
if (!disposed)
|
|
343
335
|
reveal();
|
|
344
336
|
},
|
|
345
|
-
focus: (id, onLost) => {
|
|
337
|
+
focus: (id, onLost, refit = false) => {
|
|
346
338
|
if (!disposed)
|
|
347
|
-
setFocus(id ? { id, onLost } : undefined);
|
|
339
|
+
setFocus(id ? { id, onLost } : undefined, refit);
|
|
348
340
|
},
|
|
349
341
|
get focusedScript() { return focus?.id; },
|
|
350
342
|
dispose() {
|
|
@@ -635,8 +627,8 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
635
627
|
* Show only one script in the code area, a hat or a function, or every script again without `id`.
|
|
636
628
|
* `onLost` runs when that script is deleted.
|
|
637
629
|
*/
|
|
638
|
-
focusScript(id, onLost) {
|
|
639
|
-
visibility.focus(id, onLost);
|
|
630
|
+
focusScript(id, onLost, refit = false) {
|
|
631
|
+
visibility.focus(id, onLost, refit);
|
|
640
632
|
},
|
|
641
633
|
get focusedScript() { return visibility.focusedScript; },
|
|
642
634
|
createFunction(target, kind, name, { loadNamedFunctions } = {}) {
|
|
@@ -689,10 +681,11 @@ export function attachCodeWorkspace({ vm, ScratchBlocks: blocks, workspace, opti
|
|
|
689
681
|
return;
|
|
690
682
|
// Let a target switch restore that pane's mode first. Normal views keep showing all scripts.
|
|
691
683
|
if (visibility.focusedScript) {
|
|
692
|
-
visibility.focus(endpoint.scriptId);
|
|
684
|
+
visibility.focus(endpoint.scriptId, undefined, true);
|
|
693
685
|
session.changed(endpoint);
|
|
694
686
|
}
|
|
695
|
-
|
|
687
|
+
else
|
|
688
|
+
workspace.centerOnBlock(endpoint.scriptId);
|
|
696
689
|
});
|
|
697
690
|
});
|
|
698
691
|
visibility.paint();
|
|
@@ -846,15 +839,7 @@ function attachProcedureEditor({ vm, blocks, workspace, options, onError, homeDo
|
|
|
846
839
|
let rendered;
|
|
847
840
|
let positioned = false;
|
|
848
841
|
function position() {
|
|
849
|
-
|
|
850
|
-
const metrics = editor.getMetrics();
|
|
851
|
-
if (!definition || !metrics?.viewWidth || !metrics.viewHeight)
|
|
852
|
-
return false;
|
|
853
|
-
// Put the hat at the top left whatever its place in the code area.
|
|
854
|
-
const { x, y } = definition.getRelativeToSurfaceXY();
|
|
855
|
-
const padding = 16;
|
|
856
|
-
editor.scrollbar.set(x * editor.scale - metrics.contentLeft - padding, y * editor.scale - metrics.contentTop - padding);
|
|
857
|
-
return true;
|
|
842
|
+
return fitScriptInWorkspace(editor, definitionId, blocks);
|
|
858
843
|
}
|
|
859
844
|
function load() {
|
|
860
845
|
if (!exists() || !target.blocks._blocks[definitionId])
|
|
@@ -910,7 +895,7 @@ function attachProcedureEditor({ vm, blocks, workspace, options, onError, homeDo
|
|
|
910
895
|
return;
|
|
911
896
|
}
|
|
912
897
|
pendingBlock = undefined;
|
|
913
|
-
editor
|
|
898
|
+
fitScriptInWorkspace(editor, definitionId, blocks);
|
|
914
899
|
block.select();
|
|
915
900
|
}
|
|
916
901
|
let frame;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-code-views",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.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",
|
package/src/code-pane.ts
CHANGED
|
@@ -35,9 +35,9 @@ export function createCodePaneAdapter(vm, locale: string, attachMinimap?: Attach
|
|
|
35
35
|
const focused = new Map<string, {scriptId: string; onLost?: () => void}>();
|
|
36
36
|
// The code pane the code area is in, which shows its script there.
|
|
37
37
|
let current: string | undefined;
|
|
38
|
-
const applyFocus = (paneId?: string) => {
|
|
38
|
+
const applyFocus = (paneId?: string, refit = false) => {
|
|
39
39
|
const focus = paneId === undefined ? undefined : focused.get(paneId);
|
|
40
|
-
session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()));
|
|
40
|
+
session.bridge?.focusScript(focus?.scriptId, focus && (() => focus.onLost?.()), refit);
|
|
41
41
|
};
|
|
42
42
|
let pending: {frame: number; pane: Pane; presentation: Presentation} | undefined;
|
|
43
43
|
// The GUI can attach the code area again, with a new bridge; the view it is in still shows its script.
|
|
@@ -80,7 +80,7 @@ export function createCodePaneAdapter(vm, locale: string, attachMinimap?: Attach
|
|
|
80
80
|
focusPane(paneId: string, scriptId?: string, onLost?: () => void) {
|
|
81
81
|
if (scriptId) focused.set(paneId, {scriptId, onLost});
|
|
82
82
|
else focused.delete(paneId);
|
|
83
|
-
if (current === paneId) applyFocus(paneId);
|
|
83
|
+
if (current === paneId) applyFocus(paneId, true);
|
|
84
84
|
// Previews of other views redraw with their script.
|
|
85
85
|
session.changed();
|
|
86
86
|
},
|
|
@@ -139,6 +139,8 @@ export function createCodePaneAdapter(vm, locale: string, attachMinimap?: Attach
|
|
|
139
139
|
const frame = requestAnimationFrame(() => {
|
|
140
140
|
pending = undefined;
|
|
141
141
|
session.bridge?.restorePane(pane.targetId, presentation);
|
|
142
|
+
// A picked script owns its initial viewport, including after a saved pane is restored.
|
|
143
|
+
if (focused.has(pane.id)) applyFocus(pane.id, true);
|
|
142
144
|
});
|
|
143
145
|
pending = {frame, pane, presentation};
|
|
144
146
|
},
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ export {attachDockedCodeExtensions} from './code-extensions.ts';
|
|
|
12
12
|
export type {NativeFieldEditSession} from './native-extension-field.ts';
|
|
13
13
|
export type {FieldActionPanel, ProgrammaticExtensionField} from './programmatic-extension-field.ts';
|
|
14
14
|
export {encodeEdit} from './protocol.ts';
|
|
15
|
+
export {fitScriptInWorkspace} from './script-fit.ts';
|
|
15
16
|
export {readScriptRelations} from './script-relations.ts';
|
|
16
17
|
export type {ScriptEndpoint, ScriptRelation} from './script-relations.ts';
|
|
17
18
|
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
type ScriptFitWorkspace = {
|
|
2
|
+
RTL?: boolean;
|
|
3
|
+
options?: {zoomOptions?: {minScale?: number; maxScale?: number}};
|
|
4
|
+
scale: number;
|
|
5
|
+
getInjectionDiv(): HTMLElement;
|
|
6
|
+
getBlockById(id: string): {
|
|
7
|
+
getSvgRoot(): SVGGraphicsElement | null;
|
|
8
|
+
getRelativeToSurfaceXY(): {x: number; y: number};
|
|
9
|
+
} | null;
|
|
10
|
+
getMetrics(): {viewWidth: number; viewHeight: number; contentLeft: number; contentTop: number} | null;
|
|
11
|
+
resize?(): void;
|
|
12
|
+
setScale(scale: number): void;
|
|
13
|
+
scrollbar?: {set(x: number, y: number): void};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const padding = 24;
|
|
17
|
+
const positive = (value: number | undefined, fallback: number) =>
|
|
18
|
+
Number.isFinite(value) && value > 0 ? value : fallback;
|
|
19
|
+
|
|
20
|
+
/** Fit a visible script once. Hidden or not-yet-rendered workspaces can retry after their layout settles. */
|
|
21
|
+
export function fitScriptInWorkspace(workspace: ScriptFitWorkspace, scriptId: string,
|
|
22
|
+
blocks: {svgResize(workspace: ScriptFitWorkspace): void}): boolean {
|
|
23
|
+
const injection = workspace.getInjectionDiv();
|
|
24
|
+
const block = workspace.getBlockById(scriptId);
|
|
25
|
+
if (!block || !injection.clientWidth || !injection.clientHeight || !workspace.scrollbar) return false;
|
|
26
|
+
const root = block.getSvgRoot();
|
|
27
|
+
if (!root) return false;
|
|
28
|
+
// A split can have its new DOM size while Blockly's cached SVG size still belongs to the old pane.
|
|
29
|
+
blocks.svgResize(workspace);
|
|
30
|
+
workspace.resize?.();
|
|
31
|
+
const bounds = root.getBBox();
|
|
32
|
+
const metrics = workspace.getMetrics();
|
|
33
|
+
if (!metrics?.viewWidth || !metrics.viewHeight || !(bounds.width > 0) || !(bounds.height > 0)) return false;
|
|
34
|
+
// SVG bounds include nested reporters and the hat's overhang, but exclude folded, hidden bodies.
|
|
35
|
+
const origin = block.getRelativeToSurfaceXY();
|
|
36
|
+
const left = origin.x + bounds.x;
|
|
37
|
+
const top = origin.y + bounds.y;
|
|
38
|
+
const zoom = workspace.options?.zoomOptions;
|
|
39
|
+
const workspaceMin = positive(zoom?.minScale, 0.01);
|
|
40
|
+
const workspaceMax = Math.max(workspaceMin, positive(zoom?.maxScale, Infinity));
|
|
41
|
+
const minScale = Math.min(Math.max(0.5, workspaceMin), workspaceMax);
|
|
42
|
+
const maxScale = Math.max(minScale, Math.min(1.25, workspaceMax));
|
|
43
|
+
const fit = Math.min(Math.max(1, metrics.viewWidth - padding * 2) / bounds.width,
|
|
44
|
+
Math.max(1, metrics.viewHeight - padding * 2) / bounds.height);
|
|
45
|
+
const scale = Math.max(minScale, Math.min(maxScale, fit));
|
|
46
|
+
workspace.setScale(scale);
|
|
47
|
+
const scaled = workspace.getMetrics();
|
|
48
|
+
if (!scaled) return false;
|
|
49
|
+
const width = bounds.width * workspace.scale;
|
|
50
|
+
const height = bounds.height * workspace.scale;
|
|
51
|
+
const x = width > scaled.viewWidth - padding * 2 && workspace.RTL ?
|
|
52
|
+
scaled.viewWidth - padding - width : Math.max(padding, (scaled.viewWidth - width) / 2);
|
|
53
|
+
const y = workspace.scale <= minScale ? padding : Math.max(padding, (scaled.viewHeight - height) / 2);
|
|
54
|
+
workspace.scrollbar.set(left * workspace.scale - scaled.contentLeft - x,
|
|
55
|
+
top * workspace.scale - scaled.contentTop - y);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
@@ -4,6 +4,7 @@ import type {createCodePaneAdapter} from './code-pane.ts';
|
|
|
4
4
|
import {getCodeSession} from './workspace.ts';
|
|
5
5
|
|
|
6
6
|
export {attachCodeWorkspace} from './workspace.ts';
|
|
7
|
+
export {fitScriptInWorkspace} from './script-fit.ts';
|
|
7
8
|
export {attachFunctionCallMenu, createCodePaneAdapter, createFunction, mountProcedureEditor, readHatScripts, readSavedCodePane, readScriptRelationGraph} from './code-pane.ts';
|
|
8
9
|
export type {SavedCodePane} from './code-pane.ts';
|
|
9
10
|
|
package/src/workspace.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {PresentationElements} from './preview-chrome.ts';
|
|
|
8
8
|
import {attachScriptView, getDockedScriptLayout, type ScriptView} from './script-layout-view.ts';
|
|
9
9
|
import {attachScriptArrows, registerScriptRelationNavigation} from './script-arrows.ts';
|
|
10
10
|
import type {RelationBlock, ScriptEndpoint} from './script-relations.ts';
|
|
11
|
+
import {fitScriptInWorkspace} from './script-fit.ts';
|
|
11
12
|
|
|
12
13
|
export type AttachPreviewMinimap = (workspace, ScratchBlocks, watermark: HTMLElement) => {dispose(): void};
|
|
13
14
|
/** Adds the code area's zoom buttons and minimap to another workspace. */
|
|
@@ -296,27 +297,16 @@ function attachScriptVisibility({vm, blocks, workspace, homeDocument, onError})
|
|
|
296
297
|
}
|
|
297
298
|
|
|
298
299
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
300
|
+
* Fit the shown script once the code area has its size: its destination view may only be laid out
|
|
301
|
+
* after the script was picked. Later edits and manual zooms keep their viewport.
|
|
301
302
|
*/
|
|
302
303
|
let revealPending = false;
|
|
303
304
|
function reveal() {
|
|
304
305
|
if (!revealPending) return;
|
|
305
|
-
|
|
306
|
-
const injection = workspace.getInjectionDiv();
|
|
307
|
-
if (!block || !injection.clientWidth || !injection.clientHeight) return;
|
|
308
|
-
// The scroll area follows the shown scripts, which may have changed since the scrollbars were sized.
|
|
309
|
-
workspace.resize?.();
|
|
310
|
-
const metrics = workspace.getMetrics?.();
|
|
311
|
-
if (!metrics?.viewWidth || !metrics.viewHeight) return;
|
|
312
|
-
revealPending = false;
|
|
313
|
-
const {x, y} = block.getRelativeToSurfaceXY();
|
|
314
|
-
const padding = 16;
|
|
315
|
-
workspace.scrollbar?.set(x * workspace.scale - metrics.contentLeft - padding,
|
|
316
|
-
y * workspace.scale - metrics.contentTop - padding);
|
|
306
|
+
if (focus && fitScriptInWorkspace(workspace, focus.id, blocks)) revealPending = false;
|
|
317
307
|
}
|
|
318
308
|
|
|
319
|
-
function setFocus(next?: {id: string; onLost?: () => void}) {
|
|
309
|
+
function setFocus(next?: {id: string; onLost?: () => void}, refit = false) {
|
|
320
310
|
const changedScript = next?.id !== focus?.id;
|
|
321
311
|
focus = next && {id: next.id, onLost: next.onLost, extras: changedScript ? new Set() : focus?.extras ?? new Set()};
|
|
322
312
|
if (focus) {
|
|
@@ -337,11 +327,11 @@ function attachScriptVisibility({vm, blocks, workspace, homeDocument, onError})
|
|
|
337
327
|
} else restoreWorkspaceMethods();
|
|
338
328
|
safePaint();
|
|
339
329
|
if (workspace.rendered && workspace.isVisible?.() !== false) workspace.resize?.();
|
|
340
|
-
if (focus && changedScript) {
|
|
330
|
+
if (focus && (changedScript || refit)) {
|
|
341
331
|
revealPending = true;
|
|
342
|
-
|
|
332
|
+
// The pane can still be moving from its previous size during this activation.
|
|
343
333
|
schedulePaint();
|
|
344
|
-
}
|
|
334
|
+
} else if (!focus) revealPending = false;
|
|
345
335
|
}
|
|
346
336
|
|
|
347
337
|
// Blocks dragged in from the palette, or pulled out of the shown script, stay in view. Only the end of a
|
|
@@ -387,8 +377,8 @@ function attachScriptVisibility({vm, blocks, workspace, homeDocument, onError})
|
|
|
387
377
|
resized: () => {
|
|
388
378
|
if (!disposed) reveal();
|
|
389
379
|
},
|
|
390
|
-
focus: (id?: string, onLost?: () => void) => {
|
|
391
|
-
if (!disposed) setFocus(id ? {id, onLost} : undefined);
|
|
380
|
+
focus: (id?: string, onLost?: () => void, refit = false) => {
|
|
381
|
+
if (!disposed) setFocus(id ? {id, onLost} : undefined, refit);
|
|
392
382
|
},
|
|
393
383
|
get focusedScript() { return focus?.id; },
|
|
394
384
|
dispose() {
|
|
@@ -664,8 +654,8 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
664
654
|
* Show only one script in the code area, a hat or a function, or every script again without `id`.
|
|
665
655
|
* `onLost` runs when that script is deleted.
|
|
666
656
|
*/
|
|
667
|
-
focusScript(id?: string, onLost?: () => void) {
|
|
668
|
-
visibility.focus(id, onLost);
|
|
657
|
+
focusScript(id?: string, onLost?: () => void, refit = false) {
|
|
658
|
+
visibility.focus(id, onLost, refit);
|
|
669
659
|
},
|
|
670
660
|
get focusedScript() { return visibility.focusedScript; },
|
|
671
661
|
createFunction(target: CodeTarget & {id: string}, kind: 'procedure' | 'named', name: string, {loadNamedFunctions}: {
|
|
@@ -718,10 +708,9 @@ export function attachCodeWorkspace({vm, ScratchBlocks: blocks, workspace, optio
|
|
|
718
708
|
if (session.bridge !== bridge) return;
|
|
719
709
|
// Let a target switch restore that pane's mode first. Normal views keep showing all scripts.
|
|
720
710
|
if (visibility.focusedScript) {
|
|
721
|
-
visibility.focus(endpoint.scriptId);
|
|
711
|
+
visibility.focus(endpoint.scriptId, undefined, true);
|
|
722
712
|
session.changed(endpoint);
|
|
723
|
-
}
|
|
724
|
-
workspace.centerOnBlock(endpoint.scriptId);
|
|
713
|
+
} else workspace.centerOnBlock(endpoint.scriptId);
|
|
725
714
|
});
|
|
726
715
|
});
|
|
727
716
|
visibility.paint();
|
|
@@ -885,15 +874,7 @@ function attachProcedureEditor({vm, blocks, workspace, options, onError, homeDoc
|
|
|
885
874
|
let rendered: string | undefined;
|
|
886
875
|
let positioned = false;
|
|
887
876
|
function position() {
|
|
888
|
-
|
|
889
|
-
const metrics = editor.getMetrics();
|
|
890
|
-
if (!definition || !metrics?.viewWidth || !metrics.viewHeight) return false;
|
|
891
|
-
// Put the hat at the top left whatever its place in the code area.
|
|
892
|
-
const {x, y} = definition.getRelativeToSurfaceXY();
|
|
893
|
-
const padding = 16;
|
|
894
|
-
editor.scrollbar.set(x * editor.scale - metrics.contentLeft - padding,
|
|
895
|
-
y * editor.scale - metrics.contentTop - padding);
|
|
896
|
-
return true;
|
|
877
|
+
return fitScriptInWorkspace(editor, definitionId, blocks);
|
|
897
878
|
}
|
|
898
879
|
function load() {
|
|
899
880
|
if (!exists() || !target.blocks._blocks[definitionId]) return;
|
|
@@ -939,7 +920,7 @@ function attachProcedureEditor({vm, blocks, workspace, options, onError, homeDoc
|
|
|
939
920
|
return;
|
|
940
921
|
}
|
|
941
922
|
pendingBlock = undefined;
|
|
942
|
-
editor
|
|
923
|
+
fitScriptInWorkspace(editor, definitionId, blocks);
|
|
943
924
|
block.select();
|
|
944
925
|
}
|
|
945
926
|
|