@kubohiroya/turbowarp-plus-editor-workbench 0.4.0 → 0.4.1
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 +42 -0
- package/README.md +42 -0
- package/dist/ai-agent-settings.d.ts +97 -0
- package/dist/ai-agent-settings.js +316 -0
- package/dist/ai-agents.d.ts +24 -0
- package/dist/ai-agents.js +75 -0
- package/dist/drawer-views.d.ts +66 -0
- package/dist/drawer-views.js +152 -0
- package/dist/sprite-tabs.d.ts +20 -0
- package/dist/sprite-tabs.js +101 -20
- package/dist/standard-layout.d.ts +4 -1
- package/dist/standard-layout.js +57 -13
- package/dist/target-tab-menu.d.ts +29 -0
- package/dist/target-tab-menu.js +76 -0
- package/dist/workspace-layout.d.ts +1 -0
- package/dist/workspace-layout.js +6 -2
- package/package.json +5 -1
- package/src/ai-agent-settings.ts +380 -0
- package/src/ai-agents.ts +95 -0
- package/src/drawer-views.ts +203 -0
- package/src/editor.css +270 -0
- package/src/sprite-layout-menu.css +37 -0
- package/src/sprite-tabs.ts +100 -20
- package/src/standard-layout.ts +67 -17
- package/src/target-tab-menu.ts +99 -0
- package/src/workspace-layout.ts +6 -3
package/README.ja.md
CHANGED
|
@@ -29,6 +29,48 @@ Scratch 配置では分割を既定で有効にする。既存のステージサ
|
|
|
29
29
|
|
|
30
30
|
Editor はこのパッケージを npm から固定して使う。
|
|
31
31
|
|
|
32
|
+
## 左側ドロワーの表示
|
|
33
|
+
|
|
34
|
+
Plus 標準配置の左側ドロワーは、スプライト一覧と背景を表示する。ホストは `StandardLayout` に `drawerViews` を渡して、表示する内容を足せる。
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
<StandardLayout {...props} storage={localStorage} storageKey="twPlusEditorLayout" drawerViews={{
|
|
38
|
+
views: [
|
|
39
|
+
{id: 'project', render: ({locale}) => <ProjectExplorer locale={locale} />, tools: ({locale}) => <ExplorerTools />},
|
|
40
|
+
{id: 'git-history', label: 'Git 履歴', icon: <HistoryIcon />, render: () => <GitHistory />}
|
|
41
|
+
],
|
|
42
|
+
agents: {renderMenu: ({locale, close}) => <div role="menu">…</div>}
|
|
43
|
+
}} />
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- ドロワーの最上部に、アイコンだけのボタンを 1 行に並べたもの(ドロワーが狭すぎるときだけ折り返す)(`role="toolbar"`、「ドロワーの表示」)が出る。1 スプライト一覧、2 プロジェクトフォルダ、3 Git 履歴、4 AI エージェント。それぞれ日本語か英語の `aria-label` と `title` を持つ。ボタンは、ホストが渡したものだけを出す。スプライト一覧は常に先頭にある。
|
|
47
|
+
- 1〜3 は内容を切り替え、選択中のボタンを `aria-pressed` で示す。矢印キーでボタンの間を移る(Tab で止まるのは 1 つ。左右は端で反対側に回り、Home と End は両端へ移る)。Enter と Space で選ぶ。
|
|
48
|
+
- `id` は `project` か `git-history`。知らない `id` と、重なった `id` は無視する。`label` と `icon` を省くと、パッケージの名前と、`currentColor` で描く 16 px のアイコンを使う。`render` は選択中だけ呼ぶ。`tools` は、スプライト一覧の検索・ピン・アンドックと同じ位置に出す。検索とアンドックは、スプライト一覧を選んでいるときだけ出る。ピン留めはドロワーそのものの操作なので、どの内容でも出す。ほかの内容を出している間も、GUI の対象ペインは隠したまま残す。
|
|
49
|
+
- 4 はメニューを開くボタン(`aria-haspopup="menu"`、`aria-expanded`)。`renderMenu` がメニューの中身を描く。ふつうは `role="menu"` の要素 1 つと、その中の `menuitem` にする。開くと最初の項目に移る。Escape、外側の押下、`close()` で閉じ、ボタンに戻る。ドロワーの内容は切り替えない。
|
|
50
|
+
- 選んだ内容は、分割レイアウトと同じ `storage` の `<storageKey>.drawer`(`{"version": 1, "view": "project"}`)に保存する。保存した内容をホストが渡していないときはスプライト一覧に戻り、ホストがまた渡すと復元する。
|
|
51
|
+
- `drawerViews` がないとき、または使える内容も `agents` もないときは、ドロワーは変わらない。ボタンを出さず、スプライト一覧を囲む要素も足さない。内容は起動時から渡す。後から最初の内容を足すと、GUI の対象ペインを 1 度作り直す。
|
|
52
|
+
|
|
53
|
+
試験用ホストの `/example/?standard=1&drawer=1&lang=ja` で、見本の内容を入れたボタンと、パッケージの AI エージェントのメニューを試せる。
|
|
54
|
+
|
|
55
|
+
### AI エージェントのメニューと設定
|
|
56
|
+
|
|
57
|
+
`@kubohiroya/turbowarp-plus-editor-workbench/ai-agents` は、4 のボタンのメニューと、その設定のダイアログを渡す。
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import styles from '@kubohiroya/turbowarp-plus-editor-workbench/editor.css';
|
|
61
|
+
import {createAiAgents} from '@kubohiroya/turbowarp-plus-editor-workbench/ai-agents';
|
|
62
|
+
|
|
63
|
+
const aiAgents = createAiAgents(React, {styles});
|
|
64
|
+
<StandardLayout {...props} drawerViews={{views, agents: aiAgents.drawerMenu}} />
|
|
65
|
+
// ほかの場所、たとえば設定のメニューの項目(アイコンは aiAgents.settingsIcon())から:
|
|
66
|
+
aiAgents.openSettings({locale, returnFocus: trigger});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- メニューは、メニューに出すことにしたサービスを、アイコンと名前で並べ、区切りのあとに「AI エージェント設定…」を置く。出すサービスがないときは、その項目と短い案内だけを出す。矢印キー、Home、End で項目を移る。サービスを選ぶと、`window.open(url, '_blank', 'noopener,noreferrer')` で開く。
|
|
70
|
+
- 用意されたサービスは ChatGPT(`https://chatgpt.com/`)と Claude(`https://claude.ai/new`)。それぞれメニューから外せ、以前の会話などの URL に置き換えられる。URL を空にすると既定に戻る。利用者は、名前と URL で自分のサービスを足し、消せる。URL は `https:` で始まるものだけを受け付け、ほかは欄の下に誤りを出す。アイコンは汎用のもので、用意されたサービスは頭文字入りの吹き出し、自分のサービスは地球儀にする。各サービスのロゴは使わない。
|
|
71
|
+
- ダイアログ(`role="dialog"`、`aria-modal`)は React を使わない DOM で、`openAgentSettingsDialog` で開ける。開くと最初の欄に移り、Tab はダイアログの中を回る。Escape かキャンセルで保存せずに閉じ、`returnFocus`、なければ開く前の要素に戻る。
|
|
72
|
+
- 設定は `localStorage` の `twPlusAiAgents` に `{"version": 1, "services": [...]}` で保存する。各サービスの `target` は `kind: "url"`。ほかの `kind` は後の段階のためにあけてあり、いまは読み飛ばす。ない・壊れた・誤ったデータは、項目ごとに既定に戻す。
|
|
73
|
+
|
|
32
74
|
## 検証
|
|
33
75
|
|
|
34
76
|
`npm test` で配分・縦横比・設定の読み書きを、`npm run test:browser` で独立した試験用ホストを検証する。試験用ホストを手動で開くには、このディレクトリで `npm install` と `python3 -m http.server` を実行し、`/example/` を表示する。
|
package/README.md
CHANGED
|
@@ -31,6 +31,48 @@ Use `/example/?standard=1` in the test host to try the Plus layout. Use `standar
|
|
|
31
31
|
|
|
32
32
|
The Editor pins this package from npm.
|
|
33
33
|
|
|
34
|
+
## Left drawer views
|
|
35
|
+
|
|
36
|
+
In the Plus standard layout, the left drawer shows the sprite list and backdrop. A host can add more contents by passing `drawerViews` to `StandardLayout`:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
<StandardLayout {...props} storage={localStorage} storageKey="twPlusEditorLayout" drawerViews={{
|
|
40
|
+
views: [
|
|
41
|
+
{id: 'project', render: ({locale}) => <ProjectExplorer locale={locale} />, tools: ({locale}) => <ExplorerTools />},
|
|
42
|
+
{id: 'git-history', label: 'Git history', icon: <HistoryIcon />, render: () => <GitHistory />}
|
|
43
|
+
],
|
|
44
|
+
agents: {renderMenu: ({locale, close}) => <div role="menu">…</div>}
|
|
45
|
+
}} />
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- The top of the drawer then shows one row of icon-only buttons (it wraps only when the drawer is too narrow) (`role="toolbar"`, "ドロワーの表示" / "Drawer views"): 1 Sprites, 2 Project folder, 3 Git history and 4 AI agents. Each has `aria-label` and `title` in Japanese or English. A button is shown only for what the host supplies; the sprite list is always first.
|
|
49
|
+
- Buttons 1–3 switch the content and mark the selected one with `aria-pressed`. Arrow keys move between the buttons with one tab stop (left and right wrap around, Home and End go to the ends); Enter and Space activate.
|
|
50
|
+
- `id` is `project` or `git-history`; unknown and repeated ids are ignored. `label` and `icon` default to the package's names and 16 px `currentColor` icons. `render` is called only while the view is selected. `tools` is shown where the sprite list shows its search, pin and undock buttons. Search and undock appear only while the sprite list is selected; the pin button, which pins the drawer itself, stays in every view. The GUI's target pane stays mounted, hidden, while another view is shown.
|
|
51
|
+
- Button 4 is a menu button (`aria-haspopup="menu"`, `aria-expanded`). `renderMenu` renders the menu content, normally one `role="menu"` element with `menuitem` children. The first item gets focus when the menu opens. Escape, a press outside or `close()` closes it and returns focus to the button. The menu does not change the drawer's content.
|
|
52
|
+
- The selected view is saved in `storage` under `<storageKey>.drawer` (`{"version": 1, "view": "project"}`), next to the split layout. A saved view the host does not supply falls back to the sprite list and comes back when the host supplies it again.
|
|
53
|
+
- Without `drawerViews`, or with no supported views and no `agents`, the drawer is unchanged: no bar and no extra element around the sprite list. Supply the views from startup; adding the first one later remounts the GUI's target pane once.
|
|
54
|
+
|
|
55
|
+
Use `/example/?standard=1&drawer=1&lang=ja` in the test host to try the bar with sample views and the package's AI agent menu.
|
|
56
|
+
|
|
57
|
+
### AI agent menu and settings
|
|
58
|
+
|
|
59
|
+
`@kubohiroya/turbowarp-plus-editor-workbench/ai-agents` supplies the menu for button 4 and its settings dialog:
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import styles from '@kubohiroya/turbowarp-plus-editor-workbench/editor.css';
|
|
63
|
+
import {createAiAgents} from '@kubohiroya/turbowarp-plus-editor-workbench/ai-agents';
|
|
64
|
+
|
|
65
|
+
const aiAgents = createAiAgents(React, {styles});
|
|
66
|
+
<StandardLayout {...props} drawerViews={{views, agents: aiAgents.drawerMenu}} />
|
|
67
|
+
// Anywhere else, for example a settings menu entry with aiAgents.settingsIcon():
|
|
68
|
+
aiAgents.openSettings({locale, returnFocus: trigger});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- The menu lists the turned-on services, each with an icon and its name, then a separator and "AI エージェント設定…" / "AI agent settings…". Without turned-on services it shows only that item and a short hint. Arrow keys, Home and End move between the items. Choosing a service opens its address with `window.open(url, '_blank', 'noopener,noreferrer')`.
|
|
72
|
+
- The presets are ChatGPT (`https://chatgpt.com/`) and Claude (`https://claude.ai/new`). Each can be turned off, and its address replaced by a deep link such as a previous conversation; an empty address means the default. The user can add and remove services of their own (name and address). Only `https:` addresses are accepted; anything else shows an error under the field. The icons are generic: a chat bubble with an initial for a preset, a globe for the user's own service. No service logos are used.
|
|
73
|
+
- The dialog (`role="dialog"`, `aria-modal`) is plain DOM, so it can be opened without React (`openAgentSettingsDialog`). It focuses its first field, keeps Tab inside, closes on Escape or Cancel without saving, and returns focus to `returnFocus` or to the element focused before.
|
|
74
|
+
- The settings are saved in `localStorage` under `twPlusAiAgents` as `{"version": 1, "services": [...]}`. Each service has a `target` of `kind: "url"`; other kinds are left for later and ignored for now. Missing, broken or invalid data falls back entry by entry to the presets.
|
|
75
|
+
|
|
34
76
|
## Verification
|
|
35
77
|
|
|
36
78
|
Run `npm test` to verify split sizing, aspect ratios, and settings persistence. Run `npm run test:browser` to test the standalone host. To open the test host manually, run `npm install` and `python3 -m http.server` in this directory, then open `/example/`.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** The services the Editor knows by name. Their name and icon are fixed; their address can be replaced. */
|
|
2
|
+
export type AgentPresetId = 'chatgpt' | 'claude';
|
|
3
|
+
/**
|
|
4
|
+
* Where a service is opened. Only web pages for now; a desktop app launched with arguments is planned
|
|
5
|
+
* as another `kind`, which this version ignores when it reads the settings.
|
|
6
|
+
*/
|
|
7
|
+
export interface AgentUrlTarget {
|
|
8
|
+
kind: 'url';
|
|
9
|
+
/** An `https:` address: the service's start page or a deep link such as a previous conversation. */
|
|
10
|
+
url: string;
|
|
11
|
+
}
|
|
12
|
+
export type AgentTarget = AgentUrlTarget;
|
|
13
|
+
export interface AgentService {
|
|
14
|
+
id: string;
|
|
15
|
+
preset?: AgentPresetId;
|
|
16
|
+
name: string;
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
target: AgentTarget;
|
|
19
|
+
}
|
|
20
|
+
export interface AgentSettings {
|
|
21
|
+
version: 1;
|
|
22
|
+
/** The presets in their fixed order, then the user's own services. */
|
|
23
|
+
services: AgentService[];
|
|
24
|
+
}
|
|
25
|
+
export type AgentStorage = Pick<Storage, 'getItem' | 'setItem'>;
|
|
26
|
+
export declare const agentSettingsKey = "twPlusAiAgents";
|
|
27
|
+
export declare const agentPresets: readonly {
|
|
28
|
+
id: AgentPresetId;
|
|
29
|
+
name: string;
|
|
30
|
+
url: string;
|
|
31
|
+
letter: string;
|
|
32
|
+
}[];
|
|
33
|
+
export type AgentUrlProblem = 'empty' | 'invalid' | 'not-https';
|
|
34
|
+
/** The address to save, or why it cannot be used. Only `https:` addresses are accepted. */
|
|
35
|
+
export declare function checkAgentUrl(value: unknown): {
|
|
36
|
+
url: string;
|
|
37
|
+
} | {
|
|
38
|
+
problem: AgentUrlProblem;
|
|
39
|
+
};
|
|
40
|
+
export declare const defaultAgentSettings: () => AgentSettings;
|
|
41
|
+
/** Settings read from saved JSON. Anything missing, unknown or invalid falls back to the defaults, entry by entry. */
|
|
42
|
+
export declare function parseAgentSettings(raw: string | null | undefined): AgentSettings;
|
|
43
|
+
export declare const browserStorage: () => AgentStorage | null;
|
|
44
|
+
export declare function readAgentSettings(storage?: AgentStorage | null, key?: string): AgentSettings;
|
|
45
|
+
/** Saves the settings; returns false when the storage refuses them. */
|
|
46
|
+
export declare function writeAgentSettings(settings: AgentSettings, storage?: AgentStorage | null, key?: string): boolean;
|
|
47
|
+
export declare const enabledAgentServices: (settings: AgentSettings) => AgentService[];
|
|
48
|
+
export type Opener = (url: string, target: string, features: string) => unknown;
|
|
49
|
+
/** Opens the service in a new tab, without giving it access to the Editor. */
|
|
50
|
+
export declare function openAgentService(service: AgentService, open?: Opener): void;
|
|
51
|
+
declare const texts: {
|
|
52
|
+
readonly menu: readonly ["AI エージェント", "AI agents"];
|
|
53
|
+
readonly settings: readonly ["AI エージェント設定…", "AI agent settings…"];
|
|
54
|
+
readonly empty: readonly ["メニューに出す AI サービスがありません。設定で選んでください。", "No AI services are shown here. Choose them in the settings."];
|
|
55
|
+
readonly newTab: readonly ["別のタブで開きます", "Opens in a new tab"];
|
|
56
|
+
readonly title: readonly ["AI エージェント設定", "AI agent settings"];
|
|
57
|
+
readonly description: readonly ["ドロワーの AI エージェントのメニューに出すサービスを選びます。URL は、以前の会話など https: で始まるアドレスに置き換えられます。", "Choose the services the AI agent menu in the drawer shows. Each URL can be replaced by any https: address, such as a previous conversation."];
|
|
58
|
+
readonly presets: readonly ["用意されたサービス", "Built-in services"];
|
|
59
|
+
readonly custom: readonly ["追加したサービス", "Your services"];
|
|
60
|
+
readonly customEmpty: readonly ["まだありません。", "None yet."];
|
|
61
|
+
readonly customService: readonly ["追加したサービス", "Service"];
|
|
62
|
+
readonly show: readonly ["メニューに出す", "Show in the menu"];
|
|
63
|
+
readonly name: readonly ["名前", "Name"];
|
|
64
|
+
readonly url: readonly ["URL", "URL"];
|
|
65
|
+
readonly defaultUrl: readonly ["空欄のときは", "When empty:"];
|
|
66
|
+
readonly add: readonly ["サービスを追加", "Add a service"];
|
|
67
|
+
readonly remove: readonly ["削除", "Remove"];
|
|
68
|
+
readonly cancel: readonly ["キャンセル", "Cancel"];
|
|
69
|
+
readonly save: readonly ["保存", "Save"];
|
|
70
|
+
readonly nameEmpty: readonly ["名前を入力してください。", "Enter a name."];
|
|
71
|
+
readonly urlEmpty: readonly ["URL を入力してください。", "Enter a URL."];
|
|
72
|
+
readonly urlInvalid: readonly ["URL として読めません。https: で始まるアドレスを入力してください。", "This is not a URL. Enter an address that starts with https:."];
|
|
73
|
+
readonly urlNotHttps: readonly ["https: で始まるアドレスだけを使えます。", "Only addresses that start with https: can be used."];
|
|
74
|
+
readonly saveFailed: readonly ["設定を保存できませんでした。ブラウザーの保存領域が使えません。", "The settings could not be saved: browser storage is unavailable."];
|
|
75
|
+
};
|
|
76
|
+
export declare const agentText: (key: keyof typeof texts, locale: string) => "AI エージェント" | "AI agents" | "AI エージェント設定…" | "AI agent settings…" | "メニューに出す AI サービスがありません。設定で選んでください。" | "No AI services are shown here. Choose them in the settings." | "別のタブで開きます" | "Opens in a new tab" | "AI エージェント設定" | "AI agent settings" | "ドロワーの AI エージェントのメニューに出すサービスを選びます。URL は、以前の会話など https: で始まるアドレスに置き換えられます。" | "Choose the services the AI agent menu in the drawer shows. Each URL can be replaced by any https: address, such as a previous conversation." | "用意されたサービス" | "Built-in services" | "追加したサービス" | "Your services" | "まだありません。" | "None yet." | "Service" | "メニューに出す" | "Show in the menu" | "名前" | "Name" | "URL" | "空欄のときは" | "When empty:" | "サービスを追加" | "Add a service" | "削除" | "Remove" | "キャンセル" | "Cancel" | "保存" | "Save" | "名前を入力してください。" | "Enter a name." | "URL を入力してください。" | "Enter a URL." | "URL として読めません。https: で始まるアドレスを入力してください。" | "This is not a URL. Enter an address that starts with https:." | "https: で始まるアドレスだけを使えます。" | "Only addresses that start with https: can be used." | "設定を保存できませんでした。ブラウザーの保存領域が使えません。" | "The settings could not be saved: browser storage is unavailable.";
|
|
77
|
+
export interface AgentSettingsDialogOptions {
|
|
78
|
+
document?: Document;
|
|
79
|
+
locale: string;
|
|
80
|
+
/** The workbench stylesheet's class names (`editor.css`). */
|
|
81
|
+
styles?: Record<string, string>;
|
|
82
|
+
storage?: AgentStorage | null;
|
|
83
|
+
storageKey?: string;
|
|
84
|
+
/** Where focus goes when the dialog closes. Defaults to the element focused when it opened. */
|
|
85
|
+
returnFocus?: HTMLElement | null;
|
|
86
|
+
onSave?(settings: AgentSettings): void;
|
|
87
|
+
}
|
|
88
|
+
export interface AgentSettingsDialog {
|
|
89
|
+
element: HTMLElement;
|
|
90
|
+
close(): void;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The settings dialog: turns the presets on and off, replaces their address, and adds or removes the
|
|
94
|
+
* user's own services. It is plain DOM so that any part of the Editor can open it, with or without React.
|
|
95
|
+
*/
|
|
96
|
+
export declare function openAgentSettingsDialog({ document: doc, locale, styles, storage, storageKey, returnFocus, onSave }: AgentSettingsDialogOptions): AgentSettingsDialog;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { isJapanese } from "./drawer-views.js";
|
|
2
|
+
export const agentSettingsKey = 'twPlusAiAgents';
|
|
3
|
+
export const agentPresets = [
|
|
4
|
+
{ id: 'chatgpt', name: 'ChatGPT', url: 'https://chatgpt.com/', letter: 'G' },
|
|
5
|
+
{ id: 'claude', name: 'Claude', url: 'https://claude.ai/new', letter: 'C' }
|
|
6
|
+
];
|
|
7
|
+
/** The address to save, or why it cannot be used. Only `https:` addresses are accepted. */
|
|
8
|
+
export function checkAgentUrl(value) {
|
|
9
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
10
|
+
if (!text)
|
|
11
|
+
return { problem: 'empty' };
|
|
12
|
+
let url;
|
|
13
|
+
try {
|
|
14
|
+
url = new URL(text);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return { problem: 'invalid' };
|
|
18
|
+
}
|
|
19
|
+
if (url.protocol !== 'https:')
|
|
20
|
+
return { problem: 'not-https' };
|
|
21
|
+
if (!url.hostname)
|
|
22
|
+
return { problem: 'invalid' };
|
|
23
|
+
return { url: url.href };
|
|
24
|
+
}
|
|
25
|
+
const presetService = (preset, saved) => {
|
|
26
|
+
const checked = checkAgentUrl(saved?.target?.url);
|
|
27
|
+
return {
|
|
28
|
+
id: preset.id, preset: preset.id, name: preset.name,
|
|
29
|
+
enabled: saved?.enabled !== false,
|
|
30
|
+
target: { kind: 'url', url: 'url' in checked ? checked.url : preset.url }
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export const defaultAgentSettings = () => ({ version: 1, services: agentPresets.map(preset => presetService(preset)) });
|
|
34
|
+
/** Settings read from saved JSON. Anything missing, unknown or invalid falls back to the defaults, entry by entry. */
|
|
35
|
+
export function parseAgentSettings(raw) {
|
|
36
|
+
let saved;
|
|
37
|
+
try {
|
|
38
|
+
saved = JSON.parse(raw ?? 'null');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return defaultAgentSettings();
|
|
42
|
+
}
|
|
43
|
+
if (!saved || typeof saved !== 'object' || saved.version !== 1 ||
|
|
44
|
+
!Array.isArray(saved.services))
|
|
45
|
+
return defaultAgentSettings();
|
|
46
|
+
const entries = saved.services.filter(entry => entry && typeof entry === 'object');
|
|
47
|
+
const presets = agentPresets.map(preset => presetService(preset, entries.find(entry => entry.preset === preset.id)));
|
|
48
|
+
const ids = new Set(presets.map(service => service.id));
|
|
49
|
+
const custom = [];
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
if (entry.preset !== undefined || entry.target?.kind !== 'url')
|
|
52
|
+
continue;
|
|
53
|
+
const name = typeof entry.name === 'string' ? entry.name.trim() : '';
|
|
54
|
+
const checked = checkAgentUrl(entry.target.url);
|
|
55
|
+
if (!name || !('url' in checked))
|
|
56
|
+
continue;
|
|
57
|
+
let id = typeof entry.id === 'string' && entry.id ? entry.id : 'custom';
|
|
58
|
+
for (let n = 2; ids.has(id); n++)
|
|
59
|
+
id = `${String(entry.id || 'custom')}-${n}`;
|
|
60
|
+
ids.add(id);
|
|
61
|
+
custom.push({ id, name, enabled: entry.enabled !== false, target: { kind: 'url', url: checked.url } });
|
|
62
|
+
}
|
|
63
|
+
return { version: 1, services: [...presets, ...custom] };
|
|
64
|
+
}
|
|
65
|
+
export const browserStorage = () => {
|
|
66
|
+
try {
|
|
67
|
+
return globalThis.localStorage ?? null;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
export function readAgentSettings(storage = browserStorage(), key = agentSettingsKey) {
|
|
74
|
+
try {
|
|
75
|
+
return parseAgentSettings(storage?.getItem(key));
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return defaultAgentSettings();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Saves the settings; returns false when the storage refuses them. */
|
|
82
|
+
export function writeAgentSettings(settings, storage = browserStorage(), key = agentSettingsKey) {
|
|
83
|
+
if (!storage)
|
|
84
|
+
return false;
|
|
85
|
+
try {
|
|
86
|
+
storage.setItem(key, JSON.stringify({ version: 1, services: settings.services.map(service => service.preset ?
|
|
87
|
+
{ preset: service.preset, enabled: service.enabled, target: service.target } :
|
|
88
|
+
{ id: service.id, name: service.name, enabled: service.enabled, target: service.target }) }));
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export const enabledAgentServices = (settings) => settings.services.filter(service => service.enabled);
|
|
96
|
+
/** Opens the service in a new tab, without giving it access to the Editor. */
|
|
97
|
+
export function openAgentService(service, open = (...args) => window.open(...args)) {
|
|
98
|
+
if (service.target.kind === 'url')
|
|
99
|
+
open(service.target.url, '_blank', 'noopener,noreferrer');
|
|
100
|
+
}
|
|
101
|
+
const texts = {
|
|
102
|
+
menu: ['AI エージェント', 'AI agents'],
|
|
103
|
+
settings: ['AI エージェント設定…', 'AI agent settings…'],
|
|
104
|
+
empty: ['メニューに出す AI サービスがありません。設定で選んでください。',
|
|
105
|
+
'No AI services are shown here. Choose them in the settings.'],
|
|
106
|
+
newTab: ['別のタブで開きます', 'Opens in a new tab'],
|
|
107
|
+
title: ['AI エージェント設定', 'AI agent settings'],
|
|
108
|
+
description: ['ドロワーの AI エージェントのメニューに出すサービスを選びます。URL は、以前の会話など https: で始まるアドレスに置き換えられます。',
|
|
109
|
+
'Choose the services the AI agent menu in the drawer shows. Each URL can be replaced by any https: address, such as a previous conversation.'],
|
|
110
|
+
presets: ['用意されたサービス', 'Built-in services'],
|
|
111
|
+
custom: ['追加したサービス', 'Your services'],
|
|
112
|
+
customEmpty: ['まだありません。', 'None yet.'],
|
|
113
|
+
customService: ['追加したサービス', 'Service'],
|
|
114
|
+
show: ['メニューに出す', 'Show in the menu'],
|
|
115
|
+
name: ['名前', 'Name'],
|
|
116
|
+
url: ['URL', 'URL'],
|
|
117
|
+
defaultUrl: ['空欄のときは', 'When empty:'],
|
|
118
|
+
add: ['サービスを追加', 'Add a service'],
|
|
119
|
+
remove: ['削除', 'Remove'],
|
|
120
|
+
cancel: ['キャンセル', 'Cancel'],
|
|
121
|
+
save: ['保存', 'Save'],
|
|
122
|
+
nameEmpty: ['名前を入力してください。', 'Enter a name.'],
|
|
123
|
+
urlEmpty: ['URL を入力してください。', 'Enter a URL.'],
|
|
124
|
+
urlInvalid: ['URL として読めません。https: で始まるアドレスを入力してください。', 'This is not a URL. Enter an address that starts with https:.'],
|
|
125
|
+
urlNotHttps: ['https: で始まるアドレスだけを使えます。', 'Only addresses that start with https: can be used.'],
|
|
126
|
+
saveFailed: ['設定を保存できませんでした。ブラウザーの保存領域が使えません。', 'The settings could not be saved: browser storage is unavailable.']
|
|
127
|
+
};
|
|
128
|
+
export const agentText = (key, locale) => texts[key][isJapanese(locale) ? 0 : 1];
|
|
129
|
+
let openDialog = null;
|
|
130
|
+
let nextDialog = 0;
|
|
131
|
+
const focusable = 'button:not([disabled]), input:not([disabled]), [href], [tabindex]:not([tabindex="-1"])';
|
|
132
|
+
const urlProblems = { empty: 'urlEmpty', invalid: 'urlInvalid', 'not-https': 'urlNotHttps' };
|
|
133
|
+
/**
|
|
134
|
+
* The settings dialog: turns the presets on and off, replaces their address, and adds or removes the
|
|
135
|
+
* user's own services. It is plain DOM so that any part of the Editor can open it, with or without React.
|
|
136
|
+
*/
|
|
137
|
+
export function openAgentSettingsDialog({ document: doc = globalThis.document, locale, styles = {}, storage = browserStorage(), storageKey = agentSettingsKey, returnFocus, onSave }) {
|
|
138
|
+
if (openDialog?.element.isConnected) {
|
|
139
|
+
openDialog.element.querySelector(focusable)?.focus();
|
|
140
|
+
return openDialog;
|
|
141
|
+
}
|
|
142
|
+
const text = (key) => agentText(key, locale);
|
|
143
|
+
const restore = returnFocus ?? doc.activeElement;
|
|
144
|
+
const prefix = `plus-agent-settings-${++nextDialog}`;
|
|
145
|
+
let fieldId = 0;
|
|
146
|
+
const make = (tag, attributes = {}, ...children) => {
|
|
147
|
+
const node = doc.createElement(tag);
|
|
148
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
149
|
+
if (value !== undefined)
|
|
150
|
+
node.setAttribute(name, value);
|
|
151
|
+
}
|
|
152
|
+
for (const child of children)
|
|
153
|
+
if (child)
|
|
154
|
+
node.append(child);
|
|
155
|
+
return node;
|
|
156
|
+
};
|
|
157
|
+
const field = (label, input) => {
|
|
158
|
+
input.id = `${prefix}-field-${++fieldId}`;
|
|
159
|
+
const error = make('p', { id: `${input.id}-error`, class: styles.aiAgentDialogError, hidden: '' });
|
|
160
|
+
const wrapper = make('div', { class: input.type === 'checkbox' ? styles.aiAgentDialogCheck : styles.aiAgentDialogField }, ...(input.type === 'checkbox' ? [input, make('label', { for: input.id }, label)] :
|
|
161
|
+
[make('label', { for: input.id }, label), input]), error);
|
|
162
|
+
input.addEventListener('input', () => setError(input, null));
|
|
163
|
+
return wrapper;
|
|
164
|
+
};
|
|
165
|
+
const setError = (input, message) => {
|
|
166
|
+
const error = doc.getElementById(`${input.id}-error`);
|
|
167
|
+
if (error) {
|
|
168
|
+
error.textContent = message ?? '';
|
|
169
|
+
error.hidden = !message;
|
|
170
|
+
}
|
|
171
|
+
if (message) {
|
|
172
|
+
input.setAttribute('aria-invalid', 'true');
|
|
173
|
+
input.setAttribute('aria-describedby', `${input.id}-error`);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
input.removeAttribute('aria-invalid');
|
|
177
|
+
input.removeAttribute('aria-describedby');
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const checkbox = (checked) => {
|
|
181
|
+
const input = make('input', { type: 'checkbox' });
|
|
182
|
+
input.checked = checked;
|
|
183
|
+
return input;
|
|
184
|
+
};
|
|
185
|
+
const textInput = (type, value, placeholder) => {
|
|
186
|
+
const input = make('input', { type, spellcheck: 'false', autocomplete: 'off', placeholder });
|
|
187
|
+
input.value = value;
|
|
188
|
+
return input;
|
|
189
|
+
};
|
|
190
|
+
const settings = readAgentSettings(storage, storageKey);
|
|
191
|
+
const rows = [];
|
|
192
|
+
const presetGroup = make('fieldset', { class: styles.aiAgentDialogSection }, make('legend', {}, text('presets')));
|
|
193
|
+
for (const service of settings.services.filter(item => item.preset)) {
|
|
194
|
+
const preset = agentPresets.find(item => item.id === service.preset);
|
|
195
|
+
const enabled = checkbox(service.enabled);
|
|
196
|
+
const url = textInput('url', service.target.url === preset.url ? '' : service.target.url, preset.url);
|
|
197
|
+
const group = make('div', { role: 'group', 'aria-label': service.name, class: styles.aiAgentDialogRow }, field(`${service.name}${isJapanese(locale) ? ' を' : ': '}${text('show')}`, enabled), field(`${service.name} ${text('url')}`, url), make('p', { class: styles.aiAgentDialogNote }, `${text('defaultUrl')} ${preset.url}`));
|
|
198
|
+
rows.push({ service, group, enabled, url });
|
|
199
|
+
presetGroup.append(group);
|
|
200
|
+
}
|
|
201
|
+
const customList = make('div', { class: styles.aiAgentDialogList });
|
|
202
|
+
const customEmpty = make('p', { class: styles.aiAgentDialogNote }, text('customEmpty'));
|
|
203
|
+
const addButton = make('button', { type: 'button', class: styles.aiAgentDialogButton }, text('add'));
|
|
204
|
+
const custom = () => rows.filter(row => !row.service);
|
|
205
|
+
const renumber = () => {
|
|
206
|
+
custom().forEach((row, index) => row.group.setAttribute('aria-label', `${text('customService')} ${index + 1}`));
|
|
207
|
+
customEmpty.hidden = custom().length > 0;
|
|
208
|
+
};
|
|
209
|
+
const addRow = (service) => {
|
|
210
|
+
const enabled = checkbox(service?.enabled ?? true);
|
|
211
|
+
const name = textInput('text', service?.name ?? '');
|
|
212
|
+
const url = textInput('url', service?.target.url ?? '', 'https://');
|
|
213
|
+
const remove = make('button', { type: 'button', class: styles.aiAgentDialogButton }, text('remove'));
|
|
214
|
+
const group = make('div', { role: 'group', class: styles.aiAgentDialogRow }, field(text('name'), name), field(text('url'), url), field(text('show'), enabled), remove);
|
|
215
|
+
const row = { group, enabled, name, url };
|
|
216
|
+
remove.addEventListener('click', () => {
|
|
217
|
+
const index = custom().indexOf(row);
|
|
218
|
+
rows.splice(rows.indexOf(row), 1);
|
|
219
|
+
group.remove();
|
|
220
|
+
renumber();
|
|
221
|
+
(custom()[index]?.name ?? custom()[index - 1]?.name ?? addButton).focus();
|
|
222
|
+
});
|
|
223
|
+
rows.push(row);
|
|
224
|
+
customList.append(group);
|
|
225
|
+
renumber();
|
|
226
|
+
return row;
|
|
227
|
+
};
|
|
228
|
+
for (const service of settings.services.filter(item => !item.preset))
|
|
229
|
+
addRow(service);
|
|
230
|
+
addButton.addEventListener('click', () => addRow().name.focus());
|
|
231
|
+
const customGroup = make('fieldset', { class: styles.aiAgentDialogSection }, make('legend', {}, text('custom')), customList, customEmpty, addButton);
|
|
232
|
+
const status = make('p', { role: 'alert', class: styles.aiAgentDialogError, hidden: '' });
|
|
233
|
+
const cancel = make('button', { type: 'button', class: styles.aiAgentDialogButton }, text('cancel'));
|
|
234
|
+
const save = make('button', { type: 'submit', class: `${styles.aiAgentDialogButton ?? ''} ${styles.aiAgentDialogPrimary ?? ''}`.trim() }, text('save'));
|
|
235
|
+
const form = make('form', { novalidate: '', class: styles.aiAgentDialogForm }, presetGroup, customGroup, status, make('div', { class: styles.aiAgentDialogActions }, cancel, save));
|
|
236
|
+
const element = make('div', {
|
|
237
|
+
role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': `${prefix}-title`,
|
|
238
|
+
'aria-describedby': `${prefix}-description`, class: styles.aiAgentDialog
|
|
239
|
+
}, make('h2', { id: `${prefix}-title` }, text('title')), make('p', { id: `${prefix}-description` }, text('description')), form);
|
|
240
|
+
const backdrop = make('div', { class: styles.aiAgentDialogBackdrop }, element);
|
|
241
|
+
const close = () => {
|
|
242
|
+
if (!backdrop.isConnected)
|
|
243
|
+
return;
|
|
244
|
+
backdrop.remove();
|
|
245
|
+
if (openDialog?.element === element)
|
|
246
|
+
openDialog = null;
|
|
247
|
+
if (restore?.isConnected)
|
|
248
|
+
restore.focus({ preventScroll: true });
|
|
249
|
+
};
|
|
250
|
+
cancel.addEventListener('click', close);
|
|
251
|
+
element.addEventListener('keydown', event => {
|
|
252
|
+
if (event.key === 'Escape') {
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
event.stopPropagation();
|
|
255
|
+
close();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (event.key !== 'Tab')
|
|
259
|
+
return;
|
|
260
|
+
const items = [...element.querySelectorAll(focusable)];
|
|
261
|
+
const first = items[0];
|
|
262
|
+
const last = items[items.length - 1];
|
|
263
|
+
if (event.shiftKey && doc.activeElement === first) {
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
last.focus();
|
|
266
|
+
}
|
|
267
|
+
else if (!event.shiftKey && doc.activeElement === last) {
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
first.focus();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
form.addEventListener('submit', event => {
|
|
273
|
+
event.preventDefault();
|
|
274
|
+
status.hidden = true;
|
|
275
|
+
const invalid = [];
|
|
276
|
+
const services = [];
|
|
277
|
+
for (const row of rows) {
|
|
278
|
+
const preset = row.service && agentPresets.find(item => item.id === row.service.preset);
|
|
279
|
+
const checked = preset && !row.url.value.trim() ? { url: preset.url } : checkAgentUrl(row.url.value);
|
|
280
|
+
const problem = 'problem' in checked ? text(urlProblems[checked.problem]) : null;
|
|
281
|
+
setError(row.url, problem);
|
|
282
|
+
const name = row.name?.value.trim() ?? row.service.name;
|
|
283
|
+
if (row.name)
|
|
284
|
+
setError(row.name, name ? null : text('nameEmpty'));
|
|
285
|
+
if (row.name && !name)
|
|
286
|
+
invalid.push(row.name);
|
|
287
|
+
if (problem)
|
|
288
|
+
invalid.push(row.url);
|
|
289
|
+
if (!('url' in checked) || !name)
|
|
290
|
+
continue;
|
|
291
|
+
services.push(preset ? { ...row.service, enabled: row.enabled.checked, target: { kind: 'url', url: checked.url } } : {
|
|
292
|
+
id: '', name, enabled: row.enabled.checked, target: { kind: 'url', url: checked.url }
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
if (invalid.length) {
|
|
296
|
+
invalid[0].focus();
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
let count = 0;
|
|
300
|
+
for (const service of services)
|
|
301
|
+
if (!service.preset)
|
|
302
|
+
service.id = `custom-${++count}`;
|
|
303
|
+
const saved = { version: 1, services };
|
|
304
|
+
if (!writeAgentSettings(saved, storage, storageKey)) {
|
|
305
|
+
status.textContent = text('saveFailed');
|
|
306
|
+
status.hidden = false;
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
close();
|
|
310
|
+
onSave?.(saved);
|
|
311
|
+
});
|
|
312
|
+
doc.body.append(backdrop);
|
|
313
|
+
rows[0]?.enabled.focus();
|
|
314
|
+
openDialog = { element, close };
|
|
315
|
+
return openDialog;
|
|
316
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DrawerMenu } from './drawer-views.ts';
|
|
2
|
+
import { type AgentService, type AgentStorage, type Opener } from './ai-agent-settings.ts';
|
|
3
|
+
export * from './ai-agent-settings.ts';
|
|
4
|
+
export interface AiAgentsOptions {
|
|
5
|
+
/** The workbench stylesheet's class names (`editor.css`), for the menu and the settings dialog. */
|
|
6
|
+
styles?: Record<string, string>;
|
|
7
|
+
storage?: AgentStorage | null;
|
|
8
|
+
storageKey?: string;
|
|
9
|
+
/** Opens a service; defaults to a new browser tab. */
|
|
10
|
+
open?: Opener;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The AI agent menu for `drawerViews.agents`, the settings dialog and the icon the host shows beside
|
|
14
|
+
* its own entry for the dialog. Every part reads the saved settings when it opens.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createAiAgents(React: any, { styles, storage, storageKey, open }?: AiAgentsOptions): {
|
|
17
|
+
drawerMenu: DrawerMenu;
|
|
18
|
+
openSettings: ({ locale, returnFocus }: {
|
|
19
|
+
locale: string;
|
|
20
|
+
returnFocus?: HTMLElement | null;
|
|
21
|
+
}) => import("./ai-agent-settings.ts").AgentSettingsDialog;
|
|
22
|
+
settingsIcon: () => any;
|
|
23
|
+
serviceIcon: (service: AgentService) => any;
|
|
24
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { agentsIconPath } from "./drawer-views.js";
|
|
2
|
+
import { agentPresets, agentSettingsKey, agentText, browserStorage, enabledAgentServices, openAgentService, openAgentSettingsDialog, readAgentSettings } from "./ai-agent-settings.js";
|
|
3
|
+
export * from "./ai-agent-settings.js";
|
|
4
|
+
/**
|
|
5
|
+
* The AI agent menu for `drawerViews.agents`, the settings dialog and the icon the host shows beside
|
|
6
|
+
* its own entry for the dialog. Every part reads the saved settings when it opens.
|
|
7
|
+
*/
|
|
8
|
+
export function createAiAgents(React, { styles = {}, storage = browserStorage(), storageKey = agentSettingsKey, open } = {}) {
|
|
9
|
+
const { createElement: element, useRef, useState } = React;
|
|
10
|
+
const svg = (...children) => element('svg', {
|
|
11
|
+
'aria-hidden': true, focusable: false, width: 16, height: 16, viewBox: '0 0 16 16',
|
|
12
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.3, strokeLinecap: 'round', strokeLinejoin: 'round'
|
|
13
|
+
}, ...children);
|
|
14
|
+
const bubble = 'M3 2.5h10a1.5 1.5 0 0 1 1.5 1.5v6a1.5 1.5 0 0 1-1.5 1.5H7l-3 2.5v-2.5H3A1.5 1.5 0 0 1 1.5 10V4A1.5 1.5 0 0 1 3 2.5Z';
|
|
15
|
+
// The initials are drawn as strokes so they do not depend on a font or add to the item's text.
|
|
16
|
+
const initials = {
|
|
17
|
+
C: 'M9.9 5.6A2.4 2.4 0 1 0 9.9 8.4',
|
|
18
|
+
G: 'M9.9 5.6A2.4 2.4 0 1 0 10.4 7H8.4'
|
|
19
|
+
};
|
|
20
|
+
/** A plain chat bubble with the service's initial for a preset; a globe for the user's own address. */
|
|
21
|
+
const serviceIcon = (service) => {
|
|
22
|
+
const preset = agentPresets.find(item => item.id === service.preset);
|
|
23
|
+
if (preset) {
|
|
24
|
+
const svgElement = svg(element('path', { d: bubble }), element('path', { d: initials[preset.letter] }));
|
|
25
|
+
return React.cloneElement(svgElement, { 'data-initial': preset.letter });
|
|
26
|
+
}
|
|
27
|
+
return svg(element('circle', { cx: 8, cy: 8, r: 6.25 }), element('path', { d: 'M1.75 8h12.5M8 1.75c-3 3.4-3 9.1 0 12.5M8 1.75c3 3.4 3 9.1 0 12.5' }));
|
|
28
|
+
};
|
|
29
|
+
const settingsIcon = () => svg(element('path', { d: agentsIconPath }));
|
|
30
|
+
const openSettings = ({ locale, returnFocus }) => openAgentSettingsDialog({ document, locale, styles, storage, storageKey, returnFocus });
|
|
31
|
+
function AgentMenu({ locale, close }) {
|
|
32
|
+
const [services] = useState(() => enabledAgentServices(readAgentSettings(storage, storageKey)));
|
|
33
|
+
const list = useRef(null);
|
|
34
|
+
const hintId = useRef(null);
|
|
35
|
+
if (!hintId.current)
|
|
36
|
+
hintId.current = `plus-agent-menu-hint-${++nextId}`;
|
|
37
|
+
const label = agentText('menu', locale);
|
|
38
|
+
const newTab = agentText('newTab', locale);
|
|
39
|
+
const move = event => {
|
|
40
|
+
const items = [...list.current.querySelectorAll('[role="menuitem"]')];
|
|
41
|
+
const index = items.indexOf(event.target);
|
|
42
|
+
let next;
|
|
43
|
+
if (event.key === 'ArrowDown')
|
|
44
|
+
next = (index + 1) % items.length;
|
|
45
|
+
else if (event.key === 'ArrowUp')
|
|
46
|
+
next = (index - 1 + items.length) % items.length;
|
|
47
|
+
else if (event.key === 'Home')
|
|
48
|
+
next = 0;
|
|
49
|
+
else if (event.key === 'End')
|
|
50
|
+
next = items.length - 1;
|
|
51
|
+
else
|
|
52
|
+
return;
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
items[next]?.focus();
|
|
55
|
+
};
|
|
56
|
+
const item = (key, icon, text, onClick, title) => element('button', {
|
|
57
|
+
key, type: 'button', role: 'menuitem', tabIndex: -1, className: styles.aiAgentMenuItem, title, onClick
|
|
58
|
+
}, icon, element('span', null, text));
|
|
59
|
+
return element(React.Fragment, null, !services.length && element('p', { id: hintId.current, className: styles.aiAgentMenuHint }, agentText('empty', locale)), element('div', {
|
|
60
|
+
role: 'menu', 'aria-label': label, ref: list, className: styles.aiAgentMenu, onKeyDown: move,
|
|
61
|
+
'aria-describedby': services.length ? undefined : hintId.current
|
|
62
|
+
}, services.map(service => item(service.id, serviceIcon(service), service.name, () => {
|
|
63
|
+
openAgentService(service, open);
|
|
64
|
+
close();
|
|
65
|
+
}, newTab)), services.length > 0 && element('div', { role: 'separator', className: styles.aiAgentMenuSeparator }), item('settings', settingsIcon(), agentText('settings', locale), () => {
|
|
66
|
+
close();
|
|
67
|
+
openSettings({ locale, returnFocus: document.activeElement });
|
|
68
|
+
})));
|
|
69
|
+
}
|
|
70
|
+
const drawerMenu = {
|
|
71
|
+
renderMenu: props => element(AgentMenu, props)
|
|
72
|
+
};
|
|
73
|
+
return { drawerMenu, openSettings, settingsIcon, serviceIcon };
|
|
74
|
+
}
|
|
75
|
+
let nextId = 0;
|