forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookstack.js
More file actions
320 lines (296 loc) · 11.1 KB
/
bookstack.js
File metadata and controls
320 lines (296 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/***
* 加载文档到阅读区
* @param $url
* @param $id
* @param $callback
*/
// function loadDocument($url,$id,$callback) {
// $.ajax({
// url : $url,
// type : "GET",
// beforeSend :function (xhr) {
// var body = events.data('body_' + $id);
// var title = events.data('title_' + $id);
// var doc_title = events.data('doc_title_' + $id);
//
// if(body && title && doc_title){
//
// if (typeof $callback === "function") {
// body = $callback(body);
// }
// $("#page-content").html(body);
// $("title").text(title);
// $("#article-title").text(doc_title);
//
// events.trigger('article.open',{ $url : $url, $init : false , $id : $id });
//
// return false;
// }
// NProgress.start();
// },
// success : function (res) {
// if(res.errcode === 0){
// console.log("bookstack.cn");
// var body = res.data.body;
// var doc_title = res.data.doc_title;
// var title = res.data.title;
// $body = body;
// if (typeof $callback === "function" ){
// $body = $callback(body);
// }
// $("#page-content").html($body);
// $("title").text(title);
// $("#article-title").text(doc_title);
//
// events.data('body_' + $id,body);
// events.data('title_' + $id,title);
// events.data('doc_title_' + $id,doc_title);
// events.trigger('article.open',{ $url : $url, $init : true, $id : $id });
// }else{
// location.href=$url;
// //可能是存在缓存导致的加载失败,如果加载失败,直接刷新需要打开的链接【注意layer.js的引入】
// // layer.msg("加载失败");
// }
// },
// complete : function () {
// NProgress.done();
// }
// });
// }
//渲染markdown为html
function RenderByMarkdown($content) {
testEditormdView = editormd.markdownToHTML("page-content", {
markdown : $content ,//+ "\r\n" + $("#append-test").text(),
//htmlDecode : true, // 开启 HTML 标签解析,为了安全性,默认不开启
htmlDecode : "style,script,iframe", // you can filter tags decode
//toc : false,
tocm : true, // Using [TOCM]
//tocContainer : "#custom-toc-container", // 自定义 ToC 容器层
//gfm : false,
//tocDropdown : true,
// markdownSourceCode : true, // 是否保留 Markdown 源码,即是否删除保存源码的 Textarea 标签
emoji : true,
taskList : true,
tex : true, // 默认不解析
flowChart : true, // 默认不解析
sequenceDiagram : true, // 默认不解析
});
}
/***
* 加载文档到阅读区
* @param $url
* @param $id
* @param $callback
*/
function loadDocument($url,$id,$callback) {
$.ajax({
url : $url+"?fr=BookStack",
type : "GET",
beforeSend :function (xhr) {
var body = events.data('body_' + $id);
var title = events.data('title_' + $id);
var doc_title = events.data('doc_title_' + $id);
if(body && title && doc_title){
if (typeof $callback === "function") {
body = $callback(body);
}
// RenderByMarkdown(body);
$("#page-content").html(body);
$("title").text(title);
$("#article-title").text(doc_title);
events.trigger('article.open',{ $url : $url, $init : false , $id : $id });
return false;
}
NProgress.start();
},
success : function (res) {
if(res.errcode === 0){
console.log("BookStack");
var body = res.data.body;
var doc_title = res.data.doc_title;
var title = res.data.title;
$body = body;
if (typeof $callback === "function" ){
$body = $callback(body);
}
$("#page-content").html($body);
// RenderByMarkdown($body);
$("title").text(title);
$("#article-title").text(doc_title);
events.data('body_' + $id,body);
events.data('title_' + $id,title);
events.data('doc_title_' + $id,doc_title);
events.trigger('article.open',{ $url : $url, $init : true, $id : $id });
}else{
location.href=$url;
//可能是存在缓存导致的加载失败,如果加载失败,直接刷新需要打开的链接【注意layer.js的引入】
// layer.msg("加载失败");
}
},
complete : function () {
NProgress.done();
}
});
}
function initHighlighting() {
$('pre code').each(function (i, block) {
hljs.highlightBlock(block);
});
hljs.initLineNumbersOnLoad();
}
var events = $("body");
$(function () {
$(".view-backtop").on("click", function () {
$('.manual-right').animate({ scrollTop: '0px' }, 200);
});
$(".manual-right").scroll(function () {
var top = $(".manual-right").scrollTop();
if(top > 100){
$(".view-backtop").addClass("active");
}else{
$(".view-backtop").removeClass("active");
}
});
window.isFullScreen = false;
initHighlighting();
window.jsTree = $("#sidebar").jstree({
'plugins':["wholerow","types"],
"types": {
"default" : {
"icon" : false // 删除默认图标
}
},
'core' : {
'check_callback' : true,
"multiple" : false ,
'animation' : 0
}
}).on('select_node.jstree',function (node,selected,event) {
$(".m-manual").removeClass('manual-mobile-show-left');
var url = selected.node.a_attr.href;
if(url === window.location.href){
return false;
}
loadDocument(url,selected.node.id);
});
$("#slidebar").on("click",function () {
$(".m-manual").addClass('manual-mobile-show-left');
});
$(".manual-mask").on("click",function () {
$(".m-manual").removeClass('manual-mobile-show-left');
});
/**
* 关闭侧边栏
*/
$(".manual-fullscreen-switch").on("click",function () {
isFullScreen = !isFullScreen;
if (isFullScreen) {
$(".m-manual").addClass('manual-fullscreen-active');
} else {
$(".m-manual").removeClass('manual-fullscreen-active');
}
});
//处理打开事件
events.on('article.open', function (event, $param) {
if ('pushState' in history) {
if ($param.$init === false) {
window.history.replaceState($param , $param.$id , $param.$url);
} else {
window.history.pushState($param, $param.$id , $param.$url);
}
} else {
window.location.hash = $param.$url;
}
initHighlighting();
$(".manual-right").scrollTop(0);
//上一篇和下一篇的链接
var links=$("#menu-hidden a"),link_active=location.pathname,l=links.length;
for(var i=0;i<l;i++){
if (encodeURI($(links[i]).attr("href"))==link_active){
$(".hung-read-link .col-xs-12").hide();
var link_pre=$(links[i-1]),link_next=$(links[i+1]);
if(link_pre && link_pre.text()){
$(".hung-pre a").attr("href",link_pre.attr("href"));
$(".hung-pre a").text(link_pre.text());
$(".hung-pre").show();
}
if(link_next && link_next.text()){
$(".hung-next a").attr("href",link_next.attr("href"));
$(".hung-next a").text(link_next.text());
$(".hung-next").show();
}
i=l;
}
}
});
$(".hung-read-link a").click(function (e) {
//使用笨方法解决无刷新的问题。
$selector=$('a.jstree-anchor[href="'+$(this).attr("href")+'"]');
if($selector.length>0){
e.preventDefault();
var CurSelector=$('a.jstree-anchor[href="'+$(this).attr("href")+'"]');
if (CurSelector.parent().hasClass("jstree-closed")){
CurSelector.parent().find(".jstree-icon").trigger("click");
}
$('a.jstree-anchor[href="'+$(this).attr("href")+'"]').trigger("click");
}
});
$(".navg-item[data-mode]").on("click",function () {
var mode = $(this).data('mode');
$(this).siblings().removeClass('active').end().addClass('active');
$(".m-manual").removeClass("manual-mode-view manual-mode-collect manual-mode-search").addClass("manual-mode-" + mode);
});
/**
* 项目内搜索
*/
$("#searchForm").ajaxForm({
beforeSubmit : function () {
var keyword = $.trim($("#searchForm").find("input[name='keyword']").val());
if(keyword === ""){
$(".search-empty").show();
$("#searchList").html("");
return false;
}
$("#btnSearch").attr("disabled","disabled").find("i").removeClass("fa-search").addClass("loading");
window.keyword = keyword;
},
success :function (res) {
var html = "";
if(res.errcode === 0){
for(var i in res.data){
var item = res.data[i];
html += '<li><a href="javascript:;" title="'+ item.doc_name +'" data-id="'+ item.doc_id+'"> '+ item.doc_name +' </a></li>';
}
}
if(html !== ""){
$(".search-empty").hide();
}else{
$(".search-empty").show();
}
$("#searchList").html(html);
},
complete : function () {
$("#btnSearch").removeAttr("disabled").find("i").removeClass("loading").addClass("fa-search");
}
});
window.onpopstate = function (e) {
var $param = e.state;
if($param.hasOwnProperty("$url")) {
window.jsTree.jstree().deselect_all();
window.jsTree.jstree().select_node({ id : $param.$id });
$param.$init = false;
//events.trigger('article.open', $param );
}else{
console.log($param);
}
};
try {
var $node = window.jsTree.jstree().get_selected();
if (typeof $node === "object") {
$node = window.jsTree.jstree().get_node({id: $node[0]});
events.trigger('article.open', {$url: $node.a_attr.href, $init: true, $id: $node.a_attr.id});
}
}catch (e){
console.log(e);
}
});