UNPKG

@syncfusion/ej2-documenteditor

Version:

Feature-rich document editor control with built-in support for context menu, options pane and dialogs.

1,049 lines 651 kB
/* eslint-disable */ import { isNullOrUndefined } from '@syncfusion/ej2-base'; import { Dictionary } from '../../base/dictionary'; import { CharacterRangeType } from '../../base/types'; import { HelperMethods, Point, WrapPosition } from '../editor/editor-helper'; import { WBorder, WBorders, WCharacterFormat, WParagraphFormat } from '../format/index'; import { WListLevel } from '../list/list-level'; import { BlockContainer, BlockWidget, BodyWidget, BookmarkElementBox, EditRangeEndElementBox, EditRangeStartElementBox, ElementBox, FieldElementBox, FieldTextElementBox, HeaderFooterWidget, ImageElementBox, LineWidget, ListTextElementBox, Margin, ParagraphWidget, Rect, TabElementBox, TableCellWidget, TableRowWidget, TableWidget, TextElementBox, Widget, CheckBoxFormField, DropDownFormField, ShapeElementBox, TextFrame, ContentControl, FootnoteElementBox, FootNoteWidget, ShapeBase, CommentCharacterElementBox } from './page'; import { PageLayoutViewer, WebLayoutViewer } from './viewer'; import { TextHelper } from './text-helper'; // Check box character is rendered smaller when compared to MS Word // So, mutiplied the font side by below factor to render check box character large. var CHECK_BOX_FACTOR = 1.4; /** * @private */ var Layout = /** @class */ (function () { function Layout(documentHelper) { /** * @private */ this.islayoutFootnote = false; /** * @private */ this.isMultiColumnDoc = false; /** * @private */ this.allowLayout = true; /** * @private */ this.isReplaceAll = false; /** * @private */ this.isTextFormat = false; /** * @private */ this.isReplacingAll = false; /** * @private */ this.footHeight = 0; /** * @private */ this.existFootnoteHeight = 0; /** * @private */ this.isfootMove = false; /** * @private */ this.footnoteHeight = 0; /** * @private */ this.isTableFootNote = false; /** * @private */ this.isRelayout = false; /** * @private */ this.isRelayoutneed = false; /** * @private */ this.isOverlapFloatTable = false; this.isInitialLoad = true; /** * @private */ this.isInsertFormField = false; this.fieldBegin = undefined; this.maxTextHeight = 0; this.maxBaseline = 0; this.maxTextBaseline = 0; this.isFieldCode = false; this.isRtlFieldCode = false; this.isRTLLayout = false; this.currentCell = undefined; this.isFootnoteContentChanged = false; this.isEndnoteContentChanged = false; this.keepWithNext = false; this.is2013Justification = false; this.nextElementToLayout = undefined; this.endNoteHeight = 0; this.isMultiColumnSplit = false; this.skipUpdateContainerWidget = false; this.isLayoutWhole = false; /** * @private */ this.isBidiReLayout = false; /** * @private */ this.defaultTabWidthPixel = 48; /** * @private */ this.isRelayoutFootnote = false; this.isRelayoutOverlap = false; this.isWrapText = false; this.isYPositionUpdated = false; this.isXPositionUpdated = false; this.hasFloatingElement = false; this.isFootNoteLayoutStart = false; this.wrapPosition = []; this.shiftedFloatingItemsFromTable = []; this.isDocumentContainsRtl = false; this.layoutedFootnoteElement = []; this.documentHelper = documentHelper; } Layout.prototype.isSameStyle = function (currentParagraph, isAfterSpacing) { var nextOrPrevSibling = undefined; if (isAfterSpacing) { if (currentParagraph.nextWidget instanceof ParagraphWidget) { nextOrPrevSibling = currentParagraph.nextWidget; } } else { if (currentParagraph.previousWidget instanceof ParagraphWidget) { nextOrPrevSibling = currentParagraph.previousWidget; } } if (isNullOrUndefined(nextOrPrevSibling)) { //Need to skip contextual spacing behavior when document is not Word 2013 and paragraph preserved inside the table cell with AllowSpaceOfSameStyleInTable compatiblity options. if (currentParagraph.paragraphFormat.contextualSpacing && (currentParagraph.isInsideTable ? (!this.documentHelper.allowSpaceOfSameStyleInTable || this.documentHelper.compatibilityMode === 'Word2013') : false)) { if (currentParagraph.index === 0) { nextOrPrevSibling = this.updateFirstParagraphSpacingBasedOnContextualSpacing(currentParagraph, isAfterSpacing); } else if (currentParagraph.index === currentParagraph.associatedCell.childWidgets.length - 1) { nextOrPrevSibling = this.updateLastParagraphSpacingBasedOnContextualSpacing(currentParagraph); if (nextOrPrevSibling === currentParagraph) { return true; } } } if (isNullOrUndefined(nextOrPrevSibling)) { return false; } } if (nextOrPrevSibling instanceof ParagraphWidget && currentParagraph.paragraphFormat.baseStyle === nextOrPrevSibling.paragraphFormat.baseStyle) { if (currentParagraph.paragraphFormat.listFormat.listId >= 0 && nextOrPrevSibling.paragraphFormat.listFormat.listId >= 0) { if (!currentParagraph.paragraphFormat.contextualSpacing) { if (isAfterSpacing && currentParagraph.paragraphFormat.spaceAfterAuto) { return true; } else if (!isAfterSpacing && currentParagraph.paragraphFormat.spaceBeforeAuto) { return true; } } } return currentParagraph.paragraphFormat.contextualSpacing; } return false; }; Layout.prototype.updateFirstParagraphSpacingBasedOnContextualSpacing = function (paragraph, isAfterSpacing) { var ownerCell = paragraph.associatedCell; var ownerRow = ownerCell.ownerRow; var ownerTable = ownerRow.ownerTable; var nextOrPrevSibling; if (isAfterSpacing) { nextOrPrevSibling = isNullOrUndefined(paragraph.nextRenderedWidget) ? (!isNullOrUndefined(ownerCell.nextRenderedWidget) ? ownerCell.nextRenderedWidget.firstChild : undefined) : paragraph.nextRenderedWidget; } else { nextOrPrevSibling = isNullOrUndefined(paragraph.previousRenderedWidget) ? (!isNullOrUndefined(ownerCell.previousRenderedWidget) ? ownerCell.previousRenderedWidget.firstChild : undefined) : paragraph.previousRenderedWidget; } if (ownerCell.index === 0 && paragraph.index === 0) { if (ownerRow.index === 0) { if (ownerTable.isInsideTable && ownerTable.index == 0) { nextOrPrevSibling = this.checkOwnerTablePrevItem(ownerTable, paragraph); } else { //If paragraph is preserved in first row first cell means, need to check owner table previous sibling. var ownerTablePrevSibling = ownerTable.previousRenderedWidget; return ownerTablePrevSibling; } } else { return nextOrPrevSibling; } } else if (paragraph.index === 0 && !isAfterSpacing) { //If para is first item in any cell excluding first cell, need to check previous cell last item. var prevCell = ownerRow.childWidgets[ownerCell.index - 1]; var prevCelllastItem = prevCell.childWidgets[prevCell.childWidgets.length - 1]; //if previous cell last item is table means skip before spacing value no need to check any paragraph styles. if (prevCelllastItem instanceof TableWidget && paragraph.paragraphFormat.baseStyle.name === "Normal" && paragraph.paragraphFormat.listFormat.listId < 0) { return paragraph; } } return nextOrPrevSibling; }; Layout.prototype.updateLastParagraphSpacingBasedOnContextualSpacing = function (paragraph) { var ownerCell = paragraph.associatedCell; var ownerRow = ownerCell.ownerRow; var nextCellFirstItem; if (ownerCell.index === ownerRow.childWidgets.length - 1 && paragraph.index === ownerCell.childWidgets.length - 1) { if (paragraph.paragraphFormat.baseStyle.name === "Normal" && paragraph.paragraphFormat.listFormat.listId < 0) { //If para preserved in last item in cell and cell is last cell in current row means its after spacing value not considered. return paragraph; } } else if (paragraph.index === ownerCell.childWidgets.length - 1) { //If current para is last item in current cell then need to check next cell first item. var nextCell = ownerRow.childWidgets[ownerCell.index + 1]; nextCellFirstItem = nextCell.firstChild; //If next cell first item is table then need to check inner table first para. //This is applicable for multiple nested table so when first item is table it try to get its first paragraph. while (nextCellFirstItem instanceof TableWidget) { nextCellFirstItem = nextCellFirstItem.childWidgets[0].childWidgets[0].childWidgets[0]; } } return nextCellFirstItem; }; Layout.prototype.checkOwnerTablePrevItem = function (ownerTable, paragraph) { var row = ownerTable.associatedCell.ownerRow; var prevSibling; if (row.index > 0) { if (paragraph.paragraphFormat.baseStyle.name === "Normal" && paragraph.paragraphFormat.listFormat.listId < 0) { return paragraph; } } else { if (row.ownerTable.isInsideTable && row.ownerTable.index === 0) { this.checkOwnerTablePrevItem(row.ownerTable, paragraph); } else { var prevSibling_1 = row.ownerTable.previousRenderedWidget; return prevSibling_1; } } return prevSibling; }; Object.defineProperty(Layout.prototype, "viewer", { get: function () { return this.documentHelper.owner.viewer; }, enumerable: true, configurable: true }); Layout.prototype.layout = function () { // Todo: Need to handle complete document layout(relayout). //const page: Page = this.documentHelper.pages[0]; //const body: BodyWidget = page.bodyWidgets[0]; }; /** * Releases un-managed and - optionally - managed resources. * * @returns {void} */ Layout.prototype.destroy = function () { this.documentHelper = undefined; this.value = undefined; this.allowLayout = undefined; this.isInitialLoad = undefined; this.fieldBegin = undefined; this.maxTextHeight = undefined; this.maxBaseline = undefined; this.maxTextBaseline = undefined; this.isFieldCode = undefined; this.footnoteHeight = undefined; this.isMultiColumnDoc = undefined; }; Layout.prototype.layoutItems = function (sections, isReLayout, isContinuousSection) { var _this = this; var page; var height = 0; var width = 0; for (var i = 0; i < sections.length; i++) { var section = sections[i]; if (section.sectionFormat.numberOfColumns > 1) { this.isMultiColumnDoc = true; } var nextSection = sections[i + 1]; this.viewer.columnLayoutArea.setColumns(section.sectionFormat); var lastpage = this.documentHelper.pages[this.documentHelper.pages.length - 1]; var bodyWidget = void 0; if (!isNullOrUndefined(lastpage) && !isNullOrUndefined(lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1]) && lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1].childWidgets.length === 0 && !isNullOrUndefined(lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1].previousSplitWidget)) { bodyWidget = lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1].previousSplitWidget; } /* eslint-disable-next-line max-len */ if (i > 0 && !isNullOrUndefined(bodyWidget) && !isNullOrUndefined(bodyWidget.lastChild) && !(bodyWidget.lastChild instanceof TableWidget) && ((this.documentHelper.compatibilityMode === 'Word2013' && bodyWidget.lastChild.isEndsWithPageBreak || bodyWidget.lastChild.isEndsWithColumnBreak)) && lastpage.bodyWidgets[0].childWidgets.length === 0) { var removedPages = this.documentHelper.pages.splice(this.documentHelper.pages.length - 1, 1); removedPages[0].destroy(); lastpage = this.documentHelper.pages[this.documentHelper.pages.length - 1]; } if ((i === 0 && !isContinuousSection) || (i !== 0 && (isNullOrUndefined(section.sectionFormat.breakCode) || section.sectionFormat.breakCode === 'NewPage' || height !== section.sectionFormat.pageHeight || width !== section.sectionFormat.pageWidth || (!isNullOrUndefined(lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1].lastChild) && lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1].lastChild.isEndsWithPageBreak)))) { page = this.viewer.createNewPage(section); } else { var clientY = this.documentHelper.viewer.clientActiveArea.y; var clientHeight = this.documentHelper.viewer.clientActiveArea.height; if (isContinuousSection) { var section_1 = this.getBodyWidget(lastpage.bodyWidgets[lastpage.bodyWidgets.length - 1], true); var height_1 = this.getNextWidgetHeight(section_1); this.viewer.updateClientArea(section_1, section_1.page); clientHeight = this.viewer.clientActiveArea.height - (height_1 - this.viewer.clientActiveArea.y); clientY = height_1; isContinuousSection = false; } //if (i - 1 > 0) { page = lastpage; //} page.bodyWidgets.push(section); page.bodyWidgets[page.bodyWidgets.length - 1].page = page; this.documentHelper.viewer.updateClientArea(section, page); this.documentHelper.viewer.clientActiveArea.y = clientY; this.documentHelper.viewer.clientActiveArea.height = clientHeight; } height = section.sectionFormat.pageHeight; width = section.sectionFormat.pageWidth; this.addBodyWidget(this.viewer.clientActiveArea, section); if (this.documentHelper.pages.length > 1) { var pageIndex = 0; for (var i_1 = 0; i_1 < this.documentHelper.pages.length; i_1++) { var prevPage = this.documentHelper.pages[i_1]; var prevSectionIndex = prevPage.sectionIndex; var index = section.index; if (prevSectionIndex > index || prevPage === page) { break; } pageIndex++; } if (pageIndex < this.documentHelper.pages.length - 1) { this.documentHelper.insertPage(pageIndex, page); } } this.layoutSection(section, 0, nextSection); } if (!isReLayout) { this.layoutComments(this.documentHelper.comments); } this.updateFieldElements(); if (this.documentHelper.owner.layoutType === 'Pages') { this.layoutEndNoteElement(); } /* tslint:disable:align */ setTimeout(function () { if (_this.documentHelper) { _this.documentHelper.isScrollHandler = true; // if (this.documentHelper.owner.isSpellCheck && this.documentHelper.owner.spellChecker.enableOptimizedSpellCheck) { // this.documentHelper.triggerElementsOnLoading = true; // } _this.documentHelper.clearContent(); _this.viewer.updateScrollBars(); _this.documentHelper.isScrollHandler = false; _this.isInitialLoad = false; } }, 50); }; /** * @private */ Layout.prototype.layoutComments = function (comments) { if (!isNullOrUndefined(comments)) { this.viewer.owner.commentReviewPane.layoutComments(); } }; Layout.prototype.layoutSection = function (section, index, nextSection) { var block = section.firstChild; var nextBlock; var prevBlock; do { if (block instanceof TableWidget && block.tableFormat.preferredWidthType === 'Auto' && !block.tableFormat.allowAutoFit) { block.calculateGrid(); } this.viewer.updateClientAreaForBlock(block, true, undefined, true); var bodyIndex = block.containerWidget.indexInOwner; nextBlock = this.layoutBlock(block, index); index = 0; this.viewer.updateClientAreaForBlock(block, false); prevBlock = block; block = nextBlock; } while (block); block = section.firstChild; if (this.viewer instanceof PageLayoutViewer && section.sectionFormat.numberOfColumns > 1 && !isNullOrUndefined(nextSection) && nextSection.sectionFormat.breakCode === 'NoBreak' && (section.sectionFormat.breakCode === 'NoBreak' || (section.sectionIndex === section.page.bodyWidgets[0].sectionIndex))) { if (this.getColumnBreak(section)) { var splittedSection = section.getSplitWidgets(); var bodyWidget = splittedSection[splittedSection.length - 1]; if (!isNullOrUndefined(section.page.nextPage)) { this.splitBodyWidgetBasedOnColumn(bodyWidget); } else { var firstBody = this.getBodyWidget(bodyWidget, true); this.viewer.updateClientArea(firstBody, firstBody.page); var height = this.getNextWidgetHeight(firstBody); this.viewer.clientActiveArea.height -= height - this.viewer.clientActiveArea.y; this.viewer.clientActiveArea.y = height; } } else { if (!isNullOrUndefined(section.page.nextPage)) { section = this.documentHelper.pages[this.documentHelper.pages.length - 1].bodyWidgets[0]; } this.splitBodyWidgetBasedOnColumn(section); } } var page; if (block && block.bodyWidget && block.bodyWidget.page) { page = block.bodyWidget.page; } while (page) { if (page.footnoteWidget) { this.layoutfootNote(page.footnoteWidget); page = page.nextPage; } else { page = page.nextPage; } } page = undefined; block = undefined; }; Layout.prototype.splitBodyWidgetBasedOnColumn = function (section) { section = this.getBodyWidget(section, true); var firstSection = section; this.isMultiColumnSplit = true; if (!this.isInitialLoad && section.sectionFormat.equalWidth) { var previousStartIndex = this.documentHelper.selection.startOffset; var previousEndIndex = this.documentHelper.selection.endOffset; this.combineMultiColumn(section); this.layoutMultiColumnBody(section, false); if (previousStartIndex !== this.documentHelper.selection.startOffset) { this.documentHelper.selection.select(previousStartIndex, previousEndIndex); } } this.combineMultiColumn(section); var lineCountInfo = this.getCountOrLine(section, undefined, undefined, true); var totalHeight = lineCountInfo.lineCount; var lineToBeSplit = Math.round(totalHeight / section.sectionFormat.numberOfColumns); while (section) { var lineCountInfo_1 = this.getCountOrLine(section, lineToBeSplit, true, false); var line = lineCountInfo_1.lineWidget; var lineIndexInCell = lineCountInfo_1.lineCount; if (!isNullOrUndefined(line)) { if (line.paragraph.containerWidget instanceof BodyWidget) { this.moveToNextLine(line, true, line.indexInOwner); } else if (line.paragraph.containerWidget instanceof TableCellWidget) { var table = [line.paragraph.containerWidget.ownerTable]; var rows = [line.paragraph.containerWidget.ownerRow]; var index = line.paragraph.containerWidget.index; if (table[table.length - 1].isInsideTable) { table[table.length - 1] = this.getParentTable(table[table.length - 1]); rows[rows.length - 1] = this.getParentRow(rows[rows.length - 1]); } this.updateWidgetsToTable(table, rows, rows[rows.length - 1], false, lineIndexInCell, index, true); var tableWidget = table[table.length - 1]; var rowWidget = rows[rows.length - 1]; var nextRow = rowWidget.nextRenderedWidget; while (nextRow) { this.clearRowWidget(nextRow, true, true, false); nextRow = this.layoutRow(table, nextRow); nextRow = nextRow.nextRenderedWidget; } if (!isNullOrUndefined(tableWidget.nextRenderedWidget) && section.sectionFormat.equalWidth) { this.documentHelper.blockToShift = tableWidget.nextRenderedWidget; this.documentHelper.layout.shiftLayoutedItems(false); } } var firstBody = this.getBodyWidget(line.paragraph.bodyWidget, true); var lastBody = this.getBodyWidget(firstBody, false); if (!firstBody.sectionFormat.equalWidth && lastBody.sectionFormat.numberOfColumns - 1 === lastBody.columnIndex) { var nonEqualBody = firstBody; var initialCount = (this.getCountOrLine(firstBody)).lineCount; this.layoutMultiColumnBody(nonEqualBody, true); var finalCount = (this.getCountOrLine(firstBody)).lineCount; if (initialCount !== finalCount) { this.splitBodyWidgetBasedOnColumn(firstBody); } } this.viewer.updateClientArea(firstBody, firstBody.page); var height = this.getNextWidgetHeight(firstBody); this.viewer.clientActiveArea.height -= height - this.viewer.clientActiveArea.y; this.viewer.clientActiveArea.y = height; this.viewer.clientArea.y = this.viewer.clientActiveArea.y; this.viewer.clientArea.height = this.viewer.clientActiveArea.height; } section = section.nextRenderedWidget; if (!isNullOrUndefined(section) && section.columnIndex === section.sectionFormat.numberOfColumns - 1) { break; } } this.isMultiColumnSplit = false; if (!this.isInitialLoad) { section = this.getBodyWidget(firstSection, false); if (!isNullOrUndefined(section.nextRenderedWidget)) { this.documentHelper.blockToShift = section.nextRenderedWidget.firstChild; } } }; /** * @private */ Layout.prototype.getColumnBreak = function (section) { var firstBody = this.getBodyWidget(section, true); if (firstBody.sectionFormat.numberOfColumns <= 1) { return false; } while (firstBody) { if (firstBody.lastChild instanceof ParagraphWidget && firstBody.lastChild.isEndsWithColumnBreak) { return true; } if (isNullOrUndefined(firstBody.nextRenderedWidget) || firstBody.index !== firstBody.nextRenderedWidget.index) { break; } firstBody = firstBody.nextRenderedWidget; } return false; }; Layout.prototype.layoutMultiColumnBody = function (nonEqualBody, updatePosition) { var skipPosition = false; while (nonEqualBody) { if (!skipPosition) { this.viewer.updateClientArea(nonEqualBody, nonEqualBody.page); this.viewer.clientActiveArea.height -= nonEqualBody.y - this.viewer.clientActiveArea.y; if (nonEqualBody instanceof FootNoteWidget) { this.viewer.clientArea.height = Number.POSITIVE_INFINITY; this.viewer.clientActiveArea.height = Number.POSITIVE_INFINITY; } else { this.viewer.clientActiveArea.y = nonEqualBody.y; } } skipPosition = updatePosition ? false : true; for (var i = 0; i < nonEqualBody.childWidgets.length; i++) { var block = nonEqualBody.childWidgets[i]; if (block instanceof TableWidget) { this.clearTableWidget(block, true, true, true); } this.layoutBlock(block, 0); } if (nonEqualBody.columnIndex === nonEqualBody.sectionFormat.numberOfColumns - 1 || (!isNullOrUndefined(nonEqualBody.nextRenderedWidget) && nonEqualBody.sectionIndex !== nonEqualBody.nextRenderedWidget.sectionIndex)) { break; } nonEqualBody = nonEqualBody.nextRenderedWidget; } }; Layout.prototype.getNextWidgetHeight = function (body) { var height = 0; var updatedHeight = 0; while (body && body.childWidgets.length > 0) { height = body.lastChild.height; if (body.lastChild instanceof TableWidget) { height = this.getHeight(body.lastChild); } height += body.lastChild.y; if (height > updatedHeight) { updatedHeight = height; } if (!isNullOrUndefined(body) && body.columnIndex === body.sectionFormat.numberOfColumns - 1 || body.sectionFormat.numberOfColumns === 0 || (!isNullOrUndefined(body.nextRenderedWidget) && body.sectionIndex !== body.nextRenderedWidget.sectionIndex)) { break; } body = body.nextRenderedWidget; } return updatedHeight; }; Layout.prototype.getHeight = function (block) { var height = 0; for (var i = 0; i < block.childWidgets.length; i++) { height += block.childWidgets[i].height; } return height; }; Layout.prototype.getCountOrLine = function (section, lineToBeSplit, isSplit, getHeight) { var totalNoOflines = 0; var line; var count = 0; var skip = false; var maxHeight = 0; var lineIndexInCell = 0; var splitCountLine; while (section) { for (var i = 0; i < section.childWidgets.length; i++) { var block = section.childWidgets[i]; if (block instanceof ParagraphWidget) { for (var j = 0; j < block.childWidgets.length; j++) { if (!isSplit) { totalNoOflines++; maxHeight += block.childWidgets[j].height; } else { maxHeight += block.childWidgets[j].height; if (Math.round(lineToBeSplit) < Math.round(maxHeight)) { line = block.childWidgets[j]; skip = true; count = 0; break; } else { count++; } } } } else if (block instanceof TableWidget) { splitCountLine = this.getCountOrLineTable(block, lineToBeSplit, isSplit, maxHeight, false, getHeight); if (getHeight) { maxHeight += splitCountLine.lineCount; } else if (!isSplit) { totalNoOflines += splitCountLine.lineCount; } else if (isNullOrUndefined(splitCountLine.lineWidget)) { // count = splitCountLine.lineCount; maxHeight = splitCountLine.lineCount; } else { line = splitCountLine.lineWidget; lineIndexInCell = splitCountLine.lineCount; skip = true; } } if (skip && isSplit) { break; } } if (skip && isSplit) { break; } if (!isNullOrUndefined(section.nextRenderedWidget) && section.index !== section.nextRenderedWidget.index) { break; } section = section.nextRenderedWidget; } if (getHeight) { return { lineWidget: undefined, lineCount: maxHeight }; } else if (!isSplit) { return { lineWidget: undefined, lineCount: totalNoOflines }; } else { return { lineWidget: line, lineCount: lineIndexInCell }; } }; Layout.prototype.getCountOrLineTable = function (block, lineToBeSplit, isSplit, maxSplitHeight, isNested, getHeight) { var lineIndexInCell = 0; var skip = false; var line; var totalNoOflines = 0; var totalHeight = 0; var minCount = 0; var maxCount = 0; var minHeight = 0; var maxHeight = 0; var splitCountLine; for (var i = 0; i < block.childWidgets.length; i++) { var row = block.childWidgets[i]; var minCountCell = void 0; var maxCountCell = void 0; minCount = 0; maxCount = 0; minHeight = 0; maxHeight = 0; for (var j = 0; j < row.childWidgets.length; j++) { var cell = row.childWidgets[j]; for (var k = 0; k < cell.childWidgets.length; k++) { var blocks = cell.childWidgets[k]; if (blocks instanceof ParagraphWidget && blocks.childWidgets.length > 0) { for (var l = 0; l < blocks.childWidgets.length; l++) { minCount++; minCountCell = cell; minHeight += blocks.childWidgets[l].height; } } else { splitCountLine = this.getCountOrLineTable(blocks, lineToBeSplit, isSplit, maxSplitHeight, true, getHeight); minCount += splitCountLine.lineCount; minHeight += splitCountLine.lineCount; } } if (maxCount < minCount) { maxCount = minCount; // maxCountCell = minCountCell; } if (maxHeight < minHeight) { maxHeight = minHeight; maxCountCell = minCountCell; } minCount = 0; minHeight = 0; } if (!isSplit || isNested) { totalNoOflines = totalNoOflines + maxCount; totalHeight += maxHeight; } else { var countInCell = 0; for (var i_2 = 0; i_2 < maxCountCell.childWidgets.length; i_2++) { var blocks = maxCountCell.childWidgets[i_2]; if (blocks instanceof ParagraphWidget) { for (var j = 0; j < blocks.childWidgets.length; j++) { maxSplitHeight += blocks.childWidgets[j].height; if (Math.round(lineToBeSplit) < Math.round(maxSplitHeight)) { line = blocks.childWidgets[j]; skip = true; maxSplitHeight = 0; lineIndexInCell = countInCell; break; } else { countInCell++; } if (skip && isSplit) { break; } } } else { splitCountLine = this.getCountOrLineTable(blocks, lineToBeSplit, isSplit, maxSplitHeight, false, getHeight); if (isNullOrUndefined(splitCountLine.lineWidget)) { countInCell += splitCountLine.lineCount; // count = splitCountLine.lineCount; maxSplitHeight += blocks.height; } else { skip = true; maxSplitHeight = 0; line = splitCountLine.lineWidget; countInCell += splitCountLine.lineCount; lineIndexInCell = countInCell; break; } } if (skip && isSplit) { break; } } } maxCount = 0; if (skip && isSplit) { break; } } if (getHeight) { return { lineWidget: undefined, lineCount: totalHeight }; } else if (!isSplit) { return { lineWidget: undefined, lineCount: totalNoOflines }; } else if (isSplit && isNullOrUndefined(line) && isNested) { return { lineWidget: undefined, lineCount: totalNoOflines }; } else if (isSplit && isNullOrUndefined(line) && !isNested) { return { lineWidget: undefined, lineCount: maxSplitHeight }; } else { return { lineWidget: line, lineCount: lineIndexInCell }; } }; /** * @private */ Layout.prototype.combineMultiColumn = function (section) { section = this.getBodyWidget(section, false); while (section && section.columnIndex !== 0) { var prevSection = section.previousRenderedWidget; if (prevSection.lastChild instanceof ParagraphWidget && prevSection.lastChild.isEndsWithColumnBreak) { break; } var isPreviousSplit = false; for (var i = 0; i < section.childWidgets.length; i++) { if (section.childWidgets[i] instanceof BlockWidget && !isNullOrUndefined(section.childWidgets[i].previousSplitWidget) && !isNullOrUndefined(section.childWidgets[i].previousSplitWidget.previousSplitWidget) && section.childWidgets[i].previousSplitWidget.bodyWidget.page !== section.childWidgets[i].previousSplitWidget.previousSplitWidget.bodyWidget.page) { isPreviousSplit = true; } if ((section.childWidgets[i] instanceof BlockWidget && !isNullOrUndefined(section.childWidgets[i].previousSplitWidget) && section.childWidgets[i].previousSplitWidget.bodyWidget.page === section.childWidgets[i].bodyWidget.page && !isPreviousSplit)) { section.childWidgets[i].combineWidget(this.viewer); if (prevSection.lastChild instanceof TableWidget) { this.updateCellHeightInCombinedTable(prevSection.lastChild); } i--; continue; } prevSection.childWidgets.push(section.childWidgets[i]); section.childWidgets[i].containerWidget = prevSection; section.childWidgets[i].containerWidget.page = prevSection.page; section.childWidgets.splice(0, 1); i--; } section = section.previousRenderedWidget; } this.documentHelper.removeEmptyPages(); }; Layout.prototype.updateCellHeightInCombinedTable = function (tableWidget) { var maxCellHeight = 0; var minCellHeight = 0; for (var i = 0; i < tableWidget.childWidgets.length; i++) { var row = tableWidget.childWidgets[i]; for (var j = 0; j < row.childWidgets.length; j++) { var cell = row.childWidgets[j]; for (var k = 0; k < cell.childWidgets.length; k++) { minCellHeight += cell.childWidgets[k].height; } if (minCellHeight > maxCellHeight) { maxCellHeight = minCellHeight; } minCellHeight = 0; for (var a = 0; a < row.childWidgets.length; a++) { row.childWidgets[a].height = maxCellHeight; } } maxCellHeight = 0; } }; Layout.prototype.layoutHeaderFooter = function (section, viewer, page) { //Header layout var headerFooterWidget = viewer.getCurrentPageHeaderFooter(section, true); if (headerFooterWidget) { var parentHeader = headerFooterWidget; if (isNullOrUndefined(headerFooterWidget.page)) { headerFooterWidget.page = page; headerFooterWidget.height = 0; this.clearBlockWidget(headerFooterWidget.childWidgets, true, true, true); viewer.updateHFClientArea(section.sectionFormat, true); this.layoutHeaderFooterItems(viewer, headerFooterWidget); } headerFooterWidget = parentHeader.clone(); headerFooterWidget.parentHeaderFooter = parentHeader; this.clearBlockWidget(headerFooterWidget.childWidgets, true, true, true); var header = headerFooterWidget; header.page = page; header.height = 0; this.updateRevisionsToHeaderFooter(header, page); viewer.updateHFClientArea(section.sectionFormat, true); page.headerWidget = this.layoutHeaderFooterItems(viewer, header); //this.updateHeaderFooterToParent(header); //When the vertical position is related to margin, then it should be adjusted based on the layouted header height. Not default header height. if (section.sectionFormat.topMargin < page.boundingRectangle.bottom && page.headerWidget.floatingElements.length > 0 && page.headerWidget.floatingElements[0].textWrappingStyle !== "Behind") { page.headerWidget = this.shiftItemsForVerticalAlignment(header); } } //Footer Layout headerFooterWidget = viewer.getCurrentPageHeaderFooter(section, false); if (headerFooterWidget) { var parentHeader = headerFooterWidget; if (isNullOrUndefined(headerFooterWidget.page)) { headerFooterWidget.page = page; headerFooterWidget.height = 0; this.clearBlockWidget(headerFooterWidget.childWidgets, true, true, true); viewer.updateHFClientArea(section.sectionFormat, false); this.layoutHeaderFooterItems(viewer, headerFooterWidget); } headerFooterWidget = parentHeader.clone(); headerFooterWidget.parentHeaderFooter = parentHeader; this.clearBlockWidget(headerFooterWidget.childWidgets, true, true, true); var footer = headerFooterWidget; footer.page = page; footer.height = 0; viewer.updateHFClientArea(section.sectionFormat, false); this.updateRevisionsToHeaderFooter(footer, page); page.footerWidget = this.layoutHeaderFooterItems(viewer, footer); } }; Layout.prototype.shiftItemsForVerticalAlignment = function (headerWidget) { var floatingElements = headerWidget.floatingElements; for (var i = 0; i < floatingElements.length; i++) { var floatingItem = floatingElements[i]; var verticalOrigin = floatingItem.verticalOrigin; var paragraph = floatingItem.paragraph; // When a owner paragraph is inside the table, we have to skip the vertical alignment of the floating entity. if (verticalOrigin === 'Margin' && !paragraph.isInsideTable) { var yPosition = floatingItem.verticalPosition; if (yPosition != 0) { yPosition += this.viewer.clientActiveArea.y; var diff = yPosition - floatingItem.y; floatingItem.y = yPosition; if (floatingItem instanceof ShapeElementBox) { for (var j = 0; j < floatingItem.textFrame.childWidgets.length; j++) { var block = floatingItem.textFrame.childWidgets[j]; if (block instanceof ParagraphWidget) { block.y = block.y + diff; } } } } } } return headerWidget; }; Layout.prototype.updateHeaderFooterToParent = function (node) { var sectionIndex = node.page.sectionIndex; var typeIndex = this.viewer.getHeaderFooter(node.headerFooterType); var clone = node.clone(); this.documentHelper.headersFooters[sectionIndex][typeIndex] = clone; for (var j = 0; j < clone.childWidgets.length; j++) { var child = clone.childWidgets[j]; if (child instanceof TableWidget) { this.clearTableWidget(child, false, true); } } return clone; }; /* eslint-disable @typescript-eslint/no-explicit-any */ Layout.prototype.updateRevisionsToHeaderFooter = function (clone, page) { var childWidge = clone.childWidgets; if (clone instanceof HeaderFooterWidget && childWidge.length > 0) { for (var i = 0; i < childWidge.length; i++) { if (childWidge[i].childWidgets.length > 0) { var lineWidge = childWidge[i].childWidgets; for (var j = 0; j < lineWidge.length; j++) { var childrens = lineWidge[j].children; if (childrens) { for (var k = 0; k < childrens.length; k++) { if (childrens[k].removedIds.length > 0) { var removeId = childrens[k].removedIds; for (var l = 0; l < removeId.length; l++) { var revision = this.documentHelper.revisionsInternal.get(removeId[l]); childrens[k].revisions[l] = revision; this.updateRevisionRange(revision, page); } } } } } } } } }; Layout.prototype.updateRevisionRange = function (revision, page) { for (var i = 0; i < revision.range.length; i++) { var inline = revision.range[i]; if (!inline.line.paragraph.bodyWidget.page) { inline.line.paragraph.bodyWidget.page = page; } } }; Layout.prototype.linkFieldInHeaderFooter = function (widget) { var firstChild = widget.firstChild; do { if (firstChild instanceof ParagraphWidget) { this.linkFieldInParagraph(firstChild); } else { this.linkFieldInTable(firstChild); } /* eslint-disable no-cond-assign */ } while (firstChild = firstChild.nextWidget); }; Layout.prototype.linkFieldInParagraph = function (widget) { for (var j = 0; j < widget.childWidgets.length; j++) { var line = widget.childWidgets[j]; for (var i = 0; i < line.children.length; i++) { var element = line.children[i]; if (element instanceof FieldElementBox && (element.fieldType !== 0 || (element.fieldType === 0 && this.documentHelper.fields.indexOf(element) === -1))) { element.linkFieldCharacter(this.documentHelper); } if (element instanceof FieldTextElementBox && !isNullOrUndefined(element.previousElement) && element.fieldBegin !== element.previousElement.fieldBegin) { element.fieldBegin = element.previousElement.fieldBegin; } if (element instanceof ShapeElementBox) { var firstBlock = element.textFrame.firstChild; if (firstBlock) { do { if (firstBlock instanceof ParagraphWidget) { this.linkFieldInParagraph(firstBlock); } else { this.linkFieldInTable(firstBlock); } /* eslint-disable no-cond-assign */ } while (firstBlock = firstBlock.nextWidget); } } else if (element instanceof CommentCharacterElementBox) { var comment = this.getCommentById(this.documentHelper.comments, element.commentId); if (!isNullOrUndefined(comment)) { if (element.commentType === 0) { comment.commentStart = element; } else { comment.commentEnd = element; } element.comment = comment; } } } } }; /** * @private */ Layout.prototype.getCommentById = function (commentsCollection, commentId) { for (var i = 0; i < commentsCollection.length; i++) { var comment = commentsCollection[i]; if (comment.commentId === commentId) { return comment; } } return undefined; }; Layout.prototype.linkFieldInTable = function (widget) { for (var i = 0; i < widget.childWidgets.length; i++) { var row = widget.childWidgets[i]; for (var j = 0; j < row.childWidgets.length; j++) { var cell = row.childWidgets[j]; for (var k = 0; k < cell.childWidgets.length; k++) { var block = cell.childWidgets[k]; if (block instanceof ParagraphWidget) { this.linkFieldInParagraph(block); } else { this.linkFieldInTable(block); } } } } }; Layout.prototype.layoutHeaderFooterItems = function (viewer, widget) { this.viewer.updateClientAreaLocation(widget, viewer.clientActiveArea); if (widget.childWidgets.length === 0) { var pargaraph = new ParagraphWidget(); var line = new LineWidget(pargaraph); pargaraph.childWidgets.push(line); widget.childWidgets.push(pargaraph); pargaraph.containerWidget = widget; } this.linkFieldInHeaderFooter(widget); for (var i = 0; i < widget.childWidgets.length; i++) { var block = widget.childWidgets[i]; if (block instanceof TableWidget && block.tableFormat.preferredWidthType === 'Auto' && !block.tableFormat.allowAutoFit && !block.isGridUpdated) { block.calculateGrid(); } viewer.updateClientAreaForBlock(block, true); this.layoutBlock(block, 0); viewer.updateClientAreaForBlock(block, false); } var type = widget.headerFooterType; if (type === 'OddFooter' || type === 'EvenFooter' || type === 'FirstPageFooter') { this.shiftChildLocation(viewer.clientArea.y - viewer.clientActiveArea.y, widget); } return widget; }; L