2017-12-02 20:41:15 +00:00
|
|
|
/*
|
|
|
|
global CodeMirror linterConfig loadScript
|
2018-01-10 18:56:14 +00:00
|
|
|
global editors editor styleId ownTabId
|
2017-12-02 20:41:15 +00:00
|
|
|
global save toggleStyle setupAutocomplete makeSectionVisible getSectionForChild
|
2018-01-10 18:56:14 +00:00
|
|
|
global getSectionsHashes
|
|
|
|
global messageBox
|
2017-12-02 20:41:15 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-12-08 02:45:27 +00:00
|
|
|
onDOMscriptReady('/codemirror.js').then(() => {
|
2017-12-07 17:26:41 +00:00
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
CodeMirror.defaults.lint = linterConfig.getForCodeMirror();
|
|
|
|
|
|
|
|
const COMMANDS = {
|
|
|
|
save,
|
|
|
|
toggleStyle,
|
2018-03-03 20:30:33 +00:00
|
|
|
toggleEditorFocus,
|
2017-12-02 20:41:15 +00:00
|
|
|
jumpToLine,
|
|
|
|
nextEditor, prevEditor,
|
2018-03-03 20:31:21 +00:00
|
|
|
commentSelection,
|
2017-12-02 20:41:15 +00:00
|
|
|
};
|
2018-05-01 20:05:13 +00:00
|
|
|
const ORIGINAL_COMMANDS = {
|
|
|
|
insertTab: CodeMirror.commands.insertTab,
|
|
|
|
};
|
2017-12-02 20:41:15 +00:00
|
|
|
// reroute handling to nearest editor when keypress resolves to one of these commands
|
|
|
|
const REROUTED = new Set([
|
2018-03-03 20:31:21 +00:00
|
|
|
'save',
|
|
|
|
'toggleStyle',
|
|
|
|
'jumpToLine',
|
|
|
|
'nextEditor', 'prevEditor',
|
|
|
|
'toggleEditorFocus',
|
2017-12-18 06:55:32 +00:00
|
|
|
'find', 'findNext', 'findPrev', 'replace', 'replaceAll',
|
2017-12-02 20:41:15 +00:00
|
|
|
'colorpicker',
|
|
|
|
]);
|
|
|
|
Object.assign(CodeMirror, {
|
|
|
|
getOption,
|
|
|
|
setOption,
|
2017-12-18 06:55:32 +00:00
|
|
|
closestVisible,
|
2017-12-02 20:41:15 +00:00
|
|
|
});
|
|
|
|
Object.assign(CodeMirror.prototype, {
|
|
|
|
getSection,
|
|
|
|
rerouteHotkeys,
|
|
|
|
});
|
|
|
|
|
2018-01-14 12:57:29 +00:00
|
|
|
CodeMirror.defineInitHook(cm => {
|
2018-01-15 16:16:48 +00:00
|
|
|
if (prefs.get('editor.livePreview') && styleId) {
|
2018-01-14 12:57:29 +00:00
|
|
|
cm.on('changes', updatePreview);
|
|
|
|
}
|
|
|
|
if (prefs.get('editor.autocompleteOnTyping')) {
|
|
|
|
setupAutocomplete(cm);
|
|
|
|
}
|
|
|
|
const wrapper = cm.display.wrapper;
|
|
|
|
cm.on('blur', () => {
|
|
|
|
editors.lastActive = cm;
|
|
|
|
cm.rerouteHotkeys(true);
|
|
|
|
setTimeout(() => {
|
|
|
|
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cm.on('focus', () => {
|
|
|
|
cm.rerouteHotkeys(false);
|
|
|
|
wrapper.classList.add('CodeMirror-active');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-11 05:59:47 +00:00
|
|
|
new MutationObserver((mutations, observer) => {
|
|
|
|
if (!$('#sections')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
observer.disconnect();
|
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
prefs.subscribe(['editor.keyMap'], showHotkeyInTooltip);
|
|
|
|
addEventListener('showHotkeyInTooltip', showHotkeyInTooltip);
|
|
|
|
showHotkeyInTooltip();
|
2017-12-08 02:45:27 +00:00
|
|
|
|
2018-01-10 18:56:14 +00:00
|
|
|
// N.B. the onchange event listeners should be registered before setupLivePrefs()
|
2017-12-08 02:45:27 +00:00
|
|
|
$('#options').addEventListener('change', onOptionElementChanged);
|
2018-01-10 18:56:14 +00:00
|
|
|
setupLivePreview();
|
2017-12-17 19:04:30 +00:00
|
|
|
buildThemeElement();
|
|
|
|
buildKeymapElement();
|
2017-12-11 05:59:47 +00:00
|
|
|
setupLivePrefs();
|
2017-12-08 02:45:27 +00:00
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
Object.assign(CodeMirror.commands, COMMANDS);
|
2017-12-08 02:45:27 +00:00
|
|
|
rerouteHotkeys(true);
|
2017-12-11 05:59:47 +00:00
|
|
|
}).observe(document, {childList: true, subtree: true});
|
2017-12-02 20:41:15 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function getOption(o) {
|
|
|
|
return CodeMirror.defaults[o];
|
|
|
|
}
|
|
|
|
|
|
|
|
function setOption(o, v) {
|
|
|
|
CodeMirror.defaults[o] = v;
|
|
|
|
if (editors.length > 4 && (o === 'theme' || o === 'lineWrapping')) {
|
|
|
|
throttleSetOption({key: o, value: v, index: 0});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editors.forEach(editor => {
|
|
|
|
editor.setOption(o, v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function throttleSetOption({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
index,
|
|
|
|
timeStart = performance.now(),
|
|
|
|
cmStart = editors.lastActive || editors[0],
|
|
|
|
editorsCopy = editors.slice(),
|
|
|
|
progress,
|
|
|
|
}) {
|
|
|
|
if (index === 0) {
|
|
|
|
if (!cmStart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cmStart.setOption(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const THROTTLE_AFTER_MS = 100;
|
|
|
|
const THROTTLE_SHOW_PROGRESS_AFTER_MS = 100;
|
|
|
|
|
|
|
|
const t0 = performance.now();
|
|
|
|
const total = editorsCopy.length;
|
|
|
|
while (index < total) {
|
|
|
|
const cm = editorsCopy[index++];
|
|
|
|
if (cm === cmStart ||
|
|
|
|
cm !== editors[index] && !editors.includes(cm)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cm.setOption(key, value);
|
|
|
|
if (performance.now() - t0 > THROTTLE_AFTER_MS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index >= total) {
|
|
|
|
$.remove(progress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!progress &&
|
|
|
|
index < total / 2 &&
|
|
|
|
t0 - timeStart > THROTTLE_SHOW_PROGRESS_AFTER_MS) {
|
|
|
|
let option = $('#editor.' + key);
|
|
|
|
if (option) {
|
|
|
|
if (option.type === 'checkbox') {
|
|
|
|
option = (option.labels || [])[0] || option.nextElementSibling || option;
|
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
progress = document.body.appendChild(
|
|
|
|
$create('.set-option-progress', {targetElement: option}));
|
2017-12-02 20:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (progress) {
|
|
|
|
const optionBounds = progress.targetElement.getBoundingClientRect();
|
|
|
|
const bounds = {
|
|
|
|
top: optionBounds.top + window.scrollY + 1,
|
|
|
|
left: optionBounds.left + window.scrollX + 1,
|
|
|
|
width: (optionBounds.width - 2) * index / total | 0,
|
|
|
|
height: optionBounds.height - 2,
|
|
|
|
};
|
|
|
|
const style = progress.style;
|
|
|
|
for (const prop in bounds) {
|
|
|
|
if (bounds[prop] !== parseFloat(style[prop])) {
|
|
|
|
style[prop] = bounds[prop] + 'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(throttleSetOption, 0, {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
index,
|
|
|
|
timeStart,
|
|
|
|
cmStart,
|
|
|
|
editorsCopy,
|
|
|
|
progress,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSection() {
|
|
|
|
return this.display.wrapper.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextEditor(cm) {
|
2017-12-08 08:42:33 +00:00
|
|
|
return nextPrevEditor(cm, 1);
|
2017-12-02 20:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function prevEditor(cm) {
|
2017-12-08 08:42:33 +00:00
|
|
|
return nextPrevEditor(cm, -1);
|
2017-12-02 20:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function nextPrevEditor(cm, direction) {
|
|
|
|
cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.focus();
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function jumpToLine(cm) {
|
|
|
|
const cur = cm.getCursor();
|
|
|
|
refocusMinidialog(cm);
|
|
|
|
cm.openDialog(template.jumpToLine.cloneNode(true), str => {
|
|
|
|
const m = str.match(/^\s*(\d+)(?:\s*:\s*(\d+))?\s*$/);
|
|
|
|
if (m) {
|
|
|
|
cm.setCursor(m[1] - 1, m[2] ? m[2] - 1 : cur.ch);
|
|
|
|
}
|
|
|
|
}, {value: cur.line + 1});
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:31:21 +00:00
|
|
|
function commentSelection(cm) {
|
|
|
|
cm.blockComment(cm.getCursor('from'), cm.getCursor('to'), {fullLines: false});
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:30:33 +00:00
|
|
|
function toggleEditorFocus(cm) {
|
|
|
|
if (!cm) return;
|
|
|
|
if (cm.hasFocus()) {
|
|
|
|
setTimeout(() => cm.display.input.blur());
|
|
|
|
} else {
|
|
|
|
cm.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
function refocusMinidialog(cm) {
|
|
|
|
const section = cm.getSection();
|
|
|
|
if (!$('.CodeMirror-dialog', section)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// close the currently opened minidialog
|
|
|
|
cm.focus();
|
|
|
|
// make sure to focus the input in newly opened minidialog
|
|
|
|
setTimeout(() => {
|
|
|
|
$('.CodeMirror-dialog', section).focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onOptionElementChanged(event) {
|
|
|
|
const el = event.target;
|
|
|
|
let option = el.id.replace(/^editor\./, '');
|
|
|
|
if (!option) {
|
2017-12-03 01:18:58 +00:00
|
|
|
console.error('no "cm_option"', el);
|
2017-12-02 20:41:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let value = el.type === 'checkbox' ? el.checked : el.value;
|
|
|
|
switch (option) {
|
|
|
|
case 'tabSize':
|
|
|
|
value = Number(value);
|
|
|
|
CodeMirror.setOption('indentUnit', value);
|
|
|
|
break;
|
|
|
|
|
2018-05-01 20:05:13 +00:00
|
|
|
case 'indentWithTabs':
|
|
|
|
CodeMirror.commands.insertTab = value ?
|
|
|
|
ORIGINAL_COMMANDS.insertTab :
|
|
|
|
CodeMirror.commands.insertSoftTab;
|
|
|
|
break;
|
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
case 'theme': {
|
|
|
|
const themeLink = $('#cm-theme');
|
|
|
|
// use non-localized 'default' internally
|
|
|
|
if (!value || value === 'default' || value === t('defaultTheme')) {
|
|
|
|
value = 'default';
|
|
|
|
if (prefs.get(el.id) !== value) {
|
|
|
|
prefs.set(el.id, value);
|
|
|
|
}
|
|
|
|
themeLink.href = '';
|
|
|
|
el.selectedIndex = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const url = chrome.runtime.getURL('vendor/codemirror/theme/' + value + '.css');
|
|
|
|
if (themeLink.href === url) {
|
|
|
|
// preloaded in initCodeMirror()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// avoid flicker: wait for the second stylesheet to load, then apply the theme
|
2017-12-03 21:12:09 +00:00
|
|
|
document.head.appendChild($create('link#cm-theme2', {rel: 'stylesheet', href: url}));
|
2017-12-02 20:41:15 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
themeLink.remove();
|
|
|
|
$('#cm-theme2').id = 'cm-theme';
|
|
|
|
}, 100);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'autocompleteOnTyping':
|
|
|
|
editors.forEach(cm => setupAutocomplete(cm, el.checked));
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'autoCloseBrackets':
|
|
|
|
Promise.resolve(value && loadScript('/vendor/codemirror/addon/edit/closebrackets.js')).then(() => {
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'matchHighlight':
|
|
|
|
switch (value) {
|
|
|
|
case 'token':
|
|
|
|
case 'selection':
|
|
|
|
document.body.dataset[option] = value;
|
|
|
|
value = {showToken: value === 'token' && /[#.\-\w]/, annotateScrollbar: true};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
option = 'highlightSelectionMatches';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'colorpicker':
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:04:30 +00:00
|
|
|
function buildThemeElement() {
|
|
|
|
const themeElement = $('#editor.theme');
|
2017-12-02 20:41:15 +00:00
|
|
|
const themeList = localStorage.codeMirrorThemes;
|
2017-12-17 19:04:30 +00:00
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
const optionsFromArray = options => {
|
2017-12-17 19:04:30 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
options.forEach(opt => fragment.appendChild($create('option', opt)));
|
|
|
|
themeElement.appendChild(fragment);
|
|
|
|
};
|
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
if (themeList) {
|
2017-12-17 19:04:30 +00:00
|
|
|
optionsFromArray(themeList.split(/\s+/));
|
2017-12-02 20:41:15 +00:00
|
|
|
} else {
|
|
|
|
// Chrome is starting up and shows our edit.html, but the background page isn't loaded yet
|
|
|
|
const theme = prefs.get('editor.theme');
|
2017-12-17 19:04:30 +00:00
|
|
|
optionsFromArray([theme === 'default' ? t('defaultTheme') : theme]);
|
2017-12-02 20:41:15 +00:00
|
|
|
getCodeMirrorThemes().then(() => {
|
|
|
|
const themes = (localStorage.codeMirrorThemes || '').split(/\s+/);
|
2017-12-17 19:04:30 +00:00
|
|
|
optionsFromArray(themes);
|
|
|
|
themeElement.selectedIndex = Math.max(0, themes.indexOf(theme));
|
2017-12-02 20:41:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:04:30 +00:00
|
|
|
function buildKeymapElement() {
|
|
|
|
// move 'pc' or 'mac' prefix to the end of the displayed label
|
|
|
|
const maps = Object.keys(CodeMirror.keyMap)
|
|
|
|
.map(name => ({
|
|
|
|
value: name,
|
|
|
|
name: name.replace(/^(pc|mac)(.+)/, (s, arch, baseName) =>
|
|
|
|
baseName.toLowerCase() + '-' + (arch === 'mac' ? 'Mac' : 'PC')),
|
|
|
|
}))
|
|
|
|
.sort((a, b) => a.name < b.name && -1 || a.name > b.name && 1);
|
|
|
|
|
2017-12-02 20:41:15 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
2017-12-17 19:04:30 +00:00
|
|
|
let bin = fragment;
|
|
|
|
let groupName;
|
|
|
|
// group suffixed maps in <optgroup>
|
|
|
|
maps.forEach(({value, name}, i) => {
|
|
|
|
groupName = !name.includes('-') ? name : groupName;
|
|
|
|
const groupWithNext = maps[i + 1] && maps[i + 1].name.startsWith(groupName);
|
|
|
|
if (groupWithNext) {
|
|
|
|
if (bin === fragment) {
|
|
|
|
bin = fragment.appendChild($create('optgroup', {label: name.split('-')[0]}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const el = bin.appendChild($create('option', {value}, name));
|
|
|
|
if (value === prefs.defaults['editor.keyMap']) {
|
|
|
|
el.dataset.default = '';
|
|
|
|
el.title = t('defaultTheme');
|
|
|
|
}
|
2017-12-18 06:55:32 +00:00
|
|
|
if (!groupWithNext) bin = fragment;
|
2017-12-17 19:04:30 +00:00
|
|
|
});
|
|
|
|
$('#editor.keyMap').appendChild(fragment);
|
2017-12-02 20:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function rerouteHotkeys(enable, immediately) {
|
|
|
|
if (!immediately) {
|
|
|
|
debounce(rerouteHotkeys, 0, enable, true);
|
|
|
|
} else if (enable) {
|
|
|
|
document.addEventListener('keydown', rerouteHandler);
|
|
|
|
} else {
|
|
|
|
document.removeEventListener('keydown', rerouteHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function rerouteHandler(event) {
|
|
|
|
const keyName = CodeMirror.keyName(event);
|
|
|
|
if (!keyName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const rerouteCommand = name => {
|
|
|
|
if (REROUTED.has(name)) {
|
2017-12-18 06:55:32 +00:00
|
|
|
CodeMirror.commands[name](closestVisible(event.target));
|
2017-12-02 20:41:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (CodeMirror.lookupKey(keyName, CodeMirror.defaults.keyMap, rerouteCommand) === 'handled' ||
|
|
|
|
CodeMirror.lookupKey(keyName, CodeMirror.defaults.extraKeys, rerouteCommand) === 'handled') {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
// priority:
|
|
|
|
// 1. associated CM for applies-to element
|
|
|
|
// 2. last active if visible
|
|
|
|
// 3. first visible
|
|
|
|
function closestVisible(nearbyElement) {
|
|
|
|
const cm =
|
|
|
|
nearbyElement instanceof CodeMirror ? nearbyElement :
|
|
|
|
nearbyElement instanceof Node && (getSectionForChild(nearbyElement) || {}).CodeMirror ||
|
|
|
|
editors.lastActive;
|
|
|
|
if (nearbyElement instanceof Node && cm) {
|
|
|
|
const {left, top} = nearbyElement.getBoundingClientRect();
|
|
|
|
const bounds = cm.display.wrapper.getBoundingClientRect();
|
|
|
|
if (top >= 0 && top >= bounds.top &&
|
|
|
|
left >= 0 && left >= bounds.left) {
|
|
|
|
return cm;
|
|
|
|
}
|
2017-12-02 20:41:15 +00:00
|
|
|
}
|
|
|
|
// closest editor should have at least 2 lines visible
|
|
|
|
const lineHeight = editors[0].defaultTextHeight();
|
|
|
|
const scrollY = window.scrollY;
|
|
|
|
const windowBottom = scrollY + window.innerHeight - 2 * lineHeight;
|
|
|
|
const allSectionsContainerTop = scrollY + $('#sections').getBoundingClientRect().top;
|
|
|
|
const distances = [];
|
|
|
|
const alreadyInView = cm && offscreenDistance(null, cm) === 0;
|
|
|
|
return alreadyInView ? cm : findClosest();
|
|
|
|
|
|
|
|
function offscreenDistance(index, cm) {
|
|
|
|
if (index >= 0 && distances[index] !== undefined) {
|
|
|
|
return distances[index];
|
|
|
|
}
|
|
|
|
const section = (cm || editors[index]).getSection();
|
2017-12-18 06:55:32 +00:00
|
|
|
if (!section) {
|
|
|
|
return 1e9;
|
|
|
|
}
|
2017-12-02 20:41:15 +00:00
|
|
|
const top = allSectionsContainerTop + section.offsetTop;
|
|
|
|
if (top < scrollY + lineHeight) {
|
|
|
|
return Math.max(0, scrollY - top - lineHeight);
|
|
|
|
}
|
|
|
|
if (top < windowBottom) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const distance = top - windowBottom + section.offsetHeight;
|
|
|
|
if (index >= 0) {
|
|
|
|
distances[index] = distance;
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findClosest() {
|
|
|
|
const last = editors.length - 1;
|
|
|
|
let a = 0;
|
|
|
|
let b = last;
|
|
|
|
let c;
|
|
|
|
let distance;
|
|
|
|
while (a < b - 1) {
|
|
|
|
c = (a + b) / 2 | 0;
|
|
|
|
distance = offscreenDistance(c);
|
|
|
|
if (!distance || !c) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const distancePrev = offscreenDistance(c - 1);
|
|
|
|
const distanceNext = c < last ? offscreenDistance(c + 1) : 1e20;
|
|
|
|
if (distancePrev <= distance && distance <= distanceNext) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (b && offscreenDistance(b - 1) <= offscreenDistance(b)) {
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
const cm = editors[b];
|
|
|
|
if (distances[b] > 0) {
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
}
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function getCodeMirrorThemes() {
|
|
|
|
if (!chrome.runtime.getPackageDirectoryEntry) {
|
|
|
|
const themes = [
|
|
|
|
chrome.i18n.getMessage('defaultTheme'),
|
2018-07-21 17:58:54 +00:00
|
|
|
/* populate-theme-start */
|
2017-12-02 20:41:15 +00:00
|
|
|
'3024-day',
|
|
|
|
'3024-night',
|
|
|
|
'abcdef',
|
|
|
|
'ambiance',
|
|
|
|
'ambiance-mobile',
|
|
|
|
'base16-dark',
|
|
|
|
'base16-light',
|
|
|
|
'bespin',
|
|
|
|
'blackboard',
|
|
|
|
'cobalt',
|
|
|
|
'colorforth',
|
2018-07-21 17:58:54 +00:00
|
|
|
'darcula',
|
2017-12-02 20:41:15 +00:00
|
|
|
'dracula',
|
|
|
|
'duotone-dark',
|
|
|
|
'duotone-light',
|
|
|
|
'eclipse',
|
|
|
|
'elegant',
|
|
|
|
'erlang-dark',
|
2018-07-21 17:58:54 +00:00
|
|
|
'gruvbox-dark',
|
2017-12-02 20:41:15 +00:00
|
|
|
'hopscotch',
|
|
|
|
'icecoder',
|
2018-07-21 17:58:54 +00:00
|
|
|
'idea',
|
2017-12-02 20:41:15 +00:00
|
|
|
'isotope',
|
|
|
|
'lesser-dark',
|
|
|
|
'liquibyte',
|
2018-07-21 17:58:54 +00:00
|
|
|
'lucario',
|
2017-12-02 20:41:15 +00:00
|
|
|
'material',
|
|
|
|
'mbo',
|
|
|
|
'mdn-like',
|
|
|
|
'midnight',
|
|
|
|
'monokai',
|
|
|
|
'neat',
|
|
|
|
'neo',
|
|
|
|
'night',
|
2018-03-03 20:36:13 +00:00
|
|
|
'oceanic-next',
|
2017-12-02 20:41:15 +00:00
|
|
|
'panda-syntax',
|
|
|
|
'paraiso-dark',
|
|
|
|
'paraiso-light',
|
|
|
|
'pastel-on-dark',
|
|
|
|
'railscasts',
|
|
|
|
'rubyblue',
|
|
|
|
'seti',
|
2018-03-03 20:36:13 +00:00
|
|
|
'shadowfox',
|
2017-12-02 20:41:15 +00:00
|
|
|
'solarized',
|
2018-07-21 17:58:54 +00:00
|
|
|
'ssms',
|
2017-12-02 20:41:15 +00:00
|
|
|
'the-matrix',
|
|
|
|
'tomorrow-night-bright',
|
|
|
|
'tomorrow-night-eighties',
|
|
|
|
'ttcn',
|
|
|
|
'twilight',
|
|
|
|
'vibrant-ink',
|
|
|
|
'xq-dark',
|
|
|
|
'xq-light',
|
|
|
|
'yeti',
|
|
|
|
'zenburn',
|
2018-07-21 17:58:54 +00:00
|
|
|
/* populate-theme-end */
|
2017-12-02 20:41:15 +00:00
|
|
|
];
|
|
|
|
localStorage.codeMirrorThemes = themes.join(' ');
|
|
|
|
return Promise.resolve(themes);
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
chrome.runtime.getPackageDirectoryEntry(rootDir => {
|
|
|
|
rootDir.getDirectory('vendor/codemirror/theme', {create: false}, themeDir => {
|
|
|
|
themeDir.createReader().readEntries(entries => {
|
|
|
|
const themes = [
|
|
|
|
chrome.i18n.getMessage('defaultTheme')
|
|
|
|
].concat(
|
|
|
|
entries.filter(entry => entry.isFile)
|
|
|
|
.sort((a, b) => (a.name < b.name ? -1 : 1))
|
|
|
|
.map(entry => entry.name.replace(/\.css$/, ''))
|
|
|
|
);
|
|
|
|
localStorage.codeMirrorThemes = themes.join(' ');
|
|
|
|
resolve(themes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-12-08 02:45:27 +00:00
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
function showHotkeyInTooltip(_, mapName = prefs.get('editor.keyMap')) {
|
|
|
|
const extraKeys = CodeMirror.defaults.extraKeys;
|
|
|
|
for (const el of $$('[data-hotkey-tooltip]')) {
|
|
|
|
if (el._hotkeyTooltipKeyMap !== mapName) {
|
|
|
|
el._hotkeyTooltipKeyMap = mapName;
|
|
|
|
const title = el._hotkeyTooltipTitle = el._hotkeyTooltipTitle || el.title;
|
|
|
|
const cmd = el.dataset.hotkeyTooltip;
|
2017-12-22 15:35:46 +00:00
|
|
|
const key = cmd[0] === '=' ? cmd.slice(1) :
|
|
|
|
findKeyForCommand(cmd, mapName) ||
|
2017-12-18 06:55:32 +00:00
|
|
|
extraKeys && findKeyForCommand(cmd, extraKeys);
|
|
|
|
const newTitle = title + (title && key ? '\n' : '') + (key || '');
|
|
|
|
if (el.title !== newTitle) el.title = newTitle;
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 02:45:27 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 06:55:32 +00:00
|
|
|
function findKeyForCommand(command, map) {
|
|
|
|
if (typeof map === 'string') map = CodeMirror.keyMap[map];
|
2017-12-08 02:45:27 +00:00
|
|
|
let key = Object.keys(map).find(k => map[k] === command);
|
|
|
|
if (key) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
for (const ft of Array.isArray(map.fallthrough) ? map.fallthrough : [map.fallthrough]) {
|
|
|
|
key = ft && findKeyForCommand(command, ft);
|
|
|
|
if (key) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2018-01-10 18:56:14 +00:00
|
|
|
|
|
|
|
function setupLivePreview() {
|
|
|
|
if (!prefs.get('editor.livePreview') && !editors.length) {
|
|
|
|
setTimeout(setupLivePreview);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-04 19:27:14 +00:00
|
|
|
if (styleId) {
|
|
|
|
$('#editor.livePreview').onchange = livePreviewToggled;
|
2018-01-12 11:11:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-04 19:27:14 +00:00
|
|
|
// wait for #preview-label's class to lose 'hidden' after the first save
|
|
|
|
new MutationObserver((_, observer) => {
|
|
|
|
if (!styleId) return;
|
|
|
|
observer.disconnect();
|
|
|
|
setupLivePreview();
|
|
|
|
livePreviewToggled();
|
|
|
|
}).observe($('#preview-label'), {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['class'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function livePreviewToggled() {
|
|
|
|
const me = this instanceof Node ? this : $('#editor.livePreview');
|
|
|
|
const previewing = me.checked;
|
|
|
|
editors.forEach(cm => cm[previewing ? 'on' : 'off']('changes', updatePreview));
|
2018-08-01 21:46:14 +00:00
|
|
|
const addRemove = EventTarget.prototype[previewing ? 'addEventListener' : 'removeEventListener'];
|
|
|
|
addRemove.call($('#enabled'), 'change', updatePreview);
|
|
|
|
if (!editor) {
|
|
|
|
for (const el of $$('#sections .applies-to')) {
|
|
|
|
addRemove.call(el, 'input', updatePreview);
|
|
|
|
}
|
|
|
|
toggleLivePreviewSectionsObserver(previewing);
|
|
|
|
}
|
2018-05-04 19:27:14 +00:00
|
|
|
if (!previewing || document.body.classList.contains('dirty')) {
|
|
|
|
updatePreview(null, previewing);
|
|
|
|
}
|
2018-01-10 18:56:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 21:46:14 +00:00
|
|
|
/**
|
|
|
|
* Observes newly added section elements and sets an 'input' event listener on .applies-to inside.
|
|
|
|
* The goal is to avoid listening to 'input' on the entire #sections tree,
|
|
|
|
* which would trigger updatePreview() twice on any keystroke -
|
|
|
|
* both for the synthetic event from CodeMirror and the original event.
|
|
|
|
* Side effects:
|
|
|
|
* two expando properties on #sections
|
|
|
|
* 1. __livePreviewObserver
|
|
|
|
* 2. __livePreviewObserverEnabled
|
|
|
|
* @param {Boolean} enable
|
|
|
|
*/
|
|
|
|
function toggleLivePreviewSectionsObserver(enable) {
|
|
|
|
const sections = $('#sections');
|
|
|
|
const observing = sections.__livePreviewObserverEnabled;
|
|
|
|
let mo = sections.__livePreviewObserver;
|
|
|
|
if (enable && !mo) {
|
|
|
|
sections.__livePreviewObserver = mo = new MutationObserver(mutations => {
|
|
|
|
for (const {addedNodes} of mutations) {
|
|
|
|
for (const node of addedNodes) {
|
|
|
|
const el = node.children && $('.applies-to', node);
|
|
|
|
if (el) el.addEventListener('input', updatePreview);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (enable && !observing) {
|
|
|
|
mo.observe(sections, {childList: true});
|
|
|
|
sections.__livePreviewObserverEnabled = true;
|
|
|
|
} else if (!enable && observing) {
|
|
|
|
mo.disconnect();
|
|
|
|
sections.__livePreviewObserverEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 18:56:14 +00:00
|
|
|
function updatePreview(data, previewing) {
|
|
|
|
if (previewing !== true && previewing !== false) {
|
2018-05-16 22:33:18 +00:00
|
|
|
if (data instanceof Event && !data.target.matches('.style-contributor')) return;
|
2018-01-10 18:56:14 +00:00
|
|
|
debounce(updatePreview, data && data.id === 'enabled' ? 0 : 400, null, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const errors = $('#preview-errors');
|
|
|
|
API.refreshAllTabs({
|
|
|
|
reason: 'editPreview',
|
|
|
|
tabId: ownTabId,
|
|
|
|
style: {
|
|
|
|
id: styleId,
|
|
|
|
enabled: $('#enabled').checked,
|
|
|
|
sections: previewing && (editor ? editors[0].getValue() : getSectionsHashes()),
|
|
|
|
},
|
|
|
|
}).then(() => {
|
|
|
|
errors.classList.add('hidden');
|
|
|
|
}).catch(err => {
|
2018-03-11 13:47:27 +00:00
|
|
|
if (err && editor && !Number.isNaN(err.index)) {
|
|
|
|
const pos = editors[0].posFromIndex(err.index);
|
|
|
|
err = `${pos.line}:${pos.ch} ${err}`;
|
|
|
|
}
|
2018-01-10 18:56:14 +00:00
|
|
|
errors.classList.remove('hidden');
|
2018-03-11 13:31:25 +00:00
|
|
|
errors.onclick = () => messageBox.alert(String(err), 'pre');
|
2018-01-10 18:56:14 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-02 20:41:15 +00:00
|
|
|
});
|