@kubohiroya/turbowarp-plus-editor-code-views 0.21.2 → 0.22.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/EDITOR-MIGRATION.md +5 -0
- package/README.ja.md +16 -1
- package/dist/code-pane.d.ts +7 -1
- package/dist/code-pane.js +45 -3
- package/dist/code-popout.d.ts +10 -0
- package/dist/code-popout.js +402 -0
- package/dist/context-menu-icons.d.ts +15 -0
- package/dist/context-menu-icons.js +204 -0
- package/dist/desktop-code.d.ts +11 -0
- package/dist/desktop-code.js +47 -0
- package/dist/horizontal-block-palette.d.ts +39 -0
- package/dist/horizontal-block-palette.js +774 -0
- package/dist/palette-delete-preview.d.ts +3 -0
- package/dist/palette-delete-preview.js +73 -0
- package/dist/script-arrows.js +106 -12
- package/dist/script-layout-view.js +122 -6
- package/dist/script-layout-workspace.js +7 -2
- package/dist/script-layout.d.ts +1 -1
- package/dist/script-layout.js +3 -3
- package/dist/script-projection.d.ts +8 -0
- package/dist/script-projection.js +10 -0
- package/dist/script-view-menu.d.ts +1 -0
- package/dist/script-view-menu.js +4 -2
- package/dist/serial-script-editor.d.ts +4 -0
- package/dist/serial-script-editor.js +43 -0
- package/dist/workspace-overview.js +67 -25
- package/dist/workspace.d.ts +1 -0
- package/dist/workspace.js +3 -2
- package/package.json +27 -3
- package/src/code-pane.ts +41 -3
- package/src/code-popout.tsx +399 -0
- package/src/context-menu-icons.css +14 -0
- package/src/context-menu-icons.ts +215 -0
- package/src/desktop-code.ts +45 -0
- package/src/horizontal-block-palette.css +111 -0
- package/src/horizontal-block-palette.ts +765 -0
- package/src/palette-delete-preview.css +21 -0
- package/src/palette-delete-preview.ts +79 -0
- package/src/pin-off.svg +6 -0
- package/src/pin.svg +5 -0
- package/src/script-arrows.ts +98 -12
- package/src/script-layout-view.ts +114 -6
- package/src/script-layout-workspace.ts +7 -2
- package/src/script-layout.ts +4 -3
- package/src/script-projection.ts +12 -0
- package/src/script-view-menu.ts +5 -2
- package/src/serial-script-editor.ts +41 -0
- package/src/styles.css +80 -5
- package/src/trash.svg +3 -0
- package/src/workspace-overview.ts +63 -22
- package/src/workspace.ts +3 -2
- package/tsconfig.json +87 -1
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
import {attachPaletteCategoryReorder} from './palette-category-reorder.js';
|
|
3
|
+
import {attachPaletteCategorySearch, matchPaletteBlocks, paletteSearchHeight} from
|
|
4
|
+
'./palette-category-search.js';
|
|
5
|
+
import {attachPaletteDropdownResize} from './palette-dropdown-resize.js';
|
|
6
|
+
import {connectDetachedBlockPalette, openDetachedBlockPalette as openPaletteWindow, installGestureOwnerDocument, moveBlockWidgets} from
|
|
7
|
+
'./detached-block-palette.js';
|
|
8
|
+
import {attachPaletteMenuButton, createPaletteOrder, createPaletteVisibilityStore, defaultVisibleGroups, extensionPaletteCatalog,
|
|
9
|
+
installExtensionPaletteGroups, paletteGroupSets, paletteScreenStateKey, paletteVisibilityMenuLabel, usedGroups} from './palette-category-visibility.js';
|
|
10
|
+
import type {PaletteCategoryInfo, PaletteVisibilityStore} from './palette-category-visibility.js';
|
|
11
|
+
import {openPaletteVisibilityDialog} from './palette-visibility-dialog.js';
|
|
12
|
+
import {attachPaletteCategoryGroups, hasCategoryGroups} from
|
|
13
|
+
'./palette-category-groups.js';
|
|
14
|
+
|
|
15
|
+
/** Each editor host has its own palette state and lifecycle. */
|
|
16
|
+
export function createHorizontalBlockPalette({getScratchBlocks, openExtensionLibrary: openHostLibrary, styles}: {
|
|
17
|
+
getScratchBlocks(): {getMainWorkspace?(): object} | undefined;
|
|
18
|
+
openExtensionLibrary(document: Document): void;
|
|
19
|
+
styles: Record<string, string>;
|
|
20
|
+
}) {
|
|
21
|
+
const installed = new WeakSet();
|
|
22
|
+
const closeOnDragExit = new WeakMap();
|
|
23
|
+
const palettes = new Set<(horizontal: boolean) => void>();
|
|
24
|
+
let horizontalLayout = false;
|
|
25
|
+
|
|
26
|
+
// The VM whose editing target decides which block groups the palette shows.
|
|
27
|
+
type PaletteVM = {
|
|
28
|
+
editingTarget?: any;
|
|
29
|
+
runtime: {
|
|
30
|
+
targets?: any[];
|
|
31
|
+
_blockInfo?: PaletteCategoryInfo[];
|
|
32
|
+
_fillExtensionCategory?(...args: any[]): unknown;
|
|
33
|
+
getTargetForStage(): any;
|
|
34
|
+
emitProjectChanged(): void;
|
|
35
|
+
on(event: string, listener: (...args: any[]) => void): void;
|
|
36
|
+
off(event: string, listener: (...args: any[]) => void): void;
|
|
37
|
+
};
|
|
38
|
+
on(event: string, listener: (...args: any[]) => void): void;
|
|
39
|
+
off(event: string, listener: (...args: any[]) => void): void;
|
|
40
|
+
addSprite?(...args: any[]): Promise<unknown>;
|
|
41
|
+
duplicateSprite?(targetId: string): Promise<unknown>;
|
|
42
|
+
};
|
|
43
|
+
/** The part of the editor's screen state the palette settings are saved with. */
|
|
44
|
+
type PaletteScreenState = {
|
|
45
|
+
register(key: string, slice: {capture(): unknown}): () => void;
|
|
46
|
+
whenLoaded(): Promise<{slices: Record<string, unknown>} | undefined>;
|
|
47
|
+
};
|
|
48
|
+
let paletteVM: PaletteVM | null = null;
|
|
49
|
+
let paletteStore: PaletteVisibilityStore | null = null;
|
|
50
|
+
const paletteRefreshers = new Set<() => void>();
|
|
51
|
+
// The palettes that can ask which groups a sprite just added shows; the latest one asks.
|
|
52
|
+
const newSpritePrompts: ((target: any) => void)[] = [];
|
|
53
|
+
|
|
54
|
+
// The groups the loaded extensions offer: one per extension, or one per group an extension declares.
|
|
55
|
+
// Group names are in the editor's language, where the palette and the settings screen show them.
|
|
56
|
+
const catalogOf = (vm: PaletteVM, locale?: string) => extensionPaletteCatalog(vm.runtime._blockInfo, locale);
|
|
57
|
+
const extensionIds = (vm: PaletteVM) => catalogOf(vm).groups.map(group => group.id);
|
|
58
|
+
const originalTargets = (vm: PaletteVM) => (vm.runtime.targets ?? []).filter(target => target.isOriginal);
|
|
59
|
+
const sameGroupSet = (left: ReadonlySet<string>, right: readonly string[]) =>
|
|
60
|
+
left.size === right.length && right.every(group => left.has(group));
|
|
61
|
+
// Another sprite may share the same toolbox XML, in which case the GUI does not redraw the palette.
|
|
62
|
+
const refreshPalettes = () => setTimeout(() => {for (const update of paletteRefreshers) update();}, 0);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Lets the palette show each sprite's and the stage's own block groups, and saves them with the editor's screen
|
|
66
|
+
* state, as the tab layout is saved. Returns the function that stops it.
|
|
67
|
+
*/
|
|
68
|
+
const connectPaletteVisibility = (vm: PaletteVM, getScreenState: () => PaletteScreenState | undefined = () => undefined) => {
|
|
69
|
+
// A reordered palette is saved with the project, as the groups it shows are.
|
|
70
|
+
const store = createPaletteVisibilityStore({catalog: () => catalogOf(vm), onOrderChange: () => vm.runtime.emitProjectChanged()});
|
|
71
|
+
const removeExtensionGroups = installExtensionPaletteGroups(vm.runtime ?? {});
|
|
72
|
+
paletteVM = vm;
|
|
73
|
+
paletteStore = store;
|
|
74
|
+
let unregister: (() => void) | undefined;
|
|
75
|
+
const register = () => {
|
|
76
|
+
if (unregister) return;
|
|
77
|
+
unregister = getScreenState()?.register(paletteScreenStateKey, {capture: () => store.capture(originalTargets(vm))});
|
|
78
|
+
};
|
|
79
|
+
register();
|
|
80
|
+
// A project opens with its own settings, or with every target at its default.
|
|
81
|
+
const loaded = () => {
|
|
82
|
+
register();
|
|
83
|
+
store.reset();
|
|
84
|
+
refreshPalettes();
|
|
85
|
+
getScreenState()?.whenLoaded().then(state => {
|
|
86
|
+
if (paletteStore !== store) return;
|
|
87
|
+
const saved = state?.slices?.[paletteScreenStateKey];
|
|
88
|
+
if (saved === undefined) return;
|
|
89
|
+
store.apply(saved, originalTargets(vm));
|
|
90
|
+
refreshPalettes();
|
|
91
|
+
}, () => {});
|
|
92
|
+
};
|
|
93
|
+
const changed = () => {
|
|
94
|
+
vm.runtime.emitProjectChanged();
|
|
95
|
+
refreshPalettes();
|
|
96
|
+
};
|
|
97
|
+
// An extension added from the library shows all its groups in the sprite or stage being edited.
|
|
98
|
+
const added = (info: {id?: string}) => {
|
|
99
|
+
const target = vm.editingTarget;
|
|
100
|
+
if (!target || !info?.id) return;
|
|
101
|
+
const groups = store.visible(target, extensionIds(vm));
|
|
102
|
+
const offered = catalogOf(vm).groups.filter(group => group.extensionId === info.id).map(group => group.id);
|
|
103
|
+
if (offered.every(group => groups.has(group))) return;
|
|
104
|
+
store.set(target, [...groups, ...offered.filter(group => !groups.has(group))], extensionIds(vm));
|
|
105
|
+
changed();
|
|
106
|
+
};
|
|
107
|
+
// The VM tells a duplicate, a new sprite and a loaded one apart only by the method that made it, and each
|
|
108
|
+
// makes the new sprite the editing target.
|
|
109
|
+
const originalDuplicate = vm.duplicateSprite;
|
|
110
|
+
const originalAddSprite = vm.addSprite;
|
|
111
|
+
// A duplicated sprite starts with the groups of the sprite it was made from.
|
|
112
|
+
if (typeof originalDuplicate === 'function') {
|
|
113
|
+
vm.duplicateSprite = function (targetId: string) {
|
|
114
|
+
const source = vm.runtime.targets?.find(target => target.id === targetId);
|
|
115
|
+
return originalDuplicate.call(this, targetId).then(result => {
|
|
116
|
+
const target = vm.editingTarget;
|
|
117
|
+
if (paletteStore === store && source && target && target !== source) {
|
|
118
|
+
store.copy(source, target);
|
|
119
|
+
refreshPalettes();
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// A sprite painted, chosen, uploaded or picked by surprise asks which groups it shows, when the project has
|
|
126
|
+
// extension groups to choose from or another sprite shows groups other than its default, which it can copy.
|
|
127
|
+
if (typeof originalAddSprite === 'function') {
|
|
128
|
+
vm.addSprite = function (...args: unknown[]) {
|
|
129
|
+
const before = vm.editingTarget;
|
|
130
|
+
return originalAddSprite.apply(this, args).then(result => {
|
|
131
|
+
const target = vm.editingTarget;
|
|
132
|
+
if (paletteStore !== store || !target || target === before || target.isStage) return result;
|
|
133
|
+
const ids = extensionIds(vm);
|
|
134
|
+
const customized = originalTargets(vm).some(other => !other.isStage && other !== target &&
|
|
135
|
+
!sameGroupSet(store.visible(other, ids), defaultVisibleGroups(other, ids)));
|
|
136
|
+
if (ids.length || customized) newSpritePrompts.at(-1)?.(target);
|
|
137
|
+
return result;
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// A host that passes a VM without events still gets the filtering; only the redraws are missing.
|
|
142
|
+
const listens = typeof vm.on === 'function' && typeof vm.runtime?.on === 'function';
|
|
143
|
+
if (listens) {
|
|
144
|
+
vm.on('workspaceUpdate', refreshPalettes);
|
|
145
|
+
vm.runtime.on('PROJECT_LOADED', loaded);
|
|
146
|
+
vm.runtime.on('EXTENSION_ADDED', added);
|
|
147
|
+
}
|
|
148
|
+
return () => {
|
|
149
|
+
if (listens) {
|
|
150
|
+
vm.off('workspaceUpdate', refreshPalettes);
|
|
151
|
+
vm.runtime.off('PROJECT_LOADED', loaded);
|
|
152
|
+
vm.runtime.off('EXTENSION_ADDED', added);
|
|
153
|
+
}
|
|
154
|
+
if (vm.duplicateSprite !== originalDuplicate) vm.duplicateSprite = originalDuplicate;
|
|
155
|
+
if (vm.addSprite !== originalAddSprite) vm.addSprite = originalAddSprite;
|
|
156
|
+
unregister?.();
|
|
157
|
+
removeExtensionGroups();
|
|
158
|
+
if (paletteVM === vm) paletteVM = null;
|
|
159
|
+
if (paletteStore === store) paletteStore = null;
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const setHorizontalBlockPalette = (horizontal: boolean) => {
|
|
164
|
+
horizontalLayout = horizontal;
|
|
165
|
+
for (const setMode of palettes) setMode(horizontal);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const openDetachedBlockPalette = options => {
|
|
169
|
+
const workspace = getScratchBlocks()?.getMainWorkspace();
|
|
170
|
+
if (workspace) openPaletteWindow(workspace, options);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const attach = (ScratchBlocks, toolbox, {allowManualToggle, allowCategoryReorder}) => {
|
|
174
|
+
const workspace = toolbox.workspace_;
|
|
175
|
+
const flyout = toolbox.flyout_;
|
|
176
|
+
const menu = toolbox.HtmlDiv;
|
|
177
|
+
if (workspace.isMutator || toolbox.horizontalLayout_ || flyout.svgGroup_.tagName.toLowerCase() !== 'svg') return;
|
|
178
|
+
|
|
179
|
+
const panel = flyout.svgGroup_;
|
|
180
|
+
const body = flyout.workspace_.svgGroup_;
|
|
181
|
+
const bodyTransform = body.getAttribute('transform');
|
|
182
|
+
const scrollbar = flyout.scrollbar_.outerSvg_;
|
|
183
|
+
const surface = workspace.getParentSvg();
|
|
184
|
+
const doc = menu.ownerDocument;
|
|
185
|
+
const win = doc.defaultView;
|
|
186
|
+
const resizer = attachPaletteDropdownResize(panel, () => flyout.position());
|
|
187
|
+
const {ContextMenu, WidgetDiv, DropDownDiv} = ScratchBlocks;
|
|
188
|
+
const events = new AbortController();
|
|
189
|
+
const capture = {capture: true, signal: events.signal};
|
|
190
|
+
const original = {
|
|
191
|
+
dispose: toolbox.dispose,
|
|
192
|
+
getClientRect: toolbox.getClientRect,
|
|
193
|
+
getHeight: toolbox.getHeight,
|
|
194
|
+
getWidth: toolbox.getWidth,
|
|
195
|
+
populate: toolbox.populate_,
|
|
196
|
+
scrollToId: toolbox.scrollToCategoryById,
|
|
197
|
+
scrollToName: toolbox.scrollToCategoryByName,
|
|
198
|
+
showAll: toolbox.showAll_,
|
|
199
|
+
getFlyoutWidth: flyout.getWidth,
|
|
200
|
+
isDragTowardWorkspace: flyout.isDragTowardWorkspace,
|
|
201
|
+
layout: flyout.layout_,
|
|
202
|
+
position: flyout.position
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
let horizontal = false;
|
|
206
|
+
let detached = false;
|
|
207
|
+
let openId = null;
|
|
208
|
+
let layoutWidth;
|
|
209
|
+
let scrollBeforeSearch = 0;
|
|
210
|
+
const scrollPositions = new Map();
|
|
211
|
+
const verticalPosition = workspace.toolboxPosition;
|
|
212
|
+
const categoryById = id => toolbox.categoryMenu_.categories_.find(category => category.id_ === id);
|
|
213
|
+
const getLocale = () => ScratchBlocks.ScratchMsgs.currentLocale_ || doc.documentElement.lang;
|
|
214
|
+
// A category nested in another opens from its group's child palette.
|
|
215
|
+
const groups = attachPaletteCategoryGroups(menu, {
|
|
216
|
+
categories: () => toolbox.categoryMenu_.categories_,
|
|
217
|
+
horizontal: () => horizontal,
|
|
218
|
+
rtl: workspace.RTL,
|
|
219
|
+
choose: category => toolbox.setSelectedItem(category),
|
|
220
|
+
onChange: () => updateCategoryButtons(),
|
|
221
|
+
reorder: allowCategoryReorder,
|
|
222
|
+
onReorder: () => reordered(),
|
|
223
|
+
order: () => paletteOrder(),
|
|
224
|
+
locale: getLocale
|
|
225
|
+
});
|
|
226
|
+
const search = attachPaletteCategorySearch(panel, getLocale, (query, previous) => {
|
|
227
|
+
const category = categoryById(openId);
|
|
228
|
+
if (!category) return;
|
|
229
|
+
if (!previous) scrollBeforeSearch = flyout.getScrollPos();
|
|
230
|
+
flyout.scrollTarget = null;
|
|
231
|
+
if (query) showAllCategories();
|
|
232
|
+
else showCategory(category, scrollBeforeSearch);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const close = () => {
|
|
236
|
+
if (openId === null) return;
|
|
237
|
+
resizer.close();
|
|
238
|
+
scrollPositions.set(openId, search.query ? scrollBeforeSearch : flyout.getScrollPos());
|
|
239
|
+
search.close();
|
|
240
|
+
categoryById(openId)?.item_.setAttribute('aria-expanded', 'false');
|
|
241
|
+
openId = null;
|
|
242
|
+
updateCategoryButtons();
|
|
243
|
+
flyout.hide();
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const closeIfUnpinned = () => {
|
|
247
|
+
const pin = panel.querySelector(':scope > .sa-lock-object.locked');
|
|
248
|
+
if (!pin || win.getComputedStyle(pin).display === 'none') close();
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const categoryLabel = category => {
|
|
252
|
+
const label = doc.createElement('label');
|
|
253
|
+
label.setAttribute('text', category.name_);
|
|
254
|
+
label.setAttribute('web-class', 'categoryLabel');
|
|
255
|
+
label.setAttribute('showStatusButton', category.showStatusButton_);
|
|
256
|
+
return label;
|
|
257
|
+
};
|
|
258
|
+
const showContents = (contents, scroll) => {
|
|
259
|
+
if (flyout.RTL) {
|
|
260
|
+
layoutWidth = flyout.getWidth();
|
|
261
|
+
flyout.workspace_.scrollX = 0;
|
|
262
|
+
}
|
|
263
|
+
flyout.show(contents);
|
|
264
|
+
flyout.setScrollPos(scroll);
|
|
265
|
+
};
|
|
266
|
+
const showCategory = (category, scroll) => showContents([categoryLabel(category)].concat(category.getContents()), scroll);
|
|
267
|
+
// A search looks through every category the palette offers, whatever group it sits in: nested groups are
|
|
268
|
+
// flattened into the toolbox, and the groups the target hides are not in it.
|
|
269
|
+
const showAllCategories = () => showContents(toolbox.categoryMenu_.categories_
|
|
270
|
+
.flatMap(category => [categoryLabel(category)].concat(category.getContents())), 0);
|
|
271
|
+
|
|
272
|
+
flyout.layout_ = (contents, gaps) => {
|
|
273
|
+
const category = categoryById(openId);
|
|
274
|
+
if (!horizontal || !category || !search.query) {
|
|
275
|
+
original.layout.call(flyout, contents, gaps);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
// Each category's heading leads its blocks, so a query naming a category finds all of its blocks. A name
|
|
279
|
+
// broken over lines also matches with the break read as a space or as nothing.
|
|
280
|
+
const heading = (text: string) => {
|
|
281
|
+
const name = ScratchBlocks.utils.replaceMessageReferences(text);
|
|
282
|
+
return name.includes('\n') ? `${name.replace(/\s*\n\s*/g, ' ')} ${name.replace(/\s*\n\s*/g, '')}` : name;
|
|
283
|
+
};
|
|
284
|
+
const matching = matchPaletteBlocks(contents, '', search.query, heading,
|
|
285
|
+
(block: {inputList: {fieldRow: {getText(): string}[]}[]}) => block.inputList.flatMap(input => input.fieldRow)
|
|
286
|
+
.filter(field => field instanceof ScratchBlocks.FieldLabel)
|
|
287
|
+
.map(field => field.getText()).join(' '));
|
|
288
|
+
// A heading or button stays only when a block it leads still matches: a category heading for its category,
|
|
289
|
+
// any other label or button for the blocks up to the next label.
|
|
290
|
+
const isCategoryHeading = item => item.type === 'button' && item.button.isLabel_ &&
|
|
291
|
+
item.button.cssClass_ === 'categoryLabel';
|
|
292
|
+
const leads = (start: number, isEnd: (item) => boolean) => {
|
|
293
|
+
for (let index = start + 1; index < contents.length && !isEnd(contents[index]); index++) {
|
|
294
|
+
if (contents[index].type === 'block' && matching.has(contents[index].block)) return true;
|
|
295
|
+
}
|
|
296
|
+
return false;
|
|
297
|
+
};
|
|
298
|
+
const kept = [];
|
|
299
|
+
const keptGaps = [];
|
|
300
|
+
for (let index = 0; index < contents.length; index++) {
|
|
301
|
+
const item = contents[index];
|
|
302
|
+
const keep = item.type === 'block' ? matching.has(item.block) :
|
|
303
|
+
isCategoryHeading(item) ? leads(index, isCategoryHeading) :
|
|
304
|
+
leads(index, next => next.type === 'button' && next.button.isLabel_);
|
|
305
|
+
if (keep) {
|
|
306
|
+
kept.push(item);
|
|
307
|
+
keptGaps.push(gaps[index]);
|
|
308
|
+
} else if (item.type === 'block') {
|
|
309
|
+
item.block.dispose(false, false);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
flyout.permanentlyDisabled_ = flyout.permanentlyDisabled_.filter(block => matching.has(block));
|
|
313
|
+
if (!matching.size) {
|
|
314
|
+
const label = doc.createElement('label');
|
|
315
|
+
label.setAttribute('text', search.emptyLabel);
|
|
316
|
+
kept.push({type: 'button', button: new ScratchBlocks.FlyoutButton(flyout.workspace_, workspace, label, true)});
|
|
317
|
+
keptGaps.push(flyout.GAP_Y);
|
|
318
|
+
}
|
|
319
|
+
original.layout.call(flyout, kept, keptGaps);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const open = category => {
|
|
323
|
+
if (!category) return;
|
|
324
|
+
if (openId === category.id_) {
|
|
325
|
+
flyout.position();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
close();
|
|
329
|
+
openId = category.id_;
|
|
330
|
+
resizer.open(openId);
|
|
331
|
+
toolbox.setSelectedItem(category, false);
|
|
332
|
+
category.item_.setAttribute('aria-expanded', 'true');
|
|
333
|
+
updateCategoryButtons();
|
|
334
|
+
showCategory(category, scrollPositions.get(category.id_) ?? 0);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const updateCategoryButtons = () => {
|
|
338
|
+
for (const category of toolbox.categoryMenu_.categories_) {
|
|
339
|
+
const item = category.item_;
|
|
340
|
+
// A group's icon opens its child palette, which marks it itself.
|
|
341
|
+
if (groups.isGroup(category.id_)) continue;
|
|
342
|
+
if (horizontal) {
|
|
343
|
+
item.tabIndex = 0;
|
|
344
|
+
item.setAttribute('role', 'button');
|
|
345
|
+
item.setAttribute('aria-expanded', String(openId === category.id_));
|
|
346
|
+
} else {
|
|
347
|
+
item.removeAttribute('tabindex');
|
|
348
|
+
item.removeAttribute('role');
|
|
349
|
+
item.removeAttribute('aria-expanded');
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
groups.syncButtons(openId);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
toolbox.getWidth = () => horizontal ? 0 : original.getWidth.call(toolbox);
|
|
356
|
+
toolbox.getHeight = () => horizontal ? (detached ? 0 : menu.offsetHeight) : original.getHeight.call(toolbox);
|
|
357
|
+
// The menu stays a drop-to-delete target even when the popover is closed.
|
|
358
|
+
toolbox.getClientRect = () => {
|
|
359
|
+
if (!horizontal) return original.getClientRect.call(toolbox);
|
|
360
|
+
const {left, top, right, bottom, width, height} = menu.getBoundingClientRect();
|
|
361
|
+
return {left, top, width, height, contains: ({x, y}) => x >= left && x <= right && y >= top && y <= bottom};
|
|
362
|
+
};
|
|
363
|
+
toolbox.scrollToCategoryById = id => {
|
|
364
|
+
if (horizontal) open(categoryById(id));
|
|
365
|
+
else original.scrollToId.call(toolbox, id);
|
|
366
|
+
};
|
|
367
|
+
toolbox.scrollToCategoryByName = name => {
|
|
368
|
+
if (horizontal) open(toolbox.categoryMenu_.categories_.find(category => category.name_ === name));
|
|
369
|
+
else original.scrollToName.call(toolbox, name);
|
|
370
|
+
};
|
|
371
|
+
const categories = () => {
|
|
372
|
+
const {categories_, table} = toolbox.categoryMenu_;
|
|
373
|
+
// Addons may split categories into separate lists.
|
|
374
|
+
return categories_.every(category => category.item_.parentElement.parentElement === table) ? categories_ : [];
|
|
375
|
+
};
|
|
376
|
+
// The categories the menu shows: a group's members are moved with it, so only the group is ordered.
|
|
377
|
+
const shownCategories = () => categories().filter(category => groups.topLevelId(category.id_) === category.id_);
|
|
378
|
+
// The order is the project's, or this palette's own without a project.
|
|
379
|
+
const ownOrder = createPaletteOrder();
|
|
380
|
+
const paletteOrder = () => paletteStore?.order ?? ownOrder;
|
|
381
|
+
const applyCategoryOrder = () => {
|
|
382
|
+
const current = categories();
|
|
383
|
+
const order = paletteOrder().categories();
|
|
384
|
+
const ordered: typeof current = groups.arrange(current, id => {
|
|
385
|
+
const index = order.indexOf(id);
|
|
386
|
+
return index < 0 ? order.length : index;
|
|
387
|
+
});
|
|
388
|
+
if (ordered.every((category, index) => category === current[index])) return;
|
|
389
|
+
current.splice(0, current.length, ...ordered);
|
|
390
|
+
toolbox.categoryMenu_.table.append(...ordered.map(category => category.item_.parentElement));
|
|
391
|
+
};
|
|
392
|
+
const reorder = allowCategoryReorder && attachPaletteCategoryReorder(
|
|
393
|
+
menu,
|
|
394
|
+
() => shownCategories().map(category => category.item_),
|
|
395
|
+
(from, to) => {
|
|
396
|
+
const ordered = shownCategories().map(category => category.id_);
|
|
397
|
+
ordered.splice(to, 0, ordered.splice(from, 1)[0]);
|
|
398
|
+
paletteOrder().setCategories(ordered.concat(paletteOrder().categories().filter(id => !ordered.includes(id))));
|
|
399
|
+
groups.close();
|
|
400
|
+
reordered();
|
|
401
|
+
}
|
|
402
|
+
);
|
|
403
|
+
// Lays the palette out again after its categories or a group's members were reordered.
|
|
404
|
+
function reordered() {
|
|
405
|
+
applyCategoryOrder();
|
|
406
|
+
if (horizontal) {
|
|
407
|
+
resizer.followAnchor();
|
|
408
|
+
flyout.position();
|
|
409
|
+
} else {
|
|
410
|
+
const selected = toolbox.getSelectedItem();
|
|
411
|
+
original.showAll.call(toolbox);
|
|
412
|
+
if (selected) original.scrollToId.call(toolbox, selected.id_);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
toolbox.showAll_ = () => {
|
|
417
|
+
applyCategoryOrder();
|
|
418
|
+
if (!horizontal) {
|
|
419
|
+
original.showAll.call(toolbox);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const category = categoryById(openId);
|
|
423
|
+
if (category) showCategory(category, flyout.getScrollPos());
|
|
424
|
+
else close();
|
|
425
|
+
};
|
|
426
|
+
// The toolbox as the GUI gave it, so the palette can be drawn again when a sprite's groups change.
|
|
427
|
+
let lastTree: Element | null = null;
|
|
428
|
+
const visibleNow = () => {
|
|
429
|
+
const target = paletteVM?.editingTarget;
|
|
430
|
+
return target && paletteStore ? paletteStore.visible(target, extensionIds(paletteVM)) : null;
|
|
431
|
+
};
|
|
432
|
+
const redraw = () => {
|
|
433
|
+
if (lastTree) workspace.updateToolbox(lastTree.cloneNode(true));
|
|
434
|
+
};
|
|
435
|
+
paletteRefreshers.add(redraw);
|
|
436
|
+
const openGroupSettings = (target = paletteVM?.editingTarget, newSprite = false) => {
|
|
437
|
+
const vm = paletteVM;
|
|
438
|
+
const store = paletteStore;
|
|
439
|
+
if (!vm || !store || !target) return;
|
|
440
|
+
const catalog = catalogOf(vm, getLocale());
|
|
441
|
+
const ids = catalog.groups.map(group => group.id);
|
|
442
|
+
openPaletteVisibilityDialog(doc, {
|
|
443
|
+
locale: getLocale(),
|
|
444
|
+
targetName: target.getName(),
|
|
445
|
+
isStage: target.isStage,
|
|
446
|
+
newSprite,
|
|
447
|
+
// A second or later sprite can copy the groups one already made shows.
|
|
448
|
+
copySources: newSprite ? originalTargets(vm).filter(other => !other.isStage && other !== target)
|
|
449
|
+
.map(other => ({name: other.getName(), groups: [...store.visible(other, ids)]})) : undefined,
|
|
450
|
+
initial: store.visible(target, ids),
|
|
451
|
+
used: usedGroups(target, catalog),
|
|
452
|
+
extensions: (vm.runtime._blockInfo ?? []).map(info => {
|
|
453
|
+
const groups = catalog.groups.filter(group => group.extensionId === info.id);
|
|
454
|
+
return {
|
|
455
|
+
id: info.id,
|
|
456
|
+
metadata: {name: info.name || info.id, banner: info.menuIconURI || info.blockIconURI},
|
|
457
|
+
groups: groups.some(group => group.declared) ?
|
|
458
|
+
groups.map(group => ({id: group.id, name: group.name, color: group.color1})) : undefined
|
|
459
|
+
};
|
|
460
|
+
}),
|
|
461
|
+
sets: paletteGroupSets(getLocale(), store.sets()),
|
|
462
|
+
onApply({groups, addedSets}) {
|
|
463
|
+
store.addSets(addedSets);
|
|
464
|
+
store.set(target, groups, ids);
|
|
465
|
+
vm.runtime.emitProjectChanged();
|
|
466
|
+
redraw();
|
|
467
|
+
},
|
|
468
|
+
onCancel() {}
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
const askNewSprite = (target: any) => openGroupSettings(target, true);
|
|
472
|
+
newSpritePrompts.push(askNewSprite);
|
|
473
|
+
const openExtensionLibrary = () => openHostLibrary(doc);
|
|
474
|
+
const paletteMenu = attachPaletteMenuButton(menu, getLocale, locale => [
|
|
475
|
+
{label: paletteVisibilityMenuLabel(locale), onSelect: () => openGroupSettings()},
|
|
476
|
+
{label: /^ja(?:-|$)/i.test(locale) ? '機能拡張を追加' : 'Add Extension', separated: true, onSelect: openExtensionLibrary}
|
|
477
|
+
]);
|
|
478
|
+
|
|
479
|
+
// The GUI selects an extension by its ID once it is added; one shown as a group selects its first shown group.
|
|
480
|
+
const selectCategoryById = toolbox.setSelectedCategoryById;
|
|
481
|
+
toolbox.setSelectedCategoryById = function (id: string) {
|
|
482
|
+
return selectCategoryById.call(this, groups.selectionTarget(id));
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
toolbox.populate_ = tree => {
|
|
486
|
+
if (reorder) reorder.cancel();
|
|
487
|
+
if (tree) {
|
|
488
|
+
lastTree = tree.cloneNode(true) as Element;
|
|
489
|
+
const found = groups.prepare(tree,
|
|
490
|
+
{catalog: paletteVM ? catalogOf(paletteVM, getLocale()) : undefined, visible: visibleNow()});
|
|
491
|
+
original.populate.call(toolbox, tree);
|
|
492
|
+
groups.update(found);
|
|
493
|
+
} else {
|
|
494
|
+
original.populate.call(toolbox, tree);
|
|
495
|
+
}
|
|
496
|
+
paletteMenu.update();
|
|
497
|
+
if (!horizontal) return;
|
|
498
|
+
const category = categoryById(openId);
|
|
499
|
+
if (category) toolbox.setSelectedItem(category, false);
|
|
500
|
+
updateCategoryButtons();
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
const getBounds = () => {
|
|
504
|
+
const surfaceRect = surface.getBoundingClientRect();
|
|
505
|
+
const menuRect = menu.getBoundingClientRect();
|
|
506
|
+
const viewport = surface.ownerDocument.documentElement;
|
|
507
|
+
return {
|
|
508
|
+
surfaceLeft: surfaceRect.left,
|
|
509
|
+
left: Math.max(0, menuRect.left - surfaceRect.left, -surfaceRect.left),
|
|
510
|
+
right: Math.min(surface.clientWidth, menuRect.right - surfaceRect.left,
|
|
511
|
+
viewport.clientWidth - surfaceRect.left),
|
|
512
|
+
top: Math.max(menu.offsetHeight, -surfaceRect.top),
|
|
513
|
+
bottom: Math.min(surface.clientHeight, viewport.clientHeight - surfaceRect.top) - 4
|
|
514
|
+
};
|
|
515
|
+
};
|
|
516
|
+
const getWidth = ({left, right}) => {
|
|
517
|
+
const width = resizer.size?.width ?? original.getFlyoutWidth.call(flyout);
|
|
518
|
+
return Math.max(1, Math.min(width, right - left - 8));
|
|
519
|
+
};
|
|
520
|
+
flyout.getWidth = () => horizontal ? getWidth(getBounds()) : original.getFlyoutWidth.call(flyout);
|
|
521
|
+
|
|
522
|
+
flyout.position = () => {
|
|
523
|
+
if (!horizontal) {
|
|
524
|
+
original.position.call(flyout);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const category = categoryById(openId);
|
|
528
|
+
if (!category || !flyout.isVisible()) return;
|
|
529
|
+
|
|
530
|
+
// Keep the existing vertical flyout and scrollbar. Only their viewport and origin change.
|
|
531
|
+
const bounds = getBounds();
|
|
532
|
+
const {top} = bounds;
|
|
533
|
+
const width = getWidth(bounds);
|
|
534
|
+
const requestedHeight = resizer.size?.height ?? Math.min(360, surface.clientHeight * 0.65);
|
|
535
|
+
const height = Math.max(0, Math.min(requestedHeight, bounds.bottom - top));
|
|
536
|
+
const bodyHeight = height - paletteSearchHeight;
|
|
537
|
+
const visible = width > flyout.SCROLLBAR_PADDING && bodyHeight > 2 * flyout.SCROLLBAR_PADDING;
|
|
538
|
+
panel.style.visibility = scrollbar.style.visibility = visible ? '' : 'hidden';
|
|
539
|
+
if (!visible) {
|
|
540
|
+
resizer.place(null);
|
|
541
|
+
search.place(null);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// A category in a group is placed under the group's icon, since its own is hidden.
|
|
546
|
+
const anchor = groups.anchorOf(category).getBoundingClientRect();
|
|
547
|
+
const preferredLeft = resizer.left ?? (workspace.RTL ? anchor.right - width : anchor.left) - bounds.surfaceLeft;
|
|
548
|
+
const left = Math.min(Math.max(bounds.left, preferredLeft), bounds.right - width);
|
|
549
|
+
|
|
550
|
+
flyout.width_ = width;
|
|
551
|
+
flyout.height_ = bodyHeight;
|
|
552
|
+
// Scratch Blocks positions RTL blocks against the width used when laying them out.
|
|
553
|
+
if (flyout.RTL) flyout.workspace_.scrollX = width - layoutWidth;
|
|
554
|
+
flyout.setBackgroundPath_(width, height);
|
|
555
|
+
panel.setAttribute('width', width);
|
|
556
|
+
panel.setAttribute('height', height);
|
|
557
|
+
panel.style.transform = `translate(${left}px, ${top}px)`;
|
|
558
|
+
flyout.scrollbar_.setOrigin(left, top + paletteSearchHeight);
|
|
559
|
+
flyout.scrollbar_.resize();
|
|
560
|
+
resizer.place({left, top, width, height});
|
|
561
|
+
search.place({left, top, width});
|
|
562
|
+
panel.style.opacity = '1';
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const reposition = () => {
|
|
566
|
+
if (openId !== null) flyout.position();
|
|
567
|
+
};
|
|
568
|
+
const resizeObserver = new win.ResizeObserver(() => {
|
|
569
|
+
syncInputDocument();
|
|
570
|
+
reposition();
|
|
571
|
+
});
|
|
572
|
+
resizeObserver.observe(surface);
|
|
573
|
+
resizeObserver.observe(menu);
|
|
574
|
+
win.addEventListener('resize', reposition, {signal: events.signal});
|
|
575
|
+
|
|
576
|
+
flyout.isDragTowardWorkspace = delta => {
|
|
577
|
+
if (!horizontal) return original.isDragTowardWorkspace.call(flyout, delta);
|
|
578
|
+
// Leave vertical swipes available to scroll the block list on touch screens.
|
|
579
|
+
const touch = workspace.currentGesture_?.mostRecentEvent_?.type.startsWith('touch');
|
|
580
|
+
return Math.abs(delta.x) > Math.abs(delta.y) / 2 || (!touch && delta.y > 0);
|
|
581
|
+
};
|
|
582
|
+
closeOnDragExit.set(flyout, ({clientX, clientY}) => {
|
|
583
|
+
if (openId === null) return;
|
|
584
|
+
const {left, right, top, bottom} = panel.getBoundingClientRect();
|
|
585
|
+
if (clientX < left || clientX > right || clientY < top || clientY > bottom) closeIfUnpinned();
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
const setMode = next => {
|
|
589
|
+
if (next === horizontal) return;
|
|
590
|
+
if (reorder) reorder.cancel();
|
|
591
|
+
const category = toolbox.getSelectedItem();
|
|
592
|
+
workspace.cancelCurrentGesture();
|
|
593
|
+
groups.close();
|
|
594
|
+
close();
|
|
595
|
+
flyout.scrollTarget = null;
|
|
596
|
+
horizontal = next;
|
|
597
|
+
workspace.toolboxPosition = horizontal ? ScratchBlocks.TOOLBOX_AT_TOP : verticalPosition;
|
|
598
|
+
menu.classList.toggle(styles.horizontal, horizontal);
|
|
599
|
+
menu.toggleAttribute('data-horizontal-block-palette', horizontal);
|
|
600
|
+
panel.classList.toggle(styles.panel, horizontal);
|
|
601
|
+
scrollbar.classList.toggle(styles.scrollbar, horizontal);
|
|
602
|
+
if (horizontal) body.setAttribute('transform', `translate(0 ${paletteSearchHeight}) ${bodyTransform ?? ''}`);
|
|
603
|
+
else if (bodyTransform === null) body.removeAttribute('transform');
|
|
604
|
+
else body.setAttribute('transform', bodyTransform);
|
|
605
|
+
updateCategoryButtons();
|
|
606
|
+
if (horizontal) flyout.hide();
|
|
607
|
+
else {
|
|
608
|
+
panel.style.visibility = scrollbar.style.visibility = '';
|
|
609
|
+
if (flyout.RTL) flyout.workspace_.scrollX = 0;
|
|
610
|
+
toolbox.showAll_();
|
|
611
|
+
if (category) toolbox.setSelectedItem(category);
|
|
612
|
+
}
|
|
613
|
+
workspace.resize();
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
const showContextMenu = event => {
|
|
617
|
+
event.preventDefault();
|
|
618
|
+
event.stopPropagation();
|
|
619
|
+
const japanese = doc.documentElement.lang.toLowerCase().startsWith('ja');
|
|
620
|
+
const text = horizontal ?
|
|
621
|
+
(japanese ? 'パレットを縦表示にする' : 'Use vertical palette') :
|
|
622
|
+
(japanese ? 'パレットを横表示にする' : 'Use horizontal palette');
|
|
623
|
+
// The addon wrapper for ContextMenu.show expects an active Blockly gesture.
|
|
624
|
+
const popup = ContextMenu.populate_([{
|
|
625
|
+
text,
|
|
626
|
+
twPlusMenuIcon: horizontal ? 'paletteVertical' : 'paletteHorizontal',
|
|
627
|
+
enabled: true,
|
|
628
|
+
callback: () => {
|
|
629
|
+
WidgetDiv.hideIfOwner(toolbox);
|
|
630
|
+
setMode(!horizontal);
|
|
631
|
+
}
|
|
632
|
+
}], workspace.RTL);
|
|
633
|
+
WidgetDiv.show(toolbox, workspace.RTL, () => popup.dispose());
|
|
634
|
+
ContextMenu.position_(popup, event, workspace.RTL);
|
|
635
|
+
};
|
|
636
|
+
if (allowManualToggle) {
|
|
637
|
+
menu.addEventListener('contextmenu', showContextMenu, capture);
|
|
638
|
+
panel.addEventListener('contextmenu', event => {
|
|
639
|
+
// Keep each block's own context menu.
|
|
640
|
+
if (event.target === panel || event.target === flyout.svgBackground_) showContextMenu(event);
|
|
641
|
+
}, capture);
|
|
642
|
+
}
|
|
643
|
+
menu.addEventListener('mouseup', event => {
|
|
644
|
+
if (allowManualToggle && (event.button === 2 || (event.button === 0 && event.ctrlKey))) event.stopPropagation();
|
|
645
|
+
}, capture);
|
|
646
|
+
menu.addEventListener('keydown', event => {
|
|
647
|
+
if (!horizontal || !['Enter', ' '].includes(event.key)) return;
|
|
648
|
+
const category = toolbox.categoryMenu_.categories_.find(item => item.item_ === event.target);
|
|
649
|
+
if (!category) return;
|
|
650
|
+
event.preventDefault();
|
|
651
|
+
if (groups.isGroup(category.id_)) groups.toggle(category, true);
|
|
652
|
+
else if (openId === category.id_) close();
|
|
653
|
+
else open(category);
|
|
654
|
+
}, capture);
|
|
655
|
+
menu.addEventListener('scroll', () => {
|
|
656
|
+
resizer.followAnchor();
|
|
657
|
+
if (horizontal) flyout.position();
|
|
658
|
+
}, {signal: events.signal});
|
|
659
|
+
menu.addEventListener('wheel', event => {
|
|
660
|
+
if (!horizontal || event.ctrlKey || event.deltaX || !event.deltaY || menu.scrollWidth <= menu.clientWidth) return;
|
|
661
|
+
event.preventDefault();
|
|
662
|
+
menu.scrollLeft += event.deltaY * (event.deltaMode === 1 ? 16 : 1);
|
|
663
|
+
}, {passive: false, signal: events.signal});
|
|
664
|
+
const onPointerDown = event => {
|
|
665
|
+
if (WidgetDiv.DIV?.contains(event.target)) return;
|
|
666
|
+
WidgetDiv.hideIfOwner(toolbox);
|
|
667
|
+
if (openId === null || groups.contains(event.target) || resizer.contains(event.target) || search.contains(event.target) || [menu, panel, scrollbar, DropDownDiv.DIV]
|
|
668
|
+
.some(element => element?.contains(event.target))) return;
|
|
669
|
+
closeIfUnpinned();
|
|
670
|
+
};
|
|
671
|
+
const onKeyDown = event => {
|
|
672
|
+
if (event.key !== 'Escape' || event.isComposing || groups.isOpen()) return;
|
|
673
|
+
if (WidgetDiv.owner_ === toolbox) WidgetDiv.hideIfOwner(toolbox);
|
|
674
|
+
else if (!WidgetDiv.isVisible() && !DropDownDiv.isVisible()) close();
|
|
675
|
+
};
|
|
676
|
+
const onWorkspaceKeyDown = event => {
|
|
677
|
+
if (surface.contains(event.target)) ScratchBlocks.onKeyDown_(event);
|
|
678
|
+
};
|
|
679
|
+
let inputDocument: Document;
|
|
680
|
+
function syncInputDocument() {
|
|
681
|
+
const current = surface.ownerDocument;
|
|
682
|
+
if (current === inputDocument) return;
|
|
683
|
+
if (inputDocument) workspace.cancelCurrentGesture();
|
|
684
|
+
moveBlockWidgets(ScratchBlocks, current);
|
|
685
|
+
inputDocument?.removeEventListener('pointerdown', onPointerDown, true);
|
|
686
|
+
inputDocument?.removeEventListener('keydown', onKeyDown, true);
|
|
687
|
+
inputDocument?.removeEventListener('keydown', onWorkspaceKeyDown);
|
|
688
|
+
inputDocument = current;
|
|
689
|
+
inputDocument.addEventListener('pointerdown', onPointerDown, capture);
|
|
690
|
+
inputDocument.addEventListener('keydown', onKeyDown, capture);
|
|
691
|
+
if (current !== doc) inputDocument.addEventListener('keydown', onWorkspaceKeyDown, {signal: events.signal});
|
|
692
|
+
}
|
|
693
|
+
syncInputDocument();
|
|
694
|
+
const disconnectPalette = connectDetachedBlockPalette({
|
|
695
|
+
blocks: ScratchBlocks, workspace, flyout, menu,
|
|
696
|
+
categories: () => toolbox.categoryMenu_.categories_.map(({id_, name_, item_}) => ({
|
|
697
|
+
id: id_,
|
|
698
|
+
label: ScratchBlocks.utils.replaceMessageReferences(name_),
|
|
699
|
+
color: item_.querySelector('.scratchCategoryItemBubble')?.style.backgroundColor,
|
|
700
|
+
image: item_.querySelector('.scratchCategoryItemIcon')?.style.backgroundImage
|
|
701
|
+
})),
|
|
702
|
+
grouping: () => ({groups: groups.current(), order: paletteOrder()}),
|
|
703
|
+
show(id) {
|
|
704
|
+
const category = categoryById(id);
|
|
705
|
+
if (category) showCategory(category, 0);
|
|
706
|
+
},
|
|
707
|
+
setDetached(value) {
|
|
708
|
+
close();
|
|
709
|
+
detached = value;
|
|
710
|
+
for (const element of [menu, panel, scrollbar]) element.toggleAttribute('data-detached-palette-source', value);
|
|
711
|
+
if (!value) flyout.hide();
|
|
712
|
+
workspace.resize();
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
toolbox.dispose = () => {
|
|
716
|
+
paletteRefreshers.delete(redraw);
|
|
717
|
+
newSpritePrompts.splice(newSpritePrompts.indexOf(askNewSprite) >>> 0, 1);
|
|
718
|
+
paletteMenu.dispose();
|
|
719
|
+
groups.dispose();
|
|
720
|
+
disconnectPalette();
|
|
721
|
+
if (inputDocument !== doc) moveBlockWidgets(ScratchBlocks, doc);
|
|
722
|
+
palettes.delete(setMode);
|
|
723
|
+
closeOnDragExit.delete(flyout);
|
|
724
|
+
resizeObserver.disconnect();
|
|
725
|
+
resizer.dispose();
|
|
726
|
+
search.dispose();
|
|
727
|
+
if (reorder) reorder.dispose();
|
|
728
|
+
events.abort();
|
|
729
|
+
WidgetDiv.hideIfOwner(toolbox);
|
|
730
|
+
original.dispose.call(toolbox);
|
|
731
|
+
};
|
|
732
|
+
// Scratch Blocks drew the first toolbox before the palette was attached, dropping the categories nested in groups.
|
|
733
|
+
const firstTree = workspace.options.languageTree;
|
|
734
|
+
if (hasCategoryGroups(firstTree)) toolbox.populate_(firstTree.cloneNode(true));
|
|
735
|
+
palettes.add(setMode);
|
|
736
|
+
setMode(horizontalLayout);
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
const installHorizontalBlockPalette = (ScratchBlocks,
|
|
740
|
+
{allowManualToggle = false, allowCategoryReorder = false} = {}) => {
|
|
741
|
+
const toolbox = ScratchBlocks.Toolbox?.prototype;
|
|
742
|
+
if (!toolbox?.init || installed.has(toolbox)) return;
|
|
743
|
+
installed.add(toolbox);
|
|
744
|
+
installGestureOwnerDocument(ScratchBlocks);
|
|
745
|
+
|
|
746
|
+
const gesture = ScratchBlocks.Gesture.prototype;
|
|
747
|
+
const {handleMove} = gesture;
|
|
748
|
+
gesture.handleMove = function (...args) {
|
|
749
|
+
const result = handleMove.apply(this, args);
|
|
750
|
+
if (this.isDraggingBlock_ && this.mostRecentEvent_) {
|
|
751
|
+
closeOnDragExit.get(this.flyout_)?.(this.mostRecentEvent_);
|
|
752
|
+
}
|
|
753
|
+
return result;
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
const {init} = toolbox;
|
|
757
|
+
toolbox.init = function (...args) {
|
|
758
|
+
const result = init.apply(this, args);
|
|
759
|
+
attach(ScratchBlocks, this, {allowManualToggle, allowCategoryReorder});
|
|
760
|
+
return result;
|
|
761
|
+
};
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
return {connectPaletteVisibility, setHorizontalBlockPalette, openDetachedBlockPalette, installHorizontalBlockPalette};
|
|
765
|
+
}
|