Skip to content

Commit 901032b

Browse files
committedAug 9, 2020
feat: Emit attribute groups in the runtime aggregate rewrite data.
1 parent 1808055 commit 901032b

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed
 

‎packages/@css-blocks/ember-app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"scripts": {
2020
"test": "yarn run test:runner",
21-
"test:runner": "echo TODO",
21+
"test:runner": "mocha --opts test/mocha.opts dist/test",
2222
"compile": "tsc --build && tsc --build runtime",
2323
"prepare": "tsc --build --force && tsc --build --force runtime",
2424
"pretest": "yarn run compile",

‎packages/@css-blocks/ember-app/src/AggregateRewriteData.ts

+13
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ export interface AggregateRewriteData {
7777
}
7878

7979
export interface BlockInfo {
80+
// Maps the source name of the attribute (E.g. `.button[color]`) to a
81+
// a map that maps the value of the attribute to the source name of that
82+
// attribute value. Example:
83+
// {
84+
// ":scope[mode]": {
85+
// "collapsed": ":scope[mode=collapsed]",
86+
// "open": ":scope[mode=open]",
87+
// "minimal": ":scope[mode=minimal]",
88+
// }
89+
// }
90+
// The value returned from this mapping is suitable for being passed as a key
91+
// to `blockInterfaceStyles`.
92+
groups: ObjectDictionary<ObjectDictionary<string>>;
8093
// The styles of this block
8194
// Note: this includes all the styles that are inherited but not overridden
8295
// but does not include the styles that are inherited but then overridden.

‎packages/@css-blocks/ember-app/src/RuntimeDataGenerator.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Block, Configuration, Style, isAttrValue, isBlockClass, isStyle } from "@css-blocks/core";
1+
import { AttrValue, Block, Configuration, Style, isAttrValue, isBlockClass, isStyle } from "@css-blocks/core";
22
import { EmberAnalyzer } from "@css-blocks/ember-utils";
33
import { SimpleAttribute, StyleMapping } from "@opticss/template-api";
44
import { ObjectDictionary } from "@opticss/util";
@@ -184,6 +184,21 @@ export class RuntimeDataGenerator {
184184
generateBlockInfo(block: Block): BlockInfo {
185185
let blockInterfaceStyles: ObjectDictionary<LocalStyleIndex> = {};
186186
let resolvedInterface = block.resolvedStyleInterface();
187+
let resolvedStyles = Object.values(resolvedInterface);
188+
let attributes: Array<[string, ObjectDictionary<string>]> = resolvedStyles.filter(s => isAttrValue(s)).map((s) => {
189+
let attrValue = <AttrValue>s;
190+
let groupName = attrValue.attribute.asSource();
191+
let attrName = attrValue.asSource();
192+
return [groupName, {[attrValue.value]: attrName}];
193+
});
194+
let groups: ObjectDictionary<ObjectDictionary<string>> = attributes.reduce(
195+
(v, [groupName, valueMap]) => {
196+
if (!v[groupName]) v[groupName] = {};
197+
Object.assign(v[groupName], valueMap);
198+
return v;
199+
},
200+
{} as ObjectDictionary<ObjectDictionary<string>>,
201+
);
187202
let styleNames = Object.keys(resolvedInterface).sort();
188203
let i = 0;
189204
for (let styleName of styleNames) {
@@ -196,6 +211,7 @@ export class RuntimeDataGenerator {
196211
implementations[this.blockIndex(impl)] = styleNames.map(n => this.styleIndex(implInterface[n]));
197212
}
198213
return {
214+
groups,
199215
blockInterfaceStyles,
200216
implementations,
201217
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Block, BlockFactory } from "@css-blocks/core";
2+
import { EmberAnalyzer } from "@css-blocks/ember-utils";
3+
import { StyleMapping } from "@opticss/template-api";
4+
import * as assert from "assert";
5+
6+
import { RuntimeDataGenerator } from "../src/RuntimeDataGenerator";
7+
8+
describe("Runtime Data Generator", function () {
9+
10+
beforeEach(async () => {
11+
});
12+
13+
afterEach(async () => {
14+
});
15+
16+
it("Generates block information", function () {
17+
let factory = new BlockFactory({});
18+
let analyzer = new EmberAnalyzer(factory);
19+
let mapping = new StyleMapping({analyzedAttributes: ["class"], analyzedTagnames: false, rewriteIdents: {id: false, class: true}});
20+
let block = new Block("test-block-1", "test-block-1", "abcde");
21+
let aClass = block.ensureClass("a-class");
22+
let attr = aClass.ensureAttribute("[an-attr]");
23+
attr.ensureValue("one");
24+
attr.ensureValue("two");
25+
attr.ensureValue("three");
26+
let subBlock = new Block("test-block-2", "test-block-2", "abcde");
27+
subBlock.setBase(block);
28+
let subClass = subBlock.ensureClass("a-class");
29+
let subAttr = subClass.ensureAttribute("[an-attr]");
30+
subAttr.ensureValue("three");
31+
subAttr.ensureValue("four");
32+
let generator = new RuntimeDataGenerator([block, subBlock], mapping, analyzer, factory.configuration, new Set());
33+
let blockInfo = generator.generateBlockInfo(block);
34+
assert.deepEqual(
35+
blockInfo,
36+
{
37+
blockInterfaceStyles: {
38+
".a-class": 0,
39+
".a-class[an-attr=one]": 1,
40+
".a-class[an-attr=three]": 2,
41+
".a-class[an-attr=two]": 3,
42+
":scope": 4,
43+
},
44+
groups: {
45+
".a-class[an-attr]": {
46+
one: ".a-class[an-attr=one]",
47+
two: ".a-class[an-attr=two]",
48+
three: ".a-class[an-attr=three]",
49+
},
50+
},
51+
implementations: {
52+
"0": [ 0, 1, 2, 3, 4 ],
53+
"1": [ 5, 1, 6, 3, 7 ],
54+
},
55+
},
56+
);
57+
});
58+
59+
});

0 commit comments

Comments
 (0)
Please sign in to comment.