-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
209 lines (180 loc) · 5.25 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
interface AttachMateSettings {
fileExtensions: string;
insertAltText: boolean;
}
const DEFAULT_SETTINGS: AttachMateSettings = {
fileExtensions: "msg,eml",
insertAltText: true,
};
export default class AttachMate extends Plugin {
settings: AttachMateSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new settingTab(this.app, this));
this.app.workspace.on("editor-drop", (evt, editor, data) => {
const cursorPosition = editor.getCursor();
// Extract the file name from the drop event
let fileName = null;
if (
evt.dataTransfer &&
evt.dataTransfer.files &&
evt.dataTransfer.files.length > 0
) {
fileName = evt.dataTransfer.files[0].name;
}
// Check if the file name ends with the correct ending
let foundRightEnding = false;
if (this.settings.fileExtensions == "*") {
foundRightEnding = true;
} else {
const endingsInSettings =
this.settings.fileExtensions.split(",");
for (let i = 0; i < endingsInSettings.length; i++) {
if (fileName && fileName.endsWith(endingsInSettings[i])) {
foundRightEnding = true;
break;
}
}
}
if (!foundRightEnding) {
return;
}
// Get the element that represents the editor content
const editorElement = editor.containerEl;
// Create a MutationObserver to watch for changes in the editor
const observer = new MutationObserver((mutations) => {
for (let mutation of mutations) {
if (
mutation.type === "childList" ||
mutation.type === "characterData"
) {
// The drop has completed, execute code
const lineText = editor.getLine(cursorPosition.line);
// Split the line text into before and after the cursor position
const beforeCursor = lineText.substring(
0,
cursorPosition.ch
);
const afterCursor = lineText.substring(
cursorPosition.ch
);
// Perform the replacement only on the part after the cursor
const modifiedAfterCursor = afterCursor.replace(
"![[",
"[["
);
// Combine the parts to form the modified line text
const modifiedLineText =
beforeCursor + modifiedAfterCursor;
editor.setLine(cursorPosition.line, modifiedLineText);
// Recalculate the line text and find the position of the newly created link
const updatedLineText = editor.getLine(
cursorPosition.line
);
const positionOfNewLink = updatedLineText.indexOf(
"[[",
cursorPosition.ch
);
const positionOfClosingBrackets =
updatedLineText.indexOf("]]", positionOfNewLink);
editor.focus();
if (
positionOfNewLink !== -1 &&
positionOfClosingBrackets !== -1
) {
// Set the cursor to the position of the "]]" of the newly created link
editor.setCursor({
line: cursorPosition.line,
ch: positionOfClosingBrackets,
});
editor.replaceRange(
"|attachment",
editor.getCursor()
);
// Set cursor within the newly added link
setTimeout(() => {
const newLineText = editor.getLine(
cursorPosition.line
);
const positionOfDivider = newLineText.indexOf(
"|attachment]]",
positionOfNewLink
);
// Define the start and end positions of the selection
const start = {
line: cursorPosition.line,
ch: positionOfDivider + 1,
};
const end = {
line: cursorPosition.line,
ch: positionOfDivider + 11, // +11 to match the word "attachment"
};
// Set the selection in CodeMirror
editor.setSelection(start, end);
}, 10);
}
// Stop observing after the change is detected and handled
observer.disconnect();
break;
}
}
});
// Configure the observer to watch for changes in child elements and text content
observer.observe(editorElement, {
childList: true,
characterData: true,
subtree: true,
});
});
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class settingTab extends PluginSettingTab {
plugin: AttachMate;
constructor(app: App, plugin: AttachMate) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("File endings to react to")
.setDesc(
"Enter the file endings to react to, separated by commas. Use * to react to all files."
)
.addText((text) =>
text
.setPlaceholder("msg,eml")
.setValue(this.plugin.settings.fileExtensions)
.onChange(async (value) => {
this.plugin.settings.fileExtensions = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Insert alternative text")
.setDesc(
"Automatically move cursor to be able to enter display name"
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.insertAltText)
.onChange(async (value) => {
this.plugin.settings.insertAltText = value;
await this.plugin.saveSettings();
})
);
}
}