Add: make source editor work with newly created style
This commit is contained in:
parent
3cb1943648
commit
228057d231
19
edit/edit.js
19
edit/edit.js
|
@ -1264,12 +1264,13 @@ function setStyleMeta(style) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function initWithStyle(request) {
|
function initWithStyle(request) {
|
||||||
if (!editor) {
|
if (!isUsercss()) {
|
||||||
if (!request.style.usercssData) {
|
|
||||||
initWithSectionStyle(request);
|
initWithSectionStyle(request);
|
||||||
} else {
|
return;
|
||||||
editor = createSourceEditor(request.style);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
editor = createSourceEditor(request.style);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1278,6 +1279,16 @@ function initWithStyle(request) {
|
||||||
} else {
|
} else {
|
||||||
editor.replaceStyle(request.style);
|
editor.replaceStyle(request.style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isUsercss() {
|
||||||
|
if (request.style.usercssData) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!request.style.id && prefs.get('newStyleFormat') === 'usercss') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initWithSectionStyle({style, codeIsUpdated}) {
|
function initWithSectionStyle({style, codeIsUpdated}) {
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
|
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
|
||||||
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
|
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
|
||||||
/* global hotkeyRerouter setupAutocomplete setupOptionsExpand */
|
/* global hotkeyRerouter setupAutocomplete setupOptionsExpand */
|
||||||
/* global editors linterConfig updateLinter regExpTester */
|
/* global editors linterConfig updateLinter regExpTester mozParser */
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function createSourceEditor(style) {
|
function createSourceEditor(style) {
|
||||||
// style might be an object reference to background page
|
|
||||||
style = deepCopy(style);
|
|
||||||
|
|
||||||
// draw HTML
|
// draw HTML
|
||||||
$('#sections').innerHTML = '';
|
$('#sections').innerHTML = '';
|
||||||
$('#name').disabled = true;
|
$('#name').disabled = true;
|
||||||
|
@ -22,12 +19,6 @@ function createSourceEditor(style) {
|
||||||
|
|
||||||
setupOptionsExpand();
|
setupOptionsExpand();
|
||||||
|
|
||||||
// draw CodeMirror
|
|
||||||
$('#sections textarea').value = style.sourceCode;
|
|
||||||
const cm = CodeMirror.fromTextArea($('#sections textarea'));
|
|
||||||
// too many functions depend on this global
|
|
||||||
editors.push(cm);
|
|
||||||
|
|
||||||
// dirty reporter
|
// dirty reporter
|
||||||
const dirty = dirtyReporter();
|
const dirty = dirtyReporter();
|
||||||
dirty.onChange(() => {
|
dirty.onChange(() => {
|
||||||
|
@ -37,6 +28,20 @@ function createSourceEditor(style) {
|
||||||
updateTitle();
|
updateTitle();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
// draw metas info
|
// draw metas info
|
||||||
updateMetas();
|
updateMetas();
|
||||||
initHooks();
|
initHooks();
|
||||||
|
@ -64,6 +69,28 @@ function createSourceEditor(style) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
function initAppliesToReport(cm) {
|
function initAppliesToReport(cm) {
|
||||||
const APPLIES_TYPE = [
|
const APPLIES_TYPE = [
|
||||||
[t('appliesUrlOption'), 'url'],
|
[t('appliesUrlOption'), 'url'],
|
||||||
|
@ -546,7 +573,7 @@ function createSourceEditor(style) {
|
||||||
$('#name').value = style.name;
|
$('#name').value = style.name;
|
||||||
$('#enabled').checked = style.enabled;
|
$('#enabled').checked = style.enabled;
|
||||||
$('#url').href = style.url;
|
$('#url').href = style.url;
|
||||||
const {usercssData: {preprocessor}} = style;
|
const {usercssData: {preprocessor} = {}} = style;
|
||||||
cm.setPreprocessor(preprocessor);
|
cm.setPreprocessor(preprocessor);
|
||||||
// beautify only works with regular CSS
|
// beautify only works with regular CSS
|
||||||
$('#beautify').disabled = cm.getOption('mode') !== 'css';
|
$('#beautify').disabled = cm.getOption('mode') !== 'css';
|
||||||
|
@ -555,10 +582,17 @@ function createSourceEditor(style) {
|
||||||
|
|
||||||
function updateTitle() {
|
function updateTitle() {
|
||||||
// title depends on dirty and style meta
|
// title depends on dirty and style meta
|
||||||
|
if (!style.id) {
|
||||||
|
document.title = t('addStyleTitle');
|
||||||
|
} else {
|
||||||
document.title = (dirty.isDirty() ? '* ' : '') + t('editStyleTitle', [style.name]);
|
document.title = (dirty.isDirty() ? '* ' : '') + t('editStyleTitle', [style.name]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function replaceStyle(newStyle) {
|
function replaceStyle(newStyle) {
|
||||||
|
if (!style.id && newStyle.id) {
|
||||||
|
history.replaceState({}, '', `?id=${newStyle.id}`);
|
||||||
|
}
|
||||||
style = deepCopy(newStyle);
|
style = deepCopy(newStyle);
|
||||||
updateMetas();
|
updateMetas();
|
||||||
if (style.sourceCode !== cm.getValue()) {
|
if (style.sourceCode !== cm.getValue()) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user