2020-06-22 16:14:41 +00:00
|
|
|
/* global CodeMirror showHelp cmFactory onDOMready $ prefs t createHotkeyInput */
|
2017-11-22 00:12:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
(() => {
|
2017-11-22 00:12:05 +00:00
|
|
|
onDOMready().then(() => {
|
|
|
|
$('#colorpicker-settings').onclick = configureColorpicker;
|
|
|
|
});
|
2020-11-21 19:16:15 +00:00
|
|
|
prefs.subscribe('editor.colorpicker.hotkey', registerHotkey);
|
|
|
|
prefs.subscribe('editor.colorpicker', setColorpickerOption, {now: true});
|
2017-11-22 00:12:05 +00:00
|
|
|
|
|
|
|
function setColorpickerOption(id, enabled) {
|
|
|
|
const defaults = CodeMirror.defaults;
|
|
|
|
const keyName = prefs.get('editor.colorpicker.hotkey');
|
|
|
|
defaults.colorpicker = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
if (keyName) {
|
|
|
|
CodeMirror.commands.colorpicker = invokeColorpicker;
|
2017-12-08 02:45:27 +00:00
|
|
|
defaults.extraKeys = defaults.extraKeys || {};
|
2017-11-22 00:12:05 +00:00
|
|
|
defaults.extraKeys[keyName] = 'colorpicker';
|
|
|
|
}
|
|
|
|
defaults.colorpicker = {
|
|
|
|
tooltip: t('colorpickerTooltip'),
|
2017-12-21 13:04:12 +00:00
|
|
|
popup: {
|
2017-11-22 00:12:05 +00:00
|
|
|
tooltipForSwitcher: t('colorpickerSwitchFormatTooltip'),
|
2020-10-26 15:03:41 +00:00
|
|
|
paletteLine: t('numberedLine'),
|
|
|
|
paletteHint: t('colorpickerPaletteHint'),
|
2017-11-22 00:12:05 +00:00
|
|
|
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]));
|
|
|
|
},
|
2020-10-26 15:03:41 +00:00
|
|
|
get maxHeight() {
|
|
|
|
return prefs.get('editor.colorpicker.maxHeight');
|
|
|
|
},
|
|
|
|
set maxHeight(h) {
|
|
|
|
prefs.set('editor.colorpicker.maxHeight', h);
|
|
|
|
},
|
2017-11-22 00:12:05 +00:00
|
|
|
},
|
|
|
|
};
|
2017-12-08 02:45:27 +00:00
|
|
|
} else {
|
|
|
|
if (defaults.extraKeys) {
|
|
|
|
delete defaults.extraKeys[keyName];
|
|
|
|
}
|
2017-11-22 00:12:05 +00:00
|
|
|
}
|
2020-11-21 19:16:15 +00:00
|
|
|
cmFactory.globalSetOption('colorpicker', defaults.colorpicker);
|
2017-11-22 00:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerHotkey(id, hotkey) {
|
2017-11-22 02:15:52 +00:00
|
|
|
CodeMirror.commands.colorpicker = invokeColorpicker;
|
2017-11-22 00:12:05 +00:00
|
|
|
const extraKeys = CodeMirror.defaults.extraKeys;
|
|
|
|
for (const key in extraKeys) {
|
|
|
|
if (extraKeys[key] === 'colorpicker') {
|
|
|
|
delete extraKeys[key];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hotkey) {
|
|
|
|
extraKeys[hotkey] = 'colorpicker';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function invokeColorpicker(cm) {
|
|
|
|
cm.state.colorpicker.openPopup(prefs.get('editor.colorpicker.color'));
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:33:41 +00:00
|
|
|
function configureColorpicker(event) {
|
|
|
|
event.preventDefault();
|
2020-06-22 16:14:41 +00:00
|
|
|
const input = createHotkeyInput('editor.colorpicker.hotkey', () => {
|
|
|
|
$('#help-popup .dismiss').onclick();
|
2017-11-22 00:12:05 +00:00
|
|
|
});
|
|
|
|
const popup = showHelp(t('helpKeyMapHotkey'), input);
|
|
|
|
if (this instanceof Element) {
|
|
|
|
const bounds = this.getBoundingClientRect();
|
|
|
|
popup.style.left = bounds.right + 10 + 'px';
|
|
|
|
popup.style.top = bounds.top - popup.clientHeight / 2 + 'px';
|
|
|
|
popup.style.right = 'auto';
|
|
|
|
}
|
|
|
|
input.focus();
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
})();
|