@kubohiroya/turbowarp-plus-editor-code-views 0.13.0 → 0.15.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 +1 -1
- package/README.md +1 -1
- package/dist/palette-category-groups.d.ts +36 -0
- package/dist/palette-category-groups.js +95 -4
- package/dist/palette-category-reorder.d.ts +11 -1
- package/dist/palette-category-reorder.js +18 -5
- package/package.json +1 -1
- package/src/palette-category-groups.ts +114 -4
- package/src/palette-category-reorder.ts +25 -5
package/README.ja.md
CHANGED
|
@@ -18,7 +18,7 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
|
|
|
18
18
|
| `./target-combobox` | コードの窓の対象の選択 |
|
|
19
19
|
| `./detached-block-palette` | 別窓のブロックパレット |
|
|
20
20
|
| `./palette-category-reorder`・`./palette-category-search`・`./palette-dropdown-resize` | パレットのカテゴリの並べ替えと検索、パレットのドロップダウンの大きさの変更 |
|
|
21
|
-
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`attachPaletteCategoryGroups(menu, options)`
|
|
21
|
+
| `./palette-category-groups` | ツールボックスで別の category を含む category をグループとして表示する。アイコンをクリックすると、要素を並べた子ブロックパレットを影つきで重ねて開く(横配置のパレットではアイコンの右に横並び、縦配置では下に縦並び)。外側をクリックすると閉じる。Scratch Blocks は入れ子の category を捨てるので、`flattenCategoryGroups(tree)` と `filterGroupedToolboxTree(tree, groups, filter)` で木を整え、`prepareGroupedToolboxTree(tree, {catalog, visible})` は、機能拡張が宣言したグループの入れ子化と表示するグループの絞り込みを含めて、これを一度に行う。`hasCategoryGroups(tree)` は木が category を入れ子にしているかを返す。`attachPaletteCategoryGroups(menu, options)` で子ブロックパレットを開く。その `selectionTarget(id)` は、自分のブロックを持たないグループの代わりに選ぶ category を返し、`syncButtons(openId)` はグループのアイコンの状態を付け直す。`reorder: true` では、子ブロックパレットの要素を長押しして、同じグループの要素の中で並べ替えられる(別のグループや階層へは動かさない)。順番はパレットを描き直しても保ち、`arrange(categories, rank)` がツールボックスの category をそれに合わせて、グループの後にその要素が続くように並べる。`splitExtensionCategories(tree, catalog, {nested: true})` は、グループを宣言した機能拡張をこのグループにする |
|
|
22
22
|
| `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はエディターの画面状態として保存し、選ぶ画面を持つ |
|
|
23
23
|
| `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する。テキストの引数で Shift+Enter を押すと、改行を入れてダイアログで編集を続ける |
|
|
24
24
|
| `./block-number-input` | 数値の引数に入力した全角の数字・記号を半角にする |
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ The code views of TurboWarp Plus Editor: code in tabs and windows that share one
|
|
|
19
19
|
| `./detached-block-palette` | The block palette in its own window |
|
|
20
20
|
| `./palette-category-visibility`, `./palette-visibility-dialog` | Choose which block groups the palette shows for each sprite and the stage. The setting is saved with the editor's screen state, with a screen to choose |
|
|
21
21
|
| `./palette-category-reorder`, `./palette-category-search`, `./palette-dropdown-resize` | Reordering and searching palette categories, and resizing palette dropdowns |
|
|
22
|
-
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `attachPaletteCategoryGroups(menu, options)` opens the child palettes. `splitExtensionCategories(tree, catalog, {nested: true})` makes an extension that declares groups such a group |
|
|
22
|
+
| `./palette-category-groups` | Shows a category of the toolbox that holds other categories as a group: clicking its icon opens a child palette of its members, floating with a shadow — a row to the right of the icon in a horizontal palette, a column below it in a vertical one — and a click outside closes it. `flattenCategoryGroups(tree)` and `filterGroupedToolboxTree(tree, groups, filter)` prepare the tree, which Scratch Blocks would otherwise drop nested categories from; `prepareGroupedToolboxTree(tree, {catalog, visible})` does both in one step, also nesting the groups extensions declare and keeping only the groups to show, and `hasCategoryGroups(tree)` tells whether a tree nests categories. `attachPaletteCategoryGroups(menu, options)` opens the child palettes; its `selectionTarget(id)` gives the category to select for a group without blocks of its own, and `syncButtons(openId)` marks the groups' icons. With `reorder: true`, a long press on a member of a child palette drags it among the members of the same group (not to another group or level); the order is kept while the palette is drawn again, and `arrange(categories, rank)` orders the toolbox's categories to match, each group followed by its members. `splitExtensionCategories(tree, catalog, {nested: true})` makes an extension that declares groups such a group |
|
|
23
23
|
| `./block-argument-text` | Shortens long text arguments and edits them in a dialog. Shift+Enter in a text argument continues in the dialog with a line break |
|
|
24
24
|
| `./block-number-input` | Converts full-width digits and signs typed into number arguments to half-width |
|
|
25
25
|
| `./clean-up-comments` | Keeps comments with their scripts in "Clean up blocks": block comments keep their offset from the block, scripts with their comments do not cover one another, and workspace comments that cover a script move beside the scripts (`installCleanUpComments(ScratchBlocks)`) |
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PaletteGroupCatalog } from './palette-category-visibility.ts';
|
|
1
2
|
/**
|
|
2
3
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
4
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -26,6 +27,17 @@ export declare const flattenCategoryGroups: (tree: Element | null | undefined) =
|
|
|
26
27
|
* with neither members nor blocks of its own is removed with its members. Returns the groups that remain.
|
|
27
28
|
*/
|
|
28
29
|
export declare const filterGroupedToolboxTree: (tree: Element, groups: PaletteGroups, filter: (tree: Element) => void) => PaletteGroups;
|
|
30
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
31
|
+
export declare const hasCategoryGroups: (tree: Element | null | undefined) => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
34
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
35
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
36
|
+
*/
|
|
37
|
+
export declare const prepareGroupedToolboxTree: (tree: Element, { catalog, visible }?: {
|
|
38
|
+
catalog?: PaletteGroupCatalog;
|
|
39
|
+
visible?: ReadonlySet<string> | null;
|
|
40
|
+
}) => PaletteGroups;
|
|
29
41
|
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
30
42
|
export interface PaletteCategory {
|
|
31
43
|
id_: string;
|
|
@@ -43,6 +55,13 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
43
55
|
choose(category: PaletteCategory): void;
|
|
44
56
|
/** Called when a child palette opens or the last one closes. */
|
|
45
57
|
onChange?(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Lets a long press on a member of a child palette drag it to another place among the members of the same group,
|
|
60
|
+
* as the palette's own categories can be reordered. Members do not move to another group or level.
|
|
61
|
+
*/
|
|
62
|
+
reorder?: boolean;
|
|
63
|
+
/** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
|
|
64
|
+
onReorder?(): void;
|
|
46
65
|
}
|
|
47
66
|
export interface PaletteCategoryGroupsController {
|
|
48
67
|
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
@@ -55,6 +74,23 @@ export interface PaletteCategoryGroupsController {
|
|
|
55
74
|
isOpen(): boolean;
|
|
56
75
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
57
76
|
anchorOf(category: PaletteCategory): HTMLElement;
|
|
77
|
+
/**
|
|
78
|
+
* The category to select for an id: a group without blocks of its own stands for its first member, so selecting
|
|
79
|
+
* an extension that declares groups selects its first shown group.
|
|
80
|
+
*/
|
|
81
|
+
selectionTarget(id: string): string;
|
|
82
|
+
/**
|
|
83
|
+
* Orders the toolbox's categories: by the rank of the outermost group each is in (or its own, outside any group),
|
|
84
|
+
* each group followed by its members in their order, depth first. Keeps the given order otherwise.
|
|
85
|
+
*/
|
|
86
|
+
arrange<T extends {
|
|
87
|
+
id_: string;
|
|
88
|
+
}>(categories: readonly T[], rank: (topLevelId: string) => number): T[];
|
|
89
|
+
/**
|
|
90
|
+
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
91
|
+
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
92
|
+
*/
|
|
93
|
+
syncButtons(openId: string | null): void;
|
|
58
94
|
/** Whether the node is in a child palette. */
|
|
59
95
|
contains(node: Node | null): boolean;
|
|
60
96
|
close(): void;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { filterToolboxTree, splitExtensionCategories } from "./palette-category-visibility.js";
|
|
2
|
+
import { attachPaletteCategoryReorder } from "./palette-category-reorder.js";
|
|
1
3
|
/**
|
|
2
4
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
5
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -107,6 +109,19 @@ export const filterGroupedToolboxTree = (tree, groups, filter) => {
|
|
|
107
109
|
}
|
|
108
110
|
return remaining;
|
|
109
111
|
};
|
|
112
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
113
|
+
export const hasCategoryGroups = (tree) => !!tree?.querySelector?.('category category');
|
|
114
|
+
/**
|
|
115
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
116
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
117
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
118
|
+
*/
|
|
119
|
+
export const prepareGroupedToolboxTree = (tree, { catalog, visible } = {}) => {
|
|
120
|
+
if (catalog)
|
|
121
|
+
splitExtensionCategories(tree, catalog, { nested: true });
|
|
122
|
+
const groups = flattenCategoryGroups(tree);
|
|
123
|
+
return visible ? filterGroupedToolboxTree(tree, groups, filtered => filterToolboxTree(filtered, visible)) : groups;
|
|
124
|
+
};
|
|
110
125
|
const selectedClass = 'categorySelected';
|
|
111
126
|
/** Makes clicking a group's icon in the palette menu open its child palette. */
|
|
112
127
|
export const attachPaletteCategoryGroups = (menu, options) => {
|
|
@@ -117,6 +132,20 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
117
132
|
const parentOf = new Map();
|
|
118
133
|
// The open child palettes, outermost first, with the icon each opened from.
|
|
119
134
|
const stack = [];
|
|
135
|
+
// The order members were dragged into, by group, kept while the toolbox is drawn again.
|
|
136
|
+
const memberOrder = new Map();
|
|
137
|
+
const applyMemberOrder = () => {
|
|
138
|
+
for (const [id, members] of groups.members) {
|
|
139
|
+
const saved = memberOrder.get(id);
|
|
140
|
+
if (!saved)
|
|
141
|
+
continue;
|
|
142
|
+
const rank = (member) => {
|
|
143
|
+
const index = saved.indexOf(member);
|
|
144
|
+
return index < 0 ? saved.length : index;
|
|
145
|
+
};
|
|
146
|
+
groups.members.set(id, members.slice().sort((a, b) => rank(a) - rank(b)));
|
|
147
|
+
}
|
|
148
|
+
};
|
|
120
149
|
let selected = null;
|
|
121
150
|
const categoryById = (id) => options.categories().find(category => category.id_ === id);
|
|
122
151
|
const topLevelOf = (id) => {
|
|
@@ -143,7 +172,8 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
143
172
|
const closeFrom = (depth) => {
|
|
144
173
|
let changed = false;
|
|
145
174
|
while (stack.length > depth) {
|
|
146
|
-
const { anchor, popup } = stack.pop();
|
|
175
|
+
const { anchor, popup, reorder } = stack.pop();
|
|
176
|
+
reorder?.dispose();
|
|
147
177
|
anchor.setAttribute('aria-expanded', 'false');
|
|
148
178
|
popup.remove();
|
|
149
179
|
changed = true;
|
|
@@ -245,14 +275,25 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
245
275
|
popup.style.color = look.color;
|
|
246
276
|
popup.style.fontSize = look.fontSize;
|
|
247
277
|
popup.style.fontFamily = look.fontFamily;
|
|
278
|
+
if (options.rtl)
|
|
279
|
+
popup.dir = 'rtl';
|
|
248
280
|
if (groups.withOwnContents.has(group.id_))
|
|
249
281
|
popup.append(createEntry(group, depth, true));
|
|
250
|
-
|
|
251
|
-
|
|
282
|
+
const entries = members.map(member => createEntry(member, depth + 1));
|
|
283
|
+
popup.append(...entries);
|
|
252
284
|
(menu.parentElement ?? doc.body).append(popup);
|
|
253
285
|
place(popup, anchor);
|
|
254
286
|
anchor.setAttribute('aria-expanded', 'true');
|
|
255
|
-
|
|
287
|
+
// The group's own blocks stay first; its members can be dragged among themselves.
|
|
288
|
+
const reorder = options.reorder ? attachPaletteCategoryReorder(popup, () => entries, (from, to) => {
|
|
289
|
+
const order = members.map(member => member.id_);
|
|
290
|
+
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
291
|
+
memberOrder.set(group.id_, order);
|
|
292
|
+
groups.members.set(group.id_, order);
|
|
293
|
+
open(group, anchor, depth, false);
|
|
294
|
+
options.onReorder?.();
|
|
295
|
+
}, { horizontal: options.horizontal, area: entry => entry }) : undefined;
|
|
296
|
+
stack.push({ id: group.id_, anchor, popup, reorder });
|
|
256
297
|
options.onChange?.();
|
|
257
298
|
if (focus)
|
|
258
299
|
popup.firstElementChild?.focus();
|
|
@@ -310,6 +351,7 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
310
351
|
update(next) {
|
|
311
352
|
close();
|
|
312
353
|
groups = next;
|
|
354
|
+
applyMemberOrder();
|
|
313
355
|
parentOf.clear();
|
|
314
356
|
for (const [id, members] of groups.members)
|
|
315
357
|
for (const member of members)
|
|
@@ -345,6 +387,55 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
345
387
|
toggle,
|
|
346
388
|
isOpen: () => stack.length > 0,
|
|
347
389
|
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
390
|
+
arrange(categories, rank) {
|
|
391
|
+
const position = new Map();
|
|
392
|
+
const visit = (id) => {
|
|
393
|
+
position.set(id, position.size);
|
|
394
|
+
for (const member of groups.members.get(id) ?? [])
|
|
395
|
+
visit(member);
|
|
396
|
+
};
|
|
397
|
+
for (const [id] of groups.members)
|
|
398
|
+
if (!parentOf.has(id))
|
|
399
|
+
visit(id);
|
|
400
|
+
// Categories of the same rank keep the place of their outermost group, or their own.
|
|
401
|
+
const place = new Map();
|
|
402
|
+
categories.forEach((category, index) => {
|
|
403
|
+
const top = topLevelOf(category.id_);
|
|
404
|
+
if (!place.has(top))
|
|
405
|
+
place.set(top, index);
|
|
406
|
+
});
|
|
407
|
+
const key = (category) => {
|
|
408
|
+
const top = topLevelOf(category.id_);
|
|
409
|
+
return [rank(top), place.get(top), position.get(category.id_) ?? 0];
|
|
410
|
+
};
|
|
411
|
+
return categories.slice().sort((a, b) => {
|
|
412
|
+
const ka = key(a);
|
|
413
|
+
const kb = key(b);
|
|
414
|
+
return ka[0] - kb[0] || ka[1] - kb[1] || ka[2] - kb[2];
|
|
415
|
+
});
|
|
416
|
+
},
|
|
417
|
+
selectionTarget(id) {
|
|
418
|
+
let target = id;
|
|
419
|
+
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
420
|
+
target = groups.members.get(target)[0];
|
|
421
|
+
}
|
|
422
|
+
return target;
|
|
423
|
+
},
|
|
424
|
+
syncButtons(openId) {
|
|
425
|
+
const horizontal = options.horizontal();
|
|
426
|
+
for (const category of options.categories()) {
|
|
427
|
+
if (!groups.members.has(category.id_) || parentOf.has(category.id_))
|
|
428
|
+
continue;
|
|
429
|
+
const item = category.item_;
|
|
430
|
+
if (horizontal)
|
|
431
|
+
item.tabIndex = 0;
|
|
432
|
+
else
|
|
433
|
+
item.removeAttribute('tabindex');
|
|
434
|
+
item.setAttribute('role', 'button');
|
|
435
|
+
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
436
|
+
topLevelOf(openId) === category.id_);
|
|
437
|
+
}
|
|
438
|
+
},
|
|
348
439
|
contains,
|
|
349
440
|
close,
|
|
350
441
|
dispose() {
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface PaletteCategoryReorderOptions {
|
|
2
|
+
/** Whether the items run in a row. By default, whether the menu is a horizontal block palette. */
|
|
3
|
+
horizontal?(): boolean;
|
|
4
|
+
/** The element an item takes up, which a drop over it lands on. By default, the row that holds the item. */
|
|
5
|
+
area?(item: HTMLElement): Element;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Lets a long press on an item of the menu drag it to another place among the items. Used for the palette's
|
|
9
|
+
* categories and for the members of a group's child palette.
|
|
10
|
+
*/
|
|
11
|
+
export declare function attachPaletteCategoryReorder(menu: HTMLElement, getItems: () => HTMLElement[], onDrop: (from: number, to: number) => void, options?: PaletteCategoryReorderOptions): {
|
|
2
12
|
cancel: () => void;
|
|
3
13
|
dispose(): void;
|
|
4
14
|
};
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const holdDelay = 500;
|
|
2
2
|
const movementThreshold = 8;
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Lets a long press on an item of the menu drag it to another place among the items. Used for the palette's
|
|
5
|
+
* categories and for the members of a group's child palette.
|
|
6
|
+
*/
|
|
7
|
+
export function attachPaletteCategoryReorder(menu, getItems, onDrop, options = {}) {
|
|
8
|
+
const horizontal = options.horizontal ?? (() => menu.hasAttribute('data-horizontal-block-palette'));
|
|
9
|
+
const area = options.area ?? ((item) => item.parentElement);
|
|
4
10
|
const doc = menu.ownerDocument;
|
|
5
11
|
const win = doc.defaultView;
|
|
6
12
|
const events = new AbortController();
|
|
@@ -33,14 +39,14 @@ export function attachPaletteCategoryReorder(menu, getItems, onDrop) {
|
|
|
33
39
|
const hit = doc.elementFromPoint(event.clientX, event.clientY);
|
|
34
40
|
if (!items.length || !menu.contains(hit))
|
|
35
41
|
return;
|
|
36
|
-
const item = items.find(candidate => candidate.
|
|
42
|
+
const item = items.find(candidate => area(candidate).contains(hit));
|
|
37
43
|
if (item)
|
|
38
44
|
return item;
|
|
39
45
|
const first = items[0];
|
|
40
46
|
const last = items[items.length - 1];
|
|
41
47
|
const start = first.getBoundingClientRect();
|
|
42
48
|
const end = last.getBoundingClientRect();
|
|
43
|
-
if (!
|
|
49
|
+
if (!horizontal()) {
|
|
44
50
|
if (event.clientY < start.top)
|
|
45
51
|
return first;
|
|
46
52
|
if (event.clientY > end.bottom)
|
|
@@ -131,14 +137,20 @@ export function attachPaletteCategoryReorder(menu, getItems, onDrop) {
|
|
|
131
137
|
if (event.target === menu && press?.pointerId === event.pointerId)
|
|
132
138
|
cancel();
|
|
133
139
|
}, capture);
|
|
134
|
-
|
|
140
|
+
// Only the window losing focus: an element's blur also reaches the window while capturing, as when the press
|
|
141
|
+
// moves the focus from one category to another.
|
|
142
|
+
win.addEventListener('blur', event => {
|
|
143
|
+
if (event.target === win)
|
|
144
|
+
cancel();
|
|
145
|
+
}, capture);
|
|
135
146
|
doc.addEventListener('keydown', event => {
|
|
136
147
|
if (!preview || event.key !== 'Escape')
|
|
137
148
|
return;
|
|
138
149
|
event.stopImmediatePropagation();
|
|
139
150
|
cancel();
|
|
140
151
|
}, capture);
|
|
141
|
-
// ScratchBlocks selects categories on mouseup or touchend
|
|
152
|
+
// ScratchBlocks selects categories on mouseup or touchend, and a group or a child palette's item opens on click.
|
|
153
|
+
// Ignore the release of a reorder.
|
|
142
154
|
const swallowRelease = (event) => {
|
|
143
155
|
if (!suppressRelease)
|
|
144
156
|
return;
|
|
@@ -147,6 +159,7 @@ export function attachPaletteCategoryReorder(menu, getItems, onDrop) {
|
|
|
147
159
|
};
|
|
148
160
|
menu.addEventListener('mouseup', swallowRelease, capture);
|
|
149
161
|
menu.addEventListener('touchend', swallowRelease, { ...capture, passive: false });
|
|
162
|
+
menu.addEventListener('click', swallowRelease, capture);
|
|
150
163
|
menu.addEventListener('touchmove', event => {
|
|
151
164
|
if (preview)
|
|
152
165
|
event.preventDefault();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-code-views",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.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,3 +1,7 @@
|
|
|
1
|
+
import {filterToolboxTree, splitExtensionCategories} from './palette-category-visibility.ts';
|
|
2
|
+
import type {PaletteGroupCatalog} from './palette-category-visibility.ts';
|
|
3
|
+
import {attachPaletteCategoryReorder} from './palette-category-reorder.ts';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
3
7
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -108,6 +112,21 @@ export const filterGroupedToolboxTree = (tree: Element, groups: PaletteGroups, f
|
|
|
108
112
|
return remaining;
|
|
109
113
|
};
|
|
110
114
|
|
|
115
|
+
/** Whether the toolbox tree nests categories, which Scratch Blocks drops unless the tree is prepared first. */
|
|
116
|
+
export const hasCategoryGroups = (tree: Element | null | undefined): boolean => !!tree?.querySelector?.('category category');
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Prepares a toolbox tree for the palette before Scratch Blocks draws it: with a catalog, each extension that declares
|
|
120
|
+
* groups becomes a group of them; nested categories are flattened; with the groups to show, the others are removed.
|
|
121
|
+
* Pass the groups returned to the controller's `update` once the palette is drawn.
|
|
122
|
+
*/
|
|
123
|
+
export const prepareGroupedToolboxTree = (tree: Element,
|
|
124
|
+
{catalog, visible}: {catalog?: PaletteGroupCatalog; visible?: ReadonlySet<string> | null} = {}): PaletteGroups => {
|
|
125
|
+
if (catalog) splitExtensionCategories(tree, catalog, {nested: true});
|
|
126
|
+
const groups = flattenCategoryGroups(tree);
|
|
127
|
+
return visible ? filterGroupedToolboxTree(tree, groups, filtered => filterToolboxTree(filtered, visible)) : groups;
|
|
128
|
+
};
|
|
129
|
+
|
|
111
130
|
/** The part of a Scratch Blocks toolbox category the child palettes use. */
|
|
112
131
|
export interface PaletteCategory {
|
|
113
132
|
id_: string;
|
|
@@ -126,6 +145,13 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
126
145
|
choose(category: PaletteCategory): void;
|
|
127
146
|
/** Called when a child palette opens or the last one closes. */
|
|
128
147
|
onChange?(): void;
|
|
148
|
+
/**
|
|
149
|
+
* Lets a long press on a member of a child palette drag it to another place among the members of the same group,
|
|
150
|
+
* as the palette's own categories can be reordered. Members do not move to another group or level.
|
|
151
|
+
*/
|
|
152
|
+
reorder?: boolean;
|
|
153
|
+
/** Called after the members of a group were reordered, so the host can lay out its categories with `arrange`. */
|
|
154
|
+
onReorder?(): void;
|
|
129
155
|
}
|
|
130
156
|
|
|
131
157
|
export interface PaletteCategoryGroupsController {
|
|
@@ -139,6 +165,21 @@ export interface PaletteCategoryGroupsController {
|
|
|
139
165
|
isOpen(): boolean;
|
|
140
166
|
/** The icon the palette shows for the category: its own, or that of the group it is in. */
|
|
141
167
|
anchorOf(category: PaletteCategory): HTMLElement;
|
|
168
|
+
/**
|
|
169
|
+
* The category to select for an id: a group without blocks of its own stands for its first member, so selecting
|
|
170
|
+
* an extension that declares groups selects its first shown group.
|
|
171
|
+
*/
|
|
172
|
+
selectionTarget(id: string): string;
|
|
173
|
+
/**
|
|
174
|
+
* Orders the toolbox's categories: by the rank of the outermost group each is in (or its own, outside any group),
|
|
175
|
+
* each group followed by its members in their order, depth first. Keeps the given order otherwise.
|
|
176
|
+
*/
|
|
177
|
+
arrange<T extends {id_: string}>(categories: readonly T[], rank: (topLevelId: string) => number): T[];
|
|
178
|
+
/**
|
|
179
|
+
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
180
|
+
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
181
|
+
*/
|
|
182
|
+
syncButtons(openId: string | null): void;
|
|
142
183
|
/** Whether the node is in a child palette. */
|
|
143
184
|
contains(node: Node | null): boolean;
|
|
144
185
|
close(): void;
|
|
@@ -156,7 +197,20 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
156
197
|
let groups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
157
198
|
const parentOf = new Map<string, string>();
|
|
158
199
|
// The open child palettes, outermost first, with the icon each opened from.
|
|
159
|
-
const stack: {id: string; anchor: HTMLElement; popup: HTMLElement}[] = [];
|
|
200
|
+
const stack: {id: string; anchor: HTMLElement; popup: HTMLElement; reorder?: {dispose(): void}}[] = [];
|
|
201
|
+
// The order members were dragged into, by group, kept while the toolbox is drawn again.
|
|
202
|
+
const memberOrder = new Map<string, string[]>();
|
|
203
|
+
const applyMemberOrder = () => {
|
|
204
|
+
for (const [id, members] of groups.members) {
|
|
205
|
+
const saved = memberOrder.get(id);
|
|
206
|
+
if (!saved) continue;
|
|
207
|
+
const rank = (member: string) => {
|
|
208
|
+
const index = saved.indexOf(member);
|
|
209
|
+
return index < 0 ? saved.length : index;
|
|
210
|
+
};
|
|
211
|
+
groups.members.set(id, members.slice().sort((a, b) => rank(a) - rank(b)));
|
|
212
|
+
}
|
|
213
|
+
};
|
|
160
214
|
let selected: string | null = null;
|
|
161
215
|
|
|
162
216
|
const categoryById = (id: string) => options.categories().find(category => category.id_ === id);
|
|
@@ -181,7 +235,8 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
181
235
|
const closeFrom = (depth: number) => {
|
|
182
236
|
let changed = false;
|
|
183
237
|
while (stack.length > depth) {
|
|
184
|
-
const {anchor, popup} = stack.pop()!;
|
|
238
|
+
const {anchor, popup, reorder} = stack.pop()!;
|
|
239
|
+
reorder?.dispose();
|
|
185
240
|
anchor.setAttribute('aria-expanded', 'false');
|
|
186
241
|
popup.remove();
|
|
187
242
|
changed = true;
|
|
@@ -281,12 +336,24 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
281
336
|
popup.style.color = look.color;
|
|
282
337
|
popup.style.fontSize = look.fontSize;
|
|
283
338
|
popup.style.fontFamily = look.fontFamily;
|
|
339
|
+
if (options.rtl) popup.dir = 'rtl';
|
|
284
340
|
if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
|
|
285
|
-
|
|
341
|
+
const entries = members.map(member => createEntry(member, depth + 1));
|
|
342
|
+
popup.append(...entries);
|
|
286
343
|
(menu.parentElement ?? doc.body).append(popup);
|
|
287
344
|
place(popup, anchor);
|
|
288
345
|
anchor.setAttribute('aria-expanded', 'true');
|
|
289
|
-
|
|
346
|
+
// The group's own blocks stay first; its members can be dragged among themselves.
|
|
347
|
+
const reorder = options.reorder ? attachPaletteCategoryReorder(popup, () => entries,
|
|
348
|
+
(from, to) => {
|
|
349
|
+
const order = members.map(member => member.id_);
|
|
350
|
+
order.splice(to, 0, order.splice(from, 1)[0]);
|
|
351
|
+
memberOrder.set(group.id_, order);
|
|
352
|
+
groups.members.set(group.id_, order);
|
|
353
|
+
open(group, anchor, depth, false);
|
|
354
|
+
options.onReorder?.();
|
|
355
|
+
}, {horizontal: options.horizontal, area: entry => entry}) : undefined;
|
|
356
|
+
stack.push({id: group.id_, anchor, popup, reorder});
|
|
290
357
|
options.onChange?.();
|
|
291
358
|
if (focus) (popup.firstElementChild as HTMLElement | null)?.focus();
|
|
292
359
|
};
|
|
@@ -339,6 +406,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
339
406
|
update(next) {
|
|
340
407
|
close();
|
|
341
408
|
groups = next;
|
|
409
|
+
applyMemberOrder();
|
|
342
410
|
parentOf.clear();
|
|
343
411
|
for (const [id, members] of groups.members) for (const member of members) parentOf.set(member, id);
|
|
344
412
|
for (const category of options.categories()) {
|
|
@@ -368,6 +436,48 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
368
436
|
toggle,
|
|
369
437
|
isOpen: () => stack.length > 0,
|
|
370
438
|
anchorOf: category => categoryById(topLevelOf(category.id_))?.item_ ?? category.item_,
|
|
439
|
+
arrange(categories, rank) {
|
|
440
|
+
const position = new Map<string, number>();
|
|
441
|
+
const visit = (id: string) => {
|
|
442
|
+
position.set(id, position.size);
|
|
443
|
+
for (const member of groups.members.get(id) ?? []) visit(member);
|
|
444
|
+
};
|
|
445
|
+
for (const [id] of groups.members) if (!parentOf.has(id)) visit(id);
|
|
446
|
+
// Categories of the same rank keep the place of their outermost group, or their own.
|
|
447
|
+
const place = new Map<string, number>();
|
|
448
|
+
categories.forEach((category, index) => {
|
|
449
|
+
const top = topLevelOf(category.id_);
|
|
450
|
+
if (!place.has(top)) place.set(top, index);
|
|
451
|
+
});
|
|
452
|
+
const key = (category: {id_: string}) => {
|
|
453
|
+
const top = topLevelOf(category.id_);
|
|
454
|
+
return [rank(top), place.get(top)!, position.get(category.id_) ?? 0];
|
|
455
|
+
};
|
|
456
|
+
return categories.slice().sort((a, b) => {
|
|
457
|
+
const ka = key(a);
|
|
458
|
+
const kb = key(b);
|
|
459
|
+
return ka[0] - kb[0] || ka[1] - kb[1] || ka[2] - kb[2];
|
|
460
|
+
});
|
|
461
|
+
},
|
|
462
|
+
selectionTarget(id) {
|
|
463
|
+
let target = id;
|
|
464
|
+
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
465
|
+
target = groups.members.get(target)![0];
|
|
466
|
+
}
|
|
467
|
+
return target;
|
|
468
|
+
},
|
|
469
|
+
syncButtons(openId) {
|
|
470
|
+
const horizontal = options.horizontal();
|
|
471
|
+
for (const category of options.categories()) {
|
|
472
|
+
if (!groups.members.has(category.id_) || parentOf.has(category.id_)) continue;
|
|
473
|
+
const item = category.item_;
|
|
474
|
+
if (horizontal) item.tabIndex = 0;
|
|
475
|
+
else item.removeAttribute('tabindex');
|
|
476
|
+
item.setAttribute('role', 'button');
|
|
477
|
+
item.toggleAttribute('data-palette-group-shown', horizontal && openId !== null && openId !== category.id_ &&
|
|
478
|
+
topLevelOf(openId) === category.id_);
|
|
479
|
+
}
|
|
480
|
+
},
|
|
371
481
|
contains,
|
|
372
482
|
close,
|
|
373
483
|
dispose() {
|
|
@@ -4,9 +4,23 @@ const movementThreshold = 8;
|
|
|
4
4
|
|
|
5
5
|
type Press = {pointerId: number; source: HTMLElement; x: number; y: number; timer: number};
|
|
6
6
|
|
|
7
|
+
export interface PaletteCategoryReorderOptions {
|
|
8
|
+
/** Whether the items run in a row. By default, whether the menu is a horizontal block palette. */
|
|
9
|
+
horizontal?(): boolean;
|
|
10
|
+
/** The element an item takes up, which a drop over it lands on. By default, the row that holds the item. */
|
|
11
|
+
area?(item: HTMLElement): Element;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Lets a long press on an item of the menu drag it to another place among the items. Used for the palette's
|
|
16
|
+
* categories and for the members of a group's child palette.
|
|
17
|
+
*/
|
|
7
18
|
export function attachPaletteCategoryReorder(
|
|
8
|
-
menu: HTMLElement, getItems: () => HTMLElement[], onDrop: (from: number, to: number) => void
|
|
19
|
+
menu: HTMLElement, getItems: () => HTMLElement[], onDrop: (from: number, to: number) => void,
|
|
20
|
+
options: PaletteCategoryReorderOptions = {}
|
|
9
21
|
) {
|
|
22
|
+
const horizontal = options.horizontal ?? (() => menu.hasAttribute('data-horizontal-block-palette'));
|
|
23
|
+
const area = options.area ?? ((item: HTMLElement) => item.parentElement);
|
|
10
24
|
const doc = menu.ownerDocument;
|
|
11
25
|
const win = doc.defaultView;
|
|
12
26
|
const events = new AbortController();
|
|
@@ -39,14 +53,14 @@ export function attachPaletteCategoryReorder(
|
|
|
39
53
|
const itemAt = (event: PointerEvent, items: HTMLElement[]) => {
|
|
40
54
|
const hit = doc.elementFromPoint(event.clientX, event.clientY);
|
|
41
55
|
if (!items.length || !menu.contains(hit)) return;
|
|
42
|
-
const item = items.find(candidate => candidate.
|
|
56
|
+
const item = items.find(candidate => area(candidate).contains(hit));
|
|
43
57
|
if (item) return item;
|
|
44
58
|
|
|
45
59
|
const first = items[0];
|
|
46
60
|
const last = items[items.length - 1];
|
|
47
61
|
const start = first.getBoundingClientRect();
|
|
48
62
|
const end = last.getBoundingClientRect();
|
|
49
|
-
if (!
|
|
63
|
+
if (!horizontal()) {
|
|
50
64
|
if (event.clientY < start.top) return first;
|
|
51
65
|
if (event.clientY > end.bottom) return last;
|
|
52
66
|
} else if (menu.dir === 'rtl') {
|
|
@@ -121,14 +135,19 @@ export function attachPaletteCategoryReorder(
|
|
|
121
135
|
menu.addEventListener('lostpointercapture', event => {
|
|
122
136
|
if (event.target === menu && press?.pointerId === event.pointerId) cancel();
|
|
123
137
|
}, capture);
|
|
124
|
-
|
|
138
|
+
// Only the window losing focus: an element's blur also reaches the window while capturing, as when the press
|
|
139
|
+
// moves the focus from one category to another.
|
|
140
|
+
win.addEventListener('blur', event => {
|
|
141
|
+
if (event.target === win) cancel();
|
|
142
|
+
}, capture);
|
|
125
143
|
doc.addEventListener('keydown', event => {
|
|
126
144
|
if (!preview || event.key !== 'Escape') return;
|
|
127
145
|
event.stopImmediatePropagation();
|
|
128
146
|
cancel();
|
|
129
147
|
}, capture);
|
|
130
148
|
|
|
131
|
-
// ScratchBlocks selects categories on mouseup or touchend
|
|
149
|
+
// ScratchBlocks selects categories on mouseup or touchend, and a group or a child palette's item opens on click.
|
|
150
|
+
// Ignore the release of a reorder.
|
|
132
151
|
const swallowRelease = (event: Event) => {
|
|
133
152
|
if (!suppressRelease) return;
|
|
134
153
|
event.preventDefault();
|
|
@@ -136,6 +155,7 @@ export function attachPaletteCategoryReorder(
|
|
|
136
155
|
};
|
|
137
156
|
menu.addEventListener('mouseup', swallowRelease, capture);
|
|
138
157
|
menu.addEventListener('touchend', swallowRelease, {...capture, passive: false});
|
|
158
|
+
menu.addEventListener('click', swallowRelease, capture);
|
|
139
159
|
menu.addEventListener('touchmove', event => {
|
|
140
160
|
if (preview) event.preventDefault();
|
|
141
161
|
}, {...capture, passive: false});
|