fix colorpicker swatch glitches by monkeypatching match-highlighter

This commit is contained in:
tophf 2017-11-23 05:59:46 +03:00
parent a30ef3ed14
commit 06cda0b2c1
2 changed files with 49 additions and 10 deletions

View File

@ -2,6 +2,7 @@
'use strict'; 'use strict';
window.initColorpicker = () => { window.initColorpicker = () => {
initOverlayHooks();
onDOMready().then(() => { onDOMready().then(() => {
$('#colorpicker-settings').onclick = configureColorpicker; $('#colorpicker-settings').onclick = configureColorpicker;
}); });
@ -105,4 +106,51 @@ window.initColorpicker = () => {
} }
input.focus(); input.focus();
} }
function initOverlayHooks() {
const COLORVIEW_DISABLED_SUFFIX = ' colorview-disabled';
const originalAddOverlay = CodeMirror.prototype.addOverlay;
CodeMirror.prototype.addOverlay = addOverlayHook;
function addOverlayHook(overlay) {
if (overlay === (this.state.matchHighlighter || {}).overlay) {
if (overlay.token !== tokenHook) {
overlay.stylusColorpickerHelper = {
token: overlay.token,
};
overlay.token = tokenHook;
}
}
originalAddOverlay.apply(this, arguments);
}
function tokenHook(stream) {
const style = this.stylusColorpickerHelper.token.call(this, stream);
if (style === 'matchhighlight') {
return tokenHookForHighlighter.call(this, stream, style);
} else {
return style;
}
}
function tokenHookForHighlighter(stream, style) {
const {start, pos, lineOracle: {baseTokens}} = stream;
if (!baseTokens) {
return style;
}
for (let prev = 0, i = 1; i < baseTokens.length; i += 2) {
const end = baseTokens[i];
if (prev < start && start < end) {
const base = baseTokens[i + 1];
if (base && base.includes('colorview')) {
return style + COLORVIEW_DISABLED_SUFFIX;
}
} else if (end > pos) {
break;
}
prev = end;
}
return style;
}
}
}; };

View File

@ -5,7 +5,7 @@
white-space: nowrap; white-space: nowrap;
} }
.cm-colorview::before { .cm-colorview:not(.cm-colorview-disabled)::before {
content: ""; content: "";
position: relative; position: relative;
display: inline-block; display: inline-block;
@ -17,15 +17,6 @@
background-repeat: repeat; background-repeat: repeat;
} }
/* overlay at the start splits our colorview: don't style the tail */
.cm-colorview.cm-overlay + .cm-colorview:not([data-colorpicker])::before,
/* overlay in the middle splits our colorview: don't style the middle */
.cm-colorview + .cm-colorview.cm-overlay:not([data-colorpicker])::before,
/* ...and the tail */
.cm-colorview + .cm-colorview.cm-overlay:not([data-colorpicker]) + .cm-colorview:not([data-colorpicker])::before {
content: none;
}
.codemirror-colorview-background { .codemirror-colorview-background {
position: absolute; position: absolute;
left: 2px; left: 2px;