Add editor.appliesToLineWidget option

This commit is contained in:
eight 2017-09-14 09:58:22 +08:00
parent 2ba4c4f11b
commit 70bbc08ee0
3 changed files with 77 additions and 13 deletions

View File

@ -40,6 +40,14 @@
"message": "Applies to", "message": "Applies to",
"description": "Label for 'applies to' fields on the edit/add screen" "description": "Label for 'applies to' fields on the edit/add screen"
}, },
"appliesLineWidgetLabel": {
"message": "Display 'Applies to' info",
"description": "Label for the checkbox to display applies-to information in the single editor"
},
"appliesLineWidgetWarning": {
"message": "Does not work with minified CSS",
"description": "A warning that applies-to information won't show properly with minified CSS"
},
"appliesRegexpOption": { "appliesRegexpOption": {
"message": "URLs matching the regexp", "message": "URLs matching the regexp",
"description": "Option to make the style apply to the entered string as a regular expression" "description": "Option to make the style apply to the entered string as a regular expression"

View File

@ -43,15 +43,72 @@ function createSourceEditor(style) {
function initAppliesToReport(cm) { function initAppliesToReport(cm) {
const DELAY = 500; const DELAY = 500;
let widgets = []; let widgets = [], timer, fromLine, toLine, style, isInit;
let timer; const optionEl = buildOption();
let fromLine = null;
let toLine = null; $('#options').insertBefore(optionEl, $('#options > .option.aligned'));
let style = getComputedStyle(cm.getGutterElement());
if (prefs.get('editor.appliesToLineWidget')) {
init();
}
prefs.subscribe(['editor.appliesToLineWidget'], (key, value) => {
if (!isInit && value) {
init();
} else if (isInit && !value) {
uninit();
}
optionEl.checked = value;
});
optionEl.addEventListener('change', e => {
prefs.set('editor.appliesToLineWidget', e.target.checked);
});
function buildOption() {
return $element({className: 'option', appendChild: [
$element({
tag: 'input',
type: 'checkbox',
id: 'editor.appliesToLineWidget',
checked: prefs.get('editor.appliesToLineWidget')
}),
$element({
tag: 'label',
htmlFor: 'editor.appliesToLineWidget',
textContent: ' ' + t('appliesLineWidgetLabel'),
title: t('appliesLineWidgetWarning')
})
]});
}
function init() {
isInit = true;
style = getComputedStyle(cm.getGutterElement());
fromLine = null;
toLine = null;
cm.on('change', onChange);
cm.on('optionChange', onOptionChange);
// is it possible to avoid flickering?
window.addEventListener('load', updateStyle);
update(); update();
}
cm.on('change', (cm, {from, to}) => { function uninit() {
isInit = false;
widgets.forEach(w => w.clear());
widgets.length = 0;
cm.off('change', onChange);
cm.off('optionChange', onOptionChange);
window.removeEventListener('load', updateStyle);
}
function onChange(cm, {from, to}) {
if (fromLine === null || toLine === null) { if (fromLine === null || toLine === null) {
fromLine = from.line; fromLine = from.line;
toLine = to.line; toLine = to.line;
@ -61,16 +118,13 @@ function createSourceEditor(style) {
} }
clearTimeout(timer); clearTimeout(timer);
timer = setTimeout(update, DELAY); timer = setTimeout(update, DELAY);
}); }
cm.on('optionChange', (cm, option) => { function onOptionChange(cm, option) {
if (option === 'theme') { if (option === 'theme') {
updateStyle(); updateStyle();
} }
}); }
// is it possible to avoid flickering?
window.addEventListener('load', updateStyle);
function update() { function update() {
cm.operation(doUpdate); cm.operation(doUpdate);

View File

@ -51,6 +51,8 @@ var prefs = new function Prefs() {
'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token 'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token
'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu 'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu
'editor.appliesToLineWidget': true, // show applies-to line widget on the editor
'iconset': 0, // 0 = dark-themed icon 'iconset': 0, // 0 = dark-themed icon
// 1 = light-themed icon // 1 = light-themed icon