2022-01-28 00:11:25 +00:00
|
|
|
/* global $ $create $remove messageBoxProxy */// dom.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global API */// msg.js
|
|
|
|
/* global CodeMirror */
|
2022-01-28 00:11:25 +00:00
|
|
|
/* global RX_META debounce */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global MozDocMapper clipString helpPopup rerouteHotkeys showCodeMirrorPopup */// util.js
|
|
|
|
/* global createSection */// sections-editor-section.js
|
|
|
|
/* global editor */
|
|
|
|
/* global linterMan */
|
2022-01-10 16:12:29 +00:00
|
|
|
/* global styleSectionsEqual */ // sections-util.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global t */// localization.js
|
2018-11-07 06:09:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported SectionsEditor */
|
|
|
|
function SectionsEditor() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {style, /** @type DirtyReporter */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%'});
|
|
|
|
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
|
2021-02-25 07:04:25 +00:00
|
|
|
let cmExtrasHeight; // resize grip + borders
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
updateMeta();
|
2021-02-02 19:45:47 +00:00
|
|
|
rerouteHotkeys.toggle(true); // enabled initially because we don't always focus a CodeMirror
|
2021-07-30 12:44:06 +00:00
|
|
|
editor.livePreview.init();
|
2021-01-01 14:27:58 +00:00
|
|
|
container.classList.add('section-editor');
|
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
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @namespace Editor */
|
2020-11-08 08:12:42 +00:00
|
|
|
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,
|
2021-12-29 19:57:22 +00:00
|
|
|
updateMeta,
|
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}`;
|
|
|
|
},
|
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
getValue(asObject) {
|
|
|
|
const st = getModel();
|
|
|
|
return asObject ? st : MozDocMapper.styleToCss(st);
|
|
|
|
},
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
getSearchableInputs(cm) {
|
2021-02-02 19:45:47 +00:00
|
|
|
const sec = sections.find(s => s.cm === cm);
|
|
|
|
return sec ? sec.appliesTo.map(a => a.valueEl).filter(Boolean) : [];
|
2020-11-08 08:12:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2022-01-23 09:44:25 +00:00
|
|
|
async replaceStyle(newStyle, draft) {
|
2022-01-10 16:12:29 +00:00
|
|
|
const sameCode = styleSectionsEqual(newStyle, getModel());
|
2022-01-23 09:44:25 +00:00
|
|
|
if (!sameCode && !draft && !await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) {
|
2022-01-10 16:12:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-23 09:44:25 +00:00
|
|
|
if (!draft) {
|
|
|
|
dirty.clear();
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
// FIXME: avoid recreating all editors?
|
2022-01-10 16:12:29 +00:00
|
|
|
if (!sameCode) {
|
2022-01-23 09:44:25 +00:00
|
|
|
await initSections(newStyle.sections, {
|
|
|
|
keepDirty: draft,
|
|
|
|
replace: true,
|
|
|
|
si: draft && draft.si,
|
|
|
|
});
|
2022-01-10 16:12:29 +00:00
|
|
|
}
|
2022-01-10 18:29:07 +00:00
|
|
|
editor.useSavedStyle(newStyle);
|
2020-11-08 08:12:42 +00:00
|
|
|
updateLivePreview();
|
|
|
|
},
|
|
|
|
|
2022-01-10 16:12:29 +00:00
|
|
|
async saveImpl() {
|
2020-11-08 08:12:42 +00:00
|
|
|
let newStyle = getModel();
|
|
|
|
if (!validate(newStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
newStyle = await API.styles.editSave(newStyle);
|
2022-01-10 14:57:46 +00:00
|
|
|
dirty.clear();
|
2022-01-10 18:29:07 +00:00
|
|
|
editor.useSavedStyle(newStyle);
|
2020-11-08 08:12:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
scrollToEditor(cm) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {el} = sections.find(s => s.cm === cm);
|
|
|
|
const r = el.getBoundingClientRect();
|
|
|
|
const h = window.innerHeight;
|
|
|
|
if (r.bottom > h && r.top > 0 ||
|
|
|
|
r.bottom < h && r.top < 0) {
|
|
|
|
window.scrollBy(0, (r.top + r.bottom - h) / 2 | 0);
|
2020-11-08 08:12:42 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-11 15:12:06 +00:00
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
editor.ready = initSections(style.sections);
|
2020-11-08 08:12:42 +00:00
|
|
|
|
|
|
|
/** @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) {
|
2021-02-25 07:04:25 +00:00
|
|
|
headerOffset = Math.ceil(container.getBoundingClientRect().top + scrollY);
|
2019-06-20 19:36:27 +00:00
|
|
|
}
|
2021-02-25 07:04:25 +00:00
|
|
|
if (cmExtrasHeight == null) {
|
|
|
|
cmExtrasHeight = $('.resize-grip', wrapper).offsetHeight + // grip
|
|
|
|
wrapper.offsetHeight - wrapper.clientHeight; // borders
|
|
|
|
}
|
|
|
|
contentHeight += cmExtrasHeight;
|
2020-10-11 14:13:25 +00:00
|
|
|
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);
|
2021-02-25 07:04:25 +00:00
|
|
|
const maxHeight = Math.floor(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 {
|
2021-01-01 14:27:58 +00:00
|
|
|
$remove(progressElement);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showToMozillaHelp(event) {
|
|
|
|
event.preventDefault();
|
2021-01-01 14:27:58 +00:00
|
|
|
helpPopup.show(t('styleMozillaFormatHeading'), t('styleToMozillaFormatHelp'));
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
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});
|
2021-07-30 12:44:06 +00:00
|
|
|
popup.codebox.setValue(editor.getValue());
|
2018-11-07 06:09:29 +00:00
|
|
|
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();
|
2021-01-26 13:33:17 +00:00
|
|
|
if (!RX_META.test(code) ||
|
2020-10-25 19:36:41 +00:00
|
|
|
!await getPreprocessor(code) ||
|
2021-01-01 14:27:58 +00:00
|
|
|
await messageBoxProxy.confirm(
|
2020-10-25 19:36:41 +00:00
|
|
|
t('importPreprocessor'), 'pre-line',
|
|
|
|
t('importPreprocessorTitle'))
|
|
|
|
) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {sections, errors} = await API.worker.parseMozFormat({code});
|
|
|
|
if (!sections.length || errors.some(e => !e.recoverable)) {
|
|
|
|
await Promise.reject(errors);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-10-26 14:24:11 +00:00
|
|
|
await initSections(sections, {
|
|
|
|
replace: replaceOldStyle,
|
|
|
|
focusOn: replaceOldStyle ? 0 : false,
|
2021-02-01 21:33:41 +00:00
|
|
|
keepDirty: true,
|
2020-10-26 14:24:11 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
helpPopup.close();
|
2020-10-25 19:36:41 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
showError(err);
|
|
|
|
}
|
|
|
|
lockPageUI(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPreprocessor(code) {
|
|
|
|
try {
|
2021-01-01 14:27:58 +00:00
|
|
|
return (await API.usercss.buildMeta({sourceCode: code})).usercssData.preprocessor;
|
2020-10-25 19:36:41 +00:00
|
|
|
} 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) {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBoxProxy.show({
|
2018-11-07 06:09:29 +00:00
|
|
|
className: 'center danger',
|
|
|
|
title: t('styleFromMozillaFormatError'),
|
2021-01-01 14:27:58 +00:00
|
|
|
contents: $create('pre',
|
|
|
|
(Array.isArray(errors) ? errors : [errors])
|
|
|
|
.map(e => e.message || e).join('\n')),
|
2018-11-07 06:09:29 +00:00
|
|
|
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;
|
2021-01-01 14:27:58 +00:00
|
|
|
linterMan.refreshReport();
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.updateToc();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
/** @returns {StyleObj} */
|
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()) {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBoxProxy.alert(t('styleMissingName'));
|
2018-11-07 06:09:29 +00:00
|
|
|
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()) {
|
2021-01-01 14:27:58 +00:00
|
|
|
messageBoxProxy.alert(t('styleBadRegexp'));
|
2018-11-07 06:09:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function updateMeta() {
|
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() {
|
2021-01-01 14:27:58 +00:00
|
|
|
editor.livePreview.update(getModel());
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2022-01-23 09:44:25 +00:00
|
|
|
keepDirty = false,
|
|
|
|
si = editor.scrollInfo,
|
2020-10-11 14:13:25 +00:00
|
|
|
} = {}) {
|
2022-01-23 09:44:25 +00:00
|
|
|
editor.ready = false;
|
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
|
|
|
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);
|
2021-01-01 14:27:58 +00:00
|
|
|
// only restore focus if it's the first CM to avoid derpy quirks
|
|
|
|
focusOn = si.cms[0].focus && 0;
|
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);
|
2021-02-01 21:33:41 +00:00
|
|
|
if (!keepDirty) dirty.clear();
|
2021-01-01 14:27:58 +00:00
|
|
|
if (i === focusOn) sections[i].cm.focus();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2022-01-23 09:44:25 +00:00
|
|
|
if (!si || si.cms.every(cm => !cm.height)) {
|
|
|
|
requestAnimationFrame(fitToAvailableSpace);
|
|
|
|
}
|
2020-11-28 22:07:05 +00:00
|
|
|
container.style.removeProperty('height');
|
|
|
|
setGlobalProgress();
|
2022-01-23 09:44:25 +00:00
|
|
|
editor.ready = true;
|
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;
|
2021-01-01 14:27:58 +00:00
|
|
|
$('.applies-to-help', el).onclick = () => helpPopup.show(t('appliesLabel'), t('appliesHelp'));
|
2020-11-08 08:12:42 +00:00
|
|
|
$('.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeImportOnPaste(cm, event) {
|
|
|
|
const text = event.clipboardData.getData('text') || '';
|
|
|
|
if (/@-moz-document/i.test(text) &&
|
2021-01-01 14:27:58 +00:00
|
|
|
/@-moz-document\s+(url|url-prefix|domain|regexp)\(/i
|
|
|
|
.test(text.replace(/\/\*([^*]|\*(?!\/))*(\*\/|$)/g, ''))
|
2020-11-08 08:12:42 +00:00
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
showMozillaFormatImport(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 22:07:05 +00:00
|
|
|
function refreshOnView(cm, {code, force} = {}) {
|
2020-12-10 07:59:28 +00:00
|
|
|
if (code) {
|
2021-01-01 14:27:58 +00:00
|
|
|
linterMan.enableForEditor(cm, code);
|
2020-12-10 07:59:28 +00:00
|
|
|
}
|
2020-11-28 22:07:05 +00:00
|
|
|
if (force || !xo) {
|
2020-12-10 07:59:28 +00:00
|
|
|
refreshOnViewNow(cm);
|
2020-11-28 22:07:05 +00:00
|
|
|
} 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-12-10 07:59:28 +00:00
|
|
|
async function refreshOnViewNow(cm) {
|
2021-01-01 14:27:58 +00:00
|
|
|
linterMan.enableForEditor(cm);
|
2020-11-28 22:07:05 +00:00
|
|
|
cm.refresh();
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|