Skip to content

Commit

Permalink
refactor: 设置属性分类
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Oct 14, 2023
1 parent eda6e48 commit eb3a4db
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 92 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.7.6"
"version": "2.8.0"
}
2 changes: 1 addition & 1 deletion src/fileEditorSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
}
toHighlightEl.appendText(text.slice(index));

if (this.plugin.settings.showTags && !(matchData.item.type == "heading")) {
if (this.plugin.settings.file.showTags && !(matchData.item.type == "heading")) {
let tags: string | Array<string> =
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tags ||
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tag,
Expand Down
14 changes: 7 additions & 7 deletions src/fuzzyFileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
if (query == "") {
this.historyMatchData = new HistoryMatchDataNode("\0");
let items = this.index.items;
let fileHistoryDisplay = this.plugin.settings.fileHistoryDisplay == "使用完整路径";
let fileHistoryDisplay = this.plugin.settings.file.historyDisplay == "使用完整路径";
let lastOpenFiles: MatchData[] = app.workspace
.getLastOpenFiles()
.map((p) => items.find((q) => q.type == "file" && q.path == p))
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
if (d) matchData1.push(d);
}

if (this.plugin.settings.usePathToSearch && matchData1.length <= 10) {
if (this.plugin.settings.file.usePathToSearch && matchData1.length <= 10) {
toMatchData = indexNode.itemIndexByPath.length == 0 ? this.index.items : indexNode.itemIndexByPath;
for (let p of toMatchData.filter((p) => p.type == "file" && !matchData1.map((p) => p.item.path).includes(p.path))) {
let d = <MatchData>p.pinyinOfPath.match(query, p);
Expand Down Expand Up @@ -208,7 +208,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
e_title.appendText(text.slice(index));

if (!matchData.usePath) {
if (this.plugin.settings.showTags) {
if (this.plugin.settings.file.showTags) {
let tags: string | Array<string> =
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tags ||
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tag,
Expand All @@ -221,7 +221,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
}

let e_note: HTMLDivElement = null;
if (this.plugin.settings.showPath && !matchData.usePath)
if (this.plugin.settings.file.showPath && !matchData.usePath)
e_note = e_content.createEl("div", {
cls: "fz-suggestion-note",
text: matchData.item.path,
Expand All @@ -233,7 +233,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
});
e_flair.innerHTML +=
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon lucide-forward"><polyline points="15 17 20 12 15 7"></polyline><path d="M4 18v-2a4 4 0 0 1 4-4h12"></path></svg>';
if (!this.plugin.settings.showPath) e_flair.style.top = "9px";
if (!this.plugin.settings.file.showPath) e_flair.style.top = "9px";
if (e_note) e_note.style.width = "calc(100% - 30px)";
}
}
Expand Down Expand Up @@ -356,9 +356,9 @@ class PinyinIndex extends PI<Item> {
isEffectiveFile(file: TAbstractFile) {
if (!(file instanceof TFile)) return false;

if (this.plugin.settings.showAllFileTypes) return true;
if (this.plugin.settings.file.showAllFileTypes) return true;
else if (DOCUMENT_EXTENSIONS.includes(file.extension)) return true;
else if (this.plugin.settings.showAttachments && this.plugin.settings.attachmentExtensions.includes(file.extension)) return true;
else if (this.plugin.settings.file.showAttachments && this.plugin.settings.file.attachmentExtensions.includes(file.extension)) return true;
else return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fuzzyModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default abstract class FuzzyModal<T extends Item> extends SuggestModal<Ma
this.historyMatchData = new HistoryMatchDataNode("\0");

this.scope.register([], "Backspace", async (e) => {
if (this.plugin.settings.closeWithBackspace && this.inputEl.value === "") {
if (this.plugin.settings.global.closeWithBackspace && this.inputEl.value === "") {
this.close();
}
});
Expand Down
176 changes: 94 additions & 82 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,69 @@ import TraditionalDict from "./traditional_dict";
import DoublePinyinDict from "./double_pinyin";

interface FuzyyChinesePinyinSettings {
traditionalChineseSupport: boolean;
showAllFileTypes: boolean;
showAttachments: boolean;
fileHistoryDisplay: string;
attachmentExtensions: Array<string>;
usePathToSearch: boolean;
useFileEditorSuggest: boolean;
useTagEditorSuggest: boolean;
showPath: boolean;
showTags: boolean;
doublePinyin: string;
closeWithBackspace: boolean;
devMode: boolean;
global: {
traditionalChineseSupport: boolean;
doublePinyin: string;
closeWithBackspace: boolean;
};
file: {
showAllFileTypes: boolean;
showAttachments: boolean;
attachmentExtensions: Array<string>;
usePathToSearch: boolean;
useFileEditorSuggest: boolean;
showPath: boolean;
showTags: boolean;
historyDisplay: string;
};
other: {
useTagEditorSuggest: boolean;
devMode: boolean;
};
}

const DEFAULT_SETTINGS: FuzyyChinesePinyinSettings = {
traditionalChineseSupport: false,
showAttachments: false,
showAllFileTypes: false,
attachmentExtensions: [
"bmp",
"png",
"jpg",
"jpeg",
"gif",
"svg",
"webp",
"mp3",
"wav",
"m4a",
"3gp",
"flac",
"ogg",
"oga",
"opus",
"mp4",
"webm",
"ogv",
"mov",
"mkv",
"pdf",
],
usePathToSearch: false,
useFileEditorSuggest: false,
useTagEditorSuggest: false,
showPath: true,
showTags: false,
doublePinyin: "全拼",
closeWithBackspace: false,
devMode: false,
fileHistoryDisplay: "使用完整路径",
global: {
traditionalChineseSupport: false,
doublePinyin: "全拼",
closeWithBackspace: false,
},
file: {
showAttachments: false,
showAllFileTypes: false,
attachmentExtensions: [
"bmp",
"png",
"jpg",
"jpeg",
"gif",
"svg",
"webp",
"mp3",
"wav",
"m4a",
"3gp",
"flac",
"ogg",
"oga",
"opus",
"mp4",
"webm",
"ogv",
"mov",
"mkv",
"pdf",
],
usePathToSearch: false,
useFileEditorSuggest: false,
showPath: true,
showTags: false,
historyDisplay: "使用完整路径",
},
other: {
useTagEditorSuggest: false,
devMode: false,
},
};

export default class FuzzyChinesePinyinPlugin extends Plugin {
Expand All @@ -80,12 +92,12 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
editorSuggests: EditorSuggest<any>[];

loadPinyinDict() {
let PinyinKeys_ = this.settings.traditionalChineseSupport ? Object.keys(TraditionalDict) : Object.keys(SimplifiedDict);
let PinyinValues = this.settings.traditionalChineseSupport ? Object.values(TraditionalDict) : Object.values(SimplifiedDict);
let PinyinKeys_ = this.settings.global.traditionalChineseSupport ? Object.keys(TraditionalDict) : Object.keys(SimplifiedDict);
let PinyinValues = this.settings.global.traditionalChineseSupport ? Object.values(TraditionalDict) : Object.values(SimplifiedDict);
let PinyinKeys =
this.settings.doublePinyin == "全拼"
this.settings.global.doublePinyin == "全拼"
? PinyinKeys_
: PinyinKeys_.map((p) => fullPinyin2doublePinyin(p, DoublePinyinDict[this.settings.doublePinyin]));
: PinyinKeys_.map((p) => fullPinyin2doublePinyin(p, DoublePinyinDict[this.settings.global.doublePinyin]));

// originalKeys 永远是全拼的拼音,keys 是转换后的拼音(可能也是全拼或双拼)
this.pinyinDict = { keys: PinyinKeys, values: PinyinValues, originalKeys: PinyinKeys_ };
Expand All @@ -104,15 +116,15 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
this.indexManager = new IndexManager(this, [this.folderModal, this.fileModal, this.commandModal, this.tagEditorSuggest]);
this.editorSuggests = [this.fileEditorSuggest, this.tagEditorSuggest];

if (this.settings.useFileEditorSuggest) {
if (this.settings.file.useFileEditorSuggest) {
this.app.workspace.editorSuggest.suggests.unshift(this.fileEditorSuggest);
}
if (this.settings.useTagEditorSuggest) {
if (this.settings.other.useTagEditorSuggest) {
this.app.workspace.editorSuggest.suggests.unshift(this.tagEditorSuggest);
}

runOnLayoutReady(() => {
if (this.settings.devMode) {
if (this.settings.other.devMode) {
this.indexManager.devLoad();
globalThis.refreshFuzzyChineseIndex = () => {
globalThis.FuzzyChineseIndex = {};
Expand Down Expand Up @@ -173,7 +185,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
}
onunload() {
this.editorSuggests.forEach((editorSuggest) => this.app.workspace.editorSuggest.removeSuggest(editorSuggest));
if (this.settings.devMode) {
if (this.settings.other.devMode) {
this.indexManager.devUnload();
}
}
Expand Down Expand Up @@ -205,14 +217,14 @@ class SettingTab extends PluginSettingTab {
.setName("Backspace 关闭搜索")
.setDesc("当输入框为空时按下 Backspace 关闭搜索")
.addToggle((text) =>
text.setValue(this.plugin.settings.closeWithBackspace).onChange(async (value) => {
this.plugin.settings.closeWithBackspace = value;
text.setValue(this.plugin.settings.global.closeWithBackspace).onChange(async (value) => {
this.plugin.settings.global.closeWithBackspace = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("繁体支持").addToggle((text) => {
text.setValue(this.plugin.settings.traditionalChineseSupport).onChange(async (value) => {
this.plugin.settings.traditionalChineseSupport = value;
text.setValue(this.plugin.settings.global.traditionalChineseSupport).onChange(async (value) => {
this.plugin.settings.global.traditionalChineseSupport = value;
await this.plugin.saveSettings();
this.plugin.loadPinyinDict();
});
Expand All @@ -225,9 +237,9 @@ class SettingTab extends PluginSettingTab {
小鹤双拼: "小鹤双拼",
微软双拼: "微软双拼",
})
.setValue(this.plugin.settings.doublePinyin)
.setValue(this.plugin.settings.global.doublePinyin)
.onChange(async (value: string) => {
this.plugin.settings.doublePinyin = value;
this.plugin.settings.global.doublePinyin = value;
this.plugin.pinyinDict.keys =
value == "全拼"
? this.plugin.pinyinDict.originalKeys
Expand All @@ -242,44 +254,44 @@ class SettingTab extends PluginSettingTab {
.setName("显示附件")
.setDesc("显示如图片、视频、PDF等附件文件。")
.addToggle((text) =>
text.setValue(this.plugin.settings.showAttachments).onChange(async (value) => {
this.plugin.settings.showAttachments = value;
text.setValue(this.plugin.settings.file.showAttachments).onChange(async (value) => {
this.plugin.settings.file.showAttachments = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("显示所有类型文件").addToggle((text) =>
text.setValue(this.plugin.settings.showAllFileTypes).onChange(async (value) => {
this.plugin.settings.showAllFileTypes = value;
text.setValue(this.plugin.settings.file.showAllFileTypes).onChange(async (value) => {
this.plugin.settings.file.showAllFileTypes = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("使用路径搜索")
.setDesc("当搜索结果少于10个时搜索路径")
.addToggle((text) =>
text.setValue(this.plugin.settings.usePathToSearch).onChange(async (value) => {
this.plugin.settings.usePathToSearch = value;
text.setValue(this.plugin.settings.file.usePathToSearch).onChange(async (value) => {
this.plugin.settings.file.usePathToSearch = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("显示路径").addToggle((text) =>
text.setValue(this.plugin.settings.showPath).onChange(async (value) => {
this.plugin.settings.showPath = value;
text.setValue(this.plugin.settings.file.showPath).onChange(async (value) => {
this.plugin.settings.file.showPath = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("显示 Tag").addToggle((text) =>
text.setValue(this.plugin.settings.showTags).onChange(async (value) => {
this.plugin.settings.showTags = value;
text.setValue(this.plugin.settings.file.showTags).onChange(async (value) => {
this.plugin.settings.file.showTags = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("使用双链建议")
.setDesc("输入[[的时候文件连接能支持中文拼音搜索(实验性功能)")
.addToggle((text) =>
text.setValue(this.plugin.settings.useFileEditorSuggest).onChange(async (value) => {
this.plugin.settings.useFileEditorSuggest = value;
text.setValue(this.plugin.settings.file.useFileEditorSuggest).onChange(async (value) => {
this.plugin.settings.file.useFileEditorSuggest = value;
if (value) {
this.app.workspace.editorSuggest.suggests.unshift(this.plugin.fileEditorSuggest);
} else {
Expand All @@ -294,9 +306,9 @@ class SettingTab extends PluginSettingTab {
使用完整路径: "使用完整路径",
仅使用文件名: "仅使用文件名",
})
.setValue(this.plugin.settings.fileHistoryDisplay)
.setValue(this.plugin.settings.file.historyDisplay)
.onChange(async (value: string) => {
this.plugin.settings.fileHistoryDisplay = value;
this.plugin.settings.file.historyDisplay = value;
await this.plugin.saveSettings();
})
);
Expand All @@ -305,8 +317,8 @@ class SettingTab extends PluginSettingTab {
.setDesc("只显示这些后缀的附件")
.addTextArea((text) => {
text.inputEl.addClass("fuzzy-chinese-attachment-extensions");
text.setValue(this.plugin.settings.attachmentExtensions.join("\n")).onChange(async (value) => {
this.plugin.settings.attachmentExtensions = value
text.setValue(this.plugin.settings.file.attachmentExtensions.join("\n")).onChange(async (value) => {
this.plugin.settings.file.attachmentExtensions = value
.trim()
.split("\n")
.map((x) => x.trim());
Expand All @@ -318,8 +330,8 @@ class SettingTab extends PluginSettingTab {
.setName("使用标签建议")
.setDesc("实验性功能")
.addToggle((text) =>
text.setValue(this.plugin.settings.useTagEditorSuggest).onChange(async (value) => {
this.plugin.settings.useTagEditorSuggest = value;
text.setValue(this.plugin.settings.other.useTagEditorSuggest).onChange(async (value) => {
this.plugin.settings.other.useTagEditorSuggest = value;
if (value) {
this.app.workspace.editorSuggest.suggests.unshift(this.plugin.tagEditorSuggest);
} else {
Expand All @@ -332,8 +344,8 @@ class SettingTab extends PluginSettingTab {
.setName("dev 模式")
.setDesc("将索引存储到 global 以便重启时不重建索引")
.addToggle((text) =>
text.setValue(this.plugin.settings.devMode).onChange(async (value) => {
this.plugin.settings.devMode = value;
text.setValue(this.plugin.settings.other.devMode).onChange(async (value) => {
this.plugin.settings.other.devMode = value;
await this.plugin.saveSettings();
})
);
Expand Down

0 comments on commit eb3a4db

Please sign in to comment.