-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmd_editor_core.js
770 lines (699 loc) · 28.3 KB
/
md_editor_core.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
var modified = false;
var objApp = window.external;
var wizEditor;
var docTitle = "";
$(function() {
var objDatabase = null;
var objDocument = null;
var objCommon = getObjCommon();
var plainPasteMode = false; // 纯文本粘贴模式
var filesDirName = "index_files/"; // 本地文件目录名,不可更改
var filesFullPath = getLocalFilesPath();
var pluginFullPath = getPluginPath();
var optionSettings = getOptionSettings();
var code = loadDocument();
//分清是不是快捷键保存,解决为知4.5版本自动保存问题
var wizVerisonGreaterThan45 = null;
var wantSaveKey = false;
var wantSaveTime = null;
try {
wizVerisonGreaterThan45 = objApp.Window.CurrentDocumentBrowserObject != null;
}
catch (err) {
}
setEmojiFilePath();
////////////////////////////////////////////////
// 配置编辑器功能
wizEditor = editormd("test-editormd", {
theme : optionSettings.EditToolbarTheme, // 工具栏区域主题样式,见editormd.themes定义,夜间模式dark
editorTheme : optionSettings.EditEditorTheme, // 编辑器区域主题样式,见editormd.editorThemes定义,夜间模式pastel-on-dark
previewTheme : optionSettings.EditPreviewTheme, // 预览区区域主题样式,见editormd.previewThemes定义,夜间模式dark
value : code,
path : pluginFullPath + "Editor.md/lib/",
htmlDecode : "style,script,iframe", // 开启HTML标签解析,为了安全性,默认不开启
codeFold : true, // 代码折叠,默认关闭
tex : true, // 开启科学公式TeX语言支持,默认关闭
flowChart : true, // 开启流程图支持,默认关闭
sequenceDiagram : true, // 开启时序/序列图支持,默认关闭
toc : true, // [TOC]自动生成目录,默认开启
tocm : false, // [TOCM]自动生成下拉菜单的目录,默认关闭
tocTitle : "", // 下拉菜单的目录的标题
tocDropdown : false, // [TOC]自动生成下拉菜单的目录,默认关闭
emoji : optionSettings.EmojiSupport == "1" ? true : false, // Emoji表情,默认关闭
taskList : true, // Task lists,默认关闭
disabledKeyMaps : [
"F9", "F10", "F11" // 禁用切换全屏状态,因为为知已经支持
],
keymapMode : optionSettings.KeymapMode, // 键盘映射模式
toolbarIcons : function() {
return getEditToolbarButton(optionSettings.EditToolbarButton);
},
toolbarIconsClass : {
saveIcon : "fa-floppy-o", // 指定一个FontAawsome的图标类
captureIcon : "fa-scissors",
plainPasteIcon : "fa-clipboard",
optionsIcon : "fa-gear",
outlineIcon : "fa-list",
counterIcon : "fa-th-large",
},
toolbarHandlers : {
saveIcon : function() {
saveDocument();
},
captureIcon : function() {
captureScreenImage();
},
plainPasteIcon : function() {
plainPasteMode = !plainPasteMode;
showPlainPasteMode();
},
optionsIcon : function() {
this.executePlugin("optionsDialog", "options-dialog/options-dialog");
},
outlineIcon : function() {
this.executePlugin("outlineDialog", "outline-dialog/outline-dialog");
},
counterIcon : function() {
this.executePlugin("counterDialog", "counter-dialog/counter-dialog");
},
},
lang : {
description : "为知笔记Markdown编辑器,基于 Editor.md 构建。",
toolbar : {
saveIcon : "保存 (Ctrl+S)",
captureIcon : "截取屏幕",
plainPasteIcon : "纯文本粘贴模式",
optionsIcon : "选项",
outlineIcon : "内容目录",
counterIcon : "文章信息",
}
},
onload : function() {
var keyMap = {
"Ctrl-F9": function(cm) {
$.proxy(wizEditor.toolbarHandlers["watch"], wizEditor)();
},
"Ctrl-F10": function(cm) {
$.proxy(wizEditor.toolbarHandlers["preview"], wizEditor)();
},
"F1": function(cm) {
wizEditor.cm.execCommand("defaultTab");
},
"Ctrl-Alt-F": function(cm) {
wizEditor.cm.execCommand("find");
},
"Ctrl": function(cm) {
// 可能按了保存快捷键,记录
wantSaveKey = true;
wantSaveTime = new Date();
}
};
this.addKeyMap(keyMap);
showPlainPasteMode();
// 监听文本变化事件
this.cm.on("change", function(_cm, changeObj) {
modified = true;
});
// 监听粘贴事件
this.cm.on("paste", function (_cm, e) {
var clipboardData = event.clipboardData || window.clipboardData;
if (clipboardData) {
if (clipboardData.types == "Files") {
clipboardToImage();
}
else if ($.inArray("text/html", clipboardData.types) != -1) {
if (!plainPasteMode && clipboardHTMLToMd(clipboardData.getData("text/html"))) {
e.preventDefault();
}
}
else {
//类型为"text/plain",快捷键Ctrl+Shift+V
}
}
});
// 绑定Ctrl-S快捷键和Vim的w命令保存
CodeMirror.commands.save = saveDocument;
var isWebPage = false;
if (isWebPage)
{
$.get('Editor.md/examples/test.md', function(md){
wizEditor.setMarkdown(md);
wizEditor.save();
});
}
},
onimageUploadButton : function() {
var filename = objCommon.SelectWindowsFile(true, "Image Files(*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp|");
return getSavedLocalImage(filename);
},
onloadLocalFile : function(filename, fun) {
fun(objCommon.LoadTextFromFile(filename));
},
onloadLocalJsonFile : function(filename, fun) {
fun($.parseJSON(objCommon.LoadTextFromFile(filename)));
},
onsaveOptions : function(optionsValue) {
setOptionSettings(optionsValue);
},
ongetOptions : function() {
return optionSettings;
},
ongetObjDocument : function() {
return objDocument;
},
ongetObjCommon : function() {
return objCommon;
},
onclickHyperlink : function(hrefValue) {
return openOtherDocument(hrefValue) && openHrefInBrowser(hrefValue);
}
});
////////////////////////////////////////////////
// 获得配置值
function getConfigValue(key, defaultValue) {
var value = null;
if (objCommon == null) {
value = localStorage.getItem(key);
}
else {
value = objCommon.GetValueFromIni(pluginFullPath + "plugin.ini", "PluginConfig", key);
}
if (value == null || value == "") {
value = defaultValue;
}
return value;
};
////////////////////////////////////////////////
// 设置配置值
function setConfigValue(key, value) {
if (objCommon == null) {
localStorage.setItem(key, value);
}
else {
objCommon.SetValueToIni(pluginFullPath + "plugin.ini", "PluginConfig", key, value);
}
};
////////////////////////////////////////////////
// 获得选项配置值
function getOptionSettings() {
var optionsValue = {
// MarkdownStyle : getConfigValue("MarkdownStyle", "WizDefault"),
// ReadTheme : getConfigValue("ReadTheme", "default"),
EditToolbarButton : getConfigValue("EditToolbarButton", "default"),
EditToolbarTheme : getConfigValue("EditToolbarTheme", "default"),
EditEditorTheme : getConfigValue("EditEditorTheme", "default"),
EditPreviewTheme : getConfigValue("EditPreviewTheme", "default"),
EmojiSupport : getConfigValue("EmojiSupport", "1"),
HrefInBrowser : getConfigValue("HrefInBrowser", "0"),
KeymapMode : getConfigValue("KeymapMode", "default"),
};
return optionsValue;
};
////////////////////////////////////////////////
// 设置选项配置值
function setOptionSettings(optionsValue) {
if (optionSettings.EditToolbarButton != optionsValue.EditToolbarButton) {
setConfigValue("EditToolbarButton", optionsValue.EditToolbarButton);
var doc = wizEditor.getValue();
wizEditor.config("toolbarIcons", getEditToolbarButton(optionsValue.EditToolbarButton));
wizEditor.setValue(doc);
}
if (optionSettings.EditToolbarTheme != optionsValue.EditToolbarTheme) {
setConfigValue("EditToolbarTheme", optionsValue.EditToolbarTheme);
wizEditor.setTheme(optionsValue.EditToolbarTheme);
}
if (optionSettings.EditEditorTheme != optionsValue.EditEditorTheme) {
setConfigValue("EditEditorTheme", optionsValue.EditEditorTheme);
wizEditor.setEditorTheme(optionsValue.EditEditorTheme);
}
if (optionSettings.EditPreviewTheme != optionsValue.EditPreviewTheme) {
setConfigValue("EditPreviewTheme", optionsValue.EditPreviewTheme);
wizEditor.setPreviewTheme(optionsValue.EditPreviewTheme);
}
var showMsg = false;
// if (optionSettings.MarkdownStyle != optionsValue.MarkdownStyle) {
// setConfigValue("MarkdownStyle", optionsValue.MarkdownStyle);
// showMsg = true;
// setHookRead(optionsValue.MarkdownStyle == "Editor_md");
// }
// if (optionSettings.ReadTheme != optionsValue.ReadTheme) {
// setConfigValue("ReadTheme", optionsValue.ReadTheme);
// }
if (optionSettings.EmojiSupport != optionsValue.EmojiSupport) {
setConfigValue("EmojiSupport", optionsValue.EmojiSupport);
var doc = wizEditor.getValue();
wizEditor.config("emoji", optionsValue.EmojiSupport == "1" ? true : false);
wizEditor.setValue(doc);
}
if (optionSettings.KeymapMode != optionsValue.KeymapMode) {
setConfigValue("KeymapMode", optionsValue.KeymapMode);
wizEditor.setKeymapMode(optionsValue.KeymapMode);
}
if (optionSettings.HrefInBrowser != optionsValue.HrefInBrowser) {
setConfigValue("HrefInBrowser", optionsValue.HrefInBrowser);
}
optionSettings = optionsValue;
if (objCommon != null && showMsg) {
objApp.Window.ShowMessage("设置新选项后,您应该重新运行{p}。", "{p}", 0x00000040);
}
};
////////////////////////////////////////////////
// 设置拦截原本的Markdown渲染
function setHookRead(isHook) {
var appPath = objApp.AppPath;
var hookPath = appPath + "WizTools/htmleditor/utils.js";
if (!objCommon.PathFileExists(hookPath)) {
return;
};
var hookText = objCommon.LoadTextFromFile(hookPath);
if (!hookText) {
return;
}
if (hookText.indexOf("WizIsMarkdownByTitle") == -1) {
return;
}
var findText = "function WizIsMarkdown(doc) {";
var findIndex = hookText.indexOf(findText);
if (findIndex == -1) {
return;
}
// 备份
var bakPath = hookPath + ".bak";
if (!objCommon.PathFileExists(bakPath)) {
objCommon.SaveTextToFile(bakPath, hookText, "utf-8-bom");
};
var saveText = null;
var addText = wizVerisonGreaterThan45 ? "return false;" : "return WizIsMarkdownByTitle(doc);";
var addIndex = findIndex + findText.length;
var alreadyText = hookText.substring(addIndex, addIndex + addText.length);
if (alreadyText == addText) {
if (!isHook) {
saveText = hookText.substring(0, addIndex) + hookText.substring(addIndex + addText.length);
}
}
else {
if (isHook) {
saveText = hookText.substring(0, addIndex) + addText + hookText.substring(addIndex);
}
}
if (saveText != null) {
objCommon.SaveTextToFile(hookPath, saveText, "utf-8-bom");
objApp.Window.ShowMessage("Markdown渲染模式已被修改,如出现问题可用\n" + bakPath + "\n文件进行还原。", "{p}", 0x00000040);
}
};
////////////////////////////////////////////////
// 获得工具栏按钮
function getEditToolbarButton(style) {
if (style == "lite") {
return [
"saveIcon", "|",
"bold", "italic", "|",
"link", "quote", "code", "image", "|",
"list-ol", "list-ul", "h1", "hr", "|",
"undo", "redo", "||",
"outlineIcon", "counterIcon", "optionsIcon", "help", "info"
];
} else{
return [
"saveIcon", "|",
"undo", "redo", "|",
"bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
"h1", "h2", "h3", "|",
"list-ul", "list-ol", "hr", "|",
"plainPasteIcon", "link", "reference-link", "image", "captureIcon", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|",
"goto-line", "watch", "preview", "clear", "search", "||",
"outlineIcon", "counterIcon", "optionsIcon", "help", "info"
];
};
};
////////////////////////////////////////////////
// 获得为知常用操作对象
function getObjCommon() {
if (objCommon == null) {
try {
objCommon = objApp.CreateWizObject("WizKMControls.WizCommonUI");
}
catch (err) {
}
}
return objCommon;
};
////////////////////////////////////////////////
// 获得插件路径
function getPluginPath () {
var pluginPath = "";
try {
pluginPath = objApp.GetPluginPathByScriptFileName("md_editor.js");
}
catch (err) {
}
return pluginPath;
};
////////////////////////////////////////////////
// 截取屏幕
captureScreenImage = function () {
var filename = objCommon.CaptureScreen(0);
if (objCommon.PathFileExists(filename)) {
wizEditor.insertValue(" + ")");
};
};
////////////////////////////////////////////////
// 剪贴板图片
clipboardToImage = function () {
var filename = objCommon.ClipboardToImage(objApp.Window.HWND, "");
if (objCommon.PathFileExists(filename)) {
wizEditor.insertValue(" + ")");
}
};
////////////////////////////////////////////////
// 剪贴板解析HTML转换到Markdown
clipboardHTMLToMd = function (htmlText) {
if (htmlText != "") {
var referencelinkRegEx = /reference-link/;
wizEditor.insertValue(toMarkdown(htmlText, {
gfm: true,
converters:[
{
filter: 'div',
replacement: function(content) {
return content + '\n';
}
},
{
filter: 'span',
replacement: function(content) {
return content;
}
},
{
filter: function (node) {
return (node.nodeName === 'A' && referencelinkRegEx.test(node.className));
},
replacement: function(content) {
return "";
}
}]})
);
return true;
}
return false;
};
////////////////////////////////////////////////
// 显示纯文本粘贴模式
showPlainPasteMode = function () {
if (plainPasteMode) {
$(".fa-clipboard").addClass("menu-selected");
} else{
$(".fa-clipboard").removeClass("menu-selected");
};
};
////////////////////////////////////////////////
// 保存文档
saveDocument = function () {
if (objDocument) {
var doc = wizEditor.getValue();
var arrResult = dealImgDoc(doc);
if (arrResult[0] != doc) {
var cursor = wizEditor.getCursor();
wizEditor.setMarkdown(arrResult[0]);
wizEditor.setCursor(cursor);
doc = arrResult[0];
};
doc = $('<div/>').text(doc).html();
doc = doc.replace(/\t/g, ' '); // 替换制表符
doc = doc.replace(/\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029)/g, "<br/>").replace(/ /g, '\u00a0');
doc += arrResult[1];
doc = "<!DOCTYPE html><html><head><style id=\"wiz_custom_css\"></style></head><body>" + doc + "</body></html>";
objDocument.UpdateDocument3(doc, 0);
modified = false;
}
};
////////////////////////////////////////////////
// Ctrl+S保存调用
OnPluginSaveMDEditor = function () {
if (wizVerisonGreaterThan45) {
if (modified) {
if (wantSaveKey && wantSaveTime) {
wantSaveKey = false;
var closeTime = new Date();
var spanTime = closeTime.getTime() - wantSaveTime.getTime();
if (spanTime < 800) { // 间隔太短表示快捷键保存的
saveDocument();
}
} else {
saveDocument();
}
}
}
else {
saveDocument();
}
};
////////////////////////////////////////////////
// 关闭标签前的事件
OnBeforeCloseTabMDEditor = function () {
};
////////////////////////////////////////////////
// 处理带图片内容
function dealImgDoc (doc) {
var arrImgTags = "";
function dealImg (imgSrc) {
var result = saveImageToLocal(imgSrc);
arrImgTags += result[1];
return result[0];
}
var imgReg = /(!\[[^\[]*?\]\()(.+?)(\s+['"][\s\S]*?['"])?(\))/g;
doc = doc.replace(imgReg, function(whole, a, b, c, d) {
if (c) {
return a + dealImg(b) + c + d;
} else{
return a + dealImg(b) + d;
}
});
var imgStrDiv = "";
if (arrImgTags != "") {
imgStrDiv = "<ed_tag name=\"markdownimage\" style=\"display:none;\">" + arrImgTags + "</ed_tag>";
};
return [doc, imgStrDiv];
}
////////////////////////////////////////////////
// 保存图片到本地临时目录
// 返回新图片路径名和图片HTML标签内容
function saveImageToLocal(filename) {
filename = filename.replace(/\\/g, '/');
var imgName = filename.substring(filename.lastIndexOf('/') + 1);
var filenameNew = filename;
var tagImg = "";
var imgFullPath = "";
if (filename.indexOf(filesDirName) == 0) {
imgFullPath = filesFullPath + imgName;
}
else {
imgFullPath = filename;
if (imgFullPath.indexOf("file:///") == 0) {
imgFullPath = imgFullPath.substring(8);
}
}
if (imgFullPath != "") {
if (objCommon.PathFileExists(imgFullPath)) {
// 转换可能包含中文名的名称,转换成Unicode
var imgNameNew = escape(imgName).replace(/%/g, '_');
// 如果超过50个字符,则简短
var extPos = imgNameNew.lastIndexOf('.');
if (extPos == -1) {
extPos = imgNameNew.length;
}
var imgNameWithoutExt = imgNameNew.substring(0, extPos);
var imgExt = imgNameNew.substring(extPos);
if (imgNameNew.length > 50) {
imgNameWithoutExt = imgNameWithoutExt.substring(0, 35 - imgExt.length);
imgNameNew = imgNameWithoutExt + imgExt;
}
// 路径不同,则进行拷贝
var imgCopyToFullPath = filesFullPath + imgNameNew;
if (imgFullPath != imgCopyToFullPath) {
// 目标文件已经存在
if (objCommon.PathFileExists(imgCopyToFullPath)) {
var date = new Date();
imgNameNew = imgNameWithoutExt + date.getTime() + imgExt;
if (imgNameNew.length > 50) {
imgNameWithoutExt = imgNameWithoutExt.substring(0, 35 - imgExt.length);
imgNameNew = imgNameWithoutExt + date.getTime() + imgExt;
}
imgCopyToFullPath = filesFullPath + imgNameNew;
}
objCommon.CopyFile(imgFullPath, imgCopyToFullPath);
}
filenameNew = filesDirName + imgNameNew;
tagImg = "<img src=\"" + imgCopyToFullPath + "\">";
}
}
return [filenameNew, tagImg];
}
////////////////////////////////////////////////
// 获得保存到本地的图片
function getSavedLocalImage(filename) {
return saveImageToLocal(filename)[0];
}
////////////////////////////////////////////////
// 设置表情文件的地址
function setEmojiFilePath () {
editormd.emoji.path = pluginFullPath + "Editor.md/emoji/emojis/";
editormd.twemoji.path = pluginFullPath + "Editor.md/emoji/twemoji/36x36/";
}
////////////////////////////////////////////////
// 加载文档
function loadDocument() {
var guid = getQueryString("guid", location.href);
var kbGUID = getQueryString("kbguid", location.href);
if (kbGUID == "" || kbGUID == null) {
objDatabase = objApp.Database;
}
else {
objDatabase = objApp.GetGroupDatabase(kbGUID);
}
var code = "";
try {
objDocument = objDatabase.DocumentFromGUID(guid);
docTitle = objDocument.Title;
document.title = "编辑 " + objDocument.Title.replace(new RegExp(".md", "gi"), "");
var content = objDocument.GetHtml();
var tempBody = document.body.innerHTML;
document.body.innerHTML = content;
var imgs = document.body.getElementsByTagName('img');
if(imgs.length){
for (var i = imgs.length - 1; i >= 0; i--) {
var pi = imgs[i];
if(pi && pi.parentNode.getAttribute("name") != "markdownimage") {
var imgmd = document.createTextNode(" + ")");
$(pi).replaceWith(imgmd);
}
}
}
var links = document.body.getElementsByTagName('a');
if(links.length){
for (var i = links.length - 1; i >= 0; i--) {
var pi = links[i];
if(pi && pi.getAttribute("href").indexOf("wiz://open_") != -1) {
var linkmd = document.createTextNode("[" + pi.textContent + "](" + pi.getAttribute("href") + ")");
$(pi).replaceWith(linkmd);
}
}
}
content = document.body.innerText;
document.body.innerHTML = tempBody;
code = content;
/*code = objDocument.GetText(0);*/
code = code.replace(/\u00a0/g, ' ');
// 如果用原生编辑器保存过图片,会被替换成错的图片路径
var imgErrorPath = guid + "_128_files/";
code = code.replace(new RegExp(imgErrorPath, "g"), filesDirName);
}
catch (err) {
}
return code;
};
////////////////////////////////////////////////
// 得到本地文件路径
function getLocalFilesPath() {
var htmlName = document.location.href;
var htmlPath = htmlName.substring(0, htmlName.lastIndexOf('/') + 1);
var htmlWinPath = htmlPath.substring(8);
return decodeURI(htmlWinPath + filesDirName);
}
////////////////////////////////////////////////
// 解析参数
function getQueryString(name, hrefValue) {
if (hrefValue.indexOf("?") == -1 || hrefValue.indexOf(name + '=') == -1) {
return '';
}
var queryString = hrefValue.substring(hrefValue.indexOf("?") + 1);
var parameters = queryString.split("&");
var pos, paraName, paraValue;
for (var i = 0; i < parameters.length; i++) {
pos = parameters[i].indexOf('=');
if (pos == -1) { continue; }
paraName = parameters[i].substring(0, pos);
paraValue = parameters[i].substring(pos + 1);
if (paraName == name) {
return unescape(paraValue.replace(/\+/g, " "));
}
}
return '';
};
////////////////////////////////////////////////
// 打开新文档
function openOtherDocument(hrefValue) {
var guid = getQueryString("guid", hrefValue);
if (guid == "") {
return true;
}
var kbGUID = getQueryString("kbguid", hrefValue);
var newDatabase = null
if (kbGUID == "" || kbGUID == null) {
newDatabase = objApp.Database;
}
else {
newDatabase = objApp.GetGroupDatabase(kbGUID);
}
var isAttachment = hrefValue.indexOf("wiz://open_attachment") != -1;
try {
if (isAttachment) {
var newAttachment = newDatabase.AttachmentFromGUID(guid);
objCommon.RunExe("explorer", newAttachment.FileName, false);
}
else
{
var newDocument = newDatabase.DocumentFromGUID(guid);
objApp.Window.ViewDocument(newDocument, true);
}
return false;
}
catch (err) {
}
return true;
};
////////////////////////////////////////////////
// 用默认浏览器打开链接
function openHrefInBrowser(hrefValue) {
if (optionSettings.HrefInBrowser == "1") {
try {
objCommon.RunExe("explorer", '\"' + hrefValue + '\"', false);
return false;
}
catch (err) {
}
}
return true;
};
});
////////////////////////////////////////////////
// 预防页面被跳转丢失编辑
window.onbeforeunload = function () {
if (modified) {
modified = false;
if (6 == objApp.Window.ShowMessage("是否将更改保存到 " + docTitle + " ?", "{p}", 0x04 + 0x20)) {
saveDocument();
}
}
};
////////////////////////////////////////////////
// 为知回调
// 关闭标签时会调用来判断是否需要保存
function OnPluginQueryModified() {
var modifiedTemp = modified;
modified = false; // 防止再次调用window.onbeforeunload
return modifiedTemp;
};
////////////////////////////////////////////////
// 为知回调
// 可响应Ctrl+S保存事件
function OnPluginSave() {
OnPluginSaveMDEditor();
return true;
};
////////////////////////////////////////////////
// 关闭标签前的事件
function onBeforeCloseTab_MDEditor() {
OnBeforeCloseTabMDEditor();
}