@kubohiroya/turbowarp-plus-editor-code-views 0.2.1 → 0.4.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
@@ -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 では `tw-plus/workspace.json`、作品フォルダでは `.tw-plus/workspace.json` の `blockPalette` のスライスになる。スプライトはタブの配置と同じく名前で、背景は `stage` で記録する。
50
+ - 設定のないスプライト・背景は、標準のグループと、そのコードが使っている機能拡張のグループを出す。
51
+ - 非表示にしても、コード領域のブロックはそのまま見えて編集できる。
52
+
53
+ `./palette-category-visibility`
54
+
55
+ | 関数 | 内容 |
56
+ | --- | --- |
57
+ | `createPaletteVisibilityStore()` | 1つの作品の設定を、実行時の対象の ID ごとに持つ。名前を変えたスプライトも設定を保つ。`visible`・`set`・`copy`(複製)・`sets`・`addSets`、画面状態のスライスへの `capture(targets)` と `apply(value, targets)`、`reset` |
58
+ | `paletteScreenStateKey` | 画面状態のスライスの名前(`blockPalette`) |
59
+ | `filterToolboxTree(tree, visible)` | パレットを描く前のツールボックスの木から、出さないカテゴリを外す |
60
+ | `usedGroups(target)`・`groupOfOpcode(opcode)`・`defaultVisibleGroups(target, extensionIds)` | コードが使っているグループとその数、設定のない対象に出すグループ |
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/turbowarp-extension-security-dialog`(0.9.0 以上)を入れ、その `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 saved with the editor's screen state, 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 belongs to the editor, not to the project's code, and is saved with the same screen state as the tab layout: the `blockPalette` slice of `tw-plus/workspace.json` in an SB3, or `.tw-plus/workspace.json` in a project folder. Sprites are recorded by name, as the tab layout records them, and the stage as `stage`.
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
+ | `createPaletteVisibilityStore()` | One project's settings, by runtime target ID, so a renamed sprite keeps its setting: `visible`, `set`, `copy` (for a duplicate), `sets`, `addSets`, `capture(targets)` and `apply(value, targets)` for the screen-state slice, and `reset` |
58
+ | `paletteScreenStateKey` | The name of the screen-state slice (`blockPalette`) |
59
+ | `filterToolboxTree(tree, visible)` | Removes the categories a target does not show from the parsed toolbox before the palette is drawn |
60
+ | `usedGroups(target)`, `groupOfOpcode(opcode)`, `defaultVisibleGroups(target, extensionIds)` | The groups the code uses and how many blocks of each, and the groups shown for a target without a setting |
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/turbowarp-extension-security-dialog` (0.9.0 or later) and loads its `styles.css`, and registers the store as a screen-state slice. Choosing groups inside one extension comes once the VM can make a category per group.
67
+
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Which block groups the palette shows for each sprite and the stage.
3
+ *
4
+ * The settings belong to the editor, not to the project's code: they are kept in memory per target and saved with
5
+ * the editor's own screen state (a slice of `tw-plus/workspace.json` in an SB3, `.tw-plus/workspace.json` in a
6
+ * project folder), the same way the tab layout is. Hiding a group 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
+ /** A named set of groups the project keeps. */
12
+ export interface SavedGroupSet {
13
+ name: string;
14
+ groups: string[];
15
+ }
16
+ export interface PaletteGroupSet {
17
+ id: string;
18
+ name: string;
19
+ groups: readonly string[];
20
+ }
21
+ /** The sets every project has, then the project's own sets. */
22
+ export declare function paletteGroupSets(locale?: string, projectSets?: Record<string, SavedGroupSet>): PaletteGroupSet[];
23
+ /** The group a block belongs to, or null for a block no palette group offers. */
24
+ export declare function groupOfOpcode(opcode: string): string | null;
25
+ type BlockLike = {
26
+ opcode?: string;
27
+ shadow?: boolean;
28
+ };
29
+ /** The parts of a VM target this module reads. */
30
+ export interface PaletteTarget {
31
+ id: string;
32
+ isStage: boolean;
33
+ getName(): string;
34
+ blocks?: {
35
+ _blocks?: Record<string, BlockLike>;
36
+ };
37
+ }
38
+ /** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
39
+ export declare function usedGroups(target: Pick<PaletteTarget, 'blocks'>): Map<string, number>;
40
+ /**
41
+ * The groups shown when a target has no setting of its own: every standard group, and the groups of
42
+ * the extensions its code already uses, so a project opens with nothing missing.
43
+ */
44
+ export declare function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks'>, extensionIds: readonly string[]): string[];
45
+ /** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
46
+ export declare function filterToolboxTree(tree: Element | null | undefined, visible: ReadonlySet<string>): void;
47
+ /** The screen-state slice the settings are saved as. Sprites are named as the tab layout names them. */
48
+ export declare const paletteScreenStateKey = "blockPalette";
49
+ export interface SavedPaletteVisibility {
50
+ version: 1;
51
+ stage?: {
52
+ groups: string[];
53
+ };
54
+ sprites?: Record<string, {
55
+ groups: string[];
56
+ }>;
57
+ sets?: Record<string, SavedGroupSet>;
58
+ }
59
+ export interface PaletteVisibilityStore {
60
+ /** The groups the palette shows for the target. */
61
+ visible(target: PaletteTarget, extensionIds: readonly string[]): Set<string>;
62
+ /** Keeps the target's groups. Groups equal to its default leave it without a setting of its own. */
63
+ set(target: PaletteTarget, groups: readonly string[], extensionIds: readonly string[]): void;
64
+ /** A new sprite made from another, such as a duplicate, starts with its setting. */
65
+ copy(from: Pick<PaletteTarget, 'id'>, to: Pick<PaletteTarget, 'id'>): void;
66
+ /** The project's own sets. */
67
+ sets(): Record<string, SavedGroupSet>;
68
+ addSets(sets: readonly PaletteGroupSet[]): void;
69
+ /** The slice to save, or nothing when no target has a setting and the project has no sets. */
70
+ capture(targets: readonly PaletteTarget[]): SavedPaletteVisibility | undefined;
71
+ /** Takes the settings of a loaded project. A value that is not a saved slice leaves every target at its default. */
72
+ apply(value: unknown, targets: readonly PaletteTarget[]): void;
73
+ /** Forgets every setting, for a project without any. */
74
+ reset(): void;
75
+ }
76
+ /** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
77
+ export declare function createPaletteVisibilityStore(): PaletteVisibilityStore;
78
+ /** The menu item that opens the group settings. */
79
+ export declare const paletteVisibilityMenuLabel: (locale: string) => "ブロックグループの表示・非表示の設定" | "Show or hide block groups";
80
+ export interface PaletteMenuItem {
81
+ label: string;
82
+ onSelect(): void;
83
+ /** Draws a separator above the item. */
84
+ separated?: boolean;
85
+ }
86
+ /**
87
+ * Adds a ▼ button at the end of the palette's category menu: after the categories, so it sits at the
88
+ * right end of a horizontal palette and at the bottom of a vertical one. Pressing it lists the items
89
+ * the host gives — the group settings and the host's extension items — and runs the one chosen.
90
+ */
91
+ export declare function attachPaletteMenuButton(menu: HTMLElement, getLocale: () => string, getItems: (locale: string) => readonly PaletteMenuItem[]): {
92
+ button: HTMLButtonElement;
93
+ /** Puts the button back at the end after the host rebuilds the menu, and follows the locale. */
94
+ update(): void;
95
+ close: () => void;
96
+ dispose(): void;
97
+ };
98
+ export {};
@@ -0,0 +1,269 @@
1
+ /**
2
+ * Which block groups the palette shows for each sprite and the stage.
3
+ *
4
+ * The settings belong to the editor, not to the project's code: they are kept in memory per target and saved with
5
+ * the editor's own screen state (a slice of `tw-plus/workspace.json` in an SB3, `.tw-plus/workspace.json` in a
6
+ * project folder), the same way the tab layout is. Hiding a group 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. */
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
+ /**
59
+ * The groups shown when a target has no setting of its own: every standard group, and the groups of
60
+ * the extensions its code already uses, so a project opens with nothing missing.
61
+ */
62
+ export function defaultVisibleGroups(target, extensionIds) {
63
+ const used = usedGroups(target);
64
+ return [...standardGroups, ...extensionIds.filter(id => used.has(id))];
65
+ }
66
+ /** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
67
+ export function filterToolboxTree(tree, visible) {
68
+ if (!tree)
69
+ return;
70
+ for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
71
+ const id = category.getAttribute('id');
72
+ if (id && !visible.has(id))
73
+ category.remove();
74
+ }
75
+ }
76
+ /** The screen-state slice the settings are saved as. Sprites are named as the tab layout names them. */
77
+ export const paletteScreenStateKey = 'blockPalette';
78
+ const strings = (value) => Array.isArray(value) && value.every(item => typeof item === 'string') ? [...value] : undefined;
79
+ const sameGroups = (left, right) => left.length === right.length && new Set(left).size === left.length && left.every(group => right.includes(group));
80
+ /** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
81
+ export function createPaletteVisibilityStore() {
82
+ let groups = new Map();
83
+ let projectSets = {};
84
+ return {
85
+ visible(target, extensionIds) {
86
+ return new Set(groups.get(target.id) ?? defaultVisibleGroups(target, extensionIds));
87
+ },
88
+ set(target, chosen, extensionIds) {
89
+ if (sameGroups(chosen, defaultVisibleGroups(target, extensionIds)))
90
+ groups.delete(target.id);
91
+ else
92
+ groups.set(target.id, [...chosen]);
93
+ },
94
+ copy(from, to) {
95
+ const chosen = groups.get(from.id);
96
+ if (chosen)
97
+ groups.set(to.id, [...chosen]);
98
+ },
99
+ sets: () => ({ ...projectSets }),
100
+ addSets(sets) {
101
+ for (const set of sets)
102
+ projectSets[set.id] = { name: set.name, groups: [...set.groups] };
103
+ },
104
+ capture(targets) {
105
+ const saved = { version: 1 };
106
+ const sprites = {};
107
+ for (const target of targets) {
108
+ const chosen = groups.get(target.id);
109
+ if (!chosen)
110
+ continue;
111
+ if (target.isStage)
112
+ saved.stage = { groups: [...chosen] };
113
+ else
114
+ sprites[target.getName()] = { groups: [...chosen] };
115
+ }
116
+ if (Object.keys(sprites).length)
117
+ saved.sprites = sprites;
118
+ if (Object.keys(projectSets).length)
119
+ saved.sets = structuredClone(projectSets);
120
+ return saved.stage || saved.sprites || saved.sets ? saved : undefined;
121
+ },
122
+ apply(value, targets) {
123
+ groups = new Map();
124
+ projectSets = {};
125
+ if (!value || typeof value !== 'object' || value.version !== 1)
126
+ return;
127
+ const saved = value;
128
+ const setting = (entry) => entry && typeof entry === 'object' ?
129
+ strings(entry.groups) : undefined;
130
+ const sprites = saved.sprites && typeof saved.sprites === 'object' ? saved.sprites : {};
131
+ for (const target of targets) {
132
+ const chosen = target.isStage ? setting(saved.stage) :
133
+ Object.hasOwn(sprites, target.getName()) ? setting(sprites[target.getName()]) : undefined;
134
+ if (chosen)
135
+ groups.set(target.id, chosen);
136
+ }
137
+ if (saved.sets && typeof saved.sets === 'object') {
138
+ for (const [id, set] of Object.entries(saved.sets)) {
139
+ const name = set && typeof set === 'object' ? set.name : undefined;
140
+ const members = setting(set);
141
+ if (typeof name === 'string' && members)
142
+ projectSets[id] = { name, groups: members };
143
+ }
144
+ }
145
+ },
146
+ reset() {
147
+ groups = new Map();
148
+ projectSets = {};
149
+ }
150
+ };
151
+ }
152
+ /** The menu item that opens the group settings. */
153
+ export const paletteVisibilityMenuLabel = (locale) => japanese(locale) ? 'ブロックグループの表示・非表示の設定' :
154
+ 'Show or hide block groups';
155
+ const menuButtonLabel = (locale) => japanese(locale) ? 'パレットのメニュー' : 'Palette menu';
156
+ /**
157
+ * Adds a ▼ button at the end of the palette's category menu: after the categories, so it sits at the
158
+ * right end of a horizontal palette and at the bottom of a vertical one. Pressing it lists the items
159
+ * the host gives — the group settings and the host's extension items — and runs the one chosen.
160
+ */
161
+ export function attachPaletteMenuButton(menu, getLocale, getItems) {
162
+ const doc = menu.ownerDocument;
163
+ const button = doc.createElement('button');
164
+ button.type = 'button';
165
+ button.className = 'tw-palette-menu-button';
166
+ button.textContent = '▼';
167
+ button.setAttribute('aria-haspopup', 'menu');
168
+ button.setAttribute('aria-expanded', 'false');
169
+ const label = () => {
170
+ const text = menuButtonLabel(getLocale());
171
+ button.setAttribute('aria-label', text);
172
+ button.title = text;
173
+ };
174
+ label();
175
+ let popup = null;
176
+ const closeMenu = (focusButton = false) => {
177
+ if (!popup)
178
+ return;
179
+ popup.remove();
180
+ popup = null;
181
+ button.setAttribute('aria-expanded', 'false');
182
+ doc.removeEventListener('pointerdown', outside, true);
183
+ doc.removeEventListener('keydown', keys, true);
184
+ if (focusButton)
185
+ button.focus();
186
+ };
187
+ const outside = (event) => {
188
+ if (popup && !popup.contains(event.target) && event.target !== button)
189
+ closeMenu();
190
+ };
191
+ const keys = (event) => {
192
+ if (!popup)
193
+ return;
194
+ const items = Array.from(popup.querySelectorAll('[role="menuitem"]'));
195
+ const index = items.indexOf(doc.activeElement);
196
+ if (event.key === 'Escape') {
197
+ event.preventDefault();
198
+ closeMenu(true);
199
+ }
200
+ else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
201
+ event.preventDefault();
202
+ const step = event.key === 'ArrowDown' ? 1 : -1;
203
+ items[(index + step + items.length) % items.length]?.focus();
204
+ }
205
+ };
206
+ const openMenu = () => {
207
+ const locale = getLocale();
208
+ const items = getItems(locale);
209
+ if (!items.length)
210
+ return;
211
+ popup = doc.createElement('div');
212
+ popup.className = 'tw-palette-menu';
213
+ popup.setAttribute('role', 'menu');
214
+ popup.setAttribute('aria-label', menuButtonLabel(locale));
215
+ for (const item of items) {
216
+ if (item.separated && popup.childElementCount) {
217
+ const separator = doc.createElement('div');
218
+ separator.setAttribute('role', 'separator');
219
+ popup.append(separator);
220
+ }
221
+ const entry = doc.createElement('button');
222
+ entry.type = 'button';
223
+ entry.setAttribute('role', 'menuitem');
224
+ entry.textContent = item.label;
225
+ entry.addEventListener('click', event => {
226
+ event.stopPropagation();
227
+ closeMenu();
228
+ item.onSelect();
229
+ });
230
+ popup.append(entry);
231
+ }
232
+ const rect = button.getBoundingClientRect();
233
+ const view = doc.defaultView;
234
+ popup.style.position = 'fixed';
235
+ popup.style.top = `${rect.bottom + 4}px`;
236
+ popup.style.right = `${Math.max(4, (view?.innerWidth ?? rect.right) - rect.right)}px`;
237
+ doc.body.append(popup);
238
+ button.setAttribute('aria-expanded', 'true');
239
+ doc.addEventListener('pointerdown', outside, true);
240
+ doc.addEventListener('keydown', keys, true);
241
+ popup.querySelector('[role="menuitem"]')?.focus();
242
+ };
243
+ // The category menu reacts to presses on its items; this one only opens its own menu.
244
+ const press = (event) => event.stopPropagation();
245
+ button.addEventListener('mousedown', press);
246
+ button.addEventListener('pointerdown', press);
247
+ button.addEventListener('click', event => {
248
+ event.stopPropagation();
249
+ if (popup)
250
+ closeMenu();
251
+ else
252
+ openMenu();
253
+ });
254
+ menu.append(button);
255
+ return {
256
+ button,
257
+ /** Puts the button back at the end after the host rebuilds the menu, and follows the locale. */
258
+ update() {
259
+ label();
260
+ if (button.parentElement !== menu || menu.lastElementChild !== button)
261
+ menu.append(button);
262
+ },
263
+ close: () => closeMenu(),
264
+ dispose() {
265
+ closeMenu();
266
+ button.remove();
267
+ }
268
+ };
269
+ }
@@ -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 {};