Skip to content

原生utools开发类型定义

如果开发一个无UI、列表模式、文档模式的插件,将此定义下载,在根目录创建文件index.d.ts,将内容粘贴进去,就有语法提示了

index.d.ts

typescript
declare interface ExportByNone {
    mode: 'none';
    args: ExportNoneArg
}

declare interface ExportByList {
    mode: 'list';
    args: ExportListArg
}

declare interface ExportByDoc {
    mode: 'doc';
    args: ExportDocArg
}


/**
 * 文件或文件夹
 */
declare interface ActionCmdFile {
    isDirectory: boolean;
    isFile: boolean;
    name: string;
    path: string;
}

declare interface ExportArgActionText {
    code: string;
    type: "text" | "regex";
    payload: string;
}

declare interface ExportArgActionFile {
    code: string;
    type: 'files';
    payload: Array<ActionCmdFile>;
}

declare type ExportArgAction = ExportArgActionText | ExportArgActionFile;

declare interface ExportArgList {
    title: string,
    description: string,
    icon?: string // 图标(可选)
    [key: string]: any;
}

declare type CallbackSetList = (list: Array<ExportArgList>) => void;

declare interface ExportNoneArg {
    enter(action: ExportArgAction): void;

    select(action: ExportArgAction, itemData: ExportArgList, callbackSetList: CallbackSetList): void
}

declare interface ExportListArg {

    /**
     * 进入时触发
     * @param action 触发的动作
     * @param callbackSetList 列表回调
     */
    enter?(action: ExportArgAction, callbackSetList: CallbackSetList): void;

    /**
     * 搜索时触发
     * @param action 触发的动作
     * @param searchWord 搜索关键字
     * @param callbackSetList 列表回调
     */
    search?(action: ExportArgAction, searchWord: string, callbackSetList: CallbackSetList): void;

    /**
     * 选择时触发
     *
     * @param action 触发的动作
     * @param itemData 选中项
     * @param callbackSetList 列表回调
     */
    select?(action: ExportArgAction, itemData: ExportArgList, callbackSetList: CallbackSetList): void;

    /**
     * 搜索框提示词
     * @default 搜索
     */
    placeholder?: string;
}

declare interface ExportDocArgItem {

    /**
     * 标题
     */
    t: string;

    /**
     * 描述
     */
    d: string;

    /**
     * 页面
     */
    p: string;
}

declare interface ExportDocArg {
    indexes: Array<ExportDocArgItem>,
    /**
     * 子输入框为空时的占位符,默认为字符串"搜索"
     * @default 搜索
     */
    placeholder: string
}

declare type Export = Record<string, ExportByNone | ExportByDoc | ExportByList>

interface Window {
    exports: Export
}