diff --git a/README.md b/README.md index 9c69cd9..a3e336d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ 3. 携带标签一起搜索(目前仅支持单个标签) 快捷键设置: -Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: Open Search` 设置 `` 快捷键,就可以平替默认的 `快速切换:打开快速切换` 面板。 +Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: 搜索文件` 设置 `` 快捷键,就可以平替默认的 `快速切换:打开快速切换` 面板。 在文件 frontmatter 中设置 `linkText` 字段,可以将标题或者 block id 加入搜索(与 `alias` 不同的是,可以直接跳转到对应标题或块)。如 @@ -38,7 +38,7 @@ linkText: ### 文件移动 -命令 `Fuzzy Chinese Pinyin: Move File`,效果类似命令 `将文件移动到其他文件夹`。 +命令 `Fuzzy Chinese Pinyin: 移动文件`,效果类似命令 `将文件移动到其他文件夹`。 在文件列表中选择文件后,右键可一起移动,若设置了快捷键也可以快捷键打开,优先级大于当前页面文件。 特性: @@ -48,7 +48,7 @@ linkText: ### 命令搜索 -命令 `Fuzzy Chinese Pinyin: Execute Command`,效果类似命令 `命令面板: 打开命令面板`。 +命令 `Fuzzy Chinese Pinyin: 执行命令`,效果类似命令 `命令面板: 打开命令面板`。 快捷键设置: Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: Execute Command` 设置 `` 快捷键,就可以平替默认的 `命令面板:打开命令面板`。 @@ -62,7 +62,7 @@ Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: ### 当前文档标题搜索 -命令 `Fuzzy Chinese Pinyin: Search Heading` +命令 `Fuzzy Chinese Pinyin: 搜索标题` 对当前文档标题进行拼音搜索。 特性: @@ -70,6 +70,11 @@ Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: 1. 隐藏第一级标题 2. 对标题进行缩进 +### 插入模版 + +命令 `Fuzzy Chinese Pinyin: 插入模版`,效果类似命令 `模板:插入模板`。 +与核心插件模板功能相同,仅加入拼音搜索功能。 + ### 双链 选项设置: diff --git a/src/main.ts b/src/main.ts index 42e153c..b5ebb04 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,7 @@ import FolderModal from "@/modal/folderModal"; import CommandModal from "@/modal/commandModal"; import FuzzySuggestModal from "@/modal/suggestModal"; import HeadingModal from "@/modal/headingModal"; +import TemplatesModal from "@/modal/templatesModal"; import FileEditorSuggest from "@/editorSuggest/fileEditorSuggest"; import TagEditorSuggest from "@/editorSuggest/tagEditorSuggest"; // 以下两个字典来源于:https://github.com/xmflswood/pinyin-match @@ -40,6 +41,7 @@ export default class ThePlugin extends Plugin { folderModal: FolderModal; commandModal: CommandModal; headingModal: HeadingModal; + templatesModal: TemplatesModal; fileEditorSuggest: FileEditorSuggest; tagEditorSuggest: TagEditorSuggest; indexManager: IndexManager; @@ -54,6 +56,8 @@ export default class ThePlugin extends Plugin { this.folderModal = new FolderModal(this.app, this); this.commandModal = new CommandModal(this.app, this); this.headingModal = new HeadingModal(this.app, this); + this.templatesModal = new TemplatesModal(this.app, this); + this.fileEditorSuggest = new FileEditorSuggest(this.app, this); this.tagEditorSuggest = new TagEditorSuggest(this.app, this); @@ -114,14 +118,14 @@ export default class ThePlugin extends Plugin { addCommands() { this.addCommand({ id: "open-search", - name: "Open Search", + name: "搜索文件", callback: () => { this.fileModal.open(); }, }); this.addCommand({ id: "move-file", - name: "Move File", + name: "移动文件", checkCallback: (checking: boolean) => { let files = this.fileExplorerHotkey.getFiles(); let file = this.app.workspace.getActiveFile(); @@ -133,14 +137,14 @@ export default class ThePlugin extends Plugin { }); this.addCommand({ id: "execute-command", - name: "Execute Command", + name: "执行命令", callback: () => { this.commandModal.open(); }, }); this.addCommand({ id: "search-heading", - name: "Search Heading", + name: "搜索标题", checkCallback: (checking: boolean) => { const file = this.app.workspace.getActiveFile(); if (checking) return Boolean(file); @@ -148,6 +152,13 @@ export default class ThePlugin extends Plugin { this.headingModal.open(); }, }); + this.addCommand({ + id: "insert-templates", + name: "插入模板", + callback: () => { + this.templatesModal.open(); + }, + }); } onunload() { if (this.settings.other.devMode) { diff --git a/src/modal/templatesModal.ts b/src/modal/templatesModal.ts new file mode 100644 index 0000000..075e856 --- /dev/null +++ b/src/modal/templatesModal.ts @@ -0,0 +1,49 @@ +import { App, TFile } from "obsidian"; +import FuzzyModal from "./modal"; +import { MatchData, Pinyin, runOnLayoutReady, Item as uItem } from "@/utils"; +import ThePlugin from "@/main"; + +interface Item extends uItem { + file: TFile; +} + +export default class TemplatesModal extends FuzzyModal { + constructor(app: App, plugin: ThePlugin) { + super(app, plugin); + this.index = {} as any; + runOnLayoutReady(() => { + this.updateIndex(); + }); + } + onOpen(): void { + super.onOpen(); + this.updateIndex(); + } + getEmptyInputSuggestions(): MatchData[] { + return this.index.items.map((p) => ({ + item: p, + score: -1, + range: null, + })); + } + onChooseSuggestion(matchData: MatchData, evt: MouseEvent | KeyboardEvent): void { + let plugin = this.getOriginPlugin(); + plugin.insertTemplate(matchData.item.file); + } + getOriginPlugin() { + return this.app.internalPlugins.plugins.templates.instance; + } + updateIndex(): void { + let plugin = this.getOriginPlugin(); + let folder = plugin.options.folder; + let templateFiles = this.app.vault + .getFiles() + .filter((file) => file.parent.path === folder && file.extension === "md"); + + this.index.items = templateFiles.map((file: TFile) => ({ + name: file.basename, + pinyin: new Pinyin(file.basename, this.plugin), + file, + })); + } +}