Skip to content

Commit

Permalink
feat: 自动大小写敏感 #26
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Dec 7, 2023
1 parent da8150a commit a889541
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 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.9.1"
"version": "2.10.1"
}
17 changes: 6 additions & 11 deletions src/fuzzyFileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
node = node.next;
if (_f) index++;
}
let smathCase = /[A-Z]/.test(query),
let smathCase = /[A-Z]/.test(query) && this.plugin.settings.global.autoCaseSensitivity,
indexNode = this.historyMatchData.index(index - 1),
toMatchData = indexNode.itemIndex.length == 0 ? this.index.items : indexNode.itemIndex;
for (let p of toMatchData) {
Expand All @@ -160,7 +160,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
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);
let d = <MatchData>p.pinyinOfPath.match(query, p, smathCase);
if (d) {
d.usePath = true;
matchData2.push(d);
Expand Down Expand Up @@ -316,18 +316,13 @@ class PinyinIndex extends PI<Item> {
}
}
initEvent() {
this.registerEvent(
this.metadataCache.on("changed", (file, data, cache) => {
this.update("changed", file, { data, cache });
})
);
this.registerEvent(this.metadataCache.on("changed", (file, data, cache) => this.update("changed", file, { data, cache })));
this.registerEvent(this.vault.on("rename", (file, oldPath) => this.update("rename", file, { oldPath })));
this.registerEvent(this.vault.on("create", (file) => this.update("create", file)));
this.registerEvent(this.vault.on("delete", (file) => this.update("delete", file)));
}
update(type: string, f: TAbstractFile, keys?: { oldPath?: string; data?: string; cache?: CachedMetadata }) {
if (!this.isEffectiveFile(f)) return;
let file = f as TFile;
update(type: string, file: TAbstractFile, keys?: { oldPath?: string; data?: string; cache?: CachedMetadata }) {
if (!this.isEffectiveFile(file)) return;
switch (type) {
case "changed": {
this.items = this.items
Expand All @@ -353,7 +348,7 @@ class PinyinIndex extends PI<Item> {
}
}

isEffectiveFile(file: TAbstractFile) {
isEffectiveFile(file: TAbstractFile): file is TFile {
if (!(file instanceof TFile)) return false;

if (this.plugin.settings.file.showAllFileTypes) return true;
Expand Down
2 changes: 1 addition & 1 deletion src/fuzzyModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default abstract class FuzzyModal<T extends Item> extends SuggestModal<Ma
node = node.next;
if (_f) index++;
}
let smathCase = /[A-Z]/.test(query),
let smathCase = /[A-Z]/.test(query) && this.plugin.settings.global.autoCaseSensitivity,
indexNode = this.historyMatchData.index(index - 1),
toMatchData = indexNode.itemIndex.length == 0 ? this.index.items : indexNode.itemIndex;
for (let p of toMatchData) {
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface FuzyyChinesePinyinSettings {
traditionalChineseSupport: boolean;
doublePinyin: string;
closeWithBackspace: boolean;
autoCaseSensitivity: boolean,
};
file: {
showAllFileTypes: boolean;
Expand All @@ -41,6 +42,7 @@ const DEFAULT_SETTINGS: FuzyyChinesePinyinSettings = {
traditionalChineseSupport: false,
doublePinyin: "全拼",
closeWithBackspace: false,
autoCaseSensitivity: true,
},
file: {
showAttachments: false,
Expand Down Expand Up @@ -254,6 +256,12 @@ class SettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("自动大小写敏感").addToggle((text) => {
text.setValue(this.plugin.settings.global.autoCaseSensitivity).onChange(async (value) => {
this.plugin.settings.global.autoCaseSensitivity = value;
await this.plugin.saveSettings();
});
});
containerEl.createEl("h2", { text: "文件搜索" });
new Setting(containerEl)
.setName("显示附件")
Expand Down
6 changes: 3 additions & 3 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export function fuzzyPinyinSearch(query: string, items: string[] | Item[], plugi
if (isStringArray(items)) {
items = stringArray2Items(items, plugin);
}
return search(query, items);
return search(query, items, plugin.settings.global.autoCaseSensitivity);
}

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

function search(query: string, items: Item[]): MatchData<Item>[] {
function search(query: string, items: Item[], autoCaseSensitivity = true): MatchData<Item>[] {
if (query == "") {
return items.map((p) => ({
item: p,
Expand All @@ -23,7 +23,7 @@ function search(query: string, items: Item[]): MatchData<Item>[] {
}

let matchData: MatchData<Item>[] = [];
let smathCase = /[A-Z]/.test(query);
let smathCase = /[A-Z]/.test(query) && autoCaseSensitivity;
for (let p of items) {
let d = p.pinyin.match(query, p, smathCase);
if (d) matchData.push(d);
Expand Down
2 changes: 1 addition & 1 deletion src/tagEditorSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class TagEditorSuggest extends EditorSuggest<MatchData<Item>> {
node = node.next;
if (_f) index++;
}
let smathCase = /[A-Z]/.test(query),
let smathCase = /[A-Z]/.test(query) && this.plugin.settings.global.autoCaseSensitivity,
indexNode = this.historyMatchData.index(index - 1),
toMatchData = indexNode.itemIndex.length == 0 ? this.index.items : indexNode.itemIndex;
for (let p of toMatchData) {
Expand Down

0 comments on commit a889541

Please sign in to comment.