@kubohiroya/turbowarp-plus-editor-code-views 0.9.0 → 0.11.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 +29 -5
- package/README.md +29 -5
- package/dist/palette-category-visibility.d.ts +76 -8
- package/dist/palette-category-visibility.js +158 -12
- package/dist/palette-visibility-dialog.d.ts +28 -6
- package/dist/palette-visibility-dialog.js +74 -43
- package/package.json +1 -1
- package/src/palette-category-visibility.ts +201 -13
- package/src/palette-visibility-dialog.tsx +122 -61
- package/src/styles.css +49 -1
package/README.ja.md
CHANGED
|
@@ -60,14 +60,38 @@ MPL-2.0。turbowarp-plus-editor の `packages/editor-views` と `packages/editor
|
|
|
60
60
|
|
|
61
61
|
| 関数 | 内容 |
|
|
62
62
|
| --- | --- |
|
|
63
|
-
| `createPaletteVisibilityStore()` | 1つの作品の設定を、実行時の対象の ID ごとに持つ。名前を変えたスプライトも設定を保つ。`visible`・`set`・`copy`(複製)・`sets`・`addSets`、画面状態のスライスへの `capture(targets)` と `apply(value, targets)`、`reset` |
|
|
63
|
+
| `createPaletteVisibilityStore({catalog})` | 1つの作品の設定を、実行時の対象の ID ごとに持つ。名前を変えたスプライトも設定を保つ。`visible`・`set`・`copy`(複製)・`sets`・`addSets`、画面状態のスライスへの `capture(targets)` と `apply(value, targets)`、`reset` |
|
|
64
64
|
| `paletteScreenStateKey` | 画面状態のスライスの名前(`blockPalette`) |
|
|
65
|
-
| `filterToolboxTree(tree, visible)` |
|
|
66
|
-
| `
|
|
65
|
+
| `filterToolboxTree(tree, visible, catalog)` | パレットを描く前のツールボックスの木から、出さないカテゴリを外す。カタログを渡すと、グループを宣言した機能拡張を先にグループごとのカテゴリに分ける(`splitExtensionCategories`) |
|
|
66
|
+
| `installExtensionPaletteGroups(runtime)`・`extensionPaletteCatalog(runtime._blockInfo)` | 機能拡張が宣言したグループを VM のカテゴリ情報に残し、読み込み済みの機能拡張のグループと、各ブロックの属するグループを一覧にする |
|
|
67
|
+
| `usedGroups(target, catalog)`・`groupOfOpcode(opcode, catalog)`・`defaultVisibleGroups(target, extensionGroups, catalog)` | コードが使っているグループとその数、設定のない対象に出すグループ |
|
|
67
68
|
| `paletteGroupSets(locale, projectSets)` | 「標準」「表現」「変数・リスト」「基本の制御」と、作品のセット |
|
|
68
69
|
| `attachPaletteMenuButton(menu, getLocale, getItems)` | カテゴリの一覧の末尾(横配置では右端、縦配置では下端)に ▼ のボタンを置く。押すと、ホストが渡す項目のメニューを出す。項目は「ブロックグループの表示・非表示の設定」(`paletteVisibilityMenuLabel`)と、ホストの機能拡張に関する項目 |
|
|
69
70
|
|
|
70
|
-
`./palette-visibility-dialog` の `openPaletteVisibilityDialog(document, props)` は「パレット内のブロックグループの表示・非表示の設定」の画面を開く。機能拡張の各行は `@kubohiroya/turbowarp-extension-security-dialog/react` の `ExtensionRow`
|
|
71
|
+
`./palette-visibility-dialog` の `openPaletteVisibilityDialog(document, props)` は「パレット内のブロックグループの表示・非表示の設定」の画面を開く。機能拡張の各行は `@kubohiroya/turbowarp-extension-security-dialog/react` の `ExtensionRow` で描くので、拡張の追加の画面と同じ見た目になる。画面は、「まとめて選ぶ」の欄に「設定のショートカット」(「標準」「表現」などのセット)と「設定のコピー元」を左右に並べ、その下に標準と機能拡張のブロックグループのチェックを並べる。ショートカットやコピー元にポインターを重ねるかフォーカスすると、それが選ぶグループのチェックが、そのチェックにポインターを重ねたときと同じ背景になる。「確定」を押すと、出すグループと、この画面で足したショートカットを `onApply` に渡して閉じる。`newSprite` を渡すと、追加したばかりのスプライトについて尋ねる画面になり、見出しの下に「新しいスプライト「名前」」と出す。あわせて `copySources`(作成済みのスプライトの名前と、それぞれが表示中のグループ)を渡すと、「設定のコピー元」に選ぶ一覧と「コピー」を出す。一覧は「スプライトを選ぶ」から始まり、スプライトを選んでいる間は、それが選ぶグループのチェックを強調したままにする。「コピー」を押すと、選んだスプライトのグループのうちこのスプライトに出せるものを、画面のチェックに写す。画面は閉じないので、利用者はチェックを足したり外したりしてから「確定」を押す。
|
|
71
72
|
|
|
72
|
-
ホストは `@kubohiroya/turbowarp-extension-security-dialog`(0.9.0 以上)を入れ、その `styles.css` も読み込む。設定を画面状態のスライスとして登録するのはホストの役目である。
|
|
73
|
+
ホストは `@kubohiroya/turbowarp-extension-security-dialog`(0.9.0 以上)を入れ、その `styles.css` も読み込む。設定を画面状態のスライスとして登録するのはホストの役目である。
|
|
74
|
+
|
|
75
|
+
### 1つの機能拡張の中のグループ
|
|
76
|
+
|
|
77
|
+
機能拡張は、ブロックをいくつかのグループに分け、グループごとに表示・非表示を選べるようにできる。`getInfo()` にグループを宣言し、各ブロックに `paletteGroup` を付ける。
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
getInfo() {
|
|
81
|
+
return {
|
|
82
|
+
id: 'shapes', name: 'Shapes', color1: '#0fbd8c',
|
|
83
|
+
groups: [{id: 'geometry', name: '図形', color1: '#ff8c1a', color2: '#db6e00', color3: '#c26200'}, {id: 'text', name: '文字'}],
|
|
84
|
+
blocks: [
|
|
85
|
+
{opcode: 'circle', blockType: 'command', text: 'circle', paletteGroup: 'geometry'},
|
|
86
|
+
{opcode: 'write', blockType: 'command', text: 'write [TEXT]', paletteGroup: 'text', arguments: {/* ... */}},
|
|
87
|
+
{opcode: 'reset', blockType: 'command', text: 'reset'}
|
|
88
|
+
]
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- 各グループはパレットのカテゴリ `shapes/geometry` になり、名前はグループの名前になる。設定もこの ID で記録する。`paletteGroup` のないブロック(宣言していないグループを指すものを含む)は、カテゴリ `shapes` に残る。
|
|
94
|
+
- グループの `color1`・`color2`・`color3`・`menuIconURI` は省略でき、省略すると機能拡張のものを使う。ブロック自身の色はグループの色より優先する。
|
|
95
|
+
- 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
|
|
96
|
+
- ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
|
|
73
97
|
|
package/README.md
CHANGED
|
@@ -60,14 +60,38 @@ Each sprite and the stage can choose which block groups the palette shows: the s
|
|
|
60
60
|
|
|
61
61
|
| Function | What it does |
|
|
62
62
|
| --- | --- |
|
|
63
|
-
| `createPaletteVisibilityStore()` | One project's settings, by runtime target ID, so a renamed sprite keeps its setting: `visible`, `set`, `copy` (for a duplicate), `sets`, `addSets`, `capture(targets)` and `apply(value, targets)` for the screen-state slice, and `reset` |
|
|
63
|
+
| `createPaletteVisibilityStore({catalog})` | One project's settings, by runtime target ID, so a renamed sprite keeps its setting: `visible`, `set`, `copy` (for a duplicate), `sets`, `addSets`, `capture(targets)` and `apply(value, targets)` for the screen-state slice, and `reset` |
|
|
64
64
|
| `paletteScreenStateKey` | The name of the screen-state slice (`blockPalette`) |
|
|
65
|
-
| `filterToolboxTree(tree, visible)` | Removes the categories a target does not show from the parsed toolbox before the palette is drawn |
|
|
66
|
-
| `
|
|
65
|
+
| `filterToolboxTree(tree, visible, catalog)` | Removes the categories a target does not show from the parsed toolbox before the palette is drawn. With a catalog, an extension that declares groups is first split into one category per group (`splitExtensionCategories`) |
|
|
66
|
+
| `installExtensionPaletteGroups(runtime)`, `extensionPaletteCatalog(runtime._blockInfo)` | Keep the groups each extension declares on the VM's category info, and list the groups of the loaded extensions with the group of each block |
|
|
67
|
+
| `usedGroups(target, catalog)`, `groupOfOpcode(opcode, catalog)`, `defaultVisibleGroups(target, extensionGroups, catalog)` | The groups the code uses and how many blocks of each, and the groups shown for a target without a setting |
|
|
67
68
|
| `paletteGroupSets(locale, projectSets)` | Standard, Expression, Variables and lists, Basic control, and the project's own sets |
|
|
68
69
|
| `attachPaletteMenuButton(menu, getLocale, getItems)` | Puts a ▼ button at the end of the category menu (the right end of a horizontal palette, the bottom of a vertical one). Pressing it lists the items the host gives: the group settings (`paletteVisibilityMenuLabel`) and the host's extension items |
|
|
69
70
|
|
|
70
|
-
`openPaletteVisibilityDialog(document, props)` from `./palette-visibility-dialog` opens the screen that chooses the groups. Each extension is drawn with `ExtensionRow` from `@kubohiroya/turbowarp-extension-security-dialog/react`, so it looks the same as in the screen that adds extensions.
|
|
71
|
+
`openPaletteVisibilityDialog(document, props)` from `./palette-visibility-dialog` opens the screen that chooses the groups. Each extension is drawn with `ExtensionRow` from `@kubohiroya/turbowarp-extension-security-dialog/react`, so it looks the same as in the screen that adds extensions. The screen puts “Setting shortcuts” (sets such as Standard and Expression) and “Copy settings from” side by side under “Choose several at once”, above the checkboxes of the standard and extension block groups. Pointing at or focusing a shortcut or the copy source gives the checkboxes it would choose the same background as pointing at them. Confirm passes the groups to show, and the shortcuts added in the screen, to `onApply` and closes. With `newSprite`, the screen asks about a sprite just added and names it as a new sprite. Given `copySources` too (the sprites already in the project, each with the groups it shows), “Copy settings from” offers a list to choose one, starting at “Choose a sprite”, and “Copy”. While a sprite is chosen, the checkboxes it would choose stay marked. “Copy” checks that sprite's groups, as far as this sprite is offered them. The screen stays open; the person adds or removes checks and presses Confirm.
|
|
71
72
|
|
|
72
|
-
The host installs `@kubohiroya/turbowarp-extension-security-dialog` (0.9.0 or later) and loads its `styles.css`, and registers the store as a screen-state slice.
|
|
73
|
+
The host installs `@kubohiroya/turbowarp-extension-security-dialog` (0.9.0 or later) and loads its `styles.css`, and registers the store as a screen-state slice.
|
|
74
|
+
|
|
75
|
+
### Groups inside one extension
|
|
76
|
+
|
|
77
|
+
An extension can split its blocks into groups that are shown and hidden on their own. It declares them in `getInfo()` and marks each block with `paletteGroup`:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
getInfo() {
|
|
81
|
+
return {
|
|
82
|
+
id: 'shapes', name: 'Shapes', color1: '#0fbd8c',
|
|
83
|
+
groups: [{id: 'geometry', name: 'Geometry', color1: '#ff8c1a', color2: '#db6e00', color3: '#c26200'}, {id: 'text', name: 'Text'}],
|
|
84
|
+
blocks: [
|
|
85
|
+
{opcode: 'circle', blockType: 'command', text: 'circle', paletteGroup: 'geometry'},
|
|
86
|
+
{opcode: 'write', blockType: 'command', text: 'write [TEXT]', paletteGroup: 'text', arguments: {/* ... */}},
|
|
87
|
+
{opcode: 'reset', blockType: 'command', text: 'reset'}
|
|
88
|
+
]
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- Each group is the palette category `shapes/geometry`, named after the group, and the settings name it by that ID. Blocks without `paletteGroup` (or naming a group not declared) stay in the category `shapes`.
|
|
94
|
+
- `color1`, `color2`, `color3` and `menuIconURI` of a group are optional, and default to the extension's. A block's own colour wins over its group's.
|
|
95
|
+
- A label, separator or button goes with the block after it. The opcodes stay `shapes_circle` and so on, so the SB3 does not change: standard TurboWarp and Scratch ignore `groups` and `paletteGroup` and show one category.
|
|
96
|
+
- The host calls `installExtensionPaletteGroups(vm.runtime)` before extensions load, and passes `extensionPaletteCatalog(vm.runtime._blockInfo)` to the store, `filterToolboxTree` and `usedGroups`. The dialog takes the groups of such an extension as `groups` of its entry and shows a checkbox for each inside the extension's row.
|
|
73
97
|
|
|
@@ -20,8 +20,61 @@ export interface PaletteGroupSet {
|
|
|
20
20
|
}
|
|
21
21
|
/** The sets every project has, then the project's own sets. */
|
|
22
22
|
export declare function paletteGroupSets(locale?: string, projectSets?: Record<string, SavedGroupSet>): PaletteGroupSet[];
|
|
23
|
+
/**
|
|
24
|
+
* One block group an extension offers. An extension that declares no groups offers one, named and identified like
|
|
25
|
+
* the extension. One that declares `groups: [{id, name, color1?, color2?, color3?, menuIconURI?}]` in `getInfo()` and
|
|
26
|
+
* marks blocks with `paletteGroup` offers each group as `extensionId/groupId`, and its unmarked blocks as one more
|
|
27
|
+
* group with the extension's ID. Standard TurboWarp ignores both fields and shows the extension as one category.
|
|
28
|
+
*/
|
|
29
|
+
export interface ExtensionPaletteGroup {
|
|
30
|
+
id: string;
|
|
31
|
+
extensionId: string;
|
|
32
|
+
/** The group's own name; for the unmarked blocks, the extension's name. */
|
|
33
|
+
name: string;
|
|
34
|
+
extensionName: string;
|
|
35
|
+
/** Whether this is a group the extension declares, rather than its unmarked blocks. */
|
|
36
|
+
declared: boolean;
|
|
37
|
+
color1?: string;
|
|
38
|
+
color2?: string;
|
|
39
|
+
menuIconURI?: string;
|
|
40
|
+
}
|
|
41
|
+
/** The extension groups the project's extensions offer, and which group each of their blocks belongs to. */
|
|
42
|
+
export interface PaletteGroupCatalog {
|
|
43
|
+
groups: readonly ExtensionPaletteGroup[];
|
|
44
|
+
/** Block opcode to group ID, for the blocks of declared groups. */
|
|
45
|
+
groupOf: ReadonlyMap<string, string>;
|
|
46
|
+
}
|
|
47
|
+
/** The parts of the VM's category info (`runtime._blockInfo`) this module reads. */
|
|
48
|
+
export interface PaletteCategoryInfo {
|
|
49
|
+
id: string;
|
|
50
|
+
name?: string;
|
|
51
|
+
color1?: string;
|
|
52
|
+
color2?: string;
|
|
53
|
+
menuIconURI?: string;
|
|
54
|
+
blockIconURI?: string;
|
|
55
|
+
/** The groups the extension declares, kept by {@link installExtensionPaletteGroups}. */
|
|
56
|
+
paletteGroups?: unknown;
|
|
57
|
+
blocks?: readonly {
|
|
58
|
+
info?: {
|
|
59
|
+
paletteGroup?: unknown;
|
|
60
|
+
};
|
|
61
|
+
json?: {
|
|
62
|
+
type?: unknown;
|
|
63
|
+
} | null;
|
|
64
|
+
}[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Keeps the groups each extension declares on its category info, where {@link extensionPaletteCatalog} reads them,
|
|
68
|
+
* and draws the blocks of a group with a colour of its own in the group's colours. Install it before extensions load.
|
|
69
|
+
* Returns the function that removes it.
|
|
70
|
+
*/
|
|
71
|
+
export declare function installExtensionPaletteGroups(runtime: {
|
|
72
|
+
_fillExtensionCategory?: (...args: any[]) => unknown;
|
|
73
|
+
}): () => void;
|
|
74
|
+
/** The groups of the loaded extensions, in palette order, from the VM's category info. */
|
|
75
|
+
export declare function extensionPaletteCatalog(categories?: readonly PaletteCategoryInfo[]): PaletteGroupCatalog;
|
|
23
76
|
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
24
|
-
export declare function groupOfOpcode(opcode: string): string | null;
|
|
77
|
+
export declare function groupOfOpcode(opcode: string, catalog?: PaletteGroupCatalog): string | null;
|
|
25
78
|
type BlockLike = {
|
|
26
79
|
opcode?: string;
|
|
27
80
|
shadow?: boolean;
|
|
@@ -36,14 +89,23 @@ export interface PaletteTarget {
|
|
|
36
89
|
};
|
|
37
90
|
}
|
|
38
91
|
/** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
|
|
39
|
-
export declare function usedGroups(target: Pick<PaletteTarget, 'blocks'
|
|
92
|
+
export declare function usedGroups(target: Pick<PaletteTarget, 'blocks'>, catalog?: PaletteGroupCatalog): Map<string, number>;
|
|
40
93
|
/**
|
|
41
94
|
* The groups shown when a target has no setting of its own: every standard group, and the groups of
|
|
42
|
-
* the
|
|
95
|
+
* the extension groups its code already uses, so a project opens with nothing missing. `extensionGroups` are the
|
|
96
|
+
* IDs of the extension groups on offer: the extension IDs, or with a catalog, its group IDs.
|
|
97
|
+
*/
|
|
98
|
+
export declare function defaultVisibleGroups(target: Pick<PaletteTarget, 'blocks'>, extensionGroups: readonly string[], catalog?: PaletteGroupCatalog): string[];
|
|
99
|
+
/**
|
|
100
|
+
* Splits each extension category whose extension declares groups into one category per group, in the catalog's
|
|
101
|
+
* order. A label, separator or button goes with the block after it, or with the last block when none follows.
|
|
102
|
+
*/
|
|
103
|
+
export declare function splitExtensionCategories(tree: Element, catalog: PaletteGroupCatalog): void;
|
|
104
|
+
/**
|
|
105
|
+
* Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it.
|
|
106
|
+
* With a catalog, an extension that declares groups is first split into a category per group.
|
|
43
107
|
*/
|
|
44
|
-
export declare function
|
|
45
|
-
/** Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it. */
|
|
46
|
-
export declare function filterToolboxTree(tree: Element | null | undefined, visible: ReadonlySet<string>): void;
|
|
108
|
+
export declare function filterToolboxTree(tree: Element | null | undefined, visible: ReadonlySet<string>, catalog?: PaletteGroupCatalog): void;
|
|
47
109
|
/** The screen-state slice the settings are saved as. Sprites are named as the tab layout names them. */
|
|
48
110
|
export declare const paletteScreenStateKey = "blockPalette";
|
|
49
111
|
export interface SavedPaletteVisibility {
|
|
@@ -57,7 +119,10 @@ export interface SavedPaletteVisibility {
|
|
|
57
119
|
sets?: Record<string, SavedGroupSet>;
|
|
58
120
|
}
|
|
59
121
|
export interface PaletteVisibilityStore {
|
|
60
|
-
/**
|
|
122
|
+
/**
|
|
123
|
+
* The groups the palette shows for the target. `extensionIds` are the extension groups on offer: the extension
|
|
124
|
+
* IDs, or the group IDs of a catalog when the store was given one.
|
|
125
|
+
*/
|
|
61
126
|
visible(target: PaletteTarget, extensionIds: readonly string[]): Set<string>;
|
|
62
127
|
/** Keeps the target's groups. Groups equal to its default leave it without a setting of its own. */
|
|
63
128
|
set(target: PaletteTarget, groups: readonly string[], extensionIds: readonly string[]): void;
|
|
@@ -74,7 +139,10 @@ export interface PaletteVisibilityStore {
|
|
|
74
139
|
reset(): void;
|
|
75
140
|
}
|
|
76
141
|
/** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
|
|
77
|
-
export declare function createPaletteVisibilityStore(
|
|
142
|
+
export declare function createPaletteVisibilityStore({ catalog }?: {
|
|
143
|
+
/** The extension groups on offer now, so the default counts the blocks of declared groups. */
|
|
144
|
+
catalog?: () => PaletteGroupCatalog | undefined;
|
|
145
|
+
}): PaletteVisibilityStore;
|
|
78
146
|
/** The menu item that opens the group settings. */
|
|
79
147
|
export declare const paletteVisibilityMenuLabel: (locale: string) => "ブロックグループの表示・非表示の設定" | "Show or hide block groups";
|
|
80
148
|
export interface PaletteMenuItem {
|
|
@@ -33,8 +33,100 @@ const standardPrefixes = {
|
|
|
33
33
|
motion: 'motion', looks: 'looks', sound: 'sound', event: 'events', control: 'control', sensing: 'sensing',
|
|
34
34
|
operator: 'operators', procedures: 'myBlocks', argument: 'myBlocks'
|
|
35
35
|
};
|
|
36
|
+
const groupIdPattern = /^[A-Za-z0-9_-]+$/;
|
|
37
|
+
const optionalString = (value) => typeof value === 'string' && value ? value : undefined;
|
|
38
|
+
/** The groups `getInfo()` declares, leaving out any that are not well formed or repeat an ID. */
|
|
39
|
+
function declaredGroups(value) {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return [];
|
|
42
|
+
const groups = [];
|
|
43
|
+
for (const item of value) {
|
|
44
|
+
if (!item || typeof item !== 'object')
|
|
45
|
+
continue;
|
|
46
|
+
const { id, name } = item;
|
|
47
|
+
if (typeof id !== 'string' || !groupIdPattern.test(id) || groups.some(group => group.id === id))
|
|
48
|
+
continue;
|
|
49
|
+
const entry = item;
|
|
50
|
+
groups.push({ id, name: typeof name === 'string' && name ? name : id, color1: optionalString(entry.color1),
|
|
51
|
+
color2: optionalString(entry.color2), color3: optionalString(entry.color3),
|
|
52
|
+
menuIconURI: optionalString(entry.menuIconURI) });
|
|
53
|
+
}
|
|
54
|
+
return groups;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Keeps the groups each extension declares on its category info, where {@link extensionPaletteCatalog} reads them,
|
|
58
|
+
* and draws the blocks of a group with a colour of its own in the group's colours. Install it before extensions load.
|
|
59
|
+
* Returns the function that removes it.
|
|
60
|
+
*/
|
|
61
|
+
export function installExtensionPaletteGroups(runtime) {
|
|
62
|
+
const original = runtime._fillExtensionCategory;
|
|
63
|
+
if (typeof original !== 'function')
|
|
64
|
+
return () => { };
|
|
65
|
+
const wrapped = function (categoryInfo, extensionInfo, ...rest) {
|
|
66
|
+
const result = original.call(this, categoryInfo, extensionInfo, ...rest);
|
|
67
|
+
const groups = declaredGroups(extensionInfo?.groups);
|
|
68
|
+
if (categoryInfo && typeof categoryInfo === 'object') {
|
|
69
|
+
categoryInfo.paletteGroups = groups;
|
|
70
|
+
for (const block of Array.isArray(categoryInfo.blocks) ? categoryInfo.blocks : []) {
|
|
71
|
+
const group = groups.find(item => item.id === block?.info?.paletteGroup);
|
|
72
|
+
if (!group?.color1 || !block.json || block.info.color1 !== undefined)
|
|
73
|
+
continue;
|
|
74
|
+
block.json.colour = group.color1;
|
|
75
|
+
if (group.color2)
|
|
76
|
+
block.json.colourSecondary = group.color2;
|
|
77
|
+
if (group.color3)
|
|
78
|
+
block.json.colourTertiary = group.color3;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
runtime._fillExtensionCategory = wrapped;
|
|
84
|
+
return () => {
|
|
85
|
+
if (runtime._fillExtensionCategory === wrapped)
|
|
86
|
+
runtime._fillExtensionCategory = original;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** The groups of the loaded extensions, in palette order, from the VM's category info. */
|
|
90
|
+
export function extensionPaletteCatalog(categories = []) {
|
|
91
|
+
const groups = [];
|
|
92
|
+
const groupOf = new Map();
|
|
93
|
+
for (const category of categories) {
|
|
94
|
+
const extensionName = category.name || category.id;
|
|
95
|
+
const declared = declaredGroups(category.paletteGroups);
|
|
96
|
+
const used = new Set();
|
|
97
|
+
let unmarked = declared.length === 0;
|
|
98
|
+
for (const block of category.blocks ?? []) {
|
|
99
|
+
const opcode = block?.json?.type;
|
|
100
|
+
if (typeof opcode !== 'string')
|
|
101
|
+
continue;
|
|
102
|
+
const group = declared.find(item => item.id === block.info?.paletteGroup);
|
|
103
|
+
if (!group) {
|
|
104
|
+
unmarked = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
groupOf.set(opcode, `${category.id}/${group.id}`);
|
|
108
|
+
used.add(group.id);
|
|
109
|
+
}
|
|
110
|
+
const menuIconURI = category.menuIconURI || category.blockIconURI || undefined;
|
|
111
|
+
if (unmarked) {
|
|
112
|
+
groups.push({ id: category.id, extensionId: category.id, name: extensionName, extensionName, declared: false,
|
|
113
|
+
color1: category.color1, color2: category.color2, menuIconURI });
|
|
114
|
+
}
|
|
115
|
+
for (const group of declared) {
|
|
116
|
+
if (!used.has(group.id))
|
|
117
|
+
continue;
|
|
118
|
+
groups.push({ id: `${category.id}/${group.id}`, extensionId: category.id, name: group.name, extensionName,
|
|
119
|
+
declared: true, color1: group.color1 ?? category.color1, color2: group.color2 ?? category.color2,
|
|
120
|
+
menuIconURI: group.menuIconURI ?? menuIconURI });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { groups, groupOf };
|
|
124
|
+
}
|
|
36
125
|
/** The group a block belongs to, or null for a block no palette group offers. */
|
|
37
|
-
export function groupOfOpcode(opcode) {
|
|
126
|
+
export function groupOfOpcode(opcode, catalog) {
|
|
127
|
+
const declared = catalog?.groupOf.get(opcode);
|
|
128
|
+
if (declared)
|
|
129
|
+
return declared;
|
|
38
130
|
const separator = opcode.indexOf('_');
|
|
39
131
|
if (separator <= 0)
|
|
40
132
|
return null;
|
|
@@ -44,12 +136,12 @@ export function groupOfOpcode(opcode) {
|
|
|
44
136
|
return standardPrefixes[prefix] ?? prefix;
|
|
45
137
|
}
|
|
46
138
|
/** How many blocks of each group the target's code uses. Menus and other shadow blocks are not counted. */
|
|
47
|
-
export function usedGroups(target) {
|
|
139
|
+
export function usedGroups(target, catalog) {
|
|
48
140
|
const counts = new Map();
|
|
49
141
|
for (const block of Object.values(target.blocks?._blocks ?? {})) {
|
|
50
142
|
if (!block || block.shadow || typeof block.opcode !== 'string')
|
|
51
143
|
continue;
|
|
52
|
-
const group = groupOfOpcode(block.opcode);
|
|
144
|
+
const group = groupOfOpcode(block.opcode, catalog);
|
|
53
145
|
if (group)
|
|
54
146
|
counts.set(group, (counts.get(group) ?? 0) + 1);
|
|
55
147
|
}
|
|
@@ -57,16 +149,69 @@ export function usedGroups(target) {
|
|
|
57
149
|
}
|
|
58
150
|
/**
|
|
59
151
|
* The groups shown when a target has no setting of its own: every standard group, and the groups of
|
|
60
|
-
* the
|
|
152
|
+
* the extension groups its code already uses, so a project opens with nothing missing. `extensionGroups` are the
|
|
153
|
+
* IDs of the extension groups on offer: the extension IDs, or with a catalog, its group IDs.
|
|
61
154
|
*/
|
|
62
|
-
export function defaultVisibleGroups(target,
|
|
63
|
-
const used = usedGroups(target);
|
|
64
|
-
return [...standardGroups, ...
|
|
155
|
+
export function defaultVisibleGroups(target, extensionGroups, catalog) {
|
|
156
|
+
const used = usedGroups(target, catalog);
|
|
157
|
+
return [...standardGroups, ...extensionGroups.filter(id => used.has(id))];
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Splits each extension category whose extension declares groups into one category per group, in the catalog's
|
|
161
|
+
* order. A label, separator or button goes with the block after it, or with the last block when none follows.
|
|
162
|
+
*/
|
|
163
|
+
export function splitExtensionCategories(tree, catalog) {
|
|
164
|
+
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
165
|
+
const extensionId = category.getAttribute('id');
|
|
166
|
+
const groups = catalog.groups.filter(group => group.extensionId === extensionId);
|
|
167
|
+
if (!extensionId || !groups.some(group => group.declared))
|
|
168
|
+
continue;
|
|
169
|
+
const items = new Map();
|
|
170
|
+
let pending = [];
|
|
171
|
+
let last = extensionId;
|
|
172
|
+
for (const child of Array.from(category.children)) {
|
|
173
|
+
if (child.tagName.toLowerCase() !== 'block') {
|
|
174
|
+
pending.push(child);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
last = catalog.groupOf.get(child.getAttribute('type') ?? '') ?? extensionId;
|
|
178
|
+
items.set(last, [...items.get(last) ?? [], ...pending, child]);
|
|
179
|
+
pending = [];
|
|
180
|
+
}
|
|
181
|
+
if (pending.length)
|
|
182
|
+
items.set(last, [...items.get(last) ?? [], ...pending]);
|
|
183
|
+
let first = true;
|
|
184
|
+
for (const group of groups) {
|
|
185
|
+
const children = items.get(group.id);
|
|
186
|
+
if (!children)
|
|
187
|
+
continue;
|
|
188
|
+
const part = category.cloneNode(false);
|
|
189
|
+
part.setAttribute('id', group.id);
|
|
190
|
+
part.setAttribute('name', group.name);
|
|
191
|
+
if (group.color1)
|
|
192
|
+
part.setAttribute('colour', group.color1);
|
|
193
|
+
if (group.color2)
|
|
194
|
+
part.setAttribute('secondaryColour', group.color2);
|
|
195
|
+
if (group.menuIconURI)
|
|
196
|
+
part.setAttribute('iconURI', group.menuIconURI);
|
|
197
|
+
if (!first)
|
|
198
|
+
part.removeAttribute('showStatusButton');
|
|
199
|
+
first = false;
|
|
200
|
+
part.append(...children);
|
|
201
|
+
category.before(part);
|
|
202
|
+
}
|
|
203
|
+
category.remove();
|
|
204
|
+
}
|
|
65
205
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Removes the categories the target does not show from a parsed toolbox, before the palette is drawn from it.
|
|
208
|
+
* With a catalog, an extension that declares groups is first split into a category per group.
|
|
209
|
+
*/
|
|
210
|
+
export function filterToolboxTree(tree, visible, catalog) {
|
|
68
211
|
if (!tree)
|
|
69
212
|
return;
|
|
213
|
+
if (catalog)
|
|
214
|
+
splitExtensionCategories(tree, catalog);
|
|
70
215
|
for (const category of Array.from(tree.querySelectorAll(':scope > category'))) {
|
|
71
216
|
const id = category.getAttribute('id');
|
|
72
217
|
if (id && !visible.has(id))
|
|
@@ -78,15 +223,16 @@ export const paletteScreenStateKey = 'blockPalette';
|
|
|
78
223
|
const strings = (value) => Array.isArray(value) && value.every(item => typeof item === 'string') ? [...value] : undefined;
|
|
79
224
|
const sameGroups = (left, right) => left.length === right.length && new Set(left).size === left.length && left.every(group => right.includes(group));
|
|
80
225
|
/** The settings of one project, by runtime target ID, so a renamed sprite keeps its setting. */
|
|
81
|
-
export function createPaletteVisibilityStore() {
|
|
226
|
+
export function createPaletteVisibilityStore({ catalog } = {}) {
|
|
227
|
+
const defaults = (target, extensionIds) => defaultVisibleGroups(target, extensionIds, catalog?.());
|
|
82
228
|
let groups = new Map();
|
|
83
229
|
let projectSets = {};
|
|
84
230
|
return {
|
|
85
231
|
visible(target, extensionIds) {
|
|
86
|
-
return new Set(groups.get(target.id) ??
|
|
232
|
+
return new Set(groups.get(target.id) ?? defaults(target, extensionIds));
|
|
87
233
|
},
|
|
88
234
|
set(target, chosen, extensionIds) {
|
|
89
|
-
if (sameGroups(chosen,
|
|
235
|
+
if (sameGroups(chosen, defaults(target, extensionIds)))
|
|
90
236
|
groups.delete(target.id);
|
|
91
237
|
else
|
|
92
238
|
groups.set(target.id, [...chosen]);
|
|
@@ -6,6 +6,15 @@ export interface PaletteExtension {
|
|
|
6
6
|
id: string;
|
|
7
7
|
/** What the host knows about the extension; at least its name. */
|
|
8
8
|
metadata: ExtensionMetadata;
|
|
9
|
+
/**
|
|
10
|
+
* The groups the extension declares, each chosen on its own, in place of the extension. A group whose ID is the
|
|
11
|
+
* extension's holds the blocks no declared group takes.
|
|
12
|
+
*/
|
|
13
|
+
groups?: readonly {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
color?: string;
|
|
17
|
+
}[];
|
|
9
18
|
}
|
|
10
19
|
export interface PaletteVisibilityChoice {
|
|
11
20
|
/** The groups to show for the target. */
|
|
@@ -27,7 +36,7 @@ export interface PaletteVisibilityDialogProps {
|
|
|
27
36
|
newSprite?: boolean;
|
|
28
37
|
/**
|
|
29
38
|
* For a new sprite: the sprites already in the project with the groups each shows. The screen offers to copy
|
|
30
|
-
* one of them
|
|
39
|
+
* one of them into its choices, which the person then adjusts and applies.
|
|
31
40
|
*/
|
|
32
41
|
copySources?: readonly {
|
|
33
42
|
name: string;
|
|
@@ -41,7 +50,10 @@ interface DialogState {
|
|
|
41
50
|
addedSets: PaletteGroupSet[];
|
|
42
51
|
naming: boolean;
|
|
43
52
|
setName: string;
|
|
44
|
-
|
|
53
|
+
/** The sprite chosen to copy from, or null before one is chosen. */
|
|
54
|
+
copySource: number | null;
|
|
55
|
+
/** The groups a shortcut or a copy source under the pointer or focus would choose, shown in the lists. */
|
|
56
|
+
highlight: Set<string> | null;
|
|
45
57
|
}
|
|
46
58
|
/**
|
|
47
59
|
* The screen that chooses which block groups the palette shows for one sprite or the stage: sets that
|
|
@@ -55,8 +67,10 @@ export declare class PaletteVisibilityDialog extends React.PureComponent<Palette
|
|
|
55
67
|
sprite: (name: string) => string;
|
|
56
68
|
stage: string;
|
|
57
69
|
newSprite: (name: string) => string;
|
|
70
|
+
quick: string;
|
|
58
71
|
copyFrom: string;
|
|
59
|
-
|
|
72
|
+
copy: string;
|
|
73
|
+
chooseSprite: string;
|
|
60
74
|
sets: string;
|
|
61
75
|
addSet: string;
|
|
62
76
|
setName: string;
|
|
@@ -65,17 +79,25 @@ export declare class PaletteVisibilityDialog extends React.PureComponent<Palette
|
|
|
65
79
|
extensions: string;
|
|
66
80
|
used: (count: number) => string;
|
|
67
81
|
hiddenInUse: (count: number) => string;
|
|
82
|
+
otherBlocks: string;
|
|
68
83
|
cancel: string;
|
|
69
|
-
|
|
84
|
+
confirm: string;
|
|
70
85
|
close: string;
|
|
71
86
|
};
|
|
72
87
|
standard(): ("sound" | "variables" | "events" | "motion" | "looks" | "control" | "sensing" | "operators" | "lists" | "myBlocks")[];
|
|
88
|
+
/** Every group on offer, in the order the screen lists them. */
|
|
89
|
+
offered(): string[];
|
|
73
90
|
available(groups: readonly string[]): string[];
|
|
74
91
|
toggle(group: string): void;
|
|
75
92
|
toggleSet(set: PaletteGroupSet): void;
|
|
76
93
|
addSet(): void;
|
|
77
|
-
/**
|
|
78
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Choose the groups another sprite shows, as far as this one is offered them. The screen stays open, so the
|
|
96
|
+
* person sees the copy, adds or removes checks, and applies.
|
|
97
|
+
*/
|
|
98
|
+
copy(): void;
|
|
99
|
+
/** The groups of the chosen copy source this sprite can show, or none before one is chosen. */
|
|
100
|
+
copyGroups(): string[];
|
|
79
101
|
apply(): void;
|
|
80
102
|
render(): React.JSX.Element;
|
|
81
103
|
}
|