Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions common/api/ecschema-metadata.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FormatsProvider } from '@itwin/core-quantity';
import { FormatTraits } from '@itwin/core-quantity';
import { FormatType } from '@itwin/core-quantity';
import { FractionalPrecision } from '@itwin/core-quantity';
import { MutableFormatsProvider } from '@itwin/core-quantity';
import { ScientificType } from '@itwin/core-quantity';
import { ShowSignOption } from '@itwin/core-quantity';
import { UnitConversionProps } from '@itwin/core-quantity';
Expand Down Expand Up @@ -808,6 +809,24 @@ export interface FormatSet {
name: string;
}

// @beta
export class FormatSetFormatsProvider implements MutableFormatsProvider {
constructor(props: {
formatSet: FormatSet;
fallbackProvider?: FormatsProvider;
});
// (undocumented)
addFormat(name: string, format: FormatDefinition): Promise<void>;
// (undocumented)
clearFallbackProvider(): void;
// (undocumented)
getFormat(input: string): Promise<FormatDefinition | undefined>;
// (undocumented)
onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;
// (undocumented)
removeFormat(name: string): Promise<void>;
}

// @internal (undocumented)
export function getFormatProps(format: Format | OverrideFormat): FormatProps;

Expand Down
1 change: 1 addition & 0 deletions common/api/summary/ecschema-metadata.exports.csv
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ preview;interface;EnumeratorProps
public;class;Format
preview;class;Format
beta;interface;FormatSet
beta;class;FormatSetFormatsProvider
internal;function;getFormatProps
public;interface;HasMixins
preview;interface;HasMixins
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/ecschema-metadata",
"comment": "Add FormatSetFormatsProvider",
"type": "none"
}
],
"packageName": "@itwin/ecschema-metadata"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { BeEvent } from "@itwin/core-bentley";
import { FormatDefinition, FormatsChangedArgs, FormatsProvider, MutableFormatsProvider } from "@itwin/core-quantity";
import { FormatSet } from "../Deserialization/JsonProps";
import { SchemaItem } from "../Metadata/SchemaItem";

/**
* A mutable format provider that manages format definitions within a format set.
* When formats are added or removed, the underlying format set is automatically updated.
* @beta
*/
export class FormatSetFormatsProvider implements MutableFormatsProvider {
public onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void> = new BeEvent<(args: FormatsChangedArgs) => void>();

private _formatSet: FormatSet;
private _fallbackProvider?: FormatsProvider;

public constructor(props: {formatSet: FormatSet, fallbackProvider?: FormatsProvider}) {
this._formatSet = props.formatSet;
this._fallbackProvider = props.fallbackProvider;
}

public async addFormat(name: string, format: FormatDefinition): Promise<void> {
this._formatSet.formats[name] = format;
this.onFormatsChanged.raiseEvent({ formatsChanged: [name] });
}

public clearFallbackProvider(): void {
this._fallbackProvider = undefined;
}

public async getFormat(input: string): Promise<FormatDefinition | undefined> {
// Normalizes any schemaItem names coming from node addon 'schemaName:schemaItemName' -> 'schemaName.schemaItemName'
const [schemaName, itemName] = SchemaItem.parseFullName(input);

const name = (schemaName === "") ? itemName : `${schemaName}.${itemName}`;
const format = this._formatSet.formats[name];
if (format) return format;
if (this._fallbackProvider) return this._fallbackProvider.getFormat(name);
return undefined;
}

public async removeFormat(name: string): Promise<void> {
delete this._formatSet.formats[name];
this.onFormatsChanged.raiseEvent({ formatsChanged: [name] });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
* @module Metadata
*/

import { ISchemaLocater, SchemaContext } from "./Context";
import { SchemaItemKey, SchemaKey } from "./SchemaKey";
import { SchemaItem } from "./Metadata/SchemaItem";
import { Format } from "./Metadata/Format";
import { SchemaItemFormatProps } from "./Deserialization/JsonProps";
import { ISchemaLocater, SchemaContext } from "../Context";
import { SchemaItemKey, SchemaKey } from "../SchemaKey";
import { SchemaItem } from "../Metadata/SchemaItem";
import { Format } from "../Metadata/Format";
import { SchemaItemFormatProps } from "../Deserialization/JsonProps";
import { BeEvent, Logger } from "@itwin/core-bentley";
import { KindOfQuantity } from "./Metadata/KindOfQuantity";
import { getFormatProps } from "./Metadata/OverrideFormat";
import { KindOfQuantity } from "../Metadata/KindOfQuantity";
import { getFormatProps } from "../Metadata/OverrideFormat";
import { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from "@itwin/core-quantity";
import { Unit } from "./Metadata/Unit";
import { InvertedUnit } from "./Metadata/InvertedUnit";
import { Schema } from "./Metadata/Schema";
import { UnitSystem } from "./Metadata/UnitSystem";
import { Unit } from "../Metadata/Unit";
import { InvertedUnit } from "../Metadata/InvertedUnit";
import { Schema } from "../Metadata/Schema";
import { UnitSystem } from "../Metadata/UnitSystem";
const loggerCategory = "SchemaFormatsProvider";
/**
* Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.
Expand Down
3 changes: 2 additions & 1 deletion core/ecschema-metadata/src/ecschema-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export * from "./UnitConversion/UnitConverter";
export * from "./UnitProvider/SchemaUnitProvider";
export * from "./Validation/SchemaWalker";
export * from "./SchemaPartVisitorDelegate";
export * from "./SchemaFormatsProvider";
export * from "./Formatting/SchemaFormatsProvider";
export * from "./Formatting/FormatSetFormatsProvider";
export * from "./IncrementalLoading/ECSqlSchemaLocater";
export * from "./IncrementalLoading/IncrementalSchemaLocater";
export { CustomAttribute, CustomAttributeContainerProps} from "./Metadata/CustomAttribute";
Expand Down
Loading