2022-03-24 14:08:04 +00:00
|
|
|
/* global $$ $ $create */// dom.js
|
2021-12-29 19:57:22 +00:00
|
|
|
/* global API msg */// msg.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global CodeMirror */
|
|
|
|
/* global SectionsEditor */
|
|
|
|
/* global SourceEditor */
|
|
|
|
/* global clipString createHotkeyInput helpPopup */// util.js
|
2022-09-01 08:28:28 +00:00
|
|
|
/* global closeCurrentTab deepEqual mapObj sessionStore tryJSONparse */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global cmFactory */
|
2022-03-24 14:08:04 +00:00
|
|
|
/* global editor EditorHeader */// base.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global linterMan */
|
|
|
|
/* global prefs */
|
|
|
|
/* global t */// localization.js
|
2017-07-12 20:44:59 +00:00
|
|
|
'use strict';
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#region init
|
2020-11-18 11:17:15 +00:00
|
|
|
|
2022-02-19 14:12:15 +00:00
|
|
|
document.body.appendChild(t.template.body);
|
|
|
|
|
2022-02-20 16:11:21 +00:00
|
|
|
EditorMethods();
|
2022-03-24 14:08:04 +00:00
|
|
|
editor.styleReady.then(async () => {
|
|
|
|
EditorHeader();
|
|
|
|
dispatchEvent(new Event('domReady'));
|
|
|
|
await (editor.isUsercss ? SourceEditor : SectionsEditor)();
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
editor.dirty.onChange(editor.updateDirty);
|
2022-02-14 19:19:20 +00:00
|
|
|
prefs.subscribe('editor.linter', () => linterMan.run());
|
2017-04-20 14:00:43 +00:00
|
|
|
|
2020-10-11 15:12:06 +00:00
|
|
|
// enabling after init to prevent flash of validation failure on an empty name
|
2020-11-08 08:12:42 +00:00
|
|
|
$('#name').required = !editor.isUsercss;
|
2020-10-11 15:12:06 +00:00
|
|
|
$('#save-button').onclick = editor.save;
|
2021-12-29 19:57:22 +00:00
|
|
|
$('#cancel-button').onclick = editor.cancel;
|
2021-07-30 04:56:59 +00:00
|
|
|
|
|
|
|
const elSec = $('#sections-list');
|
2021-01-09 10:21:06 +00:00
|
|
|
// editor.toc.expanded pref isn't saved in compact-layout so prefs.subscribe won't work
|
2021-07-30 04:56:59 +00:00
|
|
|
if (elSec.open) editor.updateToc();
|
|
|
|
// and we also toggle `open` directly in other places e.g. in detectLayout()
|
|
|
|
new MutationObserver(() => elSec.open && editor.updateToc())
|
|
|
|
.observe(elSec, {attributes: true, attributeFilter: ['open']});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
$('#toc').onclick = e =>
|
|
|
|
editor.jumpToEditor([...$('#toc').children].indexOf(e.target));
|
|
|
|
$('#keyMap-help').onclick = () =>
|
|
|
|
require(['/edit/show-keymap-help'], () => showKeymapHelp()); /* global showKeymapHelp */
|
|
|
|
$('#linter-settings').onclick = () =>
|
|
|
|
require(['/edit/linter-dialogs'], () => linterMan.showLintConfig());
|
|
|
|
$('#lint-help').onclick = () =>
|
|
|
|
require(['/edit/linter-dialogs'], () => linterMan.showLintHelp());
|
2021-12-29 19:57:22 +00:00
|
|
|
$('#style-settings-btn').onclick = () => require([
|
|
|
|
'/edit/settings.css',
|
|
|
|
'/edit/settings', /* global StyleSettings */
|
|
|
|
], () => StyleSettings());
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
require([
|
|
|
|
'/edit/autocomplete',
|
2022-01-23 09:44:25 +00:00
|
|
|
'/edit/drafts',
|
2021-01-01 14:27:58 +00:00
|
|
|
'/edit/global-search',
|
|
|
|
]);
|
2022-03-24 14:08:04 +00:00
|
|
|
});
|
2022-02-20 16:11:21 +00:00
|
|
|
|
2022-03-24 14:08:04 +00:00
|
|
|
editor.styleReady.then(async () => {
|
2022-02-14 19:19:20 +00:00
|
|
|
// Set up mini-header on scroll
|
|
|
|
const {isUsercss} = editor;
|
|
|
|
const el = $create({
|
|
|
|
style: `
|
|
|
|
top: 0;
|
|
|
|
height: 1px;
|
|
|
|
position: absolute;
|
|
|
|
visibility: hidden;
|
|
|
|
`.replace(/;/g, '!important;'),
|
|
|
|
});
|
|
|
|
const scroller = isUsercss ? $('.CodeMirror-scroll') : document.body;
|
|
|
|
const xoRoot = isUsercss ? scroller : undefined;
|
|
|
|
const xo = new IntersectionObserver(onScrolled, {root: xoRoot});
|
|
|
|
scroller.appendChild(el);
|
2022-03-24 14:08:04 +00:00
|
|
|
onCompactToggled(editor.mqCompact);
|
|
|
|
editor.mqCompact.on('change', onCompactToggled);
|
2022-02-14 19:19:20 +00:00
|
|
|
|
|
|
|
/** @param {MediaQueryList} mq */
|
|
|
|
function onCompactToggled(mq) {
|
|
|
|
for (const el of $$('details[data-pref]')) {
|
|
|
|
el.open = mq.matches ? false : prefs.get(el.dataset.pref);
|
|
|
|
}
|
|
|
|
if (mq.matches) {
|
|
|
|
xo.observe(el);
|
|
|
|
} else {
|
|
|
|
xo.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** @param {IntersectionObserverEntry[]} entries */
|
|
|
|
function onScrolled(entries) {
|
|
|
|
const h = $('#header');
|
|
|
|
const sticky = !entries.pop().isIntersecting;
|
|
|
|
if (!isUsercss) scroller.style.paddingTop = sticky ? h.offsetHeight + 'px' : '';
|
|
|
|
h.classList.toggle('sticky', sticky);
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
//#endregion
|
|
|
|
//#region events
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
msg.onExtension(request => {
|
|
|
|
const {style} = request;
|
2017-12-02 20:41:15 +00:00
|
|
|
switch (request.method) {
|
|
|
|
case 'styleUpdated':
|
2022-01-10 16:12:29 +00:00
|
|
|
if (editor.style.id === style.id) {
|
2021-12-29 19:57:22 +00:00
|
|
|
handleExternalUpdate(request);
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-12-02 20:41:15 +00:00
|
|
|
break;
|
|
|
|
case 'styleDeleted':
|
2021-01-01 14:27:58 +00:00
|
|
|
if (editor.style.id === style.id) {
|
2017-12-02 20:41:15 +00:00
|
|
|
closeCurrentTab();
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2017-08-15 19:13:58 +00:00
|
|
|
break;
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2017-08-27 11:56:04 +00:00
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
async function handleExternalUpdate({style, reason}) {
|
2022-01-10 16:12:29 +00:00
|
|
|
if (reason === 'editPreview' ||
|
|
|
|
reason === 'editPreviewEnd') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reason === 'editSave' && editor.saving) {
|
|
|
|
editor.saving = false;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-29 19:57:22 +00:00
|
|
|
if (reason === 'toggle') {
|
|
|
|
if (editor.dirty.isDirty()) {
|
|
|
|
editor.toggleStyle(style.enabled);
|
|
|
|
} else {
|
|
|
|
Object.assign(editor.style, style);
|
|
|
|
}
|
|
|
|
editor.updateMeta();
|
|
|
|
editor.updateLivePreview();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
style = await API.styles.get(style.id);
|
|
|
|
if (reason === 'config') {
|
|
|
|
delete style.sourceCode;
|
|
|
|
delete style.sections;
|
|
|
|
delete style.name;
|
|
|
|
delete style.enabled;
|
|
|
|
Object.assign(editor.style, style);
|
|
|
|
} else {
|
|
|
|
await editor.replaceStyle(style);
|
|
|
|
}
|
|
|
|
window.dispatchEvent(new Event('styleSettings'));
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('beforeunload', e => {
|
|
|
|
let pos;
|
|
|
|
if (editor.isWindowed &&
|
|
|
|
document.visibilityState === 'visible' &&
|
|
|
|
prefs.get('openEditInWindow') &&
|
2021-12-26 16:25:25 +00:00
|
|
|
screenX !== -32000 && // Chrome uses this value for minimized windows
|
2021-01-01 14:27:58 +00:00
|
|
|
( // only if not maximized
|
|
|
|
screenX > 0 || outerWidth < screen.availWidth ||
|
|
|
|
screenY > 0 || outerHeight < screen.availHeight ||
|
|
|
|
screenX <= -10 || outerWidth >= screen.availWidth + 10 ||
|
|
|
|
screenY <= -10 || outerHeight >= screen.availHeight + 10
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
pos = {
|
|
|
|
left: screenX,
|
|
|
|
top: screenY,
|
|
|
|
width: outerWidth,
|
|
|
|
height: outerHeight,
|
|
|
|
};
|
|
|
|
prefs.set('windowPosition', pos);
|
|
|
|
}
|
|
|
|
sessionStore.windowPos = JSON.stringify(pos || {});
|
2022-01-23 09:44:25 +00:00
|
|
|
sessionStore['editorScrollInfo' + editor.style.id] = JSON.stringify(editor.makeScrollInfo());
|
2018-08-02 17:54:40 +00:00
|
|
|
const activeElement = document.activeElement;
|
|
|
|
if (activeElement) {
|
|
|
|
// blurring triggers 'change' or 'input' event if needed
|
|
|
|
activeElement.blur();
|
|
|
|
// refocus if unloading was canceled
|
|
|
|
setTimeout(() => activeElement.focus());
|
2017-07-12 19:50:13 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (editor.dirty.isDirty()) {
|
2018-08-02 17:54:40 +00:00
|
|
|
// neither confirm() nor custom messages work in modern browsers but just in case
|
2018-11-07 06:09:29 +00:00
|
|
|
e.returnValue = t('styleChangesNotSaved');
|
2017-10-08 16:43:00 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2015-10-29 18:20:05 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#endregion
|
|
|
|
//#region editor methods
|
2017-12-13 04:33:16 +00:00
|
|
|
|
2022-02-20 16:11:21 +00:00
|
|
|
function EditorMethods() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const toc = [];
|
|
|
|
const {dirty} = editor;
|
|
|
|
let {style} = editor;
|
|
|
|
let wasDirty = false;
|
|
|
|
|
|
|
|
Object.defineProperties(editor, {
|
|
|
|
scrollInfo: {
|
|
|
|
get: () => style.id && tryJSONparse(sessionStore['editorScrollInfo' + style.id]) || {},
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
get: () => style,
|
|
|
|
set: val => (style = val),
|
|
|
|
},
|
2017-12-13 04:33:16 +00:00
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @namespace Editor */
|
|
|
|
Object.assign(editor, {
|
|
|
|
|
|
|
|
applyScrollInfo(cm, si = (editor.scrollInfo.cms || [])[0]) {
|
|
|
|
if (si && si.sel) {
|
2021-01-12 14:39:24 +00:00
|
|
|
const bmOpts = {sublimeBookmark: true, clearWhenEmpty: false}; // copied from sublime.js
|
2022-09-01 08:28:28 +00:00
|
|
|
cm.setSelections(...si.sel, {scroll: false});
|
|
|
|
cm.state.sublimeBookmarks = si.bookmarks.map(b => cm.markText(b.from, b.to, bmOpts));
|
|
|
|
Object.assign(cm.display.scroller, si.scroll); // for source editor
|
|
|
|
Object.assign(cm.doc, si.scroll); // for sectioned editor
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-01-23 09:44:25 +00:00
|
|
|
makeScrollInfo() {
|
|
|
|
return {
|
|
|
|
scrollY: window.scrollY,
|
|
|
|
cms: editor.getEditors().map(cm => /** @namespace EditorScrollInfo */({
|
|
|
|
bookmarks: (cm.state.sublimeBookmarks || []).map(b => b.find()),
|
|
|
|
focus: cm.hasFocus(),
|
|
|
|
height: cm.display.wrapper.style.height.replace('100vh', ''),
|
|
|
|
parentHeight: cm.display.wrapper.parentElement.offsetHeight,
|
2022-09-01 08:28:28 +00:00
|
|
|
scroll: mapObj(cm.doc, null, ['scrollLeft', 'scrollTop']),
|
2022-01-23 09:44:25 +00:00
|
|
|
sel: [cm.doc.sel.ranges, cm.doc.sel.primIndex],
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-01-10 16:12:29 +00:00
|
|
|
async save() {
|
|
|
|
if (dirty.isDirty()) {
|
|
|
|
editor.saving = true;
|
|
|
|
await editor.saveImpl();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
toggleStyle(enabled = !style.enabled) {
|
2021-12-07 04:44:49 +00:00
|
|
|
$('#enabled').checked = enabled;
|
|
|
|
editor.updateEnabledness(enabled);
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateDirty() {
|
|
|
|
const isDirty = dirty.isDirty();
|
|
|
|
if (wasDirty !== isDirty) {
|
|
|
|
wasDirty = isDirty;
|
|
|
|
document.body.classList.toggle('dirty', isDirty);
|
|
|
|
$('#save-button').disabled = !isDirty;
|
|
|
|
}
|
|
|
|
editor.updateTitle();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateEnabledness(enabled) {
|
|
|
|
dirty.modify('enabled', style.enabled, enabled);
|
|
|
|
style.enabled = enabled;
|
|
|
|
editor.updateLivePreview();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateName(isUserInput) {
|
|
|
|
if (!editor) return;
|
|
|
|
if (isUserInput) {
|
|
|
|
const {value} = $('#name');
|
|
|
|
dirty.modify('name', style[editor.nameTarget] || style.name, value);
|
|
|
|
style[editor.nameTarget] = value;
|
|
|
|
}
|
|
|
|
editor.updateTitle();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateToc(added = editor.sections) {
|
2021-01-09 10:21:06 +00:00
|
|
|
if (!toc.el) {
|
|
|
|
toc.el = $('#toc');
|
|
|
|
toc.elDetails = toc.el.closest('details');
|
|
|
|
}
|
|
|
|
if (!toc.elDetails.open) return;
|
2021-01-01 14:27:58 +00:00
|
|
|
const {sections} = editor;
|
|
|
|
const first = sections.indexOf(added[0]);
|
2021-01-09 10:21:06 +00:00
|
|
|
const elFirst = toc.el.children[first];
|
2021-01-01 14:27:58 +00:00
|
|
|
if (first >= 0 && (!added.focus || !elFirst)) {
|
|
|
|
for (let el = elFirst, i = first; i < sections.length; i++) {
|
|
|
|
const entry = sections[i].tocEntry;
|
|
|
|
if (!deepEqual(entry, toc[i])) {
|
2021-01-09 10:21:06 +00:00
|
|
|
if (!el) el = toc.el.appendChild($create('li', {tabIndex: 0}));
|
2021-01-01 14:27:58 +00:00
|
|
|
el.tabIndex = entry.removed ? -1 : 0;
|
|
|
|
toc[i] = Object.assign({}, entry);
|
|
|
|
const s = el.textContent = clipString(entry.label) || (
|
|
|
|
entry.target == null
|
|
|
|
? t('appliesToEverything')
|
|
|
|
: clipString(entry.target) + (entry.numTargets > 1 ? ', ...' : ''));
|
|
|
|
if (s.length > 30) el.title = s;
|
|
|
|
}
|
|
|
|
el = el.nextElementSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (toc.length > sections.length) {
|
2021-01-09 10:21:06 +00:00
|
|
|
toc.el.lastElementChild.remove();
|
2021-01-01 14:27:58 +00:00
|
|
|
toc.length--;
|
|
|
|
}
|
|
|
|
if (added.focus) {
|
|
|
|
const cls = 'current';
|
2021-01-09 10:21:06 +00:00
|
|
|
const old = $('.' + cls, toc.el);
|
|
|
|
const el = elFirst || toc.el.children[first];
|
2021-01-01 14:27:58 +00:00
|
|
|
if (old && old !== el) old.classList.remove(cls);
|
|
|
|
el.classList.add(cls);
|
|
|
|
}
|
|
|
|
},
|
2022-01-10 18:29:07 +00:00
|
|
|
|
|
|
|
useSavedStyle(newStyle) {
|
|
|
|
if (style.id !== newStyle.id) {
|
|
|
|
history.replaceState({}, '', `?id=${newStyle.id}`);
|
|
|
|
}
|
|
|
|
sessionStore.justEditedStyleId = newStyle.id;
|
|
|
|
Object.assign(style, newStyle);
|
|
|
|
editor.updateClass();
|
|
|
|
editor.updateMeta();
|
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2022-02-20 16:11:21 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#region colorpickerHelper
|
|
|
|
|
|
|
|
(async function colorpickerHelper() {
|
|
|
|
prefs.subscribe('editor.colorpicker.hotkey', (id, hotkey) => {
|
|
|
|
CodeMirror.commands.colorpicker = invokeColorpicker;
|
|
|
|
const extraKeys = CodeMirror.defaults.extraKeys;
|
|
|
|
for (const key in extraKeys) {
|
|
|
|
if (extraKeys[key] === 'colorpicker') {
|
|
|
|
delete extraKeys[key];
|
|
|
|
break;
|
2020-11-23 20:22:41 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
if (hotkey) {
|
|
|
|
extraKeys[hotkey] = 'colorpicker';
|
|
|
|
}
|
2020-11-23 20:22:41 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
prefs.subscribe('editor.colorpicker', (id, enabled) => {
|
|
|
|
const defaults = CodeMirror.defaults;
|
|
|
|
const keyName = prefs.get('editor.colorpicker.hotkey');
|
|
|
|
defaults.colorpicker = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
if (keyName) {
|
|
|
|
CodeMirror.commands.colorpicker = invokeColorpicker;
|
|
|
|
defaults.extraKeys = defaults.extraKeys || {};
|
|
|
|
defaults.extraKeys[keyName] = 'colorpicker';
|
|
|
|
}
|
|
|
|
defaults.colorpicker = {
|
|
|
|
tooltip: t('colorpickerTooltip'),
|
|
|
|
popup: {
|
|
|
|
tooltipForSwitcher: t('colorpickerSwitchFormatTooltip'),
|
|
|
|
paletteLine: t('numberedLine'),
|
|
|
|
paletteHint: t('colorpickerPaletteHint'),
|
|
|
|
hexUppercase: prefs.get('editor.colorpicker.hexUppercase'),
|
|
|
|
embedderCallback: state => {
|
|
|
|
['hexUppercase', 'color']
|
|
|
|
.filter(name => state[name] !== prefs.get('editor.colorpicker.' + name))
|
|
|
|
.forEach(name => prefs.set('editor.colorpicker.' + name, state[name]));
|
|
|
|
},
|
|
|
|
get maxHeight() {
|
|
|
|
return prefs.get('editor.colorpicker.maxHeight');
|
|
|
|
},
|
|
|
|
set maxHeight(h) {
|
|
|
|
prefs.set('editor.colorpicker.maxHeight', h);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
if (defaults.extraKeys) {
|
|
|
|
delete defaults.extraKeys[keyName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmFactory.globalSetOption('colorpicker', defaults.colorpicker);
|
|
|
|
}, {runNow: true});
|
|
|
|
|
|
|
|
$('#colorpicker-settings').onclick = function (event) {
|
|
|
|
event.preventDefault();
|
2021-08-06 10:04:00 +00:00
|
|
|
const input = createHotkeyInput('editor.colorpicker.hotkey', {onDone: () => helpPopup.close()});
|
2021-01-01 14:27:58 +00:00
|
|
|
const popup = helpPopup.show(t('helpKeyMapHotkey'), input);
|
|
|
|
const bounds = this.getBoundingClientRect();
|
|
|
|
popup.style.left = bounds.right + 10 + 'px';
|
|
|
|
popup.style.top = bounds.top - popup.clientHeight / 2 + 'px';
|
|
|
|
popup.style.right = 'auto';
|
2021-08-06 10:04:00 +00:00
|
|
|
$('input', popup).focus();
|
2021-01-01 14:27:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function invokeColorpicker(cm) {
|
|
|
|
cm.state.colorpicker.openPopup(prefs.get('editor.colorpicker.color'));
|
2020-11-23 20:22:41 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
//#endregion
|