Skip to content

Commit

Permalink
feat: 增加显示未完成链接的选项
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Jan 18, 2024
1 parent 145d1e2 commit db13b33
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 126 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.14.0"
"version": "2.15.0"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"obsidian": "github:obsidianmd/obsidian-api",
"obsidian-plugin-cli": "^0.9.0",
"templater": "github:SilentVoid13/Templater",
"typescript": "^4.9.5"
"typescript": "^5.3.3"
}
}
19 changes: 12 additions & 7 deletions src/fileEditorSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,14 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
renderSuggestion(matchData: MatchData, el: HTMLElement) {
el.addClass("fz-item");
let renderer = new SuggestionRenderer(el);
renderer.setTitle(matchData.item.name);
renderer.setNote(matchData.item.path);
if (matchData.item.file) renderer.setNote(matchData.item.path);
if (matchData.usePath) renderer.setToHighlightEl("note");
renderer.render(matchData);

if (this.plugin.settings.file.showTags && matchData.item.type != "heading") {
if (this.plugin.settings.file.showTags && matchData.item.file) {
let tags: string | Array<string> =
this.app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tags ||
this.app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tag,
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tags ||
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tag,
tagArray: string[];
if (tags) {
tagArray = Array.isArray(tags)
Expand All @@ -174,7 +173,7 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
renderer.addIcon("forward");
if (!this.plugin.settings.file.showPath) renderer.flairEl.style.top = "9px";
if (renderer.noteEl) renderer.noteEl.style.width = "calc(100% - 30px)";
}
} else if (matchData.item.type == "unresolvedLink") renderer.addIcon("file-plus");
}
selectSuggestion(matchData: MatchData, evt: MouseEvent | KeyboardEvent): void {
if (matchData.item.type == "heading") {
Expand All @@ -192,7 +191,13 @@ export default class FileEditorSuggest extends EditorSuggest<MatchData> {
if (p.type == matchData.item.type && p.file == matchData.item.file) {
if (p.type == "alias") return p.alias == matchData.item.name;
else return true;
} else return false;
} else if (
p.type == "linktext" &&
matchData.item.type == "unresolvedLink" &&
p.path == matchData.item.name
)
return true;
else return false;
});
this.originEditorSuggest.selectSuggestion(matchData_, evt);
});
Expand Down
48 changes: 19 additions & 29 deletions src/fuzzyCommandModal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { App, Hotkey, Modifier, Platform, getIcon } from "obsidian";
import { App, Command, Hotkey, Modifier, Platform } from "obsidian";
import FuzzyModal from "./fuzzyModal";
import { PinyinIndex as PI, Pinyin, MatchData, SuggestionRenderer } from "./utils";
import {
PinyinIndex as PI,
Pinyin,
MatchData,
SuggestionRenderer,
incrementalUpdate,
} from "./utils";
import FuzzyChinesePinyinPlugin from "./main";

type Item = {
name: string;
pinyin: Pinyin;
command: any;
command: Command;
};

const BASIC_MODIFIER_ICONS = {
Expand Down Expand Up @@ -145,39 +151,23 @@ class PinyinIndex extends PI<Item> {
}
initEvent() {}
initIndex() {
let commands = this.app.commands.listCommands();
let commands: Command[] = this.app.commands.listCommands();
this.items = commands.map((command) => ({
name: command.name,
pinyin: new Pinyin(command.name, this.plugin),
command: command,
}));
}
update() {
let commands = this.app.commands.listCommands();
let oldCommandsNames = this.items.map((item) => item.name);
let newCommandsNames = commands.map((command) => command.name);
let addedCommands = newCommandsNames.filter(
(command) => !oldCommandsNames.includes(command)
let commands: Command[] = this.app.commands.listCommands();
this.items = incrementalUpdate(
this.items,
() => commands.map((command) => command.name),
(name) => ({
name,
pinyin: new Pinyin(name, this.plugin),
command: commands.find((p) => p.name == name),
})
);
let removedCommands = oldCommandsNames.filter(
(command) => !newCommandsNames.includes(command)
);
if (addedCommands.length > 0) {
// 添加新命令
this.items.push(
...addedCommands.map((command) => {
let item = {
name: command,
pinyin: new Pinyin(command, this.plugin),
command: commands.find((p) => p.name == command),
};
return item;
})
);
}
if (removedCommands.length > 0) {
// 删除旧命令
this.items = this.items.filter((item) => !removedCommands.includes(item.name));
}
}
}
103 changes: 58 additions & 45 deletions src/fuzzyFileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import {
MatchData as uMatchData,
Item as uItem,
SuggestionRenderer,
createFile,
incrementalUpdate,
} from "./utils";
import FuzzyChinesePinyinPlugin from "./main";
import FuzzyModal from "./fuzzyModal";
import { TextInputSuggest } from "templater/src/settings/suggesters/suggest";

const DOCUMENT_EXTENSIONS = ["md", "canvas"];

export type Item = {
export interface Item extends uItem {
file: TFile;
type: "file" | "alias" | "heading";
type: "file" | "alias" | "heading" | "unresolvedLink";
name: string;
pinyin: Pinyin;
path: string;
pinyinOfPath: Pinyin;
};
}

type FileItem = Item & { type: "file" };
type AliasItem = Item & { type: "alias" };
type UnresolvedLinkItem = Item & { type: "unresolvedLink" };

export type MatchData = {
item: Item;
Expand Down Expand Up @@ -87,28 +91,19 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
this.scope.register(["Shift"], "Enter", async (e) => {
if (this.inputEl.value == "") return;
this.close();
let nf = await app.vault.create(
app.fileManager.getNewFileParent("").path + "/" + this.inputEl.value + ".md",
""
);
let nf = await createFile(this.inputEl.value);
app.workspace.getMostRecentLeaf().openFile(nf);
});
this.scope.register(["Mod", "Shift"], "Enter", async (e) => {
if (this.inputEl.value == "") return;
this.close();
let nf = await app.vault.create(
app.fileManager.getNewFileParent("").path + "/" + this.inputEl.value + ".md",
""
);
let nf = await createFile(this.inputEl.value);
app.workspace.getLeaf("tab").openFile(nf);
});
this.scope.register(["Shift", "Alt"], "Enter", async (e) => {
if (this.inputEl.value == "") return;
this.close();
let nf = await app.vault.create(
app.fileManager.getNewFileParent("").path + "/" + this.inputEl.value + ".md",
""
);
let nf = await createFile(this.inputEl.value);
getNewOrAdjacentLeaf(app.workspace.getMostRecentLeaf()).openFile(nf);
});
this.scope.register(["Alt"], "Enter", async (e) => {
Expand Down Expand Up @@ -259,15 +254,11 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
renderSuggestion(matchData: MatchData, el: HTMLElement) {
el.addClass("fz-item");
let renderer = new SuggestionRenderer(el);
if (matchData.score == -1) {
renderer.render(matchData);
return;
}
renderer.setNote(matchData.item.path);
if (matchData.item.file) renderer.setNote(matchData.item.path);
if (matchData.usePath) renderer.setToHighlightEl("note");
renderer.render(matchData);

if (this.plugin.settings.file.showTags) {
if (this.plugin.settings.file.showTags && matchData.item.file) {
let tags: string | Array<string> =
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tags ||
app.metadataCache.getFileCache(matchData.item.file)?.frontmatter?.tag,
Expand All @@ -287,7 +278,7 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
renderer.addIcon("forward");
if (!this.plugin.settings.file.showPath) renderer.flairEl.style.top = "9px";
if (renderer.noteEl) renderer.noteEl.style.width = "calc(100% - 30px)";
}
} else if (matchData.item.type == "unresolvedLink") renderer.addIcon("file-plus");
}
async onChooseSuggestion(matchData: MatchData, evt: MouseEvent | KeyboardEvent) {
let file = await this.getChoosenItemFile(matchData);
Expand All @@ -310,17 +301,9 @@ export default class FuzzyFileModal extends FuzzyModal<Item> {
}
async getChoosenItemFile(matchData?: MatchData): Promise<TFile> {
matchData = matchData ?? this.chooser.values[this.chooser.selectedItem];
let file =
matchData.score == -1
? await this.app.vault.create(
this.app.fileManager.getNewFileParent("").path +
"/" +
matchData.item.path +
".md",
""
)
: matchData.item.file;
return file;
return matchData.score == -1 || matchData.item.type == "unresolvedLink"
? await createFile(matchData.item.path)
: matchData.item.file;
}
}

Expand Down Expand Up @@ -369,10 +352,25 @@ const getNewOrAdjacentLeaf = (leaf: WorkspaceLeaf): WorkspaceLeaf => {
class PinyinIndex extends PI<Item> {
fileItems: FileItem[] = [];
aliasItems: AliasItem[] = [];
unresolvedLinkItems: UnresolvedLinkItem[] = [];
constructor(app: App, plugin: FuzzyChinesePinyinPlugin) {
super(app, plugin);
this.id = "file";
}
//@ts-ignore
get items() {
let items: Item[] = [];
if (this.plugin.settings.file.showUnresolvedLink)
items = items.concat(this.unresolvedLinkItems);
return items.concat(this.fileItems).concat(this.aliasItems);
}
set items(value: Item[]) {
this.fileItems = value.filter((item) => item.type === "file") as FileItem[];
this.aliasItems = value.filter((item) => item.type === "alias") as AliasItem[];
this.unresolvedLinkItems = value.filter(
(item) => item.type === "unresolvedLink"
) as UnresolvedLinkItem[];
}
initIndex() {
let files = this.app.vault.getFiles().filter((f) => this.isEffectiveFile(f));

Expand All @@ -384,13 +382,18 @@ class PinyinIndex extends PI<Item> {
CachedMetadata2Item(file, this.plugin, this.fileItems)
);
}

this.updateUnresolvedLinkItems();
}
initEvent() {
this.registerEvent(
this.metadataCache.on("changed", (file, _, cache) =>
this.update("changed", file, cache)
)
);
this.registerEvent(
this.metadataCache.on("resolved", () => this.updateUnresolvedLinkItems())
);
this.registerEvent(
this.vault.on("rename", (file, oldPath) => this.update("rename", file, oldPath))
);
Expand Down Expand Up @@ -430,6 +433,28 @@ class PinyinIndex extends PI<Item> {
}
}
}
updateUnresolvedLinkItems() {
this.unresolvedLinkItems = incrementalUpdate(
this.unresolvedLinkItems ?? [],
() => {
let unresolvedLinks = new Set<string>();
Object.values(this.metadataCache.unresolvedLinks)
.map((p) => Object.keys(p))
.filter((p) => p.length > 0)
.forEach((p) => p.forEach((q) => unresolvedLinks.add(q)));
return Array.from(unresolvedLinks);
},
(name) =>
<UnresolvedLinkItem>{
type: "unresolvedLink",
name,
pinyin: new Pinyin(name, this.plugin),
path: null,
pinyinOfPath: null,
file: null,
}
);
}

isEffectiveFile(file: TAbstractFile): file is TFile {
if (!(file instanceof TFile)) return false;
Expand Down Expand Up @@ -528,15 +553,3 @@ class TagSuggest extends TextInputSuggest<uMatchData<uItem>> {
this.close();
}
}

Object.defineProperty(PinyinIndex.prototype, "items", {
get: function () {
return [].concat(this.fileItems).concat(this.aliasItems);
},
set: function (value: Item[]) {
this.fileItems = value.filter((item) => item.type === "file");
this.aliasItems = value.filter((item) => item.type === "alias");
},
enumerable: true,
configurable: true,
});
16 changes: 9 additions & 7 deletions src/fuzzySuggestModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ export default class fuzzySuggestModal extends FuzzyModal<Item> {
super(app, plugin);
this.items = items;
this.index = {
items: text_items.map((p) => {
return { name: p, pinyin: new Pinyin(p, plugin) };
}),
items: text_items.map((p) => ({
name: p,
pinyin: new Pinyin(p, plugin),
})),
};
}
getEmptyInputSuggestions(): MatchData<Item>[] {
return this.index.items.map((p) => {
return { item: p, score: 0, range: null };
});
return this.index.items.map((p) => ({ item: p, score: 0, range: null }));
}
onChooseSuggestion(matchData: MatchData<Item>, evt: MouseEvent | KeyboardEvent): void {
let i = this.index.items.indexOf(matchData.item);
this.resolve(this.items[i]);
}
async openAndGetValue(resolve: (value?: string) => void, reject: (reason?: any) => void): Promise<void> {
async openAndGetValue(
resolve: (value?: string) => void,
reject: (reason?: any) => void
): Promise<void> {
this.resolve = resolve;
this.open();
}
Expand Down
Loading

0 comments on commit db13b33

Please sign in to comment.