forked from emmetio/codemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
220 lines (190 loc) · 5.22 KB
/
editor.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
/**
* Emmet Editor interface implementation for CodeMirror.
* Interface is optimized for multiple cursor usage: authors
* should run acttion multiple times and update `selectionIndex`
* property on each iteration.
*/
import emmet from './emmet';
export var modeMap = {
'text/html': 'html',
'application/xml': 'xml',
'text/xsl': 'xsl',
'text/css': 'css',
'text/x-less': 'less',
'text/x-scss': 'scss',
'text/x-sass': 'sass'
};
export default class EmmetEditor {
constructor(ctx, selIndex=0) {
this.context = ctx;
this.selectionIndex = selIndex || 0;
}
/**
* Returns list of selections for current CodeMirror instance.
* @return {Array}
*/
selectionList() {
var cm = this.context;
return cm.listSelections().map(function(sel) {
var anchor = posToIndex(cm, sel.anchor);
var head = posToIndex(cm, sel.head);
return {
start: Math.min(anchor, head),
end: Math.max(anchor, head)
};
});
}
getCaretPos() {
return this.getSelectionRange().start;
}
setCaretPos(pos) {
this.createSelection(pos);
}
/**
* Returns current selection range (for current selection index)
* @return {Object}
*/
getSelectionRange() {
return this.selectionList()[this.selectionIndex];
}
createSelection(start, end) {
if (typeof end == 'undefined') {
end = start;
}
var sels = this.selectionList();
var cm = this.context;
sels[this.selectionIndex] = {start: start, end: end};
this.context.setSelections(sels.map(function(sel) {
return {
head: indexToPos(cm, sel.start),
anchor: indexToPos(cm, sel.end)
};
}));
}
/**
* Returns current selection
* @return {String}
*/
getSelection() {
var sel = this.getSelectionRange();
sel.start = indexToPos(this.context, sel.start);
sel.end = indexToPos(this.context, sel.end);
return this.context.getRange(sel.start, sel.end);
}
getCurrentLineRange() {
var caret = indexToPos(this.context, this.getCaretPos());
return {
start: posToIndex(this.context, caret.line, 0),
end: posToIndex(this.context, caret.line, this.context.getLine(caret.line).length)
};
}
getCurrentLine() {
var caret = indexToPos(this.context, this.getCaretPos());
return this.context.getLine(caret.line) || '';
}
replaceContent(value, start, end, noIndent) {
if (typeof end == 'undefined') {
end = (typeof start == 'undefined') ? this.getContent().length : start;
}
if (typeof start == 'undefined') {
start = 0;
}
// normalize indentation according to editor preferences
value = this.normalize(value);
// indent new value
if (!noIndent) {
value = emmet.utils.common.padString(value, emmet.utils.common.getLinePaddingFromPosition(this.getContent(), start));
}
// find new caret position
var tabstopData = emmet.tabStops.extract(value, {escape: ch => ch});
value = tabstopData.text;
var firstTabStop = tabstopData.tabstops[0] || {start: value.length, end: value.length};
firstTabStop.start += start;
firstTabStop.end += start;
this.context.replaceRange(value, indexToPos(this.context, start), indexToPos(this.context, end));
this.createSelection(firstTabStop.start, firstTabStop.end);
}
/**
* Normalizes string indentation in given string
* according to editor preferences
* @param {String} str
* @return {String}
*/
normalize(str) {
var indent = '\t';
var ctx = this.context;
if (!ctx.getOption('indentWithTabs')) {
indent = emmet.utils.common.repeatString(' ', ctx.getOption('indentUnit'));
}
return emmet.utils.editor.normalize(str, {
indentation: indent
});
}
getContent() {
return this.context.getValue();
}
getSyntax() {
var editor = this.context;
var pos = editor.posFromIndex(this.getCaretPos());
var mode = editor.getModeAt(editor.getCursor());
var syntax = mode.name;
if (syntax === 'xml' && mode.configuration) {
syntax = mode.configuration;
}
return syntax || emmet.utils.action.detectSyntax(this, syntax);
}
/**
* Returns current output profile name (@see emmet#setupProfile)
* @return {String}
*/
getProfileName() {
if (this.context.getOption('profile')) {
return this.context.getOption('profile');
}
return emmet.utils.action.detectProfile(this);
}
/**
* Ask user to enter something
* @param {String} title Dialog title
* @return {String} Entered data
*/
prompt(title) {
return prompt(title);
}
/**
* Returns current editor's file path
* @return {String}
*/
getFilePath() {
return location.href;
}
/**
* Check if current editor syntax is valid, e.g. is supported by Emmet
* @return {Boolean}
*/
isValidSyntax() {
return emmet.resources.hasSyntax(this.getSyntax());
}
}
/**
* Converts CM’s inner representation of character
* position (line, ch) to character index in text
* @param {CodeMirror} cm CodeMirror instance
* @param {Object} pos Position object
* @return {Number}
*/
function posToIndex(cm, pos) {
if (arguments.length > 2 && typeof pos !== 'object') {
pos = {line: arguments[1], ch: arguments[2]}
}
return cm.indexFromPos(pos);
}
/**
* Converts charater index in text to CM’s internal object representation
* @param {CodeMirror} cm CodeMirror instance
* @param {Number} ix Character index in CM document
* @return {Object}
*/
function indexToPos(cm, ix) {
return cm.posFromIndex(ix);
}