@json-to-office/core-docx 4.3.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3265,6 +3265,7 @@ import {
3265
3265
  Header,
3266
3266
  ImageRun,
3267
3267
  Packer,
3268
+ LineRuleType as LineRuleType2,
3268
3269
  Paragraph as Paragraph2
3269
3270
  } from "docx";
3270
3271
  function createDocxJsRenderer() {
@@ -3518,21 +3519,33 @@ function sectionChildren(section2, resources) {
3518
3519
  const blocks = section2.children.map((block2) => emitBlock(block2, resources));
3519
3520
  const bookmark = section2.bookmark;
3520
3521
  if (!bookmark) return blocks;
3521
- const out = [];
3522
+ const out = [...blocks];
3522
3523
  if (bookmark.opens) {
3523
- out.push(
3524
- new Paragraph2({
3525
- children: [new BookmarkStart2(bookmark.name, bookmark.id)],
3526
- spacing: { before: 0, after: 0, line: 0 }
3527
- })
3528
- );
3524
+ const start = new BookmarkStart2(bookmark.name, bookmark.id);
3525
+ const first = out[0];
3526
+ if (first instanceof Paragraph2) {
3527
+ first.addRunToFront(start);
3528
+ } else {
3529
+ out.unshift(anchorParagraph(start));
3530
+ }
3529
3531
  }
3530
- out.push(...blocks);
3531
3532
  if (bookmark.closes) {
3532
- out.push(new Paragraph2({ children: [new BookmarkEnd2(bookmark.id)] }));
3533
+ const end = new BookmarkEnd2(bookmark.id);
3534
+ const last = out[out.length - 1];
3535
+ if (last instanceof Paragraph2) {
3536
+ last.addChildElement(end);
3537
+ } else {
3538
+ out.push(anchorParagraph(end));
3539
+ }
3533
3540
  }
3534
3541
  return out;
3535
3542
  }
3543
+ function anchorParagraph(anchor) {
3544
+ return new Paragraph2({
3545
+ children: [anchor],
3546
+ spacing: { before: 0, after: 0, line: 20, lineRule: LineRuleType2.EXACT }
3547
+ });
3548
+ }
3536
3549
  function coreProperties(ir) {
3537
3550
  const { metadata } = ir;
3538
3551
  return {
@@ -9307,6 +9320,15 @@ function compileColumns(component, scope) {
9307
9320
  if (configs.length === 0) return [];
9308
9321
  const available = getAvailableWidthTwips(ctx.theme, ctx.themeName);
9309
9322
  const { widths, gaps } = columnMetrics(configs, props.gap, available);
9323
+ const stated = widths.map(
9324
+ (width, index) => width + gaps[index] / 2 + (index > 0 ? gaps[index - 1] / 2 : 0)
9325
+ );
9326
+ const statedTotal = stated.reduce((sum, cell) => sum + cell, 0);
9327
+ const scale = statedTotal > available ? available / statedTotal : 1;
9328
+ const exact = stated.map((cell) => cell * scale);
9329
+ const cells = exact.map((cell) => Math.round(cell));
9330
+ const total = exact.reduce((sum, cell) => sum + cell, 0);
9331
+ cells[cells.length - 1] += Math.round(total) - cells.reduce((a, b) => a + b, 0);
9310
9332
  const contents = configs.map(() => []);
9311
9333
  const children = component.children ?? [];
9312
9334
  children.forEach((child, index) => {
@@ -9318,7 +9340,7 @@ function compileColumns(component, scope) {
9318
9340
  kind: "table",
9319
9341
  id: scope.id,
9320
9342
  path: path3,
9321
- columnGrid: { unit: "twips", values: [] },
9343
+ columnGrid: { unit: "twips", values: cells },
9322
9344
  width: { kind: "percent", value: 100 },
9323
9345
  layout: "fixed",
9324
9346
  borders: NO_BORDERS,
@@ -9343,7 +9365,7 @@ function compileColumns(component, scope) {
9343
9365
  children: []
9344
9366
  }
9345
9367
  ],
9346
- widthTwips: widths[index],
9368
+ widthTwips: cells[index],
9347
9369
  margins: {
9348
9370
  topTwips: 0,
9349
9371
  rightTwips: gaps[index] / 2,
@@ -11688,12 +11710,6 @@ function determineComponentLayout(component) {
11688
11710
  }
11689
11711
  return "single";
11690
11712
  }
11691
- if ("children" in component && component.children) {
11692
- const hasColumns = component.children.some(
11693
- (child) => isColumnsComponent(child)
11694
- );
11695
- return hasColumns ? "multi-column" : "single";
11696
- }
11697
11713
  return "single";
11698
11714
  }
11699
11715
  function processLayoutComponents(components) {