@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.
@@ -130,10 +130,14 @@ export interface SavedPaletteVisibility {
130
130
  sets?: Record<string, SavedGroupSet>;
131
131
  order?: SavedPaletteOrder;
132
132
  }
133
- /** The order the palette's categories were dragged into: the top level, and the members of each group, by ID. */
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
+ */
134
137
  export interface SavedPaletteOrder {
135
138
  categories?: string[];
136
139
  groups?: Record<string, string[]>;
140
+ pinned?: string[];
137
141
  }
138
142
  /** The order the palette's categories were dragged into, for the whole project. */
139
143
  export interface PaletteOrder {
@@ -143,7 +147,12 @@ export interface PaletteOrder {
143
147
  /** A group's members, by ID, in order, or nothing when they were never reordered. */
144
148
  members(groupId: string): string[] | undefined;
145
149
  setMembers(groupId: string, ids: readonly string[]): void;
146
- /** The order to save, or nothing when nothing was reordered. */
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. */
147
156
  capture(): SavedPaletteOrder | undefined;
148
157
  /** Takes a saved order. A value that is not one leaves the palette in its own order. */
149
158
  apply(value: unknown): void;
@@ -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
- onChange?.();
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
- onChange?.();
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
- return saved.categories || saved.groups ? saved : undefined;
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
- if (!value || typeof value !== 'object')
291
- return;
292
- const saved = value;
293
- categories = strings(saved.categories) ?? [];
294
- if (saved.groups && typeof saved.groups === 'object' && !Array.isArray(saved.groups)) {
295
- for (const [id, ids] of Object.entries(saved.groups)) {
296
- const members = strings(ids);
297
- if (members)
298
- groups.set(id, members);
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.16.0",
3
+ "version": "0.18.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",
@@ -116,7 +116,8 @@
116
116
  "scripts": {
117
117
  "build": "tsc -p tsconfig.json",
118
118
  "prepare": "npm run build",
119
- "test": "npm run build && node --test test/*.test.ts"
119
+ "test": "npm run build && node --test test/*.test.ts",
120
+ "test:browser": "npm run build && node --test test/*.browser.ts"
120
121
  },
121
122
  "dependencies": {
122
123
  "@kubohiroya/turbowarp-script-transfer": "0.1.5"
@@ -129,7 +130,9 @@
129
130
  },
130
131
  "devDependencies": {
131
132
  "@kubohiroya/turbowarp-extension-security-dialog": "0.9.0",
133
+ "@kubohiroya/turbowarp-plus-editor-test-runtime": "0.2.0",
132
134
  "@kubohiroya/turbowarp-plus-editor-windows": "0.2.0",
135
+ "@playwright/test": "1.64.1",
133
136
  "@types/react": "18.3.31",
134
137
  "@types/react-dom": "18.3.7",
135
138
  "jsdom": "26.1.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[] = [];