-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
strike.js
86 lines (76 loc) · 2.25 KB
/
strike.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
/**
* @fileoverview Implements StrikeThrough markdown command
* @author Junghwan Park(junghwan.park@nhnent.com) FE Development Team/NHN Ent.
*/
import CommandManager from '../commandManager';
const strikeRegex = /^[~~](.*[\s\n]*.*)*[~~]$/;
/**
* Strike
* Add strike markdown syntax to markdown editor
* @extends Command
* @module markdownCommands/Strike
* @ignore
*/
const Strike = CommandManager.command('markdown', /** @lends Strike */{
name: 'Strike',
keyMap: ['CTRL+S', 'META+S'],
/**
* Command handler
* @param {MarkdownEditor} mde MarkdownEditor instance
*/
exec(mde) {
const cm = mde.getEditor();
const doc = cm.getDoc();
const cursor = doc.getCursor();
const selection = doc.getSelection();
const isNeedToRemove = this.hasStrikeSyntax(selection);
let result;
if (isNeedToRemove) {
result = this.remove(selection);
} else {
result = this.append(selection);
}
doc.replaceSelection(result, 'around');
const isEmptySelection = !selection;
if (isEmptySelection && !isNeedToRemove) {
this.setCursorToCenter(doc, cursor, isNeedToRemove);
}
cm.focus();
},
/**
* hasStrikeSyntax
* @param {string} text Source text
* @returns {boolean} Boolean value of strike syntax removal
*/
hasStrikeSyntax(text) {
return strikeRegex.test(text);
},
/**
* append
* @param {string} text 적용할 텍스트
* @returns {string} strikeThrough text
*/
append(text) {
return `~~${text}~~`;
},
/**
* remove
* @param {string} text 제거할 텍스트
* @returns {string} 제거된 텍스트
*/
remove(text) {
return text.substr(2, text.length - 4);
},
/**
* setCursorToCenter
* 커서를 중앙으로 이동시킨다
* @param {CodeMirror.doc} doc 코드미러 도큐먼트
* @param {object} cursor 커서객체
* @param {boolean} isRemoved 변경사항이 지우는 변경이었는지 여부
*/
setCursorToCenter(doc, cursor, isRemoved) {
const pos = isRemoved ? -2 : 2;
doc.setCursor(cursor.line, cursor.ch + pos);
}
});
export default Strike;