@ -42,6 +42,7 @@ define([
'/common/common-hash.js' ,
'/common/common-hash.js' ,
'/common/common-util.js' ,
'/common/common-util.js' ,
'/common/common-interface.js' ,
'/common/common-interface.js' ,
'/common/common-ui-elements.js' ,
'/common/hyperscript.js' ,
'/common/hyperscript.js' ,
'/bower_components/chainpad/chainpad.dist.js' ,
'/bower_components/chainpad/chainpad.dist.js' ,
'/customize/application_config.js' ,
'/customize/application_config.js' ,
@ -69,6 +70,7 @@ define([
Hash ,
Hash ,
Util ,
Util ,
UI ,
UI ,
UIElements ,
h ,
h ,
ChainPad ,
ChainPad ,
AppConfig ,
AppConfig ,
@ -424,6 +426,41 @@ define([
} ) ;
} ) ;
} ;
} ;
var addTOCHideBtn = function ( framework , $toc ) {
// Expand / collapse the toolbar
var onClick = function ( visible ) {
framework . _ . sfCommon . setAttribute ( [ 'pad' , 'showTOC' ] , visible ) ;
} ;
framework . _ . sfCommon . getAttribute ( [ 'pad' , 'showTOC' ] , function ( err , data ) {
var state = false ;
if ( ( $ ( window ) . height ( ) >= 800 || $ ( window ) . width ( ) >= 800 ) &&
( typeof ( data ) === "undefined" || data ) ) {
state = true ;
$toc . show ( ) ;
} else {
$toc . hide ( ) ;
}
var $tocButton = framework . _ . sfCommon . createButton ( '' , true , {
drawer : false ,
text : Messages . pad _tocHide ,
name : 'pad_toc' ,
icon : 'fa-list-ul' ,
} , function ( ) {
$tocButton . removeClass ( 'cp-toolbar-button-active' ) ;
$toc . toggle ( ) ;
state = $toc . is ( ':visible' ) ;
if ( state ) {
$tocButton . addClass ( 'cp-toolbar-button-active' ) ;
}
onClick ( state ) ;
} ) ;
framework . _ . toolbar . $bottomL . append ( $tocButton ) ;
if ( state ) {
$tocButton . addClass ( 'cp-toolbar-button-active' ) ;
}
} ) ;
} ;
var displayMediaTags = function ( framework , dom , mediaTagMap ) {
var displayMediaTags = function ( framework , dom , mediaTagMap ) {
setTimeout ( function ( ) { // Just in case
setTimeout ( function ( ) { // Just in case
var tags = dom . querySelectorAll ( 'media-tag:empty' ) ;
var tags = dom . querySelectorAll ( 'media-tag:empty' ) ;
@ -537,6 +574,9 @@ define([
$container : $ ( '#cp-app-pad-comments' )
$container : $ ( '#cp-app-pad-comments' )
} ) ;
} ) ;
var $toc = $ ( '#cp-app-pad-toc' ) ;
addTOCHideBtn ( framework , $toc ) ;
// My cursor
// My cursor
var cursor = module . cursor = Cursor ( inner ) ;
var cursor = module . cursor = Cursor ( inner ) ;
@ -607,6 +647,34 @@ define([
} , 500 ) ; // 500ms to make sure it is sent after chainpad sync
} , 500 ) ; // 500ms to make sure it is sent after chainpad sync
} ;
} ;
var updateTOC = Util . throttle ( function ( ) {
var toc = [ ] ;
$inner . find ( 'h1, h2, h3' ) . each ( function ( i , el ) {
toc . push ( {
level : Number ( el . tagName . slice ( 1 ) ) ,
el : el ,
title : Util . stripTags ( $ ( el ) . text ( ) )
} ) ;
} ) ;
var content = [ h ( 'h2' , Messages . markdown _toc ) ] ;
toc . forEach ( function ( obj ) {
// Only include level 2 headings
var level = obj . level ;
var a = h ( 'a.cp-pad-toc-link' , {
href : '#' ,
} ) ;
$ ( a ) . click ( function ( e ) {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
if ( ! obj . el || UIElements . isVisible ( obj . el , $inner ) ) { return ; }
obj . el . scrollIntoView ( ) ;
} ) ;
a . innerHTML = obj . title ;
content . push ( h ( 'p.cp-pad-toc-' + level , a ) ) ;
} ) ;
$toc . html ( '' ) . append ( content ) ;
} ) ;
// apply patches, and try not to lose the cursor in the process!
// apply patches, and try not to lose the cursor in the process!
framework . onContentUpdate ( function ( hjson ) {
framework . onContentUpdate ( function ( hjson ) {
if ( ! Array . isArray ( hjson ) ) { throw new Error ( Messages . typeError ) ; }
if ( ! Array . isArray ( hjson ) ) { throw new Error ( Messages . typeError ) ; }
@ -666,6 +734,8 @@ define([
}
}
comments . onContentUpdate ( ) ;
comments . onContentUpdate ( ) ;
updateTOC ( ) ;
} ) ;
} ) ;
framework . setTextContentGetter ( function ( ) {
framework . setTextContentGetter ( function ( ) {
@ -877,8 +947,12 @@ define([
framework . localChange ( ) ;
framework . localChange ( ) ;
updateCursor ( ) ;
updateCursor ( ) ;
editor . fire ( 'cp-wc' ) ; // Update word count
editor . fire ( 'cp-wc' ) ; // Update word count
updateTOC ( ) ;
} ) ;
editor . on ( 'change' , function ( ) {
framework . localChange ( ) ;
updateTOC ( ) ;
} ) ;
} ) ;
editor . on ( 'change' , framework . localChange ) ;
var wordCount = h ( 'span.cp-app-pad-wordCount' ) ;
var wordCount = h ( 'span.cp-app-pad-wordCount' ) ;
$ ( '.cke_toolbox_main' ) . append ( wordCount ) ;
$ ( '.cke_toolbox_main' ) . append ( wordCount ) ;
@ -1077,6 +1151,7 @@ define([
var $ckeToolbar = $ ( '#cke_1_top' ) . find ( '.cke_toolbox_main' ) ;
var $ckeToolbar = $ ( '#cke_1_top' ) . find ( '.cke_toolbox_main' ) ;
$mainContainer . prepend ( $ckeToolbar . addClass ( 'cke_reset_all' ) ) ;
$mainContainer . prepend ( $ckeToolbar . addClass ( 'cke_reset_all' ) ) ;
$contentContainer . append ( h ( 'div#cp-app-pad-comments' ) ) ;
$contentContainer . append ( h ( 'div#cp-app-pad-comments' ) ) ;
$contentContainer . prepend ( h ( 'div#cp-app-pad-toc' ) ) ;
$ckeToolbar . find ( '.cke_button__image_icon' ) . parent ( ) . hide ( ) ;
$ckeToolbar . find ( '.cke_button__image_icon' ) . parent ( ) . hide ( ) ;
} ) . nThen ( waitFor ( ) ) ;
} ) . nThen ( waitFor ( ) ) ;