Skip to content

Commit

Permalink
fix: 修复 tag 建议的一些错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Feb 6, 2024
1 parent 456c27f commit 2605cf6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 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.0"
"version": "2.17.1"
}
11 changes: 8 additions & 3 deletions src/fileEditorSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EditorSuggestTriggerInfo,
TFile,
} from "obsidian";
import { MatchData as fMatchData, Item as fItem } from "./fuzzyFileModal";
import { MatchData as fMatchData, Item as fItem, LinkItem } from "./fuzzyFileModal";
import { PinyinIndex, Pinyin } from "./utils";
import FuzzyChinesePinyinPlugin from "./main";

Expand Down Expand Up @@ -170,13 +170,14 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
selectSuggestion(matchData: MatchData, evt: MouseEvent | KeyboardEvent): void {
let item = matchData.item;
let result: OriginEditorSuggestResult;
let file = app.workspace.getActiveFile();
switch (item.type) {
case "heading":
case "link":
result = {
heading: item.name,
heading: isLinkItem(item) ? item.link : item.name,
type: "heading",
path: item.file.basename,
path: item.file == file ? "" : item.file.basename,
file: item.file,
subpath: "#",
level: 1,
Expand Down Expand Up @@ -216,3 +217,7 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
this.originEditorSuggest.selectSuggestion(result, evt);
}
}

function isLinkItem(item: Item): item is LinkItem {
return item.type === "link";
}
2 changes: 1 addition & 1 deletion src/fuzzyFileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Item_<T extends ItemType> extends uItem {
interface FileItem extends Item_<"file"> {}
interface AliasItem extends Item_<"alias"> {}
interface UnresolvedLinkItem extends Item_<"unresolvedLink"> {}
interface LinkItem extends Item_<"link"> {
export interface LinkItem extends Item_<"link"> {
link: string;
}
export type Item = FileItem | AliasItem | UnresolvedLinkItem | LinkItem;
Expand Down
32 changes: 6 additions & 26 deletions src/fuzzyModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default abstract class FuzzyModal<T extends Item> extends SuggestModal<Ma
suggestions: HTMLElement;
values: MatchData<T>[];
selectedItem: number;
setSelectedItem(index: number): void;
setSelectedItem(index: number, e: KeyboardEvent): void;
};
index: PinyinIndex<T>;
plugin: FuzzyChinesePinyinPlugin;
Expand All @@ -30,34 +30,14 @@ export default abstract class FuzzyModal<T extends Item> extends SuggestModal<Ma
}
});
this.scope.register(["Mod"], "N", async (e) => {
if (this.chooser.selectedItem != this.chooser.values.length - 1) {
this.chooser.setSelectedItem(this.chooser.selectedItem + 1);
this.chooser.suggestions[this.chooser.selectedItem].scrollIntoView({
block: "center",
behavior: "smooth",
});
} else {
this.chooser.setSelectedItem(0);
this.chooser.suggestions[this.chooser.selectedItem].scrollIntoView({
block: "center",
behavior: "smooth",
});
}
if (this.chooser.selectedItem != this.chooser.values.length - 1)
this.chooser.setSelectedItem(this.chooser.selectedItem + 1, e);
else this.chooser.setSelectedItem(0, e);
});
this.scope.register(["Mod"], "P", async (e) => {
if (this.chooser.selectedItem != 0) {
this.chooser.setSelectedItem(this.chooser.selectedItem - 1);
this.chooser.suggestions[this.chooser.selectedItem].scrollIntoView({
block: "center",
behavior: "smooth",
});
} else {
this.chooser.setSelectedItem(this.chooser.values.length - 1);
this.chooser.suggestions[this.chooser.selectedItem].scrollIntoView({
block: "center",
behavior: "smooth",
});
}
this.chooser.setSelectedItem(this.chooser.selectedItem - 1, e);
} else this.chooser.setSelectedItem(this.chooser.values.length - 1, e);
});
}
onOpen() {
Expand Down
8 changes: 8 additions & 0 deletions src/viewEventHijacking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export function hijackingTagForMarkdownView(plugin: FuzzyChinesePinyinPlugin) {
newInputEl.addEventListener("input", async (e) => {
tagSuggest.onInputChanged();
});
newInputEl.addEventListener("keydown", async (e) => {
if (!e.isComposing)
if ("Enter" === e.key && newInputEl.innerText.length > 0) {
e.preventDefault();
rendered["_children"][0].multiselect.addElement(newInputEl.innerText);
newInputEl.innerText = "";
}
});
});
})
);
Expand Down

0 comments on commit 2605cf6

Please sign in to comment.