obsidian-douban/src/org/wanxp/douban/setting/model/FileTreeSelectSuggest.ts
Wanxp a120e450e9 fix #139 #128
1. 优化配置将配置页面调整为tab
2. 优化文件夹选取方式避免卡顿
3. 优化模板文件选取方式避免卡顿
4. 增加设置的导入导出用以备份
5. 优化加载插件的卡顿问题,现在加载会非常快
6. 优化了配置的文本,简化了文本内容
2025-03-06 18:54:01 +08:00

79 lines
2.1 KiB
TypeScript

import {TAbstractFile, TFile, TFolder} from "obsidian";
import {TextInputSuggest} from "./TextInputSuggest";
export class FileTreeSelectSuggest extends TextInputSuggest<TAbstractFile> {
parentPath: string = "/";
getSuggestions(inputStr: string): TAbstractFile[] {
const files: TAbstractFile[] = [];
if (inputStr.length == 0 || inputStr.trim().length == 0 || inputStr.trim() == '/') {
this.searchFiles(this.app.vault.getRoot(), "", files);
return files;
}
let parentSearchPath:string = null;
let currentName:string = null;
try {
const testFile = this.app.vault.getAbstractFileByPath(inputStr.trim())
if (testFile) {
if (testFile instanceof TFile && testFile.name.endsWith(".md")) {
files.push(testFile);
return files;
}
if (testFile instanceof TFolder) {
parentSearchPath = inputStr.trim();
currentName = "";
}
}
}catch (e) {
}
if (parentSearchPath == null) {
parentSearchPath = inputStr.lastIndexOf("/") > 0 ? inputStr.substring(0, inputStr.lastIndexOf("/")) : "/";
currentName = inputStr.lastIndexOf("/") > 0 ? inputStr.substring(inputStr.lastIndexOf("/") + 1) : inputStr;
currentName = currentName.trim();
}
if (currentName == null) {
currentName = "";
}
const root = this.app.vault.getAbstractFileByPath(parentSearchPath) as TFolder;
if (!root) {
return [];
}
const name = currentName.toLowerCase();
if (root) {
this.searchFiles(root, name, files);
}
return files;
}
searchFiles(folder: TFolder, name: string, files: TAbstractFile[]): void {
folder.children.forEach((file: TAbstractFile) => {
if (file.name.toLowerCase().contains(name)) {
files.push(file);
}
});
}
renderSuggestion(file: TAbstractFile, el: HTMLElement): void {
el.setText(file.path);
}
selectSuggestion(file: TAbstractFile): void {
this.inputEl.value = file.path;
this.parentPath = file.path;
// this.inputEl.addEventListener("change", () => {
// this.onInputChanged()
// })
if (file instanceof TFolder) {
this.inputEl.value += "/";
this.inputEl.trigger("input");
}else {
this.close();
}
}
}