2017-07-12 20:44:59 +00:00
|
|
|
/* eslint brace-style: 0, operator-linebreak: 0 */
|
2017-08-18 15:32:46 +00:00
|
|
|
/* global CodeMirror parserlib */
|
2017-11-01 00:40:42 +00:00
|
|
|
/* global loadScript */
|
2017-08-29 11:28:13 +00:00
|
|
|
/* global css_beautify */
|
2017-08-28 05:22:19 +00:00
|
|
|
/* global CSSLint initLint linterConfig updateLintReport renderLintReport updateLinter */
|
2017-09-11 16:09:25 +00:00
|
|
|
/* global mozParser createSourceEditor */
|
2017-11-09 07:56:29 +00:00
|
|
|
/* global closeCurrentTab regExpTester messageBox */
|
2017-11-23 05:18:55 +00:00
|
|
|
/* global initColorpicker */
|
2017-07-12 20:44:59 +00:00
|
|
|
'use strict';
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2017-07-12 20:44:59 +00:00
|
|
|
let styleId = null;
|
2017-08-20 18:56:18 +00:00
|
|
|
// only the actually dirty items here
|
|
|
|
let dirty = {};
|
|
|
|
// array of all CodeMirror instances
|
|
|
|
const editors = [];
|
2017-07-12 20:44:59 +00:00
|
|
|
let saveSizeOnClose;
|
2017-08-20 18:56:18 +00:00
|
|
|
// use browser history back when 'back to manage' is clicked
|
|
|
|
let useHistoryBack;
|
2015-01-30 17:05:06 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
// direct & reverse mapping of @-moz-document keywords and internal property names
|
2017-07-12 20:44:59 +00:00
|
|
|
const propertyToCss = {urls: 'url', urlPrefixes: 'url-prefix', domains: 'domain', regexps: 'regexp'};
|
|
|
|
const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'domains', 'regexp': 'regexps'};
|
2015-03-21 13:24:45 +00:00
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
let editor;
|
|
|
|
|
2017-11-26 13:04:03 +00:00
|
|
|
Promise.all([
|
|
|
|
initStyleData().then(style => {
|
|
|
|
styleId = style.id;
|
|
|
|
sessionStorage.justEditedStyleId = styleId;
|
|
|
|
// we set "usercss" class on <html> when <body> is empty
|
|
|
|
// so there'll be no flickering of the elements that depend on it
|
|
|
|
if (isUsercss(style)) {
|
|
|
|
document.documentElement.classList.add('usercss');
|
|
|
|
}
|
|
|
|
// strip URL parameters when invoked for a non-existent id
|
|
|
|
if (!styleId) {
|
|
|
|
history.replaceState({}, document.title, location.pathname);
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}),
|
|
|
|
onDOMready(),
|
|
|
|
onBackgroundReady(),
|
|
|
|
])
|
|
|
|
.then(([style]) => Promise.all([
|
|
|
|
style,
|
|
|
|
initColorpicker(),
|
|
|
|
initCollapsibles(),
|
|
|
|
initHooksCommon(),
|
|
|
|
]))
|
|
|
|
.then(([style]) => {
|
|
|
|
initCodeMirror();
|
|
|
|
|
|
|
|
const usercss = isUsercss(style);
|
|
|
|
$('#heading').textContent = t(styleId ? 'editStyleHeading' : 'addStyleTitle');
|
|
|
|
$('#name').placeholder = t(usercss ? 'usercssEditorNamePlaceholder' : 'styleMissingName');
|
|
|
|
$('#name').title = usercss ? t('usercssReplaceTemplateName') : '';
|
|
|
|
|
|
|
|
if (usercss) {
|
|
|
|
editor = createSourceEditor(style);
|
|
|
|
} else {
|
|
|
|
initWithSectionStyle({style});
|
|
|
|
}
|
|
|
|
});
|
2017-04-12 17:24:05 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
// make querySelectorAll enumeration code readable
|
2017-07-16 16:49:31 +00:00
|
|
|
['forEach', 'some', 'indexOf', 'map'].forEach(method => {
|
2017-07-12 20:44:59 +00:00
|
|
|
NodeList.prototype[method] = Array.prototype[method];
|
2015-03-21 13:24:45 +00:00
|
|
|
});
|
|
|
|
|
2017-07-12 20:44:59 +00:00
|
|
|
// eslint-disable-next-line no-extend-native
|
2017-08-20 18:56:18 +00:00
|
|
|
Array.prototype.rotate = function (amount) {
|
|
|
|
// negative amount == rotate left
|
2017-07-12 20:44:59 +00:00
|
|
|
const r = this.slice(-amount, this.length);
|
2017-07-12 19:50:13 +00:00
|
|
|
Array.prototype.push.apply(r, this.slice(0, this.length - r.length));
|
|
|
|
return r;
|
2017-03-26 02:30:59 +00:00
|
|
|
};
|
2015-07-11 14:35:04 +00:00
|
|
|
|
2017-07-12 20:44:59 +00:00
|
|
|
// eslint-disable-next-line no-extend-native
|
2017-07-16 16:49:31 +00:00
|
|
|
Object.defineProperty(Array.prototype, 'last', {get: function () { return this[this.length - 1]; }});
|
2015-07-13 17:44:46 +00:00
|
|
|
|
2017-04-17 18:06:00 +00:00
|
|
|
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
|
|
|
|
new MutationObserver((mutations, observer) => {
|
2017-08-21 00:05:08 +00:00
|
|
|
const themeElement = $('#cm-theme');
|
2017-07-12 19:50:13 +00:00
|
|
|
if (themeElement) {
|
2017-07-16 18:02:00 +00:00
|
|
|
themeElement.href = prefs.get('editor.theme') === 'default' ? ''
|
2017-07-12 20:44:59 +00:00
|
|
|
: 'vendor/codemirror/theme/' + prefs.get('editor.theme') + '.css';
|
2017-07-12 19:50:13 +00:00
|
|
|
observer.disconnect();
|
|
|
|
}
|
2017-04-17 18:06:00 +00:00
|
|
|
}).observe(document, {subtree: true, childList: true});
|
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
getCodeMirrorThemes();
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
// reroute handling to nearest editor when keypress resolves to one of these commands
|
2017-07-12 20:44:59 +00:00
|
|
|
const hotkeyRerouter = {
|
2017-07-12 19:50:13 +00:00
|
|
|
commands: {
|
|
|
|
save: true, jumpToLine: true, nextEditor: true, prevEditor: true,
|
|
|
|
find: true, findNext: true, findPrev: true, replace: true, replaceAll: true,
|
|
|
|
toggleStyle: true,
|
2017-11-22 01:36:26 +00:00
|
|
|
colorpicker: true,
|
2017-07-12 19:50:13 +00:00
|
|
|
},
|
2017-07-16 16:49:31 +00:00
|
|
|
setState: enable => {
|
|
|
|
setTimeout(() => {
|
2017-07-12 20:44:59 +00:00
|
|
|
document[(enable ? 'add' : 'remove') + 'EventListener']('keydown', hotkeyRerouter.eventHandler);
|
2017-07-12 19:50:13 +00:00
|
|
|
}, 0);
|
|
|
|
},
|
2017-07-16 16:49:31 +00:00
|
|
|
eventHandler: event => {
|
2017-07-12 20:44:59 +00:00
|
|
|
const keyName = CodeMirror.keyName(event);
|
|
|
|
if (
|
2017-07-16 18:02:00 +00:00
|
|
|
CodeMirror.lookupKey(keyName, CodeMirror.getOption('keyMap'), handleCommand) === 'handled' ||
|
|
|
|
CodeMirror.lookupKey(keyName, CodeMirror.defaults.extraKeys, handleCommand) === 'handled'
|
2017-07-12 20:44:59 +00:00
|
|
|
) {
|
2017-07-12 19:50:13 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
function handleCommand(command) {
|
|
|
|
if (hotkeyRerouter.commands[command] === true) {
|
|
|
|
CodeMirror.commands[command](getEditorInSight(event.target));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function onChange(event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const node = event.target;
|
|
|
|
if ('savedValue' in node) {
|
|
|
|
const currentValue = node.type === 'checkbox' ? node.checked : node.value;
|
2017-07-12 19:50:13 +00:00
|
|
|
setCleanItem(node, node.savedValue === currentValue);
|
|
|
|
} else {
|
|
|
|
// the manually added section's applies-to is dirty only when the value is non-empty
|
2017-07-16 18:02:00 +00:00
|
|
|
setCleanItem(node, node.localName !== 'input' || !node.value.trim());
|
2017-08-20 18:56:18 +00:00
|
|
|
// only valid when actually saved
|
|
|
|
delete node.savedValue;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
updateTitle();
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-15 19:41:36 +00:00
|
|
|
// Set .dirty on stylesheet contributors that have changed
|
2015-03-21 13:24:45 +00:00
|
|
|
function setDirtyClass(node, isDirty) {
|
2017-07-12 20:44:59 +00:00
|
|
|
node.classList.toggle('dirty', isDirty);
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanItem(node, isClean) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!node.id) {
|
|
|
|
node.id = Date.now().toString(32).substr(-6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isClean) {
|
|
|
|
delete dirty[node.id];
|
|
|
|
// code sections have .CodeMirror property
|
|
|
|
if (node.CodeMirror) {
|
|
|
|
node.savedValue = node.CodeMirror.changeGeneration();
|
|
|
|
} else {
|
2017-07-12 20:44:59 +00:00
|
|
|
node.savedValue = node.type === 'checkbox' ? node.checked : node.value;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dirty[node.id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDirtyClass(node, !isClean);
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
2015-03-16 20:47:56 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function isCleanGlobal() {
|
2017-07-16 18:02:00 +00:00
|
|
|
const clean = Object.keys(dirty).length === 0;
|
2017-07-12 19:50:13 +00:00
|
|
|
setDirtyClass(document.body, !clean);
|
2017-08-21 00:05:08 +00:00
|
|
|
// let saveBtn = $('#save-button')
|
2017-07-12 20:44:59 +00:00
|
|
|
// if (clean){
|
|
|
|
// //saveBtn.removeAttribute('disabled');
|
|
|
|
// }else{
|
|
|
|
// //saveBtn.setAttribute('disabled', true);
|
|
|
|
// }
|
2017-07-12 19:50:13 +00:00
|
|
|
return clean;
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanGlobal() {
|
2017-08-21 00:05:08 +00:00
|
|
|
$$('#header, #sections > div').forEach(setCleanSection);
|
2017-08-20 18:56:18 +00:00
|
|
|
// forget the dirty applies-to ids from a deleted section after the style was saved
|
|
|
|
dirty = {};
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
2015-03-15 19:41:36 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanSection(section) {
|
2017-08-21 00:05:08 +00:00
|
|
|
$$('.style-contributor', section).forEach(node => { setCleanItem(node, true); });
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
// #header section has no codemirror
|
2017-07-12 20:44:59 +00:00
|
|
|
const cm = section.CodeMirror;
|
2017-07-12 19:50:13 +00:00
|
|
|
if (cm) {
|
|
|
|
section.savedValue = cm.changeGeneration();
|
2017-08-31 19:25:05 +00:00
|
|
|
updateTitle();
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
function initCodeMirror() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const CM = CodeMirror;
|
2017-08-20 14:06:17 +00:00
|
|
|
// lint.js is not loaded initially
|
2017-09-24 08:54:21 +00:00
|
|
|
CM.defaults.lint = linterConfig.getForCodeMirror();
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
// additional commands
|
|
|
|
CM.commands.jumpToLine = jumpToLine;
|
2017-09-01 09:23:49 +00:00
|
|
|
CM.commands.nextEditor = cm => nextPrevEditor(cm, 1);
|
|
|
|
CM.commands.prevEditor = cm => nextPrevEditor(cm, -1);
|
2017-07-12 19:50:13 +00:00
|
|
|
CM.commands.save = save;
|
|
|
|
CM.commands.toggleStyle = toggleStyle;
|
|
|
|
|
|
|
|
// user option values
|
2017-07-16 16:49:31 +00:00
|
|
|
CM.getOption = o => CodeMirror.defaults[o];
|
|
|
|
CM.setOption = (o, v) => {
|
2017-07-12 19:50:13 +00:00
|
|
|
CodeMirror.defaults[o] = v;
|
2017-09-12 19:45:33 +00:00
|
|
|
if (editors.length > 4 && (o === 'theme' || o === 'lineWrapping')) {
|
|
|
|
throttleSetOption({key: o, value: v, index: 0});
|
|
|
|
return;
|
|
|
|
}
|
2017-09-12 11:34:04 +00:00
|
|
|
editors.forEach(editor => {
|
2017-07-12 19:50:13 +00:00
|
|
|
editor.setOption(o, v);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-16 16:49:31 +00:00
|
|
|
CM.prototype.getSection = function () {
|
2017-07-12 19:50:13 +00:00
|
|
|
return this.display.wrapper.parentNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
// initialize global editor controls
|
2017-07-19 08:31:09 +00:00
|
|
|
function optionsFromArray(parent, options) {
|
2017-07-19 16:18:34 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const opt of options) {
|
|
|
|
fragment.appendChild($element({tag: 'option', textContent: opt}));
|
|
|
|
}
|
|
|
|
parent.appendChild(fragment);
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
// no need to escape the period in the id
|
|
|
|
const themeControl = $('#editor.theme');
|
2017-07-12 19:50:13 +00:00
|
|
|
const themeList = localStorage.codeMirrorThemes;
|
|
|
|
if (themeList) {
|
2017-07-19 08:31:09 +00:00
|
|
|
optionsFromArray(themeControl, themeList.split(/\s+/));
|
2017-07-12 19:50:13 +00:00
|
|
|
} else {
|
|
|
|
// Chrome is starting up and shows our edit.html, but the background page isn't loaded yet
|
2017-07-12 20:44:59 +00:00
|
|
|
const theme = prefs.get('editor.theme');
|
2017-07-19 08:31:09 +00:00
|
|
|
optionsFromArray(themeControl, [theme === 'default' ? t('defaultTheme') : theme]);
|
2017-07-12 19:50:13 +00:00
|
|
|
getCodeMirrorThemes().then(() => {
|
|
|
|
const themes = (localStorage.codeMirrorThemes || '').split(/\s+/);
|
2017-07-19 08:31:09 +00:00
|
|
|
optionsFromArray(themeControl, themes);
|
2017-07-12 19:50:13 +00:00
|
|
|
themeControl.selectedIndex = Math.max(0, themes.indexOf(theme));
|
|
|
|
});
|
|
|
|
}
|
2017-07-19 08:31:09 +00:00
|
|
|
optionsFromArray($('#editor.keyMap'), Object.keys(CM.keyMap).sort());
|
2017-08-21 00:05:08 +00:00
|
|
|
$('#options').addEventListener('change', acmeEventListener, false);
|
2017-07-12 19:50:13 +00:00
|
|
|
setupLivePrefs();
|
|
|
|
|
|
|
|
hotkeyRerouter.setState(true);
|
2017-09-12 19:45:33 +00:00
|
|
|
|
|
|
|
const THROTTLE_AFTER_MS = 100;
|
|
|
|
const THROTTLE_SHOW_PROGRESS_AFTER_MS = 100;
|
|
|
|
|
|
|
|
function throttleSetOption({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
index,
|
|
|
|
timeStart = performance.now(),
|
|
|
|
cmStart = editors.lastActive || editors[0],
|
|
|
|
editorsCopy = editors.slice(),
|
|
|
|
progress,
|
|
|
|
}) {
|
|
|
|
if (index === 0) {
|
|
|
|
if (!cmStart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmStart.setOption(key, value);
|
|
|
|
}
|
|
|
|
const t0 = performance.now();
|
|
|
|
const total = editorsCopy.length;
|
|
|
|
while (index < total) {
|
|
|
|
const cm = editorsCopy[index++];
|
|
|
|
if (cm === cmStart ||
|
|
|
|
cm !== editors[index] && !editors.includes(cm)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cm.setOption(key, value);
|
|
|
|
if (performance.now() - t0 > THROTTLE_AFTER_MS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index >= total) {
|
|
|
|
if (progress) {
|
|
|
|
progress.remove();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!progress &&
|
|
|
|
index < total / 2 &&
|
|
|
|
t0 - timeStart > THROTTLE_SHOW_PROGRESS_AFTER_MS) {
|
|
|
|
let option = $('#editor.' + key);
|
|
|
|
if (option) {
|
|
|
|
if (option.type === 'checkbox') {
|
|
|
|
option = (option.labels || [])[0] || option.nextElementSibling || option;
|
|
|
|
}
|
|
|
|
progress = document.body.appendChild($element({
|
|
|
|
className: 'set-option-progress',
|
|
|
|
targetElement: option,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (progress) {
|
|
|
|
const optionBounds = progress.targetElement.getBoundingClientRect();
|
|
|
|
const bounds = {
|
|
|
|
top: optionBounds.top + window.scrollY + 1,
|
|
|
|
left: optionBounds.left + window.scrollX + 1,
|
|
|
|
width: (optionBounds.width - 2) * index / total | 0,
|
|
|
|
height: optionBounds.height - 2,
|
|
|
|
};
|
|
|
|
const style = progress.style;
|
|
|
|
for (const prop in bounds) {
|
|
|
|
if (bounds[prop] !== parseFloat(style[prop])) {
|
|
|
|
style[prop] = bounds[prop] + 'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(throttleSetOption, 0, {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
index,
|
|
|
|
timeStart,
|
|
|
|
cmStart,
|
|
|
|
editorsCopy,
|
|
|
|
progress,
|
|
|
|
});
|
|
|
|
}
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function acmeEventListener(event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const el = event.target;
|
2017-08-28 21:07:43 +00:00
|
|
|
let option = el.id.replace(/^editor\./, '');
|
2017-07-12 20:44:59 +00:00
|
|
|
//console.log('acmeEventListener heard %s on %s', event.type, el.id);
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!option) {
|
2017-07-12 20:44:59 +00:00
|
|
|
console.error('acmeEventListener: no "cm_option" %O', el);
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
let value = el.type === 'checkbox' ? el.checked : el.value;
|
2017-07-12 19:50:13 +00:00
|
|
|
switch (option) {
|
2017-07-12 20:44:59 +00:00
|
|
|
case 'tabSize':
|
2017-11-27 09:45:04 +00:00
|
|
|
value = Number(value);
|
|
|
|
CodeMirror.setOption('indentUnit', value);
|
2017-07-12 19:50:13 +00:00
|
|
|
break;
|
2017-07-14 09:42:55 +00:00
|
|
|
case 'theme': {
|
2017-08-21 00:05:08 +00:00
|
|
|
const themeLink = $('#cm-theme');
|
2017-07-12 20:44:59 +00:00
|
|
|
// use non-localized 'default' internally
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!value || value === 'default' || value === t('defaultTheme')) {
|
2017-07-12 20:44:59 +00:00
|
|
|
value = 'default';
|
2017-07-16 18:02:00 +00:00
|
|
|
if (prefs.get(el.id) !== value) {
|
2017-07-12 19:50:13 +00:00
|
|
|
prefs.set(el.id, value);
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
themeLink.href = '';
|
2017-07-12 19:50:13 +00:00
|
|
|
el.selectedIndex = 0;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-14 08:33:59 +00:00
|
|
|
const url = chrome.runtime.getURL('vendor/codemirror/theme/' + value + '.css');
|
2017-08-20 18:56:18 +00:00
|
|
|
if (themeLink.href === url) {
|
|
|
|
// preloaded in initCodeMirror()
|
2017-07-12 19:50:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// avoid flicker: wait for the second stylesheet to load, then apply the theme
|
2017-07-19 13:07:32 +00:00
|
|
|
document.head.appendChild($element({
|
|
|
|
tag: 'link',
|
|
|
|
id: 'cm-theme2',
|
|
|
|
rel: 'stylesheet',
|
|
|
|
href: url
|
|
|
|
}));
|
|
|
|
setTimeout(() => {
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
themeLink.remove();
|
2017-07-19 16:18:34 +00:00
|
|
|
$('#cm-theme2').id = 'cm-theme';
|
2017-07-19 13:07:32 +00:00
|
|
|
}, 100);
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
2017-07-14 09:42:55 +00:00
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
case 'autocompleteOnTyping':
|
2017-09-12 11:34:04 +00:00
|
|
|
editors.forEach(cm => setupAutocomplete(cm, el.checked));
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
2017-11-16 23:36:05 +00:00
|
|
|
case 'autoCloseBrackets':
|
|
|
|
Promise.resolve(value && loadScript('/vendor/codemirror/addon/edit/closebrackets.js')).then(() => {
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
});
|
|
|
|
return;
|
2017-07-12 20:44:59 +00:00
|
|
|
case 'matchHighlight':
|
2017-07-12 19:50:13 +00:00
|
|
|
switch (value) {
|
|
|
|
case 'token':
|
|
|
|
case 'selection':
|
|
|
|
document.body.dataset[option] = value;
|
2017-07-16 18:02:00 +00:00
|
|
|
value = {showToken: value === 'token' && /[#.\-\w]/, annotateScrollbar: true};
|
2017-07-12 19:50:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = null;
|
|
|
|
}
|
2017-08-28 21:07:43 +00:00
|
|
|
option = 'highlightSelectionMatches';
|
2017-08-15 19:13:58 +00:00
|
|
|
break;
|
2017-11-15 12:59:24 +00:00
|
|
|
case 'colorpicker':
|
|
|
|
return;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
CodeMirror.setOption(option, value);
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 13:28:50 +00:00
|
|
|
function setupCodeMirror(sectionDiv, code, index) {
|
|
|
|
const cm = CodeMirror(wrapper => {
|
|
|
|
$('.code-label', sectionDiv).insertAdjacentElement('afterend', wrapper);
|
|
|
|
}, {
|
|
|
|
value: code,
|
|
|
|
lint: null,
|
|
|
|
});
|
2017-07-12 19:50:13 +00:00
|
|
|
const wrapper = cm.display.wrapper;
|
|
|
|
|
2017-09-01 08:16:57 +00:00
|
|
|
cm.on('changes', indicateCodeChangeDebounced);
|
2017-07-12 19:50:13 +00:00
|
|
|
if (prefs.get('editor.autocompleteOnTyping')) {
|
2017-09-11 16:09:25 +00:00
|
|
|
setupAutocomplete(cm);
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-09-01 09:23:49 +00:00
|
|
|
wrapper.addEventListener('keydown', event => nextPrevEditorOnKeydown(cm, event), true);
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.on('blur', () => {
|
|
|
|
editors.lastActive = cm;
|
|
|
|
hotkeyRerouter.setState(true);
|
|
|
|
setTimeout(() => {
|
|
|
|
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cm.on('focus', () => {
|
|
|
|
hotkeyRerouter.setState(false);
|
|
|
|
wrapper.classList.add('CodeMirror-active');
|
|
|
|
});
|
2017-08-29 23:26:47 +00:00
|
|
|
cm.on('paste', (cm, event) => {
|
|
|
|
const text = event.clipboardData.getData('text') || '';
|
|
|
|
if (
|
|
|
|
text.includes('@-moz-document') &&
|
|
|
|
text.replace(/\/\*[\s\S]*?\*\//g, '')
|
2017-10-08 14:08:47 +00:00
|
|
|
.match(/@-moz-document[\s\r\n]+(url|url-prefix|domain|regexp)\(/)
|
2017-08-29 23:26:47 +00:00
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
fromMozillaFormat();
|
|
|
|
$('#help-popup').codebox.setValue(text);
|
2017-08-30 07:49:42 +00:00
|
|
|
$('#help-popup').codebox.clearHistory();
|
2017-08-29 23:26:47 +00:00
|
|
|
$('#help-popup').codebox.markClean();
|
|
|
|
}
|
2017-08-29 20:53:18 +00:00
|
|
|
if (editors.length === 1) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (cm.display.sizer.clientHeight > cm.display.wrapper.clientHeight) {
|
|
|
|
maximizeCodeHeight.stats = null;
|
|
|
|
maximizeCodeHeight(cm.getSection(), true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-07-31 16:39:10 +00:00
|
|
|
if (!FIREFOX) {
|
|
|
|
cm.on('mousedown', (cm, event) => toggleContextMenuDelete.call(cm, event));
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-09-13 12:28:19 +00:00
|
|
|
wrapper.classList.add('resize-grip-enabled');
|
2017-07-12 19:50:13 +00:00
|
|
|
let lastClickTime = 0;
|
|
|
|
const resizeGrip = wrapper.appendChild(template.resizeGrip.cloneNode(true));
|
|
|
|
resizeGrip.onmousedown = event => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (event.button !== 0) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
if (Date.now() - lastClickTime < 500) {
|
|
|
|
lastClickTime = 0;
|
|
|
|
toggleSectionHeight(cm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastClickTime = Date.now();
|
2017-07-12 20:44:59 +00:00
|
|
|
const minHeight = cm.defaultTextHeight() +
|
2017-08-20 18:56:18 +00:00
|
|
|
/* .CodeMirror-lines padding */
|
|
|
|
cm.display.lineDiv.offsetParent.offsetTop +
|
|
|
|
/* borders */
|
|
|
|
wrapper.offsetHeight - wrapper.clientHeight;
|
2017-07-12 19:50:13 +00:00
|
|
|
wrapper.style.pointerEvents = 'none';
|
|
|
|
document.body.style.cursor = 's-resize';
|
|
|
|
function resize(e) {
|
|
|
|
const cmPageY = wrapper.getBoundingClientRect().top + window.scrollY;
|
|
|
|
const height = Math.max(minHeight, e.pageY - cmPageY);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (height !== wrapper.clientHeight) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.setSize(null, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
document.addEventListener('mousemove', resize);
|
|
|
|
document.addEventListener('mouseup', function resizeStop() {
|
|
|
|
document.removeEventListener('mouseup', resizeStop);
|
|
|
|
document.removeEventListener('mousemove', resize);
|
|
|
|
wrapper.style.pointerEvents = '';
|
|
|
|
document.body.style.cursor = '';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
editors.splice(index || editors.length, 0, cm);
|
|
|
|
return cm;
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function indicateCodeChange(cm) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const section = cm.getSection();
|
2017-07-12 19:50:13 +00:00
|
|
|
setCleanItem(section, cm.isClean(section.savedValue));
|
|
|
|
updateTitle();
|
2017-08-20 14:06:17 +00:00
|
|
|
updateLintReportIfEnabled(cm);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 08:16:57 +00:00
|
|
|
function indicateCodeChangeDebounced(cm, ...args) {
|
|
|
|
clearTimeout(cm.state.stylusOnChangeTimer);
|
|
|
|
cm.state.stylusOnChangeTimer = setTimeout(indicateCodeChange, 200, cm, ...args);
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
function getSectionForChild(e) {
|
2017-07-12 20:44:59 +00:00
|
|
|
return e.closest('#sections > div');
|
2015-07-16 19:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSections() {
|
2017-08-21 00:05:08 +00:00
|
|
|
return $$('#sections > div');
|
2015-04-08 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 11:43:43 +00:00
|
|
|
// remind Chrome to repaint a previously invisible editor box by toggling any element's transform
|
|
|
|
// this bug is present in some versions of Chrome (v37-40 or something)
|
2017-07-16 16:49:31 +00:00
|
|
|
document.addEventListener('scroll', () => {
|
2017-08-21 00:05:08 +00:00
|
|
|
const style = $('#name').style;
|
2017-07-12 20:44:59 +00:00
|
|
|
style.webkitTransform = style.webkitTransform ? '' : 'scale(1)';
|
2015-05-04 11:43:43 +00:00
|
|
|
});
|
|
|
|
|
2015-04-26 20:03:39 +00:00
|
|
|
// Shift-Ctrl-Wheel scrolls entire page even when mouse is over a code editor
|
2017-07-16 16:49:31 +00:00
|
|
|
document.addEventListener('wheel', event => {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (event.shiftKey && event.ctrlKey && !event.altKey && !event.metaKey) {
|
|
|
|
// Chrome scrolls horizontally when Shift is pressed but on some PCs this might be different
|
|
|
|
window.scrollBy(0, event.deltaX || event.deltaY);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-04-26 20:03:39 +00:00
|
|
|
});
|
|
|
|
|
2017-11-25 13:24:07 +00:00
|
|
|
chrome.windows && queryTabs({currentWindow: true}).then(tabs => {
|
2017-07-12 20:44:59 +00:00
|
|
|
const windowId = tabs[0].windowId;
|
|
|
|
if (prefs.get('openEditInWindow')) {
|
2017-07-16 17:01:39 +00:00
|
|
|
if (
|
2017-08-18 14:06:39 +00:00
|
|
|
/true/.test(sessionStorage.saveSizeOnClose) &&
|
2017-07-16 17:01:39 +00:00
|
|
|
'left' in prefs.get('windowPosition', {}) &&
|
|
|
|
!isWindowMaximized()
|
|
|
|
) {
|
2017-07-12 19:50:13 +00:00
|
|
|
// window was reopened via Ctrl-Shift-T etc.
|
|
|
|
chrome.windows.update(windowId, prefs.get('windowPosition'));
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (tabs.length === 1 && window.history.length === 1) {
|
2017-07-16 16:49:31 +00:00
|
|
|
chrome.windows.getAll(windows => {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (windows.length > 1) {
|
2017-07-12 20:44:59 +00:00
|
|
|
sessionStorageHash('saveSizeOnClose').set(windowId, true);
|
2017-07-12 19:50:13 +00:00
|
|
|
saveSizeOnClose = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-07-12 20:44:59 +00:00
|
|
|
saveSizeOnClose = sessionStorageHash('saveSizeOnClose').value[windowId];
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 12:55:32 +00:00
|
|
|
});
|
|
|
|
|
2017-08-27 13:49:59 +00:00
|
|
|
getOwnTab().then(tab => {
|
|
|
|
const ownTabId = tab.id;
|
|
|
|
useHistoryBack = sessionStorageHash('manageStylesHistory').value[ownTabId] === location.href;
|
2017-11-25 13:24:07 +00:00
|
|
|
if (!chrome.windows) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-27 13:49:59 +00:00
|
|
|
// When an edit page gets attached or detached, remember its state
|
|
|
|
// so we can do the same to the next one to open.
|
2017-08-27 14:36:20 +00:00
|
|
|
chrome.tabs.onAttached.addListener((tabId, info) => {
|
|
|
|
if (tabId !== ownTabId) {
|
|
|
|
return;
|
2017-08-27 13:49:59 +00:00
|
|
|
}
|
2017-08-27 14:36:20 +00:00
|
|
|
if (info.newPosition !== 0) {
|
|
|
|
prefs.set('openEditInWindow', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.windows.get(info.newWindowId, {populate: true}, win => {
|
|
|
|
// If there's only one tab in this window, it's been dragged to new window
|
|
|
|
const openEditInWindow = win.tabs.length === 1;
|
|
|
|
if (openEditInWindow && FIREFOX) {
|
|
|
|
// FF-only because Chrome retardedly resets the size during dragging
|
|
|
|
chrome.windows.update(info.newWindowId, prefs.get('windowPosition'));
|
|
|
|
}
|
|
|
|
prefs.set('openEditInWindow', openEditInWindow);
|
|
|
|
});
|
2017-08-27 13:49:59 +00:00
|
|
|
});
|
2015-06-10 12:55:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function goBackToManage(event) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (useHistoryBack) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
history.back();
|
|
|
|
}
|
2015-05-27 19:02:36 +00:00
|
|
|
}
|
2015-03-13 22:26:26 +00:00
|
|
|
|
2017-06-06 13:44:16 +00:00
|
|
|
function isWindowMaximized() {
|
2017-08-27 11:56:04 +00:00
|
|
|
return (
|
|
|
|
window.screenX <= 0 &&
|
|
|
|
window.screenY <= 0 &&
|
|
|
|
window.outerWidth >= screen.availWidth &&
|
|
|
|
window.outerHeight >= screen.availHeight &&
|
|
|
|
|
|
|
|
window.screenX > -10 &&
|
|
|
|
window.screenY > -10 &&
|
|
|
|
window.outerWidth < screen.availWidth + 10 &&
|
|
|
|
window.outerHeight < screen.availHeight + 10
|
|
|
|
);
|
2017-06-06 13:44:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 11:56:04 +00:00
|
|
|
function rememberWindowSize() {
|
2017-08-27 16:41:09 +00:00
|
|
|
if (
|
|
|
|
document.visibilityState === 'visible' &&
|
|
|
|
prefs.get('openEditInWindow') &&
|
|
|
|
!isWindowMaximized()
|
|
|
|
) {
|
2017-07-12 20:44:59 +00:00
|
|
|
prefs.set('windowPosition', {
|
2017-08-27 11:56:04 +00:00
|
|
|
left: window.screenX,
|
|
|
|
top: window.screenY,
|
|
|
|
width: window.outerWidth,
|
|
|
|
height: window.outerHeight,
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-27 11:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.onbeforeunload = () => {
|
|
|
|
if (saveSizeOnClose) {
|
|
|
|
rememberWindowSize();
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
document.activeElement.blur();
|
2017-10-08 16:43:00 +00:00
|
|
|
if (isClean()) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-20 14:06:17 +00:00
|
|
|
updateLintReportIfEnabled(null, 0);
|
2017-08-28 12:19:45 +00:00
|
|
|
// neither confirm() nor custom messages work in modern browsers but just in case
|
|
|
|
return t('styleChangesNotSaved');
|
2015-03-03 00:58:04 +00:00
|
|
|
|
2017-11-09 04:47:37 +00:00
|
|
|
function isClean() {
|
|
|
|
if (editor) {
|
|
|
|
return !editor.isDirty();
|
|
|
|
} else {
|
|
|
|
return isCleanGlobal();
|
|
|
|
}
|
2017-10-08 16:43:00 +00:00
|
|
|
}
|
2017-11-09 04:47:37 +00:00
|
|
|
};
|
2017-10-08 16:43:00 +00:00
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
function addAppliesTo(list, name, value) {
|
2017-08-21 00:05:08 +00:00
|
|
|
const showingEverything = $('.applies-to-everything', list) !== null;
|
2017-07-12 20:44:59 +00:00
|
|
|
// blow away 'Everything' if it's there
|
2017-07-12 19:50:13 +00:00
|
|
|
if (showingEverything) {
|
|
|
|
list.removeChild(list.firstChild);
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
let e;
|
2017-11-14 05:55:01 +00:00
|
|
|
if (name) {
|
2017-07-12 19:50:13 +00:00
|
|
|
e = template.appliesTo.cloneNode(true);
|
2017-08-21 00:05:08 +00:00
|
|
|
$('[name=applies-type]', e).value = name;
|
|
|
|
$('[name=applies-value]', e).value = value;
|
|
|
|
$('.remove-applies-to', e).addEventListener('click', removeAppliesTo, false);
|
2017-07-12 19:50:13 +00:00
|
|
|
} else if (showingEverything || list.hasChildNodes()) {
|
|
|
|
e = template.appliesTo.cloneNode(true);
|
|
|
|
if (list.hasChildNodes()) {
|
2017-08-21 00:05:08 +00:00
|
|
|
$('[name=applies-type]', e).value = $('li:last-child [name="applies-type"]', list).value;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.remove-applies-to', e).addEventListener('click', removeAppliesTo, false);
|
2017-07-12 19:50:13 +00:00
|
|
|
} else {
|
|
|
|
e = template.appliesToEverything.cloneNode(true);
|
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.add-applies-to', e).addEventListener('click', function () {
|
2017-07-12 20:44:59 +00:00
|
|
|
addAppliesTo(this.parentNode.parentNode);
|
|
|
|
}, false);
|
2017-07-12 19:50:13 +00:00
|
|
|
list.appendChild(e);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 10:17:59 +00:00
|
|
|
function addSection(event, section) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const div = template.section.cloneNode(true);
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.applies-to-help', div).addEventListener('click', showAppliesToHelp, false);
|
|
|
|
$('.remove-section', div).addEventListener('click', removeSection, false);
|
|
|
|
$('.add-section', div).addEventListener('click', addSection, false);
|
|
|
|
$('.beautify-section', div).addEventListener('click', beautify);
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-11-22 13:28:50 +00:00
|
|
|
const code = (section || {}).code || '';
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const appliesTo = $('.applies-to-list', div);
|
2017-07-12 20:44:59 +00:00
|
|
|
let appliesToAdded = false;
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
if (section) {
|
2017-07-14 09:02:04 +00:00
|
|
|
for (const i in propertyToCss) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (section[i]) {
|
2017-07-16 16:49:31 +00:00
|
|
|
section[i].forEach(url => {
|
2017-07-12 19:50:13 +00:00
|
|
|
addAppliesTo(appliesTo, propertyToCss[i], url);
|
|
|
|
appliesToAdded = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!appliesToAdded) {
|
|
|
|
addAppliesTo(appliesTo);
|
|
|
|
}
|
|
|
|
|
2017-07-12 20:44:59 +00:00
|
|
|
appliesTo.addEventListener('change', onChange);
|
|
|
|
appliesTo.addEventListener('input', onChange);
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
toggleTestRegExpVisibility();
|
|
|
|
appliesTo.addEventListener('change', toggleTestRegExpVisibility);
|
2017-10-07 15:40:37 +00:00
|
|
|
$('.test-regexp', div).onclick = () => {
|
|
|
|
regExpTester.toggle();
|
|
|
|
regExpTester.update(getRegExps());
|
|
|
|
};
|
|
|
|
|
|
|
|
function getRegExps() {
|
|
|
|
return [...appliesTo.children]
|
|
|
|
.map(item =>
|
|
|
|
!item.matches('.applies-to-everything') &&
|
|
|
|
$('.applies-type', item).value === 'regexp' &&
|
|
|
|
$('.applies-value', item).value.trim()
|
|
|
|
)
|
|
|
|
.filter(item => item);
|
|
|
|
}
|
|
|
|
|
2017-07-12 19:50:13 +00:00
|
|
|
function toggleTestRegExpVisibility() {
|
2017-10-07 15:40:37 +00:00
|
|
|
const show = getRegExps().length > 0;
|
2017-07-12 19:50:13 +00:00
|
|
|
div.classList.toggle('has-regexp', show);
|
|
|
|
appliesTo.oninput = appliesTo.oninput || show && (event => {
|
2017-07-16 17:01:39 +00:00
|
|
|
if (
|
|
|
|
event.target.matches('.applies-value') &&
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.applies-type', event.target.parentElement).value === 'regexp'
|
2017-07-16 17:01:39 +00:00
|
|
|
) {
|
2017-10-07 15:40:37 +00:00
|
|
|
regExpTester.update(getRegExps());
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const sections = $('#sections');
|
2017-07-12 20:44:59 +00:00
|
|
|
let cm;
|
2017-07-12 19:50:13 +00:00
|
|
|
if (event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const clickedSection = getSectionForChild(event.target);
|
2017-07-12 19:50:13 +00:00
|
|
|
sections.insertBefore(div, clickedSection.nextElementSibling);
|
2017-07-12 20:44:59 +00:00
|
|
|
const newIndex = getSections().indexOf(clickedSection) + 1;
|
2017-11-22 13:28:50 +00:00
|
|
|
cm = setupCodeMirror(div, code, newIndex);
|
2017-07-12 19:50:13 +00:00
|
|
|
makeSectionVisible(cm);
|
|
|
|
renderLintReport();
|
2017-11-22 13:28:50 +00:00
|
|
|
cm.focus();
|
2017-07-12 19:50:13 +00:00
|
|
|
} else {
|
|
|
|
sections.appendChild(div);
|
2017-11-22 13:28:50 +00:00
|
|
|
cm = setupCodeMirror(div, code);
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
div.CodeMirror = cm;
|
|
|
|
setCleanSection(div);
|
|
|
|
return div;
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAppliesTo(event) {
|
2017-07-14 08:46:10 +00:00
|
|
|
const appliesTo = event.target.parentNode;
|
|
|
|
const appliesToList = appliesTo.parentNode;
|
2017-07-12 19:50:13 +00:00
|
|
|
removeAreaAndSetDirty(appliesTo);
|
|
|
|
if (!appliesToList.hasChildNodes()) {
|
|
|
|
addAppliesTo(appliesToList);
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSection(event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const section = getSectionForChild(event.target);
|
|
|
|
const cm = section.CodeMirror;
|
2017-07-12 19:50:13 +00:00
|
|
|
removeAreaAndSetDirty(section);
|
|
|
|
editors.splice(editors.indexOf(cm), 1);
|
|
|
|
renderLintReport();
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAreaAndSetDirty(area) {
|
2017-08-21 00:05:08 +00:00
|
|
|
const contributors = $$('.style-contributor', area);
|
2017-07-12 20:44:59 +00:00
|
|
|
if (!contributors.length) {
|
2017-07-12 19:50:13 +00:00
|
|
|
setCleanItem(area, false);
|
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
contributors.some(node => {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (node.savedValue) {
|
|
|
|
// it's a saved section, so make it dirty and stop the enumeration
|
|
|
|
setCleanItem(area, false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// it's an empty section, so undirty the applies-to items,
|
|
|
|
// otherwise orphaned ids would keep the style dirty
|
|
|
|
setCleanItem(node, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateTitle();
|
|
|
|
area.parentNode.removeChild(area);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 20:07:35 +00:00
|
|
|
function makeSectionVisible(cm) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const section = cm.getSection();
|
|
|
|
const bounds = section.getBoundingClientRect();
|
|
|
|
if (
|
|
|
|
(bounds.bottom > window.innerHeight && bounds.top > 0) ||
|
|
|
|
(bounds.top < 0 && bounds.bottom < window.innerHeight)
|
|
|
|
) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (bounds.top < 0) {
|
|
|
|
window.scrollBy(0, bounds.top - 1);
|
|
|
|
} else {
|
|
|
|
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 00:08:04 +00:00
|
|
|
function setupGlobalSearch() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const originalCommand = {
|
2017-07-12 19:50:13 +00:00
|
|
|
find: CodeMirror.commands.find,
|
|
|
|
findNext: CodeMirror.commands.findNext,
|
|
|
|
findPrev: CodeMirror.commands.findPrev,
|
|
|
|
replace: CodeMirror.commands.replace
|
|
|
|
};
|
2017-07-12 20:44:59 +00:00
|
|
|
const originalOpenDialog = CodeMirror.prototype.openDialog;
|
|
|
|
const originalOpenConfirm = CodeMirror.prototype.openConfirm;
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-08-20 18:56:18 +00:00
|
|
|
// cm.state.search for last used 'find'
|
|
|
|
let curState;
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-08-20 18:56:18 +00:00
|
|
|
function shouldIgnoreCase(query) {
|
|
|
|
// treat all-lowercase non-regexp queries as case-insensitive
|
2017-07-16 18:02:00 +00:00
|
|
|
return typeof query === 'string' && query === query.toLowerCase();
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateState(cm, newState) {
|
|
|
|
if (!newState) {
|
|
|
|
if (cm.state.search) {
|
|
|
|
return cm.state.search;
|
|
|
|
}
|
|
|
|
if (!curState) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
newState = curState;
|
|
|
|
}
|
|
|
|
cm.state.search = {
|
|
|
|
query: newState.query,
|
|
|
|
overlay: newState.overlay,
|
|
|
|
annotate: cm.showMatchesOnScrollbar(newState.query, shouldIgnoreCase(newState.query))
|
2017-07-12 20:44:59 +00:00
|
|
|
};
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.addOverlay(newState.overlay);
|
|
|
|
return cm.state.search;
|
|
|
|
}
|
|
|
|
|
2017-07-19 09:13:21 +00:00
|
|
|
// overrides the original openDialog with a clone of the provided template
|
2017-07-12 19:50:13 +00:00
|
|
|
function customizeOpenDialog(cm, template, callback) {
|
2017-07-16 16:49:31 +00:00
|
|
|
cm.openDialog = (tmpl, cb, opt) => {
|
2017-07-12 19:50:13 +00:00
|
|
|
// invoke 'callback' and bind 'this' to the original callback
|
2017-07-19 09:13:21 +00:00
|
|
|
originalOpenDialog.call(cm, template.cloneNode(true), callback.bind(cb), opt);
|
2017-07-12 19:50:13 +00:00
|
|
|
};
|
2017-07-16 16:49:31 +00:00
|
|
|
setTimeout(() => { cm.openDialog = originalOpenDialog; }, 0);
|
2017-07-12 19:50:13 +00:00
|
|
|
refocusMinidialog(cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusClosestCM(activeCM) {
|
|
|
|
editors.lastActive = activeCM;
|
2017-07-12 20:44:59 +00:00
|
|
|
const cm = getEditorInSight();
|
2017-07-16 18:02:00 +00:00
|
|
|
if (cm !== activeCM) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.focus();
|
|
|
|
}
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function find(activeCM) {
|
|
|
|
activeCM = focusClosestCM(activeCM);
|
2017-07-16 16:49:31 +00:00
|
|
|
customizeOpenDialog(activeCM, template.find, function (query) {
|
2017-07-12 19:50:13 +00:00
|
|
|
this(query);
|
|
|
|
curState = activeCM.state.search;
|
2017-07-16 18:02:00 +00:00
|
|
|
if (editors.length === 1 || !curState.query) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
editors.forEach(cm => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (cm !== activeCM) {
|
2017-07-12 20:44:59 +00:00
|
|
|
cm.execCommand('clearSearch');
|
2017-07-12 19:50:13 +00:00
|
|
|
updateState(cm, curState);
|
|
|
|
}
|
|
|
|
});
|
2017-07-16 18:02:00 +00:00
|
|
|
if (CodeMirror.cmpPos(curState.posFrom, curState.posTo) === 0) {
|
2017-07-12 19:50:13 +00:00
|
|
|
findNext(activeCM);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
originalCommand.find(activeCM);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNext(activeCM, reverse) {
|
2017-07-12 20:44:59 +00:00
|
|
|
let state = updateState(activeCM);
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!state || !state.query) {
|
|
|
|
find(activeCM);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
let pos = activeCM.getCursor(reverse ? 'from' : 'to');
|
2017-08-20 18:56:18 +00:00
|
|
|
// clear the selection, don't move the cursor
|
|
|
|
activeCM.setSelection(activeCM.getCursor());
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-07-16 18:02:00 +00:00
|
|
|
const rxQuery = typeof state.query === 'object'
|
2017-07-12 20:44:59 +00:00
|
|
|
? state.query : stringAsRegExp(state.query, shouldIgnoreCase(state.query) ? 'i' : '');
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-07-16 17:01:39 +00:00
|
|
|
if (
|
|
|
|
document.activeElement &&
|
2017-07-16 18:02:00 +00:00
|
|
|
document.activeElement.name === 'applies-value' &&
|
2017-07-16 17:01:39 +00:00
|
|
|
searchAppliesTo(activeCM)
|
|
|
|
) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-14 08:46:10 +00:00
|
|
|
let cm = activeCM;
|
|
|
|
for (let i = 0; i < editors.length; i++) {
|
2017-07-12 19:50:13 +00:00
|
|
|
state = updateState(cm);
|
|
|
|
if (!cm.hasFocus()) {
|
|
|
|
pos = reverse ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(0, 0);
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
const searchCursor = cm.getSearchCursor(state.query, pos, shouldIgnoreCase(state.query));
|
2017-07-12 19:50:13 +00:00
|
|
|
if (searchCursor.find(reverse)) {
|
|
|
|
if (editors.length > 1) {
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.focus();
|
|
|
|
}
|
|
|
|
// speedup the original findNext
|
|
|
|
state.posFrom = reverse ? searchCursor.to() : searchCursor.from();
|
|
|
|
state.posTo = CodeMirror.Pos(state.posFrom.line, state.posFrom.ch);
|
2017-07-12 20:44:59 +00:00
|
|
|
originalCommand[reverse ? 'findPrev' : 'findNext'](cm);
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
} else if (!reverse && searchAppliesTo(cm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cm = editors[(editors.indexOf(cm) + (reverse ? -1 + editors.length : 1)) % editors.length];
|
|
|
|
if (reverse && searchAppliesTo(cm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// nothing found so far, so call the original search with wrap-around
|
2017-07-12 20:44:59 +00:00
|
|
|
originalCommand[reverse ? 'findPrev' : 'findNext'](activeCM);
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
function searchAppliesTo(cm) {
|
2017-08-21 00:05:08 +00:00
|
|
|
let inputs = $$('.applies-value', cm.getSection());
|
2017-07-12 19:50:13 +00:00
|
|
|
if (reverse) {
|
|
|
|
inputs = inputs.reverse();
|
|
|
|
}
|
|
|
|
inputs.splice(0, inputs.indexOf(document.activeElement) + 1);
|
2017-07-16 16:49:31 +00:00
|
|
|
return inputs.some(input => {
|
2017-07-12 20:44:59 +00:00
|
|
|
const match = rxQuery.exec(input.value);
|
2017-07-12 19:50:13 +00:00
|
|
|
if (match) {
|
|
|
|
input.focus();
|
2017-07-12 20:44:59 +00:00
|
|
|
const end = match.index + match[0].length;
|
2017-07-12 19:50:13 +00:00
|
|
|
// scroll selected part into view in long inputs,
|
|
|
|
// works only outside of current event handlers chain, hence timeout=0
|
2017-07-16 16:49:31 +00:00
|
|
|
setTimeout(() => {
|
2017-07-12 19:50:13 +00:00
|
|
|
input.setSelectionRange(end, end);
|
2017-07-12 20:44:59 +00:00
|
|
|
input.setSelectionRange(match.index, end);
|
2017-07-12 19:50:13 +00:00
|
|
|
}, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findPrev(cm) {
|
|
|
|
findNext(cm, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace(activeCM, all) {
|
2017-07-14 08:46:10 +00:00
|
|
|
let queue;
|
|
|
|
let query;
|
|
|
|
let replacement;
|
2017-07-12 19:50:13 +00:00
|
|
|
activeCM = focusClosestCM(activeCM);
|
2017-08-30 13:20:49 +00:00
|
|
|
customizeOpenDialog(activeCM, template[all ? 'replaceAll' : 'replace'], function (txt) {
|
2017-07-12 19:50:13 +00:00
|
|
|
query = txt;
|
2017-08-30 13:20:49 +00:00
|
|
|
customizeOpenDialog(activeCM, template.replaceWith, function (txt) {
|
2017-07-12 19:50:13 +00:00
|
|
|
replacement = txt;
|
|
|
|
queue = editors.rotate(-editors.indexOf(activeCM));
|
2017-07-12 20:44:59 +00:00
|
|
|
if (all) {
|
|
|
|
editors.forEach(doReplace);
|
|
|
|
} else {
|
|
|
|
doReplace();
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
|
|
|
this(query);
|
|
|
|
});
|
|
|
|
originalCommand.replace(activeCM, all);
|
|
|
|
|
|
|
|
function doReplace() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const cm = queue.shift();
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!cm) {
|
|
|
|
if (!all) {
|
|
|
|
editors.lastActive.focus();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// hide the first two dialogs (replace, replaceWith)
|
2017-07-16 19:40:13 +00:00
|
|
|
cm.openDialog = (tmpl, callback) => {
|
|
|
|
cm.openDialog = (tmpl, callback) => {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.openDialog = originalOpenDialog;
|
|
|
|
if (all) {
|
|
|
|
callback(replacement);
|
|
|
|
} else {
|
|
|
|
doConfirm(cm);
|
|
|
|
callback(replacement);
|
2017-08-21 00:05:08 +00:00
|
|
|
if (!$('.CodeMirror-dialog', cm.getWrapperElement())) {
|
2017-07-12 19:50:13 +00:00
|
|
|
// no dialog == nothing found in the current CM, move to the next
|
|
|
|
doReplace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
callback(query);
|
|
|
|
};
|
|
|
|
originalCommand.replace(cm, all);
|
|
|
|
}
|
|
|
|
function doConfirm(cm) {
|
2017-07-12 20:44:59 +00:00
|
|
|
let wrapAround = false;
|
|
|
|
const origPos = cm.getCursor();
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.openConfirm = function overrideConfirm(tmpl, callbacks, opt) {
|
2017-07-16 16:49:31 +00:00
|
|
|
const ovrCallbacks = callbacks.map(callback => () => {
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.openConfirm = overrideConfirm;
|
|
|
|
setTimeout(() => { cm.openConfirm = originalOpenConfirm; }, 0);
|
|
|
|
|
|
|
|
const pos = cm.getCursor();
|
|
|
|
callback();
|
|
|
|
const cmp = CodeMirror.cmpPos(cm.getCursor(), pos);
|
|
|
|
wrapAround |= cmp <= 0;
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const dlg = $('.CodeMirror-dialog', cm.getWrapperElement());
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!dlg || cmp === 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) {
|
2017-07-16 16:49:31 +00:00
|
|
|
if (dlg) {
|
|
|
|
dlg.remove();
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
doReplace();
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
2017-07-19 09:13:21 +00:00
|
|
|
originalOpenConfirm.call(cm, template.replaceConfirm.cloneNode(true), ovrCallbacks, opt);
|
2017-07-12 19:50:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceAll(cm) {
|
|
|
|
replace(cm, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeMirror.commands.find = find;
|
|
|
|
CodeMirror.commands.findNext = findNext;
|
|
|
|
CodeMirror.commands.findPrev = findPrev;
|
|
|
|
CodeMirror.commands.replace = replace;
|
|
|
|
CodeMirror.commands.replaceAll = replaceAll;
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
function jumpToLine(cm) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const cur = cm.getCursor();
|
2017-07-12 19:50:13 +00:00
|
|
|
refocusMinidialog(cm);
|
2017-07-19 09:13:21 +00:00
|
|
|
cm.openDialog(template.jumpToLine.cloneNode(true), str => {
|
2017-07-12 20:44:59 +00:00
|
|
|
const m = str.match(/^\s*(\d+)(?:\s*:\s*(\d+))?\s*$/);
|
2017-07-12 19:50:13 +00:00
|
|
|
if (m) {
|
|
|
|
cm.setCursor(m[1] - 1, m[2] ? m[2] - 1 : cur.ch);
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
}, {value: cur.line + 1});
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 07:56:49 +00:00
|
|
|
function toggleStyle() {
|
2017-11-08 23:19:22 +00:00
|
|
|
if (editor) {
|
|
|
|
editor.toggleStyle();
|
|
|
|
} else {
|
2017-09-12 11:37:06 +00:00
|
|
|
toggleSectionStyle();
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:37:06 +00:00
|
|
|
function toggleSectionStyle() {
|
2017-07-12 19:50:13 +00:00
|
|
|
$('#enabled').checked = !$('#enabled').checked;
|
|
|
|
save();
|
2017-06-06 07:56:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 06:09:29 +00:00
|
|
|
function toggleSectionHeight(cm) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (cm.state.toggleHeightSaved) {
|
|
|
|
// restore previous size
|
|
|
|
cm.setSize(null, cm.state.toggleHeightSaved);
|
|
|
|
cm.state.toggleHeightSaved = 0;
|
|
|
|
} else {
|
|
|
|
// maximize
|
|
|
|
const wrapper = cm.display.wrapper;
|
|
|
|
const allBounds = $('#sections').getBoundingClientRect();
|
|
|
|
const pageExtrasHeight = allBounds.top + window.scrollY +
|
|
|
|
parseFloat(getComputedStyle($('#sections')).paddingBottom);
|
|
|
|
const sectionExtrasHeight = cm.getSection().clientHeight - wrapper.offsetHeight;
|
|
|
|
cm.state.toggleHeightSaved = wrapper.clientHeight;
|
|
|
|
cm.setSize(null, window.innerHeight - sectionExtrasHeight - pageExtrasHeight);
|
|
|
|
const bounds = cm.getSection().getBoundingClientRect();
|
|
|
|
if (bounds.top < 0 || bounds.bottom > window.innerHeight) {
|
|
|
|
window.scrollBy(0, bounds.top);
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
function setupAutocomplete(cm, enable = true) {
|
|
|
|
const onOff = enable ? 'on' : 'off';
|
2017-09-17 23:33:07 +00:00
|
|
|
cm[onOff]('changes', autocompleteOnTyping);
|
2017-09-11 16:09:25 +00:00
|
|
|
cm[onOff]('pick', autocompletePicked);
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:13:49 +00:00
|
|
|
function autocompleteOnTyping(cm, [info], debounced) {
|
2017-07-12 20:44:59 +00:00
|
|
|
if (
|
|
|
|
cm.state.completionActive ||
|
|
|
|
info.origin && !info.origin.includes('input') ||
|
|
|
|
!info.text.last
|
|
|
|
) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cm.state.autocompletePicked) {
|
|
|
|
cm.state.autocompletePicked = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!debounced) {
|
2017-08-31 18:13:49 +00:00
|
|
|
debounce(autocompleteOnTyping, 100, cm, [info], true);
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-25 01:30:19 +00:00
|
|
|
if (info.text.last.match(/[-a-z!]+$/i)) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.state.autocompletePicked = false;
|
|
|
|
cm.options.hintOptions.completeSingle = false;
|
|
|
|
cm.execCommand('autocomplete');
|
|
|
|
setTimeout(() => {
|
|
|
|
cm.options.hintOptions.completeSingle = true;
|
|
|
|
});
|
|
|
|
}
|
2017-06-18 12:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function autocompletePicked(cm) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.state.autocompletePicked = true;
|
2017-06-18 12:20:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 18:20:05 +00:00
|
|
|
function refocusMinidialog(cm) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const section = cm.getSection();
|
2017-08-21 00:05:08 +00:00
|
|
|
if (!$('.CodeMirror-dialog', section)) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// close the currently opened minidialog
|
|
|
|
cm.focus();
|
|
|
|
// make sure to focus the input in newly opened minidialog
|
2017-07-16 16:49:31 +00:00
|
|
|
setTimeout(() => {
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.CodeMirror-dialog', section).focus();
|
2017-07-12 19:50:13 +00:00
|
|
|
}, 0);
|
2015-10-29 18:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
function nextPrevEditor(cm, direction) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.focus();
|
2017-09-01 09:23:49 +00:00
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextPrevEditorOnKeydown(cm, event) {
|
|
|
|
const key = event.which;
|
|
|
|
if (key < 37 || key > 40 || event.shiftKey || event.altKey || event.metaKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {line, ch} = cm.getCursor();
|
|
|
|
switch (key) {
|
|
|
|
case 37:
|
|
|
|
// arrow Left
|
|
|
|
if (line || ch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fallthrough to arrow Up
|
|
|
|
case 38:
|
|
|
|
// arrow Up
|
|
|
|
if (line > 0 || cm === editors[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
cm = nextPrevEditor(cm, -1);
|
|
|
|
cm.setCursor(cm.doc.size - 1, key === 37 ? 1e20 : ch);
|
|
|
|
break;
|
|
|
|
case 39:
|
|
|
|
// arrow Right
|
|
|
|
if (line < cm.doc.size - 1 || ch < cm.getLine(line).length - 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fallthrough to arrow Down
|
|
|
|
case 40:
|
|
|
|
// arrow Down
|
|
|
|
if (line < cm.doc.size - 1 || cm === editors.last) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
cm = nextPrevEditor(cm, 1);
|
|
|
|
cm.setCursor(0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const animation = (cm.getSection().firstElementChild.getAnimations() || [])[0];
|
|
|
|
if (animation) {
|
|
|
|
animation.playbackRate = -1;
|
|
|
|
animation.currentTime = 2000;
|
|
|
|
animation.play();
|
|
|
|
}
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
function getEditorInSight(nearbyElement) {
|
2017-07-12 19:50:13 +00:00
|
|
|
// priority: 1. associated CM for applies-to element 2. last active if visible 3. first visible
|
2017-07-12 20:44:59 +00:00
|
|
|
let cm;
|
|
|
|
if (nearbyElement && nearbyElement.className.indexOf('applies-') >= 0) {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm = getSectionForChild(nearbyElement).CodeMirror;
|
|
|
|
} else {
|
|
|
|
cm = editors.lastActive;
|
|
|
|
}
|
2017-08-30 17:44:41 +00:00
|
|
|
// closest editor should have at least 2 lines visible
|
|
|
|
const lineHeight = editors[0].defaultTextHeight();
|
|
|
|
const scrollY = window.scrollY;
|
|
|
|
const windowBottom = scrollY + window.innerHeight - 2 * lineHeight;
|
|
|
|
const allSectionsContainerTop = scrollY + $('#sections').getBoundingClientRect().top;
|
|
|
|
const distances = [];
|
|
|
|
const alreadyInView = cm && offscreenDistance(null, cm) === 0;
|
|
|
|
return alreadyInView ? cm : findClosest();
|
|
|
|
|
|
|
|
function offscreenDistance(index, cm) {
|
|
|
|
if (index >= 0 && distances[index] !== undefined) {
|
|
|
|
return distances[index];
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-30 17:44:41 +00:00
|
|
|
const section = (cm || editors[index]).getSection();
|
|
|
|
const top = allSectionsContainerTop + section.offsetTop;
|
|
|
|
if (top < scrollY + lineHeight) {
|
|
|
|
return Math.max(0, scrollY - top - lineHeight);
|
|
|
|
}
|
|
|
|
if (top < windowBottom) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-30 17:44:41 +00:00
|
|
|
const distance = top - windowBottom + section.offsetHeight;
|
|
|
|
if (index >= 0) {
|
|
|
|
distances[index] = distance;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-30 17:44:41 +00:00
|
|
|
return distance;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 17:44:41 +00:00
|
|
|
function findClosest() {
|
2017-08-30 17:54:03 +00:00
|
|
|
const last = editors.length - 1;
|
2017-08-30 17:44:41 +00:00
|
|
|
let a = 0;
|
2017-08-30 17:54:03 +00:00
|
|
|
let b = last;
|
2017-08-30 17:44:41 +00:00
|
|
|
let c;
|
|
|
|
let cm, distance;
|
|
|
|
while (a < b - 1) {
|
|
|
|
c = (a + b) / 2 | 0;
|
|
|
|
distance = offscreenDistance(c);
|
|
|
|
if (!distance || !c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const distancePrev = offscreenDistance(c - 1);
|
2017-08-30 17:54:03 +00:00
|
|
|
const distanceNext = c < last ? offscreenDistance(c + 1) : 1e20;
|
2017-08-30 17:44:41 +00:00
|
|
|
if (distancePrev <= distance && distance <= distanceNext) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-30 17:44:41 +00:00
|
|
|
while (b && offscreenDistance(b - 1) <= offscreenDistance(b)) {
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
cm = editors[b];
|
|
|
|
if (distances[b] > 0) {
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
}
|
|
|
|
return cm;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 16:24:53 +00:00
|
|
|
function beautify(event) {
|
2017-09-12 15:19:16 +00:00
|
|
|
loadScript('/vendor-overwrites/beautify/beautify-css-mod.js')
|
|
|
|
.then(() => {
|
2017-08-29 11:28:13 +00:00
|
|
|
if (!window.css_beautify && window.exports) {
|
|
|
|
window.css_beautify = window.exports.css_beautify;
|
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
})
|
|
|
|
.then(doBeautify);
|
2017-08-13 02:28:44 +00:00
|
|
|
|
2017-07-12 19:50:13 +00:00
|
|
|
function doBeautify() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const tabs = prefs.get('editor.indentWithTabs');
|
|
|
|
const options = prefs.get('editor.beautify');
|
|
|
|
options.indent_size = tabs ? 1 : prefs.get('editor.tabSize');
|
|
|
|
options.indent_char = tabs ? '\t' : ' ';
|
|
|
|
|
|
|
|
const section = getSectionForChild(event.target);
|
2017-09-12 11:34:04 +00:00
|
|
|
const scope = section ? [section.CodeMirror] : editors;
|
2017-07-12 20:44:59 +00:00
|
|
|
|
|
|
|
showHelp(t('styleBeautify'), '<div class="beautify-options">' +
|
|
|
|
optionHtml('.selector1,', 'selector_separator_newline') +
|
|
|
|
optionHtml('.selector2,', 'newline_before_open_brace') +
|
|
|
|
optionHtml('{', 'newline_after_open_brace') +
|
|
|
|
optionHtml('border: none;', 'newline_between_properties', true) +
|
|
|
|
optionHtml('display: block;', 'newline_before_close_brace', true) +
|
|
|
|
optionHtml('}', 'newline_between_rules') +
|
2017-07-12 19:50:13 +00:00
|
|
|
`<label style="display: block; clear: both;"><input data-option="indent_conditional" type="checkbox"
|
|
|
|
${options.indent_conditional !== false ? 'checked' : ''}>` +
|
|
|
|
t('styleBeautifyIndentConditional') + '</label>' +
|
2017-07-12 20:44:59 +00:00
|
|
|
'</div>' +
|
|
|
|
'<div><button role="undo"></button></div>');
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const undoButton = $('#help-popup button[role="undo"]');
|
2017-07-16 18:02:00 +00:00
|
|
|
undoButton.textContent = t(scope.length === 1 ? 'undo' : 'undoGlobal');
|
2017-07-16 16:49:31 +00:00
|
|
|
undoButton.addEventListener('click', () => {
|
2017-07-12 20:44:59 +00:00
|
|
|
let undoable = false;
|
2017-07-16 16:49:31 +00:00
|
|
|
scope.forEach(cm => {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (cm.beautifyChange && cm.beautifyChange[cm.changeGeneration()]) {
|
|
|
|
delete cm.beautifyChange[cm.changeGeneration()];
|
|
|
|
cm.undo();
|
|
|
|
cm.scrollIntoView(cm.getCursor());
|
|
|
|
undoable |= cm.beautifyChange[cm.changeGeneration()];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
undoButton.disabled = !undoable;
|
|
|
|
});
|
|
|
|
|
2017-07-16 16:49:31 +00:00
|
|
|
scope.forEach(cm => {
|
|
|
|
setTimeout(() => {
|
2017-07-12 19:50:13 +00:00
|
|
|
const pos = options.translate_positions =
|
|
|
|
[].concat.apply([], cm.doc.sel.ranges.map(r =>
|
|
|
|
[Object.assign({}, r.anchor), Object.assign({}, r.head)]));
|
2017-07-12 20:44:59 +00:00
|
|
|
const text = cm.getValue();
|
2017-08-29 11:28:13 +00:00
|
|
|
const newText = css_beautify(text, options);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (newText !== text) {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!cm.beautifyChange || !cm.beautifyChange[cm.changeGeneration()]) {
|
|
|
|
// clear the list if last change wasn't a css-beautify
|
|
|
|
cm.beautifyChange = {};
|
|
|
|
}
|
|
|
|
cm.setValue(newText);
|
|
|
|
const selections = [];
|
|
|
|
for (let i = 0; i < pos.length; i += 2) {
|
|
|
|
selections.push({anchor: pos[i], head: pos[i + 1]});
|
|
|
|
}
|
|
|
|
cm.setSelections(selections);
|
|
|
|
cm.beautifyChange[cm.changeGeneration()] = true;
|
|
|
|
undoButton.disabled = false;
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.beautify-options').onchange = ({target}) => {
|
2017-07-16 18:02:00 +00:00
|
|
|
const value = target.type === 'checkbox' ? target.checked : target.selectedIndex > 0;
|
2017-07-12 19:50:13 +00:00
|
|
|
prefs.set('editor.beautify', Object.assign(options, {[target.dataset.option]: value}));
|
|
|
|
if (target.parentNode.hasAttribute('newline')) {
|
|
|
|
target.parentNode.setAttribute('newline', value.toString());
|
|
|
|
}
|
|
|
|
doBeautify();
|
|
|
|
};
|
|
|
|
|
|
|
|
function optionHtml(label, optionName, indent) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const value = options[optionName];
|
|
|
|
return '<div newline="' + value.toString() + '">' +
|
|
|
|
'<span' + (indent ? ' indent' : '') + '>' + label + '</span>' +
|
|
|
|
'<select data-option="' + optionName + '">' +
|
|
|
|
'<option' + (value ? '' : ' selected') + '> </option>' +
|
|
|
|
'<option' + (value ? ' selected' : '') + '>\\n</option>' +
|
|
|
|
'</select></div>';
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-23 16:24:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 13:04:03 +00:00
|
|
|
function initStyleData() {
|
2017-11-28 19:19:00 +00:00
|
|
|
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
|
|
|
|
const params = new URLSearchParams(location.search.replace(/^\?/, ''));
|
2017-11-26 13:04:03 +00:00
|
|
|
const id = params.get('id');
|
|
|
|
const createEmptyStyle = () => ({
|
|
|
|
id: null,
|
|
|
|
name: '',
|
|
|
|
enabled: true,
|
|
|
|
sections: [
|
|
|
|
Object.assign({code: ''},
|
|
|
|
...Object.keys(CssToProperty)
|
|
|
|
.map(name => ({
|
|
|
|
[CssToProperty[name]]: params.get(name) && [params.get(name)] || []
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
],
|
2017-09-12 11:47:32 +00:00
|
|
|
});
|
2017-11-26 13:04:03 +00:00
|
|
|
return !id ?
|
|
|
|
Promise.resolve(createEmptyStyle()) :
|
|
|
|
getStylesSafe({id}).then(([style]) => style || createEmptyStyle());
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 04:59:51 +00:00
|
|
|
function setStyleMeta(style) {
|
2017-09-03 15:14:31 +00:00
|
|
|
$('#name').value = style.name || '';
|
|
|
|
$('#enabled').checked = style.enabled !== false;
|
|
|
|
$('#url').href = style.url || '';
|
2017-03-20 04:59:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 16:43:00 +00:00
|
|
|
function isUsercss(style) {
|
2017-11-08 23:20:57 +00:00
|
|
|
return (
|
|
|
|
style.usercssData ||
|
2017-11-09 00:21:42 +00:00
|
|
|
!style.id && prefs.get('newStyleAsUsercss')
|
2017-11-08 23:20:57 +00:00
|
|
|
);
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:37:06 +00:00
|
|
|
function initWithSectionStyle({style, codeIsUpdated}) {
|
2017-07-12 19:50:13 +00:00
|
|
|
setStyleMeta(style);
|
2017-11-15 01:23:16 +00:00
|
|
|
if (codeIsUpdated !== false) {
|
|
|
|
editors.length = 0;
|
|
|
|
getSections().forEach(div => div.remove());
|
|
|
|
addSections(style.sections.length ? style.sections : [{code: ''}]);
|
|
|
|
initHooks();
|
|
|
|
}
|
|
|
|
setCleanGlobal();
|
|
|
|
updateTitle();
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
function addSections(sections, onAdded = () => {}) {
|
|
|
|
if (addSections.running) {
|
|
|
|
console.error('addSections cannot be re-entered: please report to the developers');
|
|
|
|
// TODO: handle this properly e.g. on update/import
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-15 01:23:16 +00:00
|
|
|
addSections.running = true;
|
2017-08-29 22:49:03 +00:00
|
|
|
maximizeCodeHeight.stats = null;
|
2017-11-15 01:23:16 +00:00
|
|
|
// make a shallow copy since we might run asynchronously
|
|
|
|
// and the original array might get modified
|
|
|
|
sections = sections.slice();
|
|
|
|
const t0 = performance.now();
|
|
|
|
const divs = [];
|
|
|
|
let index = 0;
|
2017-11-15 01:53:37 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
return new Promise(function run(resolve) {
|
|
|
|
while (index < sections.length) {
|
|
|
|
const div = addSection(null, sections[index]);
|
|
|
|
maximizeCodeHeight(div, index === sections.length - 1);
|
|
|
|
onAdded(div, index);
|
|
|
|
divs.push(div);
|
2017-11-15 01:53:37 +00:00
|
|
|
maybeFocusFirstCM();
|
2017-11-15 01:23:16 +00:00
|
|
|
index++;
|
|
|
|
const elapsed = performance.now() - t0;
|
|
|
|
if (elapsed > 500) {
|
|
|
|
setGlobalProgress(index, sections.length);
|
|
|
|
}
|
|
|
|
if (elapsed > 100) {
|
|
|
|
// after 100ms the sections are added asynchronously
|
|
|
|
setTimeout(run, 0, resolve);
|
|
|
|
return;
|
2017-08-31 21:52:38 +00:00
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-11-15 01:23:16 +00:00
|
|
|
editors.last.state.renderLintReportNow = true;
|
|
|
|
addSections.running = false;
|
|
|
|
setGlobalProgress();
|
|
|
|
resolve(divs);
|
|
|
|
});
|
2017-11-15 01:53:37 +00:00
|
|
|
|
|
|
|
function maybeFocusFirstCM() {
|
|
|
|
const isPageLocked = document.documentElement.style.pointerEvents;
|
2017-11-15 05:15:06 +00:00
|
|
|
if (divs[0] && (isPageLocked ? divs.length === sections.length : index === 0)) {
|
2017-11-15 01:53:37 +00:00
|
|
|
makeSectionVisible(divs[0].CodeMirror);
|
|
|
|
divs[0].CodeMirror.focus();
|
|
|
|
}
|
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 05:10:35 +00:00
|
|
|
function initCollapsibles() {
|
|
|
|
function saveStateDelayed(event) {
|
|
|
|
if (event.target.closest('.intercepts-click')) {
|
|
|
|
event.preventDefault();
|
|
|
|
} else {
|
|
|
|
setTimeout(saveState, 0, event.target.closest('details'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function saveState(el) {
|
|
|
|
prefs.set(`editor.${el.id}.expanded`, el.open);
|
|
|
|
}
|
|
|
|
function loadState(key, value) {
|
|
|
|
$('#' + key.split('.')[1]).open = value;
|
|
|
|
}
|
|
|
|
const collapsibles = $$('#header details');
|
|
|
|
for (const el of collapsibles) {
|
|
|
|
el.open = prefs.get(`editor.${el.id}.expanded`);
|
|
|
|
$('h2', el).addEventListener('click', saveStateDelayed);
|
|
|
|
}
|
|
|
|
prefs.subscribe(collapsibles.map(el => `editor.${el.id}.expanded`), loadState);
|
2017-10-04 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function initHooks() {
|
2017-08-29 22:49:03 +00:00
|
|
|
if (initHooks.alreadyDone) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initHooks.alreadyDone = true;
|
2017-08-21 00:05:08 +00:00
|
|
|
$$('#header .style-contributor').forEach(node => {
|
2017-07-12 20:44:59 +00:00
|
|
|
node.addEventListener('change', onChange);
|
|
|
|
node.addEventListener('input', onChange);
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
2017-08-21 00:05:08 +00:00
|
|
|
$('#toggle-style-help').addEventListener('click', showToggleStyleHelp);
|
|
|
|
$('#to-mozilla').addEventListener('click', showMozillaFormat, false);
|
|
|
|
$('#to-mozilla-help').addEventListener('click', showToMozillaHelp, false);
|
|
|
|
$('#from-mozilla').addEventListener('click', fromMozillaFormat);
|
|
|
|
$('#beautify').addEventListener('click', beautify);
|
|
|
|
$('#save-button').addEventListener('click', save, false);
|
|
|
|
$('#sections-help').addEventListener('click', showSectionHelp, false);
|
|
|
|
$('#keyMap-help').addEventListener('click', showKeyMapHelp, false);
|
|
|
|
$('#cancel-button').addEventListener('click', goBackToManage);
|
2017-09-03 16:54:25 +00:00
|
|
|
|
2017-08-17 19:08:48 +00:00
|
|
|
initLint();
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-07-31 16:39:10 +00:00
|
|
|
if (!FIREFOX) {
|
|
|
|
$$([
|
|
|
|
'input:not([type])',
|
|
|
|
'input[type="text"]',
|
|
|
|
'input[type="search"]',
|
|
|
|
'input[type="number"]',
|
|
|
|
].join(',')
|
|
|
|
).forEach(e => e.addEventListener('mousedown', toggleContextMenuDelete));
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
|
|
|
|
setupGlobalSearch();
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 12:08:47 +00:00
|
|
|
// common for usercss and classic
|
|
|
|
function initHooksCommon() {
|
|
|
|
showKeyInSaveButtonTooltip();
|
|
|
|
prefs.subscribe(['editor.keyMap'], showKeyInSaveButtonTooltip);
|
|
|
|
window.addEventListener('resize', () => debounce(rememberWindowSize, 100));
|
|
|
|
|
|
|
|
function showKeyInSaveButtonTooltip(prefName, value) {
|
|
|
|
$('#save-button').title = findKeyForCommand('save', value);
|
|
|
|
}
|
|
|
|
function findKeyForCommand(command, mapName = CodeMirror.defaults.keyMap) {
|
|
|
|
const map = CodeMirror.keyMap[mapName];
|
|
|
|
let key = Object.keys(map).find(k => map[k] === command);
|
|
|
|
if (key) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
for (const ft of Array.isArray(map.fallthrough) ? map.fallthrough : [map.fallthrough]) {
|
|
|
|
key = ft && findKeyForCommand(command, ft);
|
|
|
|
if (key) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 22:29:07 +00:00
|
|
|
|
|
|
|
function toggleContextMenuDelete(event) {
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.contextMenus && event.button === 2 && prefs.get('editor.contextDelete')) {
|
2017-07-12 19:50:13 +00:00
|
|
|
chrome.contextMenus.update('editor.contextDelete', {
|
|
|
|
enabled: Boolean(
|
2017-07-16 18:02:00 +00:00
|
|
|
this.selectionStart !== this.selectionEnd ||
|
2017-07-12 19:50:13 +00:00
|
|
|
this.somethingSelected && this.somethingSelected()
|
|
|
|
),
|
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
2017-03-21 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-24 23:03:10 +00:00
|
|
|
function maximizeCodeHeight(sectionDiv, isLast) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const cm = sectionDiv.CodeMirror;
|
|
|
|
const stats = maximizeCodeHeight.stats = maximizeCodeHeight.stats || {totalHeight: 0, deltas: []};
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!stats.cmActualHeight) {
|
|
|
|
stats.cmActualHeight = getComputedHeight(cm.display.wrapper);
|
|
|
|
}
|
|
|
|
if (!stats.sectionMarginTop) {
|
|
|
|
stats.sectionMarginTop = parseFloat(getComputedStyle(sectionDiv).marginTop);
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
const sectionTop = sectionDiv.getBoundingClientRect().top - stats.sectionMarginTop;
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!stats.firstSectionTop) {
|
|
|
|
stats.firstSectionTop = sectionTop;
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
const extrasHeight = getComputedHeight(sectionDiv) - stats.cmActualHeight;
|
|
|
|
const cmMaxHeight = window.innerHeight - extrasHeight - sectionTop - stats.sectionMarginTop;
|
|
|
|
const cmDesiredHeight = cm.display.sizer.clientHeight + 2 * cm.defaultTextHeight();
|
|
|
|
const cmGrantableHeight = Math.max(stats.cmActualHeight, Math.min(cmMaxHeight, cmDesiredHeight));
|
2017-07-12 19:50:13 +00:00
|
|
|
stats.deltas.push(cmGrantableHeight - stats.cmActualHeight);
|
|
|
|
stats.totalHeight += cmGrantableHeight + extrasHeight;
|
|
|
|
if (!isLast) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stats.totalHeight += stats.firstSectionTop;
|
|
|
|
if (stats.totalHeight <= window.innerHeight) {
|
2017-07-16 16:49:31 +00:00
|
|
|
editors.forEach((cm, index) => {
|
2017-07-12 19:50:13 +00:00
|
|
|
cm.setSize(null, stats.deltas[index] + stats.cmActualHeight);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// scale heights to fill the gap between last section and bottom edge of the window
|
2017-08-21 00:05:08 +00:00
|
|
|
const sections = $('#sections');
|
2017-07-12 20:44:59 +00:00
|
|
|
const available = window.innerHeight - sections.getBoundingClientRect().bottom -
|
2017-07-12 19:50:13 +00:00
|
|
|
parseFloat(getComputedStyle(sections).marginBottom);
|
|
|
|
if (available <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
const totalDelta = stats.deltas.reduce((sum, d) => sum + d, 0);
|
2017-07-12 20:44:59 +00:00
|
|
|
const q = available / totalDelta;
|
|
|
|
const baseHeight = stats.cmActualHeight - stats.sectionMarginTop;
|
2017-07-16 16:49:31 +00:00
|
|
|
stats.deltas.forEach((delta, index) => {
|
2017-07-12 19:50:13 +00:00
|
|
|
editors[index].setSize(null, baseHeight + Math.floor(q * delta));
|
|
|
|
});
|
2015-05-24 23:03:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function updateTitle() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const DIRTY_TITLE = '* $';
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const name = $('#name').savedValue;
|
2017-07-12 20:44:59 +00:00
|
|
|
const clean = isCleanGlobal();
|
|
|
|
const title = styleId === null ? t('addStyleTitle') : t('editStyleTitle', [name]);
|
|
|
|
document.title = clean ? title : DIRTY_TITLE.replace('$', title);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate() {
|
2017-08-21 00:05:08 +00:00
|
|
|
const name = $('#name').value;
|
2017-07-16 18:02:00 +00:00
|
|
|
if (name === '') {
|
2017-09-01 09:27:59 +00:00
|
|
|
$('#name').focus();
|
2017-07-12 20:44:59 +00:00
|
|
|
return t('styleMissingName');
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
// validate the regexps
|
2017-08-21 00:05:08 +00:00
|
|
|
if ($$('.applies-to-list').some(list => {
|
2017-07-16 16:49:31 +00:00
|
|
|
list.childNodes.some(li => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (li.className === template.appliesToEverything.className) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
const valueElement = $('[name=applies-value]', li);
|
|
|
|
const type = $('[name=applies-type]', li).value;
|
2017-07-12 20:44:59 +00:00
|
|
|
const value = valueElement.value;
|
2017-07-12 19:50:13 +00:00
|
|
|
if (type && value) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (type === 'regexp') {
|
2017-07-12 19:50:13 +00:00
|
|
|
try {
|
|
|
|
new RegExp(value);
|
|
|
|
} catch (ex) {
|
|
|
|
valueElement.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
})) {
|
2017-07-12 20:44:59 +00:00
|
|
|
return t('styleBadRegexp');
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
return null;
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 20:36:56 +00:00
|
|
|
function updateLintReportIfEnabled(...args) {
|
|
|
|
if (CodeMirror.defaults.lint) {
|
|
|
|
updateLintReport(...args);
|
2017-08-20 14:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
function save() {
|
2017-11-08 23:19:22 +00:00
|
|
|
if (editor) {
|
|
|
|
editor.save();
|
|
|
|
} else {
|
2017-09-12 11:37:06 +00:00
|
|
|
saveSectionStyle();
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:37:06 +00:00
|
|
|
function saveSectionStyle() {
|
2017-08-20 14:06:17 +00:00
|
|
|
updateLintReportIfEnabled(null, 0);
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-07-12 20:44:59 +00:00
|
|
|
const error = validate();
|
2017-07-12 19:50:13 +00:00
|
|
|
if (error) {
|
|
|
|
alert(error);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
const name = $('#name').value;
|
|
|
|
const enabled = $('#enabled').checked;
|
2017-07-12 19:50:13 +00:00
|
|
|
saveStyleSafe({
|
|
|
|
id: styleId,
|
|
|
|
name: name,
|
|
|
|
enabled: enabled,
|
|
|
|
reason: 'editSave',
|
|
|
|
sections: getSectionsHashes()
|
|
|
|
})
|
|
|
|
.then(saveComplete);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
function getSectionsHashes() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const sections = [];
|
2017-07-16 16:49:31 +00:00
|
|
|
getSections().forEach(div => {
|
2017-07-12 20:44:59 +00:00
|
|
|
const meta = getMeta(div);
|
|
|
|
const code = div.CodeMirror.getValue();
|
2017-07-16 18:02:00 +00:00
|
|
|
if (/^\s*$/.test(code) && Object.keys(meta).length === 0) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
meta.code = code;
|
|
|
|
sections.push(meta);
|
|
|
|
});
|
|
|
|
return sections;
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMeta(e) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const meta = {urls: [], urlPrefixes: [], domains: [], regexps: []};
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.applies-to-list', e).childNodes.forEach(li => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (li.className === template.appliesToEverything.className) {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-21 00:05:08 +00:00
|
|
|
const type = $('[name=applies-type]', li).value;
|
|
|
|
const value = $('[name=applies-value]', li).value;
|
2017-07-12 19:50:13 +00:00
|
|
|
if (type && value) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const property = CssToProperty[type];
|
2017-07-12 19:50:13 +00:00
|
|
|
meta[property].push(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return meta;
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveComplete(style) {
|
2017-07-12 19:50:13 +00:00
|
|
|
styleId = style.id;
|
2017-07-23 15:33:55 +00:00
|
|
|
sessionStorage.justEditedStyleId = styleId;
|
2017-07-12 19:50:13 +00:00
|
|
|
setCleanGlobal();
|
|
|
|
|
|
|
|
// Go from new style URL to edit style URL
|
2017-07-16 18:02:00 +00:00
|
|
|
if (location.href.indexOf('id=') === -1) {
|
2017-07-12 20:44:59 +00:00
|
|
|
history.replaceState({}, document.title, 'edit.html?id=' + style.id);
|
2017-07-19 12:38:27 +00:00
|
|
|
$('#heading').textContent = t('editStyleHeading');
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
updateTitle();
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showMozillaFormat() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const popup = showCodeMirrorPopup(t('styleToMozillaFormatTitle'), '', {readOnly: true});
|
2017-07-12 19:50:13 +00:00
|
|
|
popup.codebox.setValue(toMozillaFormat());
|
2017-07-12 20:44:59 +00:00
|
|
|
popup.codebox.execCommand('selectAll');
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toMozillaFormat() {
|
2017-08-05 16:49:25 +00:00
|
|
|
return mozParser.format({sections: getSectionsHashes()});
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
function fromMozillaFormat() {
|
2017-11-14 06:36:43 +00:00
|
|
|
const popup = showCodeMirrorPopup(t('styleFromMozillaFormatPrompt'),
|
|
|
|
$element({appendChild: [
|
|
|
|
$element({
|
|
|
|
tag: 'button',
|
|
|
|
name: 'import-append',
|
|
|
|
textContent: t('importAppendLabel'),
|
2017-11-28 17:05:26 +00:00
|
|
|
title: 'Ctrl-Enter:\n' + t('importAppendTooltip'),
|
2017-11-14 06:36:43 +00:00
|
|
|
onclick: doImport,
|
|
|
|
}),
|
|
|
|
$element({
|
|
|
|
tag: 'button',
|
|
|
|
name: 'import-replace',
|
|
|
|
textContent: t('importReplaceLabel'),
|
2017-11-28 17:05:26 +00:00
|
|
|
title: 'Ctrl-Shift-Enter:\n' + t('importReplaceTooltip'),
|
2017-11-15 01:23:16 +00:00
|
|
|
onclick: () => doImport({replaceOldStyle: true}),
|
2017-11-14 06:36:43 +00:00
|
|
|
}),
|
|
|
|
]}));
|
2017-08-21 00:05:08 +00:00
|
|
|
const contents = $('.contents', popup);
|
2017-07-12 19:50:13 +00:00
|
|
|
contents.insertBefore(popup.codebox.display.wrapper, contents.firstElementChild);
|
|
|
|
popup.codebox.focus();
|
2017-11-15 01:23:16 +00:00
|
|
|
popup.codebox.on('changes', cm => {
|
|
|
|
popup.classList.toggle('ready', !cm.isBlank());
|
2017-11-28 17:03:50 +00:00
|
|
|
cm.markClean();
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
2017-11-15 01:23:16 +00:00
|
|
|
// overwrite default extraKeys as those are inapplicable in popup context
|
|
|
|
popup.codebox.options.extraKeys = {
|
|
|
|
'Ctrl-Enter': doImport,
|
|
|
|
'Shift-Ctrl-Enter': () => doImport({replaceOldStyle: true}),
|
|
|
|
};
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
function doImport({replaceOldStyle = false}) {
|
|
|
|
lockPageUI(true);
|
|
|
|
new Promise(setTimeout)
|
|
|
|
.then(() => mozParser.parse(popup.codebox.getValue().trim()))
|
|
|
|
.then(sections => {
|
|
|
|
removeOldSections(replaceOldStyle);
|
|
|
|
return addSections(sections, div => setCleanItem(div, false));
|
|
|
|
})
|
|
|
|
.then(sectionDivs => {
|
|
|
|
sectionDivs.forEach(div => updateLintReportIfEnabled(div.CodeMirror, 1));
|
2017-09-12 15:19:16 +00:00
|
|
|
$('.dismiss', popup).onclick();
|
|
|
|
})
|
2017-11-15 01:23:16 +00:00
|
|
|
.catch(showError)
|
|
|
|
.then(() => lockPageUI(false));
|
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
function removeOldSections(removeAll) {
|
|
|
|
let toRemove;
|
|
|
|
if (removeAll) {
|
|
|
|
toRemove = editors.slice().reverse();
|
|
|
|
} else if (editors.last.isBlank() && $('.applies-to-everything', editors.last.getSection())) {
|
|
|
|
toRemove = [editors.last];
|
|
|
|
} else {
|
|
|
|
return;
|
2017-09-12 15:19:16 +00:00
|
|
|
}
|
2017-11-15 01:23:16 +00:00
|
|
|
toRemove.forEach(cm => removeSection({target: cm.getSection()}));
|
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
function lockPageUI(locked) {
|
|
|
|
document.documentElement.style.pointerEvents = locked ? 'none' : '';
|
|
|
|
popup.classList.toggle('ready', locked ? false : !popup.codebox.isBlank());
|
|
|
|
popup.codebox.options.readOnly = locked;
|
|
|
|
popup.codebox.display.wrapper.style.opacity = locked ? '.5' : '';
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
|
2017-11-15 01:23:16 +00:00
|
|
|
function showError(errors) {
|
|
|
|
showHelp(t('styleFromMozillaFormatError'), $element({
|
|
|
|
tag: 'pre',
|
|
|
|
textContent: Array.isArray(errors) ? errors.join('\n') : errors,
|
|
|
|
}));
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
function showSectionHelp() {
|
2017-07-12 20:44:59 +00:00
|
|
|
showHelp(t('styleSectionsTitle'), t('sectionHelp'));
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showAppliesToHelp() {
|
2017-07-12 20:44:59 +00:00
|
|
|
showHelp(t('appliesLabel'), t('appliesHelp'));
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showToMozillaHelp() {
|
2017-07-12 20:44:59 +00:00
|
|
|
showHelp(t('styleMozillaFormatHeading'), t('styleToMozillaFormatHelp'));
|
2015-04-26 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 07:56:49 +00:00
|
|
|
function showToggleStyleHelp() {
|
2017-07-12 20:44:59 +00:00
|
|
|
showHelp(t('helpAlt'), t('styleEnabledToggleHint'));
|
2017-06-06 07:56:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 12:34:59 +00:00
|
|
|
function showKeyMapHelp() {
|
2017-07-12 20:44:59 +00:00
|
|
|
const keyMap = mergeKeyMaps({}, prefs.get('editor.keyMap'), CodeMirror.defaults.extraKeys);
|
|
|
|
const keyMapSorted = Object.keys(keyMap)
|
2017-07-16 16:49:31 +00:00
|
|
|
.map(key => ({key: key, cmd: keyMap[key]}))
|
2017-07-12 20:44:59 +00:00
|
|
|
.concat([{key: 'Shift-Ctrl-Wheel', cmd: 'scrollWindow'}])
|
2017-07-16 18:02:00 +00:00
|
|
|
.sort((a, b) => (a.cmd < b.cmd || (a.cmd === b.cmd && a.key < b.key) ? -1 : 1));
|
2017-07-12 20:44:59 +00:00
|
|
|
showHelp(t('cm_keyMap') + ': ' + prefs.get('editor.keyMap'),
|
2017-07-12 19:50:13 +00:00
|
|
|
'<table class="keymap-list">' +
|
2017-07-12 20:44:59 +00:00
|
|
|
'<thead><tr><th><input placeholder="' + t('helpKeyMapHotkey') + '" type="search"></th>' +
|
|
|
|
'<th><input placeholder="' + t('helpKeyMapCommand') + '" type="search"></th></tr></thead>' +
|
2017-07-16 16:49:31 +00:00
|
|
|
'<tbody>' + keyMapSorted.map(value =>
|
|
|
|
'<tr><td>' + value.key + '</td><td>' + value.cmd + '</td></tr>'
|
|
|
|
).join('') +
|
2017-07-12 20:44:59 +00:00
|
|
|
'</tbody>' +
|
|
|
|
'</table>');
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const table = $('#help-popup table');
|
2017-07-12 20:44:59 +00:00
|
|
|
table.addEventListener('input', filterTable);
|
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
const inputs = $$('input', table);
|
2017-07-12 20:44:59 +00:00
|
|
|
inputs[0].addEventListener('keydown', hotkeyHandler);
|
2017-07-12 19:50:13 +00:00
|
|
|
inputs[1].focus();
|
|
|
|
|
|
|
|
function hotkeyHandler(event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const keyName = CodeMirror.keyName(event);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (keyName === 'Esc' || keyName === 'Tab' || keyName === 'Shift-Tab') {
|
2017-07-12 19:50:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
// normalize order of modifiers,
|
2017-07-12 20:44:59 +00:00
|
|
|
// for modifier-only keys ('Ctrl-Shift') a dummy main key has to be temporarily added
|
|
|
|
const keyMap = {};
|
|
|
|
keyMap[keyName.replace(/(Shift|Ctrl|Alt|Cmd)$/, '$&-dummy')] = '';
|
|
|
|
const normalizedKey = Object.keys(CodeMirror.normalizeKeyMap(keyMap))[0];
|
|
|
|
this.value = normalizedKey.replace('-dummy', '');
|
2017-07-12 19:50:13 +00:00
|
|
|
filterTable(event);
|
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
|
2017-07-12 19:50:13 +00:00
|
|
|
function filterTable(event) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const input = event.target;
|
|
|
|
const col = input.parentNode.cellIndex;
|
|
|
|
inputs[1 - col].value = '';
|
2017-07-16 16:49:31 +00:00
|
|
|
table.tBodies[0].childNodes.forEach(row => {
|
2017-07-19 11:34:31 +00:00
|
|
|
const cell = row.children[col];
|
2017-07-19 16:18:34 +00:00
|
|
|
const text = cell.textContent;
|
|
|
|
const query = stringAsRegExp(input.value, 'gi');
|
|
|
|
const test = query.test(text);
|
|
|
|
row.style.display = input.value && test === false ? 'none' : '';
|
|
|
|
if (input.value && test) {
|
|
|
|
cell.textContent = '';
|
|
|
|
let offset = 0;
|
|
|
|
text.replace(query, (match, index) => {
|
|
|
|
if (index > offset) {
|
|
|
|
cell.appendChild(document.createTextNode(text.substring(offset, index)));
|
|
|
|
}
|
|
|
|
cell.appendChild($element({tag: 'mark', textContent: match}));
|
|
|
|
offset = index + match.length;
|
|
|
|
});
|
|
|
|
if (offset + 1 !== text.length) {
|
|
|
|
cell.appendChild(document.createTextNode(text.substring(offset)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cell.textContent = text;
|
2017-07-19 11:34:31 +00:00
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
// clear highlight from the other column
|
2017-07-19 16:18:34 +00:00
|
|
|
const otherCell = row.children[1 - col];
|
|
|
|
if (otherCell.children.length) {
|
|
|
|
const text = otherCell.textContent;
|
|
|
|
otherCell.textContent = text;
|
|
|
|
}
|
2017-07-12 19:50:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function mergeKeyMaps(merged, ...more) {
|
|
|
|
more.forEach(keyMap => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof keyMap === 'string') {
|
2017-07-12 19:50:13 +00:00
|
|
|
keyMap = CodeMirror.keyMap[keyMap];
|
|
|
|
}
|
2017-07-16 16:49:31 +00:00
|
|
|
Object.keys(keyMap).forEach(key => {
|
2017-07-12 20:44:59 +00:00
|
|
|
let cmd = keyMap[key];
|
2017-07-12 19:50:13 +00:00
|
|
|
// filter out '...', 'attach', etc. (hotkeys start with an uppercase letter)
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!merged[key] && !key.match(/^[a-z]/) && cmd !== '...') {
|
|
|
|
if (typeof cmd === 'function') {
|
2017-07-12 19:50:13 +00:00
|
|
|
// for 'emacs' keymap: provide at least something meaningful (hotkeys and the function body)
|
|
|
|
// for 'vim*' keymaps: almost nothing as it doesn't rely on CM keymap mechanism
|
2017-07-12 20:44:59 +00:00
|
|
|
cmd = cmd.toString().replace(/^function.*?\{[\s\r\n]*([\s\S]+?)[\s\r\n]*\}$/, '$1');
|
|
|
|
merged[key] = cmd.length <= 200 ? cmd : cmd.substr(0, 200) + '...';
|
2017-07-12 19:50:13 +00:00
|
|
|
} else {
|
|
|
|
merged[key] = cmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (keyMap.fallthrough) {
|
|
|
|
merged = mergeKeyMaps(merged, keyMap.fallthrough);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return merged;
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:03:50 +00:00
|
|
|
function showHelp(title = '', body) {
|
2017-07-19 12:09:29 +00:00
|
|
|
const div = $('#help-popup');
|
2017-07-12 20:44:59 +00:00
|
|
|
div.classList.remove('big');
|
2017-11-28 17:03:50 +00:00
|
|
|
const contents = $('.contents', div);
|
|
|
|
contents.textContent = '';
|
|
|
|
if (body) {
|
|
|
|
contents.appendChild(typeof body === 'string' ? tHTML(body) : body);
|
|
|
|
}
|
2017-07-19 12:09:29 +00:00
|
|
|
$('.title', div).textContent = title;
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-07-16 18:02:00 +00:00
|
|
|
if (getComputedStyle(div).display === 'none') {
|
2017-11-28 17:03:50 +00:00
|
|
|
window.addEventListener('keydown', closeHelp, true);
|
2017-08-20 18:56:18 +00:00
|
|
|
// avoid chaining on multiple showHelp() calls
|
2017-08-21 00:05:08 +00:00
|
|
|
$('.dismiss', div).onclick = closeHelp;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-11-21 15:39:13 +00:00
|
|
|
// reset any inline styles
|
|
|
|
div.style = 'display: block';
|
2017-07-12 19:50:13 +00:00
|
|
|
return div;
|
|
|
|
|
|
|
|
function closeHelp(e) {
|
2017-11-28 17:03:50 +00:00
|
|
|
if (!e || e.type === 'click' ||
|
|
|
|
(e.which === 27 && !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey &&
|
|
|
|
!$('.CodeMirror-hints, #message-box') && !(document.activeElement instanceof HTMLInputElement))) {
|
|
|
|
if (e && div.codebox && !div.codebox.options.readOnly && !div.codebox.isClean()) {
|
|
|
|
messageBox.confirm(t('confirmDiscardChanges')).then(ok => ok && closeHelp());
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 20:44:59 +00:00
|
|
|
div.style.display = '';
|
2017-08-20 14:31:25 +00:00
|
|
|
contents.textContent = '';
|
|
|
|
clearTimeout(contents.timer);
|
2017-11-28 17:03:50 +00:00
|
|
|
window.removeEventListener('keydown', closeHelp, true);
|
|
|
|
window.dispatchEvent(new Event('closeHelp'));
|
|
|
|
(editors.lastActive || editors[0]).focus();
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
function showCodeMirrorPopup(title, html, options) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const popup = showHelp(title, html);
|
|
|
|
popup.classList.add('big');
|
2017-07-12 19:50:13 +00:00
|
|
|
|
2017-08-21 00:05:08 +00:00
|
|
|
popup.codebox = CodeMirror($('.contents', popup), Object.assign({
|
2017-07-12 20:44:59 +00:00
|
|
|
mode: 'css',
|
2017-07-12 19:50:13 +00:00
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
|
|
|
foldGutter: true,
|
2017-07-12 20:44:59 +00:00
|
|
|
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'CodeMirror-lint-markers'],
|
2017-07-12 19:50:13 +00:00
|
|
|
matchBrackets: true,
|
2017-08-28 05:22:19 +00:00
|
|
|
lint: linterConfig.getForCodeMirror(),
|
2017-07-12 19:50:13 +00:00
|
|
|
styleActiveLine: true,
|
2017-07-12 20:44:59 +00:00
|
|
|
theme: prefs.get('editor.theme'),
|
|
|
|
keyMap: prefs.get('editor.keyMap')
|
2017-07-12 19:50:13 +00:00
|
|
|
}, options));
|
|
|
|
popup.codebox.focus();
|
2017-07-16 16:49:31 +00:00
|
|
|
popup.codebox.on('focus', () => { hotkeyRerouter.setState(false); });
|
|
|
|
popup.codebox.on('blur', () => { hotkeyRerouter.setState(true); });
|
2017-07-12 19:50:13 +00:00
|
|
|
return popup;
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
|
|
|
|
|
|
|
function onRuntimeMessage(request) {
|
2017-07-12 19:50:13 +00:00
|
|
|
switch (request.method) {
|
2017-07-12 20:44:59 +00:00
|
|
|
case 'styleUpdated':
|
2017-11-26 21:45:21 +00:00
|
|
|
if (styleId && styleId === request.style.id &&
|
|
|
|
request.reason !== 'editSave' &&
|
|
|
|
request.reason !== 'config') {
|
|
|
|
// code-less style from notifyAllTabs
|
2017-07-12 19:50:13 +00:00
|
|
|
if ((request.style.sections[0] || {}).code === null) {
|
2017-11-26 21:45:21 +00:00
|
|
|
request.style = BG.cachedStyles.byId.get(request.style.id);
|
|
|
|
}
|
|
|
|
if (isUsercss(request.style)) {
|
|
|
|
editor.replaceStyle(request.style, request.codeIsUpdated);
|
2017-07-12 19:50:13 +00:00
|
|
|
} else {
|
2017-11-26 21:45:21 +00:00
|
|
|
initWithSectionStyle(request);
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-07-12 20:44:59 +00:00
|
|
|
case 'styleDeleted':
|
2017-11-01 00:43:35 +00:00
|
|
|
if (styleId === request.id || editor && editor.getStyle().id === request.id) {
|
2017-07-16 16:49:31 +00:00
|
|
|
window.onbeforeunload = () => {};
|
2017-09-25 10:43:55 +00:00
|
|
|
closeCurrentTab();
|
2017-07-12 19:50:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-07-12 20:44:59 +00:00
|
|
|
case 'prefChanged':
|
2017-07-12 19:50:13 +00:00
|
|
|
if ('editor.smartIndent' in request.prefs) {
|
|
|
|
CodeMirror.setOption('smartIndent', request.prefs['editor.smartIndent']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'editDeleteText':
|
|
|
|
document.execCommand('delete');
|
|
|
|
break;
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
|
2015-05-24 23:03:10 +00:00
|
|
|
function getComputedHeight(el) {
|
2017-07-12 20:44:59 +00:00
|
|
|
const compStyle = getComputedStyle(el);
|
2017-07-12 19:50:13 +00:00
|
|
|
return el.getBoundingClientRect().height +
|
|
|
|
parseFloat(compStyle.marginTop) + parseFloat(compStyle.marginBottom);
|
2015-05-24 23:03:10 +00:00
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
function getCodeMirrorThemes() {
|
2017-07-12 19:50:13 +00:00
|
|
|
if (!chrome.runtime.getPackageDirectoryEntry) {
|
|
|
|
const themes = [
|
|
|
|
chrome.i18n.getMessage('defaultTheme'),
|
|
|
|
'3024-day',
|
|
|
|
'3024-night',
|
|
|
|
'abcdef',
|
|
|
|
'ambiance',
|
|
|
|
'ambiance-mobile',
|
|
|
|
'base16-dark',
|
|
|
|
'base16-light',
|
|
|
|
'bespin',
|
|
|
|
'blackboard',
|
|
|
|
'cobalt',
|
|
|
|
'colorforth',
|
|
|
|
'dracula',
|
|
|
|
'duotone-dark',
|
|
|
|
'duotone-light',
|
|
|
|
'eclipse',
|
|
|
|
'elegant',
|
|
|
|
'erlang-dark',
|
|
|
|
'hopscotch',
|
|
|
|
'icecoder',
|
|
|
|
'isotope',
|
|
|
|
'lesser-dark',
|
|
|
|
'liquibyte',
|
|
|
|
'material',
|
|
|
|
'mbo',
|
|
|
|
'mdn-like',
|
|
|
|
'midnight',
|
|
|
|
'monokai',
|
|
|
|
'neat',
|
|
|
|
'neo',
|
|
|
|
'night',
|
|
|
|
'panda-syntax',
|
|
|
|
'paraiso-dark',
|
|
|
|
'paraiso-light',
|
|
|
|
'pastel-on-dark',
|
|
|
|
'railscasts',
|
|
|
|
'rubyblue',
|
|
|
|
'seti',
|
|
|
|
'solarized',
|
|
|
|
'the-matrix',
|
|
|
|
'tomorrow-night-bright',
|
|
|
|
'tomorrow-night-eighties',
|
|
|
|
'ttcn',
|
|
|
|
'twilight',
|
|
|
|
'vibrant-ink',
|
|
|
|
'xq-dark',
|
|
|
|
'xq-light',
|
|
|
|
'yeti',
|
|
|
|
'zenburn',
|
|
|
|
];
|
|
|
|
localStorage.codeMirrorThemes = themes.join(' ');
|
|
|
|
return Promise.resolve(themes);
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
chrome.runtime.getPackageDirectoryEntry(rootDir => {
|
|
|
|
rootDir.getDirectory('vendor/codemirror/theme', {create: false}, themeDir => {
|
|
|
|
themeDir.createReader().readEntries(entries => {
|
|
|
|
const themes = [
|
|
|
|
chrome.i18n.getMessage('defaultTheme')
|
|
|
|
].concat(
|
|
|
|
entries.filter(entry => entry.isFile)
|
|
|
|
.sort((a, b) => (a.name < b.name ? -1 : 1))
|
|
|
|
.map(entry => entry.name.replace(/\.css$/, ''))
|
|
|
|
);
|
|
|
|
localStorage.codeMirrorThemes = themes.join(' ');
|
|
|
|
resolve(themes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-08-31 21:52:38 +00:00
|
|
|
|
|
|
|
function setGlobalProgress(done, total) {
|
|
|
|
const progressElement = $('#global-progress') ||
|
|
|
|
total && document.body.appendChild($element({id: 'global-progress'}));
|
|
|
|
if (total) {
|
|
|
|
const progress = (done / Math.max(done, total) * 100).toFixed(1);
|
|
|
|
progressElement.style.borderLeftWidth = progress + 'vw';
|
|
|
|
setTimeout(() => {
|
|
|
|
progressElement.title = progress + '%';
|
|
|
|
});
|
|
|
|
} else if (progressElement) {
|
|
|
|
progressElement.remove();
|
|
|
|
}
|
|
|
|
}
|