Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

[WIP] color : parse and render odoo theme class into css style in the VDOM #469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/plugin-odoo/src/Odoo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { OdooTableDomObjectRenderer } from './OdooTableDomObjectRenderer';
import { FontAwesomeNode } from '../../plugin-fontawesome/src/FontAwesomeNode';
import { Core } from '../../core/src/Core';
import { LinkFormatDomObjectModifierRenderer } from './LinkFormatDomObjectModifierRenderer';
import { OdooThemeColorDomObjectModifierRenderer } from './OdooThemeColorDomObjectModifierRenderer';
import { OdooXmlDomParsingEngine } from './OdooXmlDomParsingEngine';

export enum OdooPaddingClasses {
NONE = 'padding-none',
Expand Down Expand Up @@ -182,8 +184,10 @@ export class Odoo<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T>
OdooStructureXmlDomParser,
OdooTranslationXmlDomParser,
OdooParallaxSpanXmlDomParser,
OdooXmlDomParsingEngine,
],
renderers: [
OdooThemeColorDomObjectModifierRenderer,
OdooImageDomObjectRenderer,
OdooFontAwesomeDomObjectRenderer,
OdooTableDomObjectRenderer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 '';
}
}
22 changes: 22 additions & 0 deletions packages/plugin-odoo/src/OdooXmlDomParsingEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { XmlDomParsingEngine } from '../../plugin-xml/src/XmlDomParsingEngine';
import { ParsingIdentifier } from '../../plugin-parser/src/ParsingEngine';
import { DefaultXmlDomParser } from '../../plugin-xml/src/DefaultXmlDomParser';
import { Attributes } from '../../plugin-xml/src/Attributes';

export class OdooXmlDomParsingEngine extends XmlDomParsingEngine {
static readonly id: ParsingIdentifier = 'dom/xml';
static readonly defaultParser = DefaultXmlDomParser;
/**
* Parse attributes and trasform odoo color classes to color style
* so JW can treat them as normal colors
*
* @param node
*/
parseAttributes(node: Element): Attributes {
const attributes = super.parseAttributes(node);

console.log('parse attribute ici');

return attributes;
}
}
5 changes: 5 additions & 0 deletions packages/plugin-xml/src/Attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class Attributes extends Modifier {
this.set(key, attributes[key]);
}
}
if (attributes && (this.style.length || this.classList.length)) {
console.log('-------\nnew attributes');
if (this.style.length) console.log(this.style.keys(), this.style.values());
if (this.classList.length) console.log('class : ' + this.classList.className);
}
}

//--------------------------------------------------------------------------
Expand Down