2014-05-07 13:06:24 +08:00
// 主页渲染
2014-05-13 14:34:30 +08:00
//-------------
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
//----------------------
// 编辑器模式
function editorMode ( ) {
2014-12-09 23:17:36 +08:00
this . writingHash = "writing" ;
this . normalHash = "normal" ;
this . isWritingMode = location . hash . indexOf ( this . writingHash ) >= 0 ;
2014-10-22 16:20:45 +08:00
this . toggleA = null ;
}
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
editorMode . prototype . toggleAText = function ( isWriting ) {
var self = this ;
setTimeout ( function ( ) {
2014-12-02 22:42:46 +08:00
var toggleA = $ ( ".toggle-editor-mode a" ) ;
var toggleSpan = $ ( ".toggle-editor-mode span" ) ;
2014-10-22 16:20:45 +08:00
if ( isWriting ) {
2014-12-09 23:17:36 +08:00
toggleA . attr ( "href" , "#" + self . normalHash ) ;
2014-12-02 22:42:46 +08:00
toggleSpan . text ( getMsg ( "normalMode" ) ) ;
2014-10-22 16:20:45 +08:00
} else {
2014-12-09 23:17:36 +08:00
toggleA . attr ( "href" , "#" + self . writingHash ) ;
2014-12-02 22:42:46 +08:00
toggleSpan . text ( getMsg ( "writingMode" ) ) ;
2014-10-22 16:20:45 +08:00
}
} , 0 ) ;
}
editorMode . prototype . isWriting = function ( hash ) {
2015-06-15 18:01:48 +08:00
if ( ! hash ) {
hash = location . hash ;
}
2014-12-09 23:17:36 +08:00
return hash . indexOf ( this . writingHash ) >= 0
2014-10-22 16:20:45 +08:00
}
editorMode . prototype . init = function ( ) {
2015-06-15 18:01:48 +08:00
this . $themeLink = $ ( "#themeLink" ) ;
2014-10-22 16:20:45 +08:00
this . changeMode ( this . isWritingMode ) ;
var self = this ;
2014-12-09 23:17:36 +08:00
$ ( ".toggle-editor-mode" ) . click ( function ( e ) {
e . preventDefault ( ) ;
2014-10-22 16:20:45 +08:00
saveBookmark ( ) ;
var $a = $ ( this ) . find ( "a" ) ;
var isWriting = self . isWriting ( $a . attr ( "href" ) ) ;
self . changeMode ( isWriting ) ;
//
2014-12-09 23:17:36 +08:00
if ( isWriting ) {
setHash ( "m" , self . writingHash ) ;
} else {
setHash ( "m" , self . normalHash ) ;
}
2014-10-22 16:20:45 +08:00
restoreBookmark ( ) ;
} ) ;
}
// 改变模式
editorMode . prototype . changeMode = function ( isWritingMode ) {
this . toggleAText ( isWritingMode ) ;
if ( isWritingMode ) {
this . writtingMode ( ) ;
} else {
this . normalMode ( ) ;
2014-05-07 13:06:24 +08:00
}
2015-10-27 23:04:39 +08:00
} ;
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
editorMode . prototype . resizeEditor = function ( ) {
// css还没渲染完
setTimeout ( function ( ) {
resizeEditor ( ) ;
} , 10 ) ;
setTimeout ( function ( ) {
resizeEditor ( ) ;
} , 20 ) ;
setTimeout ( function ( ) {
resizeEditor ( ) ;
} , 500 ) ;
}
editorMode . prototype . normalMode = function ( ) {
2015-06-15 18:01:48 +08:00
// 最开始的时候就调用?
2014-10-22 16:20:45 +08:00
/ *
var $c = $ ( "#editorContent_ifr" ) . contents ( ) ;
$c . contents ( ) . find ( "#writtingMode" ) . remove ( ) ;
$c . contents ( ) . find ( 'link[href$="editor-writting-mode.css"]' ) . remove ( ) ;
2015-06-15 18:01:48 +08:00
* /
2014-10-22 16:20:45 +08:00
$ ( "#noteItemListWrap, #notesAndSort" ) . show ( ) ;
$ ( "#noteList" ) . unbind ( "mouseenter" ) . unbind ( "mouseleave" ) ;
var theme = UserInfo . Theme || "default" ;
theme += ".css" ;
2015-06-15 18:01:48 +08:00
var $themeLink = $ ( "#themeLink" ) ;
// 如果之前不是normal才换
if ( this . $themeLink . attr ( 'href' ) . indexOf ( 'writting-overwrite.css' ) != - 1 ) {
this . $themeLink . attr ( "href" , "/css/theme/" + theme ) ;
}
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
$ ( "#noteList" ) . width ( UserInfo . NoteListWidth ) ;
$ ( "#note" ) . css ( "left" , UserInfo . NoteListWidth ) ;
2015-10-27 23:04:39 +08:00
this . isWritingMode = false ;
2015-11-28 21:31:56 +08:00
this . resizeEditor ( ) ;
2015-10-27 23:04:39 +08:00
} ;
2015-06-15 18:01:48 +08:00
2014-10-22 16:20:45 +08:00
editorMode . prototype . writtingMode = function ( ) {
2015-10-27 23:04:39 +08:00
if ( Note . inBatch ) {
return ;
}
2015-06-15 18:01:48 +08:00
if ( this . $themeLink . attr ( 'href' ) . indexOf ( 'writting-overwrite.css' ) == - 1 ) {
this . $themeLink . attr ( "href" , "/css/theme/writting-overwrite.css" ) ;
}
2015-10-27 23:04:39 +08:00
2015-06-15 18:01:48 +08:00
/ *
2014-10-22 16:20:45 +08:00
setTimeout ( function ( ) {
var $c = $ ( "#editorContent_ifr" ) . contents ( ) ;
$c . contents ( ) . find ( "head" ) . append ( '<link type="text/css" rel="stylesheet" href="/css/editor/editor-writting-mode.css" id="writtingMode">' ) ;
} , 0 ) ;
2015-06-15 18:01:48 +08:00
* /
2014-10-22 16:20:45 +08:00
$ ( "#noteItemListWrap, #notesAndSort" ) . fadeOut ( ) ;
$ ( "#noteList" ) . hover ( function ( ) {
$ ( "#noteItemListWrap, #notesAndSort" ) . fadeIn ( ) ;
} , function ( ) {
$ ( "#noteItemListWrap, #notesAndSort" ) . fadeOut ( ) ;
} ) ;
// 点击扩展会使html的height生成, 切换后会覆盖css文件的
2015-06-15 18:01:48 +08:00
// $("#mceToolbar").css("height", "40px");
2014-10-22 16:20:45 +08:00
//$("#pageInner").addClass("animated fadeInUp");
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
this . resizeEditor ( ) ;
$ ( "#noteList" ) . width ( 250 ) ;
$ ( "#note" ) . css ( "left" , 0 ) ;
2015-06-15 18:01:48 +08:00
// 切换到写模式
Note . toggleWriteable ( ) ;
2015-10-27 23:04:39 +08:00
this . isWritingMode = true ;
} ;
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
editorMode . prototype . getWritingCss = function ( ) {
if ( this . isWritingMode ) {
2014-12-09 23:17:36 +08:00
return [ "/css/editor/editor-writting-mode.css" ] ;
2014-05-07 13:06:24 +08:00
}
2014-10-22 16:20:45 +08:00
return [ ] ;
2014-05-07 13:06:24 +08:00
}
2014-10-22 16:20:45 +08:00
var em = new editorMode ( ) ;
2015-06-15 18:01:48 +08:00
LEA . em = em ;
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
//----------------
// 拖拉改变变宽度
var Resize = {
lineMove : false ,
mdLineMove : false ,
target : null ,
leftNotebook : $ ( "#leftNotebook" ) ,
notebookSplitter : $ ( "#notebookSplitter" ) ,
noteList : $ ( "#noteList" ) ,
noteAndEditor : $ ( "#noteAndEditor" ) ,
noteSplitter : $ ( "#noteSplitter" ) ,
note : $ ( "#note" ) ,
body : $ ( "body" ) ,
leftColumn : $ ( "#left-column" ) ,
2015-01-08 00:36:28 +08:00
rightColumn : $ ( "#right-column" ) , // $("#preview-panel"), //
mdSplitter : $ ( "#mdSplitter2" ) ,
2014-10-22 16:20:45 +08:00
init : function ( ) {
var self = this ;
self . initEvent ( ) ;
} ,
initEvent : function ( ) {
var self = this ;
// 鼠标点下
$ ( ".noteSplit" ) . bind ( "mousedown" , function ( event ) {
event . preventDefault ( ) ; // 防止选择文本
self . lineMove = true ;
$ ( this ) . css ( "background-color" , "#ccc" ) ;
self . target = $ ( this ) . attr ( "id" ) ;
// 防止iframe捕获不了事件
$ ( "#noteMask" ) . css ( "z-index" , 99999 ) ; // .css("background-color", // "#ccc");
} ) ;
// 鼠标点下
self . mdSplitter . bind ( "mousedown" , function ( event ) {
event . preventDefault ( ) ; // 防止选择文本
2015-01-08 00:36:28 +08:00
if ( $ ( this ) . hasClass ( 'open' ) ) {
self . mdLineMove = true ;
}
// $(this).css("background-color", "#ccc");
2014-10-22 16:20:45 +08:00
} ) ;
// 鼠标移动时
self . body . bind ( "mousemove" , function ( event ) {
if ( self . lineMove ) { // 如果没有这个if会导致不能选择文本
event . preventDefault ( ) ;
self . resize3Columns ( event ) ;
} else if ( self . mdLineMove ) {
event . preventDefault ( ) ;
self . resizeMdColumns ( event ) ;
}
} ) ;
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
// 鼠标放开, 结束
self . body . bind ( "mouseup" , function ( event ) {
self . stopResize ( ) ;
// 取消遮罩
$ ( "#noteMask" ) . css ( "z-index" , - 1 ) ;
} ) ;
2015-01-08 00:36:28 +08:00
// 瞬间
var everLeftWidth ;
$ ( '.layout-toggler-preview' ) . click ( function ( ) {
var $t = $ ( this ) ;
var $p = self . leftColumn . parent ( ) ;
// 是开的
if ( $t . hasClass ( 'open' ) ) {
var totalWidth = $p . width ( ) ;
var minRightWidth = 22 ;
var leftWidth = totalWidth - minRightWidth ;
everLeftWidth = self . leftColumn . width ( ) ;
self . leftColumn . width ( leftWidth ) ;
self . rightColumn . css ( 'left' , 'auto' ) . width ( minRightWidth ) ;
// 禁止split
$t . removeClass ( 'open' ) ; //.addClass('close');
self . rightColumn . find ( '.layout-resizer' ) . removeClass ( 'open' ) ;
$ ( '.preview-container' ) . hide ( ) ;
2016-02-27 15:30:01 +08:00
if ( MD ) {
MD . resize ( ) ;
}
2015-01-08 00:36:28 +08:00
} else {
$t . addClass ( 'open' ) ;
self . rightColumn . find ( '.layout-resizer' ) . addClass ( 'open' ) ;
self . leftColumn . width ( everLeftWidth ) ;
$ ( '.preview-container' ) . show ( ) ;
self . rightColumn . css ( 'left' , everLeftWidth ) . width ( 'auto' ) ;
2016-02-27 15:30:01 +08:00
if ( MD ) {
MD . resize ( ) ;
2015-01-08 00:36:28 +08:00
}
}
} ) ;
2014-10-22 16:20:45 +08:00
} ,
// 停止, 保存数据
stopResize : function ( ) {
var self = this ;
if ( self . lineMove || self . mdLineMove ) {
// ajax保存
ajaxGet ( "/user/updateColumnWidth" , { mdEditorWidth : UserInfo . MdEditorWidth , notebookWidth : UserInfo . NotebookWidth , noteListWidth : UserInfo . NoteListWidth } , function ( ) {
} ) ;
}
self . lineMove = false ;
self . mdLineMove = false ;
$ ( ".noteSplit" ) . css ( "background" , "none" ) ;
self . mdSplitter . css ( "background" , "none" ) ;
} ,
// 最终调用该方法
set3ColumnsWidth : function ( notebookWidth , noteListWidth ) {
var self = this ;
if ( notebookWidth < 150 || noteListWidth < 100 ) {
return ;
}
var noteWidth = self . body . width ( ) - notebookWidth - noteListWidth ;
if ( noteWidth < 400 ) {
return ;
}
self . leftNotebook . width ( notebookWidth ) ;
self . notebookSplitter . css ( "left" , notebookWidth ) ;
self . noteAndEditor . css ( "left" , notebookWidth ) ;
self . noteList . width ( noteListWidth ) ;
self . noteSplitter . css ( "left" , noteListWidth ) ;
self . note . css ( "left" , noteListWidth ) ;
UserInfo . NotebookWidth = notebookWidth ;
UserInfo . NoteListWidth = noteListWidth ;
} ,
resize3Columns : function ( event , isFromeIfr ) {
var self = this ;
if ( isFromeIfr ) {
event . clientX += self . body . width ( ) - self . note . width ( ) ;
}
var notebookWidth , noteListWidth ;
if ( self . lineMove ) {
if ( self . target == "notebookSplitter" ) {
notebookWidth = event . clientX ;
noteListWidth = self . noteList . width ( ) ;
self . set3ColumnsWidth ( notebookWidth , noteListWidth ) ;
} else {
notebookWidth = self . leftNotebook . width ( ) ;
noteListWidth = event . clientX - notebookWidth ;
self . set3ColumnsWidth ( notebookWidth , noteListWidth ) ;
}
resizeEditor ( ) ;
}
} ,
2015-11-22 22:58:38 +08:00
resizeMDInterval : null ,
2014-10-22 16:20:45 +08:00
// mdeditor
resizeMdColumns : function ( event ) {
var self = this ;
if ( self . mdLineMove ) {
2015-01-08 00:36:28 +08:00
var mdEditorWidth = event . clientX - self . leftColumn . offset ( ) . left ; // self.leftNotebook.width() - self.noteList.width();
2014-10-22 16:20:45 +08:00
self . setMdColumnWidth ( mdEditorWidth ) ;
2015-11-22 22:58:38 +08:00
clearInterval ( self . resizeMDInterval ) ;
self . resizeMDInterval = setTimeout ( function ( ) {
2015-11-28 21:31:56 +08:00
MD . resize && MD . resize ( ) ;
2015-11-22 22:58:38 +08:00
} , 50 ) ;
2014-10-22 16:20:45 +08:00
}
} ,
// 设置宽度
setMdColumnWidth : function ( mdEditorWidth ) {
var self = this ;
2015-11-28 15:33:30 +08:00
var allWidth = $ ( '#note' ) . width ( ) ;
if ( mdEditorWidth > 100 && mdEditorWidth < allWidth - 80 ) {
2014-10-22 16:20:45 +08:00
UserInfo . MdEditorWidth = mdEditorWidth ;
self . leftColumn . width ( mdEditorWidth ) ;
self . rightColumn . css ( "left" , mdEditorWidth ) ;
2015-01-08 00:36:28 +08:00
// self.mdSplitter.css("left", mdEditorWidth);
}
2015-11-28 15:33:30 +08:00
2015-01-08 00:36:28 +08:00
// 这样, scrollPreview 才会到正确的位置
if ( MD ) {
MD . onResize ( ) ;
2014-10-22 16:20:45 +08:00
}
}
}
//--------------------------
// 手机端访问之
Mobile = {
// 点击之笔记
// 切换到编辑器模式
noteO : $ ( "#note" ) ,
bodyO : $ ( "body" ) ,
setMenuO : $ ( "#setMenu" ) ,
2014-12-09 23:17:36 +08:00
// 弃用, 统一使用Pjax
2014-10-22 16:20:45 +08:00
hashChange : function ( ) {
var self = Mobile ;
var hash = location . hash ;
// noteId
if ( hash . indexOf ( "noteId" ) != - 1 ) {
self . toEditor ( false ) ;
var noteId = hash . substr ( 8 ) ;
Note . changeNote ( noteId , false , false ) ;
} else {
// 笔记本和笔记列表
self . toNormal ( false ) ;
}
} ,
init : function ( ) {
var self = this ;
self . isMobile ( ) ;
2014-12-09 23:17:36 +08:00
// $(window).on("hashchange", self.hashChange);
// self.hashChange();
2014-10-22 16:20:45 +08:00
/ *
$ ( "#noteItemList" ) . on ( "tap" , ".item" , function ( event ) {
$ ( this ) . click ( ) ;
} ) ;
$ ( document ) . on ( "swipeleft" , function ( e ) {
e . stopPropagation ( ) ;
e . preventDefault ( ) ;
self . toEditor ( ) ;
} ) ;
$ ( document ) . on ( "swiperight" , function ( e ) {
e . stopPropagation ( ) ;
e . preventDefault ( ) ;
self . toNormal ( ) ;
} ) ;
* /
} ,
isMobile : function ( ) {
var u = navigator . userAgent ;
LEA . isMobile = false ;
2014-12-02 22:42:46 +08:00
LEA . isMobile = /Mobile|Android|iPhone|iPad/i . test ( u ) ;
2015-01-08 00:36:28 +08:00
LEA . isIpad = /iPad/i . test ( u ) ;
LEA . isIphone = /iPhone/i . test ( u ) ;
2014-10-22 16:20:45 +08:00
if ( ! LEA . isMobile && $ ( document ) . width ( ) <= 700 ) {
LEA . isMobile = true
}
return LEA . isMobile ;
} ,
2014-12-09 23:17:36 +08:00
// 改变笔记, 此时切换到编辑器模式下
// note.js click事件处理, 先切换到纯编辑器下, 再调用Note.changeNote()
2014-10-22 16:20:45 +08:00
changeNote : function ( noteId ) {
var self = this ;
if ( ! LEA . isMobile ) { return true ; }
self . toEditor ( true , noteId ) ;
return false ;
} ,
toEditor : function ( changeHash , noteId ) {
var self = this ;
self . bodyO . addClass ( "full-editor" ) ;
self . noteO . addClass ( "editor-show" ) ;
2014-12-09 23:17:36 +08:00
/ *
2014-10-22 16:20:45 +08:00
if ( changeHash ) {
if ( ! noteId ) {
noteId = Note . curNoteId ;
}
location . hash = "noteId=" + noteId ;
}
2014-12-09 23:17:36 +08:00
* /
2014-10-22 16:20:45 +08:00
} ,
toNormal : function ( changeHash ) {
var self = this ;
self . bodyO . removeClass ( "full-editor" ) ;
self . noteO . removeClass ( "editor-show" ) ;
2014-12-09 23:17:36 +08:00
/ *
2014-10-22 16:20:45 +08:00
if ( changeHash ) {
location . hash = "notebookAndNote" ;
}
2014-12-09 23:17:36 +08:00
* /
2014-10-22 16:20:45 +08:00
} ,
switchPage : function ( ) {
var self = this ;
2014-12-02 22:42:46 +08:00
if ( ! LEA . isMobile || LEA . isIpad ) { return true ; }
2014-10-22 16:20:45 +08:00
if ( self . bodyO . hasClass ( "full-editor" ) ) {
self . toNormal ( true ) ;
} else {
self . toEditor ( true ) ;
}
return false ;
}
}
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
function initSlimScroll ( ) {
if ( Mobile . isMobile ( ) ) {
return ;
}
$ ( "#notebook" ) . slimScroll ( {
height : "100%" , // $("#leftNotebook").height()+"px"
2014-05-07 13:06:24 +08:00
} ) ;
2014-10-22 16:20:45 +08:00
$ ( "#noteItemList" ) . slimScroll ( {
height : "100%" , // ($("#leftNotebook").height()-42)+"px"
2014-05-07 13:06:24 +08:00
} ) ;
2015-01-08 00:36:28 +08:00
/ *
2014-10-22 16:20:45 +08:00
$ ( "#wmd-input" ) . slimScroll ( {
height : "100%" , // $("#wmd-input").height()+"px"
} ) ;
$ ( "#wmd-input" ) . css ( "width" , "100%" ) ;
2015-01-08 00:36:28 +08:00
* /
2014-10-22 16:20:45 +08:00
$ ( "#wmd-panel-preview" ) . slimScroll ( {
height : "100%" , // $("#wmd-panel-preview").height()+"px"
2014-05-07 13:06:24 +08:00
} ) ;
2014-10-22 16:20:45 +08:00
$ ( "#wmd-panel-preview" ) . css ( "width" , "100%" ) ;
}
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
//-----------
// 初始化编辑器
function initEditor ( ) {
// editor
2014-05-07 13:06:24 +08:00
// toolbar 下拉扩展, 也要resizeEditor
2014-10-22 16:20:45 +08:00
var mceToobarEverHeight = 0 ;
2014-05-07 13:06:24 +08:00
$ ( "#moreBtn" ) . click ( function ( ) {
saveBookmark ( ) ;
2015-06-15 18:01:48 +08:00
var $editor = $ ( '#editor' ) ;
if ( $editor . hasClass ( 'all-tool' ) ) {
$editor . removeClass ( 'all-tool' ) ;
2014-05-07 13:06:24 +08:00
} else {
2015-06-15 18:01:48 +08:00
$editor . addClass ( 'all-tool' ) ;
2014-05-07 13:06:24 +08:00
}
2015-06-15 18:01:48 +08:00
2014-05-07 13:06:24 +08:00
restoreBookmark ( ) ;
} ) ;
2014-10-22 16:20:45 +08:00
// 初始化编辑器
2014-05-07 13:06:24 +08:00
tinymce . init ( {
2015-01-08 00:36:28 +08:00
inline : true ,
2015-06-15 18:01:48 +08:00
theme : 'leanote' ,
2015-01-08 00:36:28 +08:00
valid _children : "+pre[div|#text|p|span|textarea|i|b|strong]" , // ace
/ *
protect : [
/\<\/?(if|endif)\>/g , // Protect <if> & </endif>
/\<xsl\:[^>]+\>/g , // Protect <xsl:...>
// /<pre.*?>.*?<\/pre>/g, // Protect <pre ></pre>
// /<p.*?>.*?<\/p>/g, // Protect <pre ></pre>
// /<\?php.*?\?>/g // Protect php code
] ,
* /
2014-05-07 13:06:24 +08:00
setup : function ( ed ) {
2015-06-15 18:01:48 +08:00
ed . on ( 'keydown' , function ( e ) {
// 如果是readony, 则不能做任何操作
var num = e . which ? e . which : e . keyCode ;
// 如果是readony, 则不能做任何操作, 除了复制
if ( Note . readOnly && ! ( ( e . ctrlKey || e . metaKey ) && num == 67 ) ) {
e . preventDefault ( ) ;
return ;
}
2015-09-05 23:30:24 +08:00
// 当输入的时候, 把当前raw删除掉
LeaAce . removeCurToggleRaw ( ) ;
2015-06-15 18:01:48 +08:00
} ) ;
2014-05-07 13:06:24 +08:00
// 为了把下拉菜单关闭
2015-11-22 22:58:38 +08:00
/ *
2014-05-07 13:06:24 +08:00
ed . on ( "click" , function ( e ) {
2015-05-06 23:54:10 +08:00
// $("body").trigger("click");
// console.log(tinymce.activeEditor.selection.getNode());
2014-05-07 13:06:24 +08:00
} ) ;
2015-11-22 22:58:38 +08:00
* /
// electron下有问题, Ace剪切导致行数减少, #16
ed . on ( 'cut' , function ( e ) {
if ( $ ( e . target ) . hasClass ( 'ace_text-input' ) ) {
e . preventDefault ( ) ;
return ;
}
} ) ;
2014-05-07 13:06:24 +08:00
} ,
2014-09-22 22:45:57 +08:00
// fix TinyMCE Removes site base url
// http://stackoverflow.com/questions/3360084/tinymce-removes-site-base-urls
convert _urls : true ,
relative _urls : false ,
remove _script _host : false ,
2014-05-07 13:06:24 +08:00
selector : "#editorContent" ,
2015-11-22 22:58:38 +08:00
// content_css 不再需要
// content_css : [LEA.sPath + "/css/editor/editor.css"], // .concat(em.getWritingCss()),
2014-05-07 13:06:24 +08:00
skin : "custom" ,
2016-03-19 14:05:09 +08:00
language : LEA . locale , // 语言
2014-05-07 13:06:24 +08:00
plugins : [
2015-09-05 23:30:24 +08:00
"autolink link leaui_image lists hr" , "paste" ,
2014-05-07 13:06:24 +08:00
"searchreplace leanote_nav leanote_code tabfocus" ,
2015-09-05 23:30:24 +08:00
"table textcolor" ] , // nonbreaking directionality charmap
2015-01-08 00:36:28 +08:00
toolbar1 : "formatselect | forecolor backcolor | bold italic underline strikethrough | leaui_image | leanote_code leanote_inline_code | bullist numlist | alignleft aligncenter alignright alignjustify" ,
2015-10-10 16:10:54 +08:00
toolbar2 : "outdent indent blockquote | link unlink | table | hr removeformat | subscript superscript |searchreplace | pastetext | leanote_ace_pre | fontselect fontsizeselect" ,
2014-05-07 13:06:24 +08:00
// 使用tab键: http://www.tinymce.com/wiki.php/Plugin3x:nonbreaking
// http://stackoverflow.com/questions/13543220/tiny-mce-how-to-allow-people-to-indent
// nonbreaking_force_tab : true,
2015-10-10 16:10:54 +08:00
2014-05-07 13:06:24 +08:00
menubar : false ,
toolbar _items _size : 'small' ,
statusbar : false ,
url _converter : false ,
font _formats : "Arial=arial,helvetica,sans-serif;"
+ "Arial Black=arial black,avant garde;"
+ "Times New Roman=times new roman,times;"
+ "Courier New=courier new,courier;"
+ "Tahoma=tahoma,arial,helvetica,sans-serif;"
+ "Verdana=verdana,geneva;" + "宋体=SimSun;"
+ "新宋体=NSimSun;" + "黑体=SimHei;"
+ "微软雅黑=Microsoft YaHei" ,
2015-01-08 00:36:28 +08:00
block _formats : "Header 1=h1;Header 2=h2;Header 3=h3;Header 4=h4;Paragraph=p" ,
/ *
2014-05-07 13:06:24 +08:00
codemirror : {
indentOnInit : true , // Whether or not to indent code on init.
path : 'CodeMirror' , // Path to CodeMirror distribution
config : { // CodeMirror config object
//mode: 'application/x-httpd-php',
lineNumbers : true
} ,
jsFiles : [ // Additional JS files to load
// 'mode/clike/clike.js',
//'mode/php/php.js'
]
} ,
2015-01-08 00:36:28 +08:00
* /
2014-05-07 13:06:24 +08:00
// This option specifies whether data:url images (inline images) should be removed or not from the pasted contents.
// Setting this to "true" will allow the pasted images, and setting this to "false" will disallow pasted images.
// For example, Firefox enables you to paste images directly into any contentEditable field. This is normally not something people want, so this option is "false" by default.
paste _data _images : true
} ) ;
// 刷新时保存 参考autosave插件
window . onbeforeunload = function ( e ) {
2015-11-28 21:31:56 +08:00
if ( LEA . isLogout ) {
return ;
}
2015-10-30 14:02:28 +08:00
Note . curChangedSaveIt ( true ) ;
2014-05-07 13:06:24 +08:00
}
2015-10-10 16:10:54 +08:00
2015-10-30 13:57:28 +08:00
// 全局快捷键
// ctrl + s 保存
// ctrl+e 切换只读与可写
$ ( 'body' ) . on ( 'keydown' , function ( e ) {
var num = e . which ? e . which : e . keyCode ;
var ctrlOrMetaKey = e . ctrlKey || e . metaKey ;
if ( ctrlOrMetaKey ) {
// 保存
if ( num == 83 ) { // ctrl + s or command + s
Note . curChangedSaveIt ( ) ;
e . preventDefault ( ) ;
return false ;
}
else if ( num == 69 ) { // e
Note . toggleWriteableAndReadOnly ( ) ;
e . preventDefault ( ) ;
return false ;
}
}
} ) ;
2014-10-22 16:20:45 +08:00
}
2014-05-07 13:06:24 +08:00
2014-10-22 16:20:45 +08:00
//-----------------------
// 导航
2014-05-07 13:06:24 +08:00
var random = 1 ;
function scrollTo ( self , tagName , text ) {
2015-01-08 00:36:28 +08:00
var iframe = $ ( "#editorContent" ) ; // .contents();
2014-05-07 13:06:24 +08:00
var target = iframe . find ( tagName + ":contains(" + text + ")" ) ;
random ++ ;
// 找到是第几个
// 在nav是第几个
var navs = $ ( '#leanoteNavContent [data-a="' + tagName + '-' + encodeURI ( text ) + '"]' ) ;
// alert('#leanoteNavContent [data-a="' + tagName + '-' + encodeURI(text) + '"]')
var len = navs . size ( ) ;
for ( var i = 0 ; i < len ; ++ i ) {
if ( navs [ i ] == self ) {
break ;
}
}
if ( target . size ( ) >= i + 1 ) {
target = target . eq ( i ) ;
// 之前插入, 防止多行定位不准
2015-01-08 00:36:28 +08:00
// log(target.scrollTop());
var top = iframe . scrollTop ( ) - iframe . offset ( ) . top + target . offset ( ) . top ; // 相对于iframe的位置
// var nowTop = iframe.scrollTop();
// log(nowTop);
// log(top);
2014-05-07 13:06:24 +08:00
// iframe.scrollTop(top);
2015-01-08 00:36:28 +08:00
iframe . animate ( { scrollTop : top } , 300 ) ; // 有问题
2014-05-07 13:06:24 +08:00
2015-01-08 00:36:28 +08:00
/ *
2014-05-07 13:06:24 +08:00
var d = 200 ; // 时间间隔
for ( var i = 0 ; i < d ; i ++ ) {
setTimeout (
( function ( top ) {
return function ( ) {
iframe . scrollTop ( top ) ;
}
} ) ( nowTop + 1.0 * i * ( top - nowTop ) / d ) , i ) ;
}
// 最后必然执行
setTimeout ( function ( ) {
iframe . scrollTop ( top ) ;
} , d + 5 ) ;
2015-01-08 00:36:28 +08:00
* /
2014-05-07 13:06:24 +08:00
return ;
}
}
2014-10-22 16:20:45 +08:00
//--------------
// 调用之
2015-09-05 23:30:24 +08:00
// $(function() {
LEA . s3 = new Date ( ) ;
console . log ( 'initing...' ) ;
2014-10-22 16:20:45 +08:00
// 窗口缩放时
$ ( window ) . resize ( function ( ) {
Mobile . isMobile ( ) ;
resizeEditor ( ) ;
} ) ;
// 初始化编辑器
initEditor ( ) ;
// 左侧, folder 展开与关闭
$ ( ".folderHeader" ) . click ( function ( ) {
var body = $ ( this ) . next ( ) ;
var p = $ ( this ) . parent ( ) ;
if ( ! body . is ( ":hidden" ) ) {
$ ( ".folderNote" ) . removeClass ( "opened" ) . addClass ( "closed" ) ;
// body.hide();
p . removeClass ( "opened" ) . addClass ( "closed" ) ;
$ ( this ) . find ( ".fa-angle-down" ) . removeClass ( "fa-angle-down" ) . addClass ( "fa-angle-right" ) ;
} else {
$ ( ".folderNote" ) . removeClass ( "opened" ) . addClass ( "closed" ) ;
// body.show();
p . removeClass ( "closed" ) . addClass ( "opened" ) ;
$ ( this ) . find ( ".fa-angle-right" ) . removeClass ( "fa-angle-right" ) . addClass ( "fa-angle-down" ) ;
}
} ) ;
2014-05-07 13:06:24 +08:00
// 导航隐藏与显示
2015-01-08 00:36:28 +08:00
$ ( ".leanoteNav h1" ) . on ( "click" , function ( e ) {
var $leanoteNav = $ ( this ) . closest ( '.leanoteNav' ) ;
if ( ! $leanoteNav . hasClass ( "unfolder" ) ) {
$leanoteNav . addClass ( "unfolder" ) ;
2014-05-07 13:06:24 +08:00
} else {
2015-01-08 00:36:28 +08:00
$leanoteNav . removeClass ( "unfolder" ) ;
2014-05-07 13:06:24 +08:00
}
} ) ;
2014-10-22 16:20:45 +08:00
// 邮箱验证
$ ( "#wrongEmail" ) . click ( function ( ) {
openSetInfoDialog ( 1 ) ;
} ) ;
2014-05-07 13:06:24 +08:00
$ ( "#setTheme" ) . click ( function ( ) {
showDialog2 ( "#setThemeDialog" , { title : "主题设置" , postShow : function ( ) {
if ( ! UserInfo . Theme ) {
UserInfo . Theme = "default" ;
}
$ ( "#themeForm input[value='" + UserInfo . Theme + "']" ) . attr ( "checked" , true ) ;
} } ) ;
} ) ;
//---------
// 主题
$ ( "#themeForm" ) . on ( "click" , "input" , function ( e ) {
var val = $ ( this ) . val ( ) ;
2015-10-27 23:04:39 +08:00
var preHref = $ ( "#themeLink" ) . attr ( "href" ) ; // default.css?id=7
var arr = preHref . split ( '=' ) ;
var id = 1 ;
if ( arr . length == 2 ) {
id = arr [ 1 ] ;
}
$ ( "#themeLink" ) . attr ( "href" , "/css/theme/" + val + ".css?id=" + id ) ;
2014-05-07 13:06:24 +08:00
ajaxPost ( "/user/updateTheme" , { theme : val } , function ( re ) {
if ( reIsOk ( re ) ) {
UserInfo . Theme = val
}
} ) ;
} ) ;
2015-11-22 22:58:38 +08:00
2014-05-07 13:06:24 +08:00
// 禁止双击选中文字
$ ( "#notebook, #newMyNote, #myProfile, #topNav, #notesAndSort" , "#leanoteNavTrigger" ) . bind ( "selectstart" , function ( e ) {
e . preventDefault ( ) ;
return false ;
} ) ;
// 左侧隐藏或展示
function updateLeftIsMin ( is ) {
ajaxGet ( "/user/updateLeftIsMin" , { leftIsMin : is } )
}
2015-11-22 22:58:38 +08:00
// 最小化左侧
var $page = $ ( '#page' ) ;
2014-05-07 13:06:24 +08:00
function minLeft ( save ) {
2015-11-22 22:58:38 +08:00
$page . addClass ( 'mini-left' ) ;
2014-05-07 13:06:24 +08:00
if ( save ) {
updateLeftIsMin ( true ) ;
}
}
2015-11-22 22:58:38 +08:00
// 展开右侧
2014-05-07 13:06:24 +08:00
function maxLeft ( save ) {
2015-11-22 22:58:38 +08:00
$page . removeClass ( 'mini-left' ) ;
2014-05-07 13:06:24 +08:00
$ ( "#noteAndEditor" ) . css ( "left" , UserInfo . NotebookWidth ) ;
$ ( "#leftNotebook" ) . width ( UserInfo . NotebookWidth ) ;
if ( save ) {
updateLeftIsMin ( false ) ;
}
}
2015-01-08 00:36:28 +08:00
$ ( "#leftSwitcher2" ) . on ( 'click' , function ( ) {
2014-05-07 13:06:24 +08:00
maxLeft ( true ) ;
} ) ;
2015-01-08 00:36:28 +08:00
$ ( "#leftSwitcher" ) . click ( 'click' , function ( ) {
2014-10-22 16:20:45 +08:00
if ( Mobile . switchPage ( ) ) {
minLeft ( true ) ;
2014-05-07 13:06:24 +08:00
}
} ) ;
// 得到最大dropdown高度
2014-09-12 09:55:16 +08:00
// 废弃
2014-05-07 13:06:24 +08:00
function getMaxDropdownHeight ( obj ) {
var offset = $ ( obj ) . offset ( ) ;
var maxHeight = $ ( document ) . height ( ) - offset . top ;
maxHeight -= 70 ;
if ( maxHeight < 0 ) {
maxHeight = 0 ;
}
var preHeight = $ ( obj ) . find ( "ul" ) . height ( ) ;
return preHeight < maxHeight ? preHeight : maxHeight ;
}
2014-09-12 09:55:16 +08:00
2014-05-07 13:06:24 +08:00
// mini版
2014-09-12 09:55:16 +08:00
// 点击展开
$ ( "#notebookMin div.minContainer" ) . click ( function ( ) {
var target = $ ( this ) . attr ( "target" ) ;
2014-09-12 21:40:14 +08:00
maxLeft ( true ) ;
2014-09-12 09:55:16 +08:00
if ( target == "#notebookList" ) {
if ( $ ( "#myNotebooks" ) . hasClass ( "closed" ) ) {
$ ( "#myNotebooks .folderHeader" ) . trigger ( "click" ) ;
}
} else if ( target == "#tagNav" ) {
if ( $ ( "#myTag" ) . hasClass ( "closed" ) ) {
$ ( "#myTag .folderHeader" ) . trigger ( "click" ) ;
}
} else {
if ( $ ( "#myShareNotebooks" ) . hasClass ( "closed" ) ) {
$ ( "#myShareNotebooks .folderHeader" ) . trigger ( "click" ) ;
}
2014-05-07 13:06:24 +08:00
}
2014-09-12 09:55:16 +08:00
} ) ;
2014-05-07 13:06:24 +08:00
//------------------------
// 界面设置, 左侧是否是隐藏的
UserInfo . NotebookWidth = UserInfo . NotebookWidth || $ ( "#notebook" ) . width ( ) ;
UserInfo . NoteListWidth = UserInfo . NoteListWidth || $ ( "#noteList" ) . width ( ) ;
2014-10-22 16:20:45 +08:00
Resize . init ( ) ;
Resize . set3ColumnsWidth ( UserInfo . NotebookWidth , UserInfo . NoteListWidth ) ;
Resize . setMdColumnWidth ( UserInfo . MdEditorWidth ) ;
2014-09-04 12:53:04 +08:00
2014-05-07 13:06:24 +08:00
if ( UserInfo . LeftIsMin ) {
minLeft ( false ) ;
}
2015-10-27 23:04:39 +08:00
else {
maxLeft ( false ) ;
}
2014-05-07 13:06:24 +08:00
// end
2014-10-22 16:20:45 +08:00
// 开始时显示loading......
// 隐藏mask
2014-05-07 13:06:24 +08:00
$ ( "#mainMask" ) . html ( "" ) ;
$ ( "#mainMask" ) . hide ( 100 ) ;
// 4/25 防止dropdown太高
// dropdown
$ ( '.dropdown' ) . on ( 'shown.bs.dropdown' , function ( ) {
var $ul = $ ( this ) . find ( "ul" ) ;
2014-09-11 18:55:42 +08:00
// $ul.css("max-height", getMaxDropdownHeight(this));
2014-05-07 13:06:24 +08:00
} ) ;
2015-10-10 16:10:54 +08:00
/ *
2014-05-07 13:06:24 +08:00
//--------
// 建议
$ ( "#yourSuggestions" ) . click ( function ( ) {
showDialog2 ( "#suggestionsDialog" ) ;
} ) ;
$ ( "#suggestionBtn" ) . click ( function ( e ) {
e . preventDefault ( ) ;
var suggestion = $ . trim ( $ ( "#suggestionTextarea" ) . val ( ) ) ;
if ( ! suggestion ) {
$ ( "#suggestionMsg" ) . html ( "请输入您的建议, 谢谢!" ) . show ( ) . addClass ( "alert-warning" ) . removeClass ( "alert-success" ) ;
$ ( "#suggestionTextarea" ) . focus ( ) ;
return ;
}
$ ( "#suggestionBtn" ) . html ( "正在处理..." ) . addClass ( "disabled" ) ;
$ ( "#suggestionMsg" ) . html ( "正在处理..." ) ;
$ . post ( "/suggestion" , { suggestion : suggestion } , function ( ret ) {
$ ( "#suggestionBtn" ) . html ( "提交" ) . removeClass ( "disabled" ) ;
if ( ret . Ok ) {
$ ( "#suggestionMsg" ) . html ( "谢谢反馈, 我们会第一时间处理, 祝您愉快!" ) . addClass ( "alert-success" ) . removeClass ( "alert-warning" ) . show ( ) ;
} else {
$ ( "#suggestionMsg" ) . html ( "出错了" ) . show ( ) . addClass ( "alert-warning" ) . removeClass ( "alert-success" ) ;
}
} ) ;
} ) ;
2015-10-10 16:10:54 +08:00
* /
2014-05-07 13:06:24 +08:00
2014-05-13 14:34:30 +08:00
// 编辑器模式
em . init ( ) ;
2014-05-13 21:18:33 +08:00
2014-10-22 16:20:45 +08:00
// 手机端?
Mobile . init ( ) ;
2015-09-05 23:30:24 +08:00
//});
2014-12-09 23:17:36 +08:00
//------------
// pjax
//------------
var Pjax = {
init : function ( ) {
var me = this ;
// 当history改变时
window . addEventListener ( 'popstate' , function ( evt ) {
var state = evt . state ;
if ( ! state ) {
return ;
}
document . title = state . title || "Untitled" ;
log ( "pop" ) ;
me . changeNotebookAndNote ( state . noteId ) ;
} , false ) ;
// ie9
if ( ! history . pushState ) {
$ ( window ) . on ( "hashchange" , function ( ) {
var noteId = getHash ( "noteId" ) ; ;
if ( noteId ) {
me . changeNotebookAndNote ( noteId ) ;
}
} ) ;
}
} ,
// pjax调用
// popstate事件发生时, 转换到noteId下, 此时要转换notebookId
changeNotebookAndNote : function ( noteId ) {
var note = Note . getNote ( noteId ) ;
if ( ! note ) {
return ;
}
var isShare = note . Perm != undefined ;
var notebookId = note . NotebookId ;
// 如果是在当前notebook下, 就不要转换notebook了
if ( Notebook . curNotebookId == notebookId ) {
// 不push state
Note . changeNoteForPjax ( noteId , false ) ;
return ;
}
// 自己的
if ( ! isShare ) {
// 先切换到notebook下, 得到notes列表, 再changeNote
Notebook . changeNotebook ( notebookId , function ( notes ) {
Note . renderNotes ( notes ) ;
// 不push state
Note . changeNoteForPjax ( noteId , false , true ) ;
} ) ;
// 共享笔记
} else {
Share . changeNotebook ( note . UserId , notebookId , function ( notes ) {
Note . renderNotes ( notes ) ;
// 不push state
Note . changeNoteForPjax ( noteId , false , true ) ;
} ) ;
}
} ,
// ajax后调用
changeNote : function ( noteInfo ) {
var me = this ;
var noteId = noteInfo . NoteId ;
var title = noteInfo . Title ;
var url = '/note/' + noteId ;
2015-10-10 16:55:36 +08:00
if ( location . href . indexOf ( '?online' ) > 0 ) {
2015-11-22 22:58:38 +08:00
url += '?online=' + /online=([0-9])/ . exec ( location . href ) [ 1 ] ;
2015-10-10 16:55:36 +08:00
}
2014-12-09 23:17:36 +08:00
if ( location . hash ) {
url += location . hash ;
}
// 如果支持pushState
if ( history . pushState ) {
var state = ( {
url : url ,
noteId : noteId ,
title : title ,
} ) ;
history . pushState ( state , title , url ) ;
document . title = title || 'Untitled' ;
// 不支持, 则用hash
} else {
setHash ( "noteId" , noteId ) ;
}
}
} ;
$ ( function ( ) {
Pjax . init ( ) ;
} ) ;
2015-01-08 00:36:28 +08:00
//----------
// aceEditor
LeaAce = {
// aceEditorID
_aceId : 0 ,
// {id=>ace}
_aceEditors : { } ,
_isInit : false ,
_canAce : false ,
isAce : true , // 切换pre, 默认是true
disableAddHistory : function ( ) {
tinymce . activeEditor . undoManager . setCanAdd ( false ) ;
} ,
resetAddHistory : function ( ) {
tinymce . activeEditor . undoManager . setCanAdd ( true ) ;
} ,
canAce : function ( ) {
if ( this . _isInit ) {
return this . _canAce ;
}
if ( getVendorPrefix ( ) == "webkit" && ! Mobile . isMobile ( ) ) {
this . _canAce = true ;
} else {
this . _canAce = false ;
}
this . _isInit = true ;
return this . _canAce ;
} ,
canAndIsAce : function ( ) {
return this . canAce ( ) && this . isAce ;
} ,
getAceId : function ( ) {
this . aceId ++ ;
return "leanote_ace_" + ( new Date ( ) ) . getTime ( ) + "_" + this . _aceId ;
} ,
initAce : function ( id , val , force ) {
var me = this ;
if ( ! force && ! me . canAndIsAce ( ) ) {
return ;
}
var $pre = $ ( '#' + id ) ;
2015-05-06 23:54:10 +08:00
if ( $pre . length == 0 ) {
return ;
}
var rawCode = $pre . html ( ) ; // 原生code
try {
me . disableAddHistory ( ) ;
// 本身就有格式的, 防止之前有格式的显示为<span>(ace下)
2015-06-15 18:01:48 +08:00
var classes = $pre . attr ( 'class' ) || '' ;
var isHtml = classes . indexOf ( 'brush:html' ) != - 1 ;
if ( $pre . attr ( 'style' ) ||
( ! isHtml && $pre . html ( ) . indexOf ( 'style' ) != - 1 ) ) { // 如果是html就不用考虑了, 因为html格式的支持有style
2015-05-06 23:54:10 +08:00
$pre . html ( $pre . text ( ) ) ;
}
$pre . find ( '.toggle-raw' ) . remove ( ) ;
var preHtml = $pre . html ( ) ;
2015-01-08 00:36:28 +08:00
2015-05-06 23:54:10 +08:00
$pre . removeClass ( 'ace-to-pre' ) ;
$pre . attr ( "contenteditable" , false ) ; // ? 避免tinymce编辑
var aceEditor = ace . edit ( id ) ;
2015-01-08 00:36:28 +08:00
2015-12-25 11:46:28 +08:00
aceEditor . container . style . lineHeight = 1.5 ;
2015-05-06 23:54:10 +08:00
aceEditor . setTheme ( "ace/theme/tomorrow" ) ;
2015-01-08 00:36:28 +08:00
2015-05-06 23:54:10 +08:00
var brush = me . getPreBrush ( $pre ) ;
var b = "" ;
if ( brush ) {
try {
b = brush . split ( ':' ) [ 1 ] ;
} catch ( e ) { }
}
2015-11-22 22:58:38 +08:00
if ( ! b || b === 'false' ) {
b = 'javascript' ;
}
2015-12-25 11:46:28 +08:00
2015-05-06 23:54:10 +08:00
aceEditor . session . setMode ( "ace/mode/" + b ) ;
aceEditor . session . setOption ( "useWorker" , false ) ; // 不用语法检查
// retina
if ( window . devicePixelRatio == 2 ) {
aceEditor . setFontSize ( "12px" ) ;
}
else {
aceEditor . setFontSize ( "14px" ) ;
}
aceEditor . getSession ( ) . setUseWorker ( false ) ; // 不用语法检查
aceEditor . setOption ( "showInvisibles" , false ) ; // 不显示空格, 没用
aceEditor . setShowInvisibles ( false ) ; // OK 不显示空格
aceEditor . setOption ( "wrap" , "free" ) ;
aceEditor . setShowInvisibles ( false ) ;
2015-06-15 18:01:48 +08:00
aceEditor . setReadOnly ( Note . readOnly ) ;
2015-05-06 23:54:10 +08:00
aceEditor . setAutoScrollEditorIntoView ( true ) ;
aceEditor . setOption ( "maxLines" , 10000 ) ;
aceEditor . commands . addCommand ( {
name : "undo" ,
bindKey : { win : "Ctrl-z" , mac : "Command-z" } ,
exec : function ( editor ) {
var undoManager = editor . getSession ( ) . getUndoManager ( ) ;
if ( undoManager . hasUndo ( ) ) {
undoManager . undo ( ) ;
} else {
undoManager . reset ( ) ;
tinymce . activeEditor . undoManager . undo ( ) ;
}
}
} ) ;
this . _aceEditors [ id ] = aceEditor ;
if ( val ) {
aceEditor . setValue ( val ) ;
// 不要选择代码
// TODO
} else {
// 防止 <pre><div>xx</div></pre> 这里的<div>消失
// preHtml = preHtml.replace('/ /g', ' '); // 以前是把' ' 全换成了
// aceEditor.setValue(preHtml);
// 全不选
// aceEditor.selection.clearSelection();
}
2015-01-08 00:36:28 +08:00
2015-05-06 23:54:10 +08:00
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
me . resetAddHistory ( ) ;
return aceEditor ;
} catch ( e ) {
// 当有错误时, 会有XXXXX的形式, 此时不要ace, 直接原生的!!!
console . error ( 'ace error!!!!' ) ;
console . error ( e ) ;
$pre . attr ( "contenteditable" , true ) ;
$pre . removeClass ( 'ace-tomorrow ace_editor ace-tm' ) ;
$pre . html ( rawCode ) ;
me . resetAddHistory ( ) ;
}
2015-01-08 00:36:28 +08:00
} ,
clearIntervalForInitAce : null ,
initAceFromContent : function ( editor ) {
if ( ! this . canAndIsAce ( ) ) {
var content = $ ( editor . getBody ( ) ) ;
content . find ( 'pre' ) . removeClass ( 'ace_editor' ) ;
return ;
}
var me = this ;
// 延迟
if ( this . clearIntervalForInitAce ) {
clearInterval ( this . clearIntervalForInitAce ) ;
}
this . clearIntervalForInitAce = setTimeout ( function ( ) {
var content = $ ( editor . getBody ( ) ) ;
var pres = content . find ( 'pre' ) ;
for ( var i = 0 ; i < pres . length ; ++ i ) {
var pre = pres . eq ( i ) ;
2015-06-15 18:01:48 +08:00
var aceAndNode = me . isInAce ( pre ) ;
if ( aceAndNode ) {
if ( isAceError ( aceAndNode [ 0 ] . getValue ( ) ) ) {
console . error ( '之前有些没有destroy掉' ) ;
}
else {
break ;
}
2015-01-08 00:36:28 +08:00
}
2015-06-15 18:01:48 +08:00
2015-01-08 00:36:28 +08:00
setTimeout ( ( function ( pre ) {
return function ( ) {
pre . find ( '.toggle-raw' ) . remove ( ) ;
var value = pre . html ( ) ;
value = value . replace ( / /g , " " ) . replace ( /\<br *\/*\>/gi , "\n" ) . replace ( /</g , '<' ) . replace ( />/g , '>' ) ;
pre . html ( value ) ;
var id = pre . attr ( 'id' ) ;
if ( ! id ) {
id = me . getAceId ( ) ;
pre . attr ( 'id' , id ) ;
}
me . initAce ( id ) ;
}
} ) ( pre ) ) ;
}
} , 10 ) ;
} ,
allToPre : function ( editor ) {
if ( ! this . canAndIsAce ( ) ) {
return ;
}
var me = this ;
// 延迟
if ( me . clearIntervalForInitAce ) {
clearInterval ( me . clearIntervalForInitAce ) ;
}
me . clearIntervalForInitAce = setTimeout ( function ( ) {
var content = $ ( editor . getBody ( ) ) ;
var pres = content . find ( 'pre' ) ;
for ( var i = 0 ; i < pres . length ; ++ i ) {
var pre = pres . eq ( i ) ;
setTimeout ( ( function ( pre ) {
return function ( ) {
me . aceToPre ( pre ) ;
}
} ) ( pre ) ) ;
}
} , 10 ) ;
} ,
undo : function ( editor ) {
if ( ! this . canAndIsAce ( ) ) {
return ;
}
var me = this ;
// 延迟
if ( this . clearIntervalForInitAce ) {
clearInterval ( this . clearIntervalForInitAce ) ;
}
this . clearIntervalForInitAce = setTimeout ( function ( ) {
var content = $ ( editor . getBody ( ) ) ;
var pres = content . find ( 'pre' ) ;
for ( var i = 0 ; i < pres . length ; ++ i ) {
var pre = pres . eq ( i ) ;
setTimeout ( ( function ( pre ) {
return function ( ) {
var value = pre . html ( ) ;
var id = pre . attr ( 'id' ) ;
var aceEditor = me . getAce ( id ) ;
if ( aceEditor ) {
var value = aceEditor . getValue ( ) ;
aceEditor . destroy ( ) ;
var aceEditor = me . initAce ( id , value ) ;
// 全不选
aceEditor . selection . clearSelection ( ) ;
} else {
value = value . replace ( / /g , " " ) . replace ( /\<br *\/*\>/gi , "\n" ) ;
pre . html ( value ) ;
var id = pre . attr ( 'id' ) ;
if ( ! id ) {
id = me . getAceId ( ) ;
pre . attr ( 'id' , id ) ;
}
me . initAce ( id ) ;
}
}
} ) ( pre ) ) ;
}
} , 10 ) ;
} ,
destroyAceFromContent : function ( everContent ) {
if ( ! this . canAce ( ) ) {
return ;
}
var pres = everContent . find ( 'pre' ) ;
for ( var i = 0 ; i < pres . length ; ++ i ) {
var id = pres . eq ( i ) . attr ( 'id' ) ;
var aceEditorAndPre = this . getAce ( id ) ;
if ( aceEditorAndPre ) {
aceEditorAndPre . destroy ( ) ;
this . _aceEditors [ id ] = null ;
}
}
} ,
getAce : function ( id ) {
if ( ! this . canAce ( ) ) {
return ;
}
return this . _aceEditors [ id ] ;
} ,
2015-06-15 18:01:48 +08:00
setAceReadOnly : function ( pre , readOnly ) {
var me = this ;
if ( typeof pre == 'object' ) {
var id = pre . attr ( 'id' ) ;
}
else {
var id = pre ;
}
var ace = me . getAce ( id ) ;
if ( ace ) {
ace . setReadOnly ( readOnly ) ;
}
} ,
2015-01-08 00:36:28 +08:00
// 当前焦点是否在aceEditor中
nowIsInAce : function ( ) {
if ( ! this . canAce ( ) ) {
return ;
}
var node = tinymce . activeEditor . selection . getNode ( ) ;
// log("now...");
// log(node);
return this . isInAce ( node ) ;
} ,
nowIsInPre : function ( ) {
var node = tinymce . activeEditor . selection . getNode ( ) ;
// log("now...");
// log(node);
return this . isInPre ( node ) ;
} ,
isInPre : function ( node ) {
var $node = $ ( node ) ;
var node = $node . get ( 0 ) ;
if ( node . nodeName == "PRE" ) {
return true ;
} else {
// 找到父是pre
$pre = $node . closest ( "pre" ) ;
if ( $pre . length == 0 ) {
return false ;
}
return true ;
}
} ,
// 是否在node内
isInAce : function ( node ) {
if ( ! this . canAce ( ) ) {
return ;
}
var $node = $ ( node ) ;
var node = $node . get ( 0 ) ;
if ( node . nodeName == "PRE" ) {
// $node.data('brush', brush);
var id = $node . attr ( 'id' ) ;
var aceEditor = this . getAce ( id ) ;
if ( aceEditor ) {
return [ aceEditor , $node ] ;
}
return false ;
} else {
// 找到父是pre
$pre = $node . closest ( "pre" ) ;
if ( $pre . length == 0 ) {
return false ;
}
return this . isInAce ( $pre ) ;
}
return false ;
} ,
getPreBrush : function ( node ) {
var $pre = $ ( node ) ;
var classes = $pre . attr ( 'class' ) ;
if ( ! classes ) {
return '' ;
}
var m = classes . match ( /brush:[^ ]*/ ) ;
var everBrush = "" ;
if ( m && m . length > 0 ) {
everBrush = m [ 0 ] ;
}
return everBrush ;
} ,
// pre转换成ace
preToAce : function ( pre , force ) {
if ( ! force && ! this . canAce ( ) ) {
return ;
}
var $pre = $ ( pre ) ;
var id = this . getAceId ( ) ;
$pre . attr ( 'id' , id ) ;
var editor = this . initAce ( id , "" , true ) ;
if ( editor ) {
editor . focus ( ) ;
}
} ,
aceToPre : function ( pre , isFocus ) {
var me = this ;
var $pre = $ ( pre ) ;
// 转成pre
var aceEditorAndPre = me . isInAce ( $pre ) ;
if ( aceEditorAndPre ) {
var aceEditor = aceEditorAndPre [ 0 ] ;
var $pre = aceEditorAndPre [ 1 ] ;
var value = aceEditor . getValue ( ) ;
2015-06-15 18:01:48 +08:00
// 表示有错
if ( isAceError ( value ) ) {
value = $pre . html ( ) ;
}
2015-01-08 00:36:28 +08:00
value = value . replace ( /</g , '<' ) . replace ( />/g , '>' ) ;
// var id = getAceId();
var replacePre = $ ( '<pre class="' + $pre . attr ( 'class' ) + ' ace-to-pre">' + value + "</pre>" ) ;
$pre . replaceWith ( replacePre ) ;
aceEditor . destroy ( ) ;
me . _aceEditors [ $pre . attr ( 'id' ) ] = null ;
// log($replacePre);
if ( isFocus ) {
setTimeout ( function ( ) {
var tinymceEditor = tinymce . activeEditor ;
var selection = tinymceEditor . selection ;
var rng = selection . getRng ( ) ;
// rng.setStart(replacePre.get(0), 1);
// rng.setEnd(replacePre.get(0), 9);
rng . selectNode ( replacePre . get ( 0 ) ) ;
// selection.setRng(rng);
// replacePre.focus();
tinymceEditor . focus ( ) ;
replacePre . trigger ( "click" ) ;
replacePre . html ( value + " " ) ;
// log(">>>>>>>>>>>>>>")
} , 0 ) ;
}
}
} ,
2015-09-05 23:30:24 +08:00
// 当删除了pre时, 也要删除toggle raw
removeAllToggleRaw : function ( ) {
$ ( '#editorContent .toggle-raw' ) . remove ( ) ;
} ,
removeCurToggleRaw : function ( ) {
if ( this . curToggleRaw ) {
try {
this . curToggleRaw . remove ( ) ;
}
catch ( e ) { }
}
} ,
curToggleRaw : null ,
2015-01-08 00:36:28 +08:00
// 转换raw <-> code
handleEvent : function ( ) {
if ( ! this . canAce ( ) ) {
return ;
}
var me = this ;
2015-09-05 23:30:24 +08:00
$ ( "#editorContent" ) . on ( 'mouseenter' , 'pre' , function ( e ) {
2015-01-08 00:36:28 +08:00
// log('in');
// log($(this));
var $t = $ ( this ) ;
$raw = $t . find ( '.toggle-raw' ) ;
if ( $raw . length == 0 ) {
2015-09-05 23:30:24 +08:00
var curToggleRaw = $ ( '<div class="toggle-raw" title="Toggle code with raw html"><input type="checkbox" /></div>' ) ;
$t . append ( curToggleRaw ) ;
me . curToggleRaw = curToggleRaw ;
2015-01-08 00:36:28 +08:00
}
$input = $t . find ( '.toggle-raw input' ) ;
if ( LeaAce . isInAce ( $t ) ) {
$input . prop ( 'checked' , true ) ;
} else {
$input . prop ( 'checked' , false ) ;
}
} ) ;
$ ( "#editorContent" ) . on ( 'mouseleave' , 'pre' , function ( ) {
var $raw = $ ( this ) . find ( '.toggle-raw' ) ;
$raw . remove ( ) ;
} ) ;
$ ( "#editorContent" ) . on ( 'change' , '.toggle-raw input' , function ( ) {
var checked = $ ( this ) . prop ( 'checked' ) ;
var $pre = $ ( this ) . closest ( 'pre' ) ;
2015-09-05 23:30:24 +08:00
if ( checked ) {
2015-01-08 00:36:28 +08:00
// 转成ace
me . preToAce ( $pre , true ) ;
} else {
me . aceToPre ( $pre , true ) ;
}
} ) ;
2015-09-05 23:30:24 +08:00
// 当ace里没有内容时, 连续删除则把ace remove掉
// keydown的delete事件没有
var lastDeleteTime ;
$ ( "#editorContent" ) . on ( 'keyup' , 'pre' , function ( e ) {
var keyCode = e . keyCode ;
// console.log('keyup');
if ( keyCode == 8 || keyCode == 46 ) { // BackSpace || Delete
// console.log('delete');
if ( ! lastDeleteTime ) {
lastDeleteTime = ( new Date ( ) ) . getTime ( ) ;
}
else {
var now = ( new Date ( ) ) . getTime ( ) ;
if ( now - lastDeleteTime < 300 ) { // 间隔时间很短
var inAce = me . isInAce ( $ ( this ) )
if ( inAce && ! inAce [ 0 ] . getValue ( ) ) {
// console.log('destroy');
inAce [ 0 ] . destroy ( ) ;
$ ( this ) . remove ( ) ;
return ;
}
}
lastDeleteTime = now ;
}
// console.log($(this));
}
} ) ;
2015-01-08 00:36:28 +08:00
}
} ;
2014-12-09 23:17:36 +08:00
// note.html调用
// 实始化页面
function initPage ( ) {
2015-09-05 23:30:24 +08:00
// 不要用$(function() {}) 因为要等到<script>都加载了才执行
// $(function() {
2014-12-09 23:17:36 +08:00
Notebook . renderNotebooks ( notebooks ) ;
Share . renderShareNotebooks ( sharedUserInfos , shareNotebooks ) ;
// 如果初始打开的是共享的笔记
// 那么定位到我的笔记
if ( curSharedNoteNotebookId ) {
Share . firstRenderShareNote ( curSharedUserId , curSharedNoteNotebookId , curNoteId ) ;
// 初始打开的是我的笔记
} else {
Note . setNoteCache ( noteContentJson ) ;
Note . renderNotes ( notes ) ;
if ( curNoteId ) {
// 指定某个note时才target notebook, /note定位到最新
// ie10&+要setTimeout
setTimeout ( function ( ) {
Note . changeNoteForPjax ( curNoteId , true , curNotebookId ) ;
} ) ;
if ( ! curNotebookId ) {
Notebook . selectNotebook ( $ ( tt ( '#notebook [notebookId="?"]' , Notebook . allNotebookId ) ) ) ;
}
}
}
2015-09-05 23:30:24 +08:00
2014-12-09 23:17:36 +08:00
// 指定笔记, 也要保存最新笔记
if ( latestNotes . length > 0 ) {
for ( var i = 0 ; i < latestNotes . length ; ++ i ) {
Note . addNoteCache ( latestNotes [ i ] ) ;
}
}
Tag . renderTagNav ( tagsJson ) ;
// init notebook后才调用
initSlimScroll ( ) ;
2015-01-08 00:36:28 +08:00
LeaAce . handleEvent ( ) ;
2015-09-05 23:30:24 +08:00
// });
2014-12-09 23:17:36 +08:00
}