-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
356 lines (309 loc) · 8.15 KB
/
editor.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
var previewWindow = null
var currentState = null
var docTitle = ''
var custMenuItems = [
{
name: 'Rename',
func: rename,
},
{
name: 'Save',
func: saveFile,
},
{
name: 'Delete',
func: deleteFile,
},
{
name: 'Upload',
func: upload,
},
{
name: 'Download',
func: download,
},
{
name: 'Share',
func: getShareLink,
}
]
tinymce.init({
selector: 'textarea',
resize: false,
plugins: [
'link', 'image', 'lists', 'anchor', 'media', 'table', 'emoticons',
'preview', 'pagebreak', 'fullscreen',
'autolink',
'searchreplace', 'wordcount',
'visualblocks', 'visualchars', 'code',
'insertdatetime',
'help'
],
menu: {
newFile: { title: 'File', items: 'rename save delete | upload download | share print' }
},
menubar: 'newFile edit view insert format tools table help',
placeholder: 'Type here...',
content_style: '.toolbar { border-bottom: none; }',
setup: (editor) => {
currentState = 'editor'
editor.on('change', () => {
saveFile(editor);
});
addMenuItems(editor)
}
});
var supptdImgExts = [
['avif'],
['bmp'],
['gif'],
['ico', 'cur'],
['jpg', 'jpeg', 'jpe', 'jfif', 'pjpeg', 'pjp'],
['apng', 'png'],
['webp'],
['tif', 'tiff'],
['xbm'],
]
var docTitle = document.title
var theAlert = false
var container = document.querySelector('.container')
var textarea = container.querySelector('textarea')
function rename(newName) {
if (newName) {
docTitle = newName
}
else {
docTitle = prompt("Rename Document", docTitle)
}
}
function download() {
let input = tinymce.activeEditor.getContent({ format: 'html' });
// create a new Blob object with the content you want to assign
let blob = new Blob([input], {type: "text/plain"});
// create a FileReader object
let reader = new FileReader();
// when the read operation is finished, this will be called
reader.onloadend = function() {
// the result attribute contains the contents of the file
}
// read the file as text
reader.readAsText(blob);
let link = document.querySelector('a#downloader');
link.href = window.URL.createObjectURL(blob);
let title = docTitle
if (!!title === false) title = 'New Text File'
link.download = `${title}.tnynpd`;
if (!!title) {
link.click();
}
}
function setTitle() {
let title = docTitle
if (title.split('').length <= 0) {
document.title = docTitle
}
else {
if (docTitle.includes('.')) {
ext = ''
}
document.title = `${title} | ${docTitle}`
}
ext = title.split('.').slice(-1)
}
var readfile = document.querySelector("input[type='file']#readfile");
readfile.onchange = object => {
// getting a hold of the file reference
var file = object.target.files[0];
if (!file) return
let filename = file.name
var ext = filename.split('.').slice(-1).join('.')
let title = ''
if (filename.split('.').slice(-1) === 'txt') {
title = filename.split('.').slice(0, -1).join('.')
}
else {
title = filename
}
currentState = checkImage(object, file, filename, currentState)
if (currentState !== 'image') {
// setting up the reader
var reader = new FileReader();
reader.readAsText(file,'UTF-8');
// here we tell the reader what to do when it's done reading...
reader.onload = readerEvent => {
var text = readerEvent.target.result
if (ext === 'txt') {
if (text.includes('\n')) {
text = text.split('\n').join('<br>')
}
}
tinymce.activeEditor.setContent(text);
}
}
if (title.split('').length <= 0) {
document.title = docTitle
}
else {
document.title = `${title} | ${docTitle}`
}
docTitle = title
}
function showImage(src) {
var image = document.querySelector('.image-preview')
image.src = src
image.alt = 'Image Preview'
image.setAttribute('shown', '')
}
function checkImage(element, file, name, state) {
var ext = name
if (ext.includes('.')) {
ext = ext.split('.').slice(-1)
}
supptdImgExts.forEach(function(g) {
g.forEach(function(e, i) {
if (ext == e) {
state = 'image'
}
})
})
let reader = new FileReader();
reader.onload = function () {
var path = reader.result//.replace('data:', '').replace(/^.+,/, "");
if (currentState === 'image') {
showImage(path)
saveFile({type: 'image', content: path})
}
}
reader.readAsDataURL(file);
return state
}
function previewHTML() {
let title = docTitle
let ext = ''
if (title.includes('.')) {
ext = title.split('.')[1]
}
let value = tinymce.activeEditor.getContent({ format: 'html' });
textarea = document.querySelector('textarea')
let eleVal = 'documentElement'
if (value.includes('<html')) {
value = value.split(`<html${value.split('<html')[1].split('>')[0]}>\n`)[1]
if (value.includes('</html>')) {
value = value.split('</html>')[0]
}
}
else if (value.includes('<xml')) {
value = value.split(`<xml${value.split('<xml')[1].split('>')[0]}>\n`)[1]
}
else if (value.includes('<?xml')) {
value = value.split(`<?xml${value.split('<?xml')[1].split('>')[0]}>\n`)[1]
}
if (value.includes('<svg')) {
eleVal = 'body'
}
previewWindow = open(`/preview.html?eleval=${eleVal}&content=${btoa(value)}`)
}
function outputsize() {
if (currentState !== 'image' && document.querySelector('textarea').clientWidth < 150) {
textarea.style.width = `${150}px`
}
}
outputsize()
new ResizeObserver(outputsize).observe(textarea)
function saveFile(info) {
let value = tinymce.activeEditor.getContent({ format: 'text' });
var type = 'text'
if (!!info) {
if (!!info.type) {
type = info.type
if (type === 'image') {
if (!!info.content) {
value = info.content
}
}
}
}
let title = docTitle
if (title === '' || !!title === false || title === null) return
let author = localStorage.getItem('username')
if (!!author === false) author = ''
let d = new Date()
let json = {
title: title,
content: value,
author: author,
dateModofied: `${d.getMonth()+1}/${d.getDate()}/${d.getFullYear()}`
}
localStorage.setItem(`FILEDATA://${title}`, JSON.stringify(json))
let filesObj = localStorage.getItem('files')
filesObj = JSON.parse(filesObj)
if (!!filesObj === false) {
filesObj = []
}
let hasFile = false
filesObj.forEach(function(f, i) {
if (f === title) {
hasFile = true
}
})
if (hasFile === false) {
filesObj.push(title)
}
localStorage.setItem('files', JSON.stringify(filesObj))
}
function getShareLink() {
let fileName = docTitle
let fContent = tinymce.activeEditor.getContent({ format: 'html' });
fContent = btoa(fContent)
let lHostPathName = `${location.host}/${location.pathname}`.replace('//', '/')
let fLink = `${location.protocol}//${lHostPathName}?action=filelink&file=${fileName}&content=${fContent}`
prompt('This is the link to share!', fLink)
}
function upload() {
readfile.click()
}
var isUpload = (new URLSearchParams(location.search)).get('action') === 'upload'
if (isUpload) {
window.addEventListener('DOMContentLoaded', upload)
}
function deleteFile() {
var fTitle = docTitle
console.log(`\`docTitle\` = "${docTitle}"`)
localStorage.removeItem(`FILEDATA://${fTitle}`)
let filesObj = localStorage.getItem('files')
if (!!filesObj === false) {
filesObj = []
}
else if (!filesObj.startsWith('[') || !filesObj.endsWith(']')) {
filesObj = [filesObj]
}
else filesObj = JSON.parse(filesObj)
let index = filesObj.indexOf(fTitle)
if (index >= 0) {
filesObj.splice(index, 1)
}
localStorage.setItem('files', JSON.stringify(filesObj))
if (index < 0) {
location.href = `${location.pathname}?action=new`
}
history.back();
location.href = '/'
}
function addMenuItems(editor) {
custMenuItems.forEach(function(m, i) {
var mId = m.name.toLowerCase()
if (mId.includes(' ')) {
mId = mId.split(' ')
mId.forEach(function(p, i) {
if (i !== 0) {
mId[i] = `${p[0].toUpperCase()}${p.slice(1)}`
}
})
mId = mId.join('')
}
editor.ui.registry.addMenuItem(mId, {
text: m.name,
onAction: () => m.func()
});
})
}