forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable-combi-emmet.js
More file actions
77 lines (65 loc) · 2.18 KB
/
enable-combi-emmet.js
File metadata and controls
77 lines (65 loc) · 2.18 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
68
69
70
71
72
73
74
75
76
77
import extractAbbreviation from '@emmetio/extract-abbreviation';
import { expand } from '@emmetio/expand-abbreviation';
const field = () => '';
const expandAbbreviation = (source, language) =>
expand(source.abbreviation, {
field,
syntax: language,
addons: {
jsx: true,
},
});
const enableEmmet = (editor, monaco) => {
if (!editor) {
throw new Error('Must provide monaco-editor instance.');
}
editor.addAction({
// An unique identifier of the contributed action.
id: 'emmet-abbr',
// A label of the action that will be presented to the user.
label: 'Emmet: Expand abbreviation',
// An optional array of keybindings for the action.
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_E], // eslint-disable-line no-bitwise
// A precondition for this action.
precondition: null,
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
keybindingContext: null,
contextMenuGroupId: 'navigation',
contextMenuOrder: 1.5,
// Method that will be executed when the action is triggered.
// @param editor The editor instance is passed in as a convenience
run: () => {
let word = editor.model.getValueInRange(editor.getSelection());
const pos = editor.getPosition();
if (!word) {
const lineContent = editor.model.getLineContent(pos.lineNumber);
word = extractAbbreviation(lineContent.substring(0, pos.column));
}
if (word) {
// Get expand text
const expandText = expandAbbreviation(word, 'html');
if (expandText) {
// replace range content: pos.column , pos.column -word.length;
const range = new monaco.Range(
pos.lineNumber,
pos.column - word.abbreviation.length,
pos.lineNumber,
pos.column
);
const id = { major: 1, minor: 1 };
const op = {
identifier: id,
range,
text: expandText,
forceMoveMarkers: true,
};
editor.executeEdits('', [op]);
return null;
}
return false;
}
return false;
},
});
};
export default enableEmmet;