unbork selectByTokens option

...by moving custom options definitions before isEditorPref checks CodeMirror.defaults
This commit is contained in:
tophf 2021-03-14 19:18:43 +03:00
parent db77e03e97
commit a7ae3fbc55

View File

@ -40,6 +40,8 @@
}, },
}; };
// focus and blur
const onCmFocus = cm => { const onCmFocus = cm => {
rerouteHotkeys.toggle(false); rerouteHotkeys.toggle(false);
cm.display.wrapper.classList.add('CodeMirror-active'); cm.display.wrapper.classList.add('CodeMirror-active');
@ -57,34 +59,50 @@
cm.on('blur', onCmBlur); cm.on('blur', onCmBlur);
}); });
const handledPrefs = { // propagated preferences
'editor.colorpicker'() {}, // handled in colorpicker-helper.js
async 'editor.theme'(key, value) { const prefToCmOpt = k =>
let el2;
const el = $('#cm-theme');
if (value === 'default') {
el.href = '';
} else {
const path = `/vendor/codemirror/theme/${value}.css`;
if (el.href !== location.origin + path) {
// avoid flicker: wait for the second stylesheet to load, then apply the theme
el2 = await require([path]);
}
}
cmFactory.globalSetOption('theme', value);
if (el2) {
el.remove();
el2.id = el.id;
}
},
};
const pref2opt = k => k.slice('editor.'.length);
const mirroredPrefs = prefs.knownKeys.filter(k =>
!handledPrefs[k] &&
k.startsWith('editor.') && k.startsWith('editor.') &&
Object.hasOwnProperty.call(CodeMirror.defaults, pref2opt(k))); k.slice('editor.'.length);
prefs.subscribe(mirroredPrefs, (k, val) => cmFactory.globalSetOption(pref2opt(k), val)); const prefKeys = prefs.knownKeys.filter(k =>
prefs.subscribeMany(handledPrefs); k !== 'editor.colorpicker' && // handled in colorpicker-helper.js
prefToCmOpt(k) in CodeMirror.defaults);
const {insertTab, insertSoftTab} = CodeMirror.commands;
for (const [key, fn] of Object.entries({
'editor.tabSize'(cm, value) {
cm.setOption('indentUnit', Number(value));
},
'editor.indentWithTabs'(cm, value) {
CodeMirror.commands.insertTab = value ? insertTab : insertSoftTab;
},
'editor.matchHighlight'(cm, value) {
const showToken = value === 'token' && /[#.\-\w]/;
const opt = (showToken || value === 'selection') && {
showToken,
annotateScrollbar: true,
onUpdate: updateMatchHighlightCount,
};
cm.setOption('highlightSelectionMatches', opt || null);
},
'editor.selectByTokens'(cm, value) {
cm.setOption('configureMouse', value ? configureMouseFn : null);
},
})) {
CodeMirror.defineOption(prefToCmOpt(key), prefs.get(key), fn);
prefKeys.push(key);
}
prefs.subscribe(prefKeys, (key, val) => {
const name = prefToCmOpt(key);
if (name === 'theme') {
loadCmTheme(val);
} else {
cmFactory.globalSetOption(name, val);
}
});
// lazy propagation
lazyOpt = window.IntersectionObserver && { lazyOpt = window.IntersectionObserver && {
names: ['theme', 'lineWrapping'], names: ['theme', 'lineWrapping'],
@ -160,29 +178,24 @@
//#endregion //#endregion
//#region CM option handlers //#region CM option handlers
const {insertTab, insertSoftTab} = CodeMirror.commands; async function loadCmTheme(name) {
Object.entries({ let el2;
tabSize(cm, value) { const el = $('#cm-theme');
cm.setOption('indentUnit', Number(value)); if (name === 'default') {
}, el.href = '';
indentWithTabs(cm, value) { } else {
CodeMirror.commands.insertTab = value ? insertTab : insertSoftTab; const path = `/vendor/codemirror/theme/${name}.css`;
}, if (el.href !== location.origin + path) {
matchHighlight(cm, value) { // avoid flicker: wait for the second stylesheet to load, then apply the theme
const showToken = value === 'token' && /[#.\-\w]/; el2 = await require([path]);
const opt = (showToken || value === 'selection') && { }
showToken, }
annotateScrollbar: true, cmFactory.globalSetOption('theme', name);
onUpdate: updateMatchHighlightCount, if (el2) {
}; el.remove();
cm.setOption('highlightSelectionMatches', opt || null); el2.id = el.id;
}, }
selectByTokens(cm, value) { }
cm.setOption('configureMouse', value ? configureMouseFn : null);
},
}).forEach(([name, fn]) => {
CodeMirror.defineOption(name, prefs.get('editor.' + name), fn);
});
function updateMatchHighlightCount(cm, state) { function updateMatchHighlightCount(cm, state) {
cm.display.wrapper.dataset.matchHighlightCount = state.matchesonscroll.matches.length; cm.display.wrapper.dataset.matchHighlightCount = state.matchesonscroll.matches.length;