Skip to content

Commit

Permalink
feat: 快捷键可以移动文件列表被选择的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyloong committed Jan 8, 2024
1 parent 663c283 commit bbeba1c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Obsidian -> 选项 -> 快捷键 -> 搜索 `Fuzzy` -> 给 `Fuzzy Chinese Pinyin:
### 文件移动

命令 `Fuzzy Chinese Pinyin: Move File`,效果类似命令 `将文件移动到其他文件夹`
在文件列表中选择文件后,右键可一起移动,若设置了快捷键也可以快捷键打开,优先级大于当前页面文件。

### 命令搜索

Expand Down
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.12.0"
"version": "2.13.0"
}
4 changes: 4 additions & 0 deletions src/fuzzyFolderModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default class FuzzyFolderModal extends FuzzyModal<Item> {
else app.vault.rename(this.toMoveFiles, matchData.item.name + "/" + this.toMoveFiles.name);
this.toMoveFiles = null;
}
openWithFiles(files: TAbstractFile | TFile[]) {
this.toMoveFiles = files;
this.open();
}
}

class PinyinIndex extends PI<Item> {
Expand Down
58 changes: 50 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { Plugin, EditorSuggest, WorkspaceLeaf, Menu, TFile, TAbstractFile } from "obsidian";
import {
Plugin,
EditorSuggest,
WorkspaceLeaf,
Menu,
TFile,
TAbstractFile,
Scope,
App,
KeymapEventHandler,
View,
Hotkey,
} from "obsidian";
import { fullPinyin2doublePinyin, Item, PinyinIndex, runOnLayoutReady } from "./utils";
import FuzzyModal from "./fuzzyModal";
import FuzzyFileModal from "./fuzzyFileModal";
Expand Down Expand Up @@ -100,6 +112,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
tagEditorSuggest: TagEditorSuggest;
indexManager: IndexManager;
editorSuggests: EditorSuggest<any>[];
fileExplorerHotkey: FileExplorerHotkey;

async onload() {
await this.loadSettings();
Expand Down Expand Up @@ -138,6 +151,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
} else {
this.indexManager.load();
}
this.registerFileExplorer();
});
this.addCommand({
id: "open-search",
Expand All @@ -157,8 +171,14 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
id: "move-file",
name: "Move File",
checkCallback: (checking: boolean) => {
let file = app.workspace.getActiveFile();
if (file) {
let files = this.fileExplorerHotkey.getFiles();
let file = this.app.workspace.getActiveFile();
if (this.fileExplorerHotkey.working && files.length > 0) {
if (!checking) {
this.folderModal.openWithFiles(files);
}
return true;
} else if (file) {
if (!checking) {
this.folderModal.open();
}
Expand Down Expand Up @@ -242,8 +262,7 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
item.setIcon("folder-tree")
.setTitle("FuzzyPinyin: 移动" + title)
.onClick(() => {
this.folderModal.toMoveFiles = file;
this.folderModal.open();
this.folderModal.openWithFiles(file);
});
});
})
Expand All @@ -252,15 +271,38 @@ export default class FuzzyChinesePinyinPlugin extends Plugin {
this.app.workspace.on("files-menu", (menu: Menu, files: TFile[]) => {
menu.addItem((item) => {
item.setIcon("folder-tree")
.setTitle(`FuzzyPinyin: 移动${files.length}个文件`)
.setTitle(`FuzzyPinyin: 移动 ${files.length} 个文件`)
.onClick(() => {
this.folderModal.toMoveFiles = files;
this.folderModal.open();
this.folderModal.openWithFiles(files);
});
});
})
);
}
registerFileExplorer() {
this.fileExplorerHotkey = new FileExplorerHotkey(this.app, this);
}
}

class FileExplorerHotkey {
plugin: FuzzyChinesePinyinPlugin;
app: App;
leaf: WorkspaceLeaf;
view: View;
working = false;
constructor(app: App, plugin: FuzzyChinesePinyinPlugin) {
this.plugin = plugin;
this.app = app;
this.app.workspace.iterateAllLeaves((leaf) => {
if (leaf.view?.tree?.id == "file-explorer") this.leaf = leaf;
});
if (!this.leaf) return;
this.working = true;
this.view = this.leaf.view;
}
getFiles(): TFile[] {
return Array.from(this.view.tree.selectedDoms).map((p) => p.file);
}
}

class IndexManager extends Array<PinyinIndex<any>> {
Expand Down

0 comments on commit bbeba1c

Please sign in to comment.