This repository was archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcodemirror.plugins.js
More file actions
67 lines (62 loc) · 1.79 KB
/
codemirror.plugins.js
File metadata and controls
67 lines (62 loc) · 1.79 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
/**
* Extension for CodeMirror.
*
* @author Miclle <miclle.zheng@gmail.com>
* Contributed under the same license terms as CodeMirror.
*/
(function() {
/**
* insert '## text'
*/
CodeMirror.defineExtension("insertTitle", function(num) {
var pos = this.getCursor();
pos.ch = 0;
this.replaceRange("###### ".slice(6 - num), pos, pos);
});
/**
* insert '*text*' '**text**' ...
*/
CodeMirror.defineExtension("wrapSymbolTag", function(symbol) {
var selectString = this.getSelection();
this.replaceSelection(symbol + selectString + symbol);
var cursor = this.getCursor(false);
if(selectString == "") cursor.ch = cursor.ch - symbol.length;
this.setCursor(cursor);
});
/**
* save markdown content
*/
CodeMirror.defineExtension("saveMarkdownContent", function() {
function eventFire(el, etype){
if (el.fireEvent) {
(el.fireEvent('on' + etype));
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
var link = document.createElement("a");
link.download = "markdown.md";
link.href = "data:application/stream;base64," + $.base64.encode(this.getValue());
eventFire(link, "click");
});
/**
* tab fast keys
*/
CodeMirror.defineExtension("tabFastKey", function() {
var posEnd = this.getCursor("end");
var posStart = { line: posEnd.line, ch: posEnd.ch - 1}
switch(this.getRange(posStart, posEnd)){
case "a":
this.replaceRange("[Title text](http://sample.com/)", posStart, posEnd);
break;
case "i":
this.replaceRange("", posStart, posEnd);
break;
default:
CodeMirror.commands.defaultTab(this);
break;
}
});
})();