Skip to content

Commit 084c3ec

Browse files
committed
[css-layout-api] Create user-agent run loop example in README.md
1 parent 46ce4a1 commit 084c3ec

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

css-layout-api/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ With the CSS Layout API, authors could write their own layouts which implement
1212
- Masonary layouts
1313
- Line spacing + snapping
1414

15+
This document aims to give a high level overview to the Layout API.
16+
1517
### Concepts
1618

1719
##### The `Box`
@@ -96,7 +98,16 @@ registerLayout('really-basic-block', class {
9698

9799
for (let child of children) {
98100
let fragment = yield child.doLayout(constraintSpace);
101+
102+
// Position the new fragment.
103+
fragment.inlineStart = 0;
104+
fragment.blockStart = blockSize;
99105
blockSize += fragment.blockSize;
106+
107+
// Add it as an exclusion to the constraintSpace
108+
constraintSpace.addExclusion(fragment, 'block-end');
109+
110+
// Update the running totals for our size.
100111
inlineSize = Math.max(inlineSize, fragment.inlineSize);
101112
childFragments.push(fragment);
102113
}
@@ -111,5 +122,43 @@ registerLayout('really-basic-block', class {
111122
```
112123

113124
The first thing to notice about the API is that the layout method on the class returns a generator.
125+
This is to allow two things:
126+
1. User agents implementing parallel layout.
127+
2. User agents implementing asynchronous layout.
128+
129+
A user agent could implement the logic driving the author defined layout as:
130+
131+
```js
132+
function performLayout(constraintSpace, box) {
133+
// Get the author defined layout instance.
134+
const layoutInstance = getLayoutInstanceForBox(box);
135+
136+
// Access the generator returned by *layout();
137+
const layoutGenerator = layoutInstance.layout(constraintSpace, box.children, box.styleMap);
138+
139+
// Loop through all of the fragment requests.
140+
let fragmentRequestObj = layoutGenerator.next();
141+
while (!fragmentRequestObj.done) {
142+
const fragmentRequest = [];
143+
const fragmentResult = [];
144+
145+
// Coorce fragmentRequestObj into an array.
146+
if (fragmentRequestObj.value.length) {
147+
fragmentRequest.push(...fragmentRequestObject.value);
148+
} else {
149+
fragmentRequest.push(fragmentRequestObject.value);
150+
}
151+
152+
// Request the next fragment.
153+
fragmentRequestObj = layoutGenerator.next(
154+
fragmentResult.length == 1 : fragmentResult[0] : fragmentResult);
155+
}
156+
157+
// The last value from the generator should be the final return value.
158+
const fragmentDict = fragmentRequest.value;
159+
return new Fragment(fragmentDict);
160+
}
161+
```
162+
114163

115164
TODO finish writing this.

0 commit comments

Comments
 (0)