UNPKG

forma-embedded-view-sdk

Version:

The Forma Embedded View SDK is a JavaScript library for creating custom extensions in Autodesk Forma Site Design (previously Spacemaker).

88 lines (87 loc) 2.92 kB
const hexColorPattern = /^#[0-9A-Fa-f]{6}$/; function assertValidHexColorString(color) { if (color === undefined) { return; } if (!hexColorPattern.test(color)) { throw new Error('Invalid color: expected a 6-digit hex color string like "#FF5733"'); } } /** * Manage project-level building functions. * * @remarks * Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.settings | settings}.buildingFunctions. */ export class BuildingFunctionsApi { #iframeMessenger; /** @hidden */ constructor(iframeMessenger) { this.#iframeMessenger = iframeMessenger; } /** * Add a new building function to the project. * * @returns All building functions after the addition. * * @example * const { buildingFunctions } = await Forma.settings.buildingFunctions.add({ name: "Retail", color: "#FF5733" }) */ async add(params) { assertValidHexColorString(params.color); return await this.#iframeMessenger.sendRequest("settings/add", params); } /** * Update an existing project-level building function by ID. * Returns an error if the ID belongs to a built-in building function. * * @returns All building functions after the update. * * @example * const { buildingFunctions } = await Forma.settings.buildingFunctions.update({ id: "abc123", name: "Updated name" }) */ async update(params) { assertValidHexColorString(params.color); return await this.#iframeMessenger.sendRequest("settings/update", params); } /** * Delete a project-level building function by ID. * Returns an error if the ID belongs to a built-in building function. * * @returns All building functions after the deletion. * * @example * const { buildingFunctions } = await Forma.settings.buildingFunctions.delete({ id: "abc123" }) */ async delete(params) { return await this.#iframeMessenger.sendRequest("settings/delete", params); } } /** * Access and manage project-level settings. * * @remarks * Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.settings | settings}. */ export class SettingsApi { #iframeMessenger; /** Manage building functions for the project. */ buildingFunctions; /** @hidden */ constructor(iframeMessenger) { this.#iframeMessenger = iframeMessenger; this.buildingFunctions = new BuildingFunctionsApi(iframeMessenger); } /** * Fetch all building functions for the project, including the three built-in * defaults (residential, commercial, unspecified). * * @returns All building functions for the project. * * @example * const { buildingFunctions } = await Forma.settings.get() */ async get() { return await this.#iframeMessenger.sendRequest("settings/get"); } }