@@ -1013,181 +1013,3 @@ context</a> for a given |box|, |constraintSpace|, |children| and an optional |br
10131013
10141014 15. Return |fragment|.
10151015
1016- Examples {#examples}
1017- ====================
1018-
1019- Example 1: A simple block layout {#example-1}
1020- ---------------------------------------------
1021-
1022- <pre class='lang-javascript'>
1023- // Inside LayoutWorkletGlobalScope
1024-
1025- // Note this is meant to be similar (*not* the same) as a block layout.
1026- // Everything is done in 'width' & 'height' for easy reading.
1027- registerLayout('simple-flow' , class {
1028- static get inputProperties() { return ['width', 'height'] }
1029- static get childrenInputProperties() { return ['x', 'y', 'position'] }
1030-
1031- layout(children, constraintSpace, styleMap, breakToken) {
1032- const absoluteChildren = [];
1033- const fixedChildren = [];
1034- const fragments = [];
1035-
1036- // Resolve our width using the available width in 'constraintSpace' , and
1037- // our computed width property.
1038- let width = resolveWidth(constraintSpace, styleMap.get('width' ));
1039-
1040- // Create a new constraint space for our children to consume.
1041- let childConstraintSpace = new ConstraintSpace(constraintSpace);
1042- childConstraintSpace.width = width;
1043-
1044- // Track the used height, min and max content.
1045- let height = 0;
1046- let minContent = 0;
1047- let maxContent = 0;
1048-
1049- for (let child of children) {
1050- // Check if the child is out of flow positioned.
1051- const childPosition = child.styleMap.get('position' );
1052-
1053- if (childPosition == 'absolute' ) {
1054- absoluteChildren.push(child);
1055- continue;
1056- }
1057-
1058- if (childPosition == 'fixed' ) {
1059- fixedChildren.push(child);
1060- continue;
1061- }
1062-
1063- // Layout the in flow child.
1064- const childFragment = child.doLayout(childConstraintSpace);
1065-
1066- // Position the child.
1067- childFragment.x = 0;
1068- childFragment.y = height;
1069-
1070- // Update our current height, min and max content.
1071- height += childFragment.height;
1072- minContent = Math.max(childFragment.minContent, minContent);
1073- maxContent = Math.max(childFragment.maxContent, maxContent);
1074- }
1075-
1076- // Resolve the height.
1077- height = resolveHeight(constraintSpace, styleMap.get('height' ), height);
1078-
1079- return {
1080- minContent: minContent,
1081- maxContent: maxContent,
1082- width: width,
1083- height: height,
1084- fragments: fragments,
1085- unPositionedChildren: absoluteChildren.concat(fixedChildren),
1086- breakToken: null
1087- };
1088- }
1089- });
1090- </pre>
1091-
1092- <pre class='lang-markup'>
1093- <div id="myElement">
1094- <div>
1095- CSS is awesome.
1096- </div>
1097- </div>
1098-
1099- <style>
1100- #myElement {
1101- display: layout('simple-flow' );
1102- }
1103- </style>
1104- </pre>
1105-
1106- Example 2: A simple line layout {#example-2}
1107- --------------------------------------------
1108-
1109- <pre class='lang-javascript'>
1110- // Inside LayoutWorkletGlobalScope
1111-
1112- // Note this is meant to be similar (*not* the same) as a inline layout.
1113- // Everything is done in 'width' & 'height' for easy reading.
1114- registerLayout('simple-inline-flow' , class {
1115- static get inputProperties() { return ['width', 'height'] }
1116- static get childrenInputProperties() { return [] }
1117-
1118- layout(children, constraintSpace, styleMap, breakToken) {
1119- // Resolve our width using the available width in 'constraintSpace' , and
1120- // our computed width property.
1121- const width = resolveWidth(constraintSpace, styleMap.get('width' ));
1122- const fragments = [];
1123- let height = 0;
1124-
1125- // TODO compute these.
1126- let minContent = 0;
1127- let maxContent = 0;
1128-
1129- let childFragment = null;
1130- let lineFragments = [];
1131- let lineHeight = 0;
1132- let remainingLineWidth = width; // NOTE: should be helper on constraint space?
1133-
1134- const childIter = chidlren.values();
1135- let child = childIter.next().value;
1136- let breakToken = null;
1137-
1138- while (child) {
1139- // Create a new constraint space for the child, with all the current
1140- // positioned children.
1141- const childConstraintSpace = new ConstraintSpace(constraintSpace);
1142- childConstraintSpace.addExclusion(new ExclusionRect(width, height, 0, 0));
1143- childConstraintSpace.addExclusions(lineFragments);
1144-
1145- // Perform layout on the child.
1146- childFragment = child.doLayout(childConstraintSpace, breakToken);
1147- fragments.push(childFragment);
1148-
1149- // Check if we need to position the fragment on the next line.
1150- if (childFragment.width > remainingLineWidth) {
1151- // Need to start a new line.
1152- lineFragments = [];
1153- height += lineHeight;
1154- lineHeight = 0;
1155- remainingLineWidth = width;
1156- }
1157-
1158- // Position the fragment horizontally.
1159- childFragment.x = width - remainingLineWidth;
1160-
1161- lineFragments.push(childFragment);
1162- lineHeight = Math.max(lineHeight, childFragment.height);
1163- remainingLineWidth -= childFragment.width;
1164-
1165- // Update the line fragments positions, based on the new lineHeight.
1166- for (let frag of lineFragments) {
1167- frag.y = lineHeight - frag.height;
1168- }
1169-
1170- // Step to the next child if required.
1171- if (childFragment.breakToken) {
1172- breakToken = childFragment.breakToken;
1173- } else {
1174- child = childIter.next().value;
1175- breakToken = null;
1176- }
1177- }
1178-
1179- // Resolve the height.
1180- height = resolveHeight(constraintSpace, styleMap.get('height' ), height);
1181-
1182- return {
1183- minContent: minContent,
1184- maxContent: maxContent,
1185- width: width,
1186- height: height,
1187- fragments: fragments,
1188- unpositionedFragments: [],
1189- breakToken: null
1190- };
1191- }
1192- });
1193- </pre>
0 commit comments