@kubohiroya/turbowarp-plus-editor-code-views 0.16.0 → 0.17.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 -2
- package/README.md +2 -2
- package/dist/detached-block-palette-view.js +50 -7
- package/dist/detached-block-palette.d.ts +11 -1
- package/dist/detached-block-palette.js +2 -2
- package/dist/palette-category-groups.d.ts +20 -4
- package/dist/palette-category-groups.js +186 -28
- package/dist/palette-category-visibility.d.ts +11 -2
- package/dist/palette-category-visibility.js +43 -12
- package/package.json +1 -1
- package/src/detached-block-palette-view.tsx +50 -4
- package/src/detached-block-palette.ts +10 -2
- package/src/palette-category-groups.ts +183 -30
- package/src/palette-category-visibility.ts +48 -12
- package/src/styles.css +61 -0
|
@@ -265,16 +265,40 @@ export const paletteScreenStateKey = 'blockPalette';
|
|
|
265
265
|
export function createPaletteOrder({ onChange } = {}) {
|
|
266
266
|
let categories = [];
|
|
267
267
|
let groups = new Map();
|
|
268
|
+
let pinned = new Set();
|
|
269
|
+
const listeners = new Set();
|
|
270
|
+
const notify = () => {
|
|
271
|
+
for (const listener of [...listeners])
|
|
272
|
+
listener();
|
|
273
|
+
};
|
|
274
|
+
const changed = () => {
|
|
275
|
+
onChange?.();
|
|
276
|
+
notify();
|
|
277
|
+
};
|
|
268
278
|
return {
|
|
269
279
|
categories: () => [...categories],
|
|
270
280
|
setCategories(ids) {
|
|
271
281
|
categories = [...ids];
|
|
272
|
-
|
|
282
|
+
changed();
|
|
273
283
|
},
|
|
274
284
|
members: groupId => groups.has(groupId) ? [...groups.get(groupId)] : undefined,
|
|
275
285
|
setMembers(groupId, ids) {
|
|
276
286
|
groups.set(groupId, [...ids]);
|
|
277
|
-
|
|
287
|
+
changed();
|
|
288
|
+
},
|
|
289
|
+
isPinned: groupId => pinned.has(groupId),
|
|
290
|
+
setPinned(groupId, value) {
|
|
291
|
+
if (pinned.has(groupId) === value)
|
|
292
|
+
return;
|
|
293
|
+
if (value)
|
|
294
|
+
pinned.add(groupId);
|
|
295
|
+
else
|
|
296
|
+
pinned.delete(groupId);
|
|
297
|
+
changed();
|
|
298
|
+
},
|
|
299
|
+
subscribe(listener) {
|
|
300
|
+
listeners.add(listener);
|
|
301
|
+
return () => listeners.delete(listener);
|
|
278
302
|
},
|
|
279
303
|
capture() {
|
|
280
304
|
const saved = {};
|
|
@@ -282,26 +306,33 @@ export function createPaletteOrder({ onChange } = {}) {
|
|
|
282
306
|
saved.categories = [...categories];
|
|
283
307
|
if (groups.size)
|
|
284
308
|
saved.groups = Object.fromEntries([...groups].map(([id, ids]) => [id, [...ids]]));
|
|
285
|
-
|
|
309
|
+
if (pinned.size)
|
|
310
|
+
saved.pinned = [...pinned];
|
|
311
|
+
return saved.categories || saved.groups || saved.pinned ? saved : undefined;
|
|
286
312
|
},
|
|
287
313
|
apply(value) {
|
|
288
314
|
categories = [];
|
|
289
315
|
groups = new Map();
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
316
|
+
pinned = new Set();
|
|
317
|
+
if (value && typeof value === 'object') {
|
|
318
|
+
const saved = value;
|
|
319
|
+
categories = strings(saved.categories) ?? [];
|
|
320
|
+
if (saved.groups && typeof saved.groups === 'object' && !Array.isArray(saved.groups)) {
|
|
321
|
+
for (const [id, ids] of Object.entries(saved.groups)) {
|
|
322
|
+
const members = strings(ids);
|
|
323
|
+
if (members)
|
|
324
|
+
groups.set(id, members);
|
|
325
|
+
}
|
|
299
326
|
}
|
|
327
|
+
pinned = new Set(strings(saved.pinned) ?? []);
|
|
300
328
|
}
|
|
329
|
+
notify();
|
|
301
330
|
},
|
|
302
331
|
reset() {
|
|
303
332
|
categories = [];
|
|
304
333
|
groups = new Map();
|
|
334
|
+
pinned = new Set();
|
|
335
|
+
notify();
|
|
305
336
|
}
|
|
306
337
|
};
|
|
307
338
|
}
|
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.17.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,6 +1,10 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import {IconMenuButton} from '@kubohiroya/turbowarp-plus-editor-windows';
|
|
3
3
|
import type {PaletteItem, PaletteSource} from './detached-block-palette.ts';
|
|
4
|
+
import {attachPaletteCategoryGroups} from './palette-category-groups.ts';
|
|
5
|
+
import type {PaletteCategoryGroupsController, PaletteGroups} from './palette-category-groups.ts';
|
|
6
|
+
|
|
7
|
+
const noGroups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
4
8
|
|
|
5
9
|
const horizontalLayoutIconPath = 'M2 5h5v6H2zm7 0h5v6H9z';
|
|
6
10
|
const verticalLayoutIconPath = 'M5 2h6v5H5zm0 7h6v5H5z';
|
|
@@ -37,6 +41,46 @@ export default function Palette({source, locale, categoryIcons, close, startDrag
|
|
|
37
41
|
const selected = categories.find(category => category.id === categoryId) ?? categories[0];
|
|
38
42
|
const selectedId = selected?.id;
|
|
39
43
|
React.useLayoutEffect(() => source.subscribe(refresh), [source]);
|
|
44
|
+
|
|
45
|
+
// The editor's groups open in child palettes here too, and pinning one shows its members in this list.
|
|
46
|
+
const list = React.useRef<HTMLDivElement>(null);
|
|
47
|
+
const groupsControl = React.useRef<PaletteCategoryGroupsController | null>(null);
|
|
48
|
+
const layout = React.useRef(vertical);
|
|
49
|
+
layout.current = vertical;
|
|
50
|
+
const grouping = source.grouping?.();
|
|
51
|
+
// Opening a child palette or pinning a group only draws the list again; the blocks shown stay as they are.
|
|
52
|
+
const [, redraw] = React.useReducer(value => value + 1, 0);
|
|
53
|
+
React.useLayoutEffect(() => {
|
|
54
|
+
const menu = list.current;
|
|
55
|
+
const controller = attachPaletteCategoryGroups(menu, {
|
|
56
|
+
categories: () => source.categories().map(({id, label}) => ({
|
|
57
|
+
id_: id, name_: label, setSelected() {},
|
|
58
|
+
item_: menu.querySelector<HTMLElement>(`button[data-category-id="${CSS.escape(id)}"]`)
|
|
59
|
+
})).filter(category => category.item_),
|
|
60
|
+
// A row of categories, when the blocks are laid out below them.
|
|
61
|
+
horizontal: () => layout.current,
|
|
62
|
+
choose: category => {
|
|
63
|
+
setCategory(category.id_);
|
|
64
|
+
setTooltip(null);
|
|
65
|
+
},
|
|
66
|
+
order: () => source.grouping?.()?.order,
|
|
67
|
+
locale: () => locale,
|
|
68
|
+
onChange: redraw
|
|
69
|
+
});
|
|
70
|
+
groupsControl.current = controller;
|
|
71
|
+
return () => {
|
|
72
|
+
groupsControl.current = null;
|
|
73
|
+
controller.dispose();
|
|
74
|
+
};
|
|
75
|
+
}, [source, locale]);
|
|
76
|
+
const categoryKey = categories.map(category => category.id).join('\n');
|
|
77
|
+
React.useLayoutEffect(() => {
|
|
78
|
+
groupsControl.current?.update(grouping?.groups ?? noGroups);
|
|
79
|
+
}, [categoryKey, grouping?.groups, vertical]);
|
|
80
|
+
React.useLayoutEffect(() => grouping?.order.subscribe(redraw), [grouping?.order]);
|
|
81
|
+
// A group stands for the selected category while its child palette holds it.
|
|
82
|
+
const pressed = (id: string) => id === selectedId ||
|
|
83
|
+
(!!selectedId && groupsControl.current?.isGroup(id) && groupsControl.current.shownAs(selectedId) === id);
|
|
40
84
|
React.useLayoutEffect(() => {
|
|
41
85
|
setItems(selectedId ? source.read(selectedId) : []);
|
|
42
86
|
}, [source, selectedId, revision]);
|
|
@@ -53,12 +97,14 @@ export default function Palette({source, locale, categoryIcons, close, startDrag
|
|
|
53
97
|
|
|
54
98
|
return <main className="tw-detached-palette" data-palette-orientation={vertical ? 'vertical' : 'horizontal'}>
|
|
55
99
|
<div className="tw-detached-palette__content">
|
|
56
|
-
<div className="tw-detached-palette__categories" role="group"
|
|
57
|
-
onScroll={() => setTooltip(null)}>
|
|
100
|
+
<div ref={list} className="tw-detached-palette__categories" role="group"
|
|
101
|
+
aria-label={ja ? 'ブロックのカテゴリ' : 'Block categories'} onScroll={() => setTooltip(null)}>
|
|
58
102
|
{categories.map(({id, label, color, image}) => {
|
|
59
103
|
const artwork = categoryIcons[id];
|
|
60
104
|
const backgroundImage = image || (artwork ? `url("${artwork}")` : undefined);
|
|
61
|
-
|
|
105
|
+
// Each category sits in a row of its own, which a group hides while it holds the category.
|
|
106
|
+
return <span key={id} className="tw-detached-palette__category"><button type="button" data-category-id={id}
|
|
107
|
+
aria-label={label} aria-pressed={Boolean(pressed(id))}
|
|
62
108
|
onMouseEnter={event => showLabel(event.currentTarget, label)}
|
|
63
109
|
onMouseLeave={() => setTooltip(null)}
|
|
64
110
|
onFocus={event => showLabel(event.currentTarget, label)}
|
|
@@ -71,7 +117,7 @@ export default function Palette({source, locale, categoryIcons, close, startDrag
|
|
|
71
117
|
<span aria-hidden="true" className="tw-detached-palette__category-icon" style={{backgroundColor: color, backgroundImage}}>
|
|
72
118
|
{!backgroundImage && <LayoutIcon path="M2 4h5V2h3v2h4v8H2z" />}
|
|
73
119
|
</span>
|
|
74
|
-
</button>;
|
|
120
|
+
</button></span>;
|
|
75
121
|
})}
|
|
76
122
|
</div>
|
|
77
123
|
<div className="tw-detached-palette__blocks" role="group" aria-label={selected?.label}>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import ReactDOM from 'react-dom';
|
|
3
3
|
import Palette from './detached-block-palette-view.tsx';
|
|
4
|
+
import type {PaletteGroups, PaletteGroupsOrder} from './palette-category-groups.ts';
|
|
4
5
|
|
|
5
6
|
type Point = {x: number; y: number};
|
|
6
7
|
type PaletteCategory = {id: string; label: string; color?: string; image?: string};
|
|
@@ -11,9 +12,13 @@ export type PaletteItem = {
|
|
|
11
12
|
insert?(point?: Point): void;
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
/** The groups of the editor's palette, which the detached one shows the same way, and the order and pins they share. */
|
|
16
|
+
export type PaletteGrouping = {groups: PaletteGroups; order: PaletteGroupsOrder};
|
|
17
|
+
|
|
14
18
|
export type PaletteSource = {
|
|
15
19
|
surface: SVGSVGElement;
|
|
16
20
|
categories(): PaletteCategory[];
|
|
21
|
+
grouping?(): PaletteGrouping | undefined;
|
|
17
22
|
read(id: string): PaletteItem[];
|
|
18
23
|
subscribe(refresh: () => void): () => void;
|
|
19
24
|
setDetached(detached: boolean): void;
|
|
@@ -97,12 +102,15 @@ function registerDetachedBlockPalette(workspace: object, source: PaletteSource)
|
|
|
97
102
|
};
|
|
98
103
|
}
|
|
99
104
|
|
|
100
|
-
export function connectDetachedBlockPalette({blocks, workspace, flyout, menu, categories, show, setDetached}: {
|
|
105
|
+
export function connectDetachedBlockPalette({blocks, workspace, flyout, menu, categories, grouping, show, setDetached}: {
|
|
101
106
|
blocks: any;
|
|
102
107
|
workspace: any;
|
|
103
108
|
flyout: any;
|
|
104
109
|
menu: HTMLElement;
|
|
110
|
+
/** Every category, members of groups included, in the palette's order. */
|
|
105
111
|
categories(): PaletteCategory[];
|
|
112
|
+
/** The palette's groups, so the detached palette opens them in child palettes too. */
|
|
113
|
+
grouping?(): PaletteGrouping | undefined;
|
|
106
114
|
show(id: string): void;
|
|
107
115
|
setDetached(detached: boolean): void;
|
|
108
116
|
}) {
|
|
@@ -119,7 +127,7 @@ export function connectDetachedBlockPalette({blocks, workspace, flyout, menu, ca
|
|
|
119
127
|
return position.matrixTransform(matrix.inverse());
|
|
120
128
|
};
|
|
121
129
|
return registerDetachedBlockPalette(workspace, {
|
|
122
|
-
surface, categories, setDetached,
|
|
130
|
+
surface, categories, grouping, setDetached,
|
|
123
131
|
read(id) {
|
|
124
132
|
show(id);
|
|
125
133
|
const items: PaletteItem[] = [];
|
|
@@ -154,12 +154,18 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
154
154
|
/** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
|
|
155
155
|
onReorder?(): void;
|
|
156
156
|
/**
|
|
157
|
-
* Where the members' order
|
|
158
|
-
*
|
|
157
|
+
* Where the members' order and the pinned groups are kept, such as the palette settings saved with the project.
|
|
158
|
+
* Palettes given the same one, such as the editor's and the detached one, follow each other. By default they last
|
|
159
|
+
* while the palette does.
|
|
159
160
|
*/
|
|
160
|
-
order?():
|
|
161
|
+
order?(): PaletteGroupsOrder | undefined;
|
|
162
|
+
/** The locale the pin buttons are labelled in. By default, the document's language. */
|
|
163
|
+
locale?(): string;
|
|
161
164
|
}
|
|
162
165
|
|
|
166
|
+
/** The part of a {@link PaletteOrder} the child palettes use. */
|
|
167
|
+
export type PaletteGroupsOrder = Pick<PaletteOrder, 'members' | 'setMembers' | 'isPinned' | 'setPinned' | 'subscribe'>;
|
|
168
|
+
|
|
163
169
|
export interface PaletteCategoryGroupsController {
|
|
164
170
|
/**
|
|
165
171
|
* Prepares a toolbox tree as {@link prepareGroupedToolboxTree} does, and puts each group's members in the order
|
|
@@ -168,10 +174,21 @@ export interface PaletteCategoryGroupsController {
|
|
|
168
174
|
prepare(tree: Element, options?: {catalog?: PaletteGroupCatalog; visible?: ReadonlySet<string> | null}): PaletteGroups;
|
|
169
175
|
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
170
176
|
update(groups: PaletteGroups): void;
|
|
177
|
+
/** The groups the palette was last drawn with, members in their order, to show another palette the same way. */
|
|
178
|
+
current(): PaletteGroups;
|
|
179
|
+
/** Whether the category is a group the palette shows, whose icon opens a child palette. */
|
|
171
180
|
isGroup(id: string): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* The category whose icon stands for the id in the palette: its own, when the palette shows it, or that of the
|
|
183
|
+
* nearest group the palette shows. A pinned group's members are shown in the palette that shows the group.
|
|
184
|
+
*/
|
|
185
|
+
shownAs(id: string): string;
|
|
172
186
|
/** The id of the outermost group the category is in, or its own id when it is in none. */
|
|
173
187
|
topLevelId(id: string): string;
|
|
174
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Opens the group's child palette from its icon, or closes it if it is open. A pinned group shows its members in
|
|
190
|
+
* place, so its icon only opens its own blocks, if it has any.
|
|
191
|
+
*/
|
|
175
192
|
toggle(category: PaletteCategory, focus?: boolean): void;
|
|
176
193
|
isOpen(): boolean;
|
|
177
194
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
@@ -237,13 +254,63 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
237
254
|
while (parentOf.has(current)) current = parentOf.get(current)!;
|
|
238
255
|
return current;
|
|
239
256
|
};
|
|
257
|
+
const isPinned = (id: string) => groups.members.has(id) && memberOrder().isPinned(id);
|
|
258
|
+
// A category is in the palette when every group it is in is pinned open.
|
|
259
|
+
const inPalette = (id: string) => {
|
|
260
|
+
for (let parent = parentOf.get(id); parent !== undefined; parent = parentOf.get(parent)) {
|
|
261
|
+
if (!isPinned(parent)) return false;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
};
|
|
265
|
+
const shownAs = (id: string) => {
|
|
266
|
+
let current = id;
|
|
267
|
+
while (!inPalette(current)) current = parentOf.get(current)!;
|
|
268
|
+
return current;
|
|
269
|
+
};
|
|
270
|
+
// Changes this palette makes itself, which it does not need to hear back about.
|
|
271
|
+
let writing = false;
|
|
272
|
+
const quietly = (write: () => void) => {
|
|
273
|
+
writing = true;
|
|
274
|
+
try {
|
|
275
|
+
write();
|
|
276
|
+
} finally {
|
|
277
|
+
writing = false;
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
const japanese = () => /^ja(?:-|$)/i.test(options.locale?.() ?? doc.documentElement.lang);
|
|
281
|
+
/** A pin button: pinned, it folds the group back; unpinned, it pins the group open. */
|
|
282
|
+
const pinButton = (pinned: boolean, press: () => void) => {
|
|
283
|
+
const button = doc.createElement('span');
|
|
284
|
+
button.className = 'tw-palette-group-pin';
|
|
285
|
+
button.setAttribute('data-palette-group-pin', pinned ? 'pinned' : 'unpinned');
|
|
286
|
+
button.setAttribute('role', 'button');
|
|
287
|
+
button.tabIndex = 0;
|
|
288
|
+
const label = pinned ? (japanese() ? 'グループの固定を外して、たたむ' : 'Unpin and fold the group') :
|
|
289
|
+
(japanese() ? 'グループをパレットに広げて固定する' : 'Pin the group open in the palette');
|
|
290
|
+
button.setAttribute('aria-label', label);
|
|
291
|
+
button.title = label;
|
|
292
|
+
const pressed = (event: Event) => {
|
|
293
|
+
event.preventDefault();
|
|
294
|
+
event.stopPropagation();
|
|
295
|
+
press();
|
|
296
|
+
};
|
|
297
|
+
button.addEventListener('click', pressed, {signal: events.signal});
|
|
298
|
+
button.addEventListener('keydown', event => {
|
|
299
|
+
if (event.key === 'Enter' || event.key === ' ') pressed(event);
|
|
300
|
+
}, {signal: events.signal});
|
|
301
|
+
// Scratch Blocks would open the category the button sits on.
|
|
302
|
+
for (const type of ['mouseup', 'touchend']) {
|
|
303
|
+
button.addEventListener(type, event => event.stopPropagation(), {signal: events.signal});
|
|
304
|
+
}
|
|
305
|
+
return button;
|
|
306
|
+
};
|
|
240
307
|
|
|
241
308
|
const markSelection = () => {
|
|
242
309
|
for (const [id] of groups.members) {
|
|
243
|
-
if (
|
|
310
|
+
if (!inPalette(id)) continue;
|
|
244
311
|
const item = categoryById(id)?.item_;
|
|
245
312
|
if (!item) continue;
|
|
246
|
-
const current = selected !== null && selected !== id &&
|
|
313
|
+
const current = selected !== null && selected !== id && shownAs(selected) === id;
|
|
247
314
|
item.toggleAttribute('data-palette-group-current', current);
|
|
248
315
|
if (current) item.classList.add(selectedClass);
|
|
249
316
|
else if (selected !== id) item.classList.remove(selectedClass);
|
|
@@ -285,11 +352,13 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
285
352
|
};
|
|
286
353
|
|
|
287
354
|
const activate = (category: PaletteCategory, entry: HTMLElement, depth: number, focus: boolean, self: boolean) => {
|
|
288
|
-
if (!self && groups.members.has(category.id_)) {
|
|
355
|
+
if (!self && groups.members.has(category.id_) && !isPinned(category.id_)) {
|
|
289
356
|
if (stack[depth]?.id === category.id_) closeFrom(depth);
|
|
290
357
|
else open(category, entry, depth, focus);
|
|
291
358
|
return;
|
|
292
359
|
}
|
|
360
|
+
// A pinned group without blocks of its own shows its members where it is, so there is nothing to open.
|
|
361
|
+
if (!self && groups.members.has(category.id_) && !groups.withOwnContents.has(category.id_)) return;
|
|
293
362
|
close();
|
|
294
363
|
options.choose(category);
|
|
295
364
|
};
|
|
@@ -301,9 +370,19 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
301
370
|
entry.setAttribute('data-palette-group-item', category.id_);
|
|
302
371
|
entry.setAttribute('role', 'menuitem');
|
|
303
372
|
entry.tabIndex = -1;
|
|
304
|
-
|
|
373
|
+
entry.title = category.item_.textContent?.trim() || category.item_.getAttribute('aria-label') || '';
|
|
374
|
+
for (const child of Array.from(category.item_.children)) {
|
|
375
|
+
if (!child.hasAttribute('data-palette-group-pin')) entry.append(child.cloneNode(true));
|
|
376
|
+
}
|
|
305
377
|
const isGroup = !self && groups.members.has(category.id_);
|
|
306
|
-
if (isGroup) {
|
|
378
|
+
if (isGroup && isPinned(category.id_)) {
|
|
379
|
+
entry.setAttribute('data-palette-group-pinned', '');
|
|
380
|
+
// Folding the group back draws this child palette again.
|
|
381
|
+
entry.append(pinButton(true, () => {
|
|
382
|
+
quietly(() => memberOrder().setPinned(category.id_, false));
|
|
383
|
+
reopen(depth - 1);
|
|
384
|
+
}));
|
|
385
|
+
} else if (isGroup) {
|
|
307
386
|
entry.setAttribute('aria-haspopup', 'true');
|
|
308
387
|
entry.setAttribute('aria-expanded', 'false');
|
|
309
388
|
}
|
|
@@ -325,7 +404,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
325
404
|
const backward = options.horizontal() ? (options.rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp';
|
|
326
405
|
if (event.key !== forward && event.key !== backward) return;
|
|
327
406
|
event.preventDefault();
|
|
328
|
-
const entries = Array.from(entry.parentElement!.
|
|
407
|
+
const entries = Array.from(entry.parentElement!.querySelectorAll(':scope > [data-palette-group-item]')) as HTMLElement[];
|
|
329
408
|
const index = entries.indexOf(entry) + (event.key === forward ? 1 : -1);
|
|
330
409
|
entries[(index + entries.length) % entries.length].focus();
|
|
331
410
|
}, {signal: events.signal});
|
|
@@ -355,9 +434,22 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
355
434
|
popup.style.fontSize = look.fontSize;
|
|
356
435
|
popup.style.fontFamily = look.fontFamily;
|
|
357
436
|
if (options.rtl) popup.dir = 'rtl';
|
|
437
|
+
// Pinning the group shows its members in the palette that shows the group.
|
|
438
|
+
popup.append(pinButton(false, () => pinFrom(depth)));
|
|
358
439
|
if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
|
|
359
|
-
const entries =
|
|
360
|
-
|
|
440
|
+
const entries: HTMLElement[] = [];
|
|
441
|
+
// A pinned member group is followed by its own members, here rather than in a child palette of its own.
|
|
442
|
+
const add = (member: PaletteCategory, direct: boolean) => {
|
|
443
|
+
const entry = createEntry(member, depth + 1);
|
|
444
|
+
if (direct) entries.push(entry);
|
|
445
|
+
popup.append(entry);
|
|
446
|
+
if (!isPinned(member.id_)) return;
|
|
447
|
+
for (const id of groups.members.get(member.id_) ?? []) {
|
|
448
|
+
const inner = categoryById(id);
|
|
449
|
+
if (inner) add(inner, false);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
for (const member of members) add(member, true);
|
|
361
453
|
(menu.parentElement ?? doc.body).append(popup);
|
|
362
454
|
place(popup, anchor);
|
|
363
455
|
anchor.setAttribute('aria-expanded', 'true');
|
|
@@ -366,7 +458,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
366
458
|
(from, to) => {
|
|
367
459
|
const order = members.map(member => member.id_);
|
|
368
460
|
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
369
|
-
memberOrder().setMembers(group.id_, order);
|
|
461
|
+
quietly(() => memberOrder().setMembers(group.id_, order));
|
|
370
462
|
groups.members.set(group.id_, order);
|
|
371
463
|
open(group, anchor, depth, false);
|
|
372
464
|
options.onReorder?.();
|
|
@@ -377,14 +469,81 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
377
469
|
};
|
|
378
470
|
|
|
379
471
|
const toggle = (category: PaletteCategory, focus = false) => {
|
|
380
|
-
if (
|
|
472
|
+
if (isPinned(category.id_)) {
|
|
473
|
+
close();
|
|
474
|
+
if (groups.withOwnContents.has(category.id_)) options.choose(category);
|
|
475
|
+
} else if (stack[0]?.id === category.id_) close();
|
|
381
476
|
else open(category, category.item_, 0, focus);
|
|
382
477
|
};
|
|
478
|
+
// Draws the child palette at the depth again, or the palette itself.
|
|
479
|
+
function reopen(depth: number) {
|
|
480
|
+
if (depth < 0 || !stack[depth]) {
|
|
481
|
+
close();
|
|
482
|
+
applyVisibility();
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const {id, anchor} = stack[depth];
|
|
486
|
+
const group = categoryById(id);
|
|
487
|
+
if (group) open(group, anchor, depth, false);
|
|
488
|
+
else closeFrom(depth);
|
|
489
|
+
}
|
|
490
|
+
// Pins open the group of the child palette at the depth: its members join the palette it opened from.
|
|
491
|
+
function pinFrom(depth: number) {
|
|
492
|
+
const {id} = stack[depth];
|
|
493
|
+
quietly(() => memberOrder().setPinned(id, true));
|
|
494
|
+
closeFrom(depth);
|
|
495
|
+
if (depth > 0) reopen(depth - 1);
|
|
496
|
+
else applyVisibility();
|
|
497
|
+
}
|
|
498
|
+
// Shows the categories every group of which is pinned open, hides the others, and marks the groups shown.
|
|
499
|
+
function applyVisibility() {
|
|
500
|
+
for (const category of options.categories()) {
|
|
501
|
+
const id = category.id_;
|
|
502
|
+
const item = category.item_;
|
|
503
|
+
const row = item.parentElement;
|
|
504
|
+
if (row && row !== menu) row.hidden = !inPalette(id);
|
|
505
|
+
const group = groups.members.has(id) && inPalette(id);
|
|
506
|
+
if (group) {
|
|
507
|
+
item.setAttribute('aria-haspopup', 'true');
|
|
508
|
+
if (!item.hasAttribute('aria-expanded')) item.setAttribute('aria-expanded', 'false');
|
|
509
|
+
item.setAttribute('data-palette-group', '');
|
|
510
|
+
item.classList.add('tw-palette-group');
|
|
511
|
+
}
|
|
512
|
+
const pinned = group && isPinned(id);
|
|
513
|
+
item.toggleAttribute('data-palette-group-pinned', pinned);
|
|
514
|
+
const button = item.querySelector(':scope > [data-palette-group-pin]');
|
|
515
|
+
if (pinned && !button) {
|
|
516
|
+
item.append(pinButton(true, () => {
|
|
517
|
+
quietly(() => memberOrder().setPinned(id, false));
|
|
518
|
+
close();
|
|
519
|
+
applyVisibility();
|
|
520
|
+
}));
|
|
521
|
+
} else if (!pinned) button?.remove();
|
|
522
|
+
}
|
|
523
|
+
markSelection();
|
|
524
|
+
options.onChange?.();
|
|
525
|
+
}
|
|
526
|
+
// Another palette given the same order, or a project loaded, may pin or fold groups.
|
|
527
|
+
let watched: PaletteGroupsOrder | undefined;
|
|
528
|
+
let unwatch: (() => void) | undefined;
|
|
529
|
+
const watchOrder = () => {
|
|
530
|
+
const order = memberOrder();
|
|
531
|
+
if (order === watched) return;
|
|
532
|
+
unwatch?.();
|
|
533
|
+
watched = order;
|
|
534
|
+
unwatch = order.subscribe(() => {
|
|
535
|
+
if (writing) return;
|
|
536
|
+
close();
|
|
537
|
+
applyVisibility();
|
|
538
|
+
});
|
|
539
|
+
};
|
|
383
540
|
|
|
384
541
|
const groupItem = (target: EventTarget | null) => {
|
|
385
542
|
if (!(target instanceof doc.defaultView!.Node)) return null;
|
|
543
|
+
// A pin button handles its own press.
|
|
544
|
+
if ((target as Element).closest?.('[data-palette-group-pin]')) return null;
|
|
386
545
|
return options.categories().find(category => groups.members.has(category.id_) &&
|
|
387
|
-
|
|
546
|
+
inPalette(category.id_) && category.item_.contains(target as Node)) ?? null;
|
|
388
547
|
};
|
|
389
548
|
// Scratch Blocks opens a category on mouseup (touchend on touch screens); a group opens its child palette on click.
|
|
390
549
|
for (const type of ['mouseup', 'touchend']) {
|
|
@@ -451,17 +610,8 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
451
610
|
update(next) {
|
|
452
611
|
close();
|
|
453
612
|
useGroups(next);
|
|
613
|
+
watchOrder();
|
|
454
614
|
for (const category of options.categories()) {
|
|
455
|
-
const member = parentOf.has(category.id_);
|
|
456
|
-
const row = category.item_.parentElement;
|
|
457
|
-
if (row && row !== menu) row.hidden = member;
|
|
458
|
-
const group = groups.members.has(category.id_) && !member;
|
|
459
|
-
if (group) {
|
|
460
|
-
category.item_.setAttribute('aria-haspopup', 'true');
|
|
461
|
-
category.item_.setAttribute('aria-expanded', 'false');
|
|
462
|
-
category.item_.setAttribute('data-palette-group', '');
|
|
463
|
-
category.item_.classList.add('tw-palette-group');
|
|
464
|
-
}
|
|
465
615
|
const {setSelected} = category;
|
|
466
616
|
category.setSelected = function (value: boolean) {
|
|
467
617
|
setSelected.call(this, value);
|
|
@@ -471,13 +621,15 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
471
621
|
};
|
|
472
622
|
if (category.item_.classList.contains(selectedClass)) selected = category.id_;
|
|
473
623
|
}
|
|
474
|
-
|
|
624
|
+
applyVisibility();
|
|
475
625
|
},
|
|
476
|
-
|
|
626
|
+
current: () => groups,
|
|
627
|
+
isGroup: id => groups.members.has(id) && inPalette(id),
|
|
628
|
+
shownAs,
|
|
477
629
|
topLevelId: topLevelOf,
|
|
478
630
|
toggle,
|
|
479
631
|
isOpen: () => stack.length > 0,
|
|
480
|
-
anchorOf: category => categoryById(
|
|
632
|
+
anchorOf: category => categoryById(shownAs(category.id_))?.item_ ?? category.item_,
|
|
481
633
|
arrange(categories, rank) {
|
|
482
634
|
const position = new Map<string, number>();
|
|
483
635
|
const visit = (id: string) => {
|
|
@@ -511,19 +663,20 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
511
663
|
syncButtons(openId) {
|
|
512
664
|
const horizontal = options.horizontal();
|
|
513
665
|
for (const category of options.categories()) {
|
|
514
|
-
if (!groups.members.has(category.id_) ||
|
|
666
|
+
if (!groups.members.has(category.id_) || !inPalette(category.id_)) continue;
|
|
515
667
|
const item = category.item_;
|
|
516
668
|
if (horizontal) item.tabIndex = 0;
|
|
517
669
|
else item.removeAttribute('tabindex');
|
|
518
670
|
item.setAttribute('role', 'button');
|
|
519
671
|
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
520
|
-
|
|
672
|
+
shownAs(openId) === category.id_);
|
|
521
673
|
}
|
|
522
674
|
},
|
|
523
675
|
contains,
|
|
524
676
|
close,
|
|
525
677
|
dispose() {
|
|
526
678
|
close();
|
|
679
|
+
unwatch?.();
|
|
527
680
|
events.abort();
|
|
528
681
|
}
|
|
529
682
|
};
|