Skip to content

Commit 3cb1b0c

Browse files
authored
update editors
1 parent 0739f33 commit 3cb1b0c

File tree

6 files changed

+157
-146
lines changed

6 files changed

+157
-146
lines changed

actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ else if (action === 'openfile') {
2727

2828

2929
setTimeout(function() {
30-
rename(fName)
30+
rename ? rename(fName) : ''
3131
}, 1000)
3232

3333
var type = 'text'

codeEditor.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
javascript:(function () { var script = document.createElement('script'); script.src="//cdn.jsdelivr.net/npm/eruda"; document.body.appendChild(script); script.onload = function () { eruda.init() } })();
5959
</script>
6060
<script src="https://unpkg.com/monaco-editor@latest/min/vs/loader.js"></script>
61+
<script src="editorFunctions.js"></script>
6162
<script src="actions.js"></script>
6263
<script src="codeEditor.js"></script>
6364
<script src="sectionWidth.js"></script>

codeEditor.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,35 @@ require(["vs/editor/editor.main"], function () {
7474
});
7575

7676
editor.onKeyDown(keyDown);
77-
});
77+
});
78+
79+
var previewWindow = null
80+
function previewHTML() {
81+
let title = docTitle
82+
let ext = ''
83+
if (title.includes('.')) {
84+
ext = title.split('.')[1]
85+
}
86+
87+
let value = tinymce.activeEditor.getContent({ format: 'raw' });
88+
textarea = document.querySelector('textarea')
89+
90+
let eleVal = 'documentElement'
91+
if (value.includes('<html')) {
92+
value = value.split(`<html${value.split('<html')[1].split('>')[0]}>\n`)[1]
93+
if (value.includes('</html>')) {
94+
value = value.split('</html>')[0]
95+
}
96+
}
97+
else if (value.includes('<xml')) {
98+
value = value.split(`<xml${value.split('<xml')[1].split('>')[0]}>\n`)[1]
99+
}
100+
else if (value.includes('<?xml')) {
101+
value = value.split(`<?xml${value.split('<?xml')[1].split('>')[0]}>\n`)[1]
102+
}
103+
if (value.includes('<svg')) {
104+
eleVal = 'body'
105+
}
106+
previewWindow = open(`/preview.html?eleval=${eleVal}&content=${btoa(value)}`)
107+
}
108+

editor.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<!-- Place the following <script> and <textarea> tags your HTML's <body> -->
2222
<a id="downloader"></a>
2323
<input id="eleVal" hidden>
24+
<script src="editorFunctions.js"></script>
2425
<script src="editor.js"></script>
2526
<script src="actions.js"></script>
2627
<script src="sectionWidth.js"></script>

editor.js

Lines changed: 6 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var previewWindow = null
21
var currentState = null
32
var docTitle = ''
43

@@ -21,7 +20,7 @@ var custMenuItems = [
2120
},
2221
{
2322
name: 'Download',
24-
func: download,
23+
func: downloadFile,
2524
},
2625
{
2726
name: 'Share',
@@ -48,7 +47,7 @@ tinymce.init({
4847
],
4948

5049
menu: {
51-
newFile: { title: 'File', items: 'rename save delete | upload download | share print' }
50+
newFile: { title: 'File', items: 'rename save delete | upload download | share print "open in code editor"' }
5251
},
5352
menubar: 'newFile edit view insert format tools table help',
5453

@@ -97,54 +96,10 @@ var theAlert = false
9796
var container = document.querySelector('.container')
9897
var textarea = container.querySelector('textarea')
9998

100-
function rename(newName) {
101-
if (newName) {
102-
docTitle = newName
103-
}
104-
else {
105-
docTitle = prompt("Rename Document", docTitle)
106-
}
107-
}
108-
109-
function download() {
99+
function downloadFile() {
110100
let input = tinymce.activeEditor.getContent({ format: 'raw' });
111101

112-
// create a new Blob object with the content you want to assign
113-
let blob = new Blob([input], {type: "text/plain"});
114-
115-
// create a FileReader object
116-
let reader = new FileReader();
117-
118-
// when the read operation is finished, this will be called
119-
reader.onloadend = function() {
120-
// the result attribute contains the contents of the file
121-
}
122-
123-
// read the file as text
124-
reader.readAsText(blob);
125-
126-
let link = document.querySelector('a#downloader');
127-
link.href = window.URL.createObjectURL(blob);
128-
let title = docTitle
129-
if (!!title === false) title = 'New Text File'
130-
link.download = `${title}.tnynpd`;
131-
if (!!title) {
132-
link.click();
133-
}
134-
}
135-
136-
function setTitle() {
137-
let title = docTitle
138-
if (title.split('').length <= 0) {
139-
document.title = docTitle
140-
}
141-
else {
142-
if (docTitle.includes('.')) {
143-
ext = ''
144-
}
145-
document.title = `${title} | ${docTitle}`
146-
}
147-
ext = title.split('.').slice(-1)
102+
download(input)
148103
}
149104

150105
var readfile = document.querySelector("input[type='file']#readfile");
@@ -227,35 +182,6 @@ function checkImage(element, file, name, state) {
227182
return state
228183
}
229184

230-
function previewHTML() {
231-
let title = docTitle
232-
let ext = ''
233-
if (title.includes('.')) {
234-
ext = title.split('.')[1]
235-
}
236-
237-
let value = tinymce.activeEditor.getContent({ format: 'raw' });
238-
textarea = document.querySelector('textarea')
239-
240-
let eleVal = 'documentElement'
241-
if (value.includes('<html')) {
242-
value = value.split(`<html${value.split('<html')[1].split('>')[0]}>\n`)[1]
243-
if (value.includes('</html>')) {
244-
value = value.split('</html>')[0]
245-
}
246-
}
247-
else if (value.includes('<xml')) {
248-
value = value.split(`<xml${value.split('<xml')[1].split('>')[0]}>\n`)[1]
249-
}
250-
else if (value.includes('<?xml')) {
251-
value = value.split(`<?xml${value.split('<?xml')[1].split('>')[0]}>\n`)[1]
252-
}
253-
if (value.includes('<svg')) {
254-
eleVal = 'body'
255-
}
256-
previewWindow = open(`/preview.html?eleval=${eleVal}&content=${btoa(value)}`)
257-
}
258-
259185
function outputsize() {
260186
if (currentState !== 'image' && document.querySelector('textarea').clientWidth < 150) {
261187
textarea.style.width = `${150}px`
@@ -265,45 +191,9 @@ outputsize()
265191

266192
new ResizeObserver(outputsize).observe(textarea)
267193

268-
function saveFile(value) {
269-
let title = docTitle
270-
if (title === '' || !!title === false || title === null) return
271-
272-
let author = localStorage.getItem('username')
273-
if (!!author === false) author = ''
274-
let d = new Date()
275-
let json = {
276-
title: title,
277-
content: value,
278-
author: author,
279-
dateModofied: `${d.getMonth()+1}/${d.getDate()}/${d.getFullYear()}`
280-
}
281-
localStorage.setItem(`FILEDATA://${title}`, JSON.stringify(json))
282-
let filesObj = localStorage.getItem('files')
283-
filesObj = JSON.parse(filesObj)
284-
if (!!filesObj === false) {
285-
filesObj = []
286-
}
287-
let hasFile = false
288-
filesObj.forEach(function(f, i) {
289-
if (f === title) {
290-
hasFile = true
291-
}
292-
})
293-
if (hasFile === false) {
294-
filesObj.push(title)
295-
}
296-
localStorage.setItem('files', JSON.stringify(filesObj))
297-
return title
298-
}
299-
300-
function getShareLink() {
301-
let fileName = docTitle
194+
function getShareLinkTinyMCE() {
302195
let fContent = tinymce.activeEditor.getContent({ format: 'raw' });
303-
fContent = btoa(fContent)
304-
let lHostPathName = `${location.host}/${location.pathname}`.replace('//', '/')
305-
let fLink = `${location.protocol}//${lHostPathName}?action=filelink&file=${fileName}&content=${fContent}`
306-
prompt('This is the link to share!', fLink)
196+
getShareLink(fContent)
307197
}
308198

309199
function openInCodeEditor() {
@@ -315,39 +205,11 @@ function upload() {
315205
readfile.click()
316206
}
317207

318-
319208
var isUpload = (new URLSearchParams(location.search)).get('action') === 'upload'
320209
if (isUpload) {
321210
window.addEventListener('DOMContentLoaded', upload)
322211
}
323212

324-
function deleteFile() {
325-
var fTitle = docTitle
326-
console.log(`\`docTitle\` = "${docTitle}"`)
327-
localStorage.removeItem(`FILEDATA://${fTitle}`)
328-
329-
let filesObj = localStorage.getItem('files')
330-
if (!!filesObj === false) {
331-
filesObj = []
332-
}
333-
else if (!filesObj.startsWith('[') || !filesObj.endsWith(']')) {
334-
filesObj = [filesObj]
335-
}
336-
else filesObj = JSON.parse(filesObj)
337-
let index = filesObj.indexOf(fTitle)
338-
if (index >= 0) {
339-
filesObj.splice(index, 1)
340-
}
341-
localStorage.setItem('files', JSON.stringify(filesObj))
342-
343-
if (index < 0) {
344-
location.href = `${location.pathname}?action=new`
345-
}
346-
347-
history.back();
348-
location.href = '/'
349-
}
350-
351213
function addMenuItems(editor) {
352214
custMenuItems.forEach(function(m, i) {
353215
var mId = m.name.toLowerCase()

editorFunctions.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
function rename(newName) {
2+
if (newName) {
3+
docTitle = newName
4+
}
5+
else {
6+
docTitle = prompt('Rename Document', docTitle)
7+
}
8+
setTitle()
9+
}
10+
11+
function download() {
12+
let input = tinymce.activeEditor.getContent({ format: 'raw' });
13+
14+
// create a new Blob object with the content you want to assign
15+
let blob = new Blob([input], {type: 'text/plain'});
16+
17+
// create a FileReader object
18+
let reader = new FileReader();
19+
20+
// when the read operation is finished, this will be called
21+
reader.onloadend = function() {
22+
// the result attribute contains the contents of the file
23+
}
24+
25+
// read the file as text
26+
reader.readAsText(blob);
27+
28+
let link = document.querySelector('a#downloader');
29+
link.href = window.URL.createObjectURL(blob);
30+
let title = docTitle
31+
if (!!title === false) title = 'New Text File'
32+
link.download = `${title}.tnynpd`;
33+
if (!!title) {
34+
link.click();
35+
}
36+
}
37+
38+
function setTitle() {
39+
let title = docTitle
40+
if (title.split('').length <= 0) {
41+
document.title = docTitle
42+
}
43+
else {
44+
if (docTitle.includes('.')) {
45+
ext = ''
46+
}
47+
document.title = `${title} | ${docTitle}`
48+
}
49+
ext = title.split('.').slice(-1)
50+
}
51+
52+
function saveFile(value) {
53+
let title = docTitle
54+
if (title === '' || !!title === false || title === null) return
55+
56+
let author = localStorage.getItem('username')
57+
if (!!author === false) author = ''
58+
let d = new Date()
59+
let json = {
60+
title: title,
61+
content: value,
62+
author: author,
63+
dateModofied: `${d.getMonth()+1}/${d.getDate()}/${d.getFullYear()}`
64+
}
65+
localStorage.setItem(`FILEDATA://${title}`, JSON.stringify(json))
66+
let filesObj = localStorage.getItem('files')
67+
filesObj = JSON.parse(filesObj)
68+
if (!!filesObj === false) {
69+
filesObj = []
70+
}
71+
let hasFile = false
72+
filesObj.forEach(function(f, i) {
73+
if (f === title) {
74+
hasFile = true
75+
}
76+
})
77+
if (hasFile === false) {
78+
filesObj.push(title)
79+
}
80+
localStorage.setItem('files', JSON.stringify(filesObj))
81+
return title
82+
}
83+
84+
function getShareLink(fContent) {
85+
let fileName = docTitle
86+
fContent = btoa(fContent)
87+
let lHostPathName = `${location.host}/${location.pathname}`.replace('//', '/')
88+
let fLink = `${location.protocol}//${lHostPathName}?action=filelink&file=${fileName}&content=${fContent}`
89+
prompt('This is the link to share!', fLink)
90+
}
91+
92+
function deleteFile() {
93+
var fTitle = docTitle
94+
console.log(`\`docTitle\` = "${docTitle}"`)
95+
localStorage.removeItem(`FILEDATA://${fTitle}`)
96+
97+
let filesObj = localStorage.getItem('files')
98+
if (!!filesObj === false) {
99+
filesObj = []
100+
}
101+
else if (!filesObj.startsWith('[') || !filesObj.endsWith(']')) {
102+
filesObj = [filesObj]
103+
}
104+
else filesObj = JSON.parse(filesObj)
105+
let index = filesObj.indexOf(fTitle)
106+
if (index >= 0) {
107+
filesObj.splice(index, 1)
108+
}
109+
localStorage.setItem('files', JSON.stringify(filesObj))
110+
111+
if (index < 0) {
112+
location.href = `${location.pathname}?action=new`
113+
}
114+
115+
location.href = '/'
116+
}

0 commit comments

Comments
 (0)