@kubohiroya/turbowarp-plus-editor-code-views 0.11.0 → 0.13.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 -0
- package/README.md +2 -0
- package/dist/palette-category-groups.d.ts +64 -0
- package/dist/palette-category-groups.js +355 -0
- package/dist/palette-category-visibility.d.ts +14 -3
- package/dist/palette-category-visibility.js +56 -15
- package/package.json +5 -1
- package/src/palette-category-groups.ts +378 -0
- package/src/palette-category-visibility.ts +60 -13
- package/src/styles.css +63 -0
package/README.ja.md
CHANGED
|
@@ -18,6 +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)` で子ブロックパレットを開く。`splitExtensionCategories(tree, catalog, {nested: true})` は、グループを宣言した機能拡張をこのグループにする |
|
|
21
22
|
| `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はエディターの画面状態として保存し、選ぶ画面を持つ |
|
|
22
23
|
| `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する。テキストの引数で Shift+Enter を押すと、改行を入れてダイアログで編集を続ける |
|
|
23
24
|
| `./block-number-input` | 数値の引数に入力した全角の数字・記号を半角にする |
|
|
@@ -91,6 +92,7 @@ getInfo() {
|
|
|
91
92
|
```
|
|
92
93
|
|
|
93
94
|
- 各グループはパレットのカテゴリ `shapes/geometry` になり、名前はグループの名前になる。設定もこの ID で記録する。`paletteGroup` のないブロック(宣言していないグループを指すものを含む)は、カテゴリ `shapes` に残る。
|
|
95
|
+
- グループの `id` は設定に記録する名前で、言語によって変わらない。`name` は利用者が読む表示名で、1つの文字列か、`{en: 'Speech\nand text', ja: 'セリフと\n文字'}` のような言語ごとの文字列を書く。エディターは、その言語の名前、同じ言語の名前、英語、最初の名前の順に選ぶ。`\n` は、カテゴリの一覧と設定の画面で改行になる(`resolveGroupName(name, locale)`。`extensionPaletteCatalog` に言語を渡す)。
|
|
94
96
|
- グループの `color1`・`color2`・`color3`・`menuIconURI` は省略でき、省略すると機能拡張のものを使う。ブロック自身の色はグループの色より優先する。
|
|
95
97
|
- 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
|
|
96
98
|
- ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
|
package/README.md
CHANGED
|
@@ -19,6 +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
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 |
|
|
23
24
|
| `./block-number-input` | Converts full-width digits and signs typed into number arguments to half-width |
|
|
24
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)`) |
|
|
@@ -91,6 +92,7 @@ getInfo() {
|
|
|
91
92
|
```
|
|
92
93
|
|
|
93
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`).
|
|
94
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.
|
|
95
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.
|
|
96
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.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
|
+
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
4
|
+
* each nested category becomes a top-level one right after its group, and the menu hides it. Clicking the
|
|
5
|
+
* group's icon then shows its members in a child palette laid out like the palette it opens from — a row to the
|
|
6
|
+
* right of the icon for a horizontal palette, a column below it for a vertical one — and a member that is itself
|
|
7
|
+
* a group opens the next level the same way. A click outside the child palettes closes them.
|
|
8
|
+
*/
|
|
9
|
+
/** The attribute a flattened category keeps with the id of the group it was nested in. */
|
|
10
|
+
export declare const groupParentAttribute = "palette-group-parent";
|
|
11
|
+
export interface PaletteGroups {
|
|
12
|
+
/** Each group's members, in order, by category id. A member may itself be a group. */
|
|
13
|
+
members: Map<string, string[]>;
|
|
14
|
+
/** The groups that also hold blocks of their own, which their child palette offers first. */
|
|
15
|
+
withOwnContents: Set<string>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Moves every nested category of the toolbox tree to the top level, right after the group that held it, depth
|
|
19
|
+
* first, so Scratch Blocks makes a category of each. Returns the groups found. A nested category without an id
|
|
20
|
+
* gets one from its group's id and its position.
|
|
21
|
+
*/
|
|
22
|
+
export declare const flattenCategoryGroups: (tree: Element | null | undefined) => PaletteGroups;
|
|
23
|
+
/**
|
|
24
|
+
* Runs a filter that removes top-level categories by id, such as the palette's group settings, on a flattened tree.
|
|
25
|
+
* A group the filter removes still stays, without blocks of its own, while one of its members stays; a group left
|
|
26
|
+
* with neither members nor blocks of its own is removed with its members. Returns the groups that remain.
|
|
27
|
+
*/
|
|
28
|
+
export declare const filterGroupedToolboxTree: (tree: Element, groups: PaletteGroups, filter: (tree: Element) => void) => PaletteGroups;
|
|
29
|
+
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
30
|
+
export interface PaletteCategory {
|
|
31
|
+
id_: string;
|
|
32
|
+
name_: string;
|
|
33
|
+
item_: HTMLElement;
|
|
34
|
+
setSelected(selected: boolean): void;
|
|
35
|
+
}
|
|
36
|
+
export interface PaletteCategoryGroupsOptions {
|
|
37
|
+
/** The toolbox's categories, top level and flattened members alike. */
|
|
38
|
+
categories(): PaletteCategory[];
|
|
39
|
+
/** Whether the palette is laid out in a row. */
|
|
40
|
+
horizontal(): boolean;
|
|
41
|
+
rtl?: boolean;
|
|
42
|
+
/** Opens a category that is not a group, as clicking its icon in the palette would. */
|
|
43
|
+
choose(category: PaletteCategory): void;
|
|
44
|
+
/** Called when a child palette opens or the last one closes. */
|
|
45
|
+
onChange?(): void;
|
|
46
|
+
}
|
|
47
|
+
export interface PaletteCategoryGroupsController {
|
|
48
|
+
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
49
|
+
update(groups: PaletteGroups): void;
|
|
50
|
+
isGroup(id: string): boolean;
|
|
51
|
+
/** The id of the outermost group the category is in, or its own id when it is in none. */
|
|
52
|
+
topLevelId(id: string): string;
|
|
53
|
+
/** Opens the group's child palette from its icon, or closes it if it is open. */
|
|
54
|
+
toggle(category: PaletteCategory, focus?: boolean): void;
|
|
55
|
+
isOpen(): boolean;
|
|
56
|
+
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
57
|
+
anchorOf(category: PaletteCategory): HTMLElement;
|
|
58
|
+
/** Whether the node is in a child palette. */
|
|
59
|
+
contains(node: Node | null): boolean;
|
|
60
|
+
close(): void;
|
|
61
|
+
dispose(): void;
|
|
62
|
+
}
|
|
63
|
+
/** Makes clicking a group's icon in the palette menu open its child palette. */
|
|
64
|
+
export declare const attachPaletteCategoryGroups: (menu: HTMLElement, options: PaletteCategoryGroupsOptions) => PaletteCategoryGroupsController;
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
|
+
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
4
|
+
* each nested category becomes a top-level one right after its group, and the menu hides it. Clicking the
|
|
5
|
+
* group's icon then shows its members in a child palette laid out like the palette it opens from — a row to the
|
|
6
|
+
* right of the icon for a horizontal palette, a column below it for a vertical one — and a member that is itself
|
|
7
|
+
* a group opens the next level the same way. A click outside the child palettes closes them.
|
|
8
|
+
*/
|
|
9
|
+
/** The attribute a flattened category keeps with the id of the group it was nested in. */
|
|
10
|
+
export const groupParentAttribute = 'palette-group-parent';
|
|
11
|
+
const isCategory = (node) => node.nodeType === 1 && node.tagName.toLowerCase() === 'category';
|
|
12
|
+
const ownsContents = (category) => Array.from(category.children).some(child => !isCategory(child));
|
|
13
|
+
/**
|
|
14
|
+
* Moves every nested category of the toolbox tree to the top level, right after the group that held it, depth
|
|
15
|
+
* first, so Scratch Blocks makes a category of each. Returns the groups found. A nested category without an id
|
|
16
|
+
* gets one from its group's id and its position.
|
|
17
|
+
*/
|
|
18
|
+
export const flattenCategoryGroups = (tree) => {
|
|
19
|
+
const groups = { members: new Map(), withOwnContents: new Set() };
|
|
20
|
+
if (!tree)
|
|
21
|
+
return groups;
|
|
22
|
+
const used = new Set(Array.from(tree.querySelectorAll('category[id]'), category => category.getAttribute('id')));
|
|
23
|
+
const flatten = (category, after) => {
|
|
24
|
+
const nested = Array.from(category.children).filter(isCategory);
|
|
25
|
+
if (!nested.length)
|
|
26
|
+
return after;
|
|
27
|
+
const id = category.getAttribute('id');
|
|
28
|
+
groups.members.set(id, []);
|
|
29
|
+
if (ownsContents(category))
|
|
30
|
+
groups.withOwnContents.add(id);
|
|
31
|
+
let last = after;
|
|
32
|
+
nested.forEach((child, index) => {
|
|
33
|
+
if (!child.getAttribute('id')) {
|
|
34
|
+
let generated = `${id}/${index}`;
|
|
35
|
+
while (used.has(generated))
|
|
36
|
+
generated += '_';
|
|
37
|
+
used.add(generated);
|
|
38
|
+
child.setAttribute('id', generated);
|
|
39
|
+
}
|
|
40
|
+
child.setAttribute(groupParentAttribute, id);
|
|
41
|
+
groups.members.get(id).push(child.getAttribute('id'));
|
|
42
|
+
last.after(child);
|
|
43
|
+
last = flatten(child, child);
|
|
44
|
+
});
|
|
45
|
+
return last;
|
|
46
|
+
};
|
|
47
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
48
|
+
if (!category.getAttribute('id'))
|
|
49
|
+
continue;
|
|
50
|
+
flatten(category, category);
|
|
51
|
+
}
|
|
52
|
+
return groups;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Runs a filter that removes top-level categories by id, such as the palette's group settings, on a flattened tree.
|
|
56
|
+
* A group the filter removes still stays, without blocks of its own, while one of its members stays; a group left
|
|
57
|
+
* with neither members nor blocks of its own is removed with its members. Returns the groups that remain.
|
|
58
|
+
*/
|
|
59
|
+
export const filterGroupedToolboxTree = (tree, groups, filter) => {
|
|
60
|
+
const elements = new Map();
|
|
61
|
+
const markers = new Map();
|
|
62
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
63
|
+
const id = category.getAttribute('id');
|
|
64
|
+
if (!groups.members.has(id))
|
|
65
|
+
continue;
|
|
66
|
+
const marker = tree.ownerDocument.createComment('palette-group');
|
|
67
|
+
category.before(marker);
|
|
68
|
+
elements.set(id, category);
|
|
69
|
+
markers.set(id, marker);
|
|
70
|
+
}
|
|
71
|
+
filter(tree);
|
|
72
|
+
const present = (element) => !!element && element.parentNode === tree;
|
|
73
|
+
const remaining = { members: new Map(), withOwnContents: new Set() };
|
|
74
|
+
const keep = (id) => {
|
|
75
|
+
const members = groups.members.get(id);
|
|
76
|
+
if (!members)
|
|
77
|
+
return present(Array.from(tree.children).find(child => isCategory(child) && child.getAttribute('id') === id));
|
|
78
|
+
const kept = members.filter(keep);
|
|
79
|
+
const ownShown = groups.withOwnContents.has(id) && present(elements.get(id));
|
|
80
|
+
if (!kept.length && !ownShown)
|
|
81
|
+
return false;
|
|
82
|
+
remaining.members.set(id, kept);
|
|
83
|
+
if (ownShown)
|
|
84
|
+
remaining.withOwnContents.add(id);
|
|
85
|
+
return true;
|
|
86
|
+
};
|
|
87
|
+
for (const [id, element] of elements) {
|
|
88
|
+
if (!element.hasAttribute(groupParentAttribute))
|
|
89
|
+
keep(id);
|
|
90
|
+
}
|
|
91
|
+
for (const [id, element] of elements) {
|
|
92
|
+
if (!remaining.members.has(id))
|
|
93
|
+
element.remove();
|
|
94
|
+
else if (!present(element)) {
|
|
95
|
+
// Shown only as the way to its members.
|
|
96
|
+
element.replaceChildren();
|
|
97
|
+
markers.get(id).after(element);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const marker of markers.values())
|
|
101
|
+
marker.remove();
|
|
102
|
+
// A member whose group is gone goes with it.
|
|
103
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
104
|
+
const parent = category.getAttribute(groupParentAttribute);
|
|
105
|
+
if (parent && !remaining.members.has(parent))
|
|
106
|
+
category.remove();
|
|
107
|
+
}
|
|
108
|
+
return remaining;
|
|
109
|
+
};
|
|
110
|
+
const selectedClass = 'categorySelected';
|
|
111
|
+
/** Makes clicking a group's icon in the palette menu open its child palette. */
|
|
112
|
+
export const attachPaletteCategoryGroups = (menu, options) => {
|
|
113
|
+
const doc = menu.ownerDocument;
|
|
114
|
+
const events = new doc.defaultView.AbortController();
|
|
115
|
+
const capture = { capture: true, signal: events.signal };
|
|
116
|
+
let groups = { members: new Map(), withOwnContents: new Set() };
|
|
117
|
+
const parentOf = new Map();
|
|
118
|
+
// The open child palettes, outermost first, with the icon each opened from.
|
|
119
|
+
const stack = [];
|
|
120
|
+
let selected = null;
|
|
121
|
+
const categoryById = (id) => options.categories().find(category => category.id_ === id);
|
|
122
|
+
const topLevelOf = (id) => {
|
|
123
|
+
let current = id;
|
|
124
|
+
while (parentOf.has(current))
|
|
125
|
+
current = parentOf.get(current);
|
|
126
|
+
return current;
|
|
127
|
+
};
|
|
128
|
+
const markSelection = () => {
|
|
129
|
+
for (const [id] of groups.members) {
|
|
130
|
+
if (parentOf.has(id))
|
|
131
|
+
continue;
|
|
132
|
+
const item = categoryById(id)?.item_;
|
|
133
|
+
if (!item)
|
|
134
|
+
continue;
|
|
135
|
+
const current = selected !== null && selected !== id && topLevelOf(selected) === id;
|
|
136
|
+
item.toggleAttribute('data-palette-group-current', current);
|
|
137
|
+
if (current)
|
|
138
|
+
item.classList.add(selectedClass);
|
|
139
|
+
else if (selected !== id)
|
|
140
|
+
item.classList.remove(selectedClass);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const closeFrom = (depth) => {
|
|
144
|
+
let changed = false;
|
|
145
|
+
while (stack.length > depth) {
|
|
146
|
+
const { anchor, popup } = stack.pop();
|
|
147
|
+
anchor.setAttribute('aria-expanded', 'false');
|
|
148
|
+
popup.remove();
|
|
149
|
+
changed = true;
|
|
150
|
+
}
|
|
151
|
+
if (changed)
|
|
152
|
+
options.onChange?.();
|
|
153
|
+
};
|
|
154
|
+
const close = () => closeFrom(0);
|
|
155
|
+
const place = (popup, anchor) => {
|
|
156
|
+
const container = popup.offsetParent ?? popup.parentElement;
|
|
157
|
+
const bounds = container.getBoundingClientRect();
|
|
158
|
+
const rect = anchor.getBoundingClientRect();
|
|
159
|
+
const width = popup.offsetWidth;
|
|
160
|
+
const height = popup.offsetHeight;
|
|
161
|
+
const maxLeft = Math.max(0, container.clientWidth - width);
|
|
162
|
+
const maxTop = Math.max(0, container.clientHeight - height);
|
|
163
|
+
let left;
|
|
164
|
+
let top;
|
|
165
|
+
if (options.horizontal()) {
|
|
166
|
+
left = (options.rtl ? rect.left - width : rect.right) - bounds.left;
|
|
167
|
+
top = rect.top - bounds.top;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
left = (options.rtl ? rect.right - width : rect.left) - bounds.left;
|
|
171
|
+
top = rect.bottom - bounds.top;
|
|
172
|
+
}
|
|
173
|
+
popup.style.left = `${Math.min(Math.max(0, left), maxLeft)}px`;
|
|
174
|
+
popup.style.top = `${Math.min(Math.max(0, top), maxTop)}px`;
|
|
175
|
+
};
|
|
176
|
+
const activate = (category, entry, depth, focus, self) => {
|
|
177
|
+
if (!self && groups.members.has(category.id_)) {
|
|
178
|
+
if (stack[depth]?.id === category.id_)
|
|
179
|
+
closeFrom(depth);
|
|
180
|
+
else
|
|
181
|
+
open(category, entry, depth, focus);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
close();
|
|
185
|
+
options.choose(category);
|
|
186
|
+
};
|
|
187
|
+
const createEntry = (category, depth, self = false) => {
|
|
188
|
+
const entry = doc.createElement('div');
|
|
189
|
+
entry.className = `scratchCategoryMenuItem scratchCategoryId-${category.id_}`;
|
|
190
|
+
entry.classList.add('tw-palette-group-item');
|
|
191
|
+
entry.setAttribute('data-palette-group-item', category.id_);
|
|
192
|
+
entry.setAttribute('role', 'menuitem');
|
|
193
|
+
entry.tabIndex = -1;
|
|
194
|
+
for (const child of Array.from(category.item_.children))
|
|
195
|
+
entry.append(child.cloneNode(true));
|
|
196
|
+
const isGroup = !self && groups.members.has(category.id_);
|
|
197
|
+
if (isGroup) {
|
|
198
|
+
entry.setAttribute('aria-haspopup', 'true');
|
|
199
|
+
entry.setAttribute('aria-expanded', 'false');
|
|
200
|
+
}
|
|
201
|
+
if (selected === category.id_ || (isGroup && selected !== null && isInside(selected, category.id_))) {
|
|
202
|
+
entry.classList.add(selectedClass);
|
|
203
|
+
}
|
|
204
|
+
entry.addEventListener('click', event => {
|
|
205
|
+
event.stopPropagation();
|
|
206
|
+
activate(category, entry, depth, false, self);
|
|
207
|
+
}, { signal: events.signal });
|
|
208
|
+
entry.addEventListener('keydown', event => {
|
|
209
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
event.stopPropagation();
|
|
212
|
+
activate(category, entry, depth, true, self);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const forward = options.horizontal() ? (options.rtl ? 'ArrowLeft' : 'ArrowRight') : 'ArrowDown';
|
|
216
|
+
const backward = options.horizontal() ? (options.rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp';
|
|
217
|
+
if (event.key !== forward && event.key !== backward)
|
|
218
|
+
return;
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
const entries = Array.from(entry.parentElement.children);
|
|
221
|
+
const index = entries.indexOf(entry) + (event.key === forward ? 1 : -1);
|
|
222
|
+
entries[(index + entries.length) % entries.length].focus();
|
|
223
|
+
}, { signal: events.signal });
|
|
224
|
+
return entry;
|
|
225
|
+
};
|
|
226
|
+
const isInside = (id, groupId) => {
|
|
227
|
+
for (let current = id; current !== undefined; current = parentOf.get(current)) {
|
|
228
|
+
if (current === groupId)
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
};
|
|
233
|
+
const open = (group, anchor, depth, focus) => {
|
|
234
|
+
closeFrom(depth);
|
|
235
|
+
const members = (groups.members.get(group.id_) ?? []).map(categoryById).filter(Boolean);
|
|
236
|
+
const popup = doc.createElement('div');
|
|
237
|
+
popup.className = 'tw-palette-group-popup';
|
|
238
|
+
popup.setAttribute('data-palette-group-popup', group.id_);
|
|
239
|
+
popup.setAttribute('role', 'menu');
|
|
240
|
+
popup.setAttribute('aria-label', anchor.textContent?.trim() || group.id_);
|
|
241
|
+
popup.toggleAttribute('data-horizontal', options.horizontal());
|
|
242
|
+
// The child palette is drawn in the palette's own colours and type.
|
|
243
|
+
const look = doc.defaultView.getComputedStyle(menu.querySelector('.scratchCategoryMenu, .scratchCategoryMenuHorizontal') ?? menu);
|
|
244
|
+
popup.style.setProperty('--palette-group-background', look.backgroundColor);
|
|
245
|
+
popup.style.color = look.color;
|
|
246
|
+
popup.style.fontSize = look.fontSize;
|
|
247
|
+
popup.style.fontFamily = look.fontFamily;
|
|
248
|
+
if (groups.withOwnContents.has(group.id_))
|
|
249
|
+
popup.append(createEntry(group, depth, true));
|
|
250
|
+
for (const member of members)
|
|
251
|
+
popup.append(createEntry(member, depth + 1));
|
|
252
|
+
(menu.parentElement ?? doc.body).append(popup);
|
|
253
|
+
place(popup, anchor);
|
|
254
|
+
anchor.setAttribute('aria-expanded', 'true');
|
|
255
|
+
stack.push({ id: group.id_, anchor, popup });
|
|
256
|
+
options.onChange?.();
|
|
257
|
+
if (focus)
|
|
258
|
+
popup.firstElementChild?.focus();
|
|
259
|
+
};
|
|
260
|
+
const toggle = (category, focus = false) => {
|
|
261
|
+
if (stack[0]?.id === category.id_)
|
|
262
|
+
close();
|
|
263
|
+
else
|
|
264
|
+
open(category, category.item_, 0, focus);
|
|
265
|
+
};
|
|
266
|
+
const groupItem = (target) => {
|
|
267
|
+
if (!(target instanceof doc.defaultView.Node))
|
|
268
|
+
return null;
|
|
269
|
+
return options.categories().find(category => groups.members.has(category.id_) &&
|
|
270
|
+
!parentOf.has(category.id_) && category.item_.contains(target)) ?? null;
|
|
271
|
+
};
|
|
272
|
+
// Scratch Blocks opens a category on mouseup (touchend on touch screens); a group opens its child palette on click.
|
|
273
|
+
for (const type of ['mouseup', 'touchend']) {
|
|
274
|
+
menu.addEventListener(type, event => {
|
|
275
|
+
if (groupItem(event.target))
|
|
276
|
+
event.stopPropagation();
|
|
277
|
+
}, capture);
|
|
278
|
+
}
|
|
279
|
+
menu.addEventListener('click', event => {
|
|
280
|
+
const category = groupItem(event.target);
|
|
281
|
+
if (!category)
|
|
282
|
+
return;
|
|
283
|
+
event.stopPropagation();
|
|
284
|
+
toggle(category);
|
|
285
|
+
}, capture);
|
|
286
|
+
const contains = (node) => !!node && stack.some(({ popup }) => popup.contains(node));
|
|
287
|
+
const onPointerDown = (event) => {
|
|
288
|
+
if (!stack.length)
|
|
289
|
+
return;
|
|
290
|
+
const target = event.target;
|
|
291
|
+
if (contains(target) || stack[0].anchor.contains(target))
|
|
292
|
+
return;
|
|
293
|
+
close();
|
|
294
|
+
};
|
|
295
|
+
const onKeyDown = (event) => {
|
|
296
|
+
if (event.key !== 'Escape' || event.isComposing || !stack.length)
|
|
297
|
+
return;
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
// The palette's own Escape would close the block list as well.
|
|
300
|
+
event.stopImmediatePropagation();
|
|
301
|
+
const { anchor } = stack[stack.length - 1];
|
|
302
|
+
closeFrom(stack.length - 1);
|
|
303
|
+
anchor.focus();
|
|
304
|
+
};
|
|
305
|
+
doc.addEventListener('pointerdown', onPointerDown, capture);
|
|
306
|
+
doc.addEventListener('keydown', onKeyDown, capture);
|
|
307
|
+
menu.addEventListener('scroll', close, { signal: events.signal });
|
|
308
|
+
doc.defaultView.addEventListener('resize', close, { signal: events.signal });
|
|
309
|
+
return {
|
|
310
|
+
update(next) {
|
|
311
|
+
close();
|
|
312
|
+
groups = next;
|
|
313
|
+
parentOf.clear();
|
|
314
|
+
for (const [id, members] of groups.members)
|
|
315
|
+
for (const member of members)
|
|
316
|
+
parentOf.set(member, id);
|
|
317
|
+
for (const category of options.categories()) {
|
|
318
|
+
const member = parentOf.has(category.id_);
|
|
319
|
+
const row = category.item_.parentElement;
|
|
320
|
+
if (row && row !== menu)
|
|
321
|
+
row.hidden = member;
|
|
322
|
+
const group = groups.members.has(category.id_) && !member;
|
|
323
|
+
if (group) {
|
|
324
|
+
category.item_.setAttribute('aria-haspopup', 'true');
|
|
325
|
+
category.item_.setAttribute('aria-expanded', 'false');
|
|
326
|
+
category.item_.setAttribute('data-palette-group', '');
|
|
327
|
+
category.item_.classList.add('tw-palette-group');
|
|
328
|
+
}
|
|
329
|
+
const { setSelected } = category;
|
|
330
|
+
category.setSelected = function (value) {
|
|
331
|
+
setSelected.call(this, value);
|
|
332
|
+
if (value)
|
|
333
|
+
selected = category.id_;
|
|
334
|
+
else if (selected === category.id_)
|
|
335
|
+
selected = null;
|
|
336
|
+
markSelection();
|
|
337
|
+
};
|
|
338
|
+
if (category.item_.classList.contains(selectedClass))
|
|
339
|
+
selected = category.id_;
|
|
340
|
+
}
|
|
341
|
+
markSelection();
|
|
342
|
+
},
|
|
343
|
+
isGroup: id => groups.members.has(id) && !parentOf.has(id),
|
|
344
|
+
topLevelId: topLevelOf,
|
|
345
|
+
toggle,
|
|
346
|
+
isOpen: () => stack.length > 0,
|
|
347
|
+
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
348
|
+
contains,
|
|
349
|
+
close,
|
|
350
|
+
dispose() {
|
|
351
|
+
close();
|
|
352
|
+
events.abort();
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
};
|
|
@@ -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 = {
|
|
@@ -99,8 +106,12 @@ export declare function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks
|
|
|
99
106
|
/**
|
|
100
107
|
* Splits each extension category whose extension declares groups into one category per group, in the catalog's
|
|
101
108
|
* order. A label, separator or button goes with the block after it, or with the last block when none follows.
|
|
109
|
+
* With `nested`, the extension's category stays, with its unmarked blocks, and holds the categories of its declared
|
|
110
|
+
* groups, so the palette can show it as a group that opens them (see `./palette-category-groups`).
|
|
102
111
|
*/
|
|
103
|
-
export declare function splitExtensionCategories(tree: Element, catalog: PaletteGroupCatalog
|
|
112
|
+
export declare function splitExtensionCategories(tree: Element, catalog: PaletteGroupCatalog, { nested }?: {
|
|
113
|
+
nested?: boolean;
|
|
114
|
+
}): void;
|
|
104
115
|
/**
|
|
105
116
|
* Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it.
|
|
106
117
|
* With a catalog, an extension that declares groups is first split into a category per group.
|
|
@@ -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
|
}
|
|
@@ -156,11 +178,27 @@ export function defaultVisibleGroups(target, extensionGroups, catalog) {
|
|
|
156
178
|
const used = usedGroups(target, catalog);
|
|
157
179
|
return [...standardGroups, ...extensionGroups.filter(id => used.has(id))];
|
|
158
180
|
}
|
|
181
|
+
/** A category for one group of an extension, drawn like the extension's own unless the group has its own look. */
|
|
182
|
+
function groupCategory(category, group, children) {
|
|
183
|
+
const part = category.cloneNode(false);
|
|
184
|
+
part.setAttribute('id', group.id);
|
|
185
|
+
part.setAttribute('name', group.name);
|
|
186
|
+
if (group.color1)
|
|
187
|
+
part.setAttribute('colour', group.color1);
|
|
188
|
+
if (group.color2)
|
|
189
|
+
part.setAttribute('secondaryColour', group.color2);
|
|
190
|
+
if (group.menuIconURI)
|
|
191
|
+
part.setAttribute('iconURI', group.menuIconURI);
|
|
192
|
+
part.append(...children);
|
|
193
|
+
return part;
|
|
194
|
+
}
|
|
159
195
|
/**
|
|
160
196
|
* Splits each extension category whose extension declares groups into one category per group, in the catalog's
|
|
161
197
|
* order. A label, separator or button goes with the block after it, or with the last block when none follows.
|
|
198
|
+
* With `nested`, the extension's category stays, with its unmarked blocks, and holds the categories of its declared
|
|
199
|
+
* groups, so the palette can show it as a group that opens them (see `./palette-category-groups`).
|
|
162
200
|
*/
|
|
163
|
-
export function splitExtensionCategories(tree, catalog) {
|
|
201
|
+
export function splitExtensionCategories(tree, catalog, { nested = false } = {}) {
|
|
164
202
|
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
165
203
|
const extensionId = category.getAttribute('id');
|
|
166
204
|
const groups = catalog.groups.filter(group => group.extensionId === extensionId);
|
|
@@ -180,24 +218,27 @@ export function splitExtensionCategories(tree, catalog) {
|
|
|
180
218
|
}
|
|
181
219
|
if (pending.length)
|
|
182
220
|
items.set(last, [...items.get(last) ?? [], ...pending]);
|
|
221
|
+
if (nested) {
|
|
222
|
+
category.replaceChildren(...items.get(extensionId) ?? []);
|
|
223
|
+
for (const group of groups) {
|
|
224
|
+
const children = items.get(group.id);
|
|
225
|
+
if (!group.declared || !children)
|
|
226
|
+
continue;
|
|
227
|
+
const part = groupCategory(category, group, children);
|
|
228
|
+
part.removeAttribute('showStatusButton');
|
|
229
|
+
category.append(part);
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
183
233
|
let first = true;
|
|
184
234
|
for (const group of groups) {
|
|
185
235
|
const children = items.get(group.id);
|
|
186
236
|
if (!children)
|
|
187
237
|
continue;
|
|
188
|
-
const part = category
|
|
189
|
-
part.setAttribute('id', group.id);
|
|
190
|
-
part.setAttribute('name', group.name);
|
|
191
|
-
if (group.color1)
|
|
192
|
-
part.setAttribute('colour', group.color1);
|
|
193
|
-
if (group.color2)
|
|
194
|
-
part.setAttribute('secondaryColour', group.color2);
|
|
195
|
-
if (group.menuIconURI)
|
|
196
|
-
part.setAttribute('iconURI', group.menuIconURI);
|
|
238
|
+
const part = groupCategory(category, group, children);
|
|
197
239
|
if (!first)
|
|
198
240
|
part.removeAttribute('showStatusButton');
|
|
199
241
|
first = false;
|
|
200
|
-
part.append(...children);
|
|
201
242
|
category.before(part);
|
|
202
243
|
}
|
|
203
244
|
category.remove();
|
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.13.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",
|
|
@@ -54,6 +54,10 @@
|
|
|
54
54
|
"types": "./dist/palette-dropdown-resize.d.ts",
|
|
55
55
|
"default": "./dist/palette-dropdown-resize.js"
|
|
56
56
|
},
|
|
57
|
+
"./palette-category-groups": {
|
|
58
|
+
"types": "./dist/palette-category-groups.d.ts",
|
|
59
|
+
"default": "./dist/palette-category-groups.js"
|
|
60
|
+
},
|
|
57
61
|
"./palette-category-visibility": {
|
|
58
62
|
"types": "./dist/palette-category-visibility.d.ts",
|
|
59
63
|
"default": "./dist/palette-category-visibility.js"
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
|
+
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
4
|
+
* each nested category becomes a top-level one right after its group, and the menu hides it. Clicking the
|
|
5
|
+
* group's icon then shows its members in a child palette laid out like the palette it opens from — a row to the
|
|
6
|
+
* right of the icon for a horizontal palette, a column below it for a vertical one — and a member that is itself
|
|
7
|
+
* a group opens the next level the same way. A click outside the child palettes closes them.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The attribute a flattened category keeps with the id of the group it was nested in. */
|
|
11
|
+
export const groupParentAttribute = 'palette-group-parent';
|
|
12
|
+
|
|
13
|
+
export interface PaletteGroups {
|
|
14
|
+
/** Each group's members, in order, by category id. A member may itself be a group. */
|
|
15
|
+
members: Map<string, string[]>;
|
|
16
|
+
/** The groups that also hold blocks of their own, which their child palette offers first. */
|
|
17
|
+
withOwnContents: Set<string>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const isCategory = (node: Node): node is Element =>
|
|
21
|
+
node.nodeType === 1 && (node as Element).tagName.toLowerCase() === 'category';
|
|
22
|
+
const ownsContents = (category: Element) => Array.from(category.children).some(child => !isCategory(child));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Moves every nested category of the toolbox tree to the top level, right after the group that held it, depth
|
|
26
|
+
* first, so Scratch Blocks makes a category of each. Returns the groups found. A nested category without an id
|
|
27
|
+
* gets one from its group's id and its position.
|
|
28
|
+
*/
|
|
29
|
+
export const flattenCategoryGroups = (tree: Element | null | undefined): PaletteGroups => {
|
|
30
|
+
const groups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
31
|
+
if (!tree) return groups;
|
|
32
|
+
const used = new Set(Array.from(tree.querySelectorAll('category[id]'), category => category.getAttribute('id')));
|
|
33
|
+
const flatten = (category: Element, after: Element): Element => {
|
|
34
|
+
const nested = Array.from(category.children).filter(isCategory);
|
|
35
|
+
if (!nested.length) return after;
|
|
36
|
+
const id = category.getAttribute('id')!;
|
|
37
|
+
groups.members.set(id, []);
|
|
38
|
+
if (ownsContents(category)) groups.withOwnContents.add(id);
|
|
39
|
+
let last = after;
|
|
40
|
+
nested.forEach((child, index) => {
|
|
41
|
+
if (!child.getAttribute('id')) {
|
|
42
|
+
let generated = `${id}/${index}`;
|
|
43
|
+
while (used.has(generated)) generated += '_';
|
|
44
|
+
used.add(generated);
|
|
45
|
+
child.setAttribute('id', generated);
|
|
46
|
+
}
|
|
47
|
+
child.setAttribute(groupParentAttribute, id);
|
|
48
|
+
groups.members.get(id)!.push(child.getAttribute('id')!);
|
|
49
|
+
last.after(child);
|
|
50
|
+
last = flatten(child, child);
|
|
51
|
+
});
|
|
52
|
+
return last;
|
|
53
|
+
};
|
|
54
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
55
|
+
if (!category.getAttribute('id')) continue;
|
|
56
|
+
flatten(category, category);
|
|
57
|
+
}
|
|
58
|
+
return groups;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Runs a filter that removes top-level categories by id, such as the palette's group settings, on a flattened tree.
|
|
63
|
+
* A group the filter removes still stays, without blocks of its own, while one of its members stays; a group left
|
|
64
|
+
* with neither members nor blocks of its own is removed with its members. Returns the groups that remain.
|
|
65
|
+
*/
|
|
66
|
+
export const filterGroupedToolboxTree = (tree: Element, groups: PaletteGroups, filter: (tree: Element) => void):
|
|
67
|
+
PaletteGroups => {
|
|
68
|
+
const elements = new Map<string, Element>();
|
|
69
|
+
const markers = new Map<string, Comment>();
|
|
70
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
71
|
+
const id = category.getAttribute('id')!;
|
|
72
|
+
if (!groups.members.has(id)) continue;
|
|
73
|
+
const marker = tree.ownerDocument.createComment('palette-group');
|
|
74
|
+
category.before(marker);
|
|
75
|
+
elements.set(id, category);
|
|
76
|
+
markers.set(id, marker);
|
|
77
|
+
}
|
|
78
|
+
filter(tree);
|
|
79
|
+
const present = (element: Element | undefined) => !!element && element.parentNode === tree;
|
|
80
|
+
const remaining: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
81
|
+
const keep = (id: string): boolean => {
|
|
82
|
+
const members = groups.members.get(id);
|
|
83
|
+
if (!members) return present(Array.from(tree.children).find(child => isCategory(child) && child.getAttribute('id') === id));
|
|
84
|
+
const kept = members.filter(keep);
|
|
85
|
+
const ownShown = groups.withOwnContents.has(id) && present(elements.get(id));
|
|
86
|
+
if (!kept.length && !ownShown) return false;
|
|
87
|
+
remaining.members.set(id, kept);
|
|
88
|
+
if (ownShown) remaining.withOwnContents.add(id);
|
|
89
|
+
return true;
|
|
90
|
+
};
|
|
91
|
+
for (const [id, element] of elements) {
|
|
92
|
+
if (!element.hasAttribute(groupParentAttribute)) keep(id);
|
|
93
|
+
}
|
|
94
|
+
for (const [id, element] of elements) {
|
|
95
|
+
if (!remaining.members.has(id)) element.remove();
|
|
96
|
+
else if (!present(element)) {
|
|
97
|
+
// Shown only as the way to its members.
|
|
98
|
+
element.replaceChildren();
|
|
99
|
+
markers.get(id)!.after(element);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const marker of markers.values()) marker.remove();
|
|
103
|
+
// A member whose group is gone goes with it.
|
|
104
|
+
for (const category of Array.from(tree.children).filter(isCategory)) {
|
|
105
|
+
const parent = category.getAttribute(groupParentAttribute);
|
|
106
|
+
if (parent && !remaining.members.has(parent)) category.remove();
|
|
107
|
+
}
|
|
108
|
+
return remaining;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
112
|
+
export interface PaletteCategory {
|
|
113
|
+
id_: string;
|
|
114
|
+
name_: string;
|
|
115
|
+
item_: HTMLElement;
|
|
116
|
+
setSelected(selected: boolean): void;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface PaletteCategoryGroupsOptions {
|
|
120
|
+
/** The toolbox's categories, top level and flattened members alike. */
|
|
121
|
+
categories(): PaletteCategory[];
|
|
122
|
+
/** Whether the palette is laid out in a row. */
|
|
123
|
+
horizontal(): boolean;
|
|
124
|
+
rtl?: boolean;
|
|
125
|
+
/** Opens a category that is not a group, as clicking its icon in the palette would. */
|
|
126
|
+
choose(category: PaletteCategory): void;
|
|
127
|
+
/** Called when a child palette opens or the last one closes. */
|
|
128
|
+
onChange?(): void;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface PaletteCategoryGroupsController {
|
|
132
|
+
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
133
|
+
update(groups: PaletteGroups): void;
|
|
134
|
+
isGroup(id: string): boolean;
|
|
135
|
+
/** The id of the outermost group the category is in, or its own id when it is in none. */
|
|
136
|
+
topLevelId(id: string): string;
|
|
137
|
+
/** Opens the group's child palette from its icon, or closes it if it is open. */
|
|
138
|
+
toggle(category: PaletteCategory, focus?: boolean): void;
|
|
139
|
+
isOpen(): boolean;
|
|
140
|
+
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
141
|
+
anchorOf(category: PaletteCategory): HTMLElement;
|
|
142
|
+
/** Whether the node is in a child palette. */
|
|
143
|
+
contains(node: Node | null): boolean;
|
|
144
|
+
close(): void;
|
|
145
|
+
dispose(): void;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const selectedClass = 'categorySelected';
|
|
149
|
+
|
|
150
|
+
/** Makes clicking a group's icon in the palette menu open its child palette. */
|
|
151
|
+
export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteCategoryGroupsOptions):
|
|
152
|
+
PaletteCategoryGroupsController => {
|
|
153
|
+
const doc = menu.ownerDocument;
|
|
154
|
+
const events = new doc.defaultView!.AbortController();
|
|
155
|
+
const capture = {capture: true, signal: events.signal};
|
|
156
|
+
let groups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
157
|
+
const parentOf = new Map<string, string>();
|
|
158
|
+
// The open child palettes, outermost first, with the icon each opened from.
|
|
159
|
+
const stack: {id: string; anchor: HTMLElement; popup: HTMLElement}[] = [];
|
|
160
|
+
let selected: string | null = null;
|
|
161
|
+
|
|
162
|
+
const categoryById = (id: string) => options.categories().find(category => category.id_ === id);
|
|
163
|
+
const topLevelOf = (id: string) => {
|
|
164
|
+
let current = id;
|
|
165
|
+
while (parentOf.has(current)) current = parentOf.get(current)!;
|
|
166
|
+
return current;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const markSelection = () => {
|
|
170
|
+
for (const [id] of groups.members) {
|
|
171
|
+
if (parentOf.has(id)) continue;
|
|
172
|
+
const item = categoryById(id)?.item_;
|
|
173
|
+
if (!item) continue;
|
|
174
|
+
const current = selected !== null && selected !== id && topLevelOf(selected) === id;
|
|
175
|
+
item.toggleAttribute('data-palette-group-current', current);
|
|
176
|
+
if (current) item.classList.add(selectedClass);
|
|
177
|
+
else if (selected !== id) item.classList.remove(selectedClass);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const closeFrom = (depth: number) => {
|
|
182
|
+
let changed = false;
|
|
183
|
+
while (stack.length > depth) {
|
|
184
|
+
const {anchor, popup} = stack.pop()!;
|
|
185
|
+
anchor.setAttribute('aria-expanded', 'false');
|
|
186
|
+
popup.remove();
|
|
187
|
+
changed = true;
|
|
188
|
+
}
|
|
189
|
+
if (changed) options.onChange?.();
|
|
190
|
+
};
|
|
191
|
+
const close = () => closeFrom(0);
|
|
192
|
+
|
|
193
|
+
const place = (popup: HTMLElement, anchor: HTMLElement) => {
|
|
194
|
+
const container = popup.offsetParent as HTMLElement | null ?? popup.parentElement!;
|
|
195
|
+
const bounds = container.getBoundingClientRect();
|
|
196
|
+
const rect = anchor.getBoundingClientRect();
|
|
197
|
+
const width = popup.offsetWidth;
|
|
198
|
+
const height = popup.offsetHeight;
|
|
199
|
+
const maxLeft = Math.max(0, container.clientWidth - width);
|
|
200
|
+
const maxTop = Math.max(0, container.clientHeight - height);
|
|
201
|
+
let left: number;
|
|
202
|
+
let top: number;
|
|
203
|
+
if (options.horizontal()) {
|
|
204
|
+
left = (options.rtl ? rect.left - width : rect.right) - bounds.left;
|
|
205
|
+
top = rect.top - bounds.top;
|
|
206
|
+
} else {
|
|
207
|
+
left = (options.rtl ? rect.right - width : rect.left) - bounds.left;
|
|
208
|
+
top = rect.bottom - bounds.top;
|
|
209
|
+
}
|
|
210
|
+
popup.style.left = `${Math.min(Math.max(0, left), maxLeft)}px`;
|
|
211
|
+
popup.style.top = `${Math.min(Math.max(0, top), maxTop)}px`;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const activate = (category: PaletteCategory, entry: HTMLElement, depth: number, focus: boolean, self: boolean) => {
|
|
215
|
+
if (!self && groups.members.has(category.id_)) {
|
|
216
|
+
if (stack[depth]?.id === category.id_) closeFrom(depth);
|
|
217
|
+
else open(category, entry, depth, focus);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
close();
|
|
221
|
+
options.choose(category);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const createEntry = (category: PaletteCategory, depth: number, self = false) => {
|
|
225
|
+
const entry = doc.createElement('div');
|
|
226
|
+
entry.className = `scratchCategoryMenuItem scratchCategoryId-${category.id_}`;
|
|
227
|
+
entry.classList.add('tw-palette-group-item');
|
|
228
|
+
entry.setAttribute('data-palette-group-item', category.id_);
|
|
229
|
+
entry.setAttribute('role', 'menuitem');
|
|
230
|
+
entry.tabIndex = -1;
|
|
231
|
+
for (const child of Array.from(category.item_.children)) entry.append(child.cloneNode(true));
|
|
232
|
+
const isGroup = !self && groups.members.has(category.id_);
|
|
233
|
+
if (isGroup) {
|
|
234
|
+
entry.setAttribute('aria-haspopup', 'true');
|
|
235
|
+
entry.setAttribute('aria-expanded', 'false');
|
|
236
|
+
}
|
|
237
|
+
if (selected === category.id_ || (isGroup && selected !== null && isInside(selected, category.id_))) {
|
|
238
|
+
entry.classList.add(selectedClass);
|
|
239
|
+
}
|
|
240
|
+
entry.addEventListener('click', event => {
|
|
241
|
+
event.stopPropagation();
|
|
242
|
+
activate(category, entry, depth, false, self);
|
|
243
|
+
}, {signal: events.signal});
|
|
244
|
+
entry.addEventListener('keydown', event => {
|
|
245
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
246
|
+
event.preventDefault();
|
|
247
|
+
event.stopPropagation();
|
|
248
|
+
activate(category, entry, depth, true, self);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const forward = options.horizontal() ? (options.rtl ? 'ArrowLeft' : 'ArrowRight') : 'ArrowDown';
|
|
252
|
+
const backward = options.horizontal() ? (options.rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp';
|
|
253
|
+
if (event.key !== forward && event.key !== backward) return;
|
|
254
|
+
event.preventDefault();
|
|
255
|
+
const entries = Array.from(entry.parentElement!.children) as HTMLElement[];
|
|
256
|
+
const index = entries.indexOf(entry) + (event.key === forward ? 1 : -1);
|
|
257
|
+
entries[(index + entries.length) % entries.length].focus();
|
|
258
|
+
}, {signal: events.signal});
|
|
259
|
+
return entry;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const isInside = (id: string, groupId: string) => {
|
|
263
|
+
for (let current: string | undefined = id; current !== undefined; current = parentOf.get(current)) {
|
|
264
|
+
if (current === groupId) return true;
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const open = (group: PaletteCategory, anchor: HTMLElement, depth: number, focus: boolean) => {
|
|
270
|
+
closeFrom(depth);
|
|
271
|
+
const members = (groups.members.get(group.id_) ?? []).map(categoryById).filter(Boolean) as PaletteCategory[];
|
|
272
|
+
const popup = doc.createElement('div');
|
|
273
|
+
popup.className = 'tw-palette-group-popup';
|
|
274
|
+
popup.setAttribute('data-palette-group-popup', group.id_);
|
|
275
|
+
popup.setAttribute('role', 'menu');
|
|
276
|
+
popup.setAttribute('aria-label', anchor.textContent?.trim() || group.id_);
|
|
277
|
+
popup.toggleAttribute('data-horizontal', options.horizontal());
|
|
278
|
+
// The child palette is drawn in the palette's own colours and type.
|
|
279
|
+
const look = doc.defaultView!.getComputedStyle(menu.querySelector('.scratchCategoryMenu, .scratchCategoryMenuHorizontal') ?? menu);
|
|
280
|
+
popup.style.setProperty('--palette-group-background', look.backgroundColor);
|
|
281
|
+
popup.style.color = look.color;
|
|
282
|
+
popup.style.fontSize = look.fontSize;
|
|
283
|
+
popup.style.fontFamily = look.fontFamily;
|
|
284
|
+
if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
|
|
285
|
+
for (const member of members) popup.append(createEntry(member, depth + 1));
|
|
286
|
+
(menu.parentElement ?? doc.body).append(popup);
|
|
287
|
+
place(popup, anchor);
|
|
288
|
+
anchor.setAttribute('aria-expanded', 'true');
|
|
289
|
+
stack.push({id: group.id_, anchor, popup});
|
|
290
|
+
options.onChange?.();
|
|
291
|
+
if (focus) (popup.firstElementChild as HTMLElement | null)?.focus();
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const toggle = (category: PaletteCategory, focus = false) => {
|
|
295
|
+
if (stack[0]?.id === category.id_) close();
|
|
296
|
+
else open(category, category.item_, 0, focus);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const groupItem = (target: EventTarget | null) => {
|
|
300
|
+
if (!(target instanceof doc.defaultView!.Node)) return null;
|
|
301
|
+
return options.categories().find(category => groups.members.has(category.id_) &&
|
|
302
|
+
!parentOf.has(category.id_) && category.item_.contains(target as Node)) ?? null;
|
|
303
|
+
};
|
|
304
|
+
// Scratch Blocks opens a category on mouseup (touchend on touch screens); a group opens its child palette on click.
|
|
305
|
+
for (const type of ['mouseup', 'touchend']) {
|
|
306
|
+
menu.addEventListener(type, event => {
|
|
307
|
+
if (groupItem(event.target)) event.stopPropagation();
|
|
308
|
+
}, capture);
|
|
309
|
+
}
|
|
310
|
+
menu.addEventListener('click', event => {
|
|
311
|
+
const category = groupItem(event.target);
|
|
312
|
+
if (!category) return;
|
|
313
|
+
event.stopPropagation();
|
|
314
|
+
toggle(category);
|
|
315
|
+
}, capture);
|
|
316
|
+
|
|
317
|
+
const contains = (node: Node | null) => !!node && stack.some(({popup}) => popup.contains(node));
|
|
318
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
319
|
+
if (!stack.length) return;
|
|
320
|
+
const target = event.target as Node | null;
|
|
321
|
+
if (contains(target) || stack[0].anchor.contains(target)) return;
|
|
322
|
+
close();
|
|
323
|
+
};
|
|
324
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
325
|
+
if (event.key !== 'Escape' || event.isComposing || !stack.length) return;
|
|
326
|
+
event.preventDefault();
|
|
327
|
+
// The palette's own Escape would close the block list as well.
|
|
328
|
+
event.stopImmediatePropagation();
|
|
329
|
+
const {anchor} = stack[stack.length - 1];
|
|
330
|
+
closeFrom(stack.length - 1);
|
|
331
|
+
anchor.focus();
|
|
332
|
+
};
|
|
333
|
+
doc.addEventListener('pointerdown', onPointerDown, capture);
|
|
334
|
+
doc.addEventListener('keydown', onKeyDown, capture);
|
|
335
|
+
menu.addEventListener('scroll', close, {signal: events.signal});
|
|
336
|
+
doc.defaultView!.addEventListener('resize', close, {signal: events.signal});
|
|
337
|
+
|
|
338
|
+
return {
|
|
339
|
+
update(next) {
|
|
340
|
+
close();
|
|
341
|
+
groups = next;
|
|
342
|
+
parentOf.clear();
|
|
343
|
+
for (const [id, members] of groups.members) for (const member of members) parentOf.set(member, id);
|
|
344
|
+
for (const category of options.categories()) {
|
|
345
|
+
const member = parentOf.has(category.id_);
|
|
346
|
+
const row = category.item_.parentElement;
|
|
347
|
+
if (row && row !== menu) row.hidden = member;
|
|
348
|
+
const group = groups.members.has(category.id_) && !member;
|
|
349
|
+
if (group) {
|
|
350
|
+
category.item_.setAttribute('aria-haspopup', 'true');
|
|
351
|
+
category.item_.setAttribute('aria-expanded', 'false');
|
|
352
|
+
category.item_.setAttribute('data-palette-group', '');
|
|
353
|
+
category.item_.classList.add('tw-palette-group');
|
|
354
|
+
}
|
|
355
|
+
const {setSelected} = category;
|
|
356
|
+
category.setSelected = function (value: boolean) {
|
|
357
|
+
setSelected.call(this, value);
|
|
358
|
+
if (value) selected = category.id_;
|
|
359
|
+
else if (selected === category.id_) selected = null;
|
|
360
|
+
markSelection();
|
|
361
|
+
};
|
|
362
|
+
if (category.item_.classList.contains(selectedClass)) selected = category.id_;
|
|
363
|
+
}
|
|
364
|
+
markSelection();
|
|
365
|
+
},
|
|
366
|
+
isGroup: id => groups.members.has(id) && !parentOf.has(id),
|
|
367
|
+
topLevelId: topLevelOf,
|
|
368
|
+
toggle,
|
|
369
|
+
isOpen: () => stack.length > 0,
|
|
370
|
+
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
371
|
+
contains,
|
|
372
|
+
close,
|
|
373
|
+
dispose() {
|
|
374
|
+
close();
|
|
375
|
+
events.abort();
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
};
|
|
@@ -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
|
}
|
|
@@ -222,11 +249,26 @@ export function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks'>, exte
|
|
|
222
249
|
return [...standardGroups, ...extensionGroups.filter(id => used.has(id))];
|
|
223
250
|
}
|
|
224
251
|
|
|
252
|
+
/** A category for one group of an extension, drawn like the extension's own unless the group has its own look. */
|
|
253
|
+
function groupCategory(category: Element, group: ExtensionPaletteGroup, children: Element[]): Element {
|
|
254
|
+
const part = category.cloneNode(false) as Element;
|
|
255
|
+
part.setAttribute('id', group.id);
|
|
256
|
+
part.setAttribute('name', group.name);
|
|
257
|
+
if (group.color1) part.setAttribute('colour', group.color1);
|
|
258
|
+
if (group.color2) part.setAttribute('secondaryColour', group.color2);
|
|
259
|
+
if (group.menuIconURI) part.setAttribute('iconURI', group.menuIconURI);
|
|
260
|
+
part.append(...children);
|
|
261
|
+
return part;
|
|
262
|
+
}
|
|
263
|
+
|
|
225
264
|
/**
|
|
226
265
|
* Splits each extension category whose extension declares groups into one category per group, in the catalog's
|
|
227
266
|
* order. A label, separator or button goes with the block after it, or with the last block when none follows.
|
|
267
|
+
* With `nested`, the extension's category stays, with its unmarked blocks, and holds the categories of its declared
|
|
268
|
+
* groups, so the palette can show it as a group that opens them (see `./palette-category-groups`).
|
|
228
269
|
*/
|
|
229
|
-
export function splitExtensionCategories(tree: Element, catalog: PaletteGroupCatalog
|
|
270
|
+
export function splitExtensionCategories(tree: Element, catalog: PaletteGroupCatalog,
|
|
271
|
+
{nested = false}: {nested?: boolean} = {}): void {
|
|
230
272
|
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
231
273
|
const extensionId = category.getAttribute('id');
|
|
232
274
|
const groups = catalog.groups.filter(group => group.extensionId === extensionId);
|
|
@@ -244,19 +286,24 @@ export function splitExtensionCategories(tree: Element, catalog: PaletteGroupCat
|
|
|
244
286
|
pending = [];
|
|
245
287
|
}
|
|
246
288
|
if (pending.length) items.set(last, [...items.get(last) ?? [], ...pending]);
|
|
289
|
+
if (nested) {
|
|
290
|
+
category.replaceChildren(...items.get(extensionId) ?? []);
|
|
291
|
+
for (const group of groups) {
|
|
292
|
+
const children = items.get(group.id);
|
|
293
|
+
if (!group.declared || !children) continue;
|
|
294
|
+
const part = groupCategory(category, group, children);
|
|
295
|
+
part.removeAttribute('showStatusButton');
|
|
296
|
+
category.append(part);
|
|
297
|
+
}
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
247
300
|
let first = true;
|
|
248
301
|
for (const group of groups) {
|
|
249
302
|
const children = items.get(group.id);
|
|
250
303
|
if (!children) continue;
|
|
251
|
-
const part = category
|
|
252
|
-
part.setAttribute('id', group.id);
|
|
253
|
-
part.setAttribute('name', group.name);
|
|
254
|
-
if (group.color1) part.setAttribute('colour', group.color1);
|
|
255
|
-
if (group.color2) part.setAttribute('secondaryColour', group.color2);
|
|
256
|
-
if (group.menuIconURI) part.setAttribute('iconURI', group.menuIconURI);
|
|
304
|
+
const part = groupCategory(category, group, children);
|
|
257
305
|
if (!first) part.removeAttribute('showStatusButton');
|
|
258
306
|
first = false;
|
|
259
|
-
part.append(...children);
|
|
260
307
|
category.before(part);
|
|
261
308
|
}
|
|
262
309
|
category.remove();
|
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;
|
|
@@ -980,3 +986,60 @@
|
|
|
980
986
|
.tw-palette-visibility .twextsec-row[data-highlighted] {
|
|
981
987
|
background: color-mix(in srgb, var(--looks-secondary, #7951d1) 16%, transparent);
|
|
982
988
|
}
|
|
989
|
+
|
|
990
|
+
/* The child palettes of the block palette's groups: each floats just above the palette it opens from. */
|
|
991
|
+
.tw-palette-group-popup {
|
|
992
|
+
position: absolute;
|
|
993
|
+
z-index: 40;
|
|
994
|
+
display: flex;
|
|
995
|
+
flex-direction: column;
|
|
996
|
+
padding: 0.25rem;
|
|
997
|
+
border: 1px solid rgb(0 0 0 / 12%);
|
|
998
|
+
border-radius: 0.5rem;
|
|
999
|
+
background: var(--palette-group-background, var(--ui-primary, white));
|
|
1000
|
+
box-shadow: 0 2px 4px rgb(0 0 0 / 16%), 0 6px 16px rgb(0 0 0 / 18%);
|
|
1001
|
+
user-select: none;
|
|
1002
|
+
-webkit-user-select: none;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
.tw-palette-group-popup[data-horizontal] {
|
|
1006
|
+
flex-direction: row;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
.tw-palette-group-item {
|
|
1010
|
+
border-radius: 0.375rem;
|
|
1011
|
+
padding: 0.375rem 0.5rem;
|
|
1012
|
+
white-space: nowrap;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
.tw-palette-group-popup[data-horizontal] > .tw-palette-group-item {
|
|
1016
|
+
padding: 6px 0.65rem;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
.tw-palette-group-item:focus-visible {
|
|
1020
|
+
outline: 2px solid var(--looks-secondary, #855cd6);
|
|
1021
|
+
outline-offset: -2px;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
.tw-palette-group[aria-expanded="true"],
|
|
1025
|
+
.tw-palette-group-item[aria-expanded="true"] {
|
|
1026
|
+
background: rgb(0 0 0 / 8%);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/* A group's icon has a small mark at its corner, as a menu that opens further does. */
|
|
1030
|
+
.tw-palette-group,
|
|
1031
|
+
.tw-palette-group-item[aria-haspopup] {
|
|
1032
|
+
position: relative;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
.tw-palette-group::after,
|
|
1036
|
+
.tw-palette-group-item[aria-haspopup]::after {
|
|
1037
|
+
content: '';
|
|
1038
|
+
position: absolute;
|
|
1039
|
+
right: 0.2rem;
|
|
1040
|
+
bottom: 0.2rem;
|
|
1041
|
+
border-style: solid;
|
|
1042
|
+
border-width: 0 0 0.35rem 0.35rem;
|
|
1043
|
+
border-color: transparent transparent currentColor transparent;
|
|
1044
|
+
opacity: 0.55;
|
|
1045
|
+
}
|