@kubohiroya/turbowarp-plus-editor-code-views 0.15.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 +3 -3
- package/README.md +3 -3
- 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 +31 -2
- package/dist/palette-category-groups.js +234 -39
- package/dist/palette-category-visibility.d.ts +38 -1
- package/dist/palette-category-visibility.js +86 -3
- 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 +232 -37
- package/src/palette-category-visibility.ts +113 -3
- package/src/styles.css +61 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {filterToolboxTree, splitExtensionCategories} from './palette-category-visibility.ts';
|
|
2
|
-
import
|
|
2
|
+
import {createPaletteOrder} from './palette-category-visibility.ts';
|
|
3
|
+
import type {PaletteGroupCatalog, PaletteOrder} from './palette-category-visibility.ts';
|
|
3
4
|
import {attachPaletteCategoryReorder} from './palette-category-reorder.ts';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -152,15 +153,42 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
152
153
|
reorder?: boolean;
|
|
153
154
|
/** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
|
|
154
155
|
onReorder?(): void;
|
|
156
|
+
/**
|
|
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.
|
|
160
|
+
*/
|
|
161
|
+
order?(): PaletteGroupsOrder | undefined;
|
|
162
|
+
/** The locale the pin buttons are labelled in. By default, the document's language. */
|
|
163
|
+
locale?(): string;
|
|
155
164
|
}
|
|
156
165
|
|
|
166
|
+
/** The part of a {@link PaletteOrder} the child palettes use. */
|
|
167
|
+
export type PaletteGroupsOrder = Pick<PaletteOrder, 'members' | 'setMembers' | 'isPinned' | 'setPinned' | 'subscribe'>;
|
|
168
|
+
|
|
157
169
|
export interface PaletteCategoryGroupsController {
|
|
170
|
+
/**
|
|
171
|
+
* Prepares a toolbox tree as {@link prepareGroupedToolboxTree} does, and puts each group's members in the order
|
|
172
|
+
* they were dragged into, so the palette is drawn in that order. Pass the groups returned to `update`.
|
|
173
|
+
*/
|
|
174
|
+
prepare(tree: Element, options?: {catalog?: PaletteGroupCatalog; visible?: ReadonlySet<string> | null}): PaletteGroups;
|
|
158
175
|
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
159
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. */
|
|
160
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;
|
|
161
186
|
/** The id of the outermost group the category is in, or its own id when it is in none. */
|
|
162
187
|
topLevelId(id: string): string;
|
|
163
|
-
/**
|
|
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
|
+
*/
|
|
164
192
|
toggle(category: PaletteCategory, focus?: boolean): void;
|
|
165
193
|
isOpen(): boolean;
|
|
166
194
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
@@ -199,18 +227,25 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
199
227
|
// The open child palettes, outermost first, with the icon each opened from.
|
|
200
228
|
const stack: {id: string; anchor: HTMLElement; popup: HTMLElement; reorder?: {dispose(): void}}[] = [];
|
|
201
229
|
// The order members were dragged into, by group, kept while the toolbox is drawn again.
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
230
|
+
const ownOrder = createPaletteOrder();
|
|
231
|
+
const memberOrder = () => options.order?.() ?? ownOrder;
|
|
232
|
+
const applyMemberOrder = (target: PaletteGroups) => {
|
|
233
|
+
for (const [id, members] of target.members) {
|
|
234
|
+
const saved = memberOrder().members(id);
|
|
206
235
|
if (!saved) continue;
|
|
207
236
|
const rank = (member: string) => {
|
|
208
237
|
const index = saved.indexOf(member);
|
|
209
238
|
return index < 0 ? saved.length : index;
|
|
210
239
|
};
|
|
211
|
-
|
|
240
|
+
target.members.set(id, members.slice().sort((a, b) => rank(a) - rank(b)));
|
|
212
241
|
}
|
|
213
242
|
};
|
|
243
|
+
const useGroups = (next: PaletteGroups) => {
|
|
244
|
+
groups = next;
|
|
245
|
+
applyMemberOrder(groups);
|
|
246
|
+
parentOf.clear();
|
|
247
|
+
for (const [id, members] of groups.members) for (const member of members) parentOf.set(member, id);
|
|
248
|
+
};
|
|
214
249
|
let selected: string | null = null;
|
|
215
250
|
|
|
216
251
|
const categoryById = (id: string) => options.categories().find(category => category.id_ === id);
|
|
@@ -219,13 +254,63 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
219
254
|
while (parentOf.has(current)) current = parentOf.get(current)!;
|
|
220
255
|
return current;
|
|
221
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
|
+
};
|
|
222
307
|
|
|
223
308
|
const markSelection = () => {
|
|
224
309
|
for (const [id] of groups.members) {
|
|
225
|
-
if (
|
|
310
|
+
if (!inPalette(id)) continue;
|
|
226
311
|
const item = categoryById(id)?.item_;
|
|
227
312
|
if (!item) continue;
|
|
228
|
-
const current = selected !== null && selected !== id &&
|
|
313
|
+
const current = selected !== null && selected !== id && shownAs(selected) === id;
|
|
229
314
|
item.toggleAttribute('data-palette-group-current', current);
|
|
230
315
|
if (current) item.classList.add(selectedClass);
|
|
231
316
|
else if (selected !== id) item.classList.remove(selectedClass);
|
|
@@ -267,11 +352,13 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
267
352
|
};
|
|
268
353
|
|
|
269
354
|
const activate = (category: PaletteCategory, entry: HTMLElement, depth: number, focus: boolean, self: boolean) => {
|
|
270
|
-
if (!self && groups.members.has(category.id_)) {
|
|
355
|
+
if (!self && groups.members.has(category.id_) && !isPinned(category.id_)) {
|
|
271
356
|
if (stack[depth]?.id === category.id_) closeFrom(depth);
|
|
272
357
|
else open(category, entry, depth, focus);
|
|
273
358
|
return;
|
|
274
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;
|
|
275
362
|
close();
|
|
276
363
|
options.choose(category);
|
|
277
364
|
};
|
|
@@ -283,9 +370,19 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
283
370
|
entry.setAttribute('data-palette-group-item', category.id_);
|
|
284
371
|
entry.setAttribute('role', 'menuitem');
|
|
285
372
|
entry.tabIndex = -1;
|
|
286
|
-
|
|
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
|
+
}
|
|
287
377
|
const isGroup = !self && groups.members.has(category.id_);
|
|
288
|
-
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) {
|
|
289
386
|
entry.setAttribute('aria-haspopup', 'true');
|
|
290
387
|
entry.setAttribute('aria-expanded', 'false');
|
|
291
388
|
}
|
|
@@ -307,7 +404,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
307
404
|
const backward = options.horizontal() ? (options.rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp';
|
|
308
405
|
if (event.key !== forward && event.key !== backward) return;
|
|
309
406
|
event.preventDefault();
|
|
310
|
-
const entries = Array.from(entry.parentElement!.
|
|
407
|
+
const entries = Array.from(entry.parentElement!.querySelectorAll(':scope > [data-palette-group-item]')) as HTMLElement[];
|
|
311
408
|
const index = entries.indexOf(entry) + (event.key === forward ? 1 : -1);
|
|
312
409
|
entries[(index + entries.length) % entries.length].focus();
|
|
313
410
|
}, {signal: events.signal});
|
|
@@ -337,9 +434,22 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
337
434
|
popup.style.fontSize = look.fontSize;
|
|
338
435
|
popup.style.fontFamily = look.fontFamily;
|
|
339
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)));
|
|
340
439
|
if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
|
|
341
|
-
const entries =
|
|
342
|
-
|
|
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);
|
|
343
453
|
(menu.parentElement ?? doc.body).append(popup);
|
|
344
454
|
place(popup, anchor);
|
|
345
455
|
anchor.setAttribute('aria-expanded', 'true');
|
|
@@ -348,7 +458,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
348
458
|
(from, to) => {
|
|
349
459
|
const order = members.map(member => member.id_);
|
|
350
460
|
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
351
|
-
memberOrder.
|
|
461
|
+
quietly(() => memberOrder().setMembers(group.id_, order));
|
|
352
462
|
groups.members.set(group.id_, order);
|
|
353
463
|
open(group, anchor, depth, false);
|
|
354
464
|
options.onReorder?.();
|
|
@@ -359,14 +469,81 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
359
469
|
};
|
|
360
470
|
|
|
361
471
|
const toggle = (category: PaletteCategory, focus = false) => {
|
|
362
|
-
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();
|
|
363
476
|
else open(category, category.item_, 0, focus);
|
|
364
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
|
+
};
|
|
365
540
|
|
|
366
541
|
const groupItem = (target: EventTarget | null) => {
|
|
367
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;
|
|
368
545
|
return options.categories().find(category => groups.members.has(category.id_) &&
|
|
369
|
-
|
|
546
|
+
inPalette(category.id_) && category.item_.contains(target as Node)) ?? null;
|
|
370
547
|
};
|
|
371
548
|
// Scratch Blocks opens a category on mouseup (touchend on touch screens); a group opens its child palette on click.
|
|
372
549
|
for (const type of ['mouseup', 'touchend']) {
|
|
@@ -403,23 +580,38 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
403
580
|
doc.defaultView!.addEventListener('resize', close, {signal: events.signal});
|
|
404
581
|
|
|
405
582
|
return {
|
|
583
|
+
prepare(tree, prepareOptions) {
|
|
584
|
+
const prepared = prepareGroupedToolboxTree(tree, prepareOptions);
|
|
585
|
+
close();
|
|
586
|
+
useGroups(prepared);
|
|
587
|
+
// Each group is followed by its members, each with its own members, in their order.
|
|
588
|
+
const elements = Array.from(tree.children).filter(isCategory);
|
|
589
|
+
const byId = new Map(elements.map(element => [element.getAttribute('id')!, element]));
|
|
590
|
+
const ordered: Element[] = [];
|
|
591
|
+
const visit = (element: Element) => {
|
|
592
|
+
ordered.push(element);
|
|
593
|
+
for (const member of groups.members.get(element.getAttribute('id')!) ?? []) {
|
|
594
|
+
const child = byId.get(member);
|
|
595
|
+
if (child) visit(child);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
for (const element of elements) if (!element.hasAttribute(groupParentAttribute)) visit(element);
|
|
599
|
+
const slots = elements.map(element => {
|
|
600
|
+
const slot = tree.ownerDocument.createComment('palette-category');
|
|
601
|
+
element.replaceWith(slot);
|
|
602
|
+
return slot;
|
|
603
|
+
});
|
|
604
|
+
slots.forEach((slot, index) => {
|
|
605
|
+
if (ordered[index]) slot.replaceWith(ordered[index]);
|
|
606
|
+
else slot.remove();
|
|
607
|
+
});
|
|
608
|
+
return prepared;
|
|
609
|
+
},
|
|
406
610
|
update(next) {
|
|
407
611
|
close();
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
parentOf.clear();
|
|
411
|
-
for (const [id, members] of groups.members) for (const member of members) parentOf.set(member, id);
|
|
612
|
+
useGroups(next);
|
|
613
|
+
watchOrder();
|
|
412
614
|
for (const category of options.categories()) {
|
|
413
|
-
const member = parentOf.has(category.id_);
|
|
414
|
-
const row = category.item_.parentElement;
|
|
415
|
-
if (row && row !== menu) row.hidden = member;
|
|
416
|
-
const group = groups.members.has(category.id_) && !member;
|
|
417
|
-
if (group) {
|
|
418
|
-
category.item_.setAttribute('aria-haspopup', 'true');
|
|
419
|
-
category.item_.setAttribute('aria-expanded', 'false');
|
|
420
|
-
category.item_.setAttribute('data-palette-group', '');
|
|
421
|
-
category.item_.classList.add('tw-palette-group');
|
|
422
|
-
}
|
|
423
615
|
const {setSelected} = category;
|
|
424
616
|
category.setSelected = function (value: boolean) {
|
|
425
617
|
setSelected.call(this, value);
|
|
@@ -429,13 +621,15 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
429
621
|
};
|
|
430
622
|
if (category.item_.classList.contains(selectedClass)) selected = category.id_;
|
|
431
623
|
}
|
|
432
|
-
|
|
624
|
+
applyVisibility();
|
|
433
625
|
},
|
|
434
|
-
|
|
626
|
+
current: () => groups,
|
|
627
|
+
isGroup: id => groups.members.has(id) && inPalette(id),
|
|
628
|
+
shownAs,
|
|
435
629
|
topLevelId: topLevelOf,
|
|
436
630
|
toggle,
|
|
437
631
|
isOpen: () => stack.length > 0,
|
|
438
|
-
anchorOf: category => categoryById(
|
|
632
|
+
anchorOf: category => categoryById(shownAs(category.id_))?.item_ ?? category.item_,
|
|
439
633
|
arrange(categories, rank) {
|
|
440
634
|
const position = new Map<string, number>();
|
|
441
635
|
const visit = (id: string) => {
|
|
@@ -469,19 +663,20 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
469
663
|
syncButtons(openId) {
|
|
470
664
|
const horizontal = options.horizontal();
|
|
471
665
|
for (const category of options.categories()) {
|
|
472
|
-
if (!groups.members.has(category.id_) ||
|
|
666
|
+
if (!groups.members.has(category.id_) || !inPalette(category.id_)) continue;
|
|
473
667
|
const item = category.item_;
|
|
474
668
|
if (horizontal) item.tabIndex = 0;
|
|
475
669
|
else item.removeAttribute('tabindex');
|
|
476
670
|
item.setAttribute('role', 'button');
|
|
477
671
|
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
478
|
-
|
|
672
|
+
shownAs(openId) === category.id_);
|
|
479
673
|
}
|
|
480
674
|
},
|
|
481
675
|
contains,
|
|
482
676
|
close,
|
|
483
677
|
dispose() {
|
|
484
678
|
close();
|
|
679
|
+
unwatch?.();
|
|
485
680
|
events.abort();
|
|
486
681
|
}
|
|
487
682
|
};
|
|
@@ -331,6 +331,105 @@ export interface SavedPaletteVisibility {
|
|
|
331
331
|
stage?: {groups: string[]};
|
|
332
332
|
sprites?: Record<string, {groups: string[]}>;
|
|
333
333
|
sets?: Record<string, SavedGroupSet>;
|
|
334
|
+
order?: SavedPaletteOrder;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The order the palette's categories were dragged into: the top level, and the members of each group, by ID; and the
|
|
339
|
+
* groups pinned open, whose members the palette shows in place of the group's child palette.
|
|
340
|
+
*/
|
|
341
|
+
export interface SavedPaletteOrder {
|
|
342
|
+
categories?: string[];
|
|
343
|
+
groups?: Record<string, string[]>;
|
|
344
|
+
pinned?: string[];
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The order the palette's categories were dragged into, for the whole project. */
|
|
348
|
+
export interface PaletteOrder {
|
|
349
|
+
/** The top-level categories, by ID, in order. Categories not listed follow them. */
|
|
350
|
+
categories(): string[];
|
|
351
|
+
setCategories(ids: readonly string[]): void;
|
|
352
|
+
/** A group's members, by ID, in order, or nothing when they were never reordered. */
|
|
353
|
+
members(groupId: string): string[] | undefined;
|
|
354
|
+
setMembers(groupId: string, ids: readonly string[]): void;
|
|
355
|
+
/** Whether the group is pinned open, its members shown in the palette that shows the group. */
|
|
356
|
+
isPinned(groupId: string): boolean;
|
|
357
|
+
setPinned(groupId: string, pinned: boolean): void;
|
|
358
|
+
/** Calls `listener` after anything changes, including a loaded or forgotten order. Returns the function that stops. */
|
|
359
|
+
subscribe(listener: () => void): () => void;
|
|
360
|
+
/** The order to save, or nothing when nothing was reordered or pinned. */
|
|
361
|
+
capture(): SavedPaletteOrder | undefined;
|
|
362
|
+
/** Takes a saved order. A value that is not one leaves the palette in its own order. */
|
|
363
|
+
apply(value: unknown): void;
|
|
364
|
+
reset(): void;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Keeps the order of the palette's categories. `onChange` is called when the user reorders them. */
|
|
368
|
+
export function createPaletteOrder({onChange}: {onChange?(): void} = {}): PaletteOrder {
|
|
369
|
+
let categories: string[] = [];
|
|
370
|
+
let groups = new Map<string, string[]>();
|
|
371
|
+
let pinned = new Set<string>();
|
|
372
|
+
const listeners = new Set<() => void>();
|
|
373
|
+
const notify = () => {
|
|
374
|
+
for (const listener of [...listeners]) listener();
|
|
375
|
+
};
|
|
376
|
+
const changed = () => {
|
|
377
|
+
onChange?.();
|
|
378
|
+
notify();
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
categories: () => [...categories],
|
|
382
|
+
setCategories(ids) {
|
|
383
|
+
categories = [...ids];
|
|
384
|
+
changed();
|
|
385
|
+
},
|
|
386
|
+
members: groupId => groups.has(groupId) ? [...groups.get(groupId)!] : undefined,
|
|
387
|
+
setMembers(groupId, ids) {
|
|
388
|
+
groups.set(groupId, [...ids]);
|
|
389
|
+
changed();
|
|
390
|
+
},
|
|
391
|
+
isPinned: groupId => pinned.has(groupId),
|
|
392
|
+
setPinned(groupId, value) {
|
|
393
|
+
if (pinned.has(groupId) === value) return;
|
|
394
|
+
if (value) pinned.add(groupId);
|
|
395
|
+
else pinned.delete(groupId);
|
|
396
|
+
changed();
|
|
397
|
+
},
|
|
398
|
+
subscribe(listener) {
|
|
399
|
+
listeners.add(listener);
|
|
400
|
+
return () => listeners.delete(listener);
|
|
401
|
+
},
|
|
402
|
+
capture() {
|
|
403
|
+
const saved: SavedPaletteOrder = {};
|
|
404
|
+
if (categories.length) saved.categories = [...categories];
|
|
405
|
+
if (groups.size) saved.groups = Object.fromEntries([...groups].map(([id, ids]) => [id, [...ids]]));
|
|
406
|
+
if (pinned.size) saved.pinned = [...pinned];
|
|
407
|
+
return saved.categories || saved.groups || saved.pinned ? saved : undefined;
|
|
408
|
+
},
|
|
409
|
+
apply(value) {
|
|
410
|
+
categories = [];
|
|
411
|
+
groups = new Map();
|
|
412
|
+
pinned = new Set();
|
|
413
|
+
if (value && typeof value === 'object') {
|
|
414
|
+
const saved = value as Record<string, unknown>;
|
|
415
|
+
categories = strings(saved.categories) ?? [];
|
|
416
|
+
if (saved.groups && typeof saved.groups === 'object' && !Array.isArray(saved.groups)) {
|
|
417
|
+
for (const [id, ids] of Object.entries(saved.groups as Record<string, unknown>)) {
|
|
418
|
+
const members = strings(ids);
|
|
419
|
+
if (members) groups.set(id, members);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
pinned = new Set(strings(saved.pinned) ?? []);
|
|
423
|
+
}
|
|
424
|
+
notify();
|
|
425
|
+
},
|
|
426
|
+
reset() {
|
|
427
|
+
categories = [];
|
|
428
|
+
groups = new Map();
|
|
429
|
+
pinned = new Set();
|
|
430
|
+
notify();
|
|
431
|
+
}
|
|
432
|
+
};
|
|
334
433
|
}
|
|
335
434
|
|
|
336
435
|
const strings = (value: unknown): string[] | undefined =>
|
|
@@ -357,13 +456,18 @@ export interface PaletteVisibilityStore {
|
|
|
357
456
|
apply(value: unknown, targets: readonly PaletteTarget[]): void;
|
|
358
457
|
/** Forgets every setting, for a project without any. */
|
|
359
458
|
reset(): void;
|
|
459
|
+
/** The order of the palette's categories, saved and loaded with the other settings. */
|
|
460
|
+
order: PaletteOrder;
|
|
360
461
|
}
|
|
361
462
|
|
|
362
463
|
/** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
|
|
363
|
-
export function createPaletteVisibilityStore({catalog}: {
|
|
464
|
+
export function createPaletteVisibilityStore({catalog, onOrderChange}: {
|
|
364
465
|
/** The extension groups on offer now, so the default counts the blocks of declared groups. */
|
|
365
466
|
catalog?: () => PaletteGroupCatalog | undefined;
|
|
467
|
+
/** Called when the user reorders the palette's categories, so the project can be marked as changed. */
|
|
468
|
+
onOrderChange?(): void;
|
|
366
469
|
} = {}): PaletteVisibilityStore {
|
|
470
|
+
const order = createPaletteOrder({onChange: onOrderChange});
|
|
367
471
|
const defaults = (target: PaletteTarget, extensionIds: readonly string[]) =>
|
|
368
472
|
defaultVisibleGroups(target, extensionIds, catalog?.());
|
|
369
473
|
let groups = new Map<string, string[]>();
|
|
@@ -395,13 +499,17 @@ export function createPaletteVisibilityStore({catalog}: {
|
|
|
395
499
|
}
|
|
396
500
|
if (Object.keys(sprites).length) saved.sprites = sprites;
|
|
397
501
|
if (Object.keys(projectSets).length) saved.sets = structuredClone(projectSets);
|
|
398
|
-
|
|
502
|
+
const arranged = order.capture();
|
|
503
|
+
if (arranged) saved.order = arranged;
|
|
504
|
+
return saved.stage || saved.sprites || saved.sets || saved.order ? saved : undefined;
|
|
399
505
|
},
|
|
400
506
|
apply(value, targets) {
|
|
401
507
|
groups = new Map();
|
|
402
508
|
projectSets = {};
|
|
509
|
+
order.reset();
|
|
403
510
|
if (!value || typeof value !== 'object' || (value as {version?: unknown}).version !== 1) return;
|
|
404
511
|
const saved = value as Record<string, unknown>;
|
|
512
|
+
order.apply(saved.order);
|
|
405
513
|
const setting = (entry: unknown) => entry && typeof entry === 'object' ?
|
|
406
514
|
strings((entry as {groups?: unknown}).groups) : undefined;
|
|
407
515
|
const sprites = saved.sprites && typeof saved.sprites === 'object' ? saved.sprites as Record<string, unknown> : {};
|
|
@@ -421,7 +529,9 @@ export function createPaletteVisibilityStore({catalog}: {
|
|
|
421
529
|
reset() {
|
|
422
530
|
groups = new Map();
|
|
423
531
|
projectSets = {};
|
|
424
|
-
|
|
532
|
+
order.reset();
|
|
533
|
+
},
|
|
534
|
+
order
|
|
425
535
|
};
|
|
426
536
|
}
|
|
427
537
|
|
package/src/styles.css
CHANGED
|
@@ -103,6 +103,7 @@
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
.tw-detached-palette__content {
|
|
106
|
+
position: relative;
|
|
106
107
|
min-height: 0;
|
|
107
108
|
display: grid;
|
|
108
109
|
grid-template: "categories blocks" minmax(0, 1fr) / auto minmax(0, 1fr);
|
|
@@ -155,7 +156,16 @@
|
|
|
155
156
|
cursor: pointer;
|
|
156
157
|
}
|
|
157
158
|
|
|
159
|
+
.tw-detached-palette__category {
|
|
160
|
+
display: contents;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.tw-detached-palette__category[hidden] {
|
|
164
|
+
display: none;
|
|
165
|
+
}
|
|
166
|
+
|
|
158
167
|
.tw-detached-palette__categories button {
|
|
168
|
+
position: relative;
|
|
159
169
|
display: grid;
|
|
160
170
|
place-items: center;
|
|
161
171
|
inline-size: 40px;
|
|
@@ -1043,3 +1053,54 @@
|
|
|
1043
1053
|
border-color: transparent transparent currentColor transparent;
|
|
1044
1054
|
opacity: 0.55;
|
|
1045
1055
|
}
|
|
1056
|
+
|
|
1057
|
+
/* The pin of a group: a child palette pins its group open; a pinned group's icon folds it back. */
|
|
1058
|
+
.tw-palette-group-pin {
|
|
1059
|
+
position: absolute;
|
|
1060
|
+
top: 1px;
|
|
1061
|
+
inset-inline-end: 1px;
|
|
1062
|
+
z-index: 1;
|
|
1063
|
+
inline-size: 14px;
|
|
1064
|
+
block-size: 14px;
|
|
1065
|
+
border-radius: 3px;
|
|
1066
|
+
background-color: currentColor;
|
|
1067
|
+
opacity: 0.6;
|
|
1068
|
+
cursor: pointer;
|
|
1069
|
+
mask: url("data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 16 16%22 fill=%22none%22 stroke=%22black%22 stroke-width=%221.4%22 stroke-linejoin=%22round%22%3E%3Cpath d=%22M9 2 14 7 12 8l-2 3-5-5 3-2 1-2Z%22/%3E%3Cpath d=%22m7.5 8.5-4 4%22/%3E%3Cpath d=%22M2 2l12 12%22 stroke-linecap=%22round%22/%3E%3C/svg%3E") center / 12px 12px no-repeat;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
.tw-palette-group-pin[data-palette-group-pin="pinned"] {
|
|
1073
|
+
mask-image: url("data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 16 16%22 fill=%22none%22 stroke=%22black%22 stroke-width=%221.4%22 stroke-linejoin=%22round%22%3E%3Cpath d=%22M9 2 14 7 12 8l-2 3-5-5 3-2 1-2Z%22/%3E%3Cpath d=%22m7.5 8.5-4 4%22/%3E%3C/svg%3E");
|
|
1074
|
+
opacity: 0.85;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
.tw-palette-group-pin:hover,
|
|
1078
|
+
.tw-palette-group-pin:focus-visible {
|
|
1079
|
+
opacity: 1;
|
|
1080
|
+
outline: none;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/* Room for the pin at the child palette's top right. */
|
|
1084
|
+
.tw-palette-group-popup {
|
|
1085
|
+
padding-top: 1rem;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
.tw-palette-group-popup[data-horizontal] {
|
|
1089
|
+
padding-top: 0.25rem;
|
|
1090
|
+
padding-inline-end: 1.1rem;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
.tw-palette-group-popup > .tw-palette-group-pin {
|
|
1094
|
+
top: 3px;
|
|
1095
|
+
inset-inline-end: 3px;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/* A pinned group shows its pin in place of the mark that it opens further. */
|
|
1099
|
+
.tw-palette-group[data-palette-group-pinned]::after,
|
|
1100
|
+
.tw-palette-group-item[data-palette-group-pinned]::after {
|
|
1101
|
+
display: none;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
.tw-palette-group-item[data-palette-group-pinned] {
|
|
1105
|
+
position: relative;
|
|
1106
|
+
}
|