2017-11-22 00:12:05 +00:00
|
|
|
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
|
2017-09-11 16:09:25 +00:00
|
|
|
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
|
2017-11-23 05:18:55 +00:00
|
|
|
/* global hotkeyRerouter setupAutocomplete */
|
2017-10-08 15:26:55 +00:00
|
|
|
/* global editors linterConfig updateLinter regExpTester mozParser */
|
2017-11-09 07:55:06 +00:00
|
|
|
/* global makeLink createAppliesToLineWidget messageBox */
|
2017-09-11 16:09:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function createSourceEditor(style) {
|
2017-11-09 05:56:12 +00:00
|
|
|
// a flag for isTouched()
|
2017-10-16 08:08:13 +00:00
|
|
|
let hadBeenSaved = false;
|
2017-11-26 21:45:21 +00:00
|
|
|
let savedGeneration = 0;
|
2017-10-16 08:08:13 +00:00
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
$('#name').disabled = true;
|
2017-11-26 13:04:03 +00:00
|
|
|
$('#mozilla-format-container').remove();
|
|
|
|
$('#sections').textContent = '';
|
2017-09-12 12:06:00 +00:00
|
|
|
$('#sections').appendChild(
|
2017-11-22 13:28:50 +00:00
|
|
|
$element({className: 'single-editor'})
|
2017-09-12 12:06:00 +00:00
|
|
|
);
|
2017-09-11 16:09:25 +00:00
|
|
|
|
|
|
|
const dirty = dirtyReporter();
|
|
|
|
dirty.onChange(() => {
|
2017-11-26 21:45:21 +00:00
|
|
|
document.body.classList.toggle('dirty', dirty.isDirty());
|
|
|
|
$('#save-button').disabled = !dirty.isDirty();
|
2017-09-13 09:33:32 +00:00
|
|
|
updateTitle();
|
2017-09-11 16:09:25 +00:00
|
|
|
});
|
|
|
|
|
2017-10-08 15:26:55 +00:00
|
|
|
// normalize style
|
|
|
|
if (!style.id) {
|
|
|
|
setupNewStyle(style);
|
|
|
|
} else {
|
|
|
|
// style might be an object reference to background page
|
|
|
|
style = deepCopy(style);
|
|
|
|
}
|
|
|
|
|
2017-11-22 13:28:50 +00:00
|
|
|
const cm = CodeMirror($('.single-editor'));
|
2017-11-22 13:28:27 +00:00
|
|
|
editors.push(cm);
|
2017-11-22 00:38:29 +00:00
|
|
|
updateMeta().then(() => {
|
2017-11-22 13:28:27 +00:00
|
|
|
initLint();
|
|
|
|
initLinterSwitch();
|
|
|
|
|
2017-11-22 00:38:29 +00:00
|
|
|
cm.setValue(style.sourceCode);
|
|
|
|
cm.clearHistory();
|
|
|
|
cm.markClean();
|
2017-11-26 21:45:21 +00:00
|
|
|
savedGeneration = cm.changeGeneration();
|
2017-11-22 00:38:29 +00:00
|
|
|
|
|
|
|
initHooks();
|
|
|
|
initAppliesToLineWidget();
|
|
|
|
|
|
|
|
// focus must be the last action, otherwise the style is duplicated on saving
|
|
|
|
cm.focus();
|
|
|
|
});
|
2017-09-13 08:58:03 +00:00
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function initAppliesToLineWidget() {
|
|
|
|
const PREF_NAME = 'editor.appliesToLineWidget';
|
|
|
|
const widget = createAppliesToLineWidget(cm);
|
|
|
|
widget.toggle(prefs.get(PREF_NAME));
|
2017-11-26 13:04:03 +00:00
|
|
|
prefs.subscribe([PREF_NAME], (key, value) => widget.toggle(value));
|
2017-11-01 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 06:54:47 +00:00
|
|
|
function initLinterSwitch() {
|
|
|
|
const linterEl = $('#editor.linter');
|
2017-11-30 17:42:52 +00:00
|
|
|
let prevMode = NaN;
|
2017-10-16 06:54:47 +00:00
|
|
|
cm.on('optionChange', (cm, option) => {
|
|
|
|
if (option !== 'mode') {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-30 17:42:52 +00:00
|
|
|
const mode = cm.doc.mode;
|
|
|
|
if (mode === prevMode || mode && mode.name === prevMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prevMode = mode;
|
2017-10-16 06:54:47 +00:00
|
|
|
updateLinter();
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
linterEl.addEventListener('change', update);
|
2017-11-22 13:28:27 +00:00
|
|
|
update();
|
2017-10-16 06:54:47 +00:00
|
|
|
|
|
|
|
function update() {
|
2017-11-28 17:03:50 +00:00
|
|
|
linterEl.value = linterConfig.getName();
|
2017-10-16 06:54:47 +00:00
|
|
|
|
|
|
|
const cssLintOption = linterEl.querySelector('[value="csslint"]');
|
|
|
|
if (cm.getOption('mode') !== 'css') {
|
|
|
|
cssLintOption.disabled = true;
|
|
|
|
cssLintOption.title = t('linterCSSLintIncompatible', cm.getOption('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
|
|
|
|
2017-10-08 15:26:55 +00:00
|
|
|
function setupNewStyle(style) {
|
|
|
|
style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) + '/* Insert code here... */';
|
|
|
|
let section = mozParser.format(style);
|
|
|
|
if (!section.includes('@-moz-document')) {
|
|
|
|
style.sections[0].domains = ['example.com'];
|
|
|
|
section = mozParser.format(style);
|
|
|
|
}
|
|
|
|
|
2017-11-26 13:04:03 +00:00
|
|
|
const DEFAULT_CODE = `
|
|
|
|
/* ==UserStyle==
|
|
|
|
@name ${t('usercssReplaceTemplateName') + ' - ' + new Date().toLocaleString()}
|
|
|
|
@namespace github.com/openstyles/stylus
|
|
|
|
@version 0.1.0
|
|
|
|
@description A new userstyle
|
|
|
|
@author Me
|
|
|
|
==/UserStyle== */
|
|
|
|
|
|
|
|
${section}
|
|
|
|
`.replace(/^\s+/gm, '');
|
2017-11-26 21:45:21 +00:00
|
|
|
dirty.clear('sourceGeneration');
|
2017-11-26 13:04:03 +00:00
|
|
|
style.sourceCode = '';
|
|
|
|
BG.chromeSync.getLZValue('usercssTemplate').then(code => {
|
|
|
|
style.sourceCode = code || DEFAULT_CODE;
|
|
|
|
cm.startOperation();
|
|
|
|
cm.setValue(style.sourceCode);
|
|
|
|
cm.clearHistory();
|
|
|
|
cm.markClean();
|
|
|
|
cm.endOperation();
|
2017-11-26 21:45:21 +00:00
|
|
|
dirty.clear('sourceGeneration');
|
|
|
|
savedGeneration = cm.changeGeneration();
|
2017-11-26 13:04:03 +00:00
|
|
|
});
|
2017-10-08 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
function initHooks() {
|
|
|
|
$('#save-button').onclick = save;
|
|
|
|
$('#beautify').onclick = beautify;
|
|
|
|
$('#keyMap-help').onclick = showKeyMapHelp;
|
|
|
|
$('#toggle-style-help').onclick = showToggleStyleHelp;
|
|
|
|
$('#cancel-button').onclick = goBackToManage;
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
$('#enabled').onchange = function () {
|
|
|
|
const value = this.checked;
|
2017-09-11 16:09:25 +00:00
|
|
|
dirty.modify('enabled', style.enabled, value);
|
|
|
|
style.enabled = value;
|
|
|
|
};
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
cm.on('changes', () => {
|
|
|
|
dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration());
|
2017-09-11 16:09:25 +00:00
|
|
|
updateLintReportIfEnabled(cm);
|
|
|
|
});
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
cm.on('focus', () => hotkeyRerouter.setState(false));
|
|
|
|
cm.on('blur', () => hotkeyRerouter.setState(true));
|
2017-09-11 16:09:25 +00:00
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
//if (prefs.get('editor.autocompleteOnTyping')) {
|
|
|
|
// setupAutocomplete(cm);
|
|
|
|
//}
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:26:51 +00:00
|
|
|
function updateMeta() {
|
2017-09-11 16:09:25 +00:00
|
|
|
$('#name').value = style.name;
|
|
|
|
$('#enabled').checked = style.enabled;
|
|
|
|
$('#url').href = style.url;
|
2017-10-08 15:26:55 +00:00
|
|
|
const {usercssData: {preprocessor} = {}} = style;
|
2017-09-11 16:09:25 +00:00
|
|
|
// beautify only works with regular CSS
|
2017-09-24 08:54:21 +00:00
|
|
|
$('#beautify').disabled = cm.getOption('mode') !== 'css';
|
2017-09-13 09:33:32 +00:00
|
|
|
updateTitle();
|
2017-11-22 00:38:29 +00:00
|
|
|
return cm.setPreprocessor(preprocessor);
|
2017-09-13 09:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateTitle() {
|
2017-11-26 13:04:03 +00:00
|
|
|
const newTitle = (dirty.isDirty() ? '* ' : '') +
|
|
|
|
(style.id ? t('editStyleTitle', [style.name]) : t('addStyleTitle'));
|
|
|
|
if (document.title !== newTitle) {
|
|
|
|
document.title = newTitle;
|
2017-10-08 15:26:55 +00:00
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 21:45:21 +00:00
|
|
|
function replaceStyle(newStyle, codeIsUpdated) {
|
|
|
|
const sameCode = newStyle.sourceCode === cm.getValue();
|
|
|
|
hadBeenSaved = sameCode;
|
|
|
|
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');
|
|
|
|
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 => {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
style = deepCopy(newStyle);
|
|
|
|
updateMeta();
|
|
|
|
}
|
2017-11-09 06:07:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
function toggleStyle() {
|
|
|
|
const value = !style.enabled;
|
|
|
|
dirty.modify('enabled', style.enabled, value);
|
|
|
|
style.enabled = value;
|
2017-11-08 23:26:51 +00:00
|
|
|
updateMeta();
|
2017-09-11 16:09:25 +00:00
|
|
|
// save when toggle enable state?
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
if (!dirty.isDirty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-18 03:34:12 +00:00
|
|
|
return onBackgroundReady()
|
|
|
|
.then(() => BG.usercssHelper.save({
|
|
|
|
reason: 'editSave',
|
|
|
|
id: style.id,
|
|
|
|
enabled: style.enabled,
|
2017-11-26 21:45:21 +00:00
|
|
|
sourceCode: cm.getValue(),
|
2017-09-18 03:34:12 +00:00
|
|
|
}))
|
|
|
|
.then(replaceStyle)
|
2017-11-29 10:27:55 +00:00
|
|
|
.then(() => cm.setOption('mode', cm.doc.mode))
|
2017-09-11 16:09:25 +00:00
|
|
|
.catch(err => {
|
2017-11-26 13:04:03 +00:00
|
|
|
if (err.message === t('styleMissingMeta', 'name')) {
|
|
|
|
messageBox.confirm(t('usercssReplaceTemplateConfirmation')).then(ok => ok &&
|
|
|
|
BG.chromeSync.setLZValue('usercssTemplate', style.sourceCode)
|
|
|
|
.then(() => BG.chromeSync.getLZValue('usercssTemplate'))
|
|
|
|
.then(saved => {
|
|
|
|
if (saved !== style.sourceCode) {
|
|
|
|
messageBox.alert(t('syncStorageErrorSaving'));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
2017-11-27 03:54:59 +00:00
|
|
|
const contents = Array.isArray(err) ?
|
|
|
|
$element({tag: 'pre', textContent: err.join('\n')}) :
|
|
|
|
[String(err)];
|
2017-11-09 07:55:06 +00:00
|
|
|
if (Number.isInteger(err.index)) {
|
|
|
|
const pos = cm.posFromIndex(err.index);
|
|
|
|
contents[0] += ` (line ${pos.line + 1} col ${pos.ch + 1})`;
|
|
|
|
contents.push($element({
|
|
|
|
tag: 'pre',
|
|
|
|
textContent: drawLinePointer(pos)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
messageBox.alert(contents);
|
2017-09-11 16:09:25 +00:00
|
|
|
});
|
2017-11-09 07:55:06 +00:00
|
|
|
|
|
|
|
function drawLinePointer(pos) {
|
|
|
|
const SIZE = 60;
|
|
|
|
const line = cm.getLine(pos.line);
|
2017-11-27 09:44:16 +00:00
|
|
|
const numTabs = pos.ch + 1 - line.slice(0, pos.ch + 1).replace(/\t/g, '').length;
|
2017-11-09 07:55:06 +00:00
|
|
|
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 ? '...' : '';
|
2017-11-27 09:44:16 +00:00
|
|
|
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-11-09 07:55:06 +00:00
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 05:56:12 +00:00
|
|
|
function isTouched() {
|
2017-10-16 08:08:13 +00:00
|
|
|
// indicate that the editor had been touched by the user
|
|
|
|
return dirty.isDirty() || hadBeenSaved;
|
|
|
|
}
|
|
|
|
|
2017-09-19 00:49:38 +00:00
|
|
|
return {
|
|
|
|
replaceStyle,
|
|
|
|
save,
|
|
|
|
toggleStyle,
|
|
|
|
isDirty: dirty.isDirty,
|
2017-10-16 08:08:13 +00:00
|
|
|
getStyle: () => style,
|
2017-11-09 05:56:12 +00:00
|
|
|
isTouched
|
2017-09-19 00:49:38 +00:00
|
|
|
};
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|