Skip to content

Commit

Permalink
feat: 增加功能插入模板 #58
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Sep 19, 2024
1 parent f5847a1 commit 27efebe
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
3. 携带标签一起搜索(目前仅支持单个标签)

快捷键设置:
Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: Open Search` 设置 `<Cmd+o>` 快捷键,就可以平替默认的 `快速切换:打开快速切换` 面板。
Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: 搜索文件` 设置 `<Cmd+o>` 快捷键,就可以平替默认的 `快速切换:打开快速切换` 面板。

在文件 frontmatter 中设置 `linkText` 字段,可以将标题或者 block id 加入搜索(与 `alias` 不同的是,可以直接跳转到对应标题或块)。如

Expand All @@ -38,7 +38,7 @@ linkText:

### 文件移动

命令 `Fuzzy Chinese Pinyin: Move File`,效果类似命令 `将文件移动到其他文件夹`
命令 `Fuzzy Chinese Pinyin: 移动文件`,效果类似命令 `将文件移动到其他文件夹`
在文件列表中选择文件后,右键可一起移动,若设置了快捷键也可以快捷键打开,优先级大于当前页面文件。

特性:
Expand All @@ -48,7 +48,7 @@ linkText:

### 命令搜索

命令 `Fuzzy Chinese Pinyin: Execute Command`,效果类似命令 `命令面板: 打开命令面板`
命令 `Fuzzy Chinese Pinyin: 执行命令`,效果类似命令 `命令面板: 打开命令面板`

快捷键设置:
Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin: Execute Command` 设置 `<Cmd+p>` 快捷键,就可以平替默认的 `命令面板:打开命令面板`
Expand All @@ -62,14 +62,19 @@ Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin:

### 当前文档标题搜索

命令 `Fuzzy Chinese Pinyin: Search Heading`
命令 `Fuzzy Chinese Pinyin: 搜索标题`
对当前文档标题进行拼音搜索。

特性:

1. 隐藏第一级标题
2. 对标题进行缩进

### 插入模版

命令 `Fuzzy Chinese Pinyin: 插入模版`,效果类似命令 `模板:插入模板`
与核心插件模板功能相同,仅加入拼音搜索功能。

### 双链

选项设置:
Expand Down
19 changes: 15 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +41,7 @@ export default class ThePlugin extends Plugin {
folderModal: FolderModal;
commandModal: CommandModal;
headingModal: HeadingModal;
templatesModal: TemplatesModal;
fileEditorSuggest: FileEditorSuggest;
tagEditorSuggest: TagEditorSuggest;
indexManager: IndexManager;
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -133,21 +137,28 @@ 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);
this.headingModal.setFile(file);
this.headingModal.open();
},
});
this.addCommand({
id: "insert-templates",
name: "插入模板",
callback: () => {
this.templatesModal.open();
},
});
}
onunload() {
if (this.settings.other.devMode) {
Expand Down
49 changes: 49 additions & 0 deletions src/modal/templatesModal.ts
Original file line number Diff line number Diff line change
@@ -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<Item> {
constructor(app: App, plugin: ThePlugin) {
super(app, plugin);
this.index = {} as any;
runOnLayoutReady(() => {
this.updateIndex();
});
}
onOpen(): void {
super.onOpen();
this.updateIndex();
}
getEmptyInputSuggestions(): MatchData<Item>[] {
return this.index.items.map((p) => ({
item: p,
score: -1,
range: null,
}));
}
onChooseSuggestion(matchData: MatchData<Item>, 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,
}));
}
}

0 comments on commit 27efebe

Please sign in to comment.