UNPKG

@itwin/frontend-devtools

Version:

Debug menu and supporting UI widgets

126 lines 4.9 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Tools */ import { BentleyError } from "@itwin/core-bentley"; import { IModelApp, NotifyMessageDetails, OutputMessagePriority, Tool, } from "@itwin/core-frontend"; import { copyStringToClipboard } from "../ClipboardUtilities"; import { parseArgs } from "./parseArgs"; /** Serialize a ViewState to JSON. The returned JSON can later be passed to [deserializeViewState] to reinstantiate the ViewState. * @beta */ export function serializeViewState(view) { return view.toProps(); } /** Instantiate a ViewState serialized by [serializeViewState]. * @beta */ export async function deserializeViewState(props, iModel) { const ctor = await iModel.findClassFor(props.viewDefinitionProps.classFullName, undefined); if (undefined === ctor) throw new Error("Class not found"); const view = ctor.createFromProps(props, iModel); if (undefined === view) throw new Error("Failed to construct ViewState"); await view.load(); return view; } /** Copies a JSON representation of the active viewport's view to the clipboard. * * Arguments: * * `quote`: format the JSON so it can be parsed directly by [ApplyViewTool]. * @beta */ export class SaveViewTool extends Tool { _quote = false; static get minArgs() { return 0; } static get maxArgs() { return 1; } static toolId = "SaveView"; parse(inputArgs) { const args = parseArgs(inputArgs); function getArg(name) { return args.getBoolean(name) ? true : undefined; } this._quote = true === getArg("q"); return true; } async parseAndRun(...args) { if (this.parse(args)) return this.run(); else return false; } async run() { const vp = IModelApp.viewManager.selectedView; if (undefined === vp) { IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, "No viewport")); return true; } try { let json = JSON.stringify(serializeViewState(vp.view)); if (this._quote) json = `"${json.replace(/"/g, '""')}"`; copyStringToClipboard(json); IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, "JSON copied to clipboard")); } catch (err) { IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, BentleyError.getErrorMessage(err) || "An unknown error occurred.")); } return true; } } /** Given a string containing a JSON representation of a ViewState, applies that ViewState to the active viewport. * The JSON string should be enclosed in double quotes and embedded double quote should be duplicated, example: * - "{""viewDefinitionProps"":{""classFullName"":""BisCore:SpatialViewDefinition"",""id"":""0x1a""}}" * @beta */ export class ApplyViewTool extends Tool { static toolId = "ApplyView"; static get maxArgs() { return 1; } static get minArgs() { return 1; } async run(view) { const vp = IModelApp.viewManager.selectedView; if (undefined !== view && undefined !== vp) vp.changeView(view); return true; } async parseAndRun(...args) { const vp = IModelApp.viewManager.selectedView; if (undefined === vp || 0 === args.length) return true; try { const json = JSON.parse(args[0]); const view = await deserializeViewState(json, vp.iModel); await this.run(view); } catch (err) { IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, BentleyError.getErrorMessage(err) || "An unknown error occurred.")); } return true; } } /** Given the Id of a persistent ViewDefinition, applies that view to the active viewport. * @beta */ export class ApplyViewByIdTool extends Tool { static toolId = "ApplyViewById"; static get minArgs() { return 1; } static get maxArgs() { return 1; } async parseAndRun(...args) { return this.run(args[0]); } async run(viewId) { if (typeof viewId !== "string") return false; const vp = IModelApp.viewManager.selectedView; if (!vp) return false; vp.iModel.views.load(viewId).then((view) => { vp.changeView(view); }).catch(() => { }); return true; } } //# sourceMappingURL=SavedViews.js.map