Refactor: init
This commit is contained in:
parent
8c374db353
commit
1c3317202f
82
edit/edit.js
82
edit/edit.js
|
@ -1197,47 +1197,61 @@ function beautify(event) {
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', init);
|
document.addEventListener('DOMContentLoaded', init);
|
||||||
|
|
||||||
|
function createEmptyStyle() {
|
||||||
|
const params = getParams();
|
||||||
|
const style = {
|
||||||
|
id: null,
|
||||||
|
name: '',
|
||||||
|
enabled: true,
|
||||||
|
sections: [{code: ''}]
|
||||||
|
};
|
||||||
|
for (const i in CssToProperty) {
|
||||||
|
if (params[i]) {
|
||||||
|
style.sections[0][CssToProperty[i]] = [params[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
function windowLoaded() {
|
||||||
|
if (document.readyState !== 'loading') {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
return new Promise(resolve => {
|
||||||
|
window.addEventListener('load', function self() {
|
||||||
|
window.removeEventListener('load', self);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
initCodeMirror();
|
initCodeMirror();
|
||||||
const params = getParams();
|
const params = getParams();
|
||||||
if (!params.id) {
|
return Promise.resolve().then(() => {
|
||||||
// match should be 2 - one for the whole thing, one for the parentheses
|
if (!params.id) {
|
||||||
// This is an add
|
// match should be 2 - one for the whole thing, one for the parentheses
|
||||||
$('#heading').textContent = t('addStyleTitle');
|
// This is an add
|
||||||
const section = {code: ''};
|
$('#heading').textContent = t('addStyleTitle');
|
||||||
for (const i in CssToProperty) {
|
return createEmptyStyle();
|
||||||
if (params[i]) {
|
}
|
||||||
section[CssToProperty[i]] = [params[i]];
|
$('#heading').textContent = t('editStyleHeading');
|
||||||
|
// This is an edit
|
||||||
|
return getStylesSafe({id: params.id}).then(styles => {
|
||||||
|
let style = styles[0];
|
||||||
|
if (!style) {
|
||||||
|
style = createEmptyStyle();
|
||||||
|
history.replaceState({}, document.title, location.pathname);
|
||||||
}
|
}
|
||||||
}
|
return style;
|
||||||
window.onload = () => {
|
});
|
||||||
window.onload = null;
|
}).then(style => {
|
||||||
addSection(null, section);
|
|
||||||
editors[0].setOption('lint', CodeMirror.defaults.lint);
|
|
||||||
// default to enabled
|
|
||||||
$('#enabled').checked = true;
|
|
||||||
initHooks();
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// This is an edit
|
|
||||||
$('#heading').textContent = t('editStyleHeading');
|
|
||||||
getStylesSafe({id: params.id}).then(styles => {
|
|
||||||
let style = styles[0];
|
|
||||||
if (!style) {
|
|
||||||
style = {id: null, sections: []};
|
|
||||||
history.replaceState({}, document.title, location.pathname);
|
|
||||||
}
|
|
||||||
styleId = style.id;
|
styleId = style.id;
|
||||||
sessionStorage.justEditedStyleId = styleId;
|
sessionStorage.justEditedStyleId = styleId;
|
||||||
setStyleMeta(style);
|
|
||||||
window.onload = () => {
|
return windowLoaded().then(() => {
|
||||||
window.onload = null;
|
|
||||||
initWithStyle({style});
|
initWithStyle({style});
|
||||||
};
|
});
|
||||||
if (document.readyState !== 'loading') {
|
|
||||||
window.onload();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user