@kubohiroya/turbowarp-plus-editor-code-views 0.14.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 +14 -0
- package/dist/palette-category-groups.js +59 -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 +67 -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)` で木を整え、`prepareGroupedToolboxTree(tree, {catalog, visible})` は、機能拡張が宣言したグループの入れ子化と表示するグループの絞り込みを含めて、これを一度に行う。`hasCategoryGroups(tree)` は木が category を入れ子にしているかを返す。`attachPaletteCategoryGroups(menu, options)` で子ブロックパレットを開く。その `selectionTarget(id)` は、自分のブロックを持たないグループの代わりに選ぶ category を返し、`syncButtons(openId)` はグループのアイコンの状態を付け直す。`splitExtensionCategories(tree, catalog, {nested: true})` は、グループを宣言した機能拡張をこのグループにする |
|
|
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; `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. `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)`) |
|
|
@@ -55,6 +55,13 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
55
55
|
choose(category: PaletteCategory): void;
|
|
56
56
|
/** Called when a child palette opens or the last one closes. */
|
|
57
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;
|
|
58
65
|
}
|
|
59
66
|
export interface PaletteCategoryGroupsController {
|
|
60
67
|
/** Takes the groups of the toolbox just drawn: hides their members' icons and marks the groups' icons. */
|
|
@@ -72,6 +79,13 @@ export interface PaletteCategoryGroupsController {
|
|
|
72
79
|
* an extension that declares groups selects its first shown group.
|
|
73
80
|
*/
|
|
74
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[];
|
|
75
89
|
/**
|
|
76
90
|
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
77
91
|
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { filterToolboxTree, splitExtensionCategories } from "./palette-category-visibility.js";
|
|
2
|
+
import { attachPaletteCategoryReorder } from "./palette-category-reorder.js";
|
|
2
3
|
/**
|
|
3
4
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
4
5
|
* reads only the top-level categories and drops nested ones, so the tree is flattened before it is drawn:
|
|
@@ -131,6 +132,20 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
131
132
|
const parentOf = new Map();
|
|
132
133
|
// The open child palettes, outermost first, with the icon each opened from.
|
|
133
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
|
+
};
|
|
134
149
|
let selected = null;
|
|
135
150
|
const categoryById = (id) => options.categories().find(category => category.id_ === id);
|
|
136
151
|
const topLevelOf = (id) => {
|
|
@@ -157,7 +172,8 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
157
172
|
const closeFrom = (depth) => {
|
|
158
173
|
let changed = false;
|
|
159
174
|
while (stack.length > depth) {
|
|
160
|
-
const { anchor, popup } = stack.pop();
|
|
175
|
+
const { anchor, popup, reorder } = stack.pop();
|
|
176
|
+
reorder?.dispose();
|
|
161
177
|
anchor.setAttribute('aria-expanded', 'false');
|
|
162
178
|
popup.remove();
|
|
163
179
|
changed = true;
|
|
@@ -259,14 +275,25 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
259
275
|
popup.style.color = look.color;
|
|
260
276
|
popup.style.fontSize = look.fontSize;
|
|
261
277
|
popup.style.fontFamily = look.fontFamily;
|
|
278
|
+
if (options.rtl)
|
|
279
|
+
popup.dir = 'rtl';
|
|
262
280
|
if (groups.withOwnContents.has(group.id_))
|
|
263
281
|
popup.append(createEntry(group, depth, true));
|
|
264
|
-
|
|
265
|
-
|
|
282
|
+
const entries = members.map(member => createEntry(member, depth + 1));
|
|
283
|
+
popup.append(...entries);
|
|
266
284
|
(menu.parentElement ?? doc.body).append(popup);
|
|
267
285
|
place(popup, anchor);
|
|
268
286
|
anchor.setAttribute('aria-expanded', 'true');
|
|
269
|
-
|
|
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 });
|
|
270
297
|
options.onChange?.();
|
|
271
298
|
if (focus)
|
|
272
299
|
popup.firstElementChild?.focus();
|
|
@@ -324,6 +351,7 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
324
351
|
update(next) {
|
|
325
352
|
close();
|
|
326
353
|
groups = next;
|
|
354
|
+
applyMemberOrder();
|
|
327
355
|
parentOf.clear();
|
|
328
356
|
for (const [id, members] of groups.members)
|
|
329
357
|
for (const member of members)
|
|
@@ -359,6 +387,33 @@ export const attachPaletteCategoryGroups = (menu, options) => {
|
|
|
359
387
|
toggle,
|
|
360
388
|
isOpen: () => stack.length > 0,
|
|
361
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
|
+
},
|
|
362
417
|
selectionTarget(id) {
|
|
363
418
|
let target = id;
|
|
364
419
|
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
@@ -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,5 +1,6 @@
|
|
|
1
1
|
import {filterToolboxTree, splitExtensionCategories} from './palette-category-visibility.ts';
|
|
2
2
|
import type {PaletteGroupCatalog} from './palette-category-visibility.ts';
|
|
3
|
+
import {attachPaletteCategoryReorder} from './palette-category-reorder.ts';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Block palette groups: a category of the toolbox XML that holds other categories is a group. Scratch Blocks
|
|
@@ -144,6 +145,13 @@ export interface PaletteCategoryGroupsOptions {
|
|
|
144
145
|
choose(category: PaletteCategory): void;
|
|
145
146
|
/** Called when a child palette opens or the last one closes. */
|
|
146
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;
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
export interface PaletteCategoryGroupsController {
|
|
@@ -162,6 +170,11 @@ export interface PaletteCategoryGroupsController {
|
|
|
162
170
|
* an extension that declares groups selects its first shown group.
|
|
163
171
|
*/
|
|
164
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[];
|
|
165
178
|
/**
|
|
166
179
|
* Marks the groups' icons as buttons, focusable in a horizontal palette, and marks the group whose member's blocks
|
|
167
180
|
* are open (`data-palette-group-shown`). Call it whenever the open category changes.
|
|
@@ -184,7 +197,20 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
184
197
|
let groups: PaletteGroups = {members: new Map(), withOwnContents: new Set()};
|
|
185
198
|
const parentOf = new Map<string, string>();
|
|
186
199
|
// The open child palettes, outermost first, with the icon each opened from.
|
|
187
|
-
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
|
+
};
|
|
188
214
|
let selected: string | null = null;
|
|
189
215
|
|
|
190
216
|
const categoryById = (id: string) => options.categories().find(category => category.id_ === id);
|
|
@@ -209,7 +235,8 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
209
235
|
const closeFrom = (depth: number) => {
|
|
210
236
|
let changed = false;
|
|
211
237
|
while (stack.length > depth) {
|
|
212
|
-
const {anchor, popup} = stack.pop()!;
|
|
238
|
+
const {anchor, popup, reorder} = stack.pop()!;
|
|
239
|
+
reorder?.dispose();
|
|
213
240
|
anchor.setAttribute('aria-expanded', 'false');
|
|
214
241
|
popup.remove();
|
|
215
242
|
changed = true;
|
|
@@ -309,12 +336,24 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
309
336
|
popup.style.color = look.color;
|
|
310
337
|
popup.style.fontSize = look.fontSize;
|
|
311
338
|
popup.style.fontFamily = look.fontFamily;
|
|
339
|
+
if (options.rtl) popup.dir = 'rtl';
|
|
312
340
|
if (groups.withOwnContents.has(group.id_)) popup.append(createEntry(group, depth, true));
|
|
313
|
-
|
|
341
|
+
const entries = members.map(member => createEntry(member, depth + 1));
|
|
342
|
+
popup.append(...entries);
|
|
314
343
|
(menu.parentElement ?? doc.body).append(popup);
|
|
315
344
|
place(popup, anchor);
|
|
316
345
|
anchor.setAttribute('aria-expanded', 'true');
|
|
317
|
-
|
|
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});
|
|
318
357
|
options.onChange?.();
|
|
319
358
|
if (focus) (popup.firstElementChild as HTMLElement | null)?.focus();
|
|
320
359
|
};
|
|
@@ -367,6 +406,7 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
367
406
|
update(next) {
|
|
368
407
|
close();
|
|
369
408
|
groups = next;
|
|
409
|
+
applyMemberOrder();
|
|
370
410
|
parentOf.clear();
|
|
371
411
|
for (const [id, members] of groups.members) for (const member of members) parentOf.set(member, id);
|
|
372
412
|
for (const category of options.categories()) {
|
|
@@ -396,6 +436,29 @@ export const attachPaletteCategoryGroups = (menu: HTMLElement, options: PaletteC
|
|
|
396
436
|
toggle,
|
|
397
437
|
isOpen: () => stack.length > 0,
|
|
398
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
|
+
},
|
|
399
462
|
selectionTarget(id) {
|
|
400
463
|
let target = id;
|
|
401
464
|
while (groups.members.get(target)?.length && !groups.withOwnContents.has(target)) {
|
|
@@ -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});
|