forked from klaudiosinani/tusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.js
81 lines (70 loc) · 1.79 KB
/
save.js
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
'use strict';
const {writeFile} = require('fs');
const electron = require('electron');
const Turndown = require('turndown');
const {is} = require('./util');
const mode = require('./mode');
const {dialog} = electron.remote;
const {log} = console;
class Save {
get _opts() {
return {
md: {
filters: [
{
name: 'Markdown File',
extensions: ['md']
}, {
name: 'All Files',
extensions: ['*']
}
]
},
html: {
filters: [
{
name: 'HTML File',
extensions: ['html']
}, {
name: 'All Files',
extensions: ['*']
}
]
}
};
}
_getTitle() {
const title = document.querySelector('#gwt-debug-NoteTitleView-label').innerHTML;
return title.length > 0 ? title.trim().replace(/ /g, ' ') : 'note';
}
_toHTML(noteFrame) {
return noteFrame.contentDocument.body.innerHTML;
}
_toMarkdown(noteFrame) {
const turndownUtil = new Turndown();
return turndownUtil.turndown(noteFrame.contentDocument.body);
}
_save(opts, data) {
const options = Object.assign(opts, {defaultPath: this._getTitle()});
dialog.showSaveDialog(options, path => {
if (is.undef(path)) {
return;
}
writeFile(path, data, error => {
if (error) {
dialog.showErrorBox('Failed to export note.', error.message);
return log(error);
}
});
});
}
async md() {
const noteFrame = await mode.getNoteFrame();
return this._save(this._opts.md, this._toMarkdown(noteFrame));
}
async html() {
const noteFrame = await mode.getNoteFrame();
return this._save(this._opts.html, this._toHTML(noteFrame));
}
}
module.exports = new Save();