@@ -12,6 +12,8 @@ With the CSS Layout API, authors could write their own layouts which implement
12
12
- Masonary layouts
13
13
- Line spacing + snapping
14
14
15
+ This document aims to give a high level overview to the Layout API.
16
+
15
17
### Concepts
16
18
17
19
##### The ` Box `
@@ -96,7 +98,16 @@ registerLayout('really-basic-block', class {
96
98
97
99
for (let child of children) {
98
100
let fragment = yield child .doLayout (constraintSpace);
101
+
102
+ // Position the new fragment.
103
+ fragment .inlineStart = 0 ;
104
+ fragment .blockStart = blockSize;
99
105
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.
100
111
inlineSize = Math .max (inlineSize, fragment .inlineSize );
101
112
childFragments .push (fragment);
102
113
}
@@ -111,5 +122,43 @@ registerLayout('really-basic-block', class {
111
122
```
112
123
113
124
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
+
114
163
115
164
TODO finish writing this.
0 commit comments