-
Notifications
You must be signed in to change notification settings - Fork 7
/
LanEditor.js
executable file
·1421 lines (1409 loc) · 94.2 KB
/
LanEditor.js
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var LanEditor = {
//构造函数,初始化变量,注册按键监听
init: function(arguments) {
if("undefined" == typeof(arguments) || "undefined" == typeof(arguments.textelem) ){
return {
status: false,
message: "你至少需要指定LanEditor使用的textelem"
};
}
if("function" != typeof($)) {
return {
status: false,
message: "LanEditor使用$符访问jQuery,而在此页面没有检测到jQuery"
};
}
var arg = arguments;
//初始化语法高亮
hljs.initHighlightingOnLoad();
//初始化markdown渲染器
this.converter = new Showdown.converter();
//编辑框id
this.textelem = arg.textelem;
this.showelem = "undefined" == typeof(arg.showelem) ? null : arg.showelem;
//编辑框对象
this.TextElem = $("#" + this.textelem);
$("body").append(
"<div class=\"_Keyword\" id=\"_Keyword\"><ul id=\"_KeywordLi\"></ul></div>" +
'<div class="_LanEditorBg" id="_LanEditorBg">' +
'</div>' +
'<div class="_LEBorder" id="_LEBorder">' +
'<ul class="_LEMenuList">' +
'<li class="_MenuListSe">文件列表</li>' +
'<li class="_MenuListSet">选项设置</li>' +
'</ul>' +
'<div class="_LEContent">' +
'<div id="_LEFilelist">' +
'<p>' +
'<input type="text" id="_LENName">' +
'<span id="_LEFNB">新建</span>' +
'</p>' +
'<ul>' +
'</ul>' +
'</div>' +
'<div id="_LESetting">' +
'<ul>' +
'<li id="OpenSKL">' +
'<span class="_LESName">开启关键字提示</span>' +
'<span class="_LESCon"><span></span></span>' +
'</li>' +
'<li id="OpenSKLAni">' +
'<span class="_LESName">开启提示框动画</span>' +
'<span class="_LESCon"><span></span></span>' +
'</li>' +
'<li id="OpenMenuAni">' +
'<span class="_LESName">开启菜单动画</span>' +
'<span class="_LESCon"><span></span></span>' +
'</li>' +
'<li id="OpenAutoSymbol">' +
'<span class="_LESName">开启符号补全</span>' +
'<span class="_LESCon"><span></span></span>' +
'</li>' +
'</ul>' +
'</div>' +
'</div>' +
'</div>'
);
//初始化菜单对象
this.Menu.MenuObj = $("#_LEBorder");
//初始化背景对象
this.Background.BackObj = $("#_LanEditorBg");
this.SKLelem = $("#_Keyword");
this._timer = {};
//对editor元素样式进行默认设置
(function(TextElem) {
TextElem.css({
"box-sizing": "border-box",
"overflow": "auto",
"font-size": "16px",
"font-family": "Menlo, Monaco, Consolas, Courier New, monospace",
"font-weight": 1.3,
"outline": "none",
"background-color": "#23241f",
"color": "#f8f8f2"
});
})(this.TextElem);
//注册监听
var TextElem = document.getElementById(this.textelem);
if ("undefined" != typeof TextElem.addEventListener) {
window.addEventListener("keydown", function(event) {
var TextElem = LanEditor.TextElem;
LanEditor.Menu.Toggle.call(LanEditor.Menu, event);
LanEditor.KeyRewrite(event, TextElem);
for(var i = 0; i < LanEditor.Fn.FnSet.keydown.length; i++) {
LanEditor.Fn.FnSet.keydown[i](event, TextElem);
}
}, true);
TextElem.addEventListener("keyup", function(event) {
var TextElem = LanEditor.TextElem;
LanEditor.AutoCompleteSymbol(event, TextElem);
LanEditor.AutoCompleteKeyword(event, TextElem);
for(var i = 0; i < LanEditor.Fn.FnSet.keyup.length; i++) {
LanEditor.Fn.FnSet.keyup[i](event, TextElem);
}
// 自动保存文件
if(LanEditor.SetPara.PluginsMode == false){
LanEditor.File.SaveFile();
}
// 渲染md
LanEditor.DelayTillLast.call(LanEditor, "RenderHTML", LanEditor.RenderHTML, 300);
}, false);
document.getElementById("_LEFNB").addEventListener("click", function() {
LanEditor.File.NewFile($("#_LENName").val());
LanEditor.File.Refresh($("#_LEFilelist ul"));
}, false);
document.getElementById("_LENName").addEventListener("keyup", function(event) {
if(event.which == 13){
LanEditor.File.NewFile($("#_LENName").val());
$("#_LENName").val("");
LanEditor.File.Refresh($("#_LEFilelist ul"));
}
}, false);
} else if ("undefined" != typeof TextElem.attachEvent) {
window.attachEvent("keydown", function(event) {
var TextElem = LanEditor.TextElem;
LanEditor.Menu.Toggle.call(LanEditor.Menu, event);
LanEditor.KeyRewrite(event, TextElem);
for(var i = 0; i < LanEditor.Fn.FnSet.keydown.length; i++) {
LanEditor.Fn.FnSet.keydown[i](event, TextElem);
}
});
TextElem.attachEvent("keyup", function(event) {
var TextElem = LanEditor.TextElem;
LanEditor.AutoCompleteSymbol(event, TextElem);
LanEditor.AutoCompleteKeyword(event, TextElem);
for(var i = 0; i < LanEditor.Fn.FnSet.keyup.length; i++) {
LanEditor.Fn.FnSet.keyup[i](event, TextElem);
}
if(LanEditor.SetPara.PluginsMode == false){
LanEditor.File.SaveFile();
}
LanEditor.DelayTillLast.call(LanEditor, "RenderHTML", LanEditor.RenderHTML);
}, 300);
document.getElementById("_LEFNB").attachEvent("click", function() {
LanEditor.File.NewFile($("#_LENName").val());
LanEditor.File.Refresh($("#_LEFilelist ul"));
});
document.getElementById("_LENName").attachEvent("click", function(event) {
LanEditor.File.NewFile($("#_LENName").val());
$("#_LENName").val("");
LanEditor.File.Refresh($("#_LEFilelist ul"));
});
} else {
alert("此浏览器太老,建议用chrome浏览器");
}
//初始化提示框对象参数
this.SKLPara.Set(false, 0, 0);
TextElem.focus();
//---------------------------文件列表事件代理
$("#_LEFilelist ul").delegate("span", "click", function(e) {
// 打开文件
if ($(e.target).hasClass("_LEFName")) {
var filename = $(e.target).text();
var content = LanEditor.File.GetFileContent(filename);
var time;
for (varname in localStorage) {
if (varname.split("$")[0].length == 10 && varname.split("$")[1] == filename) {
time = varname.split("$")[0];
break;
}
}
LanEditor.TextElem.val(content);
LanEditor.File.CurOpenFile.name = filename;
LanEditor.File.CurOpenFile.content = content;
LanEditor.File.CurOpenFile.time = time;
LanEditor.RenderHTML();
// 导出markdown文件
} else if ($(e.target).hasClass("_LEFM")) {
var filename = $(e.target).prev().text();
var content = LanEditor.File.GetFileContent(filename);
LanEditor.File.ExportMD(content);
// 导出HTML文件
} else if ($(e.target).hasClass("_LEFH")) {
var filename = $(e.target).prev().prev().text();
var content = LanEditor.File.GetFileContent(filename);
LanEditor.File.ExportHTML(content);
// 删除文件
} else if ($(e.target).hasClass("_LEFD")) {
var filename = $(e.target).prev().prev().prev().text();
// 删除的是否是当前正在编辑的文件
if (filename == LanEditor.File.CurOpenFile.name) {
LanEditor.File.CurOpenFile.name = null;
LanEditor.File.CurOpenFile.content = null;
LanEditor.File.CurOpenFile.time = null;
}
LanEditor.File.DeleteFileFromLocal(filename);
LanEditor.File.Refresh($("#_LEFilelist ul"));
}
});
//----------------------------菜单选项卡事件
$("._MenuListSe").click(function(e) {
$(this).css("background-color", "#948266");
$("._MenuListSet").css("background-color", "#aa9b83");
$("#_LEFilelist").css("display", "block");
$("#_LESetting").css("display", "none");
});
$("._MenuListSet").click(function(e) {
$(this).css("background-color", "#948266");
$("._MenuListSe").css("background-color", "#aa9b83");
$("#_LEFilelist").css("display", "none");
$("#_LESetting").css("display", "block");
});
//-----------------------------选项设置事件代理
$("#_LESetting ul").delegate("li", "click", function(e) {
var IdName = $(this).attr("id");
switch (IdName) {
case "OpenSKL":
if (LanEditor.SetPara.OpenSKL == 1) {
LanEditor.SetPara.OpenSKL = 0;
$("._LESCon", $(this)).css("background-color", "#dcdada").children().css("left", "1px");
} else {
LanEditor.SetPara.OpenSKL = 1;
$("._LESCon", $(this)).css("background-color", "#4dc4f5").children().css("left", "21px");
}
break;
case "OpenSKLAni":
if (LanEditor.SetPara.OpenSKLAni == 1) {
LanEditor.SetPara.OpenSKLAni = 0;
$("._LESCon", $(this)).css("background-color", "#dcdada").children().css("left", "1px");
$("#_Keyword").css("transition", "0s");
} else {
LanEditor.SetPara.OpenSKLAni = 1;
$("._LESCon", $(this)).css("background-color", "#4dc4f5").children().css("left", "21px");
$("#_Keyword").css("transition", "0.3s");
}
break;
case "OpenMenuAni":
if (LanEditor.SetPara.OpenMenuAni == 1) {
LanEditor.SetPara.OpenMenuAni = 0;
$("._LESCon", $(this)).css("background-color", "#dcdada").children().css("left", "1px");
$("#_LEBorder").css("transition", "0s");
$("#_LanEditorBg").css("transition", "0s");
} else {
LanEditor.SetPara.OpenMenuAni = 1;
$("._LESCon", $(this)).css("background-color", "#4dc4f5").children().css("left", "21px");
$("#_LEBorder").css("transition", "1s");
$("#_LanEditorBg").css("transition", "0.8s");
}
break;
case "OpenAutoSymbol":
if (LanEditor.SetPara.OpenAutoSymbol == 1) {
LanEditor.SetPara.OpenAutoSymbol = 0;
$("._LESCon", $(this)).css("background-color", "#dcdada").children().css("left", "1px");
} else {
LanEditor.SetPara.OpenAutoSymbol = 1;
$("._LESCon", $(this)).css("background-color", "#4dc4f5").children().css("left", "21px");
}
break;
}
LanEditor.SetPara.Save();
});
//加载设置
this.SetPara.Load();
this.SetPara.PluginsMode = "undefined" == typeof(arg.PluginsMode) ? false : arg.PluginsMode;
this.SetPara.OpenSKL = "undefined" == typeof(arg.OpenSKL) ? this.SetPara.OpenSKL : arg.OpenSKL;
this.SetPara.OpenSKLAni = "undefined" == typeof(arg.OpenSKLAni) ? this.SetPara.OpenSKLAni : arg.OpenSKLAni;
this.SetPara.OpenAutoSymbol = "undefined" == typeof(arg.OpenAutoSymbol) ? this.SetPara.OpenAutoSymbol : arg.OpenAutoSymbol;
this.SetPara.OpenMenuAni = "undefined" == typeof(arg.OpenMenuAni) ? this.SetPara.OpenMenuAni : arg.OpenMenuAni;
this.SetPara.Apply();
return {
status: true
};
},
//延迟执行最后一次调用的函数
DelayTillLast: function(id, fn, wait) {
if (this._timer[id]) {
window.clearTimeout(this._timer[id]);
delete this._timer[id];
}
return this._timer[id] = window.setTimeout(function() {
fn.call(LanEditor);
delete LanEditor._timer[id];
}, wait);
},
//渲染HTML
RenderHTML: function() {
if(this.showelem == null) {
return false;
}
var html = this.converter.makeHtml(this.TextElem.val());
for(var i = 0; i < this.Fn.FnSet.render.length; i++) {
html = this.Fn.FnSet.render[i](html) || html;
}
$("#" + this.showelem).html(html);
$('pre code', $("#" + this.showelem)).each(function(i, block) {
hljs.highlightBlock(block);
});
return true;
},
//获取渲染后的HTML
GetRenderHTML: function() {
var html = this.converter.makeHtml(this.TextElem.val());
for(var i = 0; i < this.Fn.FnSet.render.length; i++) {
html = this.Fn.FnSet.render[i](html) || html;
}
var VirtualDIV = $("<div></div>");
VirtualDIV.html(html);
$("pre code", VirtualDIV).each(function(i, block) {
hljs.highlightBlock(block);
});
html = VirtualDIV.html();
return html;
},
//设置提示框对象参数
SKLPara: {
//是否显示
show: null,
//候选列表总数
count: null,
//当前选中的序号,以0开始
cs: null,
//匹配关键字的长度,如果为0,则表示不需要匹配
SWL: 0,
//匹配到的结果集
ResultSet: [],
Set: function(show, count, cs) {
this.show = show;
this.count = count;
this.cs = cs;
$("._SKLi" + cs).addClass("_limatch");
},
//获取提示框对象参数
Get: function() {
return LanEditor.SKLPara;
},
SetShow: function(show) {
this.show = show;
},
SetCount: function(count) {
this.count = count;
},
SetCS: function(cs, up) {
$("._SKLi" + this.cs).removeClass("_limatch");
this.cs = cs;
$("._SKLi" + cs).addClass("_limatch");
var SKLheight = parseInt($("#_Keyword").css("height"));
var SKLScrollHeight = $("#_Keyword")[0].scrollHeight;
var SKLScrollTop = $("#_Keyword").scrollTop();
if (cs == 0) {
$("#_Keyword").scrollTop(0);
} else if (cs == this.count - 1) {
$("#_Keyword").scrollTop(SKLScrollHeight);
} else if (up == true) {
if (cs * 18 < SKLScrollTop) {
var diff = SKLScrollTop - cs * 18;
$("#_Keyword").scrollTop(SKLScrollTop - diff);
}
} else if ("undefined" == typeof up) {
if ((cs + 1) * 18 > SKLScrollTop + SKLheight) {
var diff = (cs + 1) * 18 - (SKLScrollTop + SKLheight);
$("#_Keyword").scrollTop(SKLScrollTop + diff);
}
}
}
},
//关键字自动补全,keyup阶段执行
AutoCompleteKeyword: function(event, TextElem) {
if (LanEditor.SetPara.OpenSKL == 0) {
return;
}
var e = event;
var SKLelem = $("#_Keyword");
if (e.which == 38 || e.which == 40) {
return;
}
if (e.which!=8 && e.which!=189 &&(e.which < 65 && e.which > 57 || e.which > 90 || e.which < 48) ) {
SKLelem.css({
"opacity": 0,
"z-index": -20
});
LanEditor.SKLPara.SetShow(false);
return;
}
//要匹配的单词
var word = "";
//查找光标前面的单词
var i = 1;
var pchar = TextElem.iGetPosStr(-i).charAt(0);
while (pchar != "" && pchar != "(" && pchar != ")" && pchar != ";") {
if (i > TextElem.val().length) {
break;
}
if (pchar >= "a" && pchar <= "z" || pchar >= "A" && pchar <= "Z" || pchar == "_" || pchar == "-") {
i++;
} else {
break;
}
pchar = TextElem.iGetPosStr(-i).charAt(0);
}
if (i > 1) {
word = TextElem.iGetPosStr(-(i - 1));
LanEditor.ShowKeywordList(word);
} else {
SKLelem.css({
"opacity": 0,
"z-index": -20
});
LanEditor.SKLPara.SetShow(false);
}
},
//显示关键字提示列表
ShowKeywordList: function(word) {
var TextElem = this.TextElem;
var SKLelem = this.SKLelem;
var cursorpos = CursorPos.GetCursorPos(document.getElementById(this.textelem));
var scrolltop = TextElem.scrollTop();
var left = cursorpos.left + 2;
var top = cursorpos.top - scrolltop + 18;
//查找匹配的单词结果
var resultset = this.SearchKeyword(word);
//查找单词为空,不显示提示列表
if (resultset.length <= 0) {
SKLelem.css({
"opacity": 0,
"z-index": -20
});
LanEditor.SKLPara.SetShow(false);
return;
}
//拼接HTML代码
var KeyCount = 0;
var html = "";
for (key in resultset) {
//过滤键名
if (!isNaN(key)) {
continue;
}
for (var i = 0; i < resultset[key].length; ++i) {
html += "<li class=\"_SKLi" + KeyCount + "\">" + resultset[key][i] + "</li>";
KeyCount++;
}
}
$("#_KeywordLi").html(html);
LanEditor.SKLPara.Set(true, KeyCount, 0);
var SKLheight = (KeyCount > 10 ? 180 : KeyCount * 18);
var SKLwidth = parseInt(SKLelem.css("width"));
//editor元素距body顶部的高度
var TextElemTop = CursorPos._offset(document.getElementById(LanEditor.textelem)).top;
var TextElemLeft = CursorPos._offset(document.getElementById(LanEditor.textelem)).left;
var TextElemHeight = parseInt(TextElem.css("height"));
var TextElemWidth = parseInt(TextElem.css("width"));
// 判断提示框是否超出下边界,是则调整在光标的上边
if (top + SKLheight > TextElemTop + TextElemHeight) {
top = top - SKLheight - 18;
}
// 判断提示框是否超出右边界,是则调整在光标的左边
if (left + SKLwidth > TextElemLeft + TextElemWidth) {
left = left - SKLwidth;
}
// console.log("height -> " + height + " SKLheight -> " + SKLheight);
// 显示提示框
SKLelem.css({
"opacity": 1,
"z-index": 20,
"left": left,
"top": top,
"height": SKLheight
});
// console.log(" cursorpos -> "+ cursorpos);
// console.log(" top -> " + top + " scrolltop -> " + scrolltop + " cursortop -> " + cursorpos.top);
},
SearchKeyword: function(word) {
var flag = false;
var resultset = new Array();
// 全局的resultset,并且不含正则替换后的值
this.SKLPara.ResultSet = new Array();
this.SKLPara.SWL = word.length;
var KeywordSet = this.Keyword.Set;
for (key in KeywordSet) {
resultset.push(key);
resultset[key] = new Array();
for (var i = 0; i < KeywordSet[key].length; ++i) {
// console.log("in " + Keyword[key][i] + " search -> " + word + " search reg ->" + "/" + word + "/i search flag -> " + Keyword[key][i].search("/" + word + "/i"));
var reg = new RegExp("(" + word + ")", "gi");
if (KeywordSet[key][i].search(reg) > -1) {
this.SKLPara.ResultSet.push(KeywordSet[key][i]);
resultset[key].push(KeywordSet[key][i].replace(reg, "<span class=\"_KeyHL\">$1</span>"));
flag = true;
}
}
}
return flag ? resultset : [];
},
//符号自动补全,keyup阶段执行
AutoCompleteSymbol: function(event, TextElem) {
if (LanEditor.SetPara.OpenAutoSymbol == 0) {
return;
}
var e = event;
//按回车缩进和上一行相同的间距
if (e.which == 13) {
if (LanEditor.SKLPara.show == true) {
return;
}
// 获取上一行的缩进个数
var space = TextElem.iGetSpaceNum();
//如果是成对{}按回车键,则多加4个空格缩进
var isfunc = false;
var pre = TextElem.iGetPosStr(-2);
if (pre == "{\n") {
space += 4;
isfunc = true;
}
for (var i = 0; i < space; ++i) {
TextElem.iAddField(" ");
}
var pos = TextElem.iGetFieldPos();
var insertStr = "";
if (isfunc) {
TextElem.iAddField("\n");
for (var i = 0; i < space - 4; ++i) {
insertStr += " ";
}
}
TextElem.iAddField(insertStr);
TextElem.iSelectField(pos);
} else if (e.shiftKey && e.which == 57) { // ( 左括号自动补全
if(TextElem.iGetPosStr(-1) == "("){
TextElem.iAddField(")");
}else{
TextElem.iAddField(")");
}
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
} else if (e.shiftKey && e.which == 48) { // ) 右括号判断是否成对匹配
var pre = TextElem.iGetPosStr(-2);
var next = TextElem.iGetPosStr(1);
if (pre == "()" && next == ")" || pre == "()" && next == ")") {
TextElem.iDelField(1);
TextElem.iSelectField(TextElem.iGetFieldPos() + 1);
}
} else if (e.shiftKey && e.which == 219) { // { 左大括号自动补全
TextElem.iAddField("}");
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
} else if (e.shiftKey && e.which == 221) { // } 右大括号判断是否成对匹配
var pre = TextElem.iGetPosStr(-2);
var next = TextElem.iGetPosStr(1);
if (pre == "{}" && next == "}") {
TextElem.iDelField(-1);
}
} else if (e.which == 219) { // [ 左中括号自动补全
if (TextElem.iGetPosStr(-1) == "[") {
TextElem.iAddField("]");
} else {
TextElem.iAddField("】");
}
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
} else if (e.which == 221) { // ] 右中括号判断是否成对匹配
var pre = TextElem.iGetPosStr(-2);
var next = TextElem.iGetPosStr(1);
if ((pre == "[]" || pre == "【】") && (next == "]" || next == "】")) {
TextElem.iDelField(-1);
}
} else if (e.shiftKey && e.which == 188) { // < 左尖括号自动补全
if (TextElem.iGetPosStr(-1) == "<") {
TextElem.iAddField(">");
} else {
TextElem.iAddField("》");
}
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
} else if (e.shiftKey && e.which == 190) { // > 右尖括号判断是否成对匹配
var pre = TextElem.iGetPosStr(-2);
var next = TextElem.iGetPosStr(1);
if ((pre == "<>" || pre == "《》") && (next == ">" || next == "》")) {
TextElem.iDelField(-1);
}
} else if (!e.shiftKey && e.which == 222) { // ' 左单引号自动补全
if (TextElem.iGetPosStr(-1) == "'") {
TextElem.iAddField("'");
} else {
TextElem.iAddField('’');
}
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
} else if (e.shiftKey && e.which == 222) { // “ 左双引号自动补全
if (TextElem.iGetPosStr(-1) == '"') {
TextElem.iAddField('"');
} else {
TextElem.iAddField('”');
}
TextElem.iSelectField(TextElem.iGetFieldPos() - 1);
}
},
//重写按键功能,在keydown阶段执行
KeyRewrite: function(event, TextElem) {
var e = event;
//阻止Tab事件,换成4个空格
if (e.which == 9) {
e.preventDefault();
TextElem.iAddField(" ");
//退格键删除多个空格
} else if (e.which == 8) {
//只有光标前一个字符是空格的时候才要判断是否删除多个空格
if (TextElem.iGetPosStr(-1) == " ") {
e.preventDefault();
//查找前面有多少个空格
var i = 1;
while (TextElem.iGetPosStr(-i).charAt(0) == " " && TextElem.iGetPosStr(-i).charAt(0) != "\n" && TextElem.iGetPosStr(-i).charAt(0) != "\r\n" && TextElem.iGetPosStr(-i).charAt(0) != "") {
i++;
if (i > TextElem.val().length) {
break;
}
}
if (i - 1 < 4) {
TextElem.iDelField(1);
} else if ((i - 1) % 4 == 0) {
for (var i = 0; i < 4; ++i) {
TextElem.iDelField(1);
}
} else {
var del = (i - 1) % 4;
for (var i = 0; i < del; ++i) {
TextElem.iDelField(1);
}
}
}
//显示提示框时上下键选择列表项
} else if (e.which == 38) {
if (LanEditor.SKLPara.show == true) {
e.preventDefault();
if (LanEditor.SKLPara.cs == 0) {
LanEditor.SKLPara.SetCS(LanEditor.SKLPara.count - 1);
} else if (LanEditor.SKLPara.cs > 0) {
LanEditor.SKLPara.SetCS(LanEditor.SKLPara.cs - 1, true);
}
}
//重写下键
} else if (e.which == 40) {
if (LanEditor.SKLPara.show == true) {
e.preventDefault();
if (LanEditor.SKLPara.cs == LanEditor.SKLPara.count - 1) {
LanEditor.SKLPara.SetCS(0);
} else if (LanEditor.SKLPara.cs < LanEditor.SKLPara.count - 1) {
LanEditor.SKLPara.SetCS(LanEditor.SKLPara.cs + 1);
}
}
//重写enter键
} else if (e.which == 13) {
if (LanEditor.SKLPara.show == true) {
e.preventDefault();
TextElem.iDelField(LanEditor.SKLPara.SWL);
TextElem.iAddField(LanEditor.SKLPara.ResultSet[LanEditor.SKLPara.cs]);
}
}
// console.log("keyCode -> " + e.which);
},
//------------------------------------------------------文件对象
File: {
CurOpenFile: {
name: null,
time: null,
content: null
},
// 保存为md文件
ExportMD: function(md) {
var fileName = prompt("保存为Markdown文件,请输入文件名", "新建Markdown文件");
if (fileName == null || fileName == "") {
return;
}
for(var i = 0; i < LanEditor.Fn.FnSet.expertmd.length; i++) {
md = LanEditor.Fn.FnSet.expertmd[i](md) || md;
}
this.ExportFile(fileName + ".md", md);
},
// 保存为HTML文件
ExportHTML: function(md) {
var fileName = prompt("保存为HTML文件,请输入文件名", "新建HTML文件");
if (fileName == null || fileName == "") {
return;
}
var HTML_temp_content = '<!DOCTYPE html>' +
'<html lang="zh-CN">' +
'<head><meta charset="UTF-8">' +
'<style type="text/css">' +
'h1,h2,h3,h4,h5,h6,p,blockquote{margin:0;padding:0}' +
'body{font-family:"Helvetica Neue",Helvetica,"Hiragino Sans GB",Arial,sans-serif;font-size:13px;line-height:18px;color:#737373;margin:10px 13px 10px 13px}' +
'a{color:#0069d6}a:hover{color:#0050a3;text-decoration:none}' +
'a img{border:0}p{margin-bottom:9px}h1,h2,h3,h4,h5,h6{color:#404040;line-height:36px}h1{margin-bottom:18px;font-size:30px}h2{font-size:24px}h3{font-size:18px}' +
'h4{font-size:16px}h5{font-size:14px}h6{font-size:13px}hr{margin:0 0 19px;border:0;border-bottom:1px solid #ccc}' +
'blockquote{padding:13px 13px 21px 15px;margin-bottom:18px;font-family:georgia,serif;font-style:italic}' +
'blockquote:before{content:"C";font-size:40px;margin-left:-10px;font-family:georgia,serif;color:#eee}' +
'blockquote p{font-size:14px;font-weight:300;line-height:18px;margin-bottom:0;font-style:italic}code,pre{font-family:Monaco,Andale Mono,Courier New,monospace}' +
'code{background-color:#fee9cc;color:rgba(0,0,0,0.75);padding:1px 3px;font-size:12px;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px}' +
'pre{display:block;padding:14px;margin:0 0 18px;line-height:16px;font-size:11px;border:1px solid #d9d9d9;white-space:pre-wrap;word-wrap:break-word}' +
'pre code{background-color:#fff;color:#737373;font-size:11px;padding:0}@media screen and (min-width:768px){body{width:748px;margin:10px auto}}</style>';
HTML_temp_content += '<title>' + fileName + '</title>';
var html = LanEditor.converter.makeHtml(md);
for(var i = 0; i < LanEditor.Fn.FnSet.render.length; i++) {
html = LanEditor.Fn.FnSet.render[i](html) || html;
}
HTML_temp_content += '</head><body>' + html + '</body></html>';
this.ExportFile(fileName + ".html", HTML_temp_content);
},
//创建文件下载
ExportFile: function(fileName, content) {
var aLink = document.createElement('a');
var blob = new Blob([content]);
var evt = document.createEvent("HTMLEvents");
//initEvent 不加后两个参数在FF下会报错, 感谢 Barret Lee 的反馈
evt.initEvent("click", false, false);
aLink.download = fileName;
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
},
//存储文件到localstorage
SaveFileToLocal: function(fileName, content) {
if ("undefined" == localStorage) {
return "LocalStorage not support";
}
if (fileName == null || content == null) {
return "param wrong";
}
var filename = LanEditor.Time.GetTimestamp() + "$" + fileName;
for (varname in localStorage) {
if (varname.split("$")[0].length == 10 && varname.split("$")[1] == fileName) {
filename = varname.split("$")[0] + "$" + fileName;
}
}
localStorage.setItem(filename, content);
return "OK";
},
// 从localstorage删除文件
DeleteFileFromLocal: function(fileName) {
for (varname in localStorage) {
if (varname.split("$")[0].length == 10 && varname.split("$")[1] == fileName) {
localStorage.removeItem(varname);
return true;
}
}
return false;
},
// 获取文件列表
GetFileList: function() {
var filelist = new Array();
var temp;
for (varname in localStorage) {
temp = varname.split("$")[0];
// 判断当前变量是否是文件名
if (temp.length == 10 && !isNaN(temp)) {
filelist.push({
name: varname.split("$")[1],
time: temp
});
} else {
continue;
}
}
return filelist;
},
// 获取文件
GetFileContent: function(fileName) {
for (varname in localStorage) {
if (varname.split("$")[0].length == 10 && varname.split("$")[1] == fileName) {
return localStorage.getItem(varname);
}
}
return false;
},
// 创建新文件
NewFile: function(filename) {
if (filename == "" || filename == null) {
alert("无效的文件名");
return false;
}
var result = this.SaveFileToLocal(filename, "");
if (result == "OK") {
return true;
} else if (result == "LocalStorage not support") {
alert("您的浏览器不支持永久存储,请使用chrome以获得最佳体验");
return false;
} else if (result == "param wrong") {
alert("无效的文件名,请尝试更换文件名");
return false;
}
},
// 保存当前文件
SaveFile: function() {
var md = LanEditor.TextElem.val();
if(md == ""){
return;
}
// 当前文件还没命名,提示输入文件名
if (LanEditor.File.CurOpenFile.name == null) {
var flag = confirm("当前文件未保存,是否保存?");
if (flag) {
var filename = prompt("请输入文件名", "新建MD文件");
if (filename == null || filename == "") {
return;
}
LanEditor.File.CurOpenFile.name = filename;
LanEditor.File.CurOpenFile.time = LanEditor.Time.GetTimestamp();
LanEditor.File.CurOpenFile.content = md;
LanEditor.File.SaveFileToLocal(filename, md);
}
return;
}
LanEditor.File.CurOpenFile.content = md;
LanEditor.File.SaveFileToLocal(LanEditor.File.CurOpenFile.name, md);
},
// 显示文件列表
ShowFileList: function(ShowObj) {
var filelist = this.GetFileList();
var html = "";
for (var i = 0; i < filelist.length; ++i) {
html += '<li>';
html += '<span class="_LEFName" title="' + filelist[i].name + '">' + filelist[i].name + '</span>';
html += '<span class="_LEFM" title="导出Markdown文件到本地">MD</span>';
html += '<span class="_LEFH" title="导出HTML文件到本地">HTML</span>';
html += '<span class="_LEFD" title="删除文件">删除</span>';
html += '<span class="_LEFT" title="创建日期: ' + LanEditor.Time.GetTimeString(filelist[i].time) + '">' + LanEditor.Time.GetTimeString(filelist[i].time).substr(5, 5) + '</span>';
html += '</li>';
}
ShowObj.html(html);
},
// 刷新文件列表
Refresh: function(ShowObj) {
this.ShowFileList(ShowObj);
}
},
//菜单对象
Menu: {
IsShow: false,
MenuObj: null,
//切换显示状态,@call(LanEditor.Menu, Event)
Toggle: function(e) {
if(LanEditor.SetPara.PluginsMode == true){
return ;
}
//重写ESC键
if (e.which == 27) {
e.preventDefault();
if (this.IsShow) {
// console.log("menu -> hide");
LanEditor.Background.Show(false);
this.Show(false);
} else {
// console.log("menu -> show");
LanEditor.Background.Show(true);
this.Show(true);
LanEditor.File.ShowFileList($("#_LEFilelist ul"));
}
}
},
//设置是否显示菜单,true显示,false不显示
Show: function(IsShow) {
this.MenuObj.queue([]);
if (IsShow) {
this.MenuObj.queue(function() {
LanEditor.Menu.IsShow = true;
$(this).css("display", "block");
$(this).delay(1).dequeue();
});
this.MenuObj.queue(function() {
$(this).css("height", "320px");
$(this).dequeue();
});
} else {
if (LanEditor.SetPara.OpenMenuAni) {
this.MenuObj.queue(function() {
LanEditor.Menu.IsShow = false;
$(this).css("height", "30px");
$(this).delay(900).dequeue();
});
this.MenuObj.queue(function() {
$(this).css("display", "none");
$(this).dequeue();
});
} else {
this.MenuObj.queue(function() {
LanEditor.Menu.IsShow = false;
$(this).css({
"height": "30px",
"display": "none"
});
$(this).delay(900).dequeue();
});
}
}
}
},
//背景遮罩层对象
Background: {
IsShow: false,
BackObj: null,
//切换显示状态
Toggle: function() {
this.BackObj.queue([]);
if (this.IsShow) {
this.BackObj.queue(function() {
LanEditor.Background.IsShow = false;
$(this).css("opacity", 0);
$(this).delay(800).dequeue();
});
this.BackObj.queue(function() {
$(this).css("display", "none");
$(this).dequeue();
});
} else {
this.BackObj.queue(function() {
LanEditor.Background.IsShow = true;
$(this).css("display", "block");
$(this).delay(1).dequeue();
});
this.BackObj.queue(function() {
$(this).css("opacity", 0.8);
$(this).dequeue();
});
}
},
//是否显示背景遮罩层 false不显示,true显示
Show: function(IsShow) {
if (IsShow) {
this.IsShow = false;
} else {
this.IsShow = true;
}
this.Toggle();
}
},
//时间对象
Time: {
GetTimestamp: function() {
return Math.round(new Date().getTime() / 1000);
},
GetTimeString: function(timestamp) {
var timestamp = new Date(timestamp * 1000);
return timestamp.toLocaleString();
}
},
//设置系统参数
SetPara: {
//提示框
OpenSKL: null,
//提示框动画
OpenSKLAni: null,
//菜单动画
OpenMenuAni: null,
//符号自动匹配
OpenAutoSymbol: null,
//插件模式,只能在初始化时由参数指定
PluginsMode: false,
//加载设置
Load: function() {
this.OpenSKL = parseInt(localStorage.OpenSKL || 1);
this.OpenSKLAni = parseInt(localStorage.OpenSKLAni || 1);
this.OpenMenuAni = parseInt(localStorage.OpenMenuAni || 1);
this.OpenAutoSymbol = parseInt(localStorage.OpenAutoSymbol || 1);
},
//保存设置
Save: function() {
localStorage.OpenSKL = this.OpenSKL;
localStorage.OpenSKLAni = this.OpenSKLAni;
localStorage.OpenMenuAni = this.OpenMenuAni;
localStorage.OpenAutoSymbol = this.OpenAutoSymbol;
},
//立马应用设置
Apply: function() {
$("#OpenSKL ._LESCon", $("#_LESetting")).css(function(para) {
if (para == 1) {
$("#OpenSKL ._LESCon", $("#_LESetting")).children().css("left", "21px");
return {
"background-color": "#4dc4f5"
};
}
return {
"background-color": "#dcdada"
};
}(this.OpenSKL));
$("#OpenSKLAni ._LESCon", $("#_LESetting")).css(function(para) {
if (para == 1) {
$("#OpenSKLAni ._LESCon", $("#_LESetting")).children().css("left", "21px");
return {
"background-color": "#4dc4f5"
};
}
return {
"background-color": "#dcdada"
};
}(this.OpenSKLAni));
$("#OpenMenuAni ._LESCon", $("#_LESetting")).css(function(para) {
if (para == 1) {
$("#OpenMenuAni ._LESCon", $("#_LESetting")).children().css("left", "21px");
return {
"background-color": "#4dc4f5"
};
}
$("#_LEBorder").css("transition", "0s");
return {
"background-color": "#dcdada"