@itwin/core-frontend 5.1.0-dev.13 → 5.1.0-dev.14

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 (53) 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 +19 -1
  4. package/lib/cjs/IModelApp.js.map +1 -1
  5. package/lib/cjs/IpcApp.d.ts +1 -0
  6. package/lib/cjs/IpcApp.d.ts.map +1 -1
  7. package/lib/cjs/IpcApp.js +3 -1
  8. package/lib/cjs/IpcApp.js.map +1 -1
  9. package/lib/cjs/internal/tile/ClassifierTileTree.js +3 -1
  10. package/lib/cjs/internal/tile/ClassifierTileTree.js.map +1 -1
  11. package/lib/cjs/internal/tile/PrimaryTileTree.d.ts.map +1 -1
  12. package/lib/cjs/internal/tile/PrimaryTileTree.js +4 -3
  13. package/lib/cjs/internal/tile/PrimaryTileTree.js.map +1 -1
  14. package/lib/cjs/quantity-formatting/QuantityFormatter.d.ts +76 -7
  15. package/lib/cjs/quantity-formatting/QuantityFormatter.d.ts.map +1 -1
  16. package/lib/cjs/quantity-formatting/QuantityFormatter.js +126 -23
  17. package/lib/cjs/quantity-formatting/QuantityFormatter.js.map +1 -1
  18. package/lib/cjs/tile/TileAdmin.d.ts +10 -0
  19. package/lib/cjs/tile/TileAdmin.d.ts.map +1 -1
  20. package/lib/cjs/tile/TileAdmin.js +3 -0
  21. package/lib/cjs/tile/TileAdmin.js.map +1 -1
  22. package/lib/cjs/tools/MeasureTool.d.ts +9 -0
  23. package/lib/cjs/tools/MeasureTool.d.ts.map +1 -1
  24. package/lib/cjs/tools/MeasureTool.js +53 -22
  25. package/lib/cjs/tools/MeasureTool.js.map +1 -1
  26. package/lib/esm/IModelApp.d.ts +15 -0
  27. package/lib/esm/IModelApp.d.ts.map +1 -1
  28. package/lib/esm/IModelApp.js +20 -2
  29. package/lib/esm/IModelApp.js.map +1 -1
  30. package/lib/esm/IpcApp.d.ts +1 -0
  31. package/lib/esm/IpcApp.d.ts.map +1 -1
  32. package/lib/esm/IpcApp.js +3 -1
  33. package/lib/esm/IpcApp.js.map +1 -1
  34. package/lib/esm/internal/tile/ClassifierTileTree.js +3 -1
  35. package/lib/esm/internal/tile/ClassifierTileTree.js.map +1 -1
  36. package/lib/esm/internal/tile/PrimaryTileTree.d.ts.map +1 -1
  37. package/lib/esm/internal/tile/PrimaryTileTree.js +4 -3
  38. package/lib/esm/internal/tile/PrimaryTileTree.js.map +1 -1
  39. package/lib/esm/quantity-formatting/QuantityFormatter.d.ts +76 -7
  40. package/lib/esm/quantity-formatting/QuantityFormatter.d.ts.map +1 -1
  41. package/lib/esm/quantity-formatting/QuantityFormatter.js +124 -23
  42. package/lib/esm/quantity-formatting/QuantityFormatter.js.map +1 -1
  43. package/lib/esm/tile/TileAdmin.d.ts +10 -0
  44. package/lib/esm/tile/TileAdmin.d.ts.map +1 -1
  45. package/lib/esm/tile/TileAdmin.js +3 -0
  46. package/lib/esm/tile/TileAdmin.js.map +1 -1
  47. package/lib/esm/tools/MeasureTool.d.ts +9 -0
  48. package/lib/esm/tools/MeasureTool.d.ts.map +1 -1
  49. package/lib/esm/tools/MeasureTool.js +53 -22
  50. package/lib/esm/tools/MeasureTool.js.map +1 -1
  51. package/lib/public/scripts/parse-imdl-worker.js +1 -1
  52. package/lib/workers/webpack/parse-imdl-worker.js +1 -1
  53. 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
  *
@@ -544,26 +598,51 @@ export class QuantityFormatter {
544
598
  requestedSystem = isImperial ? "imperial" : "metric";
545
599
  return this.getParserSpecByQuantityTypeAndSystem(type, requestedSystem);
546
600
  }
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 };
601
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
602
+ formatQuantity(args, spec) {
603
+ if (typeof args === "number") {
604
+ /** Format a quantity value. Default FormatterSpec implementation uses Formatter.formatQuantity. */
605
+ const magnitude = args;
606
+ if (spec)
607
+ return spec.applyFormatting(magnitude);
608
+ return magnitude.toString();
609
+ }
610
+ return this.formatQuantityAsync(args);
611
+ }
612
+ async formatQuantityAsync(args) {
613
+ const { value, valueUnitName, kindOfQuantityName } = args;
614
+ const formatProps = await IModelApp.formatsProvider.getFormat(kindOfQuantityName);
615
+ if (!formatProps)
616
+ return value.toString();
617
+ const formatSpec = await this.createFormatterSpec({
618
+ persistenceUnitName: valueUnitName,
619
+ formatProps,
620
+ formatName: kindOfQuantityName,
621
+ });
622
+ return formatSpec.applyFormatting(value);
623
+ }
624
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
625
+ parseToQuantityValue(args, parserSpec) {
626
+ if (typeof args === "string") {
627
+ /** Parse a quantity value. Default ParserSpec implementation uses ParserSpec.parseToQuantityValue. */
628
+ const inString = args;
629
+ if (parserSpec)
630
+ return parserSpec.parseToQuantityValue(inString);
631
+ return { ok: false, error: ParseError.InvalidParserSpec };
632
+ }
633
+ return this.parseToQuantityValueAsync(args);
634
+ }
635
+ async parseToQuantityValueAsync(args) {
636
+ const { value, valueUnitName, kindOfQuantityName } = args;
637
+ const formatProps = await IModelApp.formatsProvider.getFormat(kindOfQuantityName);
638
+ if (!formatProps)
639
+ return { ok: false, error: ParseError.InvalidParserSpec };
640
+ const parserSpec = await this.createParserSpec({
641
+ persistenceUnitName: valueUnitName,
642
+ formatProps,
643
+ formatName: kindOfQuantityName,
644
+ });
645
+ return parserSpec.parseToQuantityValue(value);
567
646
  }
568
647
  /**
569
648
  * Get a UnitSystemKey from a string that may have been entered via a key-in. Supports different variation of
@@ -633,6 +712,28 @@ export class QuantityFormatter {
633
712
  async getConversion(fromUnit, toUnit) {
634
713
  return this._unitsProvider.getConversion(fromUnit, toUnit);
635
714
  }
715
+ /**
716
+ * Creates a [[FormatterSpec]] for a given persistence unit name and format properties, using the [[UnitsProvider]] to resolve the persistence unit.
717
+ * @beta
718
+ * @param props - A [[CreateFormattingSpecProps]] interface.
719
+ */
720
+ async createFormatterSpec(props) {
721
+ const { persistenceUnitName, formatProps, formatName } = props;
722
+ const persistenceUnitProps = await this._unitsProvider.findUnitByName(persistenceUnitName);
723
+ const format = await Format.createFromJSON(formatName ?? "temp", this._unitsProvider, formatProps);
724
+ return FormatterSpec.create(`${format.name}_format_spec`, format, this._unitsProvider, persistenceUnitProps);
725
+ }
726
+ /**
727
+ * Creates a [[ParserSpec]] for a given persistence unit name and format properties, using the [[UnitsProvider]] to resolve the persistence unit.
728
+ * @beta
729
+ * @param props - A [[CreateFormattingSpecProps]] object.
730
+ */
731
+ async createParserSpec(props) {
732
+ const { persistenceUnitName, formatProps, formatName } = props;
733
+ const persistenceUnitProps = await this._unitsProvider.findUnitByName(persistenceUnitName);
734
+ const format = await Format.createFromJSON(formatName ?? "temp", this._unitsProvider, formatProps);
735
+ return ParserSpec.create(format, this._unitsProvider, persistenceUnitProps);
736
+ }
636
737
  }
637
738
  // ========================================================================================================================================
638
739
  // Default Data