@kubohiroya/turbowarp-plus-editor-code-views 0.16.0 → 0.18.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.
@@ -147,19 +147,26 @@ export interface PaletteCategoryGroupsOptions {
147
147
  /** Called when a child palette opens or the last one closes. */
148
148
  onChange?(): void;
149
149
  /**
150
- * Lets a long press on a member of a child palette drag it to another place among the members of the same group,
151
- * as the palette's own categories can be reordered. Members do not move to another group or level.
150
+ * Lets a long press on a member of a group drag it to another place among the members of the same group, as the
151
+ * palette's own categories can be reordered: in the group's child palette, or where a pinned group shows its
152
+ * members. Members do not move to another group or level.
152
153
  */
153
154
  reorder?: boolean;
154
155
  /** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
155
156
  onReorder?(): void;
156
157
  /**
157
- * Where the members' order is kept, such as the palette settings saved with the project. By default the order
158
- * lasts while the palette does.
158
+ * Where the members' order and the pinned groups are kept, such as the palette settings saved with the project.
159
+ * Palettes given the same one, such as the editor's and the detached one, follow each other. By default they last
160
+ * while the palette does.
159
161
  */
160
- order?(): Pick<PaletteOrder, 'members' | 'setMembers'> | undefined;
162
+ order?(): PaletteGroupsOrder | undefined;
163
+ /** The locale the pin buttons are labelled in. By default, the document's language. */
164
+ locale?(): string;
161
165
  }
162
166
 
167
+ /** The part of a {@link PaletteOrder} the child palettes use. */
168
+ export type PaletteGroupsOrder = Pick<PaletteOrder, 'members' | 'setMembers' | 'isPinned' | 'setPinned' | 'subscribe'>;
169
+
163
170
  export interface PaletteCategoryGroupsController {
164
171
  /**
165
172
  * Prepares a toolbox tree as {@link prepareGroupedToolboxTree} does, and puts each group's members in the order
@@ -168,10 +175,21 @@ export interface PaletteCategoryGroupsController {
168
175
  prepare(tree: Element, options?: {catalog?: PaletteGroupCatalog; visible?: ReadonlySet<string> | null}): PaletteGroups;
169
176
  /** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
170
177
  update(groups: PaletteGroups): void;
178
+ /** The groups the palette was last drawn with, members in their order, to show another palette the same way. */
179
+ current(): PaletteGroups;
180
+ /** Whether the category is a group the palette shows, whose icon opens a child palette. */
171
181
  isGroup(id: string): boolean;
182
+ /**
183
+ * The category whose icon stands for the id in the palette: its own, when the palette shows it, or that of the
184
+ * nearest group the palette shows. A pinned group's members are shown in the palette that shows the group.
185
+ */
186
+ shownAs(id: string): string;
172
187
  /** The id of the outermost group the category is in, or its own id when it is in none. */
173
188
  topLevelId(id: string): string;
174
- /** Opens the group's child palette from its icon, or closes it if it is open. */
189
+ /**
190
+ * Opens the group's child palette from its icon, or closes it if it is open. A pinned group shows its members in
191
+ * place, so its icon only opens its own blocks, if it has any.
192
+ */
175
193
  toggle(category: PaletteCategory, focus?: boolean): void;
176
194
  isOpen(): boolean;
177
195
  /** The icon the palette shows for the category: its own, or that of the group it is in. */
@@ -208,7 +226,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
208
226
  let groups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
209
227
  const parentOf = new Map<string, string>();
210
228
  // The open child palettes, outermost first, with the icon each opened from.
211
- const stack: {id: string; anchor: HTMLElement; popup: HTMLElement; reorder?: {dispose(): void}}[] = [];
229
+ const stack: {id: string; anchor: HTMLElement; popup: HTMLElement; reorders: {dispose(): void}[]}[] = [];
212
230
  // The order members were dragged into, by group, kept while the toolbox is drawn again.
213
231
  const ownOrder = createPaletteOrder();
214
232
  const memberOrder = () => options.order?.() ?? ownOrder;
@@ -237,13 +255,63 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
237
255
  while (parentOf.has(current)) current = parentOf.get(current)!;
238
256
  return current;
239
257
  };
258
+ const isPinned = (id: string) => groups.members.has(id) && memberOrder().isPinned(id);
259
+ // A category is in the palette when every group it is in is pinned open.
260
+ const inPalette = (id: string) => {
261
+ for (let parent = parentOf.get(id); parent !== undefined; parent = parentOf.get(parent)) {
262
+ if (!isPinned(parent)) return false;
263
+ }
264
+ return true;
265
+ };
266
+ const shownAs = (id: string) => {
267
+ let current = id;
268
+ while (!inPalette(current)) current = parentOf.get(current)!;
269
+ return current;
270
+ };
271
+ // Changes this palette makes itself, which it does not need to hear back about.
272
+ let writing = false;
273
+ const quietly = (write: () => void) => {
274
+ writing = true;
275
+ try {
276
+ write();
277
+ } finally {
278
+ writing = false;
279
+ }
280
+ };
281
+ const japanese = () => /^ja(?:-|$)/i.test(options.locale?.() ?? doc.documentElement.lang);
282
+ /** A pin button: pinned, it folds the group back; unpinned, it pins the group open. */
283
+ const pinButton = (pinned: boolean, press: () => void) => {
284
+ const button = doc.createElement('span');
285
+ button.className = 'tw-palette-group-pin';
286
+ button.setAttribute('data-palette-group-pin', pinned ? 'pinned' : 'unpinned');
287
+ button.setAttribute('role', 'button');
288
+ button.tabIndex = 0;
289
+ const label = pinned ? (japanese() ? 'グループの固定を外して、たたむ' : 'Unpin and fold the group') :
290
+ (japanese() ? 'グループをパレットに広げて固定する' : 'Pin the group open in the palette');
291
+ button.setAttribute('aria-label', label);
292
+ button.title = label;
293
+ const pressed = (event: Event) => {
294
+ event.preventDefault();
295
+ event.stopPropagation();
296
+ press();
297
+ };
298
+ button.addEventListener('click', pressed, {signal: events.signal});
299
+ button.addEventListener('keydown', event => {
300
+ if (event.key === 'Enter' || event.key === ' ') pressed(event);
301
+ }, {signal: events.signal});
302
+ // Scratch Blocks would open the category the button sits on.
303
+ for (const type of ['mouseup', 'touchend']) {
304
+ button.addEventListener(type, event => event.stopPropagation(), {signal: events.signal});
305
+ }
306
+ return button;
307
+ };
240
308
 
241
309
  const markSelection = () => {
242
310
  for (const [id] of groups.members) {
243
- if (parentOf.has(id)) continue;
311
+ if (!inPalette(id)) continue;
244
312
  const item = categoryById(id)?.item_;
245
313
  if (!item) continue;
246
- const current = selected !== null && selected !== id && topLevelOf(selected) === id;
314
+ const current = selected !== null && selected !== id && shownAs(selected) === id;
247
315
  item.toggleAttribute('data-palette-group-current', current);
248
316
  if (current) item.classList.add(selectedClass);
249
317
  else if (selected !== id) item.classList.remove(selectedClass);
@@ -253,8 +321,8 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
253
321
  const closeFrom = (depth: number) => {
254
322
  let changed = false;
255
323
  while (stack.length > depth) {
256
- const {anchor, popup, reorder} = stack.pop()!;
257
- reorder?.dispose();
324
+ const {anchor, popup, reorders} = stack.pop()!;
325
+ for (const reorder of reorders) reorder.dispose();
258
326
  anchor.setAttribute('aria-expanded', 'false');
259
327
  popup.remove();
260
328
  changed = true;
@@ -285,11 +353,13 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
285
353
  };
286
354
 
287
355
  const activate = (category: PaletteCategory, entry: HTMLElement, depth: number, focus: boolean, self: boolean) => {
288
- if (!self && groups.members.has(category.id_)) {
356
+ if (!self && groups.members.has(category.id_) && !isPinned(category.id_)) {
289
357
  if (stack[depth]?.id === category.id_) closeFrom(depth);
290
358
  else open(category, entry, depth, focus);
291
359
  return;
292
360
  }
361
+ // A pinned group without blocks of its own shows its members where it is, so there is nothing to open.
362
+ if (!self && groups.members.has(category.id_) && !groups.withOwnContents.has(category.id_)) return;
293
363
  close();
294
364
  options.choose(category);
295
365
  };
@@ -301,9 +371,19 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
301
371
  entry.setAttribute('data-palette-group-item', category.id_);
302
372
  entry.setAttribute('role', 'menuitem');
303
373
  entry.tabIndex = -1;
304
- for (const child of Array.from(category.item_.children)) entry.append(child.cloneNode(true));
374
+ entry.title = category.item_.textContent?.trim() || category.item_.getAttribute('aria-label') || '';
375
+ for (const child of Array.from(category.item_.children)) {
376
+ if (!child.hasAttribute('data-palette-group-pin')) entry.append(child.cloneNode(true));
377
+ }
305
378
  const isGroup = !self && groups.members.has(category.id_);
306
- if (isGroup) {
379
+ if (isGroup && isPinned(category.id_)) {
380
+ entry.setAttribute('data-palette-group-pinned', '');
381
+ // Folding the group back draws this child palette again.
382
+ entry.append(pinButton(true, () => {
383
+ quietly(() => memberOrder().setPinned(category.id_, false));
384
+ reopen(depth - 1);
385
+ }));
386
+ } else if (isGroup) {
307
387
  entry.setAttribute('aria-haspopup', 'true');
308
388
  entry.setAttribute('aria-expanded', 'false');
309
389
  }
@@ -325,7 +405,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
325
405
  const backward = options.horizontal() ? (options.rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp';
326
406
  if (event.key !== forward && event.key !== backward) return;
327
407
  event.preventDefault();
328
- const entries = Array.from(entry.parentElement!.children) as HTMLElement[];
408
+ const entries = Array.from(entry.parentElement!.querySelectorAll(':scope > [data-palette-group-item]')) as HTMLElement[];
329
409
  const index = entries.indexOf(entry) + (event.key === forward ? 1 : -1);
330
410
  entries[(index + entries.length) % entries.length].focus();
331
411
  }, {signal: events.signal});
@@ -355,36 +435,141 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
355
435
  popup.style.fontSize = look.fontSize;
356
436
  popup.style.fontFamily = look.fontFamily;
357
437
  if (options.rtl) popup.dir = 'rtl';
438
+ // Pinning the group shows its members in the palette that shows the group.
439
+ popup.append(pinButton(false, () => pinFrom(depth)));
358
440
  if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
359
- const entries = members.map(member => createEntry(member, depth + 1));
360
- popup.append(...entries);
441
+ // The entries of each group's members shown here: the group's own, and those of pinned member groups.
442
+ const shown = new Map<string, HTMLElement[]>([[group.id_, []]]);
443
+ // A pinned member group is followed by its own members, here rather than in a child palette of its own.
444
+ const add = (member: PaletteCategory, groupId: string) => {
445
+ const entry = createEntry(member, depth + 1);
446
+ shown.get(groupId)!.push(entry);
447
+ popup.append(entry);
448
+ if (!isPinned(member.id_)) return;
449
+ shown.set(member.id_, []);
450
+ for (const id of groups.members.get(member.id_) ?? []) {
451
+ const inner = categoryById(id);
452
+ if (inner) add(inner, member.id_);
453
+ }
454
+ };
455
+ for (const member of members) add(member, group.id_);
361
456
  (menu.parentElement ?? doc.body).append(popup);
362
457
  place(popup, anchor);
363
458
  anchor.setAttribute('aria-expanded', 'true');
364
- // The group's own blocks stay first; its members can be dragged among themselves.
365
- const reorder = options.reorder ? attachPaletteCategoryReorder(popup, () => entries,
366
- (from, to) => {
367
- const order = members.map(member => member.id_);
368
- order.splice(to, 0, order.splice(from, 1)[0]);
369
- memberOrder().setMembers(group.id_, order);
370
- groups.members.set(group.id_, order);
459
+ // The group's own blocks stay first; the members of each group shown here can be dragged among themselves.
460
+ const reorders = options.reorder ? [...shown].filter(([, entries]) => entries.length > 1).map(([groupId, entries]) =>
461
+ attachPaletteCategoryReorder(popup, () => entries, (from, to) => {
462
+ moveMember(groupId, from, to);
371
463
  open(group, anchor, depth, false);
372
464
  options.onReorder?.();
373
- }, {horizontal: options.horizontal, area: entry => entry}) : undefined;
374
- stack.push({id: group.id_, anchor, popup, reorder});
465
+ }, {horizontal: options.horizontal, area: entry => entry})) : [];
466
+ stack.push({id: group.id_, anchor, popup, reorders});
375
467
  options.onChange?.();
376
468
  if (focus) (popup.firstElementChild as HTMLElement | null)?.focus();
377
469
  };
378
470
 
379
471
  const toggle = (category: PaletteCategory, focus = false) => {
380
- if (stack[0]?.id === category.id_) close();
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
+ /** Moves a member of the group, as dragged, and keeps the order. */
499
+ function moveMember(groupId: string, from: number, to: number) {
500
+ const order = [...(groups.members.get(groupId) ?? [])];
501
+ order.splice(to, 0, order.splice(from, 1)[0]);
502
+ quietly(() => memberOrder().setMembers(groupId, order));
503
+ groups.members.set(groupId, order);
504
+ }
505
+ // Where the palette shows a pinned group's members, they can be dragged among themselves.
506
+ let inlineReorders: {dispose(): void}[] = [];
507
+ function attachInlineReorders() {
508
+ for (const reorder of inlineReorders) reorder.dispose();
509
+ inlineReorders = [];
510
+ if (!options.reorder) return;
511
+ for (const [id, members] of groups.members) {
512
+ if (members.length < 2 || !isPinned(id) || !inPalette(id)) continue;
513
+ const items = () => (groups.members.get(id) ?? []).map(member => categoryById(member)?.item_)
514
+ .filter((item): item is HTMLElement => Boolean(item));
515
+ inlineReorders.push(attachPaletteCategoryReorder(menu, items, (from, to) => {
516
+ moveMember(id, from, to);
517
+ close();
518
+ applyVisibility();
519
+ options.onReorder?.();
520
+ }, {horizontal: options.horizontal, ends: false}));
521
+ }
522
+ }
523
+ // Shows the categories every group of which is pinned open, hides the others, and marks the groups shown.
524
+ function applyVisibility() {
525
+ for (const category of options.categories()) {
526
+ const id = category.id_;
527
+ const item = category.item_;
528
+ const row = item.parentElement;
529
+ if (row && row !== menu) row.hidden = !inPalette(id);
530
+ const group = groups.members.has(id) && inPalette(id);
531
+ if (group) {
532
+ item.setAttribute('aria-haspopup', 'true');
533
+ if (!item.hasAttribute('aria-expanded')) item.setAttribute('aria-expanded', 'false');
534
+ item.setAttribute('data-palette-group', '');
535
+ item.classList.add('tw-palette-group');
536
+ }
537
+ const pinned = group && isPinned(id);
538
+ item.toggleAttribute('data-palette-group-pinned', pinned);
539
+ const button = item.querySelector(':scope > [data-palette-group-pin]');
540
+ if (pinned && !button) {
541
+ item.append(pinButton(true, () => {
542
+ quietly(() => memberOrder().setPinned(id, false));
543
+ close();
544
+ applyVisibility();
545
+ }));
546
+ } else if (!pinned) button?.remove();
547
+ }
548
+ attachInlineReorders();
549
+ markSelection();
550
+ options.onChange?.();
551
+ }
552
+ // Another palette given the same order, or a project loaded, may pin or fold groups.
553
+ let watched: PaletteGroupsOrder | undefined;
554
+ let unwatch: (() => void) | undefined;
555
+ const watchOrder = () => {
556
+ const order = memberOrder();
557
+ if (order === watched) return;
558
+ unwatch?.();
559
+ watched = order;
560
+ unwatch = order.subscribe(() => {
561
+ if (writing) return;
562
+ close();
563
+ applyVisibility();
564
+ });
565
+ };
383
566
 
384
567
  const groupItem = (target: EventTarget | null) => {
385
568
  if (!(target instanceof doc.defaultView!.Node)) return null;
569
+ // A pin button handles its own press.
570
+ if ((target as Element).closest?.('[data-palette-group-pin]')) return null;
386
571
  return options.categories().find(category => groups.members.has(category.id_) &&
387
- !parentOf.has(category.id_) && category.item_.contains(target as Node)) ?? null;
572
+ inPalette(category.id_) && category.item_.contains(target as Node)) ?? null;
388
573
  };
389
574
  // Scratch Blocks opens a category on mouseup (touchend on touch screens); a group opens its child palette on click.
390
575
  for (const type of ['mouseup', 'touchend']) {
@@ -451,17 +636,8 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
451
636
  update(next) {
452
637
  close();
453
638
  useGroups(next);
639
+ watchOrder();
454
640
  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
641
  const {setSelected} = category;
466
642
  category.setSelected = function (value: boolean) {
467
643
  setSelected.call(this, value);
@@ -471,13 +647,15 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
471
647
  };
472
648
  if (category.item_.classList.contains(selectedClass)) selected = category.id_;
473
649
  }
474
- markSelection();
650
+ applyVisibility();
475
651
  },
476
- isGroup: id => groups.members.has(id) && !parentOf.has(id),
652
+ current: () => groups,
653
+ isGroup: id => groups.members.has(id) && inPalette(id),
654
+ shownAs,
477
655
  topLevelId: topLevelOf,
478
656
  toggle,
479
657
  isOpen: () => stack.length > 0,
480
- anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
658
+ anchorOf: category => categoryById(shownAs(category.id_))?.item_ ?? category.item_,
481
659
  arrange(categories, rank) {
482
660
  const position = new Map<string, number>();
483
661
  const visit = (id: string) => {
@@ -511,19 +689,21 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
511
689
  syncButtons(openId) {
512
690
  const horizontal = options.horizontal();
513
691
  for (const category of options.categories()) {
514
- if (!groups.members.has(category.id_) || parentOf.has(category.id_)) continue;
692
+ if (!groups.members.has(category.id_) || !inPalette(category.id_)) continue;
515
693
  const item = category.item_;
516
694
  if (horizontal) item.tabIndex = 0;
517
695
  else item.removeAttribute('tabindex');
518
696
  item.setAttribute('role', 'button');
519
697
  item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
520
- topLevelOf(openId) === category.id_);
698
+ shownAs(openId) === category.id_);
521
699
  }
522
700
  },
523
701
  contains,
524
702
  close,
525
703
  dispose() {
526
704
  close();
705
+ for (const reorder of inlineReorders) reorder.dispose();
706
+ unwatch?.();
527
707
  events.abort();
528
708
  }
529
709
  };
@@ -9,6 +9,11 @@ export interface PaletteCategoryReorderOptions {
9
9
  horizontal?(): boolean;
10
10
  /** The element an item takes up, which a drop over it lands on. By default, the row that holds the item. */
11
11
  area?(item: HTMLElement): Element;
12
+ /**
13
+ * Whether a drop before the first item or after the last one lands at that end. On by default; off, a drop
14
+ * anywhere but over an item moves nothing, as when the items share the menu with others.
15
+ */
16
+ ends?: boolean;
12
17
  }
13
18
 
14
19
  /**
@@ -54,7 +59,7 @@ export function attachPaletteCategoryReorder(
54
59
  const hit = doc.elementFromPoint(event.clientX, event.clientY);
55
60
  if (!items.length || !menu.contains(hit)) return;
56
61
  const item = items.find(candidate => area(candidate).contains(hit));
57
- if (item) return item;
62
+ if (item || options.ends === false) return item;
58
63
 
59
64
  const first = items[0];
60
65
  const last = items[items.length - 1];
@@ -334,10 +334,14 @@ export interface SavedPaletteVisibility {
334
334
  order?: SavedPaletteOrder;
335
335
  }
336
336
 
337
- /** The order the palette's categories were dragged into: the top level, and the members of each group, by ID. */
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
+ */
338
341
  export interface SavedPaletteOrder {
339
342
  categories?: string[];
340
343
  groups?: Record<string, string[]>;
344
+ pinned?: string[];
341
345
  }
342
346
 
343
347
  /** The order the palette's categories were dragged into, for the whole project. */
@@ -348,7 +352,12 @@ export interface PaletteOrder {
348
352
  /** A group's members, by ID, in order, or nothing when they were never reordered. */
349
353
  members(groupId: string): string[] | undefined;
350
354
  setMembers(groupId: string, ids: readonly string[]): void;
351
- /** The order to save, or nothing when nothing was reordered. */
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. */
352
361
  capture(): SavedPaletteOrder | undefined;
353
362
  /** Takes a saved order. A value that is not one leaves the palette in its own order. */
354
363
  apply(value: unknown): void;
@@ -359,39 +368,66 @@ export interface PaletteOrder {
359
368
  export function createPaletteOrder({onChange}: {onChange?(): void} = {}): PaletteOrder {
360
369
  let categories: string[] = [];
361
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
+ };
362
380
  return {
363
381
  categories: () => [...categories],
364
382
  setCategories(ids) {
365
383
  categories = [...ids];
366
- onChange?.();
384
+ changed();
367
385
  },
368
386
  members: groupId => groups.has(groupId) ? [...groups.get(groupId)!] : undefined,
369
387
  setMembers(groupId, ids) {
370
388
  groups.set(groupId, [...ids]);
371
- onChange?.();
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);
372
401
  },
373
402
  capture() {
374
403
  const saved: SavedPaletteOrder = {};
375
404
  if (categories.length) saved.categories = [...categories];
376
405
  if (groups.size) saved.groups = Object.fromEntries([...groups].map(([id, ids]) => [id, [...ids]]));
377
- return saved.categories || saved.groups ? saved : undefined;
406
+ if (pinned.size) saved.pinned = [...pinned];
407
+ return saved.categories || saved.groups || saved.pinned ? saved : undefined;
378
408
  },
379
409
  apply(value) {
380
410
  categories = [];
381
411
  groups = new Map();
382
- if (!value || typeof value !== 'object') return;
383
- const saved = value as Record<string, unknown>;
384
- categories = strings(saved.categories) ?? [];
385
- if (saved.groups && typeof saved.groups === 'object' && !Array.isArray(saved.groups)) {
386
- for (const [id, ids] of Object.entries(saved.groups as Record<string, unknown>)) {
387
- const members = strings(ids);
388
- if (members) groups.set(id, members);
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
+ }
389
421
  }
422
+ pinned = new Set(strings(saved.pinned) ?? []);
390
423
  }
424
+ notify();
391
425
  },
392
426
  reset() {
393
427
  categories = [];
394
428
  groups = new Map();
429
+ pinned = new Set();
430
+ notify();
395
431
  }
396
432
  };
397
433
  }
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
+ }