Skip to content

Commit a5c6e32

Browse files
committed
[css-layout-api] update lines example
1 parent 22cd40d commit a5c6e32

File tree

1 file changed

+32
-56
lines changed

1 file changed

+32
-56
lines changed

css-layout-api/Overview.bs

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ enum BreakType { "none", "line", "column", "page", "region" };
520520

521521
A {{LayoutChild}} can produce multiple {{Fragment}}s. A {{LayoutChild}} may fragment in the block
522522
direction if a {{ConstraintSpace/blockFragmentationType}} is not none. Additionally {{LayoutChild}}
523-
which represents <a>inline-level</a> content, may fragment line by line if the <a for="layout
524-
definition">child display</a> (set by <code>childDisplay</code>) is <code>"normal"</code>.
523+
which represents <a>inline-level</a> content, may fragment line by line if the <a for="document
524+
layout definition">child display</a> (set by <code>childDisplay</code>) is <code>"normal"</code>.
525525

526526
A subsequent {{Fragment}} is produced by using the previous {{Fragment}}'s {{Fragment/breakToken}}.
527527
This tells the <a>child layout</a> to produce a {{Fragment}} starting at the point encoded in the
@@ -530,7 +530,8 @@ This tells the <a>child layout</a> to produce a {{Fragment}} starting at the poi
530530
Issue: Explain resuming the author defined layout.
531531

532532
<div class="example">
533-
This example shows a simple inline layout which places child fragments in the inline direction.
533+
This example shows a simple layout which places indents child fragments for a certain number of
534+
lines.
534535

535536
This example also demonstrates using the previous {{Fragment/breakToken}} of a {{Fragment}} to
536537
produce the next fragment for the {{LayoutChild}}.
@@ -541,8 +542,9 @@ It returns a {{FragmentResultOptions}} with a {{FragmentResultOptions/breakToken
541542
resume the layout.
542543

543544
<pre class="lang-javascript">
544-
registerLayout('basic-inline', class {
545+
registerLayout('indent-lines', class {
545546
static childDisplay = 'normal';
547+
static inputProperties = ['--indent', '--indent-lines'];
546548

547549
*layout(space, children, styleMap, edges, breakToken) {
548550
// Resolve our inline size.
@@ -553,22 +555,11 @@ registerLayout('basic-inline', class {
553555
const availableBlockSize =
554556
resolveBlockSize(space, styleMap) - edges.all.block;
555557

556-
const childFragments = [];
557-
558-
let currentLine = [];
559-
let usedInlineSize = 0;
560-
561-
let lineOffset = 0;
562-
let maxLineBlockSize = 0;
558+
// Detrermine the number of lines to indent, and the indent amount.
559+
const indent = resolveLength(space, styleMap.get('--indent'));
560+
let lines = styleMap.get('--indent-lines').value;
563561

564-
// Just a small little function which will update the above variables.
565-
const nextLine = function() {
566-
currentLine = [];
567-
usedInlineSize = 0;
568-
569-
lineOffset += maxLineBlockSize;
570-
maxLineBlockSize = 0;
571-
}
562+
const childFragments = [];
572563

573564
let childBreakToken = null;
574565
if (breakToken) {
@@ -578,51 +569,36 @@ registerLayout('basic-inline', class {
578569
children.splice(0, children.indexOf(childBreakToken.child));
579570
}
580571

572+
let blockOffset = edges.all.blockStart;
581573
let child = children.shift();
582574
while (child) {
583-
// Make sure we actually have space on the current line.
584-
if (usedInlineSize > availableInlineSize) {
585-
nextLine();
586-
}
575+
const shouldIndent = lines-- > 0;
576+
577+
// Adjust the inline size for the indent.
578+
const childAvailableInlineSize = shouldIndent ?
579+
availableInlineSize - indent : availableInlineSize;
587580

588-
// The constraint space here will have the inline size of the
589-
// remaining space on the line.
590-
const remainingInlineSize = availableInlineSize - usedInlineSize;
591-
const constraintSpace = new ConstraintSpace({
592-
inlineSize: availableInlineSize - usedInlineSize,
581+
const childSpace = new ConstraintSpace({
582+
inlineSize: childAvailableInlineSize,
593583
blockSize: availableBlockSize,
594584
percentageInlineSize: availableInlineSize,
585+
blockFragmentationType: space.blockFragmentationType,
595586
});
596587

597-
const fragment = yield child.layoutNextFragment(constraintSpace,
588+
const fragment = yield child.layoutNextFragment(childSpace,
598589
childBreakToken);
599590
childFragments.push(fragment);
600591

601-
// Check if there is still space on the current line.
602-
if (fragment.inlineSize > remainingInlineSize) {
603-
nextLine();
604-
605-
// Check if we have gone over the block fragmentation limit.
606-
if (constraintSpace.blockFragmentationType != 'none' &&
607-
lineOffset > constraintSpace.blockSize) {
608-
break;
609-
}
610-
}
611-
612-
// Insert fragment on the current line.
613-
currentLine.push(fragment);
614-
fragment.inlineOffset = usedInlineSize;
615-
616-
// Go through each of the fragments on the line and update their
617-
// block offsets.
618-
for (let fragmentOnLine of currentLine) {
619-
fragmentOnLine.blockOffset = lineOffset;
592+
// Position the fragment.
593+
fragment.inlineOffset = shouldIndent ?
594+
edges.all.inlineStart + indent : edges.all.inlineStart;
595+
fragment.blockOffset = blockOffset;
596+
blockOffset += fragment.blockSize;
620597

621-
const lineBlockSize =
622-
fragmentOnLine.blockOffset + fragmentOnLine.blockSize;
623-
if (maxLineBlockSize < lineBlockSize) {
624-
maxLineBlockSize = lineBlockSize;
625-
}
598+
// Check if we have gone over the block fragmentation limit.
599+
if (constraintSpace.blockFragmentationType != 'none' &&
600+
blockOffset > constraintSpace.blockSize) {
601+
break;
626602
}
627603

628604
if (fragment.breakToken) {
@@ -635,9 +611,7 @@ registerLayout('basic-inline', class {
635611
}
636612
}
637613

638-
// Determine our block size.
639-
nextLine();
640-
const contentSize = lineOffset + edges.all.block;
614+
const contentSize = blockOffset + edges.all.blockEnd;
641615
const blockSize = resolveBlockSize(space,
642616
styleMap,
643617
contentSize);
@@ -753,6 +727,8 @@ partial interface LayoutWorkletGlobalScope {
753727
double resolveBlockSize(ConstraintSpace constraintSpace,
754728
StylePropertyMapReadOnly styleMap,
755729
optional double contentSize);
730+
731+
double resolveLength(ConstraintSpace constraintSpace, CSSStyleValue value);
756732
};
757733
</pre>
758734

0 commit comments

Comments
 (0)