@itwin/core-common 5.2.0-dev.31 → 5.2.0-dev.33

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.
@@ -7,10 +7,22 @@
7
7
  * @module Annotation
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.TextBlock = exports.Paragraph = exports.FieldRun = exports.TabRun = exports.LineBreakRun = exports.FractionRun = exports.TextRun = exports.Run = exports.TextBlockComponent = void 0;
10
+ exports.TextBlock = exports.List = exports.Paragraph = exports.FieldRun = exports.TabRun = exports.LineBreakRun = exports.FractionRun = exports.TextRun = exports.Run = exports.TextBlockComponent = void 0;
11
+ exports.traverseTextBlockComponent = traverseTextBlockComponent;
12
+ exports.getMarkerText = getMarkerText;
11
13
  const TextStyle_1 = require("./TextStyle");
12
- /** Abstract representation of any of the building blocks that make up a [[TextBlock]] document - namely [[Run]]s, [[Paragraph]]s, and [[TextBlock]] itself.
13
- * The [[TextBlock]] can specify an [AnnotationTextStyle]($backend) that formats its contents. Each component can specify an optional [[styleOverrides]] to customize that formatting.
14
+ function clearStyleOverrides(component, options) {
15
+ component.styleOverrides = {};
16
+ if (!options?.preserveChildrenOverrides) {
17
+ for (const child of component.children) {
18
+ child.clearStyleOverrides(options);
19
+ }
20
+ }
21
+ }
22
+ /**
23
+ * Abstract representation of any of the building blocks that make up a [[TextBlock]] document - namely [[Run]]s and [[StructuralTextBlockComponent]]s.
24
+ * The [[TextBlock]] can specify an [AnnotationTextStyle]($backend) that formats its contents.
25
+ * Each component can specify an optional [[styleOverrides]] to customize that formatting.
14
26
  * @beta
15
27
  */
16
28
  class TextBlockComponent {
@@ -76,15 +88,15 @@ exports.TextBlockComponent = TextBlockComponent;
76
88
  var Run;
77
89
  (function (Run) {
78
90
  /** Create a run from its JSON representation.
79
- * @see [[TextRun.create]], [[FractionRun.create]], and [[LineBreakRun.create]] to create a run directly.
91
+ * @see [[TextRun.create]], [[FractionRun.create]], [[FieldRun.create]], [[TabRun.create]], and [[LineBreakRun.create]] to create a run directly.
80
92
  */
81
93
  function fromJSON(props) {
82
94
  switch (props.type) {
83
- case "text": return TextRun.create(props);
95
+ case "field": return FieldRun.create(props);
84
96
  case "fraction": return FractionRun.create(props);
85
- case "tab": return TabRun.create(props);
86
97
  case "linebreak": return LineBreakRun.create(props);
87
- case "field": return FieldRun.create(props);
98
+ case "tab": return TabRun.create(props);
99
+ case "text": return TextRun.create(props);
88
100
  }
89
101
  }
90
102
  Run.fromJSON = fromJSON;
@@ -116,7 +128,10 @@ class TextRun extends TextBlockComponent {
116
128
  };
117
129
  }
118
130
  static create(props) {
119
- return new TextRun(props);
131
+ return new TextRun({ ...props, type: "text" });
132
+ }
133
+ get isEmpty() {
134
+ return this.stringify().length === 0;
120
135
  }
121
136
  /** Simply returns [[content]]. */
122
137
  stringify() {
@@ -155,7 +170,10 @@ class FractionRun extends TextBlockComponent {
155
170
  return new FractionRun(this.toJSON());
156
171
  }
157
172
  static create(props) {
158
- return new FractionRun(props);
173
+ return new FractionRun({ ...props, type: "fraction" });
174
+ }
175
+ get isEmpty() {
176
+ return this.stringify().length === 0;
159
177
  }
160
178
  /** Formats the fraction as a string with the [[numerator]] and [[denominator]] separated by [[TextBlockStringifyOptions.fractionSeparator]]. */
161
179
  stringify(options) {
@@ -183,11 +201,14 @@ class LineBreakRun extends TextBlockComponent {
183
201
  };
184
202
  }
185
203
  static create(props) {
186
- return new LineBreakRun(props);
204
+ return new LineBreakRun({ ...props, type: "linebreak" });
187
205
  }
188
206
  clone() {
189
207
  return new LineBreakRun(this.toJSON());
190
208
  }
209
+ get isEmpty() {
210
+ return this.stringify().length === 0;
211
+ }
191
212
  /** Simply returns [[TextBlockStringifyOptions.lineBreak]]. */
192
213
  stringify(options) {
193
214
  return options?.lineBreak ?? " ";
@@ -216,6 +237,9 @@ class TabRun extends TextBlockComponent {
216
237
  static create(props) {
217
238
  return new TabRun(props);
218
239
  }
240
+ get isEmpty() {
241
+ return this.stringify().length === 0;
242
+ }
219
243
  /**
220
244
  * Converts a [[TabRun]] to its string representation.
221
245
  * If the `tabsAsSpaces` option is provided, returns a string of spaces of the specified length.
@@ -278,6 +302,7 @@ class FieldRun extends TextBlockComponent {
278
302
  propertyHost: { ...props.propertyHost },
279
303
  propertyPath: structuredClone(props.propertyPath),
280
304
  formatOptions: structuredClone(props.formatOptions),
305
+ type: "field",
281
306
  });
282
307
  }
283
308
  /** Convert the FieldRun to its JSON representation. */
@@ -300,6 +325,9 @@ class FieldRun extends TextBlockComponent {
300
325
  clone() {
301
326
  return new FieldRun(this.toJSON());
302
327
  }
328
+ get isEmpty() {
329
+ return this.stringify().length === 0;
330
+ }
303
331
  /** Convert this FieldRun to a simple string representation. */
304
332
  stringify() {
305
333
  return this.cachedContent;
@@ -339,64 +367,99 @@ class FieldRun extends TextBlockComponent {
339
367
  }
340
368
  }
341
369
  exports.FieldRun = FieldRun;
342
- /** A collection of [[Run]]s within a [[TextBlock]]. Each paragraph within a text block is laid out on a separate line.
370
+ /** A collection of [[Run]]s and [[List]]s. Paragraphs can be appended to [[List]]s and [[TextBlock]]s.
371
+ * Each paragraph is laid out on a separate line. If included in a [[List]], the paragraph will be treated as a list item.
343
372
  * @beta
344
373
  */
345
374
  class Paragraph extends TextBlockComponent {
346
- /** The runs within the paragraph. You can modify the contents of this array to change the content of the paragraph. */
347
- runs;
375
+ type = "paragraph";
376
+ children;
348
377
  constructor(props) {
349
378
  super(props);
350
- this.runs = props?.runs?.map((run) => Run.fromJSON(run)) ?? [];
379
+ this.children = props?.children?.map((child) => child.type === "list" ? List.create(child) : Run.fromJSON(child)) ?? [];
380
+ }
381
+ /** Create a paragraph from its JSON representation. */
382
+ static create(props) {
383
+ return new Paragraph(props);
384
+ }
385
+ clearStyleOverrides(options) {
386
+ clearStyleOverrides(this, options);
387
+ }
388
+ get isEmpty() {
389
+ return this.children.length === 0;
390
+ }
391
+ clone() {
392
+ return new Paragraph(this.toJSON());
351
393
  }
352
394
  toJSON() {
353
395
  return {
354
396
  ...super.toJSON(),
355
- runs: this.runs.map((run) => run.toJSON()),
397
+ children: this.children.map((child) => child.toJSON()),
356
398
  };
357
399
  }
358
- /** Create a paragraph from its JSON representation. */
400
+ /** Compute a string representation of this paragraph by concatenating the string representations of all of its children. */
401
+ stringify(options, context) {
402
+ return this.children.map((x, index) => (index > 0 && x.type === "list") ? `${options?.paragraphBreak ?? " "}${x.stringify(options, context)}` : x.stringify(options, context)).join("") ?? "";
403
+ }
404
+ equals(other) {
405
+ return (other instanceof Paragraph) && super.equals(other);
406
+ }
407
+ }
408
+ exports.Paragraph = Paragraph;
409
+ /** A collection of list items ([[Paragraph]]s). Lists can be appended to [[Paragraph]]s.
410
+ * Lists will be laid out on a new line. Each item in a list is laid out on a separate line.
411
+ * @beta
412
+ */
413
+ class List extends TextBlockComponent {
414
+ type = "list";
415
+ children;
416
+ constructor(props) {
417
+ super(props);
418
+ this.children = props?.children?.map((child) => Paragraph.create(child)) ?? [];
419
+ }
420
+ /** Create a list from its JSON representation. */
359
421
  static create(props) {
360
- return new Paragraph(props);
422
+ return new List({ ...props, type: "list" });
423
+ }
424
+ clearStyleOverrides(options) {
425
+ clearStyleOverrides(this, options);
426
+ }
427
+ get isEmpty() {
428
+ return this.children.length === 0;
361
429
  }
362
430
  clone() {
363
- return new Paragraph(this.toJSON());
431
+ return new List(this.toJSON());
364
432
  }
365
- /**
366
- * Clears any [[styleOverrides]] applied to this Paragraph.
367
- * Will also clear [[styleOverrides]] from all child components unless [[ClearTextStyleOptions.preserveChildrenOverrides]] is `true`.
368
- */
369
- clearStyleOverrides(options) {
370
- super.clearStyleOverrides();
371
- if (options?.preserveChildrenOverrides)
372
- return;
373
- for (const run of this.runs) {
374
- run.clearStyleOverrides();
375
- }
433
+ toJSON() {
434
+ return {
435
+ ...super.toJSON(),
436
+ type: "list",
437
+ children: this.children.map((run) => run.toJSON()),
438
+ };
376
439
  }
377
- /** Compute a string representation of this paragraph by concatenating the string representations of all of its [[runs]]. */
378
- stringify(options) {
379
- return this.runs.map((x) => x.stringify(options)).join("");
440
+ /** Compute a string representation of this list by concatenating the string representations of all of its [[children]]. */
441
+ stringify(options, context) {
442
+ const children = this.children.map((x, index) => {
443
+ const depth = context?.depth ?? 0;
444
+ const marker = getMarkerText(this.styleOverrides.listMarker ?? TextStyle_1.TextStyleSettings.defaultProps.listMarker, index + 1);
445
+ const tab = (options?.tabsAsSpaces ? " ".repeat(options.tabsAsSpaces) : "\t").repeat(depth);
446
+ return `${tab}${marker}${options?.listMarkerBreak ?? " "}${x.stringify(options, { depth: depth + 1 })}`;
447
+ });
448
+ return children.join(options?.paragraphBreak ?? " ") ?? "";
380
449
  }
381
450
  equals(other) {
382
- if (!(other instanceof Paragraph)) {
383
- return false;
384
- }
385
- if (this.runs.length !== other.runs.length || !super.equals(other)) {
386
- return false;
387
- }
388
- return this.runs.every((run, index) => run.equals(other.runs[index]));
451
+ return (other instanceof List) && super.equals(other);
389
452
  }
390
453
  }
391
- exports.Paragraph = Paragraph;
454
+ exports.List = List;
392
455
  ;
393
- /** Represents a formatted text document consisting of a series of [[Paragraph]]s, each laid out on a separate line and containing their own content in the form of [[Run]]s.
394
- * You can change the content of the document by directly modifying the contents of its [[paragraphs]], or via [[appendParagraph]] and [[appendRun]].
456
+ /** Represents a formatted text document consisting of a series of [[Paragraph]]s, each laid out on a separate line and containing their own content.
395
457
  * No word-wrapping is applied to the document unless a [[width]] greater than zero is specified.
396
458
  * @see [[TextAnnotation]] to position a text block as an annotation in 2d or 3d space.
397
459
  * @beta
398
460
  */
399
461
  class TextBlock extends TextBlockComponent {
462
+ children;
400
463
  /** The width of the document in meters. Lines that would exceed this width are instead wrapped around to the next line if possible.
401
464
  * A value less than or equal to zero indicates no wrapping is to be applied.
402
465
  * Default: 0
@@ -406,8 +469,6 @@ class TextBlock extends TextBlockComponent {
406
469
  justification;
407
470
  /** The margins of the document. */
408
471
  margins;
409
- /** The ordered list of paragraphs within the document. */
410
- paragraphs;
411
472
  constructor(props) {
412
473
  super(props);
413
474
  this.width = props.width ?? 0;
@@ -419,7 +480,10 @@ class TextBlock extends TextBlockComponent {
419
480
  top: props.margins?.top ?? 0,
420
481
  bottom: props.margins?.bottom ?? 0,
421
482
  };
422
- this.paragraphs = props.paragraphs?.map((x) => Paragraph.create(x)) ?? [];
483
+ this.children = props?.children?.map((para) => Paragraph.create(para)) ?? [];
484
+ }
485
+ clearStyleOverrides(options) {
486
+ clearStyleOverrides(this, options);
423
487
  }
424
488
  toJSON() {
425
489
  return {
@@ -427,7 +491,7 @@ class TextBlock extends TextBlockComponent {
427
491
  width: this.width,
428
492
  justification: this.justification,
429
493
  margins: this.margins,
430
- paragraphs: this.paragraphs.map((x) => x.toJSON()),
494
+ children: this.children.map((x) => x.toJSON()),
431
495
  };
432
496
  }
433
497
  /** Create a text block from its JSON representation. */
@@ -436,62 +500,132 @@ class TextBlock extends TextBlockComponent {
436
500
  }
437
501
  /** Returns true if every paragraph in this text block is empty. */
438
502
  get isEmpty() {
439
- return this.paragraphs.every((p) => p.runs.length === 0);
503
+ return !this.children || this.children.every((child) => child.isEmpty);
440
504
  }
441
505
  clone() {
442
506
  return new TextBlock(this.toJSON());
443
507
  }
444
- /**
445
- * Clears any [[styleOverrides]] applied to this TextBlock.
446
- * Will also clear [[styleOverrides]] from all child components unless [[ClearTextStyleOptions.preserveChildrenOverrides]] is `true`.
447
- */
448
- clearStyleOverrides(options) {
449
- super.clearStyleOverrides();
450
- if (options?.preserveChildrenOverrides)
451
- return;
452
- for (const paragraph of this.paragraphs) {
453
- paragraph.clearStyleOverrides();
454
- }
455
- }
456
- /** Compute a string representation of the document's contents by concatenating the string representations of each of its [[paragraphs]], separated by [[TextBlockStringifyOptions.paragraphBreak]]. */
508
+ /** Compute a string representation of the document's contents by concatenating the string representations of each of its [[children]], separated by [[TextBlockStringifyOptions.paragraphBreak]]. */
457
509
  stringify(options) {
458
- return this.paragraphs.map((x) => x.stringify(options)).join(options?.paragraphBreak ?? " ");
510
+ return this.children.map((x) => x.stringify(options)).join(options?.paragraphBreak ?? " ") || "";
459
511
  }
460
512
  /** Add and return a new paragraph.
461
513
  * By default, the paragraph will be created with no [[styleOverrides]], so that it inherits the style of this block.
462
- * @param seedFromLast If true and [[paragraphs]] is not empty, the new paragraph will inherit the style overrides of the last [[Paragraph]] in this block.
514
+ * @param seedFromLast If true and [[children]] is not empty, the new paragraph will inherit the style overrides of the last child in this block.
515
+ * @note Be sure you pass in [[ParagraphProps]] and not [[Paragraph]] or style overrides will be ignored.
463
516
  */
464
- appendParagraph(seedFromLast = false) {
465
- let styleOverrides = {};
466
- if (seedFromLast && this.paragraphs.length > 0) {
467
- const seed = this.paragraphs[this.paragraphs.length - 1];
468
- styleOverrides = { ...seed.styleOverrides };
469
- }
470
- const paragraph = Paragraph.create({
471
- styleOverrides
472
- });
473
- this.paragraphs.push(paragraph);
517
+ appendParagraph(props, seedFromLast = false) {
518
+ const seed = seedFromLast ? this.children[this.children.length - 1] : undefined;
519
+ const styleOverrides = { ...seed?.styleOverrides, ...props?.styleOverrides };
520
+ const paragraph = Paragraph.create({ ...props, styleOverrides });
521
+ this.children.push(paragraph);
474
522
  return paragraph;
475
523
  }
476
524
  /** Append a run to the last [[Paragraph]] in this block.
477
- * If the block contains no [[paragraphs]], a new one will first be created using [[appendParagraph]].
525
+ * If the block contains no [[children]], a new [[Paragraph]] will first be created using [[appendParagraph]].
478
526
  */
479
527
  appendRun(run) {
480
- const paragraph = this.paragraphs[this.paragraphs.length - 1] ?? this.appendParagraph();
481
- paragraph.runs.push(run);
528
+ const paragraph = this.children[this.children.length - 1] ?? this.appendParagraph();
529
+ paragraph.children.push(run);
482
530
  }
483
531
  equals(other) {
484
532
  if (!(other instanceof TextBlock)) {
485
533
  return false;
486
534
  }
487
- if (this.width !== other.width || this.justification !== other.justification || this.paragraphs.length !== other.paragraphs.length) {
535
+ if (!super.equals(other)) {
536
+ return false;
537
+ }
538
+ if (this.width !== other.width || this.justification !== other.justification) {
488
539
  return false;
489
540
  }
490
541
  const marginsAreEqual = Object.entries(this.margins).every(([key, value]) => value === other.margins[key]);
491
542
  if (!marginsAreEqual)
492
543
  return false;
493
- return this.paragraphs.every((paragraph, index) => paragraph.equals(other.paragraphs[index]));
544
+ if (this.children && other.children) {
545
+ if (this.children.length !== other.children.length) {
546
+ return false;
547
+ }
548
+ return this.children.every((child, index) => other.children && child.equals(other.children[index]));
549
+ }
550
+ return true;
494
551
  }
495
552
  }
496
553
  exports.TextBlock = TextBlock;
554
+ /**
555
+ * Recursively traverses a [[StructuralTextBlockComponent]] tree, yielding each child component along with its parent container.
556
+ * This generator enables depth-first iteration over all components in a text block structure, including paragraphs, lists, and runs.
557
+ *
558
+ * @param parent The root container whose children should be traversed.
559
+ * @returns An IterableIterator yielding objects with the child component and its parent container.
560
+ * @beta
561
+ */
562
+ function* traverseTextBlockComponent(parent) {
563
+ for (const child of parent.children) {
564
+ yield { parent, child };
565
+ if (child.type === "list" || child.type === "paragraph") {
566
+ yield* traverseTextBlockComponent(child);
567
+ }
568
+ }
569
+ }
570
+ /**
571
+ * Returns the formatted marker text for a list item based on the marker type and item number.
572
+ * Supports ordered and unordered list markers, including alphabetic, Roman numeral, and numeric formats.
573
+ * @param marker The type of list marker to use.
574
+ * @param num The item number in the list.
575
+ * @returns The formatted marker string for the list item.
576
+ * @beta
577
+ */
578
+ function getMarkerText(marker, num) {
579
+ let markerString = "";
580
+ switch (marker.enumerator) {
581
+ case undefined:
582
+ case TextStyle_1.ListMarkerEnumerator.Number:
583
+ markerString = `${num}`;
584
+ break;
585
+ case TextStyle_1.ListMarkerEnumerator.Letter:
586
+ markerString = integerToAlpha(num);
587
+ break;
588
+ case TextStyle_1.ListMarkerEnumerator.RomanNumeral:
589
+ markerString = integerToRoman(num);
590
+ break;
591
+ default:
592
+ markerString = marker.enumerator;
593
+ break;
594
+ }
595
+ if (marker.case) {
596
+ markerString = marker.case === "upper" ? markerString.toUpperCase() : markerString.toLowerCase();
597
+ }
598
+ const terminator = marker.terminator === "period" ? "." : marker.terminator === "parenthesis" ? ")" : "";
599
+ return `${markerString}${terminator}`;
600
+ }
601
+ /**
602
+ * Converts an integer to its Roman numeral representation.
603
+ * Supports numbers from 1 and above.
604
+ * @param num The integer to convert.
605
+ * @returns The Roman numeral string.
606
+ */
607
+ function integerToRoman(num) {
608
+ const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
609
+ const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
610
+ let roman = '';
611
+ for (let i = 0; i < values.length; i++) {
612
+ while (num >= values[i]) {
613
+ roman += symbols[i];
614
+ num -= values[i];
615
+ }
616
+ }
617
+ return roman;
618
+ }
619
+ /**
620
+ * Converts an integer to its alphabetic representation (A-Z, AA-ZZ, etc.).
621
+ * Used for ordered list markers with alphabetic styles.
622
+ * @param num The integer to convert (1-based).
623
+ * @returns The alphabetic string for the given number.
624
+ */
625
+ function integerToAlpha(num) {
626
+ const letterOffset = (num - 1) % 26;
627
+ const letter = String.fromCharCode(65 + letterOffset);
628
+ const depth = Math.ceil(num / 26);
629
+ return letter.repeat(depth);
630
+ }
497
631
  //# sourceMappingURL=TextBlock.js.map