2017-11-08 22:30:48 +00:00
|
|
|
/* global regExpTester debounce messageBox */
|
2017-11-01 01:11:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function createAppliesToLineWidget(cm) {
|
|
|
|
const APPLIES_TYPE = [
|
|
|
|
[t('appliesUrlOption'), 'url'],
|
|
|
|
[t('appliesUrlPrefixOption'), 'url-prefix'],
|
|
|
|
[t('appliesDomainOption'), 'domain'],
|
|
|
|
[t('appliesRegexpOption'), 'regexp']
|
|
|
|
];
|
|
|
|
const THROTTLE_DELAY = 400;
|
|
|
|
let widgets = [];
|
2017-11-15 06:04:09 +00:00
|
|
|
let fromLine, toLine, styleVariables;
|
2017-11-09 04:44:30 +00:00
|
|
|
let initialized = false;
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
return {toggle};
|
|
|
|
|
2017-11-09 04:44:30 +00:00
|
|
|
function toggle(newState = !initialized) {
|
|
|
|
newState = Boolean(newState);
|
|
|
|
if (newState !== initialized) {
|
|
|
|
if (newState) {
|
|
|
|
init();
|
|
|
|
} else {
|
|
|
|
uninit();
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2017-11-09 04:44:30 +00:00
|
|
|
initialized = true;
|
2017-11-01 01:11:27 +00:00
|
|
|
|
2017-11-15 06:04:09 +00:00
|
|
|
styleVariables = $element({tag: 'style'});
|
2017-11-15 07:54:19 +00:00
|
|
|
fromLine = 0;
|
|
|
|
toLine = cm.doc.size;
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
cm.on('change', onChange);
|
|
|
|
cm.on('optionChange', onOptionChange);
|
|
|
|
|
|
|
|
// is it possible to avoid flickering?
|
2017-11-08 22:05:22 +00:00
|
|
|
window.addEventListener('load', updateWidgetStyle);
|
2017-11-15 05:32:22 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-11-01 01:11:27 +00:00
|
|
|
|
2017-11-15 06:04:09 +00:00
|
|
|
updateWidgetStyle();
|
2017-11-01 01:11:27 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
function uninit() {
|
2017-11-09 04:44:30 +00:00
|
|
|
initialized = false;
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
widgets.forEach(clearWidget);
|
|
|
|
widgets.length = 0;
|
|
|
|
cm.off('change', onChange);
|
|
|
|
cm.off('optionChange', onOptionChange);
|
2017-11-08 22:05:22 +00:00
|
|
|
window.removeEventListener('load', updateWidgetStyle);
|
2017-11-15 05:32:22 +00:00
|
|
|
chrome.runtime.onMessage.removeListener(onRuntimeMessage);
|
2017-11-15 06:04:09 +00:00
|
|
|
styleVariables.remove();
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onChange(cm, {from, to, origin}) {
|
|
|
|
if (origin === 'appliesTo') {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-15 07:54:19 +00:00
|
|
|
fromLine = Math.min(fromLine === null ? from.line : fromLine, from.line);
|
|
|
|
toLine = Math.max(toLine === null ? to.line : toLine, to.line);
|
2017-11-08 22:14:57 +00:00
|
|
|
debounce(update, THROTTLE_DELAY);
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onOptionChange(cm, option) {
|
|
|
|
if (option === 'theme') {
|
2017-11-08 22:05:22 +00:00
|
|
|
updateWidgetStyle();
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 05:32:22 +00:00
|
|
|
function onRuntimeMessage(msg) {
|
|
|
|
if (msg.style || msg.styles ||
|
|
|
|
msg.prefs && 'disableAll' in msg.prefs ||
|
|
|
|
msg.method === 'styleDeleted') {
|
|
|
|
requestAnimationFrame(updateWidgetStyle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function update() {
|
2017-11-15 07:54:19 +00:00
|
|
|
const changed = {fromLine, toLine};
|
|
|
|
fromLine = Math.max(fromLine || 0, cm.display.viewFrom);
|
|
|
|
toLine = Math.min(toLine === null ? cm.doc.size : toLine, cm.display.viewTo);
|
|
|
|
const visible = {fromLine, toLine};
|
|
|
|
if (fromLine >= cm.display.viewFrom && toLine <= cm.display.viewTo) {
|
|
|
|
cm.operation(doUpdate);
|
|
|
|
}
|
|
|
|
if (changed.fromLine !== visible.fromLine || changed.toLine !== visible.toLine) {
|
|
|
|
setTimeout(updateInvisible, 0, changed, visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateInvisible(changed, visible) {
|
|
|
|
let inOp = false;
|
|
|
|
if (changed.fromLine < visible.fromLine) {
|
|
|
|
fromLine = Math.min(fromLine, changed.fromLine);
|
|
|
|
toLine = Math.min(changed.toLine, visible.fromLine);
|
|
|
|
inOp = true;
|
|
|
|
cm.startOperation();
|
|
|
|
doUpdate();
|
|
|
|
}
|
|
|
|
if (changed.toLine > visible.toLine) {
|
|
|
|
fromLine = Math.max(fromLine, changed.toLine);
|
|
|
|
toLine = Math.max(changed.toLine, visible.toLine);
|
|
|
|
if (!inOp) {
|
|
|
|
inOp = true;
|
|
|
|
cm.startOperation();
|
|
|
|
}
|
|
|
|
cm.operation(doUpdate);
|
|
|
|
}
|
|
|
|
if (inOp) {
|
|
|
|
cm.endOperation();
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 22:05:22 +00:00
|
|
|
function updateWidgetStyle() {
|
2017-11-15 06:04:09 +00:00
|
|
|
const gutterStyle = getComputedStyle(cm.getGutterElement());
|
|
|
|
const borderStyle = gutterStyle.borderRightWidth !== '0px' ?
|
|
|
|
`${gutterStyle.borderRightWidth} ${gutterStyle.borderRightStyle} ${gutterStyle.borderRightColor}` :
|
|
|
|
`1px solid ${gutterStyle.color}`;
|
|
|
|
const id = Date.now();
|
|
|
|
styleVariables.textContent = `
|
|
|
|
.single-editor {
|
|
|
|
--at-background-color-${id}: ${gutterStyle.backgroundColor};
|
|
|
|
--at-border-top-${id}: ${borderStyle};
|
|
|
|
--at-border-bottom-${id}: ${borderStyle};
|
|
|
|
}
|
|
|
|
.applies-to {
|
|
|
|
background-color: var(--at-background-color-${id});
|
|
|
|
border-top: var(--at-border-top-${id});
|
|
|
|
border-bottom: var(--at-border-bottom-${id});
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
document.documentElement.appendChild(styleVariables);
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function doUpdate() {
|
|
|
|
// find which widgets needs to be update
|
|
|
|
// some widgets (lines) might be deleted
|
|
|
|
widgets = widgets.filter(w => w.line.lineNo() !== null);
|
2017-11-15 07:54:19 +00:00
|
|
|
let i = widgets.findIndex(w => w.line.lineNo() > fromLine) - 1;
|
|
|
|
let j = widgets.findIndex(w => w.line.lineNo() > toLine);
|
2017-11-01 01:11:27 +00:00
|
|
|
if (i === -2) {
|
|
|
|
i = widgets.length - 1;
|
|
|
|
}
|
|
|
|
if (j < 0) {
|
|
|
|
j = widgets.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide search range
|
|
|
|
const fromIndex = widgets[i] ? cm.indexFromPos({line: widgets[i].line.lineNo(), ch: 0}) : 0;
|
2017-11-25 14:44:07 +00:00
|
|
|
const toIndex = cm.indexFromPos({line: (widgets[j] ? widgets[j].line.lineNo() : toLine) + 1, ch: 0});
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
// splice
|
2017-11-08 22:16:07 +00:00
|
|
|
i = Math.max(0, i);
|
2017-11-01 01:11:27 +00:00
|
|
|
widgets.splice(i, 0, ...createWidgets(fromIndex, toIndex, widgets.splice(i, j - i)));
|
|
|
|
|
|
|
|
fromLine = null;
|
|
|
|
toLine = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function *createWidgets(start, end, removed) {
|
|
|
|
let i = 0;
|
|
|
|
for (const section of findAppliesTo(start, end)) {
|
|
|
|
while (removed[i] && removed[i].line.lineNo() < section.pos.line) {
|
|
|
|
clearWidget(removed[i++]);
|
|
|
|
}
|
|
|
|
setupMarkers(section);
|
|
|
|
if (removed[i] && removed[i].line.lineNo() === section.pos.line) {
|
|
|
|
// reuse old widget
|
|
|
|
removed[i].section.applies.forEach(apply => {
|
|
|
|
apply.type.mark.clear();
|
|
|
|
apply.value.mark.clear();
|
|
|
|
});
|
|
|
|
removed[i].section = section;
|
|
|
|
const newNode = buildElement(section);
|
|
|
|
removed[i].node.parentNode.replaceChild(newNode, removed[i].node);
|
|
|
|
removed[i].node = newNode;
|
|
|
|
removed[i].changed();
|
|
|
|
yield removed[i];
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// new widget
|
|
|
|
const widget = cm.addLineWidget(section.pos.line, buildElement(section), {
|
|
|
|
coverGutter: true,
|
|
|
|
noHScroll: true,
|
|
|
|
above: true
|
|
|
|
});
|
|
|
|
widget.section = section;
|
|
|
|
yield widget;
|
|
|
|
}
|
|
|
|
removed.slice(i).forEach(clearWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearWidget(widget) {
|
|
|
|
widget.clear();
|
|
|
|
widget.section.applies.forEach(clearApply);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearApply(apply) {
|
|
|
|
apply.type.mark.clear();
|
|
|
|
apply.value.mark.clear();
|
|
|
|
apply.mark.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupMarkers({applies}) {
|
|
|
|
applies.forEach(setupApplyMarkers);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupApplyMarkers(apply) {
|
|
|
|
apply.type.mark = cm.markText(
|
|
|
|
cm.posFromIndex(apply.type.start),
|
|
|
|
cm.posFromIndex(apply.type.end),
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
apply.value.mark = cm.markText(
|
|
|
|
cm.posFromIndex(apply.value.start),
|
|
|
|
cm.posFromIndex(apply.value.end),
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
apply.mark = cm.markText(
|
|
|
|
cm.posFromIndex(apply.start),
|
|
|
|
cm.posFromIndex(apply.end),
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildElement({applies}) {
|
|
|
|
const el = $element({className: 'applies-to', appendChild: [
|
|
|
|
$element({tag: 'label', appendChild: [
|
|
|
|
t('appliesLabel'),
|
|
|
|
// $element({tag: 'svg'})
|
|
|
|
]}),
|
|
|
|
$element({
|
|
|
|
tag: 'ul',
|
|
|
|
className: 'applies-to-list',
|
2017-11-08 22:21:22 +00:00
|
|
|
appendChild: applies.map(makeLi)
|
2017-11-01 01:11:27 +00:00
|
|
|
})
|
|
|
|
]});
|
|
|
|
if (!$('li', el)) {
|
|
|
|
$('ul', el).appendChild($element({
|
|
|
|
tag: 'li',
|
|
|
|
className: 'applies-to-everything',
|
|
|
|
textContent: t('appliesToEverything')
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
|
2017-11-08 22:21:22 +00:00
|
|
|
function makeLi(apply) {
|
2017-11-01 01:11:27 +00:00
|
|
|
const el = $element({tag: 'li', appendChild: makeInput(apply)});
|
|
|
|
el.dataset.type = apply.type.text;
|
|
|
|
el.addEventListener('change', e => {
|
|
|
|
if (e.target.classList.contains('applies-type')) {
|
|
|
|
el.dataset.type = apply.type.text;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeInput(apply) {
|
|
|
|
const typeInput = $element({
|
|
|
|
tag: 'select',
|
|
|
|
className: 'applies-type',
|
|
|
|
appendChild: APPLIES_TYPE.map(([label, value]) => $element({
|
|
|
|
tag: 'option',
|
|
|
|
value: value,
|
|
|
|
textContent: label
|
|
|
|
})),
|
2017-11-08 22:22:55 +00:00
|
|
|
onchange() {
|
|
|
|
applyChange(apply.type, this.value);
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
typeInput.value = apply.type.text;
|
|
|
|
const valueInput = $element({
|
|
|
|
tag: 'input',
|
|
|
|
className: 'applies-value',
|
|
|
|
value: apply.value.text,
|
2017-11-08 22:22:55 +00:00
|
|
|
oninput() {
|
|
|
|
debounce(applyChange, THROTTLE_DELAY, apply.value, this.value);
|
2017-11-01 01:11:27 +00:00
|
|
|
},
|
|
|
|
onfocus: updateRegexpTest
|
|
|
|
});
|
|
|
|
const regexpTestButton = $element({
|
|
|
|
tag: 'button',
|
|
|
|
type: 'button',
|
|
|
|
className: 'applies-to-regexp-test',
|
|
|
|
textContent: t('styleRegexpTestButton'),
|
|
|
|
onclick() {
|
|
|
|
regExpTester.toggle();
|
|
|
|
regExpTester.update([apply.value.text]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const removeButton = $element({
|
|
|
|
tag: 'button',
|
|
|
|
type: 'button',
|
|
|
|
className: 'applies-to-remove',
|
|
|
|
textContent: t('appliesRemove'),
|
2017-11-08 22:55:04 +00:00
|
|
|
onclick() {
|
2017-11-01 01:11:27 +00:00
|
|
|
const i = applies.indexOf(apply);
|
|
|
|
let repl;
|
|
|
|
let from;
|
|
|
|
let to;
|
|
|
|
if (applies.length < 2) {
|
2017-11-08 22:30:48 +00:00
|
|
|
messageBox({
|
2017-11-09 05:09:03 +00:00
|
|
|
contents: chrome.i18n.getMessage('appliesRemoveError'),
|
2017-11-08 22:30:48 +00:00
|
|
|
buttons: [t('confirmClose')]
|
|
|
|
});
|
2017-11-01 01:11:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i === 0) {
|
|
|
|
from = apply.mark.find().from;
|
|
|
|
to = applies[i + 1].mark.find().from;
|
|
|
|
repl = '';
|
|
|
|
} else if (i === applies.length - 1) {
|
|
|
|
from = applies[i - 1].mark.find().to;
|
|
|
|
to = apply.mark.find().to;
|
|
|
|
repl = '';
|
|
|
|
} else {
|
|
|
|
from = applies[i - 1].mark.find().to;
|
|
|
|
to = applies[i + 1].mark.find().from;
|
|
|
|
repl = ', ';
|
|
|
|
}
|
|
|
|
cm.replaceRange(repl, from, to, 'appliesTo');
|
|
|
|
clearApply(apply);
|
2017-11-08 22:55:04 +00:00
|
|
|
this.closest('li').remove();
|
2017-11-01 01:11:27 +00:00
|
|
|
applies.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const addButton = $element({
|
|
|
|
tag: 'button',
|
|
|
|
type: 'button',
|
|
|
|
className: 'applies-to-add',
|
|
|
|
textContent: t('appliesAdd'),
|
2017-11-08 22:55:04 +00:00
|
|
|
onclick() {
|
2017-11-01 01:11:27 +00:00
|
|
|
const i = applies.indexOf(apply);
|
|
|
|
const pos = apply.mark.find().to;
|
|
|
|
const text = `, ${apply.type.text}("")`;
|
|
|
|
cm.replaceRange(text, pos, pos, 'appliesTo');
|
2017-11-08 22:52:52 +00:00
|
|
|
const newApply = createApply(
|
|
|
|
cm.indexFromPos(pos) + 2,
|
|
|
|
apply.type.text,
|
|
|
|
'',
|
|
|
|
true
|
|
|
|
);
|
2017-11-01 01:11:27 +00:00
|
|
|
setupApplyMarkers(newApply);
|
|
|
|
applies.splice(i + 1, 0, newApply);
|
2017-11-08 22:57:10 +00:00
|
|
|
this.closest('li').insertAdjacentElement('afterend', makeLi(newApply));
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return [typeInput, valueInput, regexpTestButton, removeButton, addButton];
|
|
|
|
|
|
|
|
function updateRegexpTest() {
|
|
|
|
if (apply.type.text === 'regexp') {
|
|
|
|
const re = apply.value.text.trim();
|
|
|
|
if (re) {
|
|
|
|
regExpTester.update([re]);
|
|
|
|
} else {
|
|
|
|
regExpTester.update([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyChange(input, newText) {
|
|
|
|
const range = input.mark.find();
|
|
|
|
input.mark.clear();
|
|
|
|
cm.replaceRange(newText, range.from, range.to, 'appliesTo');
|
|
|
|
input.mark = cm.markText(
|
|
|
|
range.from,
|
2017-11-08 22:58:08 +00:00
|
|
|
cm.findPosH(range.from, newText.length, 'char'),
|
2017-11-01 01:11:27 +00:00
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
input.text = newText;
|
|
|
|
|
|
|
|
if (input === apply.type) {
|
|
|
|
const range = apply.mark.find();
|
|
|
|
apply.mark.clear();
|
|
|
|
apply.mark = cm.markText(
|
|
|
|
input.mark.find().from,
|
|
|
|
range.to,
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRegexpTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 22:52:52 +00:00
|
|
|
function createApply(pos, typeText, valueText, isQuoted = false) {
|
|
|
|
const start = pos;
|
|
|
|
const typeStart = start;
|
|
|
|
const typeEnd = typeStart + typeText.length;
|
|
|
|
const valueStart = typeEnd + 1 + Number(isQuoted);
|
|
|
|
const valueEnd = valueStart + valueText.length;
|
|
|
|
const end = valueEnd + Number(isQuoted) + 1;
|
|
|
|
return {
|
|
|
|
start,
|
|
|
|
type: {
|
|
|
|
text: typeText,
|
|
|
|
start: typeStart,
|
|
|
|
end: typeEnd,
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
text: valueText,
|
|
|
|
start: valueStart,
|
|
|
|
end: valueEnd,
|
|
|
|
},
|
|
|
|
end
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function *findAppliesTo(posStart, posEnd) {
|
|
|
|
const text = cm.getValue();
|
|
|
|
const re = /^[\t ]*@-moz-document\s+/mg;
|
2017-11-09 05:50:03 +00:00
|
|
|
const applyRe = /(url|url-prefix|domain|regexp)\(((['"])(?:\\\\|\\\n|\\\3|[^\n])*?\3|[^)\n]*)\)[\s,]*/iyg;
|
2017-11-01 01:11:27 +00:00
|
|
|
let match;
|
2017-11-09 05:50:03 +00:00
|
|
|
re.lastIndex = posStart;
|
2017-11-01 01:11:27 +00:00
|
|
|
while ((match = re.exec(text))) {
|
|
|
|
if (match.index >= posEnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const applies = [];
|
|
|
|
let m;
|
2017-11-09 05:50:03 +00:00
|
|
|
applyRe.lastIndex = re.lastIndex;
|
|
|
|
while ((m = applyRe.exec(text))) {
|
2017-11-08 22:52:52 +00:00
|
|
|
const apply = createApply(
|
2017-11-09 05:50:03 +00:00
|
|
|
m.index,
|
2017-11-08 22:52:52 +00:00
|
|
|
m[1],
|
2017-11-08 23:13:49 +00:00
|
|
|
unquote(m[2]),
|
|
|
|
unquote(m[2]) !== m[2]
|
2017-11-08 22:52:52 +00:00
|
|
|
);
|
2017-11-01 01:11:27 +00:00
|
|
|
applies.push(apply);
|
2017-11-09 05:50:03 +00:00
|
|
|
re.lastIndex = applyRe.lastIndex;
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
2017-11-09 05:50:03 +00:00
|
|
|
yield {
|
|
|
|
pos: cm.posFromIndex(match.index),
|
|
|
|
applies
|
|
|
|
};
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:13:49 +00:00
|
|
|
function unquote(s) {
|
|
|
|
const first = s.charAt(0);
|
|
|
|
return (first === '"' || first === "'") && s.endsWith(first) ? s.slice(1, -1) : s;
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|