Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 26e6cf9

Browse files
committed
feat: support drag folder to editor to insert link
close #45
1 parent 81f125f commit 26e6cf9

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

src/drag-patch.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { around } from "monkey-around";
2+
import "obsidian";
3+
import { DragManager, TFile, TFolder } from "obsidian";
4+
import type ALxFolderNote from "./fn-main";
5+
6+
declare module "obsidian" {
7+
interface App {
8+
dragManager: DragManager;
9+
}
10+
interface DragInfo {
11+
source: string | undefined;
12+
type: string;
13+
icon: string;
14+
title: string;
15+
}
16+
interface DragFolderInfo extends DragInfo {
17+
type: "folder";
18+
file: TFolder;
19+
}
20+
interface DragFileInfo extends DragInfo {
21+
type: "file";
22+
file: TFile;
23+
}
24+
interface DragFilesInfo extends DragInfo {
25+
type: "files";
26+
files: TFile[];
27+
}
28+
class DragManager {
29+
dragFile(evt: DragEvent, file: TFile, source?: string): DragFolderInfo;
30+
dragFiles(evt: DragEvent, files: TFile[], source?: string): DragFilesInfo;
31+
dragFolder(
32+
evt: DragEvent,
33+
folder: TFolder,
34+
source?: string,
35+
): DragFolderInfo;
36+
}
37+
}
38+
39+
const PatchDragManager = (plugin: ALxFolderNote) => {
40+
const { getFolderNote } = plugin.CoreApi;
41+
42+
plugin.register(
43+
around(plugin.app.dragManager.constructor.prototype as DragManager, {
44+
dragFolder: (next) =>
45+
function (this: DragManager, evt, folder, source, ...args) {
46+
const fallback = () => next.call(this, evt, folder, source, ...args);
47+
try {
48+
let note = getFolderNote(folder);
49+
if (note) {
50+
return this.dragFile(evt, note, source);
51+
} else {
52+
return fallback();
53+
}
54+
} catch (error) {
55+
console.error(error);
56+
return fallback();
57+
}
58+
},
59+
// dragFiles: (next) =>
60+
// function (this: DragManager, evt, files, source, ...args) {
61+
// const fallback = () => next.call(this, evt, files, source, ...args);
62+
// try {
63+
// let dragedFiles = files as (TFile | number)[];
64+
// let pathFolderNoteMap: Map<string, TFile> = new Map();
65+
// let index = -1;
66+
// // find all folder with note,
67+
// // save folder note in map,
68+
// // and replace them with index to map sequence
69+
// for (let i = 0; i < files.length; i++) {
70+
// const af = dragedFiles[i];
71+
// let note;
72+
// if (af instanceof TFolder && (note = getFolderNote(af))) {
73+
// pathFolderNoteMap.set(note.path, note) && index++;
74+
// dragedFiles[i] = index;
75+
// }
76+
// }
77+
// if (index < 0) return fallback();
78+
// // filter out duplicate folder note
79+
// dragedFiles = dragedFiles.filter(
80+
// (af) => typeof af === "number" || !pathFolderNoteMap.has(af.path),
81+
// );
82+
// // put folder note back to original sequence
83+
// const folderNotes = [...pathFolderNoteMap.values()];
84+
// for (let i = 0; i < dragedFiles.length; i++) {
85+
// const file = dragedFiles[i];
86+
// if (typeof file === "number")
87+
// dragedFiles[file] = folderNotes[file];
88+
// }
89+
// } catch (error) {
90+
// fallback();
91+
// }
92+
// return fallback();
93+
// },
94+
}),
95+
);
96+
};
97+
export default PatchDragManager;

src/fe-patch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const resetFileExplorer = async (plugin: ALxFolderNote) => {
3434
}
3535
};
3636

37-
export const monkeyPatch = (plugin: ALxFolderNote) => {
37+
const PatchFileExplorer = (plugin: ALxFolderNote) => {
3838
const { getFolderFromNote } = plugin.CoreApi,
3939
clickHandler = getClickHandler(plugin);
4040

@@ -140,3 +140,4 @@ export const monkeyPatch = (plugin: ALxFolderNote) => {
140140
resetFileExplorer(plugin);
141141
});
142142
};
143+
export default PatchFileExplorer;

src/fn-main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FolderNoteAPI, getApi as getFNCApi } from "@aidenlx/folder-note-core";
44
import { getApi as getISCApi } from "@aidenlx/obsidian-icon-shortcodes";
55
import { Notice, Plugin } from "obsidian";
66

7-
import { monkeyPatch } from "./fe-patch";
7+
import PatchFileExplorer from "./fe-patch";
88
import { ClickNotice } from "./misc";
99
import registerSetFolderIconCmd from "./modules/set-folder-icon";
1010
import {
@@ -14,6 +14,7 @@ import {
1414
MobileNoClickMark,
1515
noHideNoteMark,
1616
} from "./settings";
17+
import PatchDragManager from "./drag-patch";
1718

1819
const foldervNotifiedKey = "foldervNotified";
1920

@@ -72,7 +73,7 @@ export default class ALxFolderNote extends Plugin {
7273
initialized = false;
7374
initialize() {
7475
if (this.initialized) return;
75-
monkeyPatch(this);
76+
PatchFileExplorer(this);
7677
document.body.toggleClass(
7778
MobileNoClickMark,
7879
!this.settings.mobileClickToOpen,
@@ -98,6 +99,7 @@ export default class ALxFolderNote extends Plugin {
9899
this.addSettingTab(tab);
99100
registerSetFolderIconCmd(this);
100101

102+
PatchDragManager(this);
101103
this.app.workspace.onLayoutReady(this.initialize.bind(this));
102104
this.noticeFoldervChange();
103105
}

0 commit comments

Comments
 (0)