use helper's decisions in colorview renderer
This commit is contained in:
parent
50d3dbe394
commit
5aba0377a0
|
@ -3,8 +3,14 @@
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
const OWN_TOKEN_NAME = 'colorview';
|
const OWN_TOKEN_NAME = 'colorview';
|
||||||
const OWN_DOM_CLASS = 'cm-' + OWN_TOKEN_NAME;
|
const OWN_TOKEN_CLASS = 'cm-' + OWN_TOKEN_NAME;
|
||||||
const OWN_BACKGROUND_CLASS = 'codemirror-colorview-background';
|
const OWN_BACKGROUND_CLASS = 'codemirror-colorview-background';
|
||||||
|
|
||||||
|
const DISABLED_TOKEN_NAME = 'colorview-disabled';
|
||||||
|
const DISABLED_NEXT_TOKEN_NAME = 'colorview-next-disabled';
|
||||||
|
const DISABLED_TOKEN_CLASS = 'cm-' + DISABLED_TOKEN_NAME;
|
||||||
|
const DISABLED_NEXT_TOKEN_CLASS = 'cm-' + DISABLED_NEXT_TOKEN_NAME;
|
||||||
|
|
||||||
const HOOKED_TOKEN = new Map([
|
const HOOKED_TOKEN = new Map([
|
||||||
['atom', colorizeAtom],
|
['atom', colorizeAtom],
|
||||||
['keyword', colorizeKeyword],
|
['keyword', colorizeKeyword],
|
||||||
|
@ -152,44 +158,53 @@
|
||||||
}
|
}
|
||||||
let lineCacheAlive = false;
|
let lineCacheAlive = false;
|
||||||
let elementIndex = 0;
|
let elementIndex = 0;
|
||||||
let elements;
|
let elements, el, token;
|
||||||
|
|
||||||
for (let i = 1; i < styles.length; i += 2) {
|
for (let i = 1; i < styles.length; i += 2) {
|
||||||
const token = styles[i + 1];
|
if (token && token.includes(DISABLED_NEXT_TOKEN_NAME)) {
|
||||||
|
token = styles[i + 1];
|
||||||
|
elementIndex++;
|
||||||
|
i += 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
token = styles[i + 1];
|
||||||
if (!token || !token.includes(OWN_TOKEN_NAME)) {
|
if (!token || !token.includes(OWN_TOKEN_NAME)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (token.includes(DISABLED_TOKEN_NAME)) {
|
||||||
|
elementIndex++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const start = styles[i - 2] || 0;
|
const start = styles[i - 2] || 0;
|
||||||
const data = lineCache.get(start);
|
const data = lineCache.get(start);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
elements = elements || text.getElementsByClassName(OWN_DOM_CLASS);
|
lineCacheAlive = true;
|
||||||
const el = elements[elementIndex++];
|
|
||||||
while (true) {
|
!elements && (elements = text.getElementsByClassName(OWN_TOKEN_CLASS));
|
||||||
const nextStyle = styles[i + 3];
|
do {
|
||||||
const nextStart = styles[i];
|
el = elements[elementIndex];
|
||||||
if (nextStyle && nextStyle.includes(OWN_TOKEN_NAME) &&
|
elementIndex += el && el.classList.contains(DISABLED_NEXT_TOKEN_CLASS) ? 2 : 1;
|
||||||
nextStart > start && nextStart <= start + data.color.length) {
|
} while (el && el.classList.contains(DISABLED_TOKEN_CLASS));
|
||||||
elementIndex++;
|
|
||||||
i += 2;
|
if (!el || (el.colorpickerData || {}).color === data.color) {
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (el.colorpickerData && el.colorpickerData.color === data.color) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////// yay we finally found something to render
|
||||||
el.colorpickerData = Object.assign({line, ch: start}, data);
|
el.colorpickerData = Object.assign({line, ch: start}, data);
|
||||||
let bg = el.firstElementChild;
|
let bg = el.firstElementChild;
|
||||||
if (!bg) {
|
if (!bg) {
|
||||||
bg = document.createElement('div');
|
bg = document.createElement('div');
|
||||||
bg.className = OWN_BACKGROUND_CLASS;
|
bg.className = OWN_BACKGROUND_CLASS;
|
||||||
bg.title = options.tooltip;
|
bg.title = options.tooltip;
|
||||||
el.appendChild(bg);
|
|
||||||
}
|
}
|
||||||
bg.style.setProperty('background-color', data.color, 'important');
|
bg.style.setProperty('background-color', data.color, 'important');
|
||||||
lineCacheAlive = true;
|
!bg.parentNode && el.appendChild(bg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lineCacheAlive) {
|
if (lineCacheAlive) {
|
||||||
lineCache.set('lastAccessTime', performance.now());
|
lineCache.set('lastAccessTime', performance.now());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user