Skip to content

Commit

Permalink
feat: 标题搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Feb 19, 2024
1 parent 2ee6f6f commit e2e16d4
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 7 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.17.3"
"version": "2.18.0"
}
1 change: 0 additions & 1 deletion src/editorSuggest/tagEditorSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export default class TagEditorSuggest extends EditorSuggest<MatchData<Item>> {
return matchData;
}
renderSuggestion(matchData: MatchData<Item>, el: HTMLElement) {
el.addClass("fz-item");
new SuggestionRenderer(el).render(matchData);
}
selectSuggestion(matchData: MatchData<Item>): void {
Expand Down
15 changes: 15 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import FuzzyFileModal from "@/modal/fileModal";
import FuzzyFolderModal from "@/modal/folderModal";
import FuzzyCommandModal from "@/modal/commandModal";
import FuzzySuggestModal from "@/modal/suggestModal";
import FuzzyHeadingModal from "@/modal/headingModal";
import FileEditorSuggest from "@/editorSuggest/fileEditorSuggest";
import TagEditorSuggest from "@/editorSuggest/tagEditorSuggest";
// 以下两个字典来源于:https://github.com/xmflswood/pinyin-match
Expand All @@ -36,6 +37,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
fileModal: FuzzyFileModal;
folderModal: FuzzyFolderModal;
commandModal: FuzzyCommandModal;
headingModal: FuzzyHeadingModal;
fileEditorSuggest: FileEditorSuggest;
tagEditorSuggest: TagEditorSuggest;
indexManager: IndexManager;
Expand All @@ -49,6 +51,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
this.fileModal = new FuzzyFileModal(this.app, this);
this.folderModal = new FuzzyFolderModal(this.app, this);
this.commandModal = new FuzzyCommandModal(this.app, this);
this.headingModal = new FuzzyHeadingModal(this.app, this);
this.fileEditorSuggest = new FileEditorSuggest(this.app, this);
this.tagEditorSuggest = new TagEditorSuggest(this.app, this);

Expand Down Expand Up @@ -149,6 +152,18 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
return true;
},
});
this.addCommand({
id: "search-heading",
name: "Search Heading",
checkCallback: (checking: boolean) => {
if (!checking) {
const file = this.app.workspace.getActiveFile();
this.headingModal.setFile(file);
this.headingModal.open();
}
return true;
},
});
}
onunload() {
if (this.settings.other.devMode) {
Expand Down
2 changes: 0 additions & 2 deletions src/modal/commandModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ export default class FuzzyCommandModal extends FuzzyModal<Item> {
this.app.commands.executeCommand(matchData.item.command);
}
renderSuggestion(matchData: MatchData<Item>, el: HTMLElement): void {
el.addClass("fz-item");

let renderer = new SuggestionRenderer(el);
renderer.render(matchData);

Expand Down
1 change: 0 additions & 1 deletion src/modal/fileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
}

renderSuggestion(matchData: MatchData, el: HTMLElement) {
el.addClass("fz-item");
let renderer = new SuggestionRenderer(el);
if (matchData.item.file) renderer.setNote(matchData.item.path);
if (matchData.usePath) renderer.setToHighlightEl("note");
Expand Down
51 changes: 51 additions & 0 deletions src/modal/headingModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { App, TFile } from "obsidian";
import FuzzyChinesePinyinPlugin from "@/main";
import { MatchData, Pinyin, SuggestionRenderer, Item as uItem } from "@/utils";
import FuzzyModal from "./modal";

interface Item extends uItem {
level: number;
}

export default class FuzzyHeadingModal extends FuzzyModal<Item> {
file: TFile;
constructor(app: App, plugin: FuzzyChinesePinyinPlugin) {
super(app, plugin);
this.index = {} as any;
}
setFile(file: TFile) {
this.file = file;
let heading = app.metadataCache.getFileCache(file)?.headings;
if (!heading) return;
if (!this.plugin.settings.heading.showFirstLevelHeading)
heading = heading.filter((h) => h.level > 1);
this.index.items = heading.map((h) => ({
name: h.heading,
pinyin: new Pinyin(h.heading, this.plugin),
level: h.level,
})) as Item[];
}
getEmptyInputSuggestions(): MatchData<Item>[] {
return this.index.items.map((p) => ({
item: p,
score: -1,
range: null,
}));
}
onChooseSuggestion(matchData: MatchData<Item>, evt: KeyboardEvent | MouseEvent): void {
const leaf = app.workspace.getMostRecentLeaf();
leaf.openLinkText("#" + matchData.item.name, this.file.path);
}
renderSuggestion(matchData: MatchData<Item>, el: HTMLElement): void {
let renderer = new SuggestionRenderer(el);
renderer.render(matchData);
renderer.addIcon("heading-" + matchData.item.level);
if (this.plugin.settings.heading.headingIndent) {
let indent = this.plugin.settings.heading.showFirstLevelHeading
? matchData.item.level - 1
: matchData.item.level - 2;
indent *= 15;
renderer.titleEl.style.marginLeft = indent + "px";
}
}
}
1 change: 0 additions & 1 deletion src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default abstract class FuzzyModal<T extends Item> extends SuggestModal<Ma
}

renderSuggestion(matchData: MatchData<T>, el: HTMLElement) {
el.addClass("fz-item");
new SuggestionRenderer(el).render(matchData);
}
onNoSuggestion(value?: MatchData<T>): void {
Expand Down
33 changes: 32 additions & 1 deletion src/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class SettingTab extends PluginSettingTab {
this.containerEl.createEl("h1", { text: "设置" });
this.addGlobalSetting();
this.addFileSetting();
this.addHeadingSetting();
this.addCommandSettings();
this.addOtherSetting();
}
Expand Down Expand Up @@ -158,6 +159,23 @@ export default class SettingTab extends PluginSettingTab {
);
});
}
addHeadingSetting() {
this.containerEl.createEl("h2", { text: "标题搜索" });
new Setting(this.containerEl).setName("显示第一级标题").addToggle((cb) =>
cb
.setValue(this.plugin.settings.heading.showFirstLevelHeading)
.onChange(async (value) => {
this.plugin.settings.heading.showFirstLevelHeading = value;
await this.plugin.saveSettings();
})
);
new Setting(this.containerEl).setName("搜索结果缩进").addToggle((cb) =>
cb.setValue(this.plugin.settings.heading.headingIndent).onChange(async (value) => {
this.plugin.settings.heading.headingIndent = value;
await this.plugin.saveSettings();
})
);
}
addCommandSettings() {
this.containerEl.createEl("h2", { text: "命令" });
this.addPinnedCommands();
Expand All @@ -178,7 +196,12 @@ export default class SettingTab extends PluginSettingTab {
});
}
addPinnedCommands() {
this.plugin.settings.command.pinnedCommands.forEach((command, index) => {
const { pinnedCommands } = this.plugin.settings.command;
if (pinnedCommands.length === 0) {
new Setting(this.containerEl).setName("没有置顶命令");
return;
}
pinnedCommands.forEach((command, index) => {
new Setting(this.containerEl)
.setName(command)
.addExtraButton((cb) =>
Expand Down Expand Up @@ -274,6 +297,10 @@ export interface FuzyyChinesePinyinSettings {
showTags: boolean;
searchWithTag: boolean;
};
heading: {
showFirstLevelHeading: boolean;
headingIndent: boolean;
};
command: {
pinnedCommands: Array<string>;
};
Expand Down Expand Up @@ -323,6 +350,10 @@ export const DEFAULT_SETTINGS: FuzyyChinesePinyinSettings = {
showTags: false,
searchWithTag: true,
},
heading: {
showFirstLevelHeading: true,
headingIndent: true,
},
command: {
pinnedCommands: [],
},
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class SuggestionRenderer {
note: string = "";
hasIcon: boolean = false;
constructor(containerEl: HTMLElement) {
containerEl.addClass("fz-item");
this.containerEl = containerEl;
this.contentEl = this.containerEl.createEl("div", { cls: "fz-suggestion-content" });
this.titleEl = this.contentEl.createEl("div", { cls: "fz-suggestion-title" });
Expand Down

0 comments on commit e2e16d4

Please sign in to comment.