usercss editor: restore "Add" and "Regexp test", convert \ in regexps

This commit is contained in:
tophf 2017-12-05 21:32:28 +03:00
parent 36ec8de04f
commit 963930ea1d
2 changed files with 19 additions and 22 deletions

View File

@ -34,13 +34,13 @@ function createAppliesToLineWidget(cm) {
$create('li.applies-to-everything', t('appliesToEverything')), $create('li.applies-to-everything', t('appliesToEverything')),
}; };
$('button', TPL.listItem).insertAdjacentElement('afterend', $('button', TPL.listItem).insertAdjacentElement('beforebegin',
$create('button.test-regexp', t('styleRegexpTestButton'))); $create('button.test-regexp', t('styleRegexpTestButton')));
CLICK_ROUTE = { CLICK_ROUTE = {
'.test-regexp': (item, apply) => { '.test-regexp': (item, apply) => {
regExpTester.toggle(); regExpTester.toggle();
regExpTester.update([apply.value.text]); regExpTester.update([unescapeDoubleslash(apply.value.text)]);
}, },
'.remove-applies-to': (item, apply) => { '.remove-applies-to': (item, apply) => {
@ -101,18 +101,15 @@ function createAppliesToLineWidget(cm) {
const apply = item.__apply; const apply = item.__apply;
changeItem(apply, 'type', typeElement.value); changeItem(apply, 'type', typeElement.value);
item.dataset.type = apply.type.text; item.dataset.type = apply.type.text;
} else {
return EVENTS.oninput.apply(this, arguments);
} }
}, },
oninput({target}) { oninput({target}) {
if (target.matches('.applies-value')) { if (target.matches('.applies-value')) {
const apply = target.closest('.applies-to-item').__apply; const item = target.closest('.applies-to-item');
debounce(changeItem, THROTTLE_DELAY, apply, 'value', target.value); const apply = item.__apply;
} changeItem(apply, 'value', target.value);
},
onfocus({target}) {
if (target.matches('.test-regexp')) {
const apply = target.closest('.applies-to-item').__apply;
updateRegexpTest(apply);
} }
}, },
onclick({target}) { onclick({target}) {
@ -489,7 +486,7 @@ function createAppliesToLineWidget(cm) {
part = apply[part]; part = apply[part];
const range = part.mark.find(); const range = part.mark.find();
part.mark.clear(); part.mark.clear();
newText = newText.replace(/\\/g, '\\\\'); newText = unescapeDoubleslash(newText).replace(/\\/g, '\\\\');
cm.replaceRange(newText, range.from, range.to, 'appliesTo'); cm.replaceRange(newText, range.from, range.to, 'appliesTo');
part.mark = cm.markText( part.mark = cm.markText(
range.from, range.from,
@ -508,13 +505,9 @@ function createAppliesToLineWidget(cm) {
); );
} }
updateRegexpTest(apply);
}
function updateRegexpTest(apply) {
if (apply.type.text === 'regexp') { if (apply.type.text === 'regexp') {
const rx = apply.value.text.trim(); const rx = apply.value.text.trim();
regExpTester.update(rx ? [rx] : {}); regExpTester.update(rx ? [unescapeDoubleslash(rx)] : {});
} }
} }
@ -526,7 +519,6 @@ function createAppliesToLineWidget(cm) {
const valueStart = typeEnd + 1 + Number(isQuoted); const valueStart = typeEnd + 1 + Number(isQuoted);
const valueEnd = valueStart + valueText.length; const valueEnd = valueStart + valueText.length;
const end = valueEnd + Number(isQuoted) + 1; const end = valueEnd + Number(isQuoted) + 1;
const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/.test(valueText);
return { return {
start, start,
type: { type: {
@ -535,7 +527,7 @@ function createAppliesToLineWidget(cm) {
end: typeEnd, end: typeEnd,
}, },
value: { value: {
text: hasSingleEscapes ? valueText : valueText.replace(/\\\\/g, '\\'), text: unescapeDoubleslash(valueText),
start: valueStart, start: valueStart,
end: valueEnd, end: valueEnd,
}, },
@ -581,4 +573,9 @@ function createAppliesToLineWidget(cm) {
const first = s.charAt(0); const first = s.charAt(0);
return (first === '"' || first === "'") && s.endsWith(first) ? s.slice(1, -1) : s; return (first === '"' || first === "'") && s.endsWith(first) ? s.slice(1, -1) : s;
} }
function unescapeDoubleslash(s) {
const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/.test(s);
return hasSingleEscapes ? s : s.replace(/\\\\/g, '\\');
}
} }

View File

@ -331,11 +331,11 @@ body[data-match-highlight="selection"] .CodeMirror-selection-highlight-scrollbar
min-height: 1.4rem; min-height: 1.4rem;
margin-left: 0.35rem; margin-left: 0.35rem;
} }
.applies-to li .add-applies-to { html:not(.usercss) .applies-to li .add-applies-to {
visibility: hidden; visibility: hidden;
text-align: left; text-align: left;
} }
.applies-to li:last-child .add-applies-to { html:not(.usercss) .applies-to li:last-child .add-applies-to {
visibility: visible visibility: visible
} }
.applies-to li .add-applies-to:first-child { .applies-to li .add-applies-to:first-child {
@ -646,8 +646,8 @@ html:not(.usercss) .usercss-only,
margin-top: 0.35rem; margin-top: 0.35rem;
} }
.CodeMirror-linewidget .applies-to li:not([data-type="regexp"]) .applies-to-regexp-test { .CodeMirror-linewidget .applies-to li[data-type="regexp"] .test-regexp {
display: none; display: inline;
} }
.CodeMirror-linewidget li.applies-to-everything { .CodeMirror-linewidget li.applies-to-everything {