UNPKG

nehan

Version:

Html layout engine for paged-media written in Typescript

62 lines 2.79 kB
import { Config, CssLoader, LayoutResult, BoxEnv, BlockNodeGenerator, FlowRootFormatContext, TableCellReducer, TableCellsReducer, CssUsedRegionLoader, } from './public-api'; export class TableCellsGenerator { constructor(context, reducer = TableCellsReducer.instance) { this.context = context; this.reducer = reducer; this.generator = this.createGenerator(); } getNext() { const next = this.generator.next(); return next.done ? undefined : next.value; } *createGenerator() { if (Config.debugLayout) { console.group(`table-cells: ${this.context.name}`); } const cellGenerators = this.context.elements.map(cellElement => { CssLoader.loadDynamic(cellElement, this); cellElement.acceptEffectorAll(CssUsedRegionLoader.instance); const env = new BoxEnv(cellElement); return new BlockNodeGenerator(new FlowRootFormatContext(env, this.context), TableCellReducer.instance); }); let loopCount = 0; while (true) { const values = cellGenerators.map(cellGen => cellGen.getNext()); if (values.every(value => value === undefined)) { break; } if (loopCount === 0 && values.some(value => value && value.type === "page-break")) { yield LayoutResult.pageBreak(this.context, "some table-cell has page-break, propagate to parent"); continue; } if (loopCount % 2 === 0) { const cellBlocks = values.map((value, index) => { if (value && value.type === "table-cell") { return value.body; } const emptyCellContext = cellGenerators[index].context; emptyCellContext.addBorderBoxEdge("start"); emptyCellContext.addBorderBoxEdge("end"); if (loopCount === 0) { emptyCellContext.addBorderBoxEdge("before"); } if (loopCount >= 2) { emptyCellContext.addBorderBoxEdge("after"); } return emptyCellContext.acceptLayoutReducer(TableCellReducer.instance).body; }); this.context.setCells(cellBlocks); } else { yield this.context.acceptLayoutReducer(this.reducer); yield LayoutResult.pageBreak(this.context, "table devided."); } loopCount++; } yield this.context.acceptLayoutReducer(this.reducer); if (Config.debugLayout) { console.groupEnd(); } } } //# sourceMappingURL=table-cells-generator.js.map