@kubohiroya/turbowarp-plus-editor-code-views 0.2.0 → 0.3.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 +25 -0
- package/README.md +25 -0
- package/dist/code-pane.d.ts +2 -2
- package/dist/palette-category-visibility.d.ts +90 -0
- package/dist/palette-category-visibility.js +263 -0
- package/dist/palette-visibility-dialog.d.ts +68 -0
- package/dist/palette-visibility-dialog.js +176 -0
- package/package.json +13 -1
- package/src/code-pane.ts +3 -2
- package/src/palette-category-visibility.ts +300 -0
- package/src/palette-visibility-dialog.tsx +250 -0
- package/src/styles.css +197 -0
package/README.ja.md
CHANGED
|
@@ -17,6 +17,7 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
17
17
|
| `./target-combobox` | コードの窓の対象の選択 |
|
|
18
18
|
| `./detached-block-palette` | 別窓のブロックパレット |
|
|
19
19
|
| `./palette-category-reorder`・`./palette-category-search`・`./palette-dropdown-resize` | パレットのカテゴリの並べ替えと検索、パレットのドロップダウンの大きさの変更 |
|
|
20
|
+
| `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はパレットのコメントに保存し、選ぶ画面を持つ |
|
|
20
21
|
| `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する |
|
|
21
22
|
| `./asset-edit-session`・`./dockview-unsaved` | 素材の編集のセッションと、Dockview のタブの未保存の印。ホストが GUI のエディターとレイアウトにつなぐ |
|
|
22
23
|
| `./styles.css` | 上のすべてのスタイルシート |
|
|
@@ -40,3 +41,27 @@ Editor は GUI の React 16 にこれらを描画するので、React 16.14 以
|
|
|
40
41
|
## ライセンス
|
|
41
42
|
|
|
42
43
|
MPL-2.0。turbowarp-plus-editor の `packages/editor-views` と `packages/editor-ui` から移したコードで、同じ作者が書いたもの。移すときに GPL-3.0 から MPL-2.0 にした。由来の監査は [turbowarp-plus-packages の移行計画](https://github.com/kubohiroya/turbowarp-plus-packages/blob/main/docs/editor-migration-plan.ja.md)の「由来の監査(7b)」にある。
|
|
44
|
+
|
|
45
|
+
## パレットに出すブロックグループ
|
|
46
|
+
|
|
47
|
+
スプライト・背景ごとに、パレットに出すブロックグループ(標準のグループと、機能拡張のグループ)を選べる。TypeScript で言えば、作品の `extensions` が package.json、スプライトごとの設定が各ファイルの import 文にあたる。
|
|
48
|
+
|
|
49
|
+
- 設定は sb3-toolchain の展開ソース形式 2 と同じ、スプライト・背景ごとのパレットのコメントに入る(作品フォルダでは `block-palette.json`)。既定と同じスプライトにはコメントを置かない。背景のコメントは、作品の「適用可能なセット」も持つ。
|
|
50
|
+
- 設定のないスプライト・背景は、標準のグループと、そのコードが使っている機能拡張のグループを出す。
|
|
51
|
+
- 非表示にしても、コード領域のブロックはそのまま見えて編集できる。
|
|
52
|
+
|
|
53
|
+
`./palette-category-visibility`
|
|
54
|
+
|
|
55
|
+
| 関数 | 内容 |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `visibleGroups(target, extensionIds)` | 対象に出すグループ |
|
|
58
|
+
| `filterToolboxTree(tree, visible)` | パレットを描く前のツールボックスの木から、出さないカテゴリを外す |
|
|
59
|
+
| `readTargetPalette(target)`・`writeTargetPalette(target, groups, options)` | パレットのコメントを読み書きする。書く文は sb3-toolchain の `formatBlockPaletteComment` と同じ |
|
|
60
|
+
| `usedGroups(target)`・`groupOfOpcode(opcode)` | コードが使っているグループと、その数 |
|
|
61
|
+
| `paletteGroupSets(locale, projectSets)` | 「標準」「表現」「変数・リスト」「基本の制御」と、作品のセット |
|
|
62
|
+
| `attachPaletteMenuButton(menu, getLocale, getItems)` | カテゴリの一覧の末尾(横配置では右端、縦配置では下端)に ▼ のボタンを置く。押すと、ホストが渡す項目のメニューを出す。項目は「ブロックグループの表示・非表示の設定」(`paletteVisibilityMenuLabel`)と、ホストの機能拡張に関する項目 |
|
|
63
|
+
|
|
64
|
+
`./palette-visibility-dialog` の `openPaletteVisibilityDialog(document, props)` は「パレット内のブロックグループの表示・非表示の設定」の画面を開く。機能拡張の各行は `@kubohiroya/turbowarp-extension-security-dialog/react` の `ExtensionRow` で描くので、拡張の追加の画面と同じ見た目になる。適用すると、出すグループと、この画面で足したセットを `onApply` に渡す。
|
|
65
|
+
|
|
66
|
+
ホストは `@kubohiroya/sb3-toolchain`(0.25.1 以上)と `@kubohiroya/turbowarp-extension-security-dialog`(0.9.0 以上)を入れ、security-dialog の `styles.css` も読み込む。1つの機能拡張の中を複数のグループに分けて選ぶ機能は、VM がグループごとのカテゴリを作れるようになってから足す。
|
|
67
|
+
|
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ The code views of TurboWarp Plus Editor: code in tabs and windows that share one
|
|
|
16
16
|
| `./variable-references` | Variable references scoped to the edited target |
|
|
17
17
|
| `./target-combobox` | The target picker of a code window |
|
|
18
18
|
| `./detached-block-palette` | The block palette in its own window |
|
|
19
|
+
| `./palette-category-visibility`, `./palette-visibility-dialog` | Choose which block groups the palette shows for each sprite and the stage. The setting is kept in a palette comment, with a screen to choose |
|
|
19
20
|
| `./palette-category-reorder`, `./palette-category-search`, `./palette-dropdown-resize` | Reordering and searching palette categories, and resizing palette dropdowns |
|
|
20
21
|
| `./block-argument-text` | Shortens long text arguments and edits them in a dialog |
|
|
21
22
|
| `./asset-edit-session`, `./dockview-unsaved` | The asset edit session and the unsaved marks on Dockview tabs, which the host connects to the GUI's editors and layout |
|
|
@@ -40,3 +41,27 @@ Run `npm test` to build the package, run the unit tests, and check that the sour
|
|
|
40
41
|
## License
|
|
41
42
|
|
|
42
43
|
MPL-2.0. This code moved here from `packages/editor-views` and `packages/editor-ui` in turbowarp-plus-editor. The same author wrote it, and it changed from GPL-3.0 to MPL-2.0 in the move. The provenance audit is in the "由来の監査(7b)" section of the [migration plan in turbowarp-plus-packages](https://github.com/kubohiroya/turbowarp-plus-packages/blob/main/docs/editor-migration-plan.ja.md).
|
|
44
|
+
|
|
45
|
+
## Block groups in the palette
|
|
46
|
+
|
|
47
|
+
Each sprite and the stage can choose which block groups the palette shows: the standard groups and the groups of extensions. In TypeScript terms, the project's `extensions` are package.json and each sprite's setting is a file's import statements.
|
|
48
|
+
|
|
49
|
+
- The setting is kept where sb3-toolchain source format 2 keeps it: one palette comment per sprite or stage (`block-palette.json` in a project folder). A sprite whose setting equals its default gets no comment. The stage's comment also holds the project's applicable sets.
|
|
50
|
+
- A sprite or stage without a setting shows the standard groups and the groups of the extensions its code uses.
|
|
51
|
+
- Hiding a group never hides blocks already in the code; they stay visible and editable.
|
|
52
|
+
|
|
53
|
+
`./palette-category-visibility`
|
|
54
|
+
|
|
55
|
+
| Function | What it does |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `visibleGroups(target, extensionIds)` | The groups shown for a target |
|
|
58
|
+
| `filterToolboxTree(tree, visible)` | Removes the categories a target does not show from the parsed toolbox before the palette is drawn |
|
|
59
|
+
| `readTargetPalette(target)`, `writeTargetPalette(target, groups, options)` | Read and write the palette comment, in the text sb3-toolchain's `formatBlockPaletteComment` writes |
|
|
60
|
+
| `usedGroups(target)`, `groupOfOpcode(opcode)` | The groups the code uses, and how many blocks of each |
|
|
61
|
+
| `paletteGroupSets(locale, projectSets)` | Standard, Expression, Variables and lists, Basic control, and the project's own sets |
|
|
62
|
+
| `attachPaletteMenuButton(menu, getLocale, getItems)` | Puts a ▼ button at the end of the category menu (the right end of a horizontal palette, the bottom of a vertical one). Pressing it lists the items the host gives: the group settings (`paletteVisibilityMenuLabel`) and the host's extension items |
|
|
63
|
+
|
|
64
|
+
`openPaletteVisibilityDialog(document, props)` from `./palette-visibility-dialog` opens the screen that chooses the groups. Each extension is drawn with `ExtensionRow` from `@kubohiroya/turbowarp-extension-security-dialog/react`, so it looks the same as in the screen that adds extensions. Applying passes the groups to show, and the sets added in the screen, to `onApply`.
|
|
65
|
+
|
|
66
|
+
The host installs `@kubohiroya/sb3-toolchain` (0.25.1 or later) and `@kubohiroya/turbowarp-extension-security-dialog` (0.9.0 or later), and loads security-dialog's `styles.css`. Choosing groups inside one extension comes once the VM can make a category per group.
|
|
67
|
+
|
package/dist/code-pane.d.ts
CHANGED
|
@@ -9,12 +9,12 @@ type Pane = {
|
|
|
9
9
|
type Bridge = NonNullable<ReturnType<typeof getCodeSession>['bridge']>;
|
|
10
10
|
type Presentation = NonNullable<ReturnType<Bridge['capturePane']>>;
|
|
11
11
|
/** The code area of a tab as a saved workspace keeps it: zoom, scroll position and script view. */
|
|
12
|
-
export
|
|
12
|
+
export type SavedCodePane = {
|
|
13
13
|
scale: number;
|
|
14
14
|
x: number;
|
|
15
15
|
y: number;
|
|
16
16
|
scriptView?: ScriptView;
|
|
17
|
-
}
|
|
17
|
+
};
|
|
18
18
|
/** Read a saved code area back, or nothing if it does not have the shape `serialize` writes. */
|
|
19
19
|
export declare function readSavedCodePane(value: Record<string, unknown>): SavedCodePane | undefined;
|
|
20
20
|
export declare function createCodePaneAdapter(vm: any, locale: string, attachMinimap?: AttachPreviewMinimap): {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { BlockPaletteSet } from '@kubohiroya/sb3-toolchain/browser';
|
|
2
|
+
/**
|
|
3
|
+
* Which block groups the palette shows for each sprite and the stage.
|
|
4
|
+
*
|
|
5
|
+
* The setting lives where sb3-toolchain source format 2 keeps it: one palette comment per sprite or
|
|
6
|
+
* stage (`block-palette.json` in a project folder). It changes only what the palette offers; blocks
|
|
7
|
+
* already in the code stay visible and editable.
|
|
8
|
+
*/
|
|
9
|
+
/** The standard groups, by the palette's category IDs. `lists` is Plus's own split of `variables`. */
|
|
10
|
+
export declare const standardGroups: readonly ["motion", "looks", "sound", "events", "control", "sensing", "operators", "variables", "lists", "myBlocks"];
|
|
11
|
+
export interface PaletteGroupSet {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
groups: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
/** The sets every project has, then the project's own sets from its stage palette comment. */
|
|
17
|
+
export declare function paletteGroupSets(locale?: string, projectSets?: Record<string, BlockPaletteSet>): PaletteGroupSet[];
|
|
18
|
+
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
19
|
+
export declare function groupOfOpcode(opcode: string): string | null;
|
|
20
|
+
type BlockLike = {
|
|
21
|
+
opcode?: string;
|
|
22
|
+
shadow?: boolean;
|
|
23
|
+
};
|
|
24
|
+
type CommentLike = {
|
|
25
|
+
text?: string;
|
|
26
|
+
id?: string;
|
|
27
|
+
};
|
|
28
|
+
/** The parts of a VM target this module reads and writes. */
|
|
29
|
+
export interface PaletteTarget {
|
|
30
|
+
isStage: boolean;
|
|
31
|
+
getName(): string;
|
|
32
|
+
blocks?: {
|
|
33
|
+
_blocks?: Record<string, BlockLike>;
|
|
34
|
+
};
|
|
35
|
+
comments: Record<string, CommentLike>;
|
|
36
|
+
createComment?(id: string, blockId: string | null, text: string, x: number, y: number, width: number, height: number, minimized: boolean): void;
|
|
37
|
+
deleteComment?(id: string): void;
|
|
38
|
+
}
|
|
39
|
+
/** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
|
|
40
|
+
export declare function usedGroups(target: Pick<PaletteTarget, 'blocks'>): Map<string, number>;
|
|
41
|
+
export interface TargetPalette {
|
|
42
|
+
/** The comment that holds the setting. */
|
|
43
|
+
commentId: string;
|
|
44
|
+
groups?: string[];
|
|
45
|
+
/** Present on the stage's comment only. */
|
|
46
|
+
order?: string[];
|
|
47
|
+
sets?: Record<string, BlockPaletteSet>;
|
|
48
|
+
}
|
|
49
|
+
/** The palette setting a target carries in its palette comment, or null when it has none or it cannot be read. */
|
|
50
|
+
export declare function readTargetPalette(target: Pick<PaletteTarget, 'comments'>): TargetPalette | null;
|
|
51
|
+
/**
|
|
52
|
+
* The groups shown when a target has no setting of its own: every standard group, and the groups of
|
|
53
|
+
* the extensions its code already uses, so an existing project opens with nothing missing.
|
|
54
|
+
*/
|
|
55
|
+
export declare function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks'>, extensionIds: readonly string[]): string[];
|
|
56
|
+
/** The groups the palette shows for the target. */
|
|
57
|
+
export declare function visibleGroups(target: Pick<PaletteTarget, 'blocks' | 'comments'>, extensionIds: readonly string[]): Set<string>;
|
|
58
|
+
/** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
|
|
59
|
+
export declare function filterToolboxTree(tree: Element | null | undefined, visible: ReadonlySet<string>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Stores the target's palette setting in its palette comment, as sb3-toolchain source format 2 reads it.
|
|
62
|
+
* A sprite whose groups equal its default gets no comment; the stage keeps one while it carries the
|
|
63
|
+
* project's group order or sets.
|
|
64
|
+
*/
|
|
65
|
+
export declare function writeTargetPalette(target: PaletteTarget, groups: readonly string[], { extensionIds, order, sets }?: {
|
|
66
|
+
extensionIds?: readonly string[];
|
|
67
|
+
order?: readonly string[];
|
|
68
|
+
sets?: Record<string, BlockPaletteSet>;
|
|
69
|
+
}): void;
|
|
70
|
+
/** The menu item that opens the group settings. */
|
|
71
|
+
export declare const paletteVisibilityMenuLabel: (locale: string) => "ブロックグループの表示・非表示の設定" | "Show or hide block groups";
|
|
72
|
+
export interface PaletteMenuItem {
|
|
73
|
+
label: string;
|
|
74
|
+
onSelect(): void;
|
|
75
|
+
/** Draws a separator above the item. */
|
|
76
|
+
separated?: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Adds a ▼ button at the end of the palette's category menu: after the categories, so it sits at the
|
|
80
|
+
* right end of a horizontal palette and at the bottom of a vertical one. Pressing it lists the items
|
|
81
|
+
* the host gives — the group settings and the host's extension items — and runs the one chosen.
|
|
82
|
+
*/
|
|
83
|
+
export declare function attachPaletteMenuButton(menu: HTMLElement, getLocale: () => string, getItems: (locale: string) => readonly PaletteMenuItem[]): {
|
|
84
|
+
button: HTMLButtonElement;
|
|
85
|
+
/** Puts the button back at the end after the host rebuilds the menu, and follows the locale. */
|
|
86
|
+
update(): void;
|
|
87
|
+
close: () => void;
|
|
88
|
+
dispose(): void;
|
|
89
|
+
};
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { blockPaletteCommentMarker, formatBlockPaletteComment } from '@kubohiroya/sb3-toolchain/browser';
|
|
2
|
+
/**
|
|
3
|
+
* Which block groups the palette shows for each sprite and the stage.
|
|
4
|
+
*
|
|
5
|
+
* The setting lives where sb3-toolchain source format 2 keeps it: one palette comment per sprite or
|
|
6
|
+
* stage (`block-palette.json` in a project folder). It changes only what the palette offers; blocks
|
|
7
|
+
* already in the code stay visible and editable.
|
|
8
|
+
*/
|
|
9
|
+
/** The standard groups, by the palette's category IDs. `lists` is Plus's own split of `variables`. */
|
|
10
|
+
export const standardGroups = ['motion', 'looks', 'sound', 'events', 'control', 'sensing', 'operators', 'variables', 'lists',
|
|
11
|
+
'myBlocks'];
|
|
12
|
+
const builtinSetNames = {
|
|
13
|
+
standard: { en: 'Standard', ja: '標準' },
|
|
14
|
+
expression: { en: 'Expression', ja: '表現' },
|
|
15
|
+
data: { en: 'Variables and lists', ja: '変数・リスト' },
|
|
16
|
+
basics: { en: 'Basic control', ja: '基本の制御' }
|
|
17
|
+
};
|
|
18
|
+
const builtinSetGroups = {
|
|
19
|
+
standard: standardGroups,
|
|
20
|
+
expression: ['motion', 'looks', 'sound', 'sensing'],
|
|
21
|
+
data: ['variables', 'lists'],
|
|
22
|
+
basics: ['events', 'control', 'operators']
|
|
23
|
+
};
|
|
24
|
+
const japanese = (locale) => /^ja(?:-|$)/i.test(locale ?? '');
|
|
25
|
+
/** The sets every project has, then the project's own sets from its stage palette comment. */
|
|
26
|
+
export function paletteGroupSets(locale, projectSets = {}) {
|
|
27
|
+
const builtin = Object.keys(builtinSetGroups).map(id => ({
|
|
28
|
+
id, name: builtinSetNames[id][japanese(locale) ? 'ja' : 'en'], groups: builtinSetGroups[id]
|
|
29
|
+
}));
|
|
30
|
+
return builtin.concat(Object.entries(projectSets).map(([id, set]) => ({ id, name: set.name, groups: set.groups })));
|
|
31
|
+
}
|
|
32
|
+
const standardPrefixes = {
|
|
33
|
+
motion: 'motion', looks: 'looks', sound: 'sound', event: 'events', control: 'control', sensing: 'sensing',
|
|
34
|
+
operator: 'operators', procedures: 'myBlocks', argument: 'myBlocks'
|
|
35
|
+
};
|
|
36
|
+
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
37
|
+
export function groupOfOpcode(opcode) {
|
|
38
|
+
const separator = opcode.indexOf('_');
|
|
39
|
+
if (separator <= 0)
|
|
40
|
+
return null;
|
|
41
|
+
const prefix = opcode.slice(0, separator);
|
|
42
|
+
if (prefix === 'data')
|
|
43
|
+
return /list/i.test(opcode.slice(separator + 1)) ? 'lists' : 'variables';
|
|
44
|
+
return standardPrefixes[prefix] ?? prefix;
|
|
45
|
+
}
|
|
46
|
+
/** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
|
|
47
|
+
export function usedGroups(target) {
|
|
48
|
+
const counts = new Map();
|
|
49
|
+
for (const block of Object.values(target.blocks?._blocks ?? {})) {
|
|
50
|
+
if (!block || block.shadow || typeof block.opcode !== 'string')
|
|
51
|
+
continue;
|
|
52
|
+
const group = groupOfOpcode(block.opcode);
|
|
53
|
+
if (group)
|
|
54
|
+
counts.set(group, (counts.get(group) ?? 0) + 1);
|
|
55
|
+
}
|
|
56
|
+
return counts;
|
|
57
|
+
}
|
|
58
|
+
const markedLine = (text) => text.split('\n').find(line => line.endsWith(blockPaletteCommentMarker));
|
|
59
|
+
/** The palette setting a target carries in its palette comment, or null when it has none or it cannot be read. */
|
|
60
|
+
export function readTargetPalette(target) {
|
|
61
|
+
for (const [commentId, comment] of Object.entries(target.comments ?? {})) {
|
|
62
|
+
const line = typeof comment?.text === 'string' ? markedLine(comment.text) : undefined;
|
|
63
|
+
if (!line)
|
|
64
|
+
continue;
|
|
65
|
+
try {
|
|
66
|
+
const value = JSON.parse(line.slice(0, -blockPaletteCommentMarker.length));
|
|
67
|
+
if (!value || typeof value !== 'object')
|
|
68
|
+
return null;
|
|
69
|
+
const strings = (list) => Array.isArray(list) && list.every(item => typeof item === 'string') ?
|
|
70
|
+
list : undefined;
|
|
71
|
+
return {
|
|
72
|
+
commentId,
|
|
73
|
+
groups: strings(value.groups),
|
|
74
|
+
order: strings(value.order),
|
|
75
|
+
sets: value.sets && typeof value.sets === 'object' ? value.sets : undefined
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The groups shown when a target has no setting of its own: every standard group, and the groups of
|
|
86
|
+
* the extensions its code already uses, so an existing project opens with nothing missing.
|
|
87
|
+
*/
|
|
88
|
+
export function defaultVisibleGroups(target, extensionIds) {
|
|
89
|
+
const used = usedGroups(target);
|
|
90
|
+
return [...standardGroups, ...extensionIds.filter(id => used.has(id))];
|
|
91
|
+
}
|
|
92
|
+
/** The groups the palette shows for the target. */
|
|
93
|
+
export function visibleGroups(target, extensionIds) {
|
|
94
|
+
const groups = readTargetPalette(target)?.groups;
|
|
95
|
+
return new Set(groups ?? defaultVisibleGroups(target, extensionIds));
|
|
96
|
+
}
|
|
97
|
+
/** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
|
|
98
|
+
export function filterToolboxTree(tree, visible) {
|
|
99
|
+
if (!tree)
|
|
100
|
+
return;
|
|
101
|
+
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
102
|
+
const id = category.getAttribute('id');
|
|
103
|
+
if (id && !visible.has(id))
|
|
104
|
+
category.remove();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const newCommentId = () => `twplus-palette-${Math.random().toString(36).slice(2, 10)}`;
|
|
108
|
+
/**
|
|
109
|
+
* Stores the target's palette setting in its palette comment, as sb3-toolchain source format 2 reads it.
|
|
110
|
+
* A sprite whose groups equal its default gets no comment; the stage keeps one while it carries the
|
|
111
|
+
* project's group order or sets.
|
|
112
|
+
*/
|
|
113
|
+
export function writeTargetPalette(target, groups, { extensionIds = [], order, sets } = {}) {
|
|
114
|
+
const current = readTargetPalette(target);
|
|
115
|
+
const name = target.getName();
|
|
116
|
+
const isDefault = sameGroups(groups, defaultVisibleGroups(target, extensionIds));
|
|
117
|
+
const projectOrder = target.isStage ? order ?? current?.order : undefined;
|
|
118
|
+
const projectSets = target.isStage ? sets ?? current?.sets : undefined;
|
|
119
|
+
const keepsProjectSettings = (projectOrder && projectOrder.length > 0) ||
|
|
120
|
+
(projectSets && Object.keys(projectSets).length > 0);
|
|
121
|
+
if (isDefault && !keepsProjectSettings) {
|
|
122
|
+
if (current)
|
|
123
|
+
target.deleteComment?.(current.commentId);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const document = { formatVersion: 1 };
|
|
127
|
+
if (projectOrder?.length)
|
|
128
|
+
document.order = [...projectOrder];
|
|
129
|
+
if (projectSets && Object.keys(projectSets).length)
|
|
130
|
+
document.sets = projectSets;
|
|
131
|
+
if (target.isStage) {
|
|
132
|
+
if (!isDefault)
|
|
133
|
+
document.stage = { groups: [...groups] };
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
document.sprites = { [name]: { groups: [...groups] } };
|
|
137
|
+
}
|
|
138
|
+
const text = formatBlockPaletteComment(document, { isStage: target.isStage, name });
|
|
139
|
+
if (current) {
|
|
140
|
+
target.comments[current.commentId].text = text;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
target.createComment?.(newCommentId(), null, text, 0, 0, 200, 200, true);
|
|
144
|
+
}
|
|
145
|
+
const sameGroups = (left, right) => left.length === right.length && new Set(left).size === left.length && left.every(group => right.includes(group));
|
|
146
|
+
/** The menu item that opens the group settings. */
|
|
147
|
+
export const paletteVisibilityMenuLabel = (locale) => japanese(locale) ? 'ブロックグループの表示・非表示の設定' :
|
|
148
|
+
'Show or hide block groups';
|
|
149
|
+
const menuButtonLabel = (locale) => japanese(locale) ? 'パレットのメニュー' : 'Palette menu';
|
|
150
|
+
/**
|
|
151
|
+
* Adds a ▼ button at the end of the palette's category menu: after the categories, so it sits at the
|
|
152
|
+
* right end of a horizontal palette and at the bottom of a vertical one. Pressing it lists the items
|
|
153
|
+
* the host gives — the group settings and the host's extension items — and runs the one chosen.
|
|
154
|
+
*/
|
|
155
|
+
export function attachPaletteMenuButton(menu, getLocale, getItems) {
|
|
156
|
+
const doc = menu.ownerDocument;
|
|
157
|
+
const button = doc.createElement('button');
|
|
158
|
+
button.type = 'button';
|
|
159
|
+
button.className = 'tw-palette-menu-button';
|
|
160
|
+
button.textContent = '▼';
|
|
161
|
+
button.setAttribute('aria-haspopup', 'menu');
|
|
162
|
+
button.setAttribute('aria-expanded', 'false');
|
|
163
|
+
const label = () => {
|
|
164
|
+
const text = menuButtonLabel(getLocale());
|
|
165
|
+
button.setAttribute('aria-label', text);
|
|
166
|
+
button.title = text;
|
|
167
|
+
};
|
|
168
|
+
label();
|
|
169
|
+
let popup = null;
|
|
170
|
+
const closeMenu = (focusButton = false) => {
|
|
171
|
+
if (!popup)
|
|
172
|
+
return;
|
|
173
|
+
popup.remove();
|
|
174
|
+
popup = null;
|
|
175
|
+
button.setAttribute('aria-expanded', 'false');
|
|
176
|
+
doc.removeEventListener('pointerdown', outside, true);
|
|
177
|
+
doc.removeEventListener('keydown', keys, true);
|
|
178
|
+
if (focusButton)
|
|
179
|
+
button.focus();
|
|
180
|
+
};
|
|
181
|
+
const outside = (event) => {
|
|
182
|
+
if (popup && !popup.contains(event.target) && event.target !== button)
|
|
183
|
+
closeMenu();
|
|
184
|
+
};
|
|
185
|
+
const keys = (event) => {
|
|
186
|
+
if (!popup)
|
|
187
|
+
return;
|
|
188
|
+
const items = Array.from(popup.querySelectorAll('[role="menuitem"]'));
|
|
189
|
+
const index = items.indexOf(doc.activeElement);
|
|
190
|
+
if (event.key === 'Escape') {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
closeMenu(true);
|
|
193
|
+
}
|
|
194
|
+
else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
195
|
+
event.preventDefault();
|
|
196
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
197
|
+
items[(index + step + items.length) % items.length]?.focus();
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const openMenu = () => {
|
|
201
|
+
const locale = getLocale();
|
|
202
|
+
const items = getItems(locale);
|
|
203
|
+
if (!items.length)
|
|
204
|
+
return;
|
|
205
|
+
popup = doc.createElement('div');
|
|
206
|
+
popup.className = 'tw-palette-menu';
|
|
207
|
+
popup.setAttribute('role', 'menu');
|
|
208
|
+
popup.setAttribute('aria-label', menuButtonLabel(locale));
|
|
209
|
+
for (const item of items) {
|
|
210
|
+
if (item.separated && popup.childElementCount) {
|
|
211
|
+
const separator = doc.createElement('div');
|
|
212
|
+
separator.setAttribute('role', 'separator');
|
|
213
|
+
popup.append(separator);
|
|
214
|
+
}
|
|
215
|
+
const entry = doc.createElement('button');
|
|
216
|
+
entry.type = 'button';
|
|
217
|
+
entry.setAttribute('role', 'menuitem');
|
|
218
|
+
entry.textContent = item.label;
|
|
219
|
+
entry.addEventListener('click', event => {
|
|
220
|
+
event.stopPropagation();
|
|
221
|
+
closeMenu();
|
|
222
|
+
item.onSelect();
|
|
223
|
+
});
|
|
224
|
+
popup.append(entry);
|
|
225
|
+
}
|
|
226
|
+
const rect = button.getBoundingClientRect();
|
|
227
|
+
const view = doc.defaultView;
|
|
228
|
+
popup.style.position = 'fixed';
|
|
229
|
+
popup.style.top = `${rect.bottom + 4}px`;
|
|
230
|
+
popup.style.right = `${Math.max(4, (view?.innerWidth ?? rect.right) - rect.right)}px`;
|
|
231
|
+
doc.body.append(popup);
|
|
232
|
+
button.setAttribute('aria-expanded', 'true');
|
|
233
|
+
doc.addEventListener('pointerdown', outside, true);
|
|
234
|
+
doc.addEventListener('keydown', keys, true);
|
|
235
|
+
popup.querySelector('[role="menuitem"]')?.focus();
|
|
236
|
+
};
|
|
237
|
+
// The category menu reacts to presses on its items; this one only opens its own menu.
|
|
238
|
+
const press = (event) => event.stopPropagation();
|
|
239
|
+
button.addEventListener('mousedown', press);
|
|
240
|
+
button.addEventListener('pointerdown', press);
|
|
241
|
+
button.addEventListener('click', event => {
|
|
242
|
+
event.stopPropagation();
|
|
243
|
+
if (popup)
|
|
244
|
+
closeMenu();
|
|
245
|
+
else
|
|
246
|
+
openMenu();
|
|
247
|
+
});
|
|
248
|
+
menu.append(button);
|
|
249
|
+
return {
|
|
250
|
+
button,
|
|
251
|
+
/** Puts the button back at the end after the host rebuilds the menu, and follows the locale. */
|
|
252
|
+
update() {
|
|
253
|
+
label();
|
|
254
|
+
if (button.parentElement !== menu || menu.lastElementChild !== button)
|
|
255
|
+
menu.append(button);
|
|
256
|
+
},
|
|
257
|
+
close: () => closeMenu(),
|
|
258
|
+
dispose() {
|
|
259
|
+
closeMenu();
|
|
260
|
+
button.remove();
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ExtensionMetadata } from '@kubohiroya/turbowarp-extension-security-dialog';
|
|
3
|
+
import { type PaletteGroupSet } from './palette-category-visibility.ts';
|
|
4
|
+
export interface PaletteExtension {
|
|
5
|
+
/** The extension ID, which is also its palette category ID. */
|
|
6
|
+
id: string;
|
|
7
|
+
/** What the host knows about the extension; at least its name. */
|
|
8
|
+
metadata: ExtensionMetadata;
|
|
9
|
+
}
|
|
10
|
+
export interface PaletteVisibilityChoice {
|
|
11
|
+
/** The groups to show for the target. */
|
|
12
|
+
groups: string[];
|
|
13
|
+
/** Sets added in this dialog, to keep with the project. */
|
|
14
|
+
addedSets: PaletteGroupSet[];
|
|
15
|
+
}
|
|
16
|
+
export interface PaletteVisibilityDialogProps {
|
|
17
|
+
locale?: string;
|
|
18
|
+
targetName: string;
|
|
19
|
+
isStage: boolean;
|
|
20
|
+
/** The groups shown now. */
|
|
21
|
+
initial: ReadonlySet<string>;
|
|
22
|
+
/** How many blocks of each group the target's code uses. */
|
|
23
|
+
used: ReadonlyMap<string, number>;
|
|
24
|
+
extensions: readonly PaletteExtension[];
|
|
25
|
+
sets: readonly PaletteGroupSet[];
|
|
26
|
+
onApply(choice: PaletteVisibilityChoice): void;
|
|
27
|
+
onCancel(): void;
|
|
28
|
+
}
|
|
29
|
+
interface DialogState {
|
|
30
|
+
shown: Set<string>;
|
|
31
|
+
addedSets: PaletteGroupSet[];
|
|
32
|
+
naming: boolean;
|
|
33
|
+
setName: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The screen that chooses which block groups the palette shows for one sprite or the stage: sets that
|
|
37
|
+
* switch several groups at once, the standard groups, and one row per extension drawn the same way as the
|
|
38
|
+
* extension review.
|
|
39
|
+
*/
|
|
40
|
+
export declare class PaletteVisibilityDialog extends React.PureComponent<PaletteVisibilityDialogProps, DialogState> {
|
|
41
|
+
constructor(props: PaletteVisibilityDialogProps);
|
|
42
|
+
get text(): {
|
|
43
|
+
title: string;
|
|
44
|
+
sprite: (name: string) => string;
|
|
45
|
+
stage: string;
|
|
46
|
+
sets: string;
|
|
47
|
+
addSet: string;
|
|
48
|
+
setName: string;
|
|
49
|
+
add: string;
|
|
50
|
+
standard: string;
|
|
51
|
+
extensions: string;
|
|
52
|
+
used: (count: number) => string;
|
|
53
|
+
hiddenInUse: (count: number) => string;
|
|
54
|
+
cancel: string;
|
|
55
|
+
apply: string;
|
|
56
|
+
close: string;
|
|
57
|
+
};
|
|
58
|
+
standard(): ("sound" | "variables" | "events" | "motion" | "looks" | "control" | "sensing" | "operators" | "lists" | "myBlocks")[];
|
|
59
|
+
available(groups: readonly string[]): string[];
|
|
60
|
+
toggle(group: string): void;
|
|
61
|
+
toggleSet(set: PaletteGroupSet): void;
|
|
62
|
+
addSet(): void;
|
|
63
|
+
apply(): void;
|
|
64
|
+
render(): React.JSX.Element;
|
|
65
|
+
}
|
|
66
|
+
/** Opens the screen as a modal `<dialog>` in `document`. Returns the function that closes it. */
|
|
67
|
+
export declare function openPaletteVisibilityDialog(document: Document, props: PaletteVisibilityDialogProps): () => void;
|
|
68
|
+
export {};
|