-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.ts
313 lines (272 loc) · 10.8 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const spawn = require('child_process').spawn;
const path = require('path');
//import * as moment from 'moment';
import { App, Modal, Notice, Plugin, PluginSettingTab, Editor,
Setting, MarkdownView, MarkdownSourceView, FileSystemAdapter } from 'obsidian';
import * as obsidian from 'obsidian';
import moment from 'moment';
interface MyPluginSettings {
reSnapPath: string;
invertRemarkableImages: boolean;
outputPath: string;
rmAddress: string;
postprocessor: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
reSnapPath: '',
invertRemarkableImages: true,
outputPath: '.',
rmAddress: '10.11.99.1',
postprocessor: ''
}
function mkCheckCallback(innerFn: () => any): (checking: boolean) => boolean {
return function checkCallback(checking: boolean): boolean {
let leaf = this.app.workspace.activeLeaf;
let view = leaf.view;
if (leaf) {
const result = view instanceof MarkdownView && view.currentMode instanceof MarkdownSourceView;
if (result && !checking) {
innerFn.call(this);
}
return result;
}
return false;
};
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
cm: CodeMirror.Editor;
async onload() {
await this.loadSettings();
const plugin = this;
this.addCommand({
id: 'insert-remarkable-drawing',
name: 'Insert a drawing from the reMarkable',
callback: () => {
plugin.tryInsertingDrawing(false)
}
});
this.addCommand({
id: 'insert-remarkable-drawing-landscape',
name: 'Insert a landscape-format drawing from the reMarkable',
callback: () => {
plugin.tryInsertingDrawing(true)
}
});
this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerCodeMirror((cm: CodeMirror.Editor) => {
this.cm = cm;
});
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
//console.log('click', evt);
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async runProcess(executable_path: string, args: string[]): Promise<Record<'stderr' | 'stdout', string>> {
let outputs: Record<'stderr' | 'stdout', string> = {
'stderr': '',
'stdout': ''
};
return new Promise(function (resolve, reject) {
const process = spawn(executable_path, args);
process.stdout.on('data', (data: string) => { outputs.stdout += data; });
process.stderr.on('data', (data: string) => { outputs.stderr += data; });
process.on('close', async function (code: number) {
if(code === 0) {
resolve(outputs);
}
else {
reject("Nonzero exitcode.\nSTDERR: " + outputs.stderr
+ "\nSTDOUT: " + outputs.stdout);
}
});
process.on('error', function (err: string) {
reject(err);
});
});
}
async callReSnap(landscape: boolean) {
const { reSnapPath, rmAddress } = this.settings;
const { spawn } = require('child_process');
let vaultAbsPath;
const adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
vaultAbsPath = adapter.getBasePath();
}
else {
// Not on desktop, thus there is no basePath available. Cancel execution.
new Notice('Could not get vault path! Is this running on mobile...?');
return;
}
const now = moment();
const drawingFileName = `rM drawing ${now.format("YYYY-MM-DD-HH.mm.ss")}.png`;
const absOutputFolderPath = adapter.getFullRealPath(this.settings.outputPath);
const drawingFilePath = path.join(absOutputFolderPath, drawingFileName);
let args = ['-o', drawingFilePath, '-s', rmAddress];
if(landscape) {
args = args.concat(['-l']);
}
const { stderr, stdout } = await this.runProcess(reSnapPath, args);
return { drawingFilePath, drawingFileName };
}
async postprocessDrawing(drawingFilePath: string) {
const { postprocessor } = this.settings;
if (postprocessor) {
const args = [drawingFilePath];
const { stderr, stdout } = await this.runProcess(postprocessor, args);
}
return true;
}
async tryInsertingDrawing(landscape: boolean) {
let success = false;
new Notice('Inserting rM drawing...', 1000);
try {
// remember the editor here, so the user could change mode (e.g. preview mode)
// in the meantime without an error
const editor = this.editor;
const { drawingFilePath, drawingFileName } = await this.callReSnap(landscape);
await this.postprocessDrawing(drawingFilePath); // no-op if no postprocessor set
editor.replaceRange(`![[${drawingFileName}]]`, editor.getCursor());
new Notice('Inserted your rM drawing!');
return true;
} catch(error) {
new Notice('Could not insert your rM drawing! Is your tablet connected ' +
'and reachable at the configured address?');
throw error;
return false;
}
}
/* Taken and adapted from hans/obsidian-citation-plugin. Cheers! */
get editor(): Editor {
const view = this.app.workspace.activeLeaf.view;
try {
if (view.editMode.type == "source") {
return view.editor;
}
else {
return null;
}
}
catch (error) {
return null;
}
}
/* Taken from hans/obsidian-citation-plugin. Cheers! */
resolveLibraryPath(rawPath: string): string {
const vaultRoot =
this.app.vault.adapter instanceof FileSystemAdapter
? this.app.vault.adapter.getBasePath()
: '/';
return path.resolve(vaultRoot, rawPath);
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
outputPathInfo: HTMLElement;
outputPathError: HTMLElement;
outputPathSuccess: HTMLElement;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
/**
* Taken and modified from hans/obsidian-citation-plugin. Cheers!
* Returns true iff the path exists (relative to the vault directory).
* Displays error/success/info in the settings as a side-effect.
*/
async checkOutputFolder(outputFolder: string): Promise<boolean> {
this.outputPathInfo.addClass('d-none');
try {
const adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
const resolvedPath = outputFolder;//this.plugin.resolveLibraryPath(outputFolder);
const stat = await adapter.stat(resolvedPath);
if(stat.type !== 'folder') { throw new Error('Chosen output folder is not a folder!'); }
}
else {
throw new Error('Could not get FileSystemAdapter! Is this running on mobile...?');
}
} catch (e) {
this.outputPathSuccess.style.display = "none";
this.outputPathError.style.display = "block";
return false;
} finally {
this.outputPathInfo.style.display = "none";
}
return true;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Obsidian & reMarkable'});
new Setting(containerEl)
.setName('reMarkable IP')
.setDesc('The IP address of your reMarkable. Use 10.11.99.1 and connect via cable if unsure.')
.addText(text => text
.setPlaceholder('Example: 10.11.99.1')
.setValue(this.plugin.settings.rmAddress)
.onChange(async (value) => {
this.plugin.settings.rmAddress = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('reSnap executable')
.setDesc('The path to the reSnap executable')
.addText(text => text
.setPlaceholder('Paste in the absolute path to reSnap.sh')
.setValue(this.plugin.settings.reSnapPath)
.onChange(async (value) => {
this.plugin.settings.reSnapPath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Output folder')
.setDesc('The folder where rM drawing images should be stored')
.addText(text => text
.setPlaceholder('Some folder from your Vault')
.setValue(this.plugin.settings.outputPath)
.onChange(async (value) => {
let success = await this.checkOutputFolder(value);
if(success) {
this.plugin.settings.outputPath = value;
await this.plugin.saveSettings();
this.outputPathError.style.display = "none";
this.outputPathSuccess.style.display = "block";
}
}));
this.outputPathInfo = containerEl.createEl('p', {
cls: 'remarkable-output-path-info d-none',
text: 'Checking output folder...',
});
this.outputPathError = containerEl.createEl('p', {
cls: 'remarkable-output-path-error d-none',
text: 'The output folder does not seem to exist. ' +
'Please type in a path to a folder that exists inside the vault.'
});
this.outputPathSuccess = containerEl.createEl('p', {
cls: 'remarkable-output-path-success d-none',
text: 'Successfully set the output folder.',
});
this.outputPathInfo.style.display = "none";
this.outputPathError.style.display = "none";
this.outputPathSuccess.style.display = "none";
new Setting(containerEl)
.setName('Postprocessing script')
.setDesc('The absolute path to a script that post-processes the captured image. ' +
'The script will be passed the filename and should overwrite the file with a modified version.')
.addText(text => text
.setPlaceholder('/some/path/to/some/script')
.setValue(this.plugin.settings.postprocessor)
.onChange(async (value) => {
this.plugin.settings.postprocessor = value;
await this.plugin.saveSettings();
}));
}
}