-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable our old emmet mechanism as well
- Loading branch information
Ives van Hoorne
committed
Feb 21, 2018
1 parent
c70a074
commit 7650b58
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/app/src/app/components/CodeEditor/Monaco/enable-combi-emmet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
3 changes: 3 additions & 0 deletions
3
packages/app/src/app/components/CodeEditor/Monaco/enable-emmet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters