same colorpicker in the manager

This commit is contained in:
tophf 2017-11-27 09:56:22 +03:00
parent a603cc081b
commit 2e03c9c9c0
5 changed files with 91 additions and 47 deletions

View File

@ -7,6 +7,7 @@
<link rel="stylesheet" href="manage/manage.css"> <link rel="stylesheet" href="manage/manage.css">
<link rel="stylesheet" href="msgbox/msgbox.css"> <link rel="stylesheet" href="msgbox/msgbox.css">
<link rel="stylesheet" href="options/onoffswitch.css"> <link rel="stylesheet" href="options/onoffswitch.css">
<link rel="stylesheet" href="vendor-overwrites/colorpicker/colorpicker.css">
<style id="style-overrides"></style> <style id="style-overrides"></style>
@ -149,7 +150,7 @@
<script src="manage/filters.js"></script> <script src="manage/filters.js"></script>
<script src="manage/updater-ui.js"></script> <script src="manage/updater-ui.js"></script>
<script src="manage/object-diff.js"></script> <script src="manage/object-diff.js"></script>
<script src="js/color-parser.js"></script> <script src="vendor-overwrites/colorpicker/colorpicker.js"></script>
<script src="manage/config-dialog.js"></script> <script src="manage/config-dialog.js"></script>
<script src="manage/manage.js"></script> <script src="manage/manage.js"></script>
</head> </head>

View File

@ -1,8 +1,9 @@
/* global colorParser messageBox makeLink */ /* global messageBox makeLink */
'use strict'; 'use strict';
function configDialog(style) { function configDialog(style) {
const form = buildConfigForm(); const form = buildConfigForm();
const colorpicker = window.colorpicker();
return messageBox({ return messageBox({
title: `${style.name} v${style.usercssData.version}`, title: `${style.name} v${style.usercssData.version}`,
@ -34,7 +35,7 @@ function configDialog(style) {
colorpicker.hide(); colorpicker.hide();
} }
if (button === 0 || enter) { if (button === 0 || enter) {
return form.getVars(); return form.getVars();
} }
}); });
@ -46,25 +47,14 @@ function configDialog(style) {
let appendChild; let appendChild;
switch (va.type) { switch (va.type) {
case 'color': case 'color':
va.inputColor = $element({tag: 'input', type: 'color'}); appendChild = [$element({
va.inputAlpha = $element({ className: 'cm-colorview',
tag: 'input', appendChild: va.inputColor = $element({
type: 'range', va,
min: 0, className: 'color-swatch',
max: 1, onclick: onColorClicked,
title: chrome.i18n.getMessage('alphaChannel'), })
step: 'any' })];
});
va.inputColor.onchange = va.inputAlpha.oninput = () => {
va.dirty = true;
const color = colorParser.parse(va.inputColor.value);
color.a = Number(va.inputAlpha.value);
va.value = colorParser.format(color);
va.inputColor.style.opacity = color.a;
};
appendChild = [
$element({appendChild: [va.inputColor, va.inputAlpha]})
];
break; break;
case 'checkbox': case 'checkbox':
@ -119,15 +109,10 @@ function configDialog(style) {
function drawValues() { function drawValues() {
for (const key of Object.keys(vars)) { for (const key of Object.keys(vars)) {
const va = vars[key]; const va = vars[key];
const value = va.value === null || va.value === undefined ? const useDefault = va.value === null || va.value === undefined;
va.default : va.value; const value = useDefault ? va.default : va.value;
if (va.type === 'color') { if (va.type === 'color') {
const color = colorParser.parse(value); va.inputColor.style.backgroundColor = value;
va.inputAlpha.value = color.a;
va.inputColor.style.opacity = color.a;
delete color.a;
va.inputColor.value = colorParser.formatHex(color);
} else if (va.type === 'checkbox') { } else if (va.type === 'checkbox') {
va.input.checked = Number(value); va.input.checked = Number(value);
} else { } else {
@ -149,6 +134,30 @@ function configDialog(style) {
return vars; return vars;
} }
function onColorClicked() {
window.removeEventListener('keydown', messageBox.listeners.key, true);
const box = $('#message-box-contents');
colorpicker.show({
color: this.va.value || this.va.default,
top: this.getBoundingClientRect().bottom - 5,
left: box.getBoundingClientRect().left - 360,
hideDelay: 1e6,
guessBrightness: box,
callback: newColor => {
if (newColor) {
this.va.dirty = true;
this.va.value = newColor;
this.style.backgroundColor = newColor;
}
setTimeout(() => {
if (!$('.colorpicker-popup')) {
window.addEventListener('keydown', messageBox.listeners.key, true);
}
});
},
});
}
return { return {
elements: labels, elements: labels,
useDefault, useDefault,

View File

@ -766,6 +766,29 @@ fieldset select {
margin-right: 4px; margin-right: 4px;
} }
.cm-colorview::before,
.color-swatch {
width: 60px !important;
height: 20px !important;
}
.cm-colorview::before {
margin: 1px !important;
}
.color-swatch {
position: absolute;
border: 1px solid gray;
margin-top: -22px;
cursor: pointer;
}
.colorpicker-popup {
z-index: 2147483647 !important;
border: none !important;
box-shadow: 3px 3px 50px rgba(0,0,0,.5) !important;
}
@keyframes fadein { @keyframes fadein {
from { from {
opacity: 0; opacity: 0;

View File

@ -1,7 +1,7 @@
/* global CodeMirror */ /* global CodeMirror */
'use strict'; 'use strict';
CodeMirror.defineExtension('colorpicker', function () { (window.CodeMirror ? window.CodeMirror.prototype : window).colorpicker = function () {
const cm = this; const cm = this;
const CSS_PREFIX = 'colorpicker-'; const CSS_PREFIX = 'colorpicker-';
const HUE_COLORS = [ const HUE_COLORS = [
@ -188,18 +188,6 @@ CodeMirror.defineExtension('colorpicker', function () {
if (!initialized) { if (!initialized) {
init(); init();
} }
$root.style = `
display: block;
position: fixed;
left: -10000px;
top: -10000px;
`.replace(/;/g, '!important;');
$root.classList.add(CSS_PREFIX + 'theme-' +
(opt.theme === 'dark' || opt.theme === 'light' ? opt.theme : guessTheme()));
document.body.appendChild($root);
shown = true;
HSV = {}; HSV = {};
currentFormat = ''; currentFormat = '';
options = PUBLIC_API.options = opt; options = PUBLIC_API.options = opt;
@ -209,8 +197,23 @@ CodeMirror.defineExtension('colorpicker', function () {
$formatChangeButton.title = opt.tooltipForSwitcher || ''; $formatChangeButton.title = opt.tooltipForSwitcher || '';
opt.hideDelay = Math.max(0, opt.hideDelay) || 2000; opt.hideDelay = Math.max(0, opt.hideDelay) || 2000;
if (!isNaN(options.left) && !isNaN(options.top)) {
$root.style = `
display: block;
position: fixed;
`.replace(/;/g, '!important;');
reposition();
}
$root.classList.add(CSS_PREFIX + 'theme-' +
(opt.theme === 'dark' || opt.theme === 'light' ?
opt.theme :
guessTheme()));
document.body.appendChild($root);
shown = true;
registerEvents(); registerEvents();
reposition();
setFromColor(opt.color); setFromColor(opt.color);
setFromHexLettercaseElement(); setFromHexLettercaseElement();
} }
@ -601,9 +604,9 @@ CodeMirror.defineExtension('colorpicker', function () {
switch (e.which) { switch (e.which) {
case 13: case 13:
setFromInputs({}); setFromInputs({});
colorpickerCallback();
// fallthrough to 27 // fallthrough to 27
case 27: case 27:
colorpickerCallback(e.which === 27 ? '' : undefined);
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
hide(); hide();
@ -622,6 +625,10 @@ CodeMirror.defineExtension('colorpicker', function () {
//region Event utilities //region Event utilities
function colorpickerCallback(colorString = currentColorToString()) { function colorpickerCallback(colorString = currentColorToString()) {
// Esc pressed?
if (!colorString) {
options.callback('');
}
if ( if (
userActivity && userActivity &&
$inputs[currentFormat].every(el => el.checkValidity()) && $inputs[currentFormat].every(el => el.checkValidity()) &&
@ -914,7 +921,8 @@ CodeMirror.defineExtension('colorpicker', function () {
function guessTheme() { function guessTheme() {
const realColor = {r: 255, g: 255, b: 255, a: 1}; const realColor = {r: 255, g: 255, b: 255, a: 1};
const start = ((cm.display.renderedView || [])[0] || {}).text || cm.display.lineDiv; const start = options.guessBrightness ||
((cm.display.renderedView || [])[0] || {}).text || cm.display.lineDiv;
for (let el = start; el; el = el.parentElement) { for (let el = start; el; el = el.parentElement) {
const bgColor = getComputedStyle(el).backgroundColor; const bgColor = getComputedStyle(el).backgroundColor;
const [r, g, b, a = 255] = bgColor.match(/\d+/g).map(Number); const [r, g, b, a = 255] = bgColor.match(/\d+/g).map(Number);
@ -953,4 +961,4 @@ CodeMirror.defineExtension('colorpicker', function () {
} }
//endregion //endregion
}); };

View File

@ -478,6 +478,9 @@
} }
static popupOnChange(newColor) { static popupOnChange(newColor) {
if (!newColor) {
return;
}
const {cm, line, ch, embedderCallback} = this; const {cm, line, ch, embedderCallback} = this;
const to = {line, ch: ch + this.prevColor.length}; const to = {line, ch: ch + this.prevColor.length};
if (cm.getRange(this, to) !== newColor) { if (cm.getRange(this, to) !== newColor) {