@@ -50,7 +50,7 @@ function slugifyWithUTF8(text) {
5050
5151var viewAjaxCallback = null ;
5252
53- //regex for blockquote
53+ //regex for extra tags
5454var spaceregex = / \s * / ;
5555var notinhtmltagregex = / (? ! [ ^ < ] * > | [ ^ < > ] * < \/ ) / ;
5656var coloregex = / \[ c o l o r = ( [ # | \( | \) | \s | \, | \w ] * ?) \] / ;
@@ -61,8 +61,44 @@ var nameandtimeregex = new RegExp(nameregex.source + spaceregex.source + timereg
6161nameregex = new RegExp ( nameregex . source + notinhtmltagregex . source , "g" ) ;
6262timeregex = new RegExp ( timeregex . source + notinhtmltagregex . source , "g" ) ;
6363
64+ function replaceExtraTags ( html ) {
65+ html = html . replace ( coloregex , '<span class="color" data-color="$1"></span>' ) ;
66+ html = html . replace ( nameandtimeregex , '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>' ) ;
67+ html = html . replace ( nameregex , '<small><i class="fa fa-user"></i> $1</small>' ) ;
68+ html = html . replace ( timeregex , '<small><i class="fa fa-clock-o"></i> $1</small>' ) ;
69+ return html ;
70+ }
71+
6472//dynamic event or object binding here
6573function finishView ( view ) {
74+ //todo list
75+ var lis = view . find ( 'li.raw' ) . removeClass ( "raw" ) . sortByDepth ( ) . toArray ( ) ;
76+ for ( var i = 0 ; i < lis . length ; i ++ ) {
77+ var li = lis [ i ] ;
78+ var html = $ ( li ) . clone ( ) [ 0 ] . innerHTML ;
79+ var p = $ ( li ) . children ( 'p' ) ;
80+ if ( p . length == 1 ) {
81+ html = p . html ( ) ;
82+ li = p [ 0 ] ;
83+ }
84+ html = replaceExtraTags ( html ) ;
85+ li . innerHTML = html ;
86+ var disabled = 'disabled' ;
87+ if ( typeof editor !== 'undefined' && havePermission ( ) )
88+ disabled = '' ;
89+ if ( / ^ \s * \[ [ x ] \] \s * / . test ( html ) ) {
90+ li . innerHTML = html . replace ( / ^ \s * \[ \] \s * / , '<input type="checkbox" class="task-list-item-checkbox "' + disabled + '><label></label>' )
91+ . replace ( / ^ \s * \[ x \] \s * / , '<input type="checkbox" class="task-list-item-checkbox" checked ' + disabled + '><label></label>' ) ;
92+ lis [ i ] . setAttribute ( 'class' , 'task-list-item' ) ;
93+ }
94+ if ( typeof editor !== 'undefined' && havePermission ( ) )
95+ $ ( li ) . find ( 'input' ) . change ( toggleTodoEvent ) ;
96+ //color tag in list will convert it to tag icon with color
97+ var tag_color = $ ( li ) . closest ( 'ul' ) . find ( ".color" ) ;
98+ tag_color . each ( function ( key , value ) {
99+ $ ( value ) . addClass ( 'fa fa-tag' ) . css ( 'color' , $ ( value ) . attr ( 'data-color' ) ) ;
100+ } ) ;
101+ }
66102 //youtube
67103 view . find ( ".youtube.raw" ) . removeClass ( "raw" )
68104 . click ( function ( ) {
@@ -149,12 +185,10 @@ function finishView(view) {
149185 var blockquote_p = blockquote . find ( "p" ) ;
150186 blockquote_p . each ( function ( key , value ) {
151187 var html = $ ( value ) . html ( ) ;
152- html = html . replace ( coloregex , '<span class="color" data-color="$1"></span>' ) ;
153- html = html . replace ( nameandtimeregex , '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>' ) ;
154- html = html . replace ( nameregex , '<small><i class="fa fa-user"></i> $1</small>' ) ;
155- html = html . replace ( timeregex , '<small><i class="fa fa-clock-o"></i> $1</small>' ) ;
188+ html = replaceExtraTags ( html ) ;
156189 $ ( value ) . html ( html ) ;
157190 } ) ;
191+ //color tag in blockquote will change its left border color
158192 var blockquote_color = blockquote . find ( ".color" ) ;
159193 blockquote_color . each ( function ( key , value ) {
160194 $ ( value ) . closest ( "blockquote" ) . css ( 'border-left-color' , $ ( value ) . attr ( 'data-color' ) ) ;
@@ -173,23 +207,8 @@ function postProcess(code) {
173207 result . find ( "iframe" ) . replaceWith ( function ( ) {
174208 return "<noiframe>" + $ ( this ) . html ( ) + "</noiframe>"
175209 } ) ;
176- //todo list
177- var lis = result . find ( 'li.raw' ) . removeClass ( "raw" ) . sortByDepth ( ) . toArray ( ) ;
178- for ( var i = 0 ; i < lis . length ; i ++ ) {
179- var li = lis [ i ] ;
180- var html = $ ( li ) . clone ( ) [ 0 ] . innerHTML ;
181- var p = $ ( li ) . children ( 'p' ) ;
182- if ( p . length == 1 ) {
183- html = p . html ( ) ;
184- li = p [ 0 ] ;
185210 }
186- if ( / ^ \s * \[ [ x ] \] \s * / . test ( html ) ) {
187- li . innerHTML = html . replace ( / ^ \s * \[ \] \s * / , '<input type="checkbox" class="task-list-item-checkbox" disabled><label></label>' )
188- . replace ( / ^ \s * \[ x \] \s * / , '<input type="checkbox" class="task-list-item-checkbox" checked disabled><label></label>' ) ;
189- lis [ i ] . setAttribute ( 'class' , 'task-list-item' ) ;
190211 }
191- }
192- return result ;
193212}
194213
195214//jQuery sortByDepth
@@ -211,6 +230,27 @@ $.fn.sortByDepth = function () {
211230 return $ ( result ) ;
212231} ;
213232
233+ function toggleTodoEvent ( e ) {
234+ var startline = $ ( this ) . closest ( 'li' ) . attr ( 'data-startline' ) - 1 ;
235+ var line = editor . getLine ( startline ) ;
236+ var matches = line . match ( / ^ [ > \s ] * [ \- \+ \* ] \s \[ ( [ x ] ) \] / ) ;
237+ if ( matches && matches . length >= 2 ) {
238+ var checked = null ;
239+ if ( matches [ 1 ] == 'x' )
240+ checked = true ;
241+ else if ( matches [ 1 ] == ' ' )
242+ checked = false ;
243+ var replacements = matches [ 0 ] . match ( / ( ^ [ > \s ] * [ \- \+ \* ] \s \[ ) ( [ x ] ) ( \] ) / ) ;
244+ editor . replaceRange ( checked ? ' ' : 'x' , {
245+ line : startline ,
246+ ch : replacements [ 1 ] . length
247+ } , {
248+ line : startline ,
249+ ch : replacements [ 1 ] . length + 1
250+ } , '+input' ) ;
251+ }
252+ }
253+
214254//remove hash
215255function removeHash ( ) {
216256 history . pushState ( "" , document . title , window . location . pathname + window . location . search ) ;
0 commit comments