init autocomplete/blur/focus for usercss in a common hook

This commit is contained in:
tophf 2018-01-14 15:57:29 +03:00
parent b42dfc8300
commit eaed0de486
3 changed files with 21 additions and 22 deletions

View File

@ -33,6 +33,27 @@ onDOMscriptReady('/codemirror.js').then(() => {
rerouteHotkeys,
});
CodeMirror.defineInitHook(cm => {
if (prefs.get('editor.livePreview')) {
cm.on('changes', updatePreview);
}
if (prefs.get('editor.autocompleteOnTyping')) {
setupAutocomplete(cm);
}
const wrapper = cm.display.wrapper;
cm.on('blur', () => {
editors.lastActive = cm;
cm.rerouteHotkeys(true);
setTimeout(() => {
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
});
});
cm.on('focus', () => {
cm.rerouteHotkeys(false);
wrapper.classList.add('CodeMirror-active');
});
});
new MutationObserver((mutations, observer) => {
if (!$('#sections')) {
return;
@ -561,11 +582,6 @@ onDOMscriptReady('/codemirror.js').then(() => {
updatePreview(null, previewing);
}
};
CodeMirror.defineInitHook(cm => {
if (prefs.get('editor.livePreview')) {
cm.on('changes', updatePreview);
}
});
}
function updatePreview(data, previewing) {

View File

@ -190,21 +190,7 @@ function setupCodeMirror(sectionDiv, code, index) {
clearTimeout(onChangeTimer);
onChangeTimer = setTimeout(indicateCodeChange, 200, cm, changes);
});
if (prefs.get('editor.autocompleteOnTyping')) {
setupAutocomplete(cm);
}
wrapper.addEventListener('keydown', event => nextPrevEditorOnKeydown(cm, event), true);
cm.on('blur', () => {
editors.lastActive = cm;
cm.rerouteHotkeys(true);
setTimeout(() => {
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
});
});
cm.on('focus', () => {
cm.rerouteHotkeys(false);
wrapper.classList.add('CodeMirror-active');
});
cm.on('paste', (cm, event) => {
const text = event.clipboardData.getData('text') || '';
if (

View File

@ -44,9 +44,6 @@ function createSourceEditor(style) {
updateLintReportIfEnabled(cm);
});
cm.on('focus', () => cm.rerouteHotkeys(false));
cm.on('blur', () => cm.rerouteHotkeys(true));
CodeMirror.commands.prevEditor = cm => nextPrevMozDocument(cm, -1);
CodeMirror.commands.nextEditor = cm => nextPrevMozDocument(cm, 1);
CodeMirror.commands.toggleStyle = toggleStyle;