-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
arrowKeyFunction.js
142 lines (121 loc) 路 4.05 KB
/
arrowKeyFunction.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
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
/**
* @modifier NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import CodeMirror from 'codemirror';
/*eslint-disable */
CodeMirror.commands.replaceLineTextToUpper = cm => {
if (cm.getOption("disableInput")) {
return CodeMirror.Pass;
}
const ranges = cm.listSelections();
const lineAdjustment = -1;
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
const from = range.anchor;
const to = range.head;
if (isSameLineSelection(range) && to.line > 0) {
replaceSingleLine(cm, from, to, lineAdjustment);
} else if (!isRangeCollapsed(range)) {
const topLine = from.line < to.line ? from.line : to.line;
if (topLine > 0) {
const upper = from.line === topLine ? from : to;
const bottom = from.line === topLine ? to : from;
replaceMultiLine(cm, upper, bottom, lineAdjustment);
}
}
}
};
CodeMirror.commands.replaceLineTextToLower = cm => {
if (cm.getOption("disableInput")) {
return CodeMirror.Pass;
}
const ranges = cm.listSelections();
const lineAdjustment = 1;
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
const from = range.anchor;
const to = range.head;
const isLastLine = to.line === cm.lastLine();
if (isSameLineSelection(range) && !isLastLine) {
replaceSingleLine(cm, from, to, lineAdjustment);
} else if (!isRangeCollapsed(range)) {
const topLine = from.line < to.line ? from.line : to.line;
const upper = from.line === topLine ? from : to;
const bottom = from.line === topLine ? to : from;
if (bottom.line < cm.lastLine()) {
replaceMultiLine(cm, upper, bottom, lineAdjustment);
}
}
}
};
function isRangeCollapsed(range) {
return isSameLineSelection(range)
&& range.anchor.ch === range.head.ch;
}
function isSameLineSelection(range) {
return range.anchor.line === range.head.line;
}
function replaceSingleLine(cm, from, to, lineAdjustment) {
const currentLine = cm.getLine(to.line);
const replacement = cm.getLine(to.line + lineAdjustment);
const range = {
anchor: from,
head: to
};
cm.replaceRange(replacement, {
line: to.line, ch: 0
}, {
line: to.line, ch: currentLine.length
}, '+input');
cm.replaceRange(currentLine, {
line: to.line + lineAdjustment, ch: 0
}, {
line: to.line + lineAdjustment, ch: replacement.length
}, '+input');
if (isRangeCollapsed(range)) {
cm.setCursor({
line: to.line + lineAdjustment,
ch: to.ch
});
} else {
cm.setSelection({
line: from.line + lineAdjustment,
ch: from.ch
}, {
line: to.line + lineAdjustment,
ch: to.ch
});
}
}
function replaceMultiLine(cm, upper, bottom, lineAdjustment) {
const rangeContent = cm.getRange({
line: upper.line, ch: 0
}, {
line: bottom.line, ch: cm.getLine(bottom.line).length
});
const edgeLineOfConcern = lineAdjustment > 0 ? bottom : upper;
const replacement = cm.getLine(edgeLineOfConcern.line + lineAdjustment);
let targetLine;
if (lineAdjustment > 0) {
targetLine = upper;
} else {
targetLine = bottom;
}
cm.replaceRange(replacement, {
line: targetLine.line, ch: 0
}, {
line: targetLine.line, ch: cm.getLine(targetLine.line).length
}, '+input');
cm.replaceRange(rangeContent, {
line: upper.line + lineAdjustment, ch: 0
}, {
line: bottom.line + lineAdjustment, ch: cm.getLine(bottom.line + lineAdjustment).length
}, '+input');
cm.setSelection({
line: upper.line + lineAdjustment, ch: upper.ch
}, {
line: bottom.line + lineAdjustment, ch: bottom.ch
});
}