Skip to content

子输入框 ​

把顶栏的搜索框变成插件自己的输入框,并可以加前缀标签、参数框和下拉。不需要权限,只能在插件主页面调用。

用法与界面说明见指南 子输入框。

子输入框 ​

方法说明
setSubInput(onChange: ({ text }) => void, placeholder?: string, isFocus?: boolean): Promise<boolean>显示子输入框。用户输入或调用 setSubInputValue 时调用 onChange。再次调用替换回调与占位文字、清空内容
removeSubInput(): Promise<boolean>移除子输入框,恢复显示插件标题;参数框、前缀、下拉一起移除
setSubInputValue(text: string): Promise<boolean>替换内容,同样触发 onChange
subInputFocus(): Promise<boolean>把焦点交给子输入框
subInputBlur(): Promise<boolean>把焦点交还插件页面
subInputSelect(): Promise<boolean>聚焦并全选内容

焦点在子输入框时,↑ ↓ Enter 和带修饰键的组合会以模拟的 keydown(isTrusted 为 false)发到插件页面的 document 上;← → 先交给插件,插件 preventDefault() 时不移动光标。

下拉附件 ​

ts
plugin.setSubInputAccessory(
  accessory: SubInputAccessory,
  onChange: ({ value }) => void,
  onSearch?: ({ query }) => void,
): Promise<boolean>

SubInputAccessory:

字段说明
type固定 'select'
options选项 { value, label, color? }[],1–200 项;value、label 最多 64 个字符,value 不重复。color 可选 green blue orange red purple cyan yellow gray
value当前值,必须是某个选项的 value
searchable弹层顶部显示搜索框
searchPlaceholder搜索框占位文字
searchMode'host'(默认)由 lingtools 按文字过滤;'plugin' 把搜索词交给 onSearch,插件回填结果
方法说明
plugin.setSubInputAccessoryOptions(options, query?): Promise<boolean>searchMode: 'plugin' 时回填搜索结果,只影响这次打开的弹层。传入 query 时,过时的回复不会覆盖新结果
plugin.removeSubInputAccessory(): Promise<boolean>移除下拉
plugin.showSubInputAccessory(): Promise<boolean>打开下拉弹层(插件页面里按 ⌘ P 也会打开)

没有子输入框时,下拉显示在插件标题后面。

参数框 ​

ts
plugin.setSubInputArguments(
  args: { name: string; placeholder: string; value?: string; invalid?: boolean }[],
  onChange: ({ name, values }) => void,
): Promise<boolean>

1–3 个参数框,显示在子输入框与下拉之间。name 唯一、最多 32 个字符;placeholder 最多 64 个字符;value 最多 1000 个字符;invalid: true 时标红。onChange 收到刚编辑的参数名和全部值。必须先有子输入框,否则返回 false。

方法说明
plugin.removeSubInputArguments(): Promise<boolean>移除参数框
plugin.focusSubInputArgument(name: string): Promise<boolean>把焦点移到某个参数框并全选

前缀标签 ​

方法说明
plugin.setSubInputPrefix({ label, detail?, icon?, thumbnail?, removable? }, onRemove?): Promise<boolean>在子输入框左边显示标签。必须先有子输入框,否则返回 false
plugin.removeSubInputPrefix(): Promise<boolean>移除前缀

前缀标签的字段:

字段说明
label文字,非空,最多 64 个字符
detail文字后的灰字(尺寸、个数等),最多 32 个字符(0.10.0 起)
icon'folder';0.10.0 起还有 'image' 'file' 'files' 'text'。与 thumbnail 同时给时只画缩略图
thumbnail缩略图,data:image/png|jpeg|webp;base64,…,不超过 64 KiB、单边不超过 1024 像素(0.10.0 起)
removable为 true 时右边带 ×(0.10.0 起)。用户点 ×,或子输入框为空时按退格,标签被移除并调用 onRemove

onRemove 只调一次;再次设置前缀、removeSubInputPrefix、removeSubInput 后作废。

参数不合法时以上方法以 invalid_params 失败。