@kubohiroya/turbowarp-plus-editor-code-views 0.2.1 → 0.4.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 +25 -0
- package/README.md +25 -0
- package/dist/palette-category-visibility.d.ts +98 -0
- package/dist/palette-category-visibility.js +269 -0
- package/dist/palette-visibility-dialog.d.ts +68 -0
- package/dist/palette-visibility-dialog.js +176 -0
- package/package.json +11 -1
- package/src/palette-category-visibility.ts +311 -0
- package/src/palette-visibility-dialog.tsx +250 -0
- package/src/styles.css +197 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom';
|
|
3
|
+
import { ExtensionRow } from '@kubohiroya/turbowarp-extension-security-dialog/react';
|
|
4
|
+
import { standardGroups } from "./palette-category-visibility.js";
|
|
5
|
+
const standardNames = {
|
|
6
|
+
en: { motion: 'Motion', looks: 'Looks', sound: 'Sound', events: 'Events', control: 'Control', sensing: 'Sensing',
|
|
7
|
+
operators: 'Operators', variables: 'Variables', lists: 'Lists', myBlocks: 'My Blocks' },
|
|
8
|
+
ja: { motion: '動き', looks: '見た目', sound: '音', events: 'イベント', control: '制御', sensing: '調べる',
|
|
9
|
+
operators: '演算', variables: '変数', lists: 'リスト', myBlocks: 'ブロック定義' }
|
|
10
|
+
};
|
|
11
|
+
const standardColors = {
|
|
12
|
+
motion: '#4C97FF', looks: '#9966FF', sound: '#CF63CF', events: '#FFBF00', control: '#FFAB19', sensing: '#5CB1D6',
|
|
13
|
+
operators: '#59C059', variables: '#FF8C1A', lists: '#FF661A', myBlocks: '#FF6680'
|
|
14
|
+
};
|
|
15
|
+
const messages = {
|
|
16
|
+
en: {
|
|
17
|
+
title: 'Block groups shown in the palette', sprite: (name) => `Sprite “${name}”`, stage: 'Stage',
|
|
18
|
+
sets: 'Applicable sets', addSet: 'Add applicable set', setName: 'Set name', add: 'Add',
|
|
19
|
+
standard: 'Standard block groups', extensions: 'Extension block groups', used: (count) => `In use ${count}`,
|
|
20
|
+
hiddenInUse: (count) => `Hidden and in use: ${count}`, cancel: 'Cancel', apply: 'Apply', close: 'Close'
|
|
21
|
+
},
|
|
22
|
+
ja: {
|
|
23
|
+
title: 'パレット内のブロックグループの表示・非表示の設定', sprite: (name) => `スプライト「${name}」`, stage: '背景',
|
|
24
|
+
sets: '適用可能なセット', addSet: '適用可能なセットを追加', setName: 'セットの名前', add: '追加',
|
|
25
|
+
standard: '標準のブロックグループ', extensions: '機能拡張のブロックグループ', used: (count) => `使用中 ${count}`,
|
|
26
|
+
hiddenInUse: (count) => `非表示中・使用中 ${count} 件`, cancel: 'キャンセル', apply: '適用', close: '閉じる'
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const isJapanese = (locale) => /^ja(?:-|$)/i.test(locale ?? '');
|
|
30
|
+
/**
|
|
31
|
+
* The screen that chooses which block groups the palette shows for one sprite or the stage: sets that
|
|
32
|
+
* switch several groups at once, the standard groups, and one row per extension drawn the same way as the
|
|
33
|
+
* extension review.
|
|
34
|
+
*/
|
|
35
|
+
export class PaletteVisibilityDialog extends React.PureComponent {
|
|
36
|
+
constructor(props) {
|
|
37
|
+
super(props);
|
|
38
|
+
this.state = { shown: new Set(props.initial), addedSets: [], naming: false, setName: '' };
|
|
39
|
+
}
|
|
40
|
+
get text() {
|
|
41
|
+
return isJapanese(this.props.locale) ? messages.ja : messages.en;
|
|
42
|
+
}
|
|
43
|
+
standard() {
|
|
44
|
+
// The stage has no motion blocks.
|
|
45
|
+
return standardGroups.filter(group => !(this.props.isStage && group === 'motion'));
|
|
46
|
+
}
|
|
47
|
+
available(groups) {
|
|
48
|
+
const offered = new Set([...this.standard(), ...this.props.extensions.map(extension => extension.id)]);
|
|
49
|
+
return groups.filter(group => offered.has(group));
|
|
50
|
+
}
|
|
51
|
+
toggle(group) {
|
|
52
|
+
this.setState(({ shown }) => {
|
|
53
|
+
const next = new Set(shown);
|
|
54
|
+
if (next.has(group))
|
|
55
|
+
next.delete(group);
|
|
56
|
+
else
|
|
57
|
+
next.add(group);
|
|
58
|
+
return { shown: next };
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
toggleSet(set) {
|
|
62
|
+
const groups = this.available(set.groups);
|
|
63
|
+
this.setState(({ shown }) => {
|
|
64
|
+
const next = new Set(shown);
|
|
65
|
+
const all = groups.every(group => next.has(group));
|
|
66
|
+
for (const group of groups) {
|
|
67
|
+
if (all)
|
|
68
|
+
next.delete(group);
|
|
69
|
+
else
|
|
70
|
+
next.add(group);
|
|
71
|
+
}
|
|
72
|
+
return { shown: next };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
addSet() {
|
|
76
|
+
const name = this.state.setName.trim();
|
|
77
|
+
if (!name)
|
|
78
|
+
return;
|
|
79
|
+
const taken = new Set([...this.props.sets, ...this.state.addedSets].map(set => set.id));
|
|
80
|
+
let id = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'set';
|
|
81
|
+
for (let index = 2; taken.has(id); index++)
|
|
82
|
+
id = `${id.replace(/-\d+$/, '')}-${index}`;
|
|
83
|
+
const groups = [...this.standard(), ...this.props.extensions.map(extension => extension.id)]
|
|
84
|
+
.filter(group => this.state.shown.has(group));
|
|
85
|
+
this.setState(({ addedSets }) => ({ addedSets: [...addedSets, { id, name, groups }], naming: false, setName: '' }));
|
|
86
|
+
}
|
|
87
|
+
apply() {
|
|
88
|
+
const order = [...this.standard(), ...this.props.extensions.map(extension => extension.id)];
|
|
89
|
+
this.props.onApply({ groups: order.filter(group => this.state.shown.has(group)), addedSets: this.state.addedSets });
|
|
90
|
+
}
|
|
91
|
+
render() {
|
|
92
|
+
const { locale, targetName, isStage, used, extensions, onCancel } = this.props;
|
|
93
|
+
const { shown, naming, setName } = this.state;
|
|
94
|
+
const text = this.text;
|
|
95
|
+
const names = isJapanese(locale) ? standardNames.ja : standardNames.en;
|
|
96
|
+
const sets = [...this.props.sets, ...this.state.addedSets];
|
|
97
|
+
const hiddenInUse = [...used].filter(([group, count]) => count > 0 && !shown.has(group)).length;
|
|
98
|
+
const usedBadge = (group) => !shown.has(group) && (used.get(group) ?? 0) > 0 && (React.createElement("span", { className: "tw-palette-visibility-used" }, text.used(used.get(group))));
|
|
99
|
+
return (React.createElement("div", { className: "tw-palette-visibility" },
|
|
100
|
+
React.createElement("header", { className: "tw-palette-visibility-header" },
|
|
101
|
+
React.createElement("div", null,
|
|
102
|
+
React.createElement("h2", null, text.title),
|
|
103
|
+
React.createElement("p", null, isStage ? text.stage : text.sprite(targetName))),
|
|
104
|
+
React.createElement("button", { type: "button", className: "tw-palette-visibility-close", "aria-label": text.close, title: text.close, onClick: onCancel }, "\u00D7")),
|
|
105
|
+
React.createElement("section", { "aria-label": text.sets },
|
|
106
|
+
React.createElement("h3", null, text.sets),
|
|
107
|
+
React.createElement("div", { className: "tw-palette-visibility-sets" },
|
|
108
|
+
sets.map(set => {
|
|
109
|
+
const groups = this.available(set.groups);
|
|
110
|
+
const all = groups.length > 0 && groups.every(group => shown.has(group));
|
|
111
|
+
return (React.createElement("button", { key: set.id, type: "button", "aria-pressed": all, "data-set": set.id, onClick: () => this.toggleSet(set) },
|
|
112
|
+
set.name,
|
|
113
|
+
all ? ' ✓' : ''));
|
|
114
|
+
}),
|
|
115
|
+
naming ? (React.createElement("span", { className: "tw-palette-visibility-naming" },
|
|
116
|
+
React.createElement("input", { type: "text", "aria-label": text.setName, placeholder: text.setName, value: setName, autoFocus: true, onChange: event => this.setState({ setName: event.currentTarget.value }), onKeyDown: event => { if (event.key === 'Enter')
|
|
117
|
+
this.addSet(); } }),
|
|
118
|
+
React.createElement("button", { type: "button", onClick: () => this.addSet() }, text.add))) : (React.createElement("button", { type: "button", className: "tw-palette-visibility-add-set", onClick: () => this.setState({ naming: true }) },
|
|
119
|
+
"\uFF0B",
|
|
120
|
+
text.addSet)))),
|
|
121
|
+
React.createElement("section", { "aria-label": text.standard },
|
|
122
|
+
React.createElement("h3", null, text.standard),
|
|
123
|
+
React.createElement("div", { className: "tw-palette-visibility-standard" }, this.standard().map(group => (React.createElement("label", { key: group, "data-group": group },
|
|
124
|
+
React.createElement("input", { type: "checkbox", checked: shown.has(group), onChange: () => this.toggle(group) }),
|
|
125
|
+
React.createElement("span", { className: "tw-palette-visibility-swatch", style: { color: standardColors[group] }, "aria-hidden": "true" }, "\u25CF"),
|
|
126
|
+
names[group],
|
|
127
|
+
usedBadge(group)))))),
|
|
128
|
+
extensions.length > 0 && (React.createElement("section", { "aria-label": text.extensions },
|
|
129
|
+
React.createElement("h3", null, text.extensions),
|
|
130
|
+
React.createElement("div", { className: "twextsec-review" },
|
|
131
|
+
React.createElement("ul", { className: "twextsec-rows" }, extensions.map(extension => (React.createElement(ExtensionRow, { key: extension.id, metadata: extension.metadata, ids: [extension.id], locale: locale, itemProps: { 'data-group': extension.id }, pick: { checked: shown.has(extension.id), onToggle: () => this.toggle(extension.id) } }, usedBadge(extension.id)))))))),
|
|
132
|
+
React.createElement("footer", { className: "tw-palette-visibility-footer" },
|
|
133
|
+
React.createElement("span", { role: "status" }, hiddenInUse > 0 ? text.hiddenInUse(hiddenInUse) : ''),
|
|
134
|
+
React.createElement("span", null,
|
|
135
|
+
React.createElement("button", { type: "button", onClick: onCancel }, text.cancel),
|
|
136
|
+
React.createElement("button", { type: "button", className: "tw-palette-visibility-apply", onClick: () => this.apply() }, text.apply)))));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Opens the screen as a modal `<dialog>` in `document`. Returns the function that closes it. */
|
|
140
|
+
export function openPaletteVisibilityDialog(document, props) {
|
|
141
|
+
const dialog = document.createElement('dialog');
|
|
142
|
+
dialog.className = 'tw-palette-visibility-dialog';
|
|
143
|
+
dialog.setAttribute('aria-label', (isJapanese(props.locale) ? messages.ja : messages.en).title);
|
|
144
|
+
document.body.append(dialog);
|
|
145
|
+
let open = true;
|
|
146
|
+
const close = () => {
|
|
147
|
+
if (!open)
|
|
148
|
+
return;
|
|
149
|
+
open = false;
|
|
150
|
+
ReactDOM.unmountComponentAtNode(dialog);
|
|
151
|
+
if (dialog.open && typeof dialog.close === 'function')
|
|
152
|
+
dialog.close();
|
|
153
|
+
dialog.remove();
|
|
154
|
+
};
|
|
155
|
+
dialog.addEventListener('cancel', event => {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
props.onCancel();
|
|
158
|
+
close();
|
|
159
|
+
});
|
|
160
|
+
ReactDOM.render(React.createElement(PaletteVisibilityDialog, {
|
|
161
|
+
...props,
|
|
162
|
+
onApply: choice => {
|
|
163
|
+
close();
|
|
164
|
+
props.onApply(choice);
|
|
165
|
+
},
|
|
166
|
+
onCancel: () => {
|
|
167
|
+
close();
|
|
168
|
+
props.onCancel();
|
|
169
|
+
}
|
|
170
|
+
}), dialog);
|
|
171
|
+
if (typeof dialog.showModal === 'function')
|
|
172
|
+
dialog.showModal();
|
|
173
|
+
else
|
|
174
|
+
dialog.setAttribute('open', '');
|
|
175
|
+
return close;
|
|
176
|
+
}
|
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.4.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",
|
|
@@ -50,6 +50,14 @@
|
|
|
50
50
|
"types": "./dist/palette-dropdown-resize.d.ts",
|
|
51
51
|
"default": "./dist/palette-dropdown-resize.js"
|
|
52
52
|
},
|
|
53
|
+
"./palette-category-visibility": {
|
|
54
|
+
"types": "./dist/palette-category-visibility.d.ts",
|
|
55
|
+
"default": "./dist/palette-category-visibility.js"
|
|
56
|
+
},
|
|
57
|
+
"./palette-visibility-dialog": {
|
|
58
|
+
"types": "./dist/palette-visibility-dialog.d.ts",
|
|
59
|
+
"default": "./dist/palette-visibility-dialog.js"
|
|
60
|
+
},
|
|
53
61
|
"./block-argument-text": {
|
|
54
62
|
"types": "./dist/block-argument-text.d.ts",
|
|
55
63
|
"default": "./dist/block-argument-text.js"
|
|
@@ -86,11 +94,13 @@
|
|
|
86
94
|
"@kubohiroya/turbowarp-script-transfer": "0.1.5"
|
|
87
95
|
},
|
|
88
96
|
"peerDependencies": {
|
|
97
|
+
"@kubohiroya/turbowarp-extension-security-dialog": ">=0.9.0 <0.10.0",
|
|
89
98
|
"@kubohiroya/turbowarp-plus-editor-windows": ">=0.2.0 <0.3.0",
|
|
90
99
|
"react": ">=16.14",
|
|
91
100
|
"react-dom": ">=16.14"
|
|
92
101
|
},
|
|
93
102
|
"devDependencies": {
|
|
103
|
+
"@kubohiroya/turbowarp-extension-security-dialog": "0.9.0",
|
|
94
104
|
"@kubohiroya/turbowarp-plus-editor-windows": "0.2.0",
|
|
95
105
|
"@types/react": "18.3.31",
|
|
96
106
|
"@types/react-dom": "18.3.7",
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which block groups the palette shows for each sprite and the stage.
|
|
3
|
+
*
|
|
4
|
+
* The settings belong to the editor, not to the project's code: they are kept in memory per target and saved with
|
|
5
|
+
* the editor's own screen state (a slice of `tw-plus/workspace.json` in an SB3, `.tw-plus/workspace.json` in a
|
|
6
|
+
* project folder), the same way the tab layout is. Hiding a group changes only what the palette offers; blocks
|
|
7
|
+
* already in the code stay visible and editable.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The standard groups, by the palette's category IDs. `lists` is Plus's own split of `variables`. */
|
|
11
|
+
export const standardGroups = ['motion', 'looks', 'sound', 'events', 'control', 'sensing', 'operators', 'variables', 'lists',
|
|
12
|
+
'myBlocks'] as const;
|
|
13
|
+
|
|
14
|
+
/** A named set of groups the project keeps. */
|
|
15
|
+
export interface SavedGroupSet {
|
|
16
|
+
name: string;
|
|
17
|
+
groups: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PaletteGroupSet {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
groups: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const builtinSetNames: Record<string, {en: string; ja: string}> = {
|
|
27
|
+
standard: {en: 'Standard', ja: '標準'},
|
|
28
|
+
expression: {en: 'Expression', ja: '表現'},
|
|
29
|
+
data: {en: 'Variables and lists', ja: '変数・リスト'},
|
|
30
|
+
basics: {en: 'Basic control', ja: '基本の制御'}
|
|
31
|
+
};
|
|
32
|
+
const builtinSetGroups: Record<string, readonly string[]> = {
|
|
33
|
+
standard: standardGroups,
|
|
34
|
+
expression: ['motion', 'looks', 'sound', 'sensing'],
|
|
35
|
+
data: ['variables', 'lists'],
|
|
36
|
+
basics: ['events', 'control', 'operators']
|
|
37
|
+
};
|
|
38
|
+
const japanese = (locale?: string) => /^ja(?:-|$)/i.test(locale ?? '');
|
|
39
|
+
|
|
40
|
+
/** The sets every project has, then the project's own sets. */
|
|
41
|
+
export function paletteGroupSets(locale?: string, projectSets: Record<string, SavedGroupSet> = {}): PaletteGroupSet[] {
|
|
42
|
+
const builtin = Object.keys(builtinSetGroups).map(id => ({
|
|
43
|
+
id, name: builtinSetNames[id][japanese(locale) ? 'ja' : 'en'], groups: builtinSetGroups[id]
|
|
44
|
+
}));
|
|
45
|
+
return builtin.concat(Object.entries(projectSets).map(([id, set]) => ({id, name: set.name, groups: set.groups})));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const standardPrefixes: Record<string, string> = {
|
|
49
|
+
motion: 'motion', looks: 'looks', sound: 'sound', event: 'events', control: 'control', sensing: 'sensing',
|
|
50
|
+
operator: 'operators', procedures: 'myBlocks', argument: 'myBlocks'
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
54
|
+
export function groupOfOpcode(opcode: string): string | null {
|
|
55
|
+
const separator = opcode.indexOf('_');
|
|
56
|
+
if (separator <= 0) return null;
|
|
57
|
+
const prefix = opcode.slice(0, separator);
|
|
58
|
+
if (prefix === 'data') return /list/i.test(opcode.slice(separator + 1)) ? 'lists' : 'variables';
|
|
59
|
+
return standardPrefixes[prefix] ?? prefix;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type BlockLike = {opcode?: string; shadow?: boolean};
|
|
63
|
+
/** The parts of a VM target this module reads. */
|
|
64
|
+
export interface PaletteTarget {
|
|
65
|
+
id: string;
|
|
66
|
+
isStage: boolean;
|
|
67
|
+
getName(): string;
|
|
68
|
+
blocks?: {_blocks?: Record<string, BlockLike>};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
|
|
72
|
+
export function usedGroups(target: Pick<PaletteTarget, 'blocks'>): Map<string, number> {
|
|
73
|
+
const counts = new Map<string, number>();
|
|
74
|
+
for (const block of Object.values(target.blocks?._blocks ?? {})) {
|
|
75
|
+
if (!block || block.shadow || typeof block.opcode !== 'string') continue;
|
|
76
|
+
const group = groupOfOpcode(block.opcode);
|
|
77
|
+
if (group) counts.set(group, (counts.get(group) ?? 0) + 1);
|
|
78
|
+
}
|
|
79
|
+
return counts;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The groups shown when a target has no setting of its own: every standard group, and the groups of
|
|
84
|
+
* the extensions its code already uses, so a project opens with nothing missing.
|
|
85
|
+
*/
|
|
86
|
+
export function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks'>, extensionIds: readonly string[]): string[] {
|
|
87
|
+
const used = usedGroups(target);
|
|
88
|
+
return [...standardGroups, ...extensionIds.filter(id => used.has(id))];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
|
|
92
|
+
export function filterToolboxTree(tree: Element | null | undefined, visible: ReadonlySet<string>): void {
|
|
93
|
+
if (!tree) return;
|
|
94
|
+
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
95
|
+
const id = category.getAttribute('id');
|
|
96
|
+
if (id && !visible.has(id)) category.remove();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The screen-state slice the settings are saved as. Sprites are named as the tab layout names them. */
|
|
101
|
+
export const paletteScreenStateKey = 'blockPalette';
|
|
102
|
+
export interface SavedPaletteVisibility {
|
|
103
|
+
version: 1;
|
|
104
|
+
stage?: {groups: string[]};
|
|
105
|
+
sprites?: Record<string, {groups: string[]}>;
|
|
106
|
+
sets?: Record<string, SavedGroupSet>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const strings = (value: unknown): string[] | undefined =>
|
|
110
|
+
Array.isArray(value) && value.every(item => typeof item === 'string') ? [...value] : undefined;
|
|
111
|
+
const sameGroups = (left: readonly string[], right: readonly string[]) =>
|
|
112
|
+
left.length === right.length && new Set(left).size === left.length && left.every(group => right.includes(group));
|
|
113
|
+
|
|
114
|
+
export interface PaletteVisibilityStore {
|
|
115
|
+
/** The groups the palette shows for the target. */
|
|
116
|
+
visible(target: PaletteTarget, extensionIds: readonly string[]): Set<string>;
|
|
117
|
+
/** Keeps the target's groups. Groups equal to its default leave it without a setting of its own. */
|
|
118
|
+
set(target: PaletteTarget, groups: readonly string[], extensionIds: readonly string[]): void;
|
|
119
|
+
/** A new sprite made from another, such as a duplicate, starts with its setting. */
|
|
120
|
+
copy(from: Pick<PaletteTarget, 'id'>, to: Pick<PaletteTarget, 'id'>): void;
|
|
121
|
+
/** The project's own sets. */
|
|
122
|
+
sets(): Record<string, SavedGroupSet>;
|
|
123
|
+
addSets(sets: readonly PaletteGroupSet[]): void;
|
|
124
|
+
/** The slice to save, or nothing when no target has a setting and the project has no sets. */
|
|
125
|
+
capture(targets: readonly PaletteTarget[]): SavedPaletteVisibility | undefined;
|
|
126
|
+
/** Takes the settings of a loaded project. A value that is not a saved slice leaves every target at its default. */
|
|
127
|
+
apply(value: unknown, targets: readonly PaletteTarget[]): void;
|
|
128
|
+
/** Forgets every setting, for a project without any. */
|
|
129
|
+
reset(): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
|
|
133
|
+
export function createPaletteVisibilityStore(): PaletteVisibilityStore {
|
|
134
|
+
let groups = new Map<string, string[]>();
|
|
135
|
+
let projectSets: Record<string, SavedGroupSet> = {};
|
|
136
|
+
return {
|
|
137
|
+
visible(target, extensionIds) {
|
|
138
|
+
return new Set(groups.get(target.id) ?? defaultVisibleGroups(target, extensionIds));
|
|
139
|
+
},
|
|
140
|
+
set(target, chosen, extensionIds) {
|
|
141
|
+
if (sameGroups(chosen, defaultVisibleGroups(target, extensionIds))) groups.delete(target.id);
|
|
142
|
+
else groups.set(target.id, [...chosen]);
|
|
143
|
+
},
|
|
144
|
+
copy(from, to) {
|
|
145
|
+
const chosen = groups.get(from.id);
|
|
146
|
+
if (chosen) groups.set(to.id, [...chosen]);
|
|
147
|
+
},
|
|
148
|
+
sets: () => ({...projectSets}),
|
|
149
|
+
addSets(sets) {
|
|
150
|
+
for (const set of sets) projectSets[set.id] = {name: set.name, groups: [...set.groups]};
|
|
151
|
+
},
|
|
152
|
+
capture(targets) {
|
|
153
|
+
const saved: SavedPaletteVisibility = {version: 1};
|
|
154
|
+
const sprites: Record<string, {groups: string[]}> = {};
|
|
155
|
+
for (const target of targets) {
|
|
156
|
+
const chosen = groups.get(target.id);
|
|
157
|
+
if (!chosen) continue;
|
|
158
|
+
if (target.isStage) saved.stage = {groups: [...chosen]};
|
|
159
|
+
else sprites[target.getName()] = {groups: [...chosen]};
|
|
160
|
+
}
|
|
161
|
+
if (Object.keys(sprites).length) saved.sprites = sprites;
|
|
162
|
+
if (Object.keys(projectSets).length) saved.sets = structuredClone(projectSets);
|
|
163
|
+
return saved.stage || saved.sprites || saved.sets ? saved : undefined;
|
|
164
|
+
},
|
|
165
|
+
apply(value, targets) {
|
|
166
|
+
groups = new Map();
|
|
167
|
+
projectSets = {};
|
|
168
|
+
if (!value || typeof value !== 'object' || (value as {version?: unknown}).version !== 1) return;
|
|
169
|
+
const saved = value as Record<string, unknown>;
|
|
170
|
+
const setting = (entry: unknown) => entry && typeof entry === 'object' ?
|
|
171
|
+
strings((entry as {groups?: unknown}).groups) : undefined;
|
|
172
|
+
const sprites = saved.sprites && typeof saved.sprites === 'object' ? saved.sprites as Record<string, unknown> : {};
|
|
173
|
+
for (const target of targets) {
|
|
174
|
+
const chosen = target.isStage ? setting(saved.stage) :
|
|
175
|
+
Object.hasOwn(sprites, target.getName()) ? setting(sprites[target.getName()]) : undefined;
|
|
176
|
+
if (chosen) groups.set(target.id, chosen);
|
|
177
|
+
}
|
|
178
|
+
if (saved.sets && typeof saved.sets === 'object') {
|
|
179
|
+
for (const [id, set] of Object.entries(saved.sets as Record<string, unknown>)) {
|
|
180
|
+
const name = set && typeof set === 'object' ? (set as {name?: unknown}).name : undefined;
|
|
181
|
+
const members = setting(set);
|
|
182
|
+
if (typeof name === 'string' && members) projectSets[id] = {name, groups: members};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
reset() {
|
|
187
|
+
groups = new Map();
|
|
188
|
+
projectSets = {};
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The menu item that opens the group settings. */
|
|
194
|
+
export const paletteVisibilityMenuLabel = (locale: string) => japanese(locale) ? 'ブロックグループの表示・非表示の設定' :
|
|
195
|
+
'Show or hide block groups';
|
|
196
|
+
|
|
197
|
+
export interface PaletteMenuItem {
|
|
198
|
+
label: string;
|
|
199
|
+
onSelect(): void;
|
|
200
|
+
/** Draws a separator above the item. */
|
|
201
|
+
separated?: boolean;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const menuButtonLabel = (locale: string) => japanese(locale) ? 'パレットのメニュー' : 'Palette menu';
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Adds a ▼ button at the end of the palette's category menu: after the categories, so it sits at the
|
|
208
|
+
* right end of a horizontal palette and at the bottom of a vertical one. Pressing it lists the items
|
|
209
|
+
* the host gives — the group settings and the host's extension items — and runs the one chosen.
|
|
210
|
+
*/
|
|
211
|
+
export function attachPaletteMenuButton(menu: HTMLElement, getLocale: () => string,
|
|
212
|
+
getItems: (locale: string) => readonly PaletteMenuItem[]) {
|
|
213
|
+
const doc = menu.ownerDocument;
|
|
214
|
+
const button = doc.createElement('button');
|
|
215
|
+
button.type = 'button';
|
|
216
|
+
button.className = 'tw-palette-menu-button';
|
|
217
|
+
button.textContent = '▼';
|
|
218
|
+
button.setAttribute('aria-haspopup', 'menu');
|
|
219
|
+
button.setAttribute('aria-expanded', 'false');
|
|
220
|
+
const label = () => {
|
|
221
|
+
const text = menuButtonLabel(getLocale());
|
|
222
|
+
button.setAttribute('aria-label', text);
|
|
223
|
+
button.title = text;
|
|
224
|
+
};
|
|
225
|
+
label();
|
|
226
|
+
let popup: HTMLElement | null = null;
|
|
227
|
+
const closeMenu = (focusButton = false) => {
|
|
228
|
+
if (!popup) return;
|
|
229
|
+
popup.remove();
|
|
230
|
+
popup = null;
|
|
231
|
+
button.setAttribute('aria-expanded', 'false');
|
|
232
|
+
doc.removeEventListener('pointerdown', outside, true);
|
|
233
|
+
doc.removeEventListener('keydown', keys, true);
|
|
234
|
+
if (focusButton) button.focus();
|
|
235
|
+
};
|
|
236
|
+
const outside = (event: Event) => {
|
|
237
|
+
if (popup && !popup.contains(event.target as Node) && event.target !== button) closeMenu();
|
|
238
|
+
};
|
|
239
|
+
const keys = (event: KeyboardEvent) => {
|
|
240
|
+
if (!popup) return;
|
|
241
|
+
const items = Array.from(popup.querySelectorAll<HTMLButtonElement>('[role="menuitem"]'));
|
|
242
|
+
const index = items.indexOf(doc.activeElement as HTMLButtonElement);
|
|
243
|
+
if (event.key === 'Escape') {
|
|
244
|
+
event.preventDefault();
|
|
245
|
+
closeMenu(true);
|
|
246
|
+
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
247
|
+
event.preventDefault();
|
|
248
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
249
|
+
items[(index + step + items.length) % items.length]?.focus();
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const openMenu = () => {
|
|
253
|
+
const locale = getLocale();
|
|
254
|
+
const items = getItems(locale);
|
|
255
|
+
if (!items.length) return;
|
|
256
|
+
popup = doc.createElement('div');
|
|
257
|
+
popup.className = 'tw-palette-menu';
|
|
258
|
+
popup.setAttribute('role', 'menu');
|
|
259
|
+
popup.setAttribute('aria-label', menuButtonLabel(locale));
|
|
260
|
+
for (const item of items) {
|
|
261
|
+
if (item.separated && popup.childElementCount) {
|
|
262
|
+
const separator = doc.createElement('div');
|
|
263
|
+
separator.setAttribute('role', 'separator');
|
|
264
|
+
popup.append(separator);
|
|
265
|
+
}
|
|
266
|
+
const entry = doc.createElement('button');
|
|
267
|
+
entry.type = 'button';
|
|
268
|
+
entry.setAttribute('role', 'menuitem');
|
|
269
|
+
entry.textContent = item.label;
|
|
270
|
+
entry.addEventListener('click', event => {
|
|
271
|
+
event.stopPropagation();
|
|
272
|
+
closeMenu();
|
|
273
|
+
item.onSelect();
|
|
274
|
+
});
|
|
275
|
+
popup.append(entry);
|
|
276
|
+
}
|
|
277
|
+
const rect = button.getBoundingClientRect();
|
|
278
|
+
const view = doc.defaultView;
|
|
279
|
+
popup.style.position = 'fixed';
|
|
280
|
+
popup.style.top = `${rect.bottom + 4}px`;
|
|
281
|
+
popup.style.right = `${Math.max(4, (view?.innerWidth ?? rect.right) - rect.right)}px`;
|
|
282
|
+
doc.body.append(popup);
|
|
283
|
+
button.setAttribute('aria-expanded', 'true');
|
|
284
|
+
doc.addEventListener('pointerdown', outside, true);
|
|
285
|
+
doc.addEventListener('keydown', keys, true);
|
|
286
|
+
popup.querySelector<HTMLButtonElement>('[role="menuitem"]')?.focus();
|
|
287
|
+
};
|
|
288
|
+
// The category menu reacts to presses on its items; this one only opens its own menu.
|
|
289
|
+
const press = (event: Event) => event.stopPropagation();
|
|
290
|
+
button.addEventListener('mousedown', press);
|
|
291
|
+
button.addEventListener('pointerdown', press);
|
|
292
|
+
button.addEventListener('click', event => {
|
|
293
|
+
event.stopPropagation();
|
|
294
|
+
if (popup) closeMenu();
|
|
295
|
+
else openMenu();
|
|
296
|
+
});
|
|
297
|
+
menu.append(button);
|
|
298
|
+
return {
|
|
299
|
+
button,
|
|
300
|
+
/** Puts the button back at the end after the host rebuilds the menu, and follows the locale. */
|
|
301
|
+
update() {
|
|
302
|
+
label();
|
|
303
|
+
if (button.parentElement !== menu || menu.lastElementChild !== button) menu.append(button);
|
|
304
|
+
},
|
|
305
|
+
close: () => closeMenu(),
|
|
306
|
+
dispose() {
|
|
307
|
+
closeMenu();
|
|
308
|
+
button.remove();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|