This repository was archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOdooThemeColorDomObjectModifierRenderer.ts
56 lines (51 loc) · 1.94 KB
/
OdooThemeColorDomObjectModifierRenderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
DomObjectRenderingEngine,
DomObject,
DomObjectElement,
DomObjectAttributes,
} from '../../plugin-renderer-dom-object/src/DomObjectRenderingEngine';
import { Attributes } from '../../plugin-xml/src/Attributes';
import { AttributesDomObjectModifierRenderer } from '../../plugin-xml/src/AttributesDomObjectModifierRenderer';
export class OdooThemeColorDomObjectModifierRenderer extends AttributesDomObjectModifierRenderer {
static odooThemeColor = /^var\(--.*\)$/g;
static id = DomObjectRenderingEngine.id;
engine: DomObjectRenderingEngine;
predicate = Attributes;
async render(modifier: Attributes, contents: DomObject[]): Promise<DomObject[]> {
contents = await super.render(modifier, contents);
for (const content of contents) {
const attributes = (content as DomObjectElement)?.attributes;
if (attributes) {
this._checkCssStyle(attributes, 'color', 'text-');
this._checkCssStyle(attributes, 'background-color', 'bg-');
}
}
return contents;
}
_checkCssStyle(
attributes: DomObjectAttributes,
styleKey: string,
odooClassPrefix: string,
): void {
if (!attributes.style) return;
const odooClassBaseValue = this._getOdooThemeColorClassForStyleColor(
attributes.style[styleKey],
);
if (odooClassBaseValue !== '') {
delete attributes.style[styleKey];
if (!attributes.class) {
attributes.class = new Set();
}
attributes.class.add(odooClassPrefix + odooClassBaseValue);
}
}
_getOdooThemeColorClassForStyleColor(styleValue: string): string {
if (
styleValue &&
styleValue.match(OdooThemeColorDomObjectModifierRenderer.odooThemeColor)
) {
return styleValue.substring(6, styleValue.length - 1);
}
return '';
}
}