This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
interactive.js
220 lines (192 loc) · 5.43 KB
/
interactive.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
/**
* Definition of interactive functions: the function
* that require additional dialog prompt and update
* editor content when user types data in prompt
*/
define(function(require, exports, module) {
var utils = require('emmet/utils/common');
var editorUtils = require('emmet/utils/editor');
var actionUtils = require('emmet/utils/action');
var range = require('emmet/assets/range');
var htmlMatcher = require('emmet/assets/htmlMatcher');
var parser = require('emmet/parser/abbreviation');
var updateTag = require('emmet/action/updateTag');
var prompt = require('./prompt');
/**
* Caches wrapping context for current selection in editor
* @param {IEmmetEditor} editor
* @param {Object} info Current editor info (content, syntax, etc.)
* @return {Object}
*/
function selectionContext(editor, info) {
info = info || editorUtils.outputInfo(editor);
var result = [];
editor.exec(function(i, sel) {
var r = range(sel);
var tag = htmlMatcher.tag(info.content, r.start);
if (!r.length() && tag) {
// no selection, use tag pair
r = utils.narrowToNonSpace(info.content, tag.range);
}
var out = {
selection: r,
tag: tag,
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
if (r.length()) {
var pasted = utils.escapeText(r.substring(info.content));
out.pastedContent = editorUtils.unindent(editor, pasted);
}
result[i] = out;
}, true);
return result;
}
function updateFinalCarets(selCtx, fromIndex, delta) {
if (!delta) {
return;
}
for (var i = fromIndex + 1, il = selCtx.length; i < il; i++) {
selCtx[i].finalCaret.line += delta;
}
}
/**
* Returns current caret position for given editor
* @param {Editor} editor Brackets editor instance
* @return {Point} Character position in editor
*/
function getCaret(editor) {
// make sure we’re taking first caret
return editor.getSelections()[0].start;
}
function lineDelta(prev, cur) {
return utils.splitByLines(cur).length - utils.splitByLines(prev).length;
}
function setFinalCarets(selCtx, editor) {
if (selCtx && selCtx.length > 1) {
editor.setSelections(selCtx.map(function(ctx) {
return {
start: ctx.finalCaret,
end: ctx.finalCaret
};
}));
}
}
return {
run: function(cmd, editor) {
if (cmd === 'wrap_with_abbreviation') {
return this.wrapWithAbbreviation(editor);
}
if (cmd === 'update_tag') {
return this.updateTag(editor);
}
if (cmd === 'interactive_expand_abbreviation') {
return this.expandAbbreviation(editor);
}
},
expandAbbreviation: function(editor) {
var info = editorUtils.outputInfo(editor);
var selCtx = [];
editor.exec(function(i, sel) {
var r = range(sel);
selCtx[i] = {
selection: r,
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
});
return this.wrapWithAbbreviation(editor, selCtx);
},
wrapWithAbbreviation: function(editor, selCtx) {
selCtx = selCtx || selectionContext(editor);
// show prompt dialog that will wrap each selection
// on user typing
prompt.show({
label: 'Enter Abbreviation',
editor: editor.editor,
update: function(abbr) {
var result, replaced;
for (var i = selCtx.length - 1, ctx; i >= 0; i--) {
ctx = selCtx[i];
result = '';
try {
if (abbr) {
result = parser.expand(abbr, ctx);
} else {
result = ctx.pastedContent;
}
} catch (e) {
console.error(e);
result = ctx.pastedContent;
}
editor._selection.index = i;
replaced = editor.replaceContent(result, ctx.selection.start, ctx.selection.end);
ctx.finalCaret = getCaret(editor.editor);
updateFinalCarets(selCtx, i, lineDelta(ctx.pastedContent, replaced));
}
},
confirm: function() {
setFinalCarets(selCtx, editor.editor);
}
});
},
updateTag: function(editor) {
var info = editorUtils.outputInfo(editor);
var selCtx = selectionContext(editor, info);
// show prompt dialog that will update each
// tag from selection
prompt.show({
label: 'Enter Abbreviation',
editor: editor.editor,
update: function(abbr) {
abbr = abbr.trim();
var tag, replaced;
var didUpdate = false;
for (var i = selCtx.length - 1, ctx; i >= 0; i--) {
ctx = selCtx[i];
tag = null;
if (abbr) {
try {
tag = updateTag.getUpdatedTag(abbr, {match: ctx.tag}, info.content, {
counter: ctx.counter
});
} catch (e) {
console.error(e);
}
}
if (!tag) {
continue;
}
replaced = [{
start: ctx.tag.open.range.start,
end: ctx.tag.open.range.end,
content: tag.source
}];
if (tag.name() != ctx.tag.name && ctx.tag.close) {
replaced.unshift({
start: ctx.tag.close.range.start,
end: ctx.tag.close.range.end,
content: '</' + tag.name() + '>'
});
}
replaced.forEach(function(data) {
didUpdate = true;
editor.replaceContent(data.content, data.start, data.end);
ctx.finalCaret = editor._posFromIndex(data.start);
});
}
return didUpdate;
},
confirm: function() {
setFinalCarets(selCtx, editor.editor);
}
});
}
};
});