11var myName ;
22var otI ;
3+ var state ;
34 function createFileUploader ( element , tree , editor ) {
45 function addButton ( name , fn ) {
5- $ ( element ) . append ( $ ( '<button>' ) . text ( name ) . on ( 'click' , fn ) ) ;
6+ $ ( element ) . append ( $ ( '<button>' ) . addClass ( 'btn btn-sm m-1' ) . text ( name ) . on ( 'click' , fn ) ) ;
67 }
7- addButton ( '<<' , function ( e ) { $ ( '.jstree' ) . toggle ( ) ; } ) ;
8- addButton ( 'A' , function ( e ) { toggleFullScreen ( ) ; } ) ;
9- addButton ( 'Save' , function ( e ) { editor . saveUrl ( ) ; } ) ;
10- addButton ( 'diff Next ' , function ( e ) { editor . execCommand ( "nextDiff" ) } ) ;
11- addButton ( 'diff Prev ' , function ( e ) { editor . execCommand ( "prevDiff" ) } ) ;
12- addButton ( 'Beautify selected ' , function ( e ) {
8+ // addButton('<<',function(e){ $('.jstree').toggle(); });
9+ // addButton('A',function(e){ toggleFullScreen(); });
10+ // addButton('Save',function(e){ editor.execCommand("saveCommand") });
11+ addButton ( 'next ' , function ( e ) { editor . execCommand ( "nextDiff" ) } ) ;
12+ addButton ( 'prev ' , function ( e ) { editor . execCommand ( "prevDiff" ) } ) ;
13+ addButton ( '{} ' , function ( e ) {
1314 if ( ! editor . getSelectedText ( ) ) return ;
1415 var beautify = ace . require ( "ace/ext/beautify" ) ; // get reference to extension
1516 var session = ace . createEditSession ( '' , editor . session . getOption ( 'mode' ) ) ;
@@ -25,9 +26,11 @@ var otI;
2526 editor . _signal ( "change" , { } ) ;
2627 } ) ;
2728 addButton ( 'NAME' , function ( e ) { myName = prompt ( 'Tell Ur Name!!!' ) ; otI . setName ( myName ) ; } ) ;
29+ addButton ( 'undo' , function ( e ) { editor . getSession ( ) . getUndoManager ( ) . undo ( false ) ; } ) ;
30+ $ ( element ) . append ( state = $ ( '<span class="m-1">Loading...</span>' ) ) ;
2831 }
2932
30- var ses = [ ] ; // {path, tab, name, session}
33+ var manager = { } ; // {path, tab, name, session}
3134 function createTree ( element , editor ) {
3235 fsbrowser ( $ ( '.tree' ) , loadFile )
3336 function loadFile ( path , type ) {
@@ -41,39 +44,37 @@ var otI;
4144 loadEditor ( path ) ;
4245 } ;
4346 function loadEditor ( path ) {
44- var s = ses . filter ( function ( i ) { return i . path == path ; } ) ;
45- if ( ! s . length ) {
47+ var s = manager [ path ] ;
48+ if ( ! s ) {
4649 var name = path . slice ( path . lastIndexOf ( "/" ) + 1 ) ;
4750 var tab = $ ( '<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#editorTab" role="tab" aria-controls="profile" aria-selected="false">'
48- + '<button class="close closeTab" type="button" >×</button>' + name + '</a></li>' ) ;
51+ + '<button class="close closeTab pl-2 " type="button" >×</button>' + name + '</a></li>' ) ;
4952 $ ( 'ul.nav' ) . append ( tab ) ;
50- var o = {
53+ s = manager [ path ] = {
5154 tab : tab ,
5255 path : path ,
5356 name : name ,
5457 session : ace . createEditSession ( '' , 'ace/mode/' + getLangFromFilename ( name ) ) ,
5558 }
59+ s . session . path = path ;
5660 tab . find ( 'a' ) . on ( 'shown.bs.tab' , function ( ev ) {
57- editor . setSession ( o . session ) ;
58- } )
61+ state . text ( '' ) ;
62+ editor . setSession ( s . session ) ;
63+ editor . dmp && editor . dmp . scan ( ) ;
64+ } ) . tab ( 'show' ) ;
5965 tab . find ( 'button' ) . on ( 'click' , function ( ev ) {
6066 ev . preventDefault ( ) ;
6167 if ( $ ( this ) . parent ( ) . is ( '[aria-expanded=true]' ) )
6268 $ ( '#preview-tab' ) . tab ( 'show' )
6369 $ ( this ) . parents ( 'li' ) . remove ( ) ;
6470 otI . shut ( path ) ;
65- ses . map ( function ( item , index ) {
66- if ( item . path == path )
67- return index ;
68- } ) . map ( function ( index ) { ses . splice ( index , 1 ) ; } ) ;
71+ delete manager [ path ] ;
6972 } )
70- s . push ( o ) ;
71- ses . push ( o ) ;
72- editor . setSession ( o . session ) ;
73+ editor . setSession ( s . session ) ;
7374 editor . loadUrl ( path ) ;
75+ } else {
76+ s . tab . find ( 'a' ) . tab ( 'show' )
7477 }
75- s [ 0 ] . tab . find ( 'a' ) . tab ( 'show' )
76- //$('#preview').hide();
7778 }
7879 function loadPreview ( path ) {
7980 $ ( '#preview-tab' ) . tab ( 'show' )
@@ -87,12 +88,6 @@ var otI;
8788 var ext = ( / (?: \. ( [ ^ . ] + ) ) ? $ / . exec ( path ) [ 1 ] || '' ) . toLowerCase ( ) ;
8889 return 'png,jpg,jpeg,webp,apng,pdf,gif,xbm,bmp,ico' . split ( ',' ) . indexOf ( ext ) >= 0 ;
8990 }
90- this . refreshPath = function ( path ) {
91- if ( path . lastIndexOf ( '/' ) < 1 )
92- path = '/' ;
93- else
94- path = path . substring ( 0 , path . lastIndexOf ( '/' ) ) ;
95- } ;
9691 return this ;
9792 }
9893 function getLangFromFilename ( filename ) {
@@ -120,82 +115,56 @@ var otI;
120115
121116 function createEditor ( element , file , lang , theme , type ) {
122117 var editor = ace . edit ( element ) ;
123- new ( require ( "marker_tooltip" ) . MarkerTooltip ) ( editor ) ; // show previous text over highlighted
118+ require ( 'ace/ext/beautify' ) ;
119+ var MT = require ( "marker_tooltip" ) ;
120+ new MT ( editor )
121+ require ( 'diff' ) ;
122+ var OT = require ( "ot" ) ;
123+ otI = new OT ( manager , function ( text ) { state . text ( text ) } ) ;
124+
124125 function httpPost ( filename , data , type ) {
125126 var formData = new FormData ( ) ;
126127 formData . append ( "data" , new Blob ( [ data ] , { type : type } ) , filename ) ;
127- $ . post ( { url :'/s/editor/upload' + file , data : formData , contentType : false , processData : false } )
128- // $.post({url: '/s/editor/upload' + file, data: data, contentType: false, processData: false})
129- . then ( function ( data ) {
130- alert ( 'saved!' ) ;
128+ $ . post ( { url :'/s/editor/upload' + filename , data : formData , contentType : false , processData : false } )
129+ . then ( function ( data ) {
130+ state . text ( 'saved!' ) ;
131131 } , function ( data ) {
132132 alert ( "ERROR[" + data . status + "]: " + data . responseText ) ;
133133 } ) ;
134134 }
135135 function httpGet ( theUrl ) {
136- $ . when ( $ . get ( '/s/editor/file' + theUrl ) , $ . get ( '/s/editor/git' + theUrl , otI . init ( theUrl ) ) . catch ( function ( ) { return [ ' ' ] } ) )
136+ $ . when ( 0 && $ . get ( '/s/editor/file' + theUrl ) , $ . get ( '/s/editor/git' + theUrl , otI . init ( theUrl ) ) . catch ( function ( ) { return [ ' ' ] } ) )
137137 . then ( function ( data , dataGit ) {
138138 delete editor . $setBaseText ; //TODO: set option to session
139139 editor . setOptions ( { setBaseText : dataGit [ 0 ] } )
140140if ( 0 )
141141 editor . setValue ( data [ 0 ] ) ;
142- // editor.clearSelection();
143142 editor . gotoLine ( 0 ) ;
144143 editor . getSession ( ) . _signal ( "changeAnnotation" , { } ) ; //TODO: bug update
144+ state . text ( 'opened!' ) ;
145145 } , function ( data ) {
146146 editor . setValue ( "" ) ;
147147 alert ( "ERROR[" + data . status + "]: " + data . responseText ) ;
148148 } ) ;
149149 }
150- if ( typeof theme === "undefined" ) theme = "textmate" ;
151150
152- // ace.config.loadModule('/libs/diff-match-patch/index', function() {
153- ace . config . loadModule ( 'diff' , function ( diff ) {
154- console . log ( 'diff loaded' )
155- } ) ;
156- ace . config . loadModule ( 'ot' , function ( OT ) {
157- otI = new OT ( editor , ses ) ;
158- console . log ( 'ot loaded' )
159- } ) ;
160- // });
161- ace . config . loadModule ( 'ace/ext/beautify' , function ( diff ) {
162- console . log ( 'ace/ext/beautify loaded' )
163- } ) ;
151+ theme = theme || "textmate" ;
164152 editor . setTheme ( "ace/theme/" + theme ) ;
165153 editor . $blockScrolling = Infinity ;
166154 editor . getSession ( ) . setUseSoftTabs ( true ) ;
167155 editor . getSession ( ) . setTabSize ( 2 ) ;
168156 editor . setHighlightActiveLine ( true ) ;
169157 editor . setShowPrintMargin ( false ) ;
158+
170159 editor . commands . addCommand ( {
171160 name : 'saveCommand' ,
172161 bindKey : { win : 'Ctrl-S' , mac : 'Command-S' } ,
173162 exec : function ( editor ) {
174- httpPost ( file , editor . getValue ( ) + "" , type ) ;
175- } ,
176- readOnly : false
177- } ) ;
178- editor . commands . addCommand ( {
179- name : 'undoCommand' ,
180- bindKey : { win : 'Ctrl-Z' , mac : 'Command-Z' } ,
181- exec : function ( editor ) {
182- editor . getSession ( ) . getUndoManager ( ) . undo ( false ) ;
183- } ,
184- readOnly : false
185- } ) ;
186- editor . commands . addCommand ( {
187- name : 'redoCommand' ,
188- bindKey : { win : 'Ctrl-Shift-Z' , mac : 'Command-Shift-Z' } ,
189- exec : function ( editor ) {
190- editor . getSession ( ) . getUndoManager ( ) . redo ( false ) ;
163+ httpPost ( editor . session . path , editor . getValue ( ) , type ) ;
191164 } ,
192165 readOnly : false
193166 } ) ;
194- editor . saveUrl = function ( ) {
195- httpPost ( file , editor . getValue ( ) + "" , type ) ;
196- }
197- editor . loadUrl = function ( filename , lang , type ) {
198- file = filename ;
167+ editor . loadUrl = function ( file , lang , type ) {
199168 if ( typeof file === "undefined" ) return file = "/index.htm" ;
200169 if ( typeof lang === "undefined" )
201170 lang = getLangFromFilename ( file ) ;
@@ -206,14 +175,26 @@ if(0)
206175 if ( lang !== "plain" ) editor . getSession ( ) . setMode ( "ace/mode/" + lang ) ;
207176 httpGet ( file ) ;
208177 }
209- editor . loadUrl ( file , lang , type ) ;
178+ editor . loadUrl ( file , lang , type ) ;
210179 return editor ;
211180 }
212- $ ( function ( ) {
181+
182+ $ ( function ( ) {
213183 $ ( window ) . on ( 'beforeunload' , function ( ) { return "Realy?" ; } ) ;
214184 var vars = { } ;
215185 var parts = window . location . href . replace ( / [ ? & ] + ( [ ^ = & ] + ) = ( [ ^ & ] * ) / gi, function ( m , key , value ) { vars [ key ] = value ; } ) ;
216186 var editor = createEditor ( "editor" , vars . file , vars . lang , vars . theme ) ;
217187 var tree = createTree ( "tree" , editor ) ;
218188 createFileUploader ( ".uploader" , tree , editor ) ;
219- } ) ;
189+ $ . ajax ( '/upnp/check' )
190+ . then ( function ( data ) {
191+ if ( data && data [ 0 ] ) {
192+ $ ( '.btn-warning' ) . show ( ) . on ( 'click' , function ( ) {
193+ var url = 'http://' + data [ 0 ] . ip + ':' + data [ 0 ] . port + '/editor' ;
194+ var m = $ ( '#mct-qr-modal' ) ;
195+ m . find ( '.modal-body img' ) . attr ( 'src' , '/qr/' + encodeURI ( btoa ( url ) ) )
196+ m . modal ( ) ;
197+ } )
198+ }
199+ } )
200+ } ) ;
0 commit comments