2020-10-11 15:12:06 +00:00
|
|
|
/* global
|
2020-11-08 08:12:42 +00:00
|
|
|
$
|
|
|
|
$$
|
|
|
|
$create
|
|
|
|
API
|
|
|
|
chromeSync
|
|
|
|
cmFactory
|
|
|
|
CodeMirror
|
|
|
|
createLivePreview
|
|
|
|
createMetaCompiler
|
|
|
|
debounce
|
|
|
|
editor
|
|
|
|
linter
|
|
|
|
messageBox
|
|
|
|
MozSectionFinder
|
|
|
|
MozSectionWidget
|
|
|
|
prefs
|
2018-11-07 06:09:29 +00:00
|
|
|
sectionsToMozFormat
|
2020-11-08 08:12:42 +00:00
|
|
|
t
|
|
|
|
*/
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported SourceEditor */
|
2020-10-11 15:12:06 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function SourceEditor() {
|
|
|
|
const {style, dirty} = editor;
|
|
|
|
let savedGeneration;
|
2020-10-22 20:47:46 +00:00
|
|
|
let placeholderName = '';
|
2020-11-08 08:12:42 +00:00
|
|
|
let prevMode = NaN;
|
2020-10-22 20:47:46 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
$$.remove('.sectioned-only');
|
|
|
|
$('#header').on('wheel', headerOnScroll);
|
2017-11-26 13:04:03 +00:00
|
|
|
$('#sections').textContent = '';
|
2017-12-03 21:12:09 +00:00
|
|
|
$('#sections').appendChild($create('.single-editor'));
|
2017-09-11 16:09:25 +00:00
|
|
|
|
2018-01-05 10:26:11 +00:00
|
|
|
if (!style.id) setupNewStyle(style);
|
2017-10-08 15:26:55 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
const cm = cmFactory.create($('.single-editor'));
|
|
|
|
const sectionFinder = MozSectionFinder(cm);
|
|
|
|
const sectionWidget = MozSectionWidget(cm, sectionFinder, editor.updateToc);
|
|
|
|
const livePreview = createLivePreview(preprocess, style.id);
|
|
|
|
/** @namespace SourceEditor */
|
|
|
|
Object.assign(editor, {
|
|
|
|
sections: sectionFinder.sections,
|
|
|
|
replaceStyle,
|
|
|
|
getEditors: () => [cm],
|
|
|
|
scrollToEditor: () => {},
|
|
|
|
getEditorTitle: () => '',
|
|
|
|
save,
|
|
|
|
prevEditor: nextPrevSection.bind(null, -1),
|
|
|
|
nextEditor: nextPrevSection.bind(null, 1),
|
|
|
|
jumpToEditor(i) {
|
|
|
|
const sec = sectionFinder.sections[i];
|
|
|
|
if (sec) {
|
|
|
|
sectionFinder.updatePositions(sec);
|
|
|
|
jumpToPos(sec.start);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
closestVisible: () => cm,
|
|
|
|
getSearchableInputs: () => [],
|
|
|
|
updateLivePreview,
|
2018-01-05 10:20:46 +00:00
|
|
|
});
|
2020-11-08 08:12:42 +00:00
|
|
|
createMetaCompiler(cm, meta => {
|
2018-10-01 14:03:17 +00:00
|
|
|
style.usercssData = meta;
|
|
|
|
style.name = meta.name;
|
2020-10-07 14:59:21 +00:00
|
|
|
style.url = meta.homepageURL || style.installationUrl;
|
2018-10-01 14:03:17 +00:00
|
|
|
updateMeta();
|
|
|
|
});
|
2020-11-08 08:12:42 +00:00
|
|
|
updateMeta();
|
|
|
|
cm.setValue(style.sourceCode);
|
|
|
|
prefs.subscribeMany({
|
|
|
|
'editor.linter': updateLinterSwitch,
|
|
|
|
'editor.appliesToLineWidget': (k, val) => sectionWidget.toggle(val),
|
|
|
|
'editor.toc.expanded': (k, val) => sectionFinder.onOff(editor.updateToc, val),
|
|
|
|
}, {now: true});
|
|
|
|
cm.clearHistory();
|
|
|
|
cm.markClean();
|
|
|
|
savedGeneration = cm.changeGeneration();
|
|
|
|
cm.on('changes', () => {
|
|
|
|
dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration());
|
|
|
|
debounce(updateLivePreview, editor.previewDelay);
|
|
|
|
});
|
|
|
|
cm.on('optionChange', (cm, option) => {
|
|
|
|
if (option !== 'mode') return;
|
|
|
|
const mode = getModeName();
|
|
|
|
if (mode === prevMode) return;
|
|
|
|
prevMode = mode;
|
|
|
|
linter.run();
|
2018-01-05 10:20:46 +00:00
|
|
|
updateLinterSwitch();
|
2017-11-22 00:38:29 +00:00
|
|
|
});
|
2020-11-08 08:12:42 +00:00
|
|
|
debounce(linter.enableForEditor, 0, cm);
|
|
|
|
if (!$.isTextInput(document.activeElement)) {
|
|
|
|
cm.focus();
|
|
|
|
}
|
2017-09-13 08:58:03 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function preprocess(style) {
|
|
|
|
return API.buildUsercss({
|
2018-11-25 13:27:11 +00:00
|
|
|
styleId: style.id,
|
2018-11-07 06:09:29 +00:00
|
|
|
sourceCode: style.sourceCode,
|
|
|
|
assignVars: true
|
|
|
|
})
|
|
|
|
.then(({style: newStyle}) => {
|
|
|
|
delete newStyle.enabled;
|
|
|
|
return Object.assign(style, newStyle);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLivePreview() {
|
|
|
|
if (!style.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
livePreview.update(Object.assign({}, style, {sourceCode: cm.getValue()}));
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:20:46 +00:00
|
|
|
function updateLinterSwitch() {
|
|
|
|
const el = $('#editor.linter');
|
2018-10-01 14:03:17 +00:00
|
|
|
el.value = getCurrentLinter();
|
2018-01-05 10:20:46 +00:00
|
|
|
const cssLintOption = $('[value="csslint"]', el);
|
|
|
|
const mode = getModeName();
|
|
|
|
if (mode !== 'css') {
|
|
|
|
cssLintOption.disabled = true;
|
|
|
|
cssLintOption.title = t('linterCSSLintIncompatible', mode);
|
|
|
|
} else {
|
|
|
|
cssLintOption.disabled = false;
|
|
|
|
cssLintOption.title = '';
|
2017-10-07 10:00:25 +00:00
|
|
|
}
|
2017-10-15 19:58:02 +00:00
|
|
|
}
|
2017-10-07 10:00:25 +00:00
|
|
|
|
2018-10-01 14:03:17 +00:00
|
|
|
function getCurrentLinter() {
|
|
|
|
const name = prefs.get('editor.linter');
|
|
|
|
if (cm.getOption('mode') !== 'css' && name === 'csslint') {
|
|
|
|
return 'stylelint';
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:47:46 +00:00
|
|
|
async function setupNewStyle(style) {
|
2018-02-22 09:41:55 +00:00
|
|
|
style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) +
|
|
|
|
`/* ${t('usercssReplaceTemplateSectionBody')} */`;
|
2017-12-26 20:39:52 +00:00
|
|
|
let section = sectionsToMozFormat(style);
|
2017-10-08 15:26:55 +00:00
|
|
|
if (!section.includes('@-moz-document')) {
|
|
|
|
style.sections[0].domains = ['example.com'];
|
2017-12-26 20:39:52 +00:00
|
|
|
section = sectionsToMozFormat(style);
|
2017-10-08 15:26:55 +00:00
|
|
|
}
|
2017-11-26 13:04:03 +00:00
|
|
|
const DEFAULT_CODE = `
|
|
|
|
/* ==UserStyle==
|
2018-08-06 10:35:33 +00:00
|
|
|
@name ${''/* a trick to preserve the trailing spaces */}
|
2017-11-26 13:04:03 +00:00
|
|
|
@namespace github.com/openstyles/stylus
|
2018-08-06 10:35:33 +00:00
|
|
|
@version 1.0.0
|
2017-11-26 13:04:03 +00:00
|
|
|
@description A new userstyle
|
|
|
|
@author Me
|
|
|
|
==/UserStyle== */
|
2018-02-22 09:41:55 +00:00
|
|
|
`.replace(/^\s+/gm, '');
|
2018-01-05 10:20:46 +00:00
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
dirty.clear('sourceGeneration');
|
2017-11-26 13:04:03 +00:00
|
|
|
style.sourceCode = '';
|
2018-01-05 10:20:46 +00:00
|
|
|
|
2020-10-22 20:47:46 +00:00
|
|
|
placeholderName = `${style.name || t('usercssReplaceTemplateName')} - ${new Date().toLocaleString()}`;
|
|
|
|
let code = await chromeSync.getLZValue('usercssTemplate');
|
|
|
|
code = code || DEFAULT_CODE;
|
|
|
|
code = code.replace(/@name(\s*)(?=[\r\n])/, (str, space) =>
|
|
|
|
`${str}${space ? '' : ' '}${placeholderName}`);
|
|
|
|
// strip the last dummy section if any, add an empty line followed by the section
|
|
|
|
style.sourceCode = code.replace(/\s*@-moz-document[^{]*{[^}]*}\s*$|\s+$/g, '') + '\n\n' + section;
|
|
|
|
cm.startOperation();
|
|
|
|
cm.setValue(style.sourceCode);
|
|
|
|
cm.clearHistory();
|
|
|
|
cm.markClean();
|
|
|
|
cm.endOperation();
|
|
|
|
dirty.clear('sourceGeneration');
|
|
|
|
savedGeneration = cm.changeGeneration();
|
2017-10-08 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:26:51 +00:00
|
|
|
function updateMeta() {
|
2020-10-22 20:47:46 +00:00
|
|
|
const name = style.customName || style.name;
|
|
|
|
if (name !== placeholderName) {
|
|
|
|
$('#name').value = name;
|
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
$('#enabled').checked = style.enabled;
|
|
|
|
$('#url').href = style.url;
|
2020-11-08 08:12:42 +00:00
|
|
|
editor.updateName();
|
|
|
|
cm.setPreprocessor((style.usercssData || {}).preprocessor);
|
2017-09-13 09:33:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
function replaceStyle(newStyle, codeIsUpdated) {
|
2020-10-11 15:12:06 +00:00
|
|
|
dirty.clear('name');
|
2017-11-26 21:45:21 +00:00
|
|
|
const sameCode = newStyle.sourceCode === cm.getValue();
|
|
|
|
if (sameCode) {
|
|
|
|
savedGeneration = cm.changeGeneration();
|
|
|
|
dirty.clear('sourceGeneration');
|
2017-10-08 15:26:55 +00:00
|
|
|
}
|
2017-11-26 21:45:21 +00:00
|
|
|
if (codeIsUpdated === false || sameCode) {
|
2017-11-29 10:34:00 +00:00
|
|
|
updateEnvironment();
|
2017-11-26 21:45:21 +00:00
|
|
|
dirty.clear('enabled');
|
2018-11-07 06:09:29 +00:00
|
|
|
updateLivePreview();
|
2017-11-26 21:45:21 +00:00
|
|
|
return;
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
2017-11-29 10:34:00 +00:00
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
Promise.resolve(messageBox.confirm(t('styleUpdateDiscardChanges'))).then(ok => {
|
2018-01-05 10:20:46 +00:00
|
|
|
if (!ok) return;
|
2017-11-29 10:34:00 +00:00
|
|
|
updateEnvironment();
|
2017-11-26 21:45:21 +00:00
|
|
|
if (!sameCode) {
|
|
|
|
const cursor = cm.getCursor();
|
|
|
|
cm.setValue(style.sourceCode);
|
|
|
|
cm.setCursor(cursor);
|
|
|
|
savedGeneration = cm.changeGeneration();
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
if (sameCode) {
|
|
|
|
// the code is same but the environment is changed
|
|
|
|
updateLivePreview();
|
|
|
|
}
|
2017-11-26 21:45:21 +00:00
|
|
|
dirty.clear();
|
|
|
|
});
|
2017-11-29 10:34:00 +00:00
|
|
|
|
|
|
|
function updateEnvironment() {
|
|
|
|
if (style.id !== newStyle.id) {
|
|
|
|
history.replaceState({}, '', `?id=${newStyle.id}`);
|
|
|
|
}
|
|
|
|
sessionStorage.justEditedStyleId = newStyle.id;
|
2018-11-07 06:09:29 +00:00
|
|
|
Object.assign(style, newStyle);
|
2018-01-10 18:56:14 +00:00
|
|
|
$('#preview-label').classList.remove('hidden');
|
2017-11-29 10:34:00 +00:00
|
|
|
updateMeta();
|
2018-11-07 06:09:29 +00:00
|
|
|
livePreview.show(Boolean(style.id));
|
2017-11-29 10:34:00 +00:00
|
|
|
}
|
2017-11-09 06:07:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
function save() {
|
2018-01-05 10:20:46 +00:00
|
|
|
if (!dirty.isDirty()) return;
|
2017-12-23 22:50:53 +00:00
|
|
|
const code = cm.getValue();
|
2018-08-18 20:17:20 +00:00
|
|
|
return ensureUniqueStyle(code)
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(() => API.editSaveUsercss({
|
2017-09-18 03:34:12 +00:00
|
|
|
id: style.id,
|
|
|
|
enabled: style.enabled,
|
2017-12-23 22:50:53 +00:00
|
|
|
sourceCode: code,
|
2020-10-11 15:12:06 +00:00
|
|
|
customName: style.customName,
|
2017-09-18 03:34:12 +00:00
|
|
|
}))
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(replaceStyle)
|
2017-09-11 16:09:25 +00:00
|
|
|
.catch(err => {
|
2018-08-18 20:17:20 +00:00
|
|
|
if (err.handled) return;
|
2017-11-27 03:54:59 +00:00
|
|
|
const contents = Array.isArray(err) ?
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('pre', err.join('\n')) :
|
2018-11-07 06:09:29 +00:00
|
|
|
[err.message || String(err)];
|
2017-11-09 07:55:06 +00:00
|
|
|
if (Number.isInteger(err.index)) {
|
|
|
|
const pos = cm.posFromIndex(err.index);
|
2018-12-08 13:21:06 +00:00
|
|
|
const meta = drawLinePointer(pos);
|
|
|
|
|
|
|
|
// save template
|
|
|
|
if (err.code === 'missingValue' && meta.includes('@name')) {
|
|
|
|
messageBox.confirm(t('usercssReplaceTemplateConfirmation')).then(ok => ok &&
|
|
|
|
chromeSync.setLZValue('usercssTemplate', code)
|
|
|
|
.then(() => chromeSync.getLZValue('usercssTemplate'))
|
|
|
|
.then(saved => saved !== code && messageBox.alert(t('syncStorageErrorSaving'))));
|
|
|
|
return;
|
|
|
|
}
|
2018-12-11 12:40:54 +00:00
|
|
|
contents[0] += ` (line ${pos.line + 1} col ${pos.ch + 1})`;
|
|
|
|
contents.push($create('pre', meta));
|
2017-11-09 07:55:06 +00:00
|
|
|
}
|
2018-03-11 13:31:25 +00:00
|
|
|
messageBox.alert(contents, 'pre');
|
2017-09-11 16:09:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:17:20 +00:00
|
|
|
function ensureUniqueStyle(code) {
|
|
|
|
return style.id ? Promise.resolve() :
|
|
|
|
API.buildUsercss({
|
|
|
|
sourceCode: code,
|
|
|
|
checkDup: true,
|
|
|
|
metaOnly: true,
|
|
|
|
}).then(({dup}) => {
|
|
|
|
if (dup) {
|
|
|
|
messageBox.alert(t('usercssAvoidOverwriting'), 'danger', t('genericError'));
|
|
|
|
return Promise.reject({handled: true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:20:46 +00:00
|
|
|
function drawLinePointer(pos) {
|
|
|
|
const SIZE = 60;
|
|
|
|
const line = cm.getLine(pos.line);
|
|
|
|
const numTabs = pos.ch + 1 - line.slice(0, pos.ch + 1).replace(/\t/g, '').length;
|
|
|
|
const pointer = ' '.repeat(pos.ch) + '^';
|
|
|
|
const start = Math.max(Math.min(pos.ch - SIZE / 2, line.length - SIZE), 0);
|
|
|
|
const end = Math.min(Math.max(pos.ch + SIZE / 2, SIZE), line.length);
|
|
|
|
const leftPad = start !== 0 ? '...' : '';
|
|
|
|
const rightPad = end !== line.length ? '...' : '';
|
|
|
|
return (
|
|
|
|
leftPad +
|
|
|
|
line.slice(start, end).replace(/\t/g, ' '.repeat(cm.options.tabSize)) +
|
|
|
|
rightPad +
|
|
|
|
'\n' +
|
|
|
|
' '.repeat(leftPad.length + numTabs * cm.options.tabSize) +
|
|
|
|
pointer.slice(start, end)
|
|
|
|
);
|
2017-10-16 08:08:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function nextPrevSection(dir) {
|
|
|
|
// ensure the data is ready in case the user wants to jump around a lot in a large style
|
|
|
|
sectionFinder.keepAliveFor(nextPrevSection, 10e3);
|
|
|
|
sectionFinder.updatePositions();
|
|
|
|
const {sections} = sectionFinder;
|
|
|
|
const num = sections.length;
|
|
|
|
if (!num) return;
|
|
|
|
dir = dir < 0 ? -1 : 0;
|
|
|
|
const pos = cm.getCursor();
|
|
|
|
let i = sections.findIndex(sec => CodeMirror.cmpPos(sec.start, pos) > Math.min(dir, 0));
|
|
|
|
if (i < 0 && (!dir || CodeMirror.cmpPos(sections[num - 1].start, pos) < 0)) {
|
|
|
|
i = 0;
|
2017-12-02 15:29:12 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
jumpToPos(sections[(i + dir + num) % num].start);
|
|
|
|
}
|
|
|
|
|
|
|
|
function jumpToPos(pos) {
|
|
|
|
const coords = cm.cursorCoords(pos, 'page');
|
|
|
|
const b = cm.display.wrapper.getBoundingClientRect();
|
|
|
|
if (coords.top < b.top + cm.defaultTextHeight() * 2 ||
|
|
|
|
coords.bottom > b.bottom - 100) {
|
|
|
|
cm.scrollIntoView(pos, b.height / 2);
|
2017-12-02 15:29:12 +00:00
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
cm.setCursor(pos, null, {scroll: false});
|
2017-12-02 15:29:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 04:01:43 +00:00
|
|
|
function headerOnScroll({target, deltaY, deltaMode, shiftKey}) {
|
|
|
|
while ((target = target.parentElement)) {
|
|
|
|
if (deltaY < 0 && target.scrollTop ||
|
|
|
|
deltaY > 0 && target.scrollTop + target.clientHeight < target.scrollHeight) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 01:36:46 +00:00
|
|
|
}
|
|
|
|
cm.display.scroller.scrollTop +=
|
|
|
|
// WheelEvent.DOM_DELTA_LINE
|
2018-08-24 11:31:29 +00:00
|
|
|
deltaMode === 1 ? deltaY * cm.defaultTextHeight() :
|
2017-12-07 01:36:46 +00:00
|
|
|
// WheelEvent.DOM_DELTA_PAGE
|
|
|
|
deltaMode === 2 || shiftKey ? Math.sign(deltaY) * cm.display.scroller.clientHeight :
|
|
|
|
// WheelEvent.DOM_DELTA_PIXEL
|
|
|
|
deltaY;
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:20:46 +00:00
|
|
|
function getModeName() {
|
|
|
|
const mode = cm.doc.mode;
|
2018-04-19 08:12:23 +00:00
|
|
|
if (!mode) return '';
|
|
|
|
return (mode.name || mode || '') +
|
|
|
|
(mode.helperType || '');
|
2018-01-05 10:20:46 +00:00
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|