@kubohiroya/turbowarp-plus-editor-code-views 0.12.0 → 0.13.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 CHANGED
@@ -92,6 +92,7 @@ getInfo() {
92
92
  ```
93
93
 
94
94
  - 各グループはパレットのカテゴリ `shapes/geometry` になり、名前はグループの名前になる。設定もこの ID で記録する。`paletteGroup` のないブロック(宣言していないグループを指すものを含む)は、カテゴリ `shapes` に残る。
95
+ - グループの `id` は設定に記録する名前で、言語によって変わらない。`name` は利用者が読む表示名で、1つの文字列か、`{en: 'Speech\nand text', ja: 'セリフと\n文字'}` のような言語ごとの文字列を書く。エディターは、その言語の名前、同じ言語の名前、英語、最初の名前の順に選ぶ。`\n` は、カテゴリの一覧と設定の画面で改行になる(`resolveGroupName(name, locale)`。`extensionPaletteCatalog` に言語を渡す)。
95
96
  - グループの `color1`・`color2`・`color3`・`menuIconURI` は省略でき、省略すると機能拡張のものを使う。ブロック自身の色はグループの色より優先する。
96
97
  - 見出し・区切り・ボタンは、その次のブロックと同じグループに入る。オペコードは `shapes_circle` などのまま変わらないので、SB3 は変わらない。標準の TurboWarp と Scratch は `groups` と `paletteGroup` を無視し、1つのカテゴリとして表示する。
97
98
  - ホストは、機能拡張を読み込む前に `installExtensionPaletteGroups(vm.runtime)` を呼び、`extensionPaletteCatalog(vm.runtime._blockInfo)` をストア、`filterToolboxTree`、`usedGroups` に渡す。設定の画面には、そのような機能拡張の項目の `groups` としてグループを渡す。画面は機能拡張の行の中に、グループごとのチェックを出す。
package/README.md CHANGED
@@ -92,6 +92,7 @@ getInfo() {
92
92
  ```
93
93
 
94
94
  - 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`.
95
+ - A group's `id` names it in the settings and stays the same in every language; its `name` is what people read. `name` is one text or one per locale, such as `{en: 'Speech\nand text', ja: 'セリフと\n文字'}`: the editor shows the one for its locale, then for its language, then English, then the first. A `\n` starts a new line in the category menu and the settings screen (`resolveGroupName(name, locale)`; pass the locale to `extensionPaletteCatalog`).
95
96
  - `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.
96
97
  - 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.
97
98
  - 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.
@@ -63,6 +63,13 @@ export interface PaletteCategoryInfo {
63
63
  } | null;
64
64
  }[];
65
65
  }
66
+ /**
67
+ * A group's display name: one text, or one per locale such as `{en: 'Speech', ja: 'セリフ'}`. A line break (`\n`)
68
+ * starts a new line in the palette and the settings screen.
69
+ */
70
+ export type GroupName = string | Readonly<Record<string, string>>;
71
+ /** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
72
+ export declare function resolveGroupName(name: GroupName, locale?: string): string;
66
73
  /**
67
74
  * Keeps the groups each extension declares on its category info, where {@link extensionPaletteCatalog} reads them,
68
75
  * and draws the blocks of a group with a colour of its own in the group's colours. Install it before extensions load.
@@ -71,8 +78,8 @@ export interface PaletteCategoryInfo {
71
78
  export declare function installExtensionPaletteGroups(runtime: {
72
79
  _fillExtensionCategory?: (...args: any[]) => unknown;
73
80
  }): () => 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;
81
+ /** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
82
+ export declare function extensionPaletteCatalog(categories?: readonly PaletteCategoryInfo[], locale?: string): PaletteGroupCatalog;
76
83
  /** The group a block belongs to, or null for a block no palette group offers. */
77
84
  export declare function groupOfOpcode(opcode: string, catalog?: PaletteGroupCatalog): string | null;
78
85
  type BlockLike = {
@@ -33,6 +33,28 @@ 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
+ /** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
37
+ export function resolveGroupName(name, locale) {
38
+ if (typeof name === 'string')
39
+ return name;
40
+ const entries = Object.entries(name);
41
+ const wanted = (locale ?? '').toLowerCase();
42
+ const language = wanted.split(/[-_]/)[0];
43
+ return entries.find(([key]) => key.toLowerCase() === wanted)?.[1] ??
44
+ entries.find(([key]) => key.toLowerCase() === language)?.[1] ??
45
+ entries.find(([key]) => key.toLowerCase().split(/[-_]/)[0] === language && language)?.[1] ??
46
+ entries.find(([key]) => key.toLowerCase() === 'en')?.[1] ?? entries[0]?.[1] ?? '';
47
+ }
48
+ /** A name as `getInfo()` gives it, or nothing when it is neither a text nor texts by locale. */
49
+ function groupName(value) {
50
+ if (typeof value === 'string')
51
+ return value || undefined;
52
+ if (!value || typeof value !== 'object' || Array.isArray(value))
53
+ return undefined;
54
+ const names = Object.fromEntries(Object.entries(value)
55
+ .filter((entry) => typeof entry[1] === 'string' && entry[1] !== ''));
56
+ return Object.keys(names).length ? names : undefined;
57
+ }
36
58
  const groupIdPattern = /^[A-Za-z0-9_-]+$/;
37
59
  const optionalString = (value) => typeof value === 'string' && value ? value : undefined;
38
60
  /** The groups `getInfo()` declares, leaving out any that are not well formed or repeat an ID. */
@@ -47,7 +69,7 @@ function declaredGroups(value) {
47
69
  if (typeof id !== 'string' || !groupIdPattern.test(id) || groups.some(group => group.id === id))
48
70
  continue;
49
71
  const entry = item;
50
- groups.push({ id, name: typeof name === 'string' && name ? name : id, color1: optionalString(entry.color1),
72
+ groups.push({ id, name: groupName(name) ?? id, color1: optionalString(entry.color1),
51
73
  color2: optionalString(entry.color2), color3: optionalString(entry.color3),
52
74
  menuIconURI: optionalString(entry.menuIconURI) });
53
75
  }
@@ -86,8 +108,8 @@ export function installExtensionPaletteGroups(runtime) {
86
108
  runtime._fillExtensionCategory = original;
87
109
  };
88
110
  }
89
- /** The groups of the loaded extensions, in palette order, from the VM's category info. */
90
- export function extensionPaletteCatalog(categories = []) {
111
+ /** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
112
+ export function extensionPaletteCatalog(categories = [], locale) {
91
113
  const groups = [];
92
114
  const groupOf = new Map();
93
115
  for (const category of categories) {
@@ -115,7 +137,7 @@ export function extensionPaletteCatalog(categories = []) {
115
137
  for (const group of declared) {
116
138
  if (!used.has(group.id))
117
139
  continue;
118
- groups.push({ id: `${category.id}/${group.id}`, extensionId: category.id, name: group.name, extensionName,
140
+ groups.push({ id: `${category.id}/${group.id}`, extensionId: category.id, name: resolveGroupName(group.name, locale), extensionName,
119
141
  declared: true, color1: group.color1 ?? category.color1, color2: group.color2 ?? category.color2,
120
142
  menuIconURI: group.menuIconURI ?? menuIconURI });
121
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.12.0",
3
+ "version": "0.13.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",
@@ -79,7 +79,7 @@ export interface PaletteGroupCatalog {
79
79
  /** A declared group as kept on the VM's category info. */
80
80
  interface DeclaredGroup {
81
81
  id: string;
82
- name: string;
82
+ name: GroupName;
83
83
  color1?: string;
84
84
  color2?: string;
85
85
  color3?: string;
@@ -98,6 +98,33 @@ export interface PaletteCategoryInfo {
98
98
  blocks?: readonly {info?: {paletteGroup?: unknown}; json?: {type?: unknown} | null}[];
99
99
  }
100
100
 
101
+ /**
102
+ * A group's display name: one text, or one per locale such as `{en: 'Speech', ja: 'セリフ'}`. A line break (`\n`)
103
+ * starts a new line in the palette and the settings screen.
104
+ */
105
+ export type GroupName = string | Readonly<Record<string, string>>;
106
+
107
+ /** The name to show in `locale`: that locale, then its language, then English, then the first one given. */
108
+ export function resolveGroupName(name: GroupName, locale?: string): string {
109
+ if (typeof name === 'string') return name;
110
+ const entries = Object.entries(name);
111
+ const wanted = (locale ?? '').toLowerCase();
112
+ const language = wanted.split(/[-_]/)[0];
113
+ return entries.find(([key]) => key.toLowerCase() === wanted)?.[1] ??
114
+ entries.find(([key]) => key.toLowerCase() === language)?.[1] ??
115
+ entries.find(([key]) => key.toLowerCase().split(/[-_]/)[0] === language && language)?.[1] ??
116
+ entries.find(([key]) => key.toLowerCase() === 'en')?.[1] ?? entries[0]?.[1] ?? '';
117
+ }
118
+
119
+ /** A name as `getInfo()` gives it, or nothing when it is neither a text nor texts by locale. */
120
+ function groupName(value: unknown): GroupName | undefined {
121
+ if (typeof value === 'string') return value || undefined;
122
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
123
+ const names = Object.fromEntries(Object.entries(value as Record<string, unknown>)
124
+ .filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1] !== ''));
125
+ return Object.keys(names).length ? names : undefined;
126
+ }
127
+
101
128
  const groupIdPattern = /^[A-Za-z0-9_-]+$/;
102
129
  const optionalString = (value: unknown) => typeof value === 'string' && value ? value : undefined;
103
130
  /** The groups `getInfo()` declares, leaving out any that are not well formed or repeat an ID. */
@@ -109,7 +136,7 @@ function declaredGroups(value: unknown): DeclaredGroup[] {
109
136
  const {id, name} = item as Record<string, unknown>;
110
137
  if (typeof id !== 'string' || !groupIdPattern.test(id) || groups.some(group => group.id === id)) continue;
111
138
  const entry = item as Record<string, unknown>;
112
- groups.push({id, name: typeof name === 'string' && name ? name : id, color1: optionalString(entry.color1),
139
+ groups.push({id, name: groupName(name) ?? id, color1: optionalString(entry.color1),
113
140
  color2: optionalString(entry.color2), color3: optionalString(entry.color3),
114
141
  menuIconURI: optionalString(entry.menuIconURI)});
115
142
  }
@@ -145,8 +172,8 @@ export function installExtensionPaletteGroups(runtime: {_fillExtensionCategory?:
145
172
  };
146
173
  }
147
174
 
148
- /** The groups of the loaded extensions, in palette order, from the VM's category info. */
149
- export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo[] = []): PaletteGroupCatalog {
175
+ /** The groups of the loaded extensions, in palette order, from the VM's category info, named in `locale`. */
176
+ export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo[] = [], locale?: string): PaletteGroupCatalog {
150
177
  const groups: ExtensionPaletteGroup[] = [];
151
178
  const groupOf = new Map<string, string>();
152
179
  for (const category of categories) {
@@ -172,7 +199,7 @@ export function extensionPaletteCatalog(categories: readonly PaletteCategoryInfo
172
199
  }
173
200
  for (const group of declared) {
174
201
  if (!used.has(group.id)) continue;
175
- groups.push({id: `${category.id}/${group.id}`, extensionId: category.id, name: group.name, extensionName,
202
+ groups.push({id: `${category.id}/${group.id}`, extensionId: category.id, name: resolveGroupName(group.name, locale), extensionName,
176
203
  declared: true, color1: group.color1 ?? category.color1, color2: group.color2 ?? category.color2,
177
204
  menuIconURI: group.menuIconURI ?? menuIconURI});
178
205
  }
package/src/styles.css CHANGED
@@ -766,6 +766,12 @@
766
766
  gap: 0.35rem;
767
767
  }
768
768
 
769
+ /* A group name may hold line breaks, in the palette's category menu and in the settings screen. */
770
+ .scratchCategoryMenuItemLabel,
771
+ .tw-palette-visibility-extension-groups label {
772
+ white-space: pre-line;
773
+ }
774
+
769
775
  /* The groups one extension declares, chosen one by one inside its row. */
770
776
  .tw-palette-visibility-extension-groups {
771
777
  display: grid;