@itwin/core-frontend 5.0.0-dev.112 → 5.0.0-dev.113

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.
Files changed (92) hide show
  1. package/lib/cjs/IModelApp.d.ts +15 -0
  2. package/lib/cjs/IModelApp.d.ts.map +1 -1
  3. package/lib/cjs/IModelApp.js +20 -1
  4. package/lib/cjs/IModelApp.js.map +1 -1
  5. package/lib/cjs/Viewport.d.ts +2 -0
  6. package/lib/cjs/Viewport.d.ts.map +1 -1
  7. package/lib/cjs/Viewport.js +4 -0
  8. package/lib/cjs/Viewport.js.map +1 -1
  9. package/lib/cjs/core-frontend.d.ts +1 -1
  10. package/lib/cjs/core-frontend.d.ts.map +1 -1
  11. package/lib/cjs/core-frontend.js.map +1 -1
  12. package/lib/cjs/extension/providers/ExtensionLoadScript.js.map +1 -1
  13. package/lib/cjs/internal/tile/ClassifierTileTree.js +3 -1
  14. package/lib/cjs/internal/tile/ClassifierTileTree.js.map +1 -1
  15. package/lib/cjs/internal/tile/PrimaryTileTree.d.ts.map +1 -1
  16. package/lib/cjs/internal/tile/PrimaryTileTree.js +4 -3
  17. package/lib/cjs/internal/tile/PrimaryTileTree.js.map +1 -1
  18. package/lib/cjs/properties/AngleDescription.d.ts +4 -1
  19. package/lib/cjs/properties/AngleDescription.d.ts.map +1 -1
  20. package/lib/cjs/properties/AngleDescription.js +10 -2
  21. package/lib/cjs/properties/AngleDescription.js.map +1 -1
  22. package/lib/cjs/properties/FormattedQuantityDescription.d.ts +11 -1
  23. package/lib/cjs/properties/FormattedQuantityDescription.d.ts.map +1 -1
  24. package/lib/cjs/properties/FormattedQuantityDescription.js +8 -2
  25. package/lib/cjs/properties/FormattedQuantityDescription.js.map +1 -1
  26. package/lib/cjs/properties/LengthDescription.d.ts +12 -3
  27. package/lib/cjs/properties/LengthDescription.d.ts.map +1 -1
  28. package/lib/cjs/properties/LengthDescription.js +30 -6
  29. package/lib/cjs/properties/LengthDescription.js.map +1 -1
  30. package/lib/cjs/quantity-formatting/QuantityFormatter.d.ts +103 -7
  31. package/lib/cjs/quantity-formatting/QuantityFormatter.d.ts.map +1 -1
  32. package/lib/cjs/quantity-formatting/QuantityFormatter.js +212 -23
  33. package/lib/cjs/quantity-formatting/QuantityFormatter.js.map +1 -1
  34. package/lib/cjs/tile/TileAdmin.d.ts +10 -0
  35. package/lib/cjs/tile/TileAdmin.d.ts.map +1 -1
  36. package/lib/cjs/tile/TileAdmin.js +3 -0
  37. package/lib/cjs/tile/TileAdmin.js.map +1 -1
  38. package/lib/cjs/tools/MeasureTool.d.ts +9 -0
  39. package/lib/cjs/tools/MeasureTool.d.ts.map +1 -1
  40. package/lib/cjs/tools/MeasureTool.js +53 -22
  41. package/lib/cjs/tools/MeasureTool.js.map +1 -1
  42. package/lib/cjs/tools/ViewTool.d.ts.map +1 -1
  43. package/lib/cjs/tools/ViewTool.js +4 -2
  44. package/lib/cjs/tools/ViewTool.js.map +1 -1
  45. package/lib/esm/IModelApp.d.ts +15 -0
  46. package/lib/esm/IModelApp.d.ts.map +1 -1
  47. package/lib/esm/IModelApp.js +21 -2
  48. package/lib/esm/IModelApp.js.map +1 -1
  49. package/lib/esm/Viewport.d.ts +2 -0
  50. package/lib/esm/Viewport.d.ts.map +1 -1
  51. package/lib/esm/Viewport.js +4 -0
  52. package/lib/esm/Viewport.js.map +1 -1
  53. package/lib/esm/core-frontend.d.ts +1 -1
  54. package/lib/esm/core-frontend.d.ts.map +1 -1
  55. package/lib/esm/core-frontend.js.map +1 -1
  56. package/lib/esm/extension/providers/ExtensionLoadScript.js +1 -1
  57. package/lib/esm/extension/providers/ExtensionLoadScript.js.map +1 -1
  58. package/lib/esm/internal/tile/ClassifierTileTree.js +3 -1
  59. package/lib/esm/internal/tile/ClassifierTileTree.js.map +1 -1
  60. package/lib/esm/internal/tile/PrimaryTileTree.d.ts.map +1 -1
  61. package/lib/esm/internal/tile/PrimaryTileTree.js +4 -3
  62. package/lib/esm/internal/tile/PrimaryTileTree.js.map +1 -1
  63. package/lib/esm/properties/AngleDescription.d.ts +4 -1
  64. package/lib/esm/properties/AngleDescription.d.ts.map +1 -1
  65. package/lib/esm/properties/AngleDescription.js +10 -2
  66. package/lib/esm/properties/AngleDescription.js.map +1 -1
  67. package/lib/esm/properties/FormattedQuantityDescription.d.ts +11 -1
  68. package/lib/esm/properties/FormattedQuantityDescription.d.ts.map +1 -1
  69. package/lib/esm/properties/FormattedQuantityDescription.js +8 -2
  70. package/lib/esm/properties/FormattedQuantityDescription.js.map +1 -1
  71. package/lib/esm/properties/LengthDescription.d.ts +12 -3
  72. package/lib/esm/properties/LengthDescription.d.ts.map +1 -1
  73. package/lib/esm/properties/LengthDescription.js +30 -6
  74. package/lib/esm/properties/LengthDescription.js.map +1 -1
  75. package/lib/esm/quantity-formatting/QuantityFormatter.d.ts +103 -7
  76. package/lib/esm/quantity-formatting/QuantityFormatter.d.ts.map +1 -1
  77. package/lib/esm/quantity-formatting/QuantityFormatter.js +210 -23
  78. package/lib/esm/quantity-formatting/QuantityFormatter.js.map +1 -1
  79. package/lib/esm/tile/TileAdmin.d.ts +10 -0
  80. package/lib/esm/tile/TileAdmin.d.ts.map +1 -1
  81. package/lib/esm/tile/TileAdmin.js +3 -0
  82. package/lib/esm/tile/TileAdmin.js.map +1 -1
  83. package/lib/esm/tools/MeasureTool.d.ts +9 -0
  84. package/lib/esm/tools/MeasureTool.d.ts.map +1 -1
  85. package/lib/esm/tools/MeasureTool.js +53 -22
  86. package/lib/esm/tools/MeasureTool.js.map +1 -1
  87. package/lib/esm/tools/ViewTool.d.ts.map +1 -1
  88. package/lib/esm/tools/ViewTool.js +4 -2
  89. package/lib/esm/tools/ViewTool.js.map +1 -1
  90. package/lib/public/scripts/parse-imdl-worker.js +1 -1
  91. package/lib/workers/webpack/parse-imdl-worker.js +1 -1
  92. package/package.json +20 -20
@@ -5,7 +5,7 @@
5
5
  /** @packageDocumentation
6
6
  * @module QuantityFormatting
7
7
  */
8
- import { BentleyError, BeUiEvent, Logger } from "@itwin/core-bentley";
8
+ import { BeEvent, BentleyError, BeUiEvent, Logger } from "@itwin/core-bentley";
9
9
  import { Format, FormatterSpec, ParseError, ParserSpec, } from "@itwin/core-quantity";
10
10
  import { FrontendLoggerCategory } from "../common/FrontendLoggerCategory";
11
11
  import { IModelApp } from "../IModelApp";
@@ -135,12 +135,66 @@ class StandardQuantityTypeDefinition {
135
135
  return ParserSpec.create(format, unitsProvider, this.persistenceUnit, alternateUnitLabelsProvider);
136
136
  }
137
137
  }
138
+ /**
139
+ * A default formatsProvider, that provides a limited set of [[FormatDefinition]], associated to a few [[KindOfQuantity]].
140
+ * Maps each KindOfQuantity to a [[QuantityType]].
141
+ * When retrieving a valid [[KindOfQuantity]], returns the [[FormatProps]] for the associated [[QuantityType]].
142
+ * @internal
143
+ */
144
+ export class QuantityTypeFormatsProvider {
145
+ onFormatsChanged = new BeEvent();
146
+ constructor() {
147
+ IModelApp.quantityFormatter.onActiveFormattingUnitSystemChanged.addListener(() => {
148
+ this.onFormatsChanged.raiseEvent({ formatsChanged: "all" });
149
+ });
150
+ }
151
+ _kindOfQuantityMap = new Map([
152
+ ["AecUnits.LENGTH", QuantityType.Length],
153
+ ["AecUnits.ANGLE", QuantityType.Angle],
154
+ ["AecUnits.AREA", QuantityType.Area],
155
+ ["AecUnits.VOLUME", QuantityType.Volume],
156
+ ["RoadRailUnits.STATION", QuantityType.Stationing],
157
+ ["RoadRailUnits.LENGTH", QuantityType.LengthSurvey],
158
+ ]);
159
+ async getFormat(name) {
160
+ const quantityType = this._kindOfQuantityMap.get(name);
161
+ if (!quantityType)
162
+ return undefined;
163
+ return IModelApp.quantityFormatter.getFormatPropsByQuantityType(quantityType);
164
+ }
165
+ }
166
+ /**
167
+ * An implementation of the [[FormatsProvider]] interface that forwards calls to getFormats to the underlying FormatsProvider.
168
+ * Also fires the onFormatsChanged event when the underlying FormatsProvider fires its own onFormatsChanged event.
169
+ * @internal
170
+ */
171
+ export class FormatsProviderManager {
172
+ _formatsProvider;
173
+ onFormatsChanged = new BeEvent();
174
+ constructor(_formatsProvider) {
175
+ this._formatsProvider = _formatsProvider;
176
+ this._formatsProvider.onFormatsChanged.addListener((args) => {
177
+ this.onFormatsChanged.raiseEvent(args);
178
+ });
179
+ }
180
+ async getFormat(name) {
181
+ return this._formatsProvider.getFormat(name);
182
+ }
183
+ get formatsProvider() { return this; }
184
+ set formatsProvider(formatsProvider) {
185
+ this._formatsProvider = formatsProvider;
186
+ this._formatsProvider.onFormatsChanged.addListener((args) => {
187
+ this.onFormatsChanged.raiseEvent(args);
188
+ });
189
+ this.onFormatsChanged.raiseEvent({ formatsChanged: "all" });
190
+ }
191
+ }
138
192
  /** Class that supports formatting quantity values into strings and parsing strings into quantity values. This class also maintains
139
193
  * the "active" unit system and caches FormatterSpecs and ParserSpecs for the "active" unit system to allow synchronous access to
140
194
  * parsing and formatting values. The support unit systems are defined by [[UnitSystemKey]] and is kept in synch with the unit systems
141
195
  * provided by the Presentation Manager on the backend. The QuantityFormatter contains a registry of quantity type definitions. These definitions implement
142
- * the [[QuantityTypeDefinition]] interface, which among other things, provide default [FormatProps]$(core-quantity), and provide methods
143
- * to generate both a [FormatterSpec]$(core-quantity) and a [ParserSpec]$(core-quantity). There are built-in quantity types that are
196
+ * the [[QuantityTypeDefinition]] interface, which among other things, provide default [[FormatProps]], and provide methods
197
+ * to generate both a [[FormatterSpec]] and a [[ParserSpec]]. There are built-in quantity types that are
144
198
  * identified by the [[QuantityType]] enum. [[CustomQuantityTypeDefinition]] can be registered to extend the available quantity types available
145
199
  * by frontend tools. The QuantityFormatter also allows the default formats to be overriden.
146
200
  *
@@ -151,6 +205,10 @@ export class QuantityFormatter {
151
205
  _alternateUnitLabelsRegistry = new AlternateUnitLabelsRegistry(getDefaultAlternateUnitLabels());
152
206
  /** Registry containing available quantity type definitions. */
153
207
  _quantityTypeRegistry = new Map();
208
+ /** Registry containing available FormatterSpec and ParserSpec, mapped by keys.
209
+ * @beta
210
+ */
211
+ _formatSpecsRegistry = new Map();
154
212
  /** Active UnitSystem key - must be one of "imperial", "metric", "usCustomary", or "usSurvey". */
155
213
  _activeUnitSystem = "imperial";
156
214
  /** Map of FormatSpecs for all available QuantityTypes and the active Unit System */
@@ -177,6 +235,7 @@ export class QuantityFormatter {
177
235
  onQuantityFormatsChanged = new BeUiEvent();
178
236
  /** Fired when the active UnitsProvider is updated. This will allow cached Formatter and Parser specs to be updated if necessary. */
179
237
  onUnitsProviderChanged = new BeUiEvent();
238
+ _removeFormatsProviderListener;
180
239
  /**
181
240
  * constructor
182
241
  * @param showMetricOrUnitSystem - Pass in `true` to show Metric formatted quantity values. Defaults to Imperial. To explicitly
@@ -190,6 +249,12 @@ export class QuantityFormatter {
190
249
  this._activeUnitSystem = showMetricOrUnitSystem;
191
250
  }
192
251
  }
252
+ [Symbol.dispose]() {
253
+ if (this._removeFormatsProviderListener) {
254
+ this._removeFormatsProviderListener();
255
+ this._removeFormatsProviderListener = undefined;
256
+ }
257
+ }
193
258
  getOverrideFormatPropsByQuantityType(quantityTypeKey, unitSystem) {
194
259
  const requestedUnitSystem = unitSystem ?? this.activeUnitSystem;
195
260
  const overrideMap = this._overrideFormatPropsByUnitSystem.get(requestedUnitSystem);
@@ -315,6 +380,46 @@ export class QuantityFormatter {
315
380
  */
316
381
  async onInitialized() {
317
382
  await this.initializeQuantityTypesRegistry();
383
+ const initialKoQs = [["AecUnits.LENGTH", "Units.M"], ["AecUnits.ANGLE", "Units.RAD"], ["AecUnits.AREA", "Units.SQ_M"], ["AecUnits.VOLUME", "Units.CUB_M"], ["RoadRailUnits.STATION", "Units.M"], ["RoadRailUnits.LENGTH", "Units.M"]];
384
+ for (const entry of initialKoQs) {
385
+ try {
386
+ await this.addFormattingSpecsToRegistry(entry[0], entry[1]);
387
+ }
388
+ catch (err) {
389
+ Logger.logWarning(`${FrontendLoggerCategory.Package}.QuantityFormatter`, err.toString());
390
+ }
391
+ }
392
+ this._removeFormatsProviderListener = IModelApp.formatsProvider.onFormatsChanged.addListener(async (args) => {
393
+ if (args.formatsChanged === "all") {
394
+ for (const [name, entry] of this._formatSpecsRegistry.entries()) {
395
+ const formatProps = await IModelApp.formatsProvider.getFormat(name);
396
+ if (formatProps) {
397
+ const persistenceUnitName = entry.formatterSpec.persistenceUnit.name;
398
+ await this.addFormattingSpecsToRegistry(name, persistenceUnitName, formatProps);
399
+ }
400
+ else {
401
+ this._formatSpecsRegistry.delete(name); // clear the specs if format was removed, or no longer exists.
402
+ }
403
+ }
404
+ }
405
+ else {
406
+ for (const name of args.formatsChanged) {
407
+ if (this._formatSpecsRegistry.has(name)) {
408
+ const formatProps = await IModelApp.formatsProvider.getFormat(name);
409
+ if (formatProps) {
410
+ const existingEntry = this._formatSpecsRegistry.get(name);
411
+ if (existingEntry) {
412
+ const persistenceUnitName = existingEntry.formatterSpec.persistenceUnit.name;
413
+ await this.addFormattingSpecsToRegistry(name, persistenceUnitName, formatProps);
414
+ }
415
+ }
416
+ else {
417
+ this._formatSpecsRegistry.delete(name);
418
+ }
419
+ }
420
+ }
421
+ }
422
+ });
318
423
  // initialize default format and parsing specs
319
424
  await this.loadFormatAndParsingMapsForSystem();
320
425
  }
@@ -544,26 +649,51 @@ export class QuantityFormatter {
544
649
  requestedSystem = isImperial ? "imperial" : "metric";
545
650
  return this.getParserSpecByQuantityTypeAndSystem(type, requestedSystem);
546
651
  }
547
- /** Generates a formatted string for a quantity given its format spec.
548
- * @param magnitude The magnitude of the quantity.
549
- * @param formatSpec The format specification. See methods getFormatterSpecByQuantityType and findFormatterSpecByQuantityType.
550
- * @return the formatted string.
551
- */
552
- formatQuantity(magnitude, formatSpec) {
553
- /** Format a quantity value. Default FormatterSpec implementation uses Formatter.formatQuantity. */
554
- if (formatSpec)
555
- return formatSpec.applyFormatting(magnitude);
556
- return magnitude.toString();
557
- }
558
- /** Parse input string into quantity given the ParserSpec
559
- * @param inString The magnitude of the quantity.
560
- * @param parserSpec The parse specification the defines the expected format of the string and the conversion to the output unit.
561
- * @return QuantityParseResult object containing either the parsed value or an error value if unsuccessful.
562
- */
563
- parseToQuantityValue(inString, parserSpec) {
564
- if (parserSpec)
565
- return parserSpec.parseToQuantityValue(inString);
566
- return { ok: false, error: ParseError.InvalidParserSpec };
652
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
653
+ formatQuantity(args, spec) {
654
+ if (typeof args === "number") {
655
+ /** Format a quantity value. Default FormatterSpec implementation uses Formatter.formatQuantity. */
656
+ const magnitude = args;
657
+ if (spec)
658
+ return spec.applyFormatting(magnitude);
659
+ return magnitude.toString();
660
+ }
661
+ return this.formatQuantityAsync(args);
662
+ }
663
+ async formatQuantityAsync(args) {
664
+ const { value, valueUnitName, kindOfQuantityName } = args;
665
+ const formatProps = await IModelApp.formatsProvider.getFormat(kindOfQuantityName);
666
+ if (!formatProps)
667
+ return value.toString();
668
+ const formatSpec = await this.createFormatterSpec({
669
+ persistenceUnitName: valueUnitName,
670
+ formatProps,
671
+ formatName: kindOfQuantityName,
672
+ });
673
+ return formatSpec.applyFormatting(value);
674
+ }
675
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
676
+ parseToQuantityValue(args, parserSpec) {
677
+ if (typeof args === "string") {
678
+ /** Parse a quantity value. Default ParserSpec implementation uses ParserSpec.parseToQuantityValue. */
679
+ const inString = args;
680
+ if (parserSpec)
681
+ return parserSpec.parseToQuantityValue(inString);
682
+ return { ok: false, error: ParseError.InvalidParserSpec };
683
+ }
684
+ return this.parseToQuantityValueAsync(args);
685
+ }
686
+ async parseToQuantityValueAsync(args) {
687
+ const { value, valueUnitName, kindOfQuantityName } = args;
688
+ const formatProps = await IModelApp.formatsProvider.getFormat(kindOfQuantityName);
689
+ if (!formatProps)
690
+ return { ok: false, error: ParseError.InvalidParserSpec };
691
+ const parserSpec = await this.createParserSpec({
692
+ persistenceUnitName: valueUnitName,
693
+ formatProps,
694
+ formatName: kindOfQuantityName,
695
+ });
696
+ return parserSpec.parseToQuantityValue(value);
567
697
  }
568
698
  /**
569
699
  * Get a UnitSystemKey from a string that may have been entered via a key-in. Supports different variation of
@@ -633,6 +763,63 @@ export class QuantityFormatter {
633
763
  async getConversion(fromUnit, toUnit) {
634
764
  return this._unitsProvider.getConversion(fromUnit, toUnit);
635
765
  }
766
+ /**
767
+ * Creates a [[FormatterSpec]] for a given persistence unit name and format properties, using the [[UnitsProvider]] to resolve the persistence unit.
768
+ * @beta
769
+ * @param props - A [[CreateFormattingSpecProps]] interface.
770
+ */
771
+ async createFormatterSpec(props) {
772
+ const { persistenceUnitName, formatProps, formatName } = props;
773
+ const persistenceUnitProps = await this._unitsProvider.findUnitByName(persistenceUnitName);
774
+ const format = await Format.createFromJSON(formatName ?? "temp", this._unitsProvider, formatProps);
775
+ return FormatterSpec.create(`${format.name}_format_spec`, format, this._unitsProvider, persistenceUnitProps);
776
+ }
777
+ /**
778
+ * Creates a [[ParserSpec]] for a given persistence unit name and format properties, using the [[UnitsProvider]] to resolve the persistence unit.
779
+ * @beta
780
+ * @param props - A [[CreateFormattingSpecProps]] object.
781
+ */
782
+ async createParserSpec(props) {
783
+ const { persistenceUnitName, formatProps, formatName } = props;
784
+ const persistenceUnitProps = await this._unitsProvider.findUnitByName(persistenceUnitName);
785
+ const format = await Format.createFromJSON(formatName ?? "temp", this._unitsProvider, formatProps);
786
+ return ParserSpec.create(format, this._unitsProvider, persistenceUnitProps);
787
+ }
788
+ /**
789
+ * @beta
790
+ * Returns a [[FormattingSpecEntry]] for a given name, typically a KindOfQuantity full name.
791
+ */
792
+ getSpecsByName(name) {
793
+ return this._formatSpecsRegistry.get(name);
794
+ }
795
+ /**
796
+ * Populates the registry with a new FormatterSpec and ParserSpec entry for the given format name.
797
+ * @beta
798
+ * @param name The key used to identify the formatter and parser spec
799
+ * @param persistenceUnitName The name of the persistence unit
800
+ * @param formatProps If not supplied, tries to retrieve the [[FormatProps]] from [[IModelApp.formatsProvider]]
801
+ */
802
+ async addFormattingSpecsToRegistry(name, persistenceUnitName, formatProps) {
803
+ if (!formatProps) {
804
+ formatProps = await IModelApp.formatsProvider.getFormat(name);
805
+ }
806
+ if (formatProps) {
807
+ const formatterSpec = await this.createFormatterSpec({
808
+ persistenceUnitName,
809
+ formatProps,
810
+ formatName: name,
811
+ });
812
+ const parserSpec = await this.createParserSpec({
813
+ persistenceUnitName,
814
+ formatProps,
815
+ formatName: name,
816
+ });
817
+ this._formatSpecsRegistry.set(name, { formatterSpec, parserSpec });
818
+ }
819
+ else {
820
+ throw new Error(`Unable to find format properties for ${name} with persistence unit ${persistenceUnitName}`);
821
+ }
822
+ }
636
823
  }
637
824
  // ========================================================================================================================================
638
825
  // Default Data