2020-11-08 08:12:42 +00:00
|
|
|
/* global
|
|
|
|
$
|
|
|
|
cmFactory
|
|
|
|
debounce
|
|
|
|
DocFuncMapper
|
|
|
|
editor
|
|
|
|
initBeautifyButton
|
|
|
|
linter
|
|
|
|
prefs
|
|
|
|
regExpTester
|
|
|
|
t
|
|
|
|
trimCommentLabel
|
|
|
|
tryRegExp
|
|
|
|
*/
|
2018-11-07 06:09:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/* exported createSection */
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
/**
|
|
|
|
* @param {StyleSection} originalSection
|
|
|
|
* @param {function():number} genId
|
|
|
|
* @param {EditorScrollInfo} [si]
|
|
|
|
* @returns {EditorSection}
|
|
|
|
*/
|
|
|
|
function createSection(originalSection, genId, si) {
|
2020-11-08 08:12:42 +00:00
|
|
|
const {dirty} = editor;
|
2018-11-07 06:09:29 +00:00
|
|
|
const sectionId = genId();
|
2020-11-18 11:17:15 +00:00
|
|
|
const el = t.template.section.cloneNode(true);
|
2020-11-08 08:12:42 +00:00
|
|
|
const elLabel = $('.code-label', el);
|
2018-11-07 06:09:29 +00:00
|
|
|
const cm = cmFactory.create(wrapper => {
|
2020-11-08 08:12:42 +00:00
|
|
|
// making it tall during initial load so IntersectionObserver sees only one adjacent CM
|
2020-11-18 11:17:15 +00:00
|
|
|
if (editor.ready !== true) {
|
|
|
|
wrapper.style.height = si ? si.height : '100vh';
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
elLabel.after(wrapper);
|
|
|
|
}, {
|
|
|
|
value: originalSection.code,
|
|
|
|
});
|
2020-06-22 16:14:41 +00:00
|
|
|
el.CodeMirror = cm; // used by getAssociatedEditor
|
2020-11-18 11:17:15 +00:00
|
|
|
editor.applyScrollInfo(cm, si);
|
2018-11-07 06:09:29 +00:00
|
|
|
|
|
|
|
const changeListeners = new Set();
|
|
|
|
|
|
|
|
const appliesToContainer = $('.applies-to-list', el);
|
|
|
|
const appliesTo = [];
|
2020-11-08 08:12:42 +00:00
|
|
|
DocFuncMapper.forEachProp(originalSection, (type, value) =>
|
|
|
|
insertApplyAfter({type, value}));
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!appliesTo.length) {
|
|
|
|
insertApplyAfter({all: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
let changeGeneration = cm.changeGeneration();
|
|
|
|
let removed = false;
|
|
|
|
|
|
|
|
registerEvents();
|
|
|
|
updateRegexpTester();
|
|
|
|
createResizeGrip(cm);
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
/** @namespace EditorSection */
|
2018-11-07 06:09:29 +00:00
|
|
|
const section = {
|
|
|
|
id: sectionId,
|
|
|
|
el,
|
|
|
|
cm,
|
2020-11-08 08:12:42 +00:00
|
|
|
appliesTo,
|
|
|
|
getModel() {
|
|
|
|
const items = appliesTo.map(a => !a.all && [a.type, a.value]);
|
|
|
|
return DocFuncMapper.toSection(items, {code: cm.getValue()});
|
|
|
|
},
|
|
|
|
remove() {
|
|
|
|
linter.disableForEditor(cm);
|
|
|
|
el.classList.add('removed');
|
|
|
|
removed = true;
|
|
|
|
appliesTo.forEach(a => a.remove());
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
cm.refresh();
|
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
cmFactory.destroy(cm);
|
|
|
|
},
|
|
|
|
restore() {
|
|
|
|
linter.enableForEditor(cm);
|
|
|
|
el.classList.remove('removed');
|
|
|
|
removed = false;
|
|
|
|
appliesTo.forEach(a => a.restore());
|
|
|
|
cm.refresh();
|
|
|
|
},
|
|
|
|
onChange(fn) {
|
|
|
|
changeListeners.add(fn);
|
|
|
|
},
|
|
|
|
off(fn) {
|
|
|
|
changeListeners.delete(fn);
|
|
|
|
},
|
|
|
|
get removed() {
|
|
|
|
return removed;
|
|
|
|
},
|
|
|
|
tocEntry: {
|
|
|
|
label: '',
|
|
|
|
get removed() {
|
|
|
|
return removed;
|
|
|
|
},
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
};
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
prefs.subscribe('editor.toc.expanded', updateTocPrefToggled, {now: true});
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
return section;
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function emitSectionChange(origin) {
|
2018-11-07 06:09:29 +00:00
|
|
|
for (const fn of changeListeners) {
|
2020-11-08 08:12:42 +00:00
|
|
|
fn(origin);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerEvents() {
|
|
|
|
cm.on('changes', () => {
|
|
|
|
const newGeneration = cm.changeGeneration();
|
|
|
|
dirty.modify(`section.${sectionId}.code`, changeGeneration, newGeneration);
|
|
|
|
changeGeneration = newGeneration;
|
2020-11-08 08:12:42 +00:00
|
|
|
emitSectionChange('code');
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2020-11-08 08:12:42 +00:00
|
|
|
cm.display.wrapper.on('keydown', event => handleKeydown(cm, event), true);
|
|
|
|
$('.test-regexp', el).onclick = () => {
|
2018-11-07 06:09:29 +00:00
|
|
|
regExpTester.toggle();
|
|
|
|
updateRegexpTester();
|
2020-11-08 08:12:42 +00:00
|
|
|
};
|
2020-06-22 16:14:41 +00:00
|
|
|
initBeautifyButton($('.beautify-section', el), () => [cm]);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeydown(cm, event) {
|
2020-10-13 18:14:54 +00:00
|
|
|
if (event.shiftKey || event.altKey || event.metaKey) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
const {key} = event;
|
2018-11-07 06:09:29 +00:00
|
|
|
const {line, ch} = cm.getCursor();
|
|
|
|
switch (key) {
|
2020-10-13 18:14:54 +00:00
|
|
|
case 'ArrowLeft':
|
2018-11-07 06:09:29 +00:00
|
|
|
if (line || ch) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
// fallthrough
|
|
|
|
case 'ArrowUp':
|
2020-11-08 08:12:42 +00:00
|
|
|
cm = line === 0 && editor.prevEditor(cm, false);
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!cm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2020-10-13 18:14:54 +00:00
|
|
|
cm.setCursor(cm.doc.size - 1, key === 'ArrowLeft' ? 1e20 : ch);
|
2018-11-07 06:09:29 +00:00
|
|
|
break;
|
2020-10-13 18:14:54 +00:00
|
|
|
case 'ArrowRight':
|
2018-11-07 06:09:29 +00:00
|
|
|
if (line < cm.doc.size - 1 || ch < cm.getLine(line).length - 1) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
// fallthrough
|
|
|
|
case 'ArrowDown':
|
2020-11-08 08:12:42 +00:00
|
|
|
cm = line === cm.doc.size - 1 && editor.nextEditor(cm, false);
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!cm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
cm.setCursor(0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateRegexpTester() {
|
|
|
|
const regexps = appliesTo.filter(a => a.type === 'regexp')
|
|
|
|
.map(a => a.value);
|
|
|
|
if (regexps.length) {
|
|
|
|
el.classList.add('has-regexp');
|
|
|
|
regExpTester.update(regexps);
|
|
|
|
} else {
|
|
|
|
el.classList.remove('has-regexp');
|
|
|
|
regExpTester.toggle(false);
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateTocEntry(origin) {
|
|
|
|
const te = section.tocEntry;
|
|
|
|
let changed;
|
|
|
|
if (origin === 'code' || !origin) {
|
|
|
|
const label = getLabelFromComment();
|
|
|
|
if (te.label !== label) {
|
|
|
|
te.label = elLabel.dataset.text = label;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!te.label) {
|
|
|
|
const target = appliesTo[0].all ? null : appliesTo[0].value;
|
|
|
|
if (te.target !== target) {
|
|
|
|
te.target = target;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (te.numTargets !== appliesTo.length) {
|
|
|
|
te.numTargets = appliesTo.length;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) editor.updateToc([section]);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateTocEntryLazy(...args) {
|
|
|
|
debounce(updateTocEntry, 0, ...args);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateTocFocus() {
|
|
|
|
editor.updateToc({focus: true, 0: section});
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function updateTocPrefToggled(key, val) {
|
|
|
|
changeListeners[val ? 'add' : 'delete'](updateTocEntryLazy);
|
|
|
|
el.onOff(val, 'focusin', updateTocFocus);
|
|
|
|
if (val) {
|
|
|
|
updateTocEntry();
|
|
|
|
if (el.contains(document.activeElement)) {
|
|
|
|
updateTocFocus();
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
function getLabelFromComment() {
|
|
|
|
let cmt = '';
|
|
|
|
let inCmt;
|
|
|
|
cm.eachLine(({text}) => {
|
|
|
|
let i = 0;
|
|
|
|
if (!inCmt) {
|
|
|
|
i = text.search(/\S/);
|
|
|
|
if (i < 0) return;
|
|
|
|
inCmt = text[i] === '/' && text[i + 1] === '*';
|
|
|
|
if (!inCmt) return true;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
const j = text.indexOf('*/', i);
|
|
|
|
cmt = trimCommentLabel(text.slice(i, j >= 0 ? j : text.length));
|
|
|
|
return j >= 0 || cmt;
|
|
|
|
});
|
|
|
|
return cmt;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertApplyAfter(init, base) {
|
|
|
|
const apply = createApply(init);
|
2020-10-11 14:13:25 +00:00
|
|
|
appliesTo.splice(base ? appliesTo.indexOf(base) + 1 : appliesTo.length, 0, apply);
|
|
|
|
appliesToContainer.insertBefore(apply.el, base ? base.el.nextSibling : null);
|
2018-11-07 06:09:29 +00:00
|
|
|
dirty.add(apply, apply);
|
|
|
|
if (appliesTo.length > 1 && appliesTo[0].all) {
|
|
|
|
removeApply(appliesTo[0]);
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
emitSectionChange('apply');
|
2020-10-11 14:13:25 +00:00
|
|
|
return apply;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeApply(apply) {
|
|
|
|
const index = appliesTo.indexOf(apply);
|
|
|
|
appliesTo.splice(index, 1);
|
|
|
|
apply.remove();
|
|
|
|
apply.el.remove();
|
|
|
|
dirty.remove(apply, apply);
|
|
|
|
if (!appliesTo.length) {
|
|
|
|
insertApplyAfter({all: true});
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
emitSectionChange('apply');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createApply({type = 'url', value, all = false}) {
|
|
|
|
const applyId = genId();
|
|
|
|
const dirtyPrefix = `section.${sectionId}.apply.${applyId}`;
|
2020-11-18 11:17:15 +00:00
|
|
|
const el = all ? t.template.appliesToEverything.cloneNode(true) :
|
|
|
|
t.template.appliesTo.cloneNode(true);
|
2018-11-07 06:09:29 +00:00
|
|
|
|
|
|
|
const selectEl = !all && $('.applies-type', el);
|
|
|
|
if (selectEl) {
|
|
|
|
selectEl.value = type;
|
2020-11-08 08:12:42 +00:00
|
|
|
selectEl.on('change', () => {
|
2018-11-07 06:09:29 +00:00
|
|
|
const oldType = type;
|
|
|
|
dirty.modify(`${dirtyPrefix}.type`, type, selectEl.value);
|
|
|
|
type = selectEl.value;
|
|
|
|
if (oldType === 'regexp' || type === 'regexp') {
|
|
|
|
updateRegexpTester();
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
emitSectionChange('apply');
|
2018-11-07 06:09:29 +00:00
|
|
|
validate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const valueEl = !all && $('.applies-value', el);
|
|
|
|
if (valueEl) {
|
|
|
|
valueEl.value = value;
|
2020-11-08 08:12:42 +00:00
|
|
|
valueEl.on('input', () => {
|
2018-11-07 06:09:29 +00:00
|
|
|
dirty.modify(`${dirtyPrefix}.value`, value, valueEl.value);
|
|
|
|
value = valueEl.value;
|
|
|
|
if (type === 'regexp') {
|
|
|
|
updateRegexpTester();
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
emitSectionChange('apply');
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2020-11-08 08:12:42 +00:00
|
|
|
valueEl.on('change', validate);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restore();
|
|
|
|
|
|
|
|
const apply = {
|
|
|
|
id: applyId,
|
|
|
|
all,
|
|
|
|
remove,
|
|
|
|
restore,
|
|
|
|
el,
|
2020-11-08 08:12:42 +00:00
|
|
|
valueEl, // used by validator
|
|
|
|
get type() {
|
|
|
|
return type;
|
|
|
|
},
|
|
|
|
get value() {
|
|
|
|
return value;
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const removeButton = $('.remove-applies-to', el);
|
|
|
|
if (removeButton) {
|
2020-11-08 08:12:42 +00:00
|
|
|
removeButton.on('click', e => {
|
2018-11-07 06:09:29 +00:00
|
|
|
e.preventDefault();
|
|
|
|
removeApply(apply);
|
|
|
|
});
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
$('.add-applies-to', el).on('click', e => {
|
2018-11-07 06:09:29 +00:00
|
|
|
e.preventDefault();
|
2020-10-11 14:13:25 +00:00
|
|
|
const newApply = insertApplyAfter({type, value: ''}, apply);
|
|
|
|
$('input', newApply.el).focus();
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return apply;
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
if (type !== 'regexp' || tryRegExp(value)) {
|
|
|
|
valueEl.setCustomValidity('');
|
|
|
|
} else {
|
|
|
|
valueEl.setCustomValidity(t('styleBadRegexp'));
|
|
|
|
setTimeout(() => valueEl.reportValidity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove() {
|
|
|
|
if (all) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dirty.remove(`${dirtyPrefix}.type`, type);
|
|
|
|
dirty.remove(`${dirtyPrefix}.value`, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore() {
|
|
|
|
if (all) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dirty.add(`${dirtyPrefix}.type`, type);
|
|
|
|
dirty.add(`${dirtyPrefix}.value`, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
|
|
|
|
function createResizeGrip(cm) {
|
|
|
|
const wrapper = cm.display.wrapper;
|
|
|
|
wrapper.classList.add('resize-grip-enabled');
|
2020-11-18 11:17:15 +00:00
|
|
|
const resizeGrip = t.template.resizeGrip.cloneNode(true);
|
2020-11-08 08:12:42 +00:00
|
|
|
wrapper.appendChild(resizeGrip);
|
|
|
|
let lastClickTime = 0;
|
|
|
|
let initHeight;
|
|
|
|
let initY;
|
|
|
|
resizeGrip.onmousedown = event => {
|
|
|
|
initHeight = wrapper.offsetHeight;
|
|
|
|
initY = event.pageY;
|
|
|
|
if (event.button !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
if (Date.now() - lastClickTime < 500) {
|
|
|
|
lastClickTime = 0;
|
|
|
|
toggleSectionHeight(cm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastClickTime = Date.now();
|
|
|
|
const minHeight = cm.defaultTextHeight() +
|
|
|
|
/* .CodeMirror-lines padding */
|
|
|
|
cm.display.lineDiv.offsetParent.offsetTop +
|
|
|
|
/* borders */
|
|
|
|
wrapper.offsetHeight - wrapper.clientHeight;
|
|
|
|
wrapper.style.pointerEvents = 'none';
|
|
|
|
document.body.style.cursor = 's-resize';
|
|
|
|
document.on('mousemove', resize);
|
|
|
|
document.on('mouseup', resizeStop);
|
|
|
|
|
|
|
|
function resize(e) {
|
|
|
|
const height = Math.max(minHeight, initHeight + e.pageY - initY);
|
|
|
|
if (height !== wrapper.offsetHeight) {
|
|
|
|
cm.setSize(null, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resizeStop() {
|
|
|
|
document.off('mouseup', resizeStop);
|
|
|
|
document.off('mousemove', resize);
|
|
|
|
wrapper.style.pointerEvents = '';
|
|
|
|
document.body.style.cursor = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function toggleSectionHeight(cm) {
|
|
|
|
if (cm.state.toggleHeightSaved) {
|
|
|
|
// restore previous size
|
|
|
|
cm.setSize(null, cm.state.toggleHeightSaved);
|
|
|
|
cm.state.toggleHeightSaved = 0;
|
|
|
|
} else {
|
|
|
|
// maximize
|
|
|
|
const wrapper = cm.display.wrapper;
|
|
|
|
const allBounds = $('#sections').getBoundingClientRect();
|
|
|
|
const pageExtrasHeight = allBounds.top + window.scrollY +
|
|
|
|
parseFloat(getComputedStyle($('#sections')).paddingBottom);
|
|
|
|
const sectionEl = wrapper.parentNode;
|
|
|
|
const sectionExtrasHeight = sectionEl.clientHeight - wrapper.offsetHeight;
|
|
|
|
cm.state.toggleHeightSaved = wrapper.clientHeight;
|
|
|
|
cm.setSize(null, window.innerHeight - sectionExtrasHeight - pageExtrasHeight);
|
|
|
|
const bounds = sectionEl.getBoundingClientRect();
|
|
|
|
if (bounds.top < 0 || bounds.bottom > window.innerHeight) {
|
|
|
|
window.scrollBy(0, bounds.top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|