2020-11-08 08:12:42 +00:00
|
|
|
/* global
|
|
|
|
$
|
|
|
|
$$
|
|
|
|
$create
|
|
|
|
API
|
|
|
|
clipString
|
|
|
|
CodeMirror
|
|
|
|
createLivePreview
|
|
|
|
createSection
|
|
|
|
debounce
|
|
|
|
editor
|
|
|
|
FIREFOX
|
|
|
|
ignoreChromeError
|
|
|
|
linter
|
|
|
|
messageBox
|
|
|
|
prefs
|
2020-11-21 21:12:32 +00:00
|
|
|
rerouteHotkeys
|
2020-11-08 08:12:42 +00:00
|
|
|
sectionsToMozFormat
|
2020-11-18 11:17:15 +00:00
|
|
|
sessionStore
|
2020-11-08 08:12:42 +00:00
|
|
|
showCodeMirrorPopup
|
|
|
|
showHelp
|
|
|
|
t
|
|
|
|
*/
|
2018-11-07 06:09:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported SectionsEditor */
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function SectionsEditor() {
|
|
|
|
const {style, dirty} = editor;
|
2018-11-07 06:09:29 +00:00
|
|
|
const container = $('#sections');
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @type {EditorSection[]} */
|
2018-11-07 06:09:29 +00:00
|
|
|
const sections = [];
|
2020-11-08 08:12:42 +00:00
|
|
|
const xo = window.IntersectionObserver &&
|
|
|
|
new IntersectionObserver(refreshOnViewListener, {rootMargin: '100%'});
|
|
|
|
const livePreview = createLivePreview(null, style.id);
|
|
|
|
|
|
|
|
let INC_ID = 0; // an increment id that is used by various object to track the order
|
|
|
|
let sectionOrder = '';
|
|
|
|
let headerOffset; // in compact mode the header is at the top so it reduces the available height
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2019-06-20 19:36:27 +00:00
|
|
|
container.classList.add('section-editor');
|
2020-10-11 14:13:25 +00:00
|
|
|
updateHeader();
|
2020-11-08 08:12:42 +00:00
|
|
|
$('#to-mozilla').on('click', showMozillaFormat);
|
|
|
|
$('#to-mozilla-help').on('click', showToMozillaHelp);
|
|
|
|
$('#from-mozilla').on('click', () => showMozillaFormatImport());
|
|
|
|
document.on('wheel', scrollEntirePageOnCtrlShift, {passive: false});
|
2020-07-30 01:26:38 +00:00
|
|
|
CodeMirror.defaults.extraKeys['Shift-Ctrl-Wheel'] = 'scrollWindow';
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!FIREFOX) {
|
2020-11-08 08:12:42 +00:00
|
|
|
$$('input:not([type]), input[type=text], input[type=search], input[type=number]')
|
|
|
|
.forEach(e => e.on('mousedown', toggleContextMenuDelete));
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @namespace SectionsEditor */
|
|
|
|
Object.assign(editor, {
|
|
|
|
|
|
|
|
sections,
|
2020-10-11 14:13:25 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
closestVisible,
|
2020-10-11 15:12:06 +00:00
|
|
|
updateLivePreview,
|
2020-11-08 08:12:42 +00:00
|
|
|
|
|
|
|
getEditors() {
|
|
|
|
return sections.filter(s => !s.removed).map(s => s.cm);
|
|
|
|
},
|
|
|
|
|
|
|
|
getEditorTitle(cm) {
|
|
|
|
const index = editor.getEditors().indexOf(cm);
|
|
|
|
return `${t('sectionCode')} ${index + 1}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
getSearchableInputs(cm) {
|
|
|
|
return sections.find(s => s.cm === cm).appliesTo.map(a => a.valueEl).filter(Boolean);
|
|
|
|
},
|
|
|
|
|
|
|
|
jumpToEditor(i) {
|
|
|
|
const {cm} = sections[i] || {};
|
|
|
|
if (cm) {
|
|
|
|
editor.scrollToEditor(cm);
|
|
|
|
cm.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
nextEditor(cm, cycle = true) {
|
|
|
|
return cycle || cm !== findLast(sections, s => !s.removed).cm
|
|
|
|
? nextPrevEditor(cm, 1)
|
|
|
|
: null;
|
|
|
|
},
|
|
|
|
|
|
|
|
prevEditor(cm, cycle = true) {
|
|
|
|
return cycle || cm !== sections.find(s => !s.removed).cm
|
|
|
|
? nextPrevEditor(cm, -1)
|
|
|
|
: null;
|
|
|
|
},
|
|
|
|
|
|
|
|
async replaceStyle(newStyle, codeIsUpdated) {
|
|
|
|
dirty.clear('name');
|
|
|
|
// FIXME: avoid recreating all editors?
|
|
|
|
if (codeIsUpdated !== false) {
|
|
|
|
await initSections(newStyle.sections, {replace: true, pristine: true});
|
|
|
|
}
|
|
|
|
Object.assign(style, newStyle);
|
|
|
|
updateHeader();
|
|
|
|
dirty.clear();
|
|
|
|
// Go from new style URL to edit style URL
|
|
|
|
if (location.href.indexOf('id=') === -1 && style.id) {
|
|
|
|
history.replaceState({}, document.title, 'edit.html?id=' + style.id);
|
|
|
|
$('#heading').textContent = t('editStyleHeading');
|
|
|
|
}
|
|
|
|
livePreview.show(Boolean(style.id));
|
|
|
|
updateLivePreview();
|
|
|
|
},
|
|
|
|
|
|
|
|
async save() {
|
|
|
|
if (!dirty.isDirty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let newStyle = getModel();
|
|
|
|
if (!validate(newStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newStyle = await API.editSave(newStyle);
|
|
|
|
destroyRemovedSections();
|
2020-11-18 11:17:15 +00:00
|
|
|
sessionStore.justEditedStyleId = newStyle.id;
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.replaceStyle(newStyle, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
scrollToEditor(cm) {
|
|
|
|
const section = sections.find(s => s.cm === cm).el;
|
|
|
|
const bounds = section.getBoundingClientRect();
|
|
|
|
if (
|
|
|
|
(bounds.bottom > window.innerHeight && bounds.top > 0) ||
|
|
|
|
(bounds.top < 0 && bounds.bottom < window.innerHeight)
|
|
|
|
) {
|
|
|
|
if (bounds.top < 0) {
|
|
|
|
window.scrollBy(0, bounds.top - 1);
|
|
|
|
} else {
|
|
|
|
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-10-11 15:12:06 +00:00
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.ready = initSections(style.sections, {pristine: true});
|
|
|
|
|
|
|
|
/** @param {EditorSection} section */
|
2019-06-20 19:36:27 +00:00
|
|
|
function fitToContent(section) {
|
2020-11-18 11:17:15 +00:00
|
|
|
const {cm, cm: {display: {wrapper, sizer}}} = section;
|
2020-10-11 14:13:25 +00:00
|
|
|
if (cm.display.renderedView) {
|
2019-06-20 19:36:27 +00:00
|
|
|
resize();
|
|
|
|
} else {
|
2020-10-11 14:13:25 +00:00
|
|
|
cm.on('update', resize);
|
2019-06-20 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resize() {
|
2020-10-11 14:13:25 +00:00
|
|
|
let contentHeight = sizer.offsetHeight;
|
|
|
|
if (contentHeight < cm.defaultTextHeight()) {
|
2019-06-20 19:36:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-11 14:13:25 +00:00
|
|
|
if (headerOffset == null) {
|
2020-11-18 11:17:15 +00:00
|
|
|
headerOffset = container.getBoundingClientRect().top;
|
2019-06-20 19:36:27 +00:00
|
|
|
}
|
2020-10-11 14:13:25 +00:00
|
|
|
contentHeight += 9; // border & resize grip
|
|
|
|
cm.off('update', resize);
|
|
|
|
const cmHeight = wrapper.offsetHeight;
|
2020-11-18 11:17:15 +00:00
|
|
|
const appliesToHeight = Math.min(section.el.offsetHeight - cmHeight, window.innerHeight / 2);
|
|
|
|
const maxHeight = (window.innerHeight - headerOffset) - appliesToHeight;
|
2020-11-08 08:12:42 +00:00
|
|
|
const fit = Math.min(contentHeight, maxHeight);
|
|
|
|
if (Math.abs(fit - cmHeight) > 1) {
|
|
|
|
cm.setSize(null, fit);
|
|
|
|
}
|
2019-06-20 19:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fitToAvailableSpace() {
|
2020-11-08 08:12:42 +00:00
|
|
|
const lastSectionBottom = sections[sections.length - 1].el.getBoundingClientRect().bottom;
|
|
|
|
const delta = Math.floor((window.innerHeight - lastSectionBottom) / sections.length);
|
2020-10-13 18:19:12 +00:00
|
|
|
if (delta > 1) {
|
|
|
|
sections.forEach(({cm}) => {
|
2020-11-08 08:12:42 +00:00
|
|
|
cm.setSize(null, cm.display.lastWrapHeight + delta);
|
2020-10-13 18:19:12 +00:00
|
|
|
});
|
2019-06-20 19:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function genId() {
|
|
|
|
return INC_ID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalProgress(done, total) {
|
|
|
|
const progressElement = $('#global-progress') ||
|
|
|
|
total && document.body.appendChild($create('#global-progress'));
|
|
|
|
if (total) {
|
|
|
|
const progress = (done / Math.max(done, total) * 100).toFixed(1);
|
|
|
|
progressElement.style.borderLeftWidth = progress + 'vw';
|
|
|
|
setTimeout(() => {
|
|
|
|
progressElement.title = progress + '%';
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$.remove(progressElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showToMozillaHelp(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
showHelp(t('styleMozillaFormatHeading'), t('styleToMozillaFormatHelp'));
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/**
|
|
|
|
priority:
|
|
|
|
1. associated CM for applies-to element
|
|
|
|
2. last active if visible
|
|
|
|
3. first visible
|
|
|
|
*/
|
2020-12-08 15:34:16 +00:00
|
|
|
function closestVisible(el) {
|
2018-11-07 06:09:29 +00:00
|
|
|
// closest editor should have at least 2 lines visible
|
|
|
|
const lineHeight = sections[0].cm.defaultTextHeight();
|
2020-12-08 15:34:16 +00:00
|
|
|
const margin = 2 * lineHeight;
|
|
|
|
const cm = el instanceof CodeMirror ? el :
|
|
|
|
el instanceof Node && getAssociatedEditor(el) || getLastActivatedEditor();
|
|
|
|
if (el === cm) el = document.body;
|
|
|
|
if (el instanceof Node && cm) {
|
|
|
|
const {wrapper} = cm.display;
|
|
|
|
if (!container.contains(el) || wrapper.closest('.section').contains(el)) {
|
|
|
|
const rect = wrapper.getBoundingClientRect();
|
|
|
|
if (rect.top < window.innerHeight - margin && rect.bottom > margin) {
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
const scrollY = window.scrollY;
|
2020-12-08 15:34:16 +00:00
|
|
|
const windowBottom = scrollY + window.innerHeight - margin;
|
|
|
|
const allSectionsContainerTop = scrollY + container.getBoundingClientRect().top;
|
2018-11-07 06:09:29 +00:00
|
|
|
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];
|
|
|
|
}
|
2018-12-19 13:09:16 +00:00
|
|
|
const section = cm && cm.display.wrapper.closest('.section');
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!section) {
|
|
|
|
return 1e9;
|
|
|
|
}
|
|
|
|
const top = allSectionsContainerTop + section.offsetTop;
|
|
|
|
if (top < scrollY + lineHeight) {
|
|
|
|
return Math.max(0, scrollY - top - lineHeight);
|
|
|
|
}
|
|
|
|
if (top < windowBottom) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const distance = top - windowBottom + section.offsetHeight;
|
|
|
|
if (index >= 0) {
|
|
|
|
distances[index] = distance;
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findClosest() {
|
2020-11-08 08:12:42 +00:00
|
|
|
const editors = editor.getEditors();
|
2018-11-07 06:09:29 +00:00
|
|
|
const last = editors.length - 1;
|
|
|
|
let a = 0;
|
|
|
|
let b = last;
|
|
|
|
let c;
|
|
|
|
let distance;
|
|
|
|
while (a < b - 1) {
|
|
|
|
c = (a + b) / 2 | 0;
|
|
|
|
distance = offscreenDistance(c);
|
|
|
|
if (!distance || !c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const distancePrev = offscreenDistance(c - 1);
|
|
|
|
const distanceNext = c < last ? offscreenDistance(c + 1) : 1e20;
|
|
|
|
if (distancePrev <= distance && distance <= distanceNext) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (b && offscreenDistance(b - 1) <= offscreenDistance(b)) {
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
const cm = editors[b];
|
|
|
|
if (distances[b] > 0) {
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.scrollToEditor(cm);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:14:41 +00:00
|
|
|
function getAssociatedEditor(nearbyElement) {
|
|
|
|
for (let el = nearbyElement; el; el = el.parentElement) {
|
|
|
|
// added by createSection
|
|
|
|
if (el.CodeMirror) {
|
|
|
|
return el.CodeMirror;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:47:28 +00:00
|
|
|
function findLast(arr, match) {
|
|
|
|
for (let i = arr.length - 1; i >= 0; i--) {
|
|
|
|
if (match(arr[i])) {
|
|
|
|
return arr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function nextPrevEditor(cm, direction) {
|
2020-11-08 08:12:42 +00:00
|
|
|
const editors = editor.getEditors();
|
2018-11-07 06:09:29 +00:00
|
|
|
cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.scrollToEditor(cm);
|
2018-11-07 06:09:29 +00:00
|
|
|
cm.focus();
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastActivatedEditor() {
|
|
|
|
let result;
|
|
|
|
for (const section of sections) {
|
2020-11-08 08:12:42 +00:00
|
|
|
if (section.removed) {
|
2018-11-07 06:09:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// .lastActive is initiated by codemirror-factory
|
|
|
|
if (!result || section.cm.lastActive > result.lastActive) {
|
|
|
|
result = section.cm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function scrollEntirePageOnCtrlShift(event) {
|
|
|
|
// make Shift-Ctrl-Wheel scroll entire page even when mouse is over a code editor
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMozillaFormat() {
|
|
|
|
const popup = showCodeMirrorPopup(t('styleToMozillaFormatTitle'), '', {readOnly: true});
|
|
|
|
popup.codebox.setValue(sectionsToMozFormat(getModel()));
|
|
|
|
popup.codebox.execCommand('selectAll');
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMozillaFormatImport(text = '') {
|
|
|
|
const popup = showCodeMirrorPopup(t('styleFromMozillaFormatPrompt'),
|
|
|
|
$create('.buttons', [
|
|
|
|
$create('button', {
|
|
|
|
name: 'import-replace',
|
|
|
|
textContent: t('importReplaceLabel'),
|
|
|
|
title: 'Ctrl-Shift-Enter:\n' + t('importReplaceTooltip'),
|
|
|
|
onclick: () => doImport({replaceOldStyle: true}),
|
|
|
|
}),
|
|
|
|
$create('button', {
|
|
|
|
name: 'import-append',
|
|
|
|
textContent: t('importAppendLabel'),
|
|
|
|
title: 'Ctrl-Enter:\n' + t('importAppendTooltip'),
|
|
|
|
onclick: doImport,
|
|
|
|
}),
|
|
|
|
]));
|
|
|
|
const contents = $('.contents', popup);
|
|
|
|
contents.insertBefore(popup.codebox.display.wrapper, contents.firstElementChild);
|
|
|
|
popup.codebox.focus();
|
|
|
|
popup.codebox.on('changes', cm => {
|
|
|
|
popup.classList.toggle('ready', !cm.isBlank());
|
|
|
|
cm.markClean();
|
|
|
|
});
|
|
|
|
if (text) {
|
|
|
|
popup.codebox.setValue(text);
|
|
|
|
popup.codebox.clearHistory();
|
|
|
|
popup.codebox.markClean();
|
|
|
|
}
|
|
|
|
// overwrite default extraKeys as those are inapplicable in popup context
|
|
|
|
popup.codebox.options.extraKeys = {
|
|
|
|
'Ctrl-Enter': doImport,
|
|
|
|
'Shift-Ctrl-Enter': () => doImport({replaceOldStyle: true}),
|
|
|
|
};
|
|
|
|
|
2020-10-25 19:36:41 +00:00
|
|
|
async function doImport({replaceOldStyle = false}) {
|
2018-11-07 06:09:29 +00:00
|
|
|
lockPageUI(true);
|
2020-10-25 19:36:41 +00:00
|
|
|
try {
|
|
|
|
const code = popup.codebox.getValue().trim();
|
|
|
|
if (!/==userstyle==/i.test(code) ||
|
|
|
|
!await getPreprocessor(code) ||
|
|
|
|
await messageBox.confirm(
|
|
|
|
t('importPreprocessor'), 'pre-line',
|
|
|
|
t('importPreprocessorTitle'))
|
|
|
|
) {
|
|
|
|
const {sections, errors} = await API.parseCss({code});
|
2018-11-07 06:09:29 +00:00
|
|
|
// shouldn't happen but just in case
|
|
|
|
if (!sections.length || errors.length) {
|
|
|
|
throw errors;
|
|
|
|
}
|
2020-10-26 14:24:11 +00:00
|
|
|
await initSections(sections, {
|
|
|
|
replace: replaceOldStyle,
|
|
|
|
focusOn: replaceOldStyle ? 0 : false,
|
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
$('.dismiss').dispatchEvent(new Event('click'));
|
2020-10-25 19:36:41 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
showError(err);
|
|
|
|
}
|
|
|
|
lockPageUI(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPreprocessor(code) {
|
|
|
|
try {
|
|
|
|
return (await API.buildUsercssMeta({sourceCode: code})).usercssData.preprocessor;
|
|
|
|
} catch (e) {}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function lockPageUI(locked) {
|
|
|
|
document.documentElement.style.pointerEvents = locked ? 'none' : '';
|
|
|
|
if (popup.codebox) {
|
|
|
|
popup.classList.toggle('ready', locked ? false : !popup.codebox.isBlank());
|
|
|
|
popup.codebox.options.readOnly = locked;
|
|
|
|
popup.codebox.display.wrapper.style.opacity = locked ? '.5' : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showError(errors) {
|
|
|
|
messageBox({
|
|
|
|
className: 'center danger',
|
|
|
|
title: t('styleFromMozillaFormatError'),
|
|
|
|
contents: $create('pre', Array.isArray(errors) ? errors.join('\n') : errors),
|
|
|
|
buttons: [t('confirmClose')],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSectionOrder() {
|
|
|
|
const oldOrder = sectionOrder;
|
2020-11-08 08:12:42 +00:00
|
|
|
const validSections = sections.filter(s => !s.removed);
|
2018-11-07 06:09:29 +00:00
|
|
|
sectionOrder = validSections.map(s => s.id).join(',');
|
|
|
|
dirty.modify('sectionOrder', oldOrder, sectionOrder);
|
|
|
|
container.dataset.sectionCount = validSections.length;
|
|
|
|
linter.refreshReport();
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.updateToc();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @returns {Style} */
|
2018-11-07 06:09:29 +00:00
|
|
|
function getModel() {
|
|
|
|
return Object.assign({}, style, {
|
2020-11-18 11:17:15 +00:00
|
|
|
sections: sections.filter(s => !s.removed).map(s => s.getModel()),
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate() {
|
2020-10-11 15:12:06 +00:00
|
|
|
if (!$('#name').reportValidity()) {
|
2018-11-07 06:09:29 +00:00
|
|
|
messageBox.alert(t('styleMissingName'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const section of sections) {
|
|
|
|
for (const apply of section.appliesTo) {
|
2020-11-08 08:12:42 +00:00
|
|
|
if (apply.type !== 'regexp') {
|
2018-11-07 06:09:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!apply.valueEl.reportValidity()) {
|
|
|
|
messageBox.alert(t('styleBadRegexp'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyRemovedSections() {
|
|
|
|
for (let i = 0; i < sections.length;) {
|
2020-11-08 08:12:42 +00:00
|
|
|
if (!sections[i].removed) {
|
2018-11-07 06:09:29 +00:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sections[i].destroy();
|
|
|
|
sections[i].el.remove();
|
|
|
|
sections.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateHeader() {
|
2020-10-11 15:12:06 +00:00
|
|
|
$('#name').value = style.customName || style.name || '';
|
|
|
|
$('#enabled').checked = style.enabled !== false;
|
2018-11-07 06:09:29 +00:00
|
|
|
$('#url').href = style.url || '';
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.updateName();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLivePreview() {
|
2020-11-08 08:12:42 +00:00
|
|
|
debounce(updateLivePreviewNow, editor.previewDelay);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateLivePreviewNow() {
|
2018-11-07 06:09:29 +00:00
|
|
|
livePreview.update(getModel());
|
|
|
|
}
|
|
|
|
|
2020-11-28 22:07:05 +00:00
|
|
|
async function initSections(src, {
|
2018-11-07 06:09:29 +00:00
|
|
|
focusOn = 0,
|
2020-10-26 14:24:11 +00:00
|
|
|
replace = false,
|
|
|
|
pristine = false,
|
2020-10-11 14:13:25 +00:00
|
|
|
} = {}) {
|
2020-10-26 14:24:11 +00:00
|
|
|
if (replace) {
|
|
|
|
sections.forEach(s => s.remove(true));
|
|
|
|
sections.length = 0;
|
|
|
|
container.textContent = '';
|
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
let si = editor.scrollInfo;
|
|
|
|
if (si && si.cms && si.cms.length === src.length) {
|
|
|
|
si.scrollY2 = si.scrollY + window.innerHeight;
|
|
|
|
container.style.height = si.scrollY2 + 'px';
|
|
|
|
scrollTo(0, si.scrollY);
|
2020-11-21 21:12:32 +00:00
|
|
|
rerouteHotkeys(true);
|
2020-11-18 11:17:15 +00:00
|
|
|
} else {
|
|
|
|
si = null;
|
|
|
|
}
|
2020-11-28 22:07:05 +00:00
|
|
|
let forceRefresh = true;
|
|
|
|
let y = 0;
|
|
|
|
let tPrev;
|
|
|
|
for (let i = 0; i < src.length; i++) {
|
|
|
|
const t = performance.now();
|
|
|
|
if (!tPrev) {
|
|
|
|
tPrev = t;
|
|
|
|
} else if (t - tPrev > 100) {
|
|
|
|
tPrev = 0;
|
|
|
|
forceRefresh = false;
|
|
|
|
await new Promise(setTimeout);
|
2020-10-11 14:13:25 +00:00
|
|
|
}
|
2020-11-28 22:07:05 +00:00
|
|
|
if (si) forceRefresh = y < si.scrollY2 && (y += si.cms[i].parentHeight) > si.scrollY;
|
|
|
|
insertSectionAfter(src[i], null, forceRefresh, si && si.cms[i]);
|
|
|
|
setGlobalProgress(i, src.length);
|
|
|
|
if (pristine) dirty.clear();
|
|
|
|
if (i === focusOn && !si) sections[i].cm.focus();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-11-28 22:07:05 +00:00
|
|
|
if (!si) requestAnimationFrame(fitToAvailableSpace);
|
|
|
|
container.style.removeProperty('height');
|
|
|
|
setGlobalProgress();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @param {EditorSection} section */
|
2018-11-07 06:09:29 +00:00
|
|
|
function removeSection(section) {
|
2020-11-08 08:12:42 +00:00
|
|
|
if (sections.every(s => s.removed || s === section)) {
|
2018-11-07 06:09:29 +00:00
|
|
|
// TODO: hide remove button when `#sections[data-section-count=1]`
|
|
|
|
throw new Error('Cannot remove last section');
|
|
|
|
}
|
|
|
|
if (section.cm.isBlank()) {
|
|
|
|
const index = sections.indexOf(section);
|
|
|
|
sections.splice(index, 1);
|
|
|
|
section.el.remove();
|
|
|
|
section.remove();
|
|
|
|
section.destroy();
|
|
|
|
} else {
|
|
|
|
const lines = [];
|
|
|
|
const MAX_LINES = 10;
|
|
|
|
section.cm.doc.iter(0, MAX_LINES + 1, ({text}) => lines.push(text) && false);
|
|
|
|
const title = t('sectionCode') + '\n' +
|
|
|
|
'-'.repeat(20) + '\n' +
|
|
|
|
lines.slice(0, MAX_LINES).map(s => clipString(s, 100)).join('\n') +
|
|
|
|
(lines.length > MAX_LINES ? '\n...' : '');
|
|
|
|
$('.deleted-section', section.el).title = title;
|
|
|
|
section.remove();
|
|
|
|
}
|
|
|
|
dirty.remove(section, section);
|
|
|
|
updateSectionOrder();
|
|
|
|
section.off(updateLivePreview);
|
|
|
|
updateLivePreview();
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @param {EditorSection} section */
|
2018-11-07 06:09:29 +00:00
|
|
|
function restoreSection(section) {
|
|
|
|
section.restore();
|
|
|
|
updateSectionOrder();
|
|
|
|
section.onChange(updateLivePreview);
|
|
|
|
updateLivePreview();
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/**
|
|
|
|
* @param {StyleSection} [init]
|
|
|
|
* @param {EditorSection} [base]
|
|
|
|
* @param {boolean} [forceRefresh]
|
2020-11-18 11:17:15 +00:00
|
|
|
* @param {EditorScrollInfo} [si]
|
2020-11-08 08:12:42 +00:00
|
|
|
*/
|
2020-11-18 11:17:15 +00:00
|
|
|
function insertSectionAfter(init, base, forceRefresh, si) {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!init) {
|
|
|
|
init = {code: '', urlPrefixes: ['http://example.com']};
|
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
const section = createSection(init, genId, si);
|
2020-10-11 14:13:25 +00:00
|
|
|
const {cm} = section;
|
2020-11-28 22:07:05 +00:00
|
|
|
const {code} = init;
|
2020-11-18 11:17:15 +00:00
|
|
|
const index = base ? sections.indexOf(base) + 1 : sections.length;
|
|
|
|
sections.splice(index, 0, section);
|
2020-10-11 14:13:25 +00:00
|
|
|
container.insertBefore(section.el, base ? base.el.nextSibling : null);
|
2020-11-28 22:07:05 +00:00
|
|
|
refreshOnView(cm, {code, force: base || forceRefresh});
|
2020-11-08 08:12:42 +00:00
|
|
|
registerEvents(section);
|
2020-11-28 22:07:05 +00:00
|
|
|
if ((!si || !si.height) && (!base || code)) {
|
2020-10-11 14:13:25 +00:00
|
|
|
// Fit a) during startup or b) when the clone button is clicked on a section with some code
|
|
|
|
fitToContent(section);
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
if (base) {
|
2020-10-11 14:13:25 +00:00
|
|
|
cm.focus();
|
2020-11-18 11:17:15 +00:00
|
|
|
editor.scrollToEditor(cm);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
updateSectionOrder();
|
|
|
|
updateLivePreview();
|
2020-11-28 22:07:05 +00:00
|
|
|
section.onChange(updateLivePreview);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @param {EditorSection} section */
|
2018-11-07 06:09:29 +00:00
|
|
|
function moveSectionUp(section) {
|
|
|
|
const index = sections.indexOf(section);
|
|
|
|
if (index === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
container.insertBefore(section.el, sections[index - 1].el);
|
|
|
|
sections[index] = sections[index - 1];
|
|
|
|
sections[index - 1] = section;
|
|
|
|
updateSectionOrder();
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @param {EditorSection} section */
|
2018-11-07 06:09:29 +00:00
|
|
|
function moveSectionDown(section) {
|
|
|
|
const index = sections.indexOf(section);
|
|
|
|
if (index === sections.length - 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
container.insertBefore(sections[index + 1].el, section.el);
|
|
|
|
sections[index] = sections[index + 1];
|
|
|
|
sections[index + 1] = section;
|
|
|
|
updateSectionOrder();
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @param {EditorSection} section */
|
|
|
|
function registerEvents(section) {
|
|
|
|
const {el, cm} = section;
|
|
|
|
$('.applies-to-help', el).onclick = () => showHelp(t('appliesLabel'), t('appliesHelp'));
|
|
|
|
$('.remove-section', el).onclick = () => removeSection(section);
|
|
|
|
$('.add-section', el).onclick = () => insertSectionAfter(undefined, section);
|
|
|
|
$('.clone-section', el).onclick = () => insertSectionAfter(section.getModel(), section);
|
|
|
|
$('.move-section-up', el).onclick = () => moveSectionUp(section);
|
|
|
|
$('.move-section-down', el).onclick = () => moveSectionDown(section);
|
|
|
|
$('.restore-section', el).onclick = () => restoreSection(section);
|
|
|
|
cm.on('paste', maybeImportOnPaste);
|
|
|
|
if (!FIREFOX) {
|
|
|
|
cm.on('mousedown', (cm, event) => toggleContextMenuDelete.call(cm, event));
|
2020-10-26 14:33:23 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function maybeImportOnPaste(cm, event) {
|
|
|
|
const text = event.clipboardData.getData('text') || '';
|
|
|
|
if (/@-moz-document/i.test(text) &&
|
|
|
|
/@-moz-document\s+(url|url-prefix|domain|regexp)\(/i
|
|
|
|
.test(text.replace(/\/\*([^*]|\*(?!\/))*(\*\/|$)/g, ''))
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
showMozillaFormatImport(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 22:07:05 +00:00
|
|
|
function refreshOnView(cm, {code, force} = {}) {
|
|
|
|
if (force || !xo) {
|
|
|
|
refreshOnViewNow(cm, code);
|
|
|
|
} else {
|
2020-11-08 08:12:42 +00:00
|
|
|
xo.observe(cm.display.wrapper);
|
2020-11-28 22:07:05 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
/** @param {IntersectionObserverEntry[]} entries */
|
2020-11-08 08:12:42 +00:00
|
|
|
function refreshOnViewListener(entries) {
|
2020-11-18 11:17:15 +00:00
|
|
|
for (const e of entries) {
|
|
|
|
const r = e.isIntersecting && e.intersectionRect;
|
|
|
|
if (r) {
|
|
|
|
xo.unobserve(e.target);
|
|
|
|
const cm = e.target.CodeMirror;
|
|
|
|
if (r.bottom > 0 && r.top < window.innerHeight) {
|
2020-11-28 22:07:05 +00:00
|
|
|
refreshOnViewNow(cm);
|
2020-11-18 11:17:15 +00:00
|
|
|
} else {
|
2020-11-28 22:07:05 +00:00
|
|
|
setTimeout(refreshOnViewNow, 0, cm);
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 22:07:05 +00:00
|
|
|
async function refreshOnViewNow(cm, code) {
|
|
|
|
cm.refresh();
|
|
|
|
linter.enableForEditor(cm, code);
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function toggleContextMenuDelete(event) {
|
|
|
|
if (chrome.contextMenus && event.button === 2 && prefs.get('editor.contextDelete')) {
|
|
|
|
chrome.contextMenus.update('editor.contextDelete', {
|
|
|
|
enabled: Boolean(
|
|
|
|
this.selectionStart !== this.selectionEnd ||
|
|
|
|
this.somethingSelected && this.somethingSelected()
|
|
|
|
),
|
|
|
|
}, ignoreChromeError);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|