Skip to content

Commit

Permalink
feat: api
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Nov 30, 2023
1 parent f761fb9 commit da8150a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"author": "lazyloong",

"minAppVersion": "1.0.0",
"version": "2.8.2"
"version": "2.9.1"
}
15 changes: 6 additions & 9 deletions src/fuzzyCommandModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FuzzyChinesePinyinPlugin from "./main";

type Item = {
name: string;
pinyin: Pinyin<Item>;
pinyin: Pinyin;
command: any;
};

Expand Down Expand Up @@ -126,14 +126,11 @@ class PinyinIndex extends PI<Item> {
initEvent() {}
initIndex() {
let commands = app.commands.listCommands();
this.items = commands.map((command) => {
let item = {
name: command.name,
pinyin: new Pinyin(command.name, this.plugin),
command: command,
};
return item;
});
this.items = commands.map((command) => ({
name: command.name,
pinyin: new Pinyin(command.name, this.plugin),
command: command,
}));
}
update() {
let commands = app.commands.listCommands();
Expand Down
2 changes: 1 addition & 1 deletion src/fuzzyFolderModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PinyinIndex extends PI<Item> {
}
initIndex() {
let root = app.vault.getRoot();
let iterate = (node: TFolder, nodePinyin: Pinyin<Item>) => {
let iterate = (node: TFolder, nodePinyin: Pinyin) => {
let children = node.children;
for (let child of children) {
if (child instanceof TFolder) {
Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, Notice, Plugin, PluginSettingTab, Setting, EditorSuggest } from "obsidian";
import { Pinyin, PinyinIndex, runOnLayoutReady } from "./utils";
import { Item, PinyinIndex, runOnLayoutReady } from "./utils";
import FuzzyModal from "./fuzzyModal";
import FuzzyFileModal from "./fuzzyFileModal";
import FuzzyFolderModal from "./fuzzyFolderModal";
Expand All @@ -12,6 +12,7 @@ import SimplifiedDict from "./simplified_dict";
import TraditionalDict from "./traditional_dict";

import DoublePinyinDict from "./double_pinyin";
import { fuzzyPinyinSearch, stringArray2Items } from "./search";

interface FuzyyChinesePinyinSettings {
global: {
Expand Down Expand Up @@ -181,7 +182,11 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
return false;
});
this.addSettingTab(new SettingTab(this.app, this));
this.api = { suggester: this.suggester };
this.api = {
suggester: this.suggester,
search: (query: string, items: string[] | Item[]) => fuzzyPinyinSearch(query, items, this),
stringArray2Items: stringArray2Items,
};
}
onunload() {
this.editorSuggests.forEach((editorSuggest) => this.app.workspace.editorSuggest.removeSuggest(editorSuggest));
Expand Down
41 changes: 41 additions & 0 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import FuzzyChinesePinyinPlugin from "./main";
import { Pinyin, Item, MatchData } from "./utils";

export function fuzzyPinyinSearch(query: string, items: string[] | Item[], plugin: FuzzyChinesePinyinPlugin): MatchData<Item>[] {
if (items.length == 0) return null;
if (isStringArray(items)) {
items = stringArray2Items(items, plugin);
}
return search(query, items);
}

function isStringArray(arr: string[] | Item[]): arr is string[] {
return typeof arr[0] == "string";
}

function search(query: string, items: Item[]): MatchData<Item>[] {
if (query == "") {
return items.map((p) => ({
item: p,
score: 0,
range: [[0, p.name.length - 1]],
}));
}

let matchData: MatchData<Item>[] = [];
let smathCase = /[A-Z]/.test(query);
for (let p of items) {
let d = p.pinyin.match(query, p, smathCase);
if (d) matchData.push(d);
}

matchData = matchData.sort((a, b) => b.score - a.score);
return matchData;
}

export function stringArray2Items(items: string[], plugin: FuzzyChinesePinyinPlugin): Item[] {
return items.map((item) => ({
name: item,
pinyin: new Pinyin(item, plugin),
}));
}

0 comments on commit da8150a

Please sign in to comment.