2020-11-08 08:12:42 +00:00
|
|
|
/* global
|
|
|
|
$create
|
|
|
|
CodeMirror
|
|
|
|
prefs
|
|
|
|
*/
|
2017-09-11 16:09:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported DirtyReporter */
|
|
|
|
class DirtyReporter {
|
|
|
|
constructor() {
|
|
|
|
this._dirty = new Map();
|
|
|
|
this._onchange = new Set();
|
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
add(obj, value) {
|
|
|
|
const wasDirty = this.isDirty();
|
|
|
|
const saved = this._dirty.get(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
if (!saved) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.set(obj, {type: 'add', newValue: value});
|
2017-09-11 16:09:25 +00:00
|
|
|
} else if (saved.type === 'remove') {
|
|
|
|
if (saved.savedValue === value) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.delete(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
} else {
|
|
|
|
saved.newValue = value;
|
|
|
|
saved.type = 'modify';
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
this.notifyChange(wasDirty);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
remove(obj, value) {
|
|
|
|
const wasDirty = this.isDirty();
|
|
|
|
const saved = this._dirty.get(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
if (!saved) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.set(obj, {type: 'remove', savedValue: value});
|
2017-09-11 16:09:25 +00:00
|
|
|
} else if (saved.type === 'add') {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.delete(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
} else if (saved.type === 'modify') {
|
|
|
|
saved.type = 'remove';
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
this.notifyChange(wasDirty);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
modify(obj, oldValue, newValue) {
|
|
|
|
const wasDirty = this.isDirty();
|
|
|
|
const saved = this._dirty.get(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
if (!saved) {
|
|
|
|
if (oldValue !== newValue) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.set(obj, {type: 'modify', savedValue: oldValue, newValue});
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
} else if (saved.type === 'modify') {
|
|
|
|
if (saved.savedValue === newValue) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.delete(obj);
|
2017-09-11 16:09:25 +00:00
|
|
|
} else {
|
|
|
|
saved.newValue = newValue;
|
|
|
|
}
|
|
|
|
} else if (saved.type === 'add') {
|
|
|
|
saved.newValue = newValue;
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
this.notifyChange(wasDirty);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
clear(obj) {
|
|
|
|
const wasDirty = this.isDirty();
|
2017-10-16 08:05:41 +00:00
|
|
|
if (obj === undefined) {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.clear();
|
2017-10-16 08:05:41 +00:00
|
|
|
} else {
|
2020-11-08 08:12:42 +00:00
|
|
|
this._dirty.delete(obj);
|
2017-10-16 08:05:41 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
this.notifyChange(wasDirty);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
isDirty() {
|
|
|
|
return this._dirty.size > 0;
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
onChange(cb, add = true) {
|
|
|
|
this._onchange[add ? 'add' : 'delete'](cb);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
notifyChange(wasDirty) {
|
|
|
|
if (wasDirty !== this.isDirty()) {
|
|
|
|
this._onchange.forEach(cb => cb());
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
has(key) {
|
|
|
|
return this._dirty.has(key);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported DocFuncMapper */
|
|
|
|
const DocFuncMapper = {
|
|
|
|
TO_CSS: {
|
|
|
|
urls: 'url',
|
2017-12-26 20:39:52 +00:00
|
|
|
urlPrefixes: 'url-prefix',
|
2020-11-08 08:12:42 +00:00
|
|
|
domains: 'domain',
|
|
|
|
regexps: 'regexp',
|
|
|
|
},
|
|
|
|
FROM_CSS: {
|
|
|
|
'url': 'urls',
|
|
|
|
'url-prefix': 'urlPrefixes',
|
|
|
|
'domain': 'domains',
|
|
|
|
'regexp': 'regexps',
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object} section
|
|
|
|
* @param {function(func:string, value:string)} fn
|
|
|
|
*/
|
|
|
|
forEachProp(section, fn) {
|
|
|
|
for (const [propName, func] of Object.entries(DocFuncMapper.TO_CSS)) {
|
|
|
|
const props = section[propName];
|
|
|
|
if (props) props.forEach(value => fn(func, value));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Array<?[type,value]>} funcItems
|
|
|
|
* @param {?Object} [section]
|
|
|
|
* @returns {Object} section
|
|
|
|
*/
|
|
|
|
toSection(funcItems, section = {}) {
|
|
|
|
for (const item of funcItems) {
|
|
|
|
const [func, value] = item || [];
|
|
|
|
const propName = DocFuncMapper.FROM_CSS[func];
|
|
|
|
if (propName) {
|
|
|
|
const props = section[propName] || (section[propName] = []);
|
|
|
|
if (Array.isArray(value)) props.push(...value);
|
|
|
|
else props.push(value);
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
return section;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* exported sectionsToMozFormat */
|
|
|
|
function sectionsToMozFormat(style) {
|
|
|
|
return style.sections.map(section => {
|
|
|
|
const cssFuncs = [];
|
|
|
|
DocFuncMapper.forEachProp(section, (type, value) =>
|
|
|
|
cssFuncs.push(`${type}("${value.replace(/\\/g, '\\\\')}")`));
|
|
|
|
return cssFuncs.length ?
|
|
|
|
`@-moz-document ${cssFuncs.join(', ')} {\n${section.code}\n}` :
|
2017-12-26 20:39:52 +00:00
|
|
|
section.code;
|
|
|
|
}).join('\n\n');
|
|
|
|
}
|
2018-07-22 08:59:56 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported trimCommentLabel */
|
|
|
|
function trimCommentLabel(str, limit = 1000) {
|
|
|
|
// stripping /*** foo ***/ to foo
|
|
|
|
return clipString(str.replace(/^[!-/:;=\s]*|[-#$&(+,./:;<=>\s*]*$/g, ''), limit);
|
|
|
|
}
|
2018-07-22 08:59:56 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported clipString */
|
2018-07-22 08:59:56 +00:00
|
|
|
function clipString(str, limit = 100) {
|
|
|
|
return str.length <= limit ? str : str.substr(0, limit) + '...';
|
|
|
|
}
|
2018-10-01 14:03:17 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported memoize */
|
2018-10-01 14:03:17 +00:00
|
|
|
function memoize(fn) {
|
|
|
|
let cached = false;
|
|
|
|
let result;
|
|
|
|
return (...args) => {
|
|
|
|
if (!cached) {
|
|
|
|
result = fn(...args);
|
|
|
|
cached = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
2020-06-22 16:14:41 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported createHotkeyInput */
|
2020-06-22 16:14:41 +00:00
|
|
|
/**
|
|
|
|
* @param {!string} prefId
|
|
|
|
* @param {?function(isEnter:boolean)} onDone
|
|
|
|
*/
|
|
|
|
function createHotkeyInput(prefId, onDone = () => {}) {
|
|
|
|
return $create('input', {
|
|
|
|
type: 'search',
|
|
|
|
spellcheck: false,
|
|
|
|
value: prefs.get(prefId),
|
|
|
|
onkeydown(event) {
|
|
|
|
const key = CodeMirror.keyName(event);
|
|
|
|
if (key === 'Tab' || key === 'Shift-Tab') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
switch (key) {
|
|
|
|
case 'Enter':
|
|
|
|
if (this.checkValidity()) onDone(true);
|
|
|
|
return;
|
|
|
|
case 'Esc':
|
|
|
|
onDone(false);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// disallow: [Shift?] characters, modifiers-only, [modifiers?] + Esc, Tab, nav keys
|
|
|
|
if (!key || new RegExp('^(' + [
|
|
|
|
'(Back)?Space',
|
|
|
|
'(Shift-)?.', // a single character
|
|
|
|
'(Shift-?|Ctrl-?|Alt-?|Cmd-?){0,2}(|Esc|Tab|(Page)?(Up|Down)|Left|Right|Home|End|Insert|Delete)',
|
|
|
|
].join('|') + ')$', 'i').test(key)) {
|
|
|
|
this.value = key || this.value;
|
|
|
|
this.setCustomValidity('Not allowed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.value = key;
|
|
|
|
this.setCustomValidity('');
|
|
|
|
prefs.set(prefId, key);
|
|
|
|
},
|
|
|
|
oninput() {
|
|
|
|
// fired on pressing "x" to clear the field
|
|
|
|
prefs.set(prefId, '');
|
|
|
|
},
|
|
|
|
onpaste(event) {
|
|
|
|
event.preventDefault();
|
2020-11-18 11:17:15 +00:00
|
|
|
},
|
2020-06-22 16:14:41 +00:00
|
|
|
});
|
|
|
|
}
|