@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.
@@ -128,7 +128,40 @@ export interface SavedPaletteVisibility {
128
128
  groups: string[];
129
129
  }>;
130
130
  sets?: Record<string, SavedGroupSet>;
131
+ order?: SavedPaletteOrder;
131
132
  }
133
+ /**
134
+ * The order the palette's categories were dragged into: the top level, and the members of each group, by ID; and the
135
+ * groups pinned open, whose members the palette shows in place of the group's child palette.
136
+ */
137
+ export interface SavedPaletteOrder {
138
+ categories?: string[];
139
+ groups?: Record<string, string[]>;
140
+ pinned?: string[];
141
+ }
142
+ /** The order the palette's categories were dragged into, for the whole project. */
143
+ export interface PaletteOrder {
144
+ /** The top-level categories, by ID, in order. Categories not listed follow them. */
145
+ categories(): string[];
146
+ setCategories(ids: readonly string[]): void;
147
+ /** A group's members, by ID, in order, or nothing when they were never reordered. */
148
+ members(groupId: string): string[] | undefined;
149
+ setMembers(groupId: string, ids: readonly string[]): void;
150
+ /** Whether the group is pinned open, its members shown in the palette that shows the group. */
151
+ isPinned(groupId: string): boolean;
152
+ setPinned(groupId: string, pinned: boolean): void;
153
+ /** Calls `listener` after anything changes, including a loaded or forgotten order. Returns the function that stops. */
154
+ subscribe(listener: () => void): () => void;
155
+ /** The order to save, or nothing when nothing was reordered or pinned. */
156
+ capture(): SavedPaletteOrder | undefined;
157
+ /** Takes a saved order. A value that is not one leaves the palette in its own order. */
158
+ apply(value: unknown): void;
159
+ reset(): void;
160
+ }
161
+ /** Keeps the order of the palette's categories. `onChange` is called when the user reorders them. */
162
+ export declare function createPaletteOrder({ onChange }?: {
163
+ onChange?(): void;
164
+ }): PaletteOrder;
132
165
  export interface PaletteVisibilityStore {
133
166
  /**
134
167
  * The groups the palette shows for the target. `extensionIds` are the extension groups on offer: the extension
@@ -148,11 +181,15 @@ export interface PaletteVisibilityStore {
148
181
  apply(value: unknown, targets: readonly PaletteTarget[]): void;
149
182
  /** Forgets every setting, for a project without any. */
150
183
  reset(): void;
184
+ /** The order of the palette's categories, saved and loaded with the other settings. */
185
+ order: PaletteOrder;
151
186
  }
152
187
  /** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
153
- export declare function createPaletteVisibilityStore({ catalog }?: {
188
+ export declare function createPaletteVisibilityStore({ catalog, onOrderChange }?: {
154
189
  /** The extension groups on offer now, so the default counts the blocks of declared groups. */
155
190
  catalog?: () => PaletteGroupCatalog | undefined;
191
+ /** Called when the user reorders the palette's categories, so the project can be marked as changed. */
192
+ onOrderChange?(): void;
156
193
  }): PaletteVisibilityStore;
157
194
  /** The menu item that opens the group settings. */
158
195
  export declare const paletteVisibilityMenuLabel: (locale: string) => "ブロックグループの表示・非表示の設定" | "Show or hide block groups";
@@ -261,10 +261,86 @@ export function filterToolboxTree(tree, visible, catalog) {
261
261
  }
262
262
  /** The screen-state slice the settings are saved as. Sprites are named as the tab layout names them. */
263
263
  export const paletteScreenStateKey = 'blockPalette';
264
+ /** Keeps the order of the palette's categories. `onChange` is called when the user reorders them. */
265
+ export function createPaletteOrder({ onChange } = {}) {
266
+ let categories = [];
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
+ };
278
+ return {
279
+ categories: () => [...categories],
280
+ setCategories(ids) {
281
+ categories = [...ids];
282
+ changed();
283
+ },
284
+ members: groupId => groups.has(groupId) ? [...groups.get(groupId)] : undefined,
285
+ setMembers(groupId, ids) {
286
+ groups.set(groupId, [...ids]);
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);
302
+ },
303
+ capture() {
304
+ const saved = {};
305
+ if (categories.length)
306
+ saved.categories = [...categories];
307
+ if (groups.size)
308
+ saved.groups = Object.fromEntries([...groups].map(([id, ids]) => [id, [...ids]]));
309
+ if (pinned.size)
310
+ saved.pinned = [...pinned];
311
+ return saved.categories || saved.groups || saved.pinned ? saved : undefined;
312
+ },
313
+ apply(value) {
314
+ categories = [];
315
+ groups = new Map();
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
+ }
326
+ }
327
+ pinned = new Set(strings(saved.pinned) ?? []);
328
+ }
329
+ notify();
330
+ },
331
+ reset() {
332
+ categories = [];
333
+ groups = new Map();
334
+ pinned = new Set();
335
+ notify();
336
+ }
337
+ };
338
+ }
264
339
  const strings = (value) => Array.isArray(value) && value.every(item => typeof item === 'string') ? [...value] : undefined;
265
340
  const sameGroups = (left, right) => left.length === right.length && new Set(left).size === left.length && left.every(group => right.includes(group));
266
341
  /** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
267
- export function createPaletteVisibilityStore({ catalog } = {}) {
342
+ export function createPaletteVisibilityStore({ catalog, onOrderChange } = {}) {
343
+ const order = createPaletteOrder({ onChange: onOrderChange });
268
344
  const defaults = (target, extensionIds) => defaultVisibleGroups(target, extensionIds, catalog?.());
269
345
  let groups = new Map();
270
346
  let projectSets = {};
@@ -304,14 +380,19 @@ export function createPaletteVisibilityStore({ catalog } = {}) {
304
380
  saved.sprites = sprites;
305
381
  if (Object.keys(projectSets).length)
306
382
  saved.sets = structuredClone(projectSets);
307
- return saved.stage || saved.sprites || saved.sets ? saved : undefined;
383
+ const arranged = order.capture();
384
+ if (arranged)
385
+ saved.order = arranged;
386
+ return saved.stage || saved.sprites || saved.sets || saved.order ? saved : undefined;
308
387
  },
309
388
  apply(value, targets) {
310
389
  groups = new Map();
311
390
  projectSets = {};
391
+ order.reset();
312
392
  if (!value || typeof value !== 'object' || value.version !== 1)
313
393
  return;
314
394
  const saved = value;
395
+ order.apply(saved.order);
315
396
  const setting = (entry) => entry && typeof entry === 'object' ?
316
397
  strings(entry.groups) : undefined;
317
398
  const sprites = saved.sprites && typeof saved.sprites === 'object' ? saved.sprites : {};
@@ -333,7 +414,9 @@ export function createPaletteVisibilityStore({ catalog } = {}) {
333
414
  reset() {
334
415
  groups = new Map();
335
416
  projectSets = {};
336
- }
417
+ order.reset();
418
+ },
419
+ order
337
420
  };
338
421
  }
339
422
  /** The menu item that opens the group settings. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.15.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" aria-label={ja ? 'ブロックのカテゴリ' : 'Block categories'}
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
- return <button key={id} type="button" aria-label={label} aria-pressed={id === selectedId}
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[] = [];