This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
218 lines (190 loc) · 5.76 KB
/
main.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
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
/* Copyright (c) 2013-present The TagSpaces Authors.
* Use of this source code is governed by the MIT license which can be found in the LICENSE.txt file. */
/* globals Mousetrap, initI18N, $, isWeb, sendMessageToHost, getParameterByName, RTFJS, setContent */
'use strict';
sendMessageToHost({ command: 'loadDefaultTextContent' });
let $rtfContent;
$(document).ready(() => {
const locale = getParameterByName('locale');
// const filePath = getParameterByName('file');
// const searchQuery = getParameterByName('query');
let extSettings;
initI18N(locale, 'ns.viewerRTF.json');
loadExtSettings();
$rtfContent = $('#rtfContent');
const zoomSteps = [
'zoomSmallest',
'zoomSmaller',
'zoomSmall',
'zoomDefault',
'zoomLarge',
'zoomLarger',
'zoomLargest'
];
let currentZoomState = 3;
if (extSettings && extSettings.zoomState) {
currentZoomState = extSettings.zoomState;
}
$rtfContent.removeClass();
$rtfContent.addClass('markdown ' + zoomSteps[currentZoomState]);
$('#zoomInButton').off();
$('#zoomInButton').on('click', () => {
currentZoomState += 1;
if (currentZoomState >= zoomSteps.length) {
currentZoomState = 6;
}
$rtfContent.removeClass();
$rtfContent.addClass('markdown ' + zoomSteps[currentZoomState]);
saveExtSettings();
});
$('#zoomOutButton').off();
$('#zoomOutButton').on('click', () => {
currentZoomState -= 1;
if (currentZoomState < 0) {
currentZoomState = 0;
}
$rtfContent.removeClass();
$rtfContent.addClass('markdown ' + zoomSteps[currentZoomState]);
saveExtSettings();
});
$('#zoomResetButton').off();
$('#zoomResetButton').on('click', () => {
currentZoomState = 3;
$rtfContent.removeClass();
$rtfContent.addClass('markdown ' + zoomSteps[currentZoomState]);
saveExtSettings();
});
function saveExtSettings() {
const settings = {
zoomState: currentZoomState
};
localStorage.setItem('viewerHTMLSettings', JSON.stringify(settings));
}
function loadExtSettings() {
extSettings = JSON.parse(localStorage.getItem('viewerHTMLSettings'));
}
});
// fixing embedding of local images
function fixingEmbeddingOfLocalImages($rtfContentPar, fileDirectory) {
const hasURLProtocol = url =>
url.indexOf('http://') === 0 ||
url.indexOf('https://') === 0 ||
url.indexOf('file://') === 0 ||
url.indexOf('data:') === 0;
$rtfContentPar.find('img[src]').each((index, link) => {
const currentSrc = $(link).attr('src');
if (!hasURLProtocol(currentSrc)) {
const path = (isWeb ? '' : 'file://') + fileDirectory + '/' + currentSrc;
$(link).attr('src', path);
}
});
/* $rtfContentPar.find('a[href]').each((index, link) => {
let currentSrc = $(link).attr('href');
let path;
if (!hasURLProtocol(currentSrc)) {
const path1 = (isWeb ? '' : 'file://') + fileDirectory + '/' + currentSrc;
$(link).attr('href', path1);
}
$(link).off();
$(link).on('click', e => {
e.preventDefault();
if (path) {
currentSrc = encodeURIComponent(path);
}
const msg = { command: 'openLinkExternally', link: currentSrc };
sendMessageToHost(msg);
});
}); */
}
function stringToBinaryArray(string) {
const buffer = new ArrayBuffer(string.length);
const bufferView = new Uint8Array(buffer);
for (let i = 0; i < string.length; i += 1) {
bufferView[i] = string.charCodeAt(i);
}
return buffer;
}
function setPictBorder(elem, show) {
return elem.css('border', show ? '1px dotted red' : 'none');
}
function setUnsafeLink(elem, warn) {
return elem.css('border', warn ? '1px dashed red' : 'none');
}
function displayRtfFile(blob) {
try {
const showPicBorder = $('#showpicborder').prop('checked');
const warnHttpLinks = $('#warnhttplink').prop('checked');
const settings = {
onPicture: create => {
const elem = create().attr('class', 'rtfpict'); // WHY does addClass not work on <svg>?!
return setPictBorder(elem, showPicBorder);
},
onHyperlink: (create, hyperlink) => {
const url = hyperlink.url();
const lnk = create();
const span = setUnsafeLink(
$('<span>')
.addClass('unsafelink')
.append(lnk),
warnHttpLinks
);
span.click(evt => {
evt.preventDefault();
sendMessageToHost({ command: 'openLinkExternally', link: url });
});
return {
content: lnk,
element: span
};
}
};
const doc = new RTFJS.Document(blob, settings);
let haveMeta = false;
const meta = doc.metadata();
for (let prop in meta) {
console.log(meta);
$('#rtfContent').append(
$('<div>')
.append($('<span>').text(prop + ': '))
.append($('<span>').text(meta[prop].toString()))
);
haveMeta = true;
}
if (haveMeta) {
$('#havemeta').show();
}
$('#rtfContent')
.empty()
.append(doc.render());
$('#closebutton').show();
$('#tools').show();
console.log('All done!');
} catch (e) {
if (e instanceof RTFJS.Error) {
console.log('Error: ' + e.message);
$('#content').text('Error: ' + e.message);
} else {
throw e;
}
}
}
function setContent(content, fileDir) {
$rtfContent = $('#rtfContent');
displayRtfFile(stringToBinaryArray(content));
let fileDirectory = fileDir;
if (fileDirectory.indexOf('file://') === 0) {
fileDirectory = fileDirectory.substring(
'file://'.length,
fileDirectory.length
);
}
fixingEmbeddingOfLocalImages($rtfContent, fileDirectory);
/* Mousetrap.bind(['command++', 'ctrl++'], (e) => {
increaseFont();
return false;
});
Mousetrap.bind(['command+-', 'ctrl+-'], (e) => {
decreaseFont();
return false;
}); */
}