@kubohiroya/turbowarp-plus-editor-code-views 0.12.0 → 0.14.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 +2 -1
- package/README.md +2 -1
- package/dist/palette-category-groups.d.ts +22 -0
- package/dist/palette-category-groups.js +36 -0
- package/dist/palette-category-visibility.d.ts +9 -2
- package/dist/palette-category-visibility.js +26 -4
- package/package.json +1 -1
- package/src/palette-category-groups.ts +47 -0
- package/src/palette-category-visibility.ts +32 -5
- package/src/styles.css +6 -0
package/README.ja.md
CHANGED
|
@@ -18,7 +18,7 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
18
18
|
| `./target-combobox` | コードの窓の対象の選択 |
|
|
19
19
|
| `./detached-block-palette` | 別窓のブロックパレット |
|
|
20
20
|
| `./palette-category-reorder`・`./palette-category-search`・`./palette-dropdown-resize` | パレットのカテゴリの並べ替えと検索、パレットのドロップダウンの大きさの変更 |
|
|
21
|
-
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`attachPaletteCategoryGroups(menu, options)`
|
|
21
|
+
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`prepareGroupedToolboxTree(tree, {catalog, visible})` は、機能拡張が宣言したグループの入れ子化と表示するグループの絞り込みを含めて、これを一度に行う。`hasCategoryGroups(tree)` は木が category を入れ子にしているかを返す。`attachPaletteCategoryGroups(menu, options)` で子ブロックパレットを開く。その `selectionTarget(id)` は、自分のブロックを持たないグループの代わりに選ぶ category を返し、`syncButtons(openId)` はグループのアイコンの状態を付け直す。`splitExtensionCategories(tree, catalog, {nested: true})` は、グループを宣言した機能拡張をこのグループにする |
|
|
22
22
|
| `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はエディターの画面状態として保存し、選ぶ画面を持つ |
|
|
23
23
|
| `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する。テキストの引数で Shift+Enter を押すと、改行を入れてダイアログで編集を続ける |
|
|
24
24
|
| `./block-number-input` | 数値の引数に入力した全角の数字・記号を半角にする |
|
|
@@ -92,6 +92,7 @@ getInfo() {
|
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
- 各グループはパレットのカテゴリ `shapes/geometry` になり、名前はグループの名前になる。設定もこの ID で記録する。`paletteGroup` のないブロック(宣言していないグループを指すものを含む)は、カテゴリ `shapes` に残る。
|
|
95
|
+
- グループの `id` は設定に記録する名前で、言語によって変わらない。`name` は利用者が読む表示名で、1つの文字列か、`{en: 'Speech\nand text', ja: 'セリフと\n文字'}` のような言語ごとの文字列を書く。エディターは、その言語の名前、同じ言語の名前、英語、最初の名前の順に選ぶ。`\n` は、カテゴリの一覧と設定の画面で改行になる(`resolveGroupName(name, locale)`。`extensionPaletteCatalog` に言語を渡す)。
|
|
95
96
|
- グループの `color1`・`color2`・`color3`・`menuIconURI` は省略でき、省略すると機能拡張のものを使う。ブロック自身の色はグループの色より優先する。
|
|
96
97
|
- 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
|
|
97
98
|
- ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ The code views of TurboWarp Plus Editor: code in tabs and windows that share one
|
|
|
19
19
|
| `./detached-block-palette` | The block palette in its own window |
|
|
20
20
|
| `./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 |
|
|
21
21
|
| `./palette-category-reorder`, `./palette-category-search`, `./palette-dropdown-resize` | Reordering and searching palette categories, and resizing palette dropdowns |
|
|
22
|
-
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `attachPaletteCategoryGroups(menu, options)` opens the child palettes. `splitExtensionCategories(tree, catalog, {nested: true})` makes an extension that declares groups such a group |
|
|
22
|
+
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `prepareGroupedToolboxTree(tree, {catalog, visible})` does both in one step, also nesting the groups extensions declare and keeping only the groups to show, and `hasCategoryGroups(tree)` tells whether a tree nests categories. `attachPaletteCategoryGroups(menu, options)` opens the child palettes; its `selectionTarget(id)` gives the category to select for a group without blocks of its own, and `syncButtons(openId)` marks the groups' icons. `splitExtensionCategories(tree, catalog, {nested: true})` makes an extension that declares groups such a group |
|
|
23
23
|
| `./block-argument-text` | Shortens long text arguments and edits them in a dialog. Shift+Enter in a text argument continues in the dialog with a line break |
|
|
24
24
|
| `./block-number-input` | Converts full-width digits and signs typed into number arguments to half-width |
|
|
25
25
|
| `./clean-up-comments` | Keeps comments with their scripts in "Clean up blocks": block comments keep their offset from the block, scripts with their comments do not cover one another, and workspace comments that cover a script move beside the scripts (`installCleanUpComments(ScratchBlocks)`) |
|
|
@@ -92,6 +92,7 @@ getInfo() {
|
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
- Each group is the palette category `shapes/geometry`, named after the group, and the settings name it by that ID. Blocks without `paletteGroup` (or naming a group not declared) stay in the category `shapes`.
|
|
95
|
+
- A group's `id` names it in the settings and stays the same in every language; its `name` is what people read. `name` is one text or one per locale, such as `{en: 'Speech\nand text', ja: 'セリフと\n文字'}`: the editor shows the one for its locale, then for its language, then English, then the first. A `\n` starts a new line in the category menu and the settings screen (`resolveGroupName(name, locale)`; pass the locale to `extensionPaletteCatalog`).
|
|
95
96
|
- `color1`, `color2`, `color3` and `menuIconURI` of a group are optional, and default to the extension's. A block's own colour wins over its group's.
|
|
96
97
|
- A label, separator or button goes with the block after it. The opcodes stay `shapes_circle` and so on, so the SB3 does not change: standard TurboWarp and Scratch ignore `groups` and `paletteGroup` and show one category.
|
|
97
98
|
- The host calls `installExtensionPaletteGroups(vm.runtime)` before extensions load, and passes `extensionPaletteCatalog(vm.runtime._blockInfo)` to the store, `filterToolboxTree` and `usedGroups`. The dialog takes the groups of such an extension as `groups` of its entry and shows a checkbox for each inside the extension's row.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PaletteGroupCatalog } from './palette-category-visibility.ts';
|
|
1
2
|
/**
|
|
2
3
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
4
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -26,6 +27,17 @@ export declare const flattenCategoryGroups: (tree: Element | null | undefined) =
|
|
|
26
27
|
* with neither members nor blocks of its own is removed with its members. Returns the groups that remain.
|
|
27
28
|
*/
|
|
28
29
|
export declare const filterGroupedToolboxTree: (tree: Element, groups: PaletteGroups, filter: (tree: Element) => void) => PaletteGroups;
|
|
30
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
31
|
+
export declare const hasCategoryGroups: (tree: Element | null | undefined) => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
34
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
35
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
36
|
+
*/
|
|
37
|
+
export declare const prepareGroupedToolboxTree: (tree: Element, { catalog, visible }?: {
|
|
38
|
+
catalog?: PaletteGroupCatalog;
|
|
39
|
+
visible?: ReadonlySet<string> | null;
|
|
40
|
+
}) => PaletteGroups;
|
|
29
41
|
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
30
42
|
export interface PaletteCategory {
|
|
31
43
|
id_: string;
|
|
@@ -55,6 +67,16 @@ export interface PaletteCategoryGroupsController {
|
|
|
55
67
|
isOpen(): boolean;
|
|
56
68
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
57
69
|
anchorOf(category: PaletteCategory): HTMLElement;
|
|
70
|
+
/**
|
|
71
|
+
* The category to select for an id: a group without blocks of its own stands for its first member, so selecting
|
|
72
|
+
* an extension that declares groups selects its first shown group.
|
|
73
|
+
*/
|
|
74
|
+
selectionTarget(id: string): string;
|
|
75
|
+
/**
|
|
76
|
+
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
77
|
+
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
78
|
+
*/
|
|
79
|
+
syncButtons(openId: string | null): void;
|
|
58
80
|
/** Whether the node is in a child palette. */
|
|
59
81
|
contains(node: Node | null): boolean;
|
|
60
82
|
close(): void;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { filterToolboxTree, splitExtensionCategories } from "./palette-category-visibility.js";
|
|
1
2
|
/**
|
|
2
3
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
4
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -107,6 +108,19 @@ export const filterGroupedToolboxTree = (tree, groups, filter) => {
|
|
|
107
108
|
}
|
|
108
109
|
return remaining;
|
|
109
110
|
};
|
|
111
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
112
|
+
export const hasCategoryGroups = (tree) => !!tree?.querySelector?.('category category');
|
|
113
|
+
/**
|
|
114
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
115
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
116
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
117
|
+
*/
|
|
118
|
+
export const prepareGroupedToolboxTree = (tree, { catalog, visible } = {}) => {
|
|
119
|
+
if (catalog)
|
|
120
|
+
splitExtensionCategories(tree, catalog, { nested: true });
|
|
121
|
+
const groups = flattenCategoryGroups(tree);
|
|
122
|
+
return visible ? filterGroupedToolboxTree(tree, groups, filtered => filterToolboxTree(filtered, visible)) : groups;
|
|
123
|
+
};
|
|
110
124
|
const selectedClass = 'categorySelected';
|
|
111
125
|
/** Makes clicking a group's icon in the palette menu open its child palette. */
|
|
112
126
|
export const attachPaletteCategoryGroups = (menu, options) => {
|
|
@@ -345,6 +359,28 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
345
359
|
toggle,
|
|
346
360
|
isOpen: () => stack.length > 0,
|
|
347
361
|
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
362
|
+
selectionTarget(id) {
|
|
363
|
+
let target = id;
|
|
364
|
+
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
365
|
+
target = groups.members.get(target)[0];
|
|
366
|
+
}
|
|
367
|
+
return target;
|
|
368
|
+
},
|
|
369
|
+
syncButtons(openId) {
|
|
370
|
+
const horizontal = options.horizontal();
|
|
371
|
+
for (const category of options.categories()) {
|
|
372
|
+
if (!groups.members.has(category.id_) || parentOf.has(category.id_))
|
|
373
|
+
continue;
|
|
374
|
+
const item = category.item_;
|
|
375
|
+
if (horizontal)
|
|
376
|
+
item.tabIndex = 0;
|
|
377
|
+
else
|
|
378
|
+
item.removeAttribute('tabindex');
|
|
379
|
+
item.setAttribute('role', 'button');
|
|
380
|
+
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
381
|
+
topLevelOf(openId) === category.id_);
|
|
382
|
+
}
|
|
383
|
+
},
|
|
348
384
|
contains,
|
|
349
385
|
close,
|
|
350
386
|
dispose() {
|
|
@@ -63,6 +63,13 @@ export interface PaletteCategoryInfo {
|
|
|
63
63
|
} | null;
|
|
64
64
|
}[];
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* A group's display name: one text, or one per locale such as `{en: 'Speech', ja: 'セリフ'}`. A line break (`\n`)
|
|
68
|
+
* starts a new line in the palette and the settings screen.
|
|
69
|
+
*/
|
|
70
|
+
export type GroupName = string | Readonly<Record<string, string>>;
|
|
71
|
+
/** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
|
|
72
|
+
export declare function resolveGroupName(name: GroupName, locale?: string): string;
|
|
66
73
|
/**
|
|
67
74
|
* Keeps the groups each extension declares on its category info, where {@link extensionPaletteCatalog} reads them,
|
|
68
75
|
* and draws the blocks of a group with a colour of its own in the group's colours. Install it before extensions load.
|
|
@@ -71,8 +78,8 @@ export interface PaletteCategoryInfo {
|
|
|
71
78
|
export declare function installExtensionPaletteGroups(runtime: {
|
|
72
79
|
_fillExtensionCategory?: (...args: any[]) => unknown;
|
|
73
80
|
}): () => void;
|
|
74
|
-
/** The groups of the loaded extensions, in palette order, from the VM's category info
|
|
75
|
-
export declare function extensionPaletteCatalog(categories?: readonly PaletteCategoryInfo[]): PaletteGroupCatalog;
|
|
81
|
+
/** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
|
|
82
|
+
export declare function extensionPaletteCatalog(categories?: readonly PaletteCategoryInfo[], locale?: string): PaletteGroupCatalog;
|
|
76
83
|
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
77
84
|
export declare function groupOfOpcode(opcode: string, catalog?: PaletteGroupCatalog): string | null;
|
|
78
85
|
type BlockLike = {
|
|
@@ -33,6 +33,28 @@ const standardPrefixes = {
|
|
|
33
33
|
motion: 'motion', looks: 'looks', sound: 'sound', event: 'events', control: 'control', sensing: 'sensing',
|
|
34
34
|
operator: 'operators', procedures: 'myBlocks', argument: 'myBlocks'
|
|
35
35
|
};
|
|
36
|
+
/** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
|
|
37
|
+
export function resolveGroupName(name, locale) {
|
|
38
|
+
if (typeof name === 'string')
|
|
39
|
+
return name;
|
|
40
|
+
const entries = Object.entries(name);
|
|
41
|
+
const wanted = (locale ?? '').toLowerCase();
|
|
42
|
+
const language = wanted.split(/[-_]/)[0];
|
|
43
|
+
return entries.find(([key]) => key.toLowerCase() === wanted)?.[1] ??
|
|
44
|
+
entries.find(([key]) => key.toLowerCase() === language)?.[1] ??
|
|
45
|
+
entries.find(([key]) => key.toLowerCase().split(/[-_]/)[0] === language && language)?.[1] ??
|
|
46
|
+
entries.find(([key]) => key.toLowerCase() === 'en')?.[1] ?? entries[0]?.[1] ?? '';
|
|
47
|
+
}
|
|
48
|
+
/** A name as `getInfo()` gives it, or nothing when it is neither a text nor texts by locale. */
|
|
49
|
+
function groupName(value) {
|
|
50
|
+
if (typeof value === 'string')
|
|
51
|
+
return value || undefined;
|
|
52
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
53
|
+
return undefined;
|
|
54
|
+
const names = Object.fromEntries(Object.entries(value)
|
|
55
|
+
.filter((entry) => typeof entry[1] === 'string' && entry[1] !== ''));
|
|
56
|
+
return Object.keys(names).length ? names : undefined;
|
|
57
|
+
}
|
|
36
58
|
const groupIdPattern = /^[A-Za-z0-9_-]+$/;
|
|
37
59
|
const optionalString = (value) => typeof value === 'string' && value ? value : undefined;
|
|
38
60
|
/** The groups `getInfo()` declares, leaving out any that are not well formed or repeat an ID. */
|
|
@@ -47,7 +69,7 @@ function declaredGroups(value) {
|
|
|
47
69
|
if (typeof id !== 'string' || !groupIdPattern.test(id) || groups.some(group => group.id === id))
|
|
48
70
|
continue;
|
|
49
71
|
const entry = item;
|
|
50
|
-
groups.push({ id, name:
|
|
72
|
+
groups.push({ id, name: groupName(name) ?? id, color1: optionalString(entry.color1),
|
|
51
73
|
color2: optionalString(entry.color2), color3: optionalString(entry.color3),
|
|
52
74
|
menuIconURI: optionalString(entry.menuIconURI) });
|
|
53
75
|
}
|
|
@@ -86,8 +108,8 @@ export function installExtensionPaletteGroups(runtime) {
|
|
|
86
108
|
runtime._fillExtensionCategory = original;
|
|
87
109
|
};
|
|
88
110
|
}
|
|
89
|
-
/** The groups of the loaded extensions, in palette order, from the VM's category info
|
|
90
|
-
export function extensionPaletteCatalog(categories = []) {
|
|
111
|
+
/** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
|
|
112
|
+
export function extensionPaletteCatalog(categories = [], locale) {
|
|
91
113
|
const groups = [];
|
|
92
114
|
const groupOf = new Map();
|
|
93
115
|
for (const category of categories) {
|
|
@@ -115,7 +137,7 @@ export function extensionPaletteCatalog(categories = []) {
|
|
|
115
137
|
for (const group of declared) {
|
|
116
138
|
if (!used.has(group.id))
|
|
117
139
|
continue;
|
|
118
|
-
groups.push({ id: `${category.id}/${group.id}`, extensionId: category.id, name: group.name, extensionName,
|
|
140
|
+
groups.push({ id: `${category.id}/${group.id}`, extensionId: category.id, name: resolveGroupName(group.name, locale), extensionName,
|
|
119
141
|
declared: true, color1: group.color1 ?? category.color1, color2: group.color2 ?? category.color2,
|
|
120
142
|
menuIconURI: group.menuIconURI ?? menuIconURI });
|
|
121
143
|
}
|
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.14.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",
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {filterToolboxTree, splitExtensionCategories} from './palette-category-visibility.ts';
|
|
2
|
+
import type {PaletteGroupCatalog} from './palette-category-visibility.ts';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
6
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -108,6 +111,21 @@ export const filterGroupedToolboxTree = (tree: Element, groups: PaletteGroups, f
|
|
|
108
111
|
return remaining;
|
|
109
112
|
};
|
|
110
113
|
|
|
114
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
115
|
+
export const hasCategoryGroups = (tree: Element | null | undefined): boolean => !!tree?.querySelector?.('category category');
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
119
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
120
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
121
|
+
*/
|
|
122
|
+
export const prepareGroupedToolboxTree = (tree: Element,
|
|
123
|
+
{catalog, visible}: {catalog?: PaletteGroupCatalog; visible?: ReadonlySet<string> | null} = {}): PaletteGroups => {
|
|
124
|
+
if (catalog) splitExtensionCategories(tree, catalog, {nested: true});
|
|
125
|
+
const groups = flattenCategoryGroups(tree);
|
|
126
|
+
return visible ? filterGroupedToolboxTree(tree, groups, filtered => filterToolboxTree(filtered, visible)) : groups;
|
|
127
|
+
};
|
|
128
|
+
|
|
111
129
|
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
112
130
|
export interface PaletteCategory {
|
|
113
131
|
id_: string;
|
|
@@ -139,6 +157,16 @@ export interface PaletteCategoryGroupsController {
|
|
|
139
157
|
isOpen(): boolean;
|
|
140
158
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
141
159
|
anchorOf(category: PaletteCategory): HTMLElement;
|
|
160
|
+
/**
|
|
161
|
+
* The category to select for an id: a group without blocks of its own stands for its first member, so selecting
|
|
162
|
+
* an extension that declares groups selects its first shown group.
|
|
163
|
+
*/
|
|
164
|
+
selectionTarget(id: string): string;
|
|
165
|
+
/**
|
|
166
|
+
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
167
|
+
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
168
|
+
*/
|
|
169
|
+
syncButtons(openId: string | null): void;
|
|
142
170
|
/** Whether the node is in a child palette. */
|
|
143
171
|
contains(node: Node | null): boolean;
|
|
144
172
|
close(): void;
|
|
@@ -368,6 +396,25 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
368
396
|
toggle,
|
|
369
397
|
isOpen: () => stack.length > 0,
|
|
370
398
|
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
399
|
+
selectionTarget(id) {
|
|
400
|
+
let target = id;
|
|
401
|
+
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
402
|
+
target = groups.members.get(target)![0];
|
|
403
|
+
}
|
|
404
|
+
return target;
|
|
405
|
+
},
|
|
406
|
+
syncButtons(openId) {
|
|
407
|
+
const horizontal = options.horizontal();
|
|
408
|
+
for (const category of options.categories()) {
|
|
409
|
+
if (!groups.members.has(category.id_) || parentOf.has(category.id_)) continue;
|
|
410
|
+
const item = category.item_;
|
|
411
|
+
if (horizontal) item.tabIndex = 0;
|
|
412
|
+
else item.removeAttribute('tabindex');
|
|
413
|
+
item.setAttribute('role', 'button');
|
|
414
|
+
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
415
|
+
topLevelOf(openId) === category.id_);
|
|
416
|
+
}
|
|
417
|
+
},
|
|
371
418
|
contains,
|
|
372
419
|
close,
|
|
373
420
|
dispose() {
|
|
@@ -79,7 +79,7 @@ export interface PaletteGroupCatalog {
|
|
|
79
79
|
/** A declared group as kept on the VM's category info. */
|
|
80
80
|
interface DeclaredGroup {
|
|
81
81
|
id: string;
|
|
82
|
-
name:
|
|
82
|
+
name: GroupName;
|
|
83
83
|
color1?: string;
|
|
84
84
|
color2?: string;
|
|
85
85
|
color3?: string;
|
|
@@ -98,6 +98,33 @@ export interface PaletteCategoryInfo {
|
|
|
98
98
|
blocks?: readonly {info?: {paletteGroup?: unknown}; json?: {type?: unknown} | null}[];
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A group's display name: one text, or one per locale such as `{en: 'Speech', ja: 'セリフ'}`. A line break (`\n`)
|
|
103
|
+
* starts a new line in the palette and the settings screen.
|
|
104
|
+
*/
|
|
105
|
+
export type GroupName = string | Readonly<Record<string, string>>;
|
|
106
|
+
|
|
107
|
+
/** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
|
|
108
|
+
export function resolveGroupName(name: GroupName, locale?: string): string {
|
|
109
|
+
if (typeof name === 'string') return name;
|
|
110
|
+
const entries = Object.entries(name);
|
|
111
|
+
const wanted = (locale ?? '').toLowerCase();
|
|
112
|
+
const language = wanted.split(/[-_]/)[0];
|
|
113
|
+
return entries.find(([key]) => key.toLowerCase() === wanted)?.[1] ??
|
|
114
|
+
entries.find(([key]) => key.toLowerCase() === language)?.[1] ??
|
|
115
|
+
entries.find(([key]) => key.toLowerCase().split(/[-_]/)[0] === language && language)?.[1] ??
|
|
116
|
+
entries.find(([key]) => key.toLowerCase() === 'en')?.[1] ?? entries[0]?.[1] ?? '';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A name as `getInfo()` gives it, or nothing when it is neither a text nor texts by locale. */
|
|
120
|
+
function groupName(value: unknown): GroupName | undefined {
|
|
121
|
+
if (typeof value === 'string') return value || undefined;
|
|
122
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
|
|
123
|
+
const names = Object.fromEntries(Object.entries(value as Record<string, unknown>)
|
|
124
|
+
.filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1] !== ''));
|
|
125
|
+
return Object.keys(names).length ? names : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
101
128
|
const groupIdPattern = /^[A-Za-z0-9_-]+$/;
|
|
102
129
|
const optionalString = (value: unknown) => typeof value === 'string' && value ? value : undefined;
|
|
103
130
|
/** The groups `getInfo()` declares, leaving out any that are not well formed or repeat an ID. */
|
|
@@ -109,7 +136,7 @@ function declaredGroups(value: unknown): DeclaredGroup[] {
|
|
|
109
136
|
const {id, name} = item as Record<string, unknown>;
|
|
110
137
|
if (typeof id !== 'string' || !groupIdPattern.test(id) || groups.some(group => group.id === id)) continue;
|
|
111
138
|
const entry = item as Record<string, unknown>;
|
|
112
|
-
groups.push({id, name:
|
|
139
|
+
groups.push({id, name: groupName(name) ?? id, color1: optionalString(entry.color1),
|
|
113
140
|
color2: optionalString(entry.color2), color3: optionalString(entry.color3),
|
|
114
141
|
menuIconURI: optionalString(entry.menuIconURI)});
|
|
115
142
|
}
|
|
@@ -145,8 +172,8 @@ export function installExtensionPaletteGroups(runtime: {_fillExtensionCategory?:
|
|
|
145
172
|
};
|
|
146
173
|
}
|
|
147
174
|
|
|
148
|
-
/** The groups of the loaded extensions, in palette order, from the VM's category info
|
|
149
|
-
export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo[] = []): PaletteGroupCatalog {
|
|
175
|
+
/** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
|
|
176
|
+
export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo[] = [], locale?: string): PaletteGroupCatalog {
|
|
150
177
|
const groups: ExtensionPaletteGroup[] = [];
|
|
151
178
|
const groupOf = new Map<string, string>();
|
|
152
179
|
for (const category of categories) {
|
|
@@ -172,7 +199,7 @@ export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo
|
|
|
172
199
|
}
|
|
173
200
|
for (const group of declared) {
|
|
174
201
|
if (!used.has(group.id)) continue;
|
|
175
|
-
groups.push({id: `${category.id}/${group.id}`, extensionId: category.id, name: group.name, extensionName,
|
|
202
|
+
groups.push({id: `${category.id}/${group.id}`, extensionId: category.id, name: resolveGroupName(group.name, locale), extensionName,
|
|
176
203
|
declared: true, color1: group.color1 ?? category.color1, color2: group.color2 ?? category.color2,
|
|
177
204
|
menuIconURI: group.menuIconURI ?? menuIconURI});
|
|
178
205
|
}
|
package/src/styles.css
CHANGED
|
@@ -766,6 +766,12 @@
|
|
|
766
766
|
gap: 0.35rem;
|
|
767
767
|
}
|
|
768
768
|
|
|
769
|
+
/* A group name may hold line breaks, in the palette's category menu and in the settings screen. */
|
|
770
|
+
.scratchCategoryMenuItemLabel,
|
|
771
|
+
.tw-palette-visibility-extension-groups label {
|
|
772
|
+
white-space: pre-line;
|
|
773
|
+
}
|
|
774
|
+
|
|
769
775
|
/* The groups one extension declares, chosen one by one inside its row. */
|
|
770
776
|
.tw-palette-visibility-extension-groups {
|
|
771
777
|
display: grid;
|