2011-05-17 18:50:41 +04:00
var example = [
2011-05-18 19:31:16 +04:00
"# hello, This is Markdown Live Preview" ,
2011-05-17 18:50:41 +04:00
"" ,
"----" ,
"## what is Markdown?" ,
"see [Wikipedia](http://en.wikipedia.org/wiki/Markdown)" ,
"" ,
"> Markdown is a lightweight markup language, originally created by John Gruber and Aaron Swartz allowing people \"to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML)\"." ,
"" ,
"----" ,
"## usage" ,
2013-02-17 04:27:39 +04:00
"1. Write markdown text in this textarea." ,
"2. Click 'HTML Preview' button." ,
"" ,
"----" ,
"## markdown quick reference" ,
"# headers" ,
"" ,
"*emphasis*" ,
"" ,
"**strong**" ,
"" ,
"* list" ,
"" ,
">block quote" ,
"" ,
" code (4 spaces indent)" ,
"[links](http://wikipedia.org)" ,
"" ,
"----" ,
"## changelog" ,
"* 17-Feb-2013 re-design" ,
2011-05-17 18:50:41 +04:00
"" ,
"----" ,
"## thanks" ,
2013-02-17 04:27:39 +04:00
"* [markdown-js](https://github.com/evilstreak/markdown-js)" ,
""
2011-05-17 18:50:41 +04:00
] . join ( "\n" ) ;
2011-05-18 19:10:58 +04:00
2011-05-16 20:26:21 +04:00
$ ( function ( ) {
2013-02-17 04:27:39 +04:00
var currentMode = 'edit' ;
var container = $ ( '#container' ) ;
var header = $ ( '#header' ) ;
var headerHeight = header . outerHeight ( ) ;
var titleHeight = $ ( '#header h1' ) . outerHeight ( ) ;
var fixedTop = - titleHeight ;
var scrollTops = {
'edit' : 0 ,
'preview' : 0
} ;
var isEdited = false ;
$ ( window ) . scroll ( function ( ) {
var scrollTop = $ ( window ) . scrollTop ( ) ;
scrollTops [ currentMode ] = scrollTop ;
if ( ( scrollTop > titleHeight ) ) {
header . css ( {
'position' : 'fixed' ,
'top' : fixedTop + 'px' ,
'z-index' : '100'
} ) ;
container . css ( {
'margin-top' : headerHeight + 'px'
} ) ;
} else {
header . css ( 'position' , 'static' ) ;
container . css ( {
'margin-top' : 0
} ) ;
}
} ) ;
$ ( '#markdown' ) . val ( example ) ;
$ ( '#output' ) . html ( markdown . toHTML ( example ) ) ;
$ ( '#markdown' ) . bind ( 'keyup' , function ( ) {
isEdited = true ;
$ ( '#output' ) . html ( markdown . toHTML ( $ ( '#markdown' ) . val ( ) ) ) ;
} ) ;
//menu
var menuItems = $ ( '#menu a' ) ;
menuItems . click ( function ( event ) {
event . preventDefault ( ) ;
menuItems . removeClass ( 'active' ) ;
var sender = $ ( event . currentTarget ) ;
sender . addClass ( 'active' ) ;
$ ( '#content .mode' ) . hide ( ) ;
var menuId = sender . data ( 'menuId' ) ;
currentMode = menuId ;
$ ( '#' + menuId ) . show ( ) ;
$ ( window ) . scrollTop ( scrollTops [ currentMode ] ) ;
2011-05-16 20:26:21 +04:00
} ) ;
2011-05-18 19:10:58 +04:00
//reference
$ ( "table#reference tr td:odd" ) . each ( function ( index , element ) {
var self = $ ( element ) ;
if ( self . html ( ) === "" ) {
self . html ( markdown . toHTML ( self . siblings ( ) . html ( ) ) ) ;
}
} ) ;
2013-02-17 04:27:39 +04:00
//clear
$ ( '#clearButton' ) . click ( function ( event ) {
event . preventDefault ( ) ;
if ( window . confirm ( 'Are you sure you want to delete?' ) ) {
$ ( '#markdown' ) . val ( '' ) ;
}
} ) ;
//autoresize
$ ( 'textarea' ) . autosize ( ) ;
2013-02-17 09:43:20 +04:00
$ ( '#markdown' ) . focus ( ) ;
2013-02-17 04:27:39 +04:00
//leave
$ ( window ) . bind ( 'beforeunload' , function ( ) {
if ( isEdited ) {
return 'Are you sure you want to leave? Your changes will be lost.' ;
}
} ) ;
2011-05-16 20:26:21 +04:00
} ) ;