2017-12-17 19:07:37 +00:00
|
|
|
/* global regExpTester debounce messageBox CodeMirror template colorMimicry */
|
2017-11-01 01:11:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function createAppliesToLineWidget(cm) {
|
|
|
|
const THROTTLE_DELAY = 400;
|
2017-12-03 15:49:26 +00:00
|
|
|
let TPL, EVENTS, CLICK_ROUTE;
|
2017-11-01 01:11:27 +00:00
|
|
|
let widgets = [];
|
2017-12-05 17:41:08 +00:00
|
|
|
let fromLine, toLine, actualStyle;
|
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-12-03 15:49:26 +00:00
|
|
|
TPL = {
|
|
|
|
container:
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('div.applies-to', [
|
|
|
|
$create('label', t('appliesLabel')),
|
|
|
|
$create('ul.applies-to-list'),
|
|
|
|
]),
|
2017-12-05 17:41:08 +00:00
|
|
|
listItem: template.appliesTo,
|
2017-12-03 21:12:09 +00:00
|
|
|
appliesToEverything:
|
|
|
|
$create('li.applies-to-everything', t('appliesToEverything')),
|
2017-12-03 15:49:26 +00:00
|
|
|
};
|
|
|
|
|
2017-12-05 18:32:28 +00:00
|
|
|
$('button', TPL.listItem).insertAdjacentElement('beforebegin',
|
2017-12-05 17:41:08 +00:00
|
|
|
$create('button.test-regexp', t('styleRegexpTestButton')));
|
|
|
|
|
2017-12-03 15:49:26 +00:00
|
|
|
CLICK_ROUTE = {
|
2017-12-05 18:52:24 +00:00
|
|
|
'.test-regexp': showRegExpTester,
|
2017-12-03 15:49:26 +00:00
|
|
|
|
|
|
|
'.remove-applies-to': (item, apply) => {
|
|
|
|
const applies = item.closest('.applies-to').__applies;
|
|
|
|
const i = applies.indexOf(apply);
|
|
|
|
let repl;
|
|
|
|
let from;
|
|
|
|
let to;
|
|
|
|
if (applies.length < 2) {
|
|
|
|
messageBox({
|
|
|
|
contents: t('appliesRemoveError'),
|
|
|
|
buttons: [t('confirmClose')]
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
item.remove();
|
|
|
|
applies.splice(i, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
'.add-applies-to': (item, apply) => {
|
|
|
|
const applies = this.closest('.applies-to').__applies;
|
|
|
|
const i = applies.indexOf(apply);
|
|
|
|
const pos = apply.mark.find().to;
|
|
|
|
const text = `, ${apply.type.text}("")`;
|
|
|
|
cm.replaceRange(text, pos, pos, 'appliesTo');
|
|
|
|
const newApply = createApply(
|
|
|
|
cm.indexFromPos(pos) + 2,
|
|
|
|
apply.type.text,
|
|
|
|
'',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
setupApplyMarkers(newApply);
|
|
|
|
applies.splice(i + 1, 0, newApply);
|
|
|
|
item.insertAdjacentElement('afterend', buildChildren(applies, newApply));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
EVENTS = {
|
|
|
|
onchange({target}) {
|
|
|
|
const typeElement = target.closest('.applies-type');
|
|
|
|
if (typeElement) {
|
|
|
|
const item = target.closest('.applies-to-item');
|
|
|
|
const apply = item.__apply;
|
2017-12-05 18:52:24 +00:00
|
|
|
changeItem(item, apply, 'type', typeElement.value);
|
2017-12-03 15:49:26 +00:00
|
|
|
item.dataset.type = apply.type.text;
|
2017-12-05 18:32:28 +00:00
|
|
|
} else {
|
|
|
|
return EVENTS.oninput.apply(this, arguments);
|
2017-12-03 15:49:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
oninput({target}) {
|
|
|
|
if (target.matches('.applies-value')) {
|
2017-12-05 18:32:28 +00:00
|
|
|
const item = target.closest('.applies-to-item');
|
|
|
|
const apply = item.__apply;
|
2017-12-05 18:52:24 +00:00
|
|
|
changeItem(item, apply, 'value', target.value);
|
2017-12-03 15:49:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onclick({target}) {
|
|
|
|
for (const selector in CLICK_ROUTE) {
|
|
|
|
const routed = target.closest(selector);
|
|
|
|
if (routed) {
|
|
|
|
const item = routed.closest('.applies-to-item');
|
|
|
|
CLICK_ROUTE[selector].call(routed, item, item.__apply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-05 17:41:08 +00:00
|
|
|
actualStyle = $create('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);
|
|
|
|
|
2017-11-15 05:32:22 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-11-01 01:11:27 +00:00
|
|
|
|
2017-12-11 05:59:47 +00:00
|
|
|
requestAnimationFrame(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-15 05:32:22 +00:00
|
|
|
chrome.runtime.onMessage.removeListener(onRuntimeMessage);
|
2017-12-05 17:41:08 +00:00
|
|
|
actualStyle.remove();
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 13:04:03 +00:00
|
|
|
function onChange(cm, event) {
|
|
|
|
const {from, to, origin} = event;
|
2017-11-01 01:11:27 +00:00
|
|
|
if (origin === 'appliesTo') {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-26 13:04:03 +00:00
|
|
|
const lastChanged = CodeMirror.changeEnd(event).line;
|
2017-11-15 07:54:19 +00:00
|
|
|
fromLine = Math.min(fromLine === null ? from.line : fromLine, from.line);
|
2017-11-26 13:04:03 +00:00
|
|
|
toLine = Math.max(toLine === null ? lastChanged : toLine, to.line);
|
|
|
|
if (origin === 'setValue') {
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
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);
|
2017-11-26 13:04:03 +00:00
|
|
|
toLine = Math.min(toLine === null ? cm.doc.size : toLine, cm.display.viewTo || toLine);
|
2017-11-15 07:54:19 +00:00
|
|
|
const visible = {fromLine, toLine};
|
2017-11-26 13:04:03 +00:00
|
|
|
if (fromLine >= cm.display.viewFrom && toLine <= (cm.display.viewTo || toLine)) {
|
2017-11-15 07:54:19 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-12-03 15:49:26 +00:00
|
|
|
doUpdate();
|
2017-11-15 07:54:19 +00:00
|
|
|
}
|
|
|
|
if (inOp) {
|
|
|
|
cm.endOperation();
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 22:05:22 +00:00
|
|
|
function updateWidgetStyle() {
|
2017-12-05 17:41:08 +00:00
|
|
|
const MIN_LUMA = .05;
|
|
|
|
const MIN_LUMA_DIFF = .4;
|
|
|
|
const color = {
|
2017-12-17 19:07:37 +00:00
|
|
|
wrapper: colorMimicry.get(cm.display.wrapper),
|
|
|
|
gutter: colorMimicry.get(cm.display.gutters, {
|
2017-12-05 17:41:08 +00:00
|
|
|
bg: 'backgroundColor',
|
|
|
|
border: 'borderRightColor',
|
|
|
|
}),
|
2017-12-17 19:07:37 +00:00
|
|
|
line: colorMimicry.get('.CodeMirror-linenumber', null, cm.display.lineDiv),
|
|
|
|
comment: colorMimicry.get('span.cm-comment', null, cm.display.lineDiv),
|
2017-12-05 17:41:08 +00:00
|
|
|
};
|
|
|
|
const hasBorder =
|
|
|
|
color.gutter.style.borderRightWidth !== '0px' &&
|
|
|
|
!/transparent|\b0\)/g.test(color.gutter.style.borderRightColor);
|
|
|
|
const diff = {
|
|
|
|
wrapper: Math.abs(color.gutter.bgLuma - color.wrapper.foreLuma),
|
|
|
|
border: hasBorder ? Math.abs(color.gutter.bgLuma - color.gutter.borderLuma) : 0,
|
|
|
|
line: Math.abs(color.gutter.bgLuma - color.line.foreLuma),
|
|
|
|
};
|
|
|
|
const preferLine = diff.line > diff.wrapper || diff.line > MIN_LUMA_DIFF;
|
|
|
|
const fore = preferLine ? color.line.fore : color.wrapper.fore;
|
|
|
|
|
|
|
|
const border = fore.replace(/[\d.]+(?=\))/, MIN_LUMA_DIFF / 2);
|
|
|
|
const borderStyleForced = `1px ${hasBorder ? color.gutter.style.borderRightStyle : 'solid'} ${border}`;
|
|
|
|
|
|
|
|
actualStyle.textContent = `
|
2017-11-15 06:04:09 +00:00
|
|
|
.applies-to {
|
2017-12-05 17:41:08 +00:00
|
|
|
background-color: ${color.gutter.bg};
|
|
|
|
border-top: ${borderStyleForced};
|
|
|
|
border-bottom: ${borderStyleForced};
|
|
|
|
}
|
|
|
|
.applies-to label {
|
|
|
|
color: ${fore};
|
|
|
|
}
|
|
|
|
.applies-to input,
|
|
|
|
.applies-to select {
|
|
|
|
background-color: rgba(255, 255, 255, ${
|
|
|
|
Math.max(MIN_LUMA, Math.pow(Math.max(0, color.gutter.bgLuma - MIN_LUMA * 2), 2)).toFixed(2)
|
|
|
|
});
|
|
|
|
border: ${borderStyleForced};
|
|
|
|
transition: none;
|
|
|
|
color: ${fore};
|
|
|
|
}
|
|
|
|
.applies-to .svg-icon.select-arrow {
|
|
|
|
fill: ${fore};
|
|
|
|
transition: none;
|
2017-11-15 06:04:09 +00:00
|
|
|
}
|
2017-12-17 19:07:37 +00:00
|
|
|
.applies-to button {
|
|
|
|
color: ${fore};
|
|
|
|
}
|
2017-11-15 06:04:09 +00:00
|
|
|
`;
|
2017-12-05 17:41:08 +00:00
|
|
|
document.documentElement.appendChild(actualStyle);
|
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
|
2017-11-26 23:39:29 +00:00
|
|
|
const fromPos = {line: widgets[i] ? widgets[i].line.lineNo() : 0, ch: 0};
|
|
|
|
const toPos = {line: widgets[j] ? widgets[j].line.lineNo() : toLine + 1, ch: 0};
|
|
|
|
|
|
|
|
// calc index->pos lookup table
|
|
|
|
let line = 0;
|
|
|
|
let index = 0;
|
|
|
|
let fromIndex, toIndex;
|
|
|
|
const lineIndexes = [index];
|
|
|
|
cm.doc.iter(({text}) => {
|
|
|
|
fromIndex = line === fromPos.line ? index : fromIndex;
|
|
|
|
lineIndexes.push((index += text.length + 1));
|
|
|
|
line++;
|
|
|
|
toIndex = line >= toPos.line ? index : toIndex;
|
|
|
|
return toIndex;
|
|
|
|
});
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
// splice
|
2017-11-08 22:16:07 +00:00
|
|
|
i = Math.max(0, i);
|
2017-11-26 23:39:29 +00:00
|
|
|
widgets.splice(i, 0, ...createWidgets(fromIndex, toIndex, widgets.splice(i, j - i), lineIndexes));
|
2017-11-01 01:11:27 +00:00
|
|
|
|
|
|
|
fromLine = null;
|
|
|
|
toLine = null;
|
|
|
|
}
|
|
|
|
|
2017-11-26 23:39:29 +00:00
|
|
|
function *createWidgets(start, end, removed, lineIndexes) {
|
2017-11-01 01:11:27 +00:00
|
|
|
let i = 0;
|
2017-11-26 22:33:16 +00:00
|
|
|
let itemHeight;
|
2017-11-01 01:11:27 +00:00
|
|
|
for (const section of findAppliesTo(start, end)) {
|
2017-12-03 15:49:26 +00:00
|
|
|
let removedWidget = removed[i];
|
|
|
|
while (removedWidget && removedWidget.line.lineNo() < section.pos.line) {
|
|
|
|
clearWidget(removed[i]);
|
|
|
|
removedWidget = removed[++i];
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
2017-11-26 23:39:29 +00:00
|
|
|
for (const a of section.applies) {
|
|
|
|
setupApplyMarkers(a, lineIndexes);
|
|
|
|
}
|
2017-12-03 15:49:26 +00:00
|
|
|
if (removedWidget && removedWidget.line.lineNo() === section.pos.line) {
|
2017-11-01 01:11:27 +00:00
|
|
|
// reuse old widget
|
2017-12-03 15:49:26 +00:00
|
|
|
removedWidget.section.applies.forEach(apply => {
|
2017-11-01 01:11:27 +00:00
|
|
|
apply.type.mark.clear();
|
|
|
|
apply.value.mark.clear();
|
|
|
|
});
|
2017-12-03 15:49:26 +00:00
|
|
|
removedWidget.section = section;
|
2017-11-01 01:11:27 +00:00
|
|
|
const newNode = buildElement(section);
|
2017-12-03 15:49:26 +00:00
|
|
|
const removedNode = removedWidget.node;
|
|
|
|
if (removedNode.parentNode) {
|
|
|
|
removedNode.parentNode.replaceChild(newNode, removedNode);
|
2017-11-26 22:33:16 +00:00
|
|
|
}
|
2017-12-03 15:49:26 +00:00
|
|
|
removedWidget.node = newNode;
|
|
|
|
removedWidget.changed();
|
|
|
|
yield removedWidget;
|
2017-11-01 01:11:27 +00:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// new widget
|
|
|
|
const widget = cm.addLineWidget(section.pos.line, buildElement(section), {
|
|
|
|
coverGutter: true,
|
|
|
|
noHScroll: true,
|
2017-11-26 22:33:16 +00:00
|
|
|
above: true,
|
|
|
|
height: itemHeight ? section.applies.length * itemHeight : undefined,
|
2017-11-01 01:11:27 +00:00
|
|
|
});
|
|
|
|
widget.section = section;
|
2017-11-26 23:59:44 +00:00
|
|
|
itemHeight = itemHeight || widget.node.offsetHeight / (section.applies.length || 1);
|
2017-11-01 01:11:27 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-26 23:39:29 +00:00
|
|
|
function setupApplyMarkers(apply, lineIndexes) {
|
2017-11-01 01:11:27 +00:00
|
|
|
apply.type.mark = cm.markText(
|
2017-11-26 23:39:29 +00:00
|
|
|
posFromIndex(cm, apply.type.start, lineIndexes),
|
|
|
|
posFromIndex(cm, apply.type.end, lineIndexes),
|
2017-11-01 01:11:27 +00:00
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
apply.value.mark = cm.markText(
|
2017-11-26 23:39:29 +00:00
|
|
|
posFromIndex(cm, apply.value.start, lineIndexes),
|
|
|
|
posFromIndex(cm, apply.value.end, lineIndexes),
|
2017-11-01 01:11:27 +00:00
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
apply.mark = cm.markText(
|
2017-11-26 23:39:29 +00:00
|
|
|
posFromIndex(cm, apply.start, lineIndexes),
|
|
|
|
posFromIndex(cm, apply.end, lineIndexes),
|
2017-11-01 01:11:27 +00:00
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-26 23:39:29 +00:00
|
|
|
function posFromIndex(cm, index, lineIndexes) {
|
|
|
|
if (!lineIndexes) {
|
|
|
|
return cm.posFromIndex(index);
|
|
|
|
}
|
|
|
|
let line = lineIndexes.prev || 0;
|
|
|
|
const prev = lineIndexes[line];
|
|
|
|
const next = lineIndexes[line + 1];
|
|
|
|
if (prev <= index && index < next) {
|
|
|
|
return {line, ch: index - prev};
|
|
|
|
}
|
|
|
|
let a = index < prev ? 0 : line;
|
|
|
|
let b = index < next ? line + 1 : lineIndexes.length - 1;
|
|
|
|
while (a < b - 1) {
|
|
|
|
const mid = (a + b) >> 1;
|
|
|
|
if (lineIndexes[mid] < index) {
|
|
|
|
a = mid;
|
|
|
|
} else {
|
|
|
|
b = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line = lineIndexes[b] > index ? a : b;
|
|
|
|
Object.defineProperty(lineIndexes, 'prev', {value: line, configurable: true});
|
|
|
|
return {line, ch: index - lineIndexes[line]};
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function buildElement({applies}) {
|
2017-12-03 15:49:26 +00:00
|
|
|
const container = TPL.container.cloneNode(true);
|
|
|
|
const list = $('.applies-to-list', container);
|
|
|
|
for (const apply of applies) {
|
|
|
|
list.appendChild(buildChildren(applies, apply));
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
2017-12-03 15:49:26 +00:00
|
|
|
if (!list.children[0]) {
|
|
|
|
list.appendChild(TPL.appliesToEverything.cloneNode(true));
|
|
|
|
}
|
|
|
|
return Object.assign(container, EVENTS, {__applies: applies});
|
|
|
|
}
|
2017-11-16 06:48:13 +00:00
|
|
|
|
2017-12-03 15:49:26 +00:00
|
|
|
function buildChildren(applies, apply) {
|
|
|
|
const el = TPL.listItem.cloneNode(true);
|
|
|
|
el.dataset.type = apply.type.text;
|
|
|
|
el.__apply = apply;
|
|
|
|
$('.applies-type', el).value = apply.type.text;
|
|
|
|
$('.applies-value', el).value = apply.value.text;
|
|
|
|
return el;
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
|
2017-12-05 18:52:24 +00:00
|
|
|
function changeItem(itemElement, apply, part, newText) {
|
2017-12-03 18:34:01 +00:00
|
|
|
if (!apply) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-03 15:49:26 +00:00
|
|
|
part = apply[part];
|
|
|
|
const range = part.mark.find();
|
|
|
|
part.mark.clear();
|
2017-12-05 18:32:28 +00:00
|
|
|
newText = unescapeDoubleslash(newText).replace(/\\/g, '\\\\');
|
2017-12-03 15:49:26 +00:00
|
|
|
cm.replaceRange(newText, range.from, range.to, 'appliesTo');
|
|
|
|
part.mark = cm.markText(
|
|
|
|
range.from,
|
|
|
|
cm.findPosH(range.from, newText.length, 'char'),
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
part.text = newText;
|
|
|
|
|
|
|
|
if (part === apply.type) {
|
|
|
|
const range = apply.mark.find();
|
|
|
|
apply.mark.clear();
|
|
|
|
apply.mark = cm.markText(
|
|
|
|
part.mark.find().from,
|
|
|
|
range.to,
|
|
|
|
{clearWhenEmpty: false}
|
|
|
|
);
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
|
2017-12-05 18:52:24 +00:00
|
|
|
if (apply.type.text === 'regexp' && apply.value.text.trim()) {
|
|
|
|
showRegExpTester(itemElement);
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 22:52:52 +00:00
|
|
|
function createApply(pos, typeText, valueText, isQuoted = false) {
|
2017-12-03 18:34:01 +00:00
|
|
|
typeText = typeText.toLowerCase();
|
2017-11-08 22:52:52 +00:00
|
|
|
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: {
|
2017-12-05 18:32:28 +00:00
|
|
|
text: unescapeDoubleslash(valueText),
|
2017-11-08 22:52:52 +00:00
|
|
|
start: valueStart,
|
|
|
|
end: valueEnd,
|
|
|
|
},
|
|
|
|
end
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function *findAppliesTo(posStart, posEnd) {
|
|
|
|
const text = cm.getValue();
|
2017-12-03 15:49:26 +00:00
|
|
|
const re = /^[\t ]*@-moz-document[\s\n]+/gm;
|
|
|
|
const applyRe = new RegExp([
|
|
|
|
/(?:\/\*[^*]*\*\/[\s\n]*)*/,
|
|
|
|
/(url|url-prefix|domain|regexp)/,
|
|
|
|
/\(((['"])(?:\\\\|\\\n|\\\3|[^\n])*?\3|[^)\n]*)\)\s*(,\s*)?/,
|
|
|
|
].map(rx => rx.source).join(''), 'giy');
|
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
|
|
|
}
|
2017-12-05 18:32:28 +00:00
|
|
|
|
|
|
|
function unescapeDoubleslash(s) {
|
|
|
|
const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/.test(s);
|
|
|
|
return hasSingleEscapes ? s : s.replace(/\\\\/g, '\\');
|
|
|
|
}
|
2017-12-05 18:52:24 +00:00
|
|
|
|
|
|
|
function showRegExpTester(item) {
|
|
|
|
regExpTester.toggle(true);
|
|
|
|
regExpTester.update(
|
|
|
|
item.closest('.applies-to').__applies
|
|
|
|
.filter(a => a.type.text === 'regexp')
|
|
|
|
.map(a => unescapeDoubleslash(a.value.text)));
|
|
|
|
}
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|