1- var previewWindow = null
21var currentState = null
32var 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
9796var container = document . querySelector ( '.container' )
9897var 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
150105var 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-
259185function outputsize ( ) {
260186 if ( currentState !== 'image' && document . querySelector ( 'textarea' ) . clientWidth < 150 ) {
261187 textarea . style . width = `${ 150 } px`
@@ -265,45 +191,9 @@ outputsize()
265191
266192new 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
309199function openInCodeEditor ( ) {
@@ -315,39 +205,11 @@ function upload() {
315205 readfile . click ( )
316206}
317207
318-
319208var isUpload = ( new URLSearchParams ( location . search ) ) . get ( 'action' ) === 'upload'
320209if ( 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-
351213function addMenuItems ( editor ) {
352214 custMenuItems . forEach ( function ( m , i ) {
353215 var mId = m . name . toLowerCase ( )
0 commit comments