读取、设置与监听宿主的“搜索框内容”,并在 UI 环境为插件页面注册底部 Footer 操作区。
搜索框内容(UI 环境)
ts
import { Context } from '@sofastapp/api';
// 读取搜索框内容(字符串)
const text = await Context.getSearchContent();
// 设置搜索框内容
await Context.setSearchContent('Hello Sofast');
// 清空搜索框内容
await Context.clearSearchContent();
// 监听搜索框内容变化,返回取消函数
const unwatch = Context.watchSearchContent((val: string) => {
console.log('context =', val);
});
// 取消监听
unwatch();说明
setSearchContent、clearSearchContent、getSearchContent、watchSearchContent仅在 UI(iframe)环境可用。- No-View/Node 环境仅支持:
getSearchContent/clearSearchContent/watchSearchContent。
底部 Footer(UI 环境)
为插件页面注册底部操作区按钮,支持普通按钮与 Action Panel(展开菜单)。
ts
import { Context } from '@sofastapp/api';
// 1) 单个按钮:点击或按下快捷键触发 onClick
await Context.setFooter({
type: 'button',
id: 'submit',
label: '提交',
icon: 'Check', // 可选,lucide 图标名
keys: ['Enter'],
onClick: async () => {
// 你的处理逻辑
console.log('Submit clicked');
},
});
// 2) Action Panel:点击或按下快捷键展开菜单,再选择条目触发 onSelect
await Context.setFooter({
type: 'action-panel',
id: 'more',
label: '更多操作',
keys: ['Mod+K'],
title: '选择一个操作',
items: [
{
id: 'create',
name: '新建',
icon: 'Plus',
color: '#10b981',
keys: ['N'], // 作为快捷键与展示提示
onSelect: () => console.log('新建'),
},
{
id: 'remove',
name: '删除',
icon: 'Trash2',
color: '#ef4444',
onSelect: async () => console.log('删除'),
},
],
});
// 3) 传入多个按钮
await Context.setFooter([
{ type: 'button', label: '保存', icon: 'Save', onClick: () => {} },
{ type: 'action-panel', label: '更多', items: [{ name: '设置', onSelect: () => {} }] },
]);类型约定(仅供参考):
ts
type FooterButtonSpec =
| {
type: 'button';
id?: string;
label?: string;
icon?: string;
keys?: string[]; // 支持提示与触发
onClick: () => void | Promise<void>;
}
| {
type: 'action-panel';
id?: string;
label?: string;
keys?: string[]; // 支持提示与触发(打开/关闭面板)
title?: string;
items: Array<{
id?: string;
name: string;
color?: string; // 例如 '#ef4444' 或 'red'
icon?: string; // lucide 图标名,如 'Plus' | 'Trash2'
keys?: string[]; // 支持快捷键触发与展示提示
onSelect?: () => void | Promise<void>;
}>;
};说明
Context.setFooter仅在 UI(iframe)环境可用。- 传入的回调函数会被安全包装并由宿主侧调用,无需自行处理跨窗口通信。
- 按钮级别
keys用于触发按钮或打开/关闭 Action Panel。items[*].keys会绑定快捷键行为:
- 面板未打开时也可直接触发对应项
onSelect。- 面板已打开时,仅带修饰键(Ctrl/Cmd/Alt/Shift)的组合会触发;无修饰键时由列表内部处理。