Skip to content

Commit

Permalink
feat: helper 新增输入防抖,iconfont 修复配置无法修改的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mohuishou committed Jun 21, 2021
1 parent debbbde commit bb4fb6f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 25 deletions.
19 changes: 18 additions & 1 deletion helper/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "utools-helper",
"version": "2.0.0",
"version": "2.1.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -27,8 +27,12 @@
"url": "https://github.com/mohuishou/utools.git"
},
"devDependencies": {
"@types/node": "^15.0.1",
"@types/node": "^15.12.4",
"@types/throttle-debounce": "^2.1.0",
"typescript": "^4.2.4",
"utools-api-types": "^2.0.3"
},
"dependencies": {
"throttle-debounce": "^3.0.1"
}
}
20 changes: 18 additions & 2 deletions helper/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import {
import { IListItem } from "./listItem";
import { ErrorIcon } from "./icon";
import { Setting } from "./config";
import { debounce } from "throttle-debounce";

export interface Plugin {
code: string;
mode?: TplFeatureMode;
placeholder?: string;

/**
* 节流延迟,默认 500ms,单位 ms
*/
delay?: number;

/**
* 是否在 select 结束之后退出插件
*/
Expand Down Expand Up @@ -44,6 +50,8 @@ export interface Plugin {
class Feature implements TplFeature {
plugin: Plugin;
mode: TplFeatureMode = "list";
delay: number = 500;
search: any;

args: TplFeatureArgs = {
placeholder: "请输入关键词查询",
Expand All @@ -65,8 +73,7 @@ class Feature implements TplFeature {
search: async (action, word, cb) => {
try {
if (!this.plugin.search) return;
let items = await this.plugin.search(word, action);
if (items) return cb(items);
await this.search(word, action, cb);
if (this.plugin.outPlugin) utools.outPlugin();
if (this.plugin.hideMainWindow) utools.hideMainWindow();
} catch (error) {
Expand Down Expand Up @@ -107,6 +114,15 @@ class Feature implements TplFeature {
this.plugin = plugin;
if (plugin.mode) this.mode = plugin.mode;
if (plugin.placeholder) this.args.placeholder = plugin.placeholder;
if (plugin.delay) this.delay = plugin.delay;
this.search = debounce(
this.delay,
false,
async (word: string, action: Action, cb: CallbackSetList) => {
let items = await this.plugin.search(word, action);
if (items) return cb(items);
}
);
}
}

Expand Down
32 changes: 25 additions & 7 deletions plugins/iconfont/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion plugins/iconfont/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"@types/mousetrap": "^1.6.3",
"axios": "^0.21.1",
"mousetrap": "^1.6.5",
"utools-helper": "^1.0.0"
"utools-helper": "^2.1.0"
},
"devDependencies": {
"@types/node": "^15.12.4",
"@types/throttle-debounce": "^2.1.0",
"utools-api-types": "^2.0.3"
}
}
6 changes: 3 additions & 3 deletions plugins/iconfont/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const config: IConfigItem[] = [
name: "featured",
label: "是否精选",
type: "select",
required: true,
required: false,
default: "",
options: [
{ label: "所有图标", value: "" },
Expand All @@ -16,7 +16,7 @@ export const config: IConfigItem[] = [
name: "color",
label: "颜色筛选",
type: "select",
required: true,
required: false,
default: "1",
options: [
{ label: "单色", value: "0" },
Expand All @@ -28,7 +28,7 @@ export const config: IConfigItem[] = [
label: "图标类型",
type: "select",
default: "",
required: true,
required: false,
options: [
{ label: "全部", value: "" },
{ label: "线性", value: "line" },
Expand Down
4 changes: 0 additions & 4 deletions plugins/iconfont/src/iconfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { stringify } from "querystring";
import { writeFileSync } from "fs";
import { join } from "path";
import { nativeImage } from "electron";

interface keywordParams {
[index: string]: { key: string; val: any };
}
Expand Down Expand Up @@ -56,7 +55,6 @@ export class Iconfont implements Plugin {

async search(keyword: string): Promise<ListItem[]> {
let words = keyword.trim().split(/\s+/g);
console.log(keyword, words);

const r = await this.request.post("api/icon/search.json", stringify(this.params(words)), {
headers: {
Expand All @@ -65,13 +63,11 @@ export class Iconfont implements Plugin {
});

let icons = r.data.data.icons.map((icon: any) => {
console.log("替换前: " + icon.show_svg);
if (Setting.Get("fill_color")) {
icon.show_svg = icon.show_svg.replace(
/fill="#.*?"/gim,
`fill="${Setting.Get("fill_color")}"`
);
console.log("替换后: " + icon.show_svg);
}

return {
Expand Down
6 changes: 1 addition & 5 deletions plugins/iconfont/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
"module": "commonjs",
"target": "ES2018",
"experimentalDecorators": true,
"types": [
"utools-helper/@types/utools",
"utools-helper/@types/electron",
"@types/node"
]
"types": ["utools-api-types", "@types/node"]
}
}

0 comments on commit bb4fb6f

Please sign in to comment.