2017-09-11 16:09:25 +00:00
|
|
|
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
|
|
|
|
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
|
2017-10-04 08:47:56 +00:00
|
|
|
/* global hotkeyRerouter setupAutocomplete setupOptionsExpand */
|
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-09-11 16:09:25 +00:00
|
|
|
// draw HTML
|
2017-11-01 00:50:10 +00:00
|
|
|
$('#sections').textContent = '';
|
2017-09-11 16:09:25 +00:00
|
|
|
$('#name').disabled = true;
|
|
|
|
$('#mozilla-format-heading').parentNode.remove();
|
|
|
|
|
2017-09-12 12:06:00 +00:00
|
|
|
$('#sections').appendChild(
|
|
|
|
$element({className: 'single-editor', appendChild: [
|
|
|
|
$element({tag: 'textarea'})
|
|
|
|
]})
|
|
|
|
);
|
2017-09-11 16:09:25 +00:00
|
|
|
|
2017-10-11 14:23:39 +00:00
|
|
|
$('#header').appendChild($element({
|
|
|
|
id: 'footer',
|
|
|
|
appendChild: makeLink('https://github.com/openstyles/stylus/wiki/Usercss', t('externalUsercssDocument'))
|
|
|
|
}));
|
|
|
|
|
2017-10-04 08:47:56 +00:00
|
|
|
setupOptionsExpand();
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
// dirty reporter
|
|
|
|
const dirty = dirtyReporter();
|
|
|
|
dirty.onChange(() => {
|
|
|
|
const DIRTY = dirty.isDirty();
|
|
|
|
document.body.classList.toggle('dirty', DIRTY);
|
|
|
|
$('#save-button').disabled = !DIRTY;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw CodeMirror
|
|
|
|
$('#sections textarea').value = style.sourceCode;
|
|
|
|
const cm = CodeMirror.fromTextArea($('#sections textarea'));
|
|
|
|
// too many functions depend on this global
|
|
|
|
editors.push(cm);
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
// draw metas info
|
2017-11-08 23:26:51 +00:00
|
|
|
updateMeta();
|
2017-09-11 16:09:25 +00:00
|
|
|
initHooks();
|
2017-11-01 01:11:27 +00:00
|
|
|
initAppliesToLineWidget();
|
2017-09-13 08:58:03 +00:00
|
|
|
|
2017-10-07 10:00:25 +00:00
|
|
|
// setup linter
|
|
|
|
initLint();
|
2017-10-16 06:54:47 +00:00
|
|
|
initLinterSwitch();
|
2017-10-07 10:00:25 +00:00
|
|
|
|
2017-11-01 01:11:27 +00:00
|
|
|
function initAppliesToLineWidget() {
|
|
|
|
const PREF_NAME = 'editor.appliesToLineWidget';
|
|
|
|
const widget = createAppliesToLineWidget(cm);
|
|
|
|
const optionEl = buildOption();
|
|
|
|
|
|
|
|
$('#options').insertBefore(optionEl, $('#options > .option.aligned'));
|
|
|
|
widget.toggle(prefs.get(PREF_NAME));
|
|
|
|
prefs.subscribe([PREF_NAME], (key, value) => {
|
|
|
|
widget.toggle(value);
|
|
|
|
optionEl.checked = value;
|
|
|
|
});
|
|
|
|
optionEl.addEventListener('change', e => {
|
|
|
|
prefs.set(PREF_NAME, e.target.checked);
|
|
|
|
});
|
|
|
|
|
|
|
|
function buildOption() {
|
|
|
|
return $element({className: 'option', appendChild: [
|
|
|
|
$element({
|
|
|
|
tag: 'input',
|
|
|
|
type: 'checkbox',
|
|
|
|
id: PREF_NAME,
|
|
|
|
checked: prefs.get(PREF_NAME)
|
|
|
|
}),
|
|
|
|
$element({
|
|
|
|
tag: 'label',
|
|
|
|
htmlFor: PREF_NAME,
|
|
|
|
textContent: ' ' + t('appliesLineWidgetLabel'),
|
|
|
|
title: t('appliesLineWidgetWarning')
|
|
|
|
})
|
|
|
|
]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:54:47 +00:00
|
|
|
function initLinterSwitch() {
|
|
|
|
const linterEl = $('#editor.linter');
|
|
|
|
cm.on('optionChange', (cm, option) => {
|
|
|
|
if (option !== 'mode') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateLinter();
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
linterEl.addEventListener('change', update);
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
linterEl.value = linterConfig.getDefault();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceCode = `/* ==UserStyle==
|
|
|
|
@name New Style - ${Date.now()}
|
|
|
|
@namespace github.com/openstyles/stylus
|
|
|
|
@version 0.1.0
|
|
|
|
@description A new userstyle
|
|
|
|
@author Me
|
|
|
|
==/UserStyle== */
|
|
|
|
|
|
|
|
${section}
|
|
|
|
`;
|
|
|
|
dirty.modify('source', '', sourceCode);
|
|
|
|
style.sourceCode = sourceCode;
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:25 +00:00
|
|
|
function initHooks() {
|
|
|
|
// sidebar commands
|
|
|
|
$('#save-button').onclick = save;
|
|
|
|
$('#beautify').onclick = beautify;
|
|
|
|
$('#keyMap-help').onclick = showKeyMapHelp;
|
|
|
|
$('#toggle-style-help').onclick = showToggleStyleHelp;
|
|
|
|
$('#cancel-button').onclick = goBackToManage;
|
|
|
|
|
|
|
|
// enable
|
|
|
|
$('#enabled').onchange = e => {
|
|
|
|
const value = e.target.checked;
|
|
|
|
dirty.modify('enabled', style.enabled, value);
|
|
|
|
style.enabled = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// source
|
|
|
|
cm.on('change', () => {
|
|
|
|
const value = cm.getValue();
|
2017-09-16 01:24:50 +00:00
|
|
|
dirty.modify('source', style.sourceCode, value);
|
|
|
|
style.sourceCode = value;
|
2017-09-11 16:09:25 +00:00
|
|
|
|
|
|
|
updateLintReportIfEnabled(cm);
|
|
|
|
});
|
|
|
|
|
|
|
|
// hotkeyRerouter
|
|
|
|
cm.on('focus', () => {
|
|
|
|
hotkeyRerouter.setState(false);
|
|
|
|
});
|
|
|
|
cm.on('blur', () => {
|
|
|
|
hotkeyRerouter.setState(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// autocomplete
|
|
|
|
if (prefs.get('editor.autocompleteOnTyping')) {
|
|
|
|
setupAutocomplete(cm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-24 08:54:21 +00:00
|
|
|
cm.setPreprocessor(preprocessor);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTitle() {
|
|
|
|
// title depends on dirty and style meta
|
2017-10-08 15:26:55 +00:00
|
|
|
if (!style.id) {
|
|
|
|
document.title = t('addStyleTitle');
|
|
|
|
} else {
|
|
|
|
document.title = (dirty.isDirty() ? '* ' : '') + t('editStyleTitle', [style.name]);
|
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 17:39:45 +00:00
|
|
|
function replaceStyle(newStyle) {
|
2017-10-08 15:26:55 +00:00
|
|
|
if (!style.id && newStyle.id) {
|
|
|
|
history.replaceState({}, '', `?id=${newStyle.id}`);
|
|
|
|
}
|
2017-09-14 01:09:40 +00:00
|
|
|
style = deepCopy(newStyle);
|
2017-11-08 23:26:51 +00:00
|
|
|
updateMeta();
|
2017-09-16 01:24:50 +00:00
|
|
|
if (style.sourceCode !== cm.getValue()) {
|
2017-09-11 16:09:25 +00:00
|
|
|
const cursor = cm.getCursor();
|
2017-09-16 01:24:50 +00:00
|
|
|
cm.setValue(style.sourceCode);
|
2017-09-11 16:09:25 +00:00
|
|
|
cm.setCursor(cursor);
|
|
|
|
}
|
|
|
|
dirty.clear();
|
2017-10-16 08:08:13 +00:00
|
|
|
hadBeenSaved = false;
|
2017-09-11 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:07:06 +00:00
|
|
|
function setStyleDirty(newStyle) {
|
|
|
|
dirty.clear();
|
|
|
|
dirty.modify('source', newStyle.sourceCode, style.sourceCode);
|
|
|
|
dirty.modify('enabled', newStyle.enabled, style.enabled);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
sourceCode: style.sourceCode
|
|
|
|
}))
|
|
|
|
.then(replaceStyle)
|
2017-10-16 08:08:13 +00:00
|
|
|
.then(() => {
|
|
|
|
hadBeenSaved = true;
|
|
|
|
})
|
2017-09-11 16:09:25 +00:00
|
|
|
.catch(err => {
|
2017-11-09 07:55:06 +00:00
|
|
|
const contents = [String(err)];
|
|
|
|
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)
|
|
|
|
}));
|
|
|
|
}
|
2017-09-11 16:09:25 +00:00
|
|
|
console.error(err);
|
2017-11-09 07:55:06 +00:00
|
|
|
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);
|
|
|
|
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) + rightPad + '\n' +
|
|
|
|
' '.repeat(leftPad.length) + pointer.slice(start, end);
|
|
|
|
}
|
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-11-08 23:23:54 +00:00
|
|
|
function replaceMeta(newStyle) {
|
2017-10-16 08:08:13 +00:00
|
|
|
style.enabled = newStyle.enabled;
|
|
|
|
dirty.clear('enabled');
|
2017-11-08 23:26:51 +00:00
|
|
|
updateMeta();
|
2017-10-16 08:08:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 00:49:38 +00:00
|
|
|
return {
|
|
|
|
replaceStyle,
|
2017-11-08 23:23:54 +00:00
|
|
|
replaceMeta,
|
2017-11-09 06:07:06 +00:00
|
|
|
setStyleDirty,
|
2017-09-19 00:49:38 +00:00
|
|
|
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
|
|
|
}
|