@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
134 lines • 5.59 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplyViewByIdTool = exports.ApplyViewTool = exports.SaveViewTool = void 0;
exports.serializeViewState = serializeViewState;
exports.deserializeViewState = deserializeViewState;
/** @packageDocumentation
* @module Tools
*/
const core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const ClipboardUtilities_1 = require("../ClipboardUtilities");
const parseArgs_1 = require("./parseArgs");
/** Serialize a ViewState to JSON. The returned JSON can later be passed to [deserializeViewState] to reinstantiate the ViewState.
* @beta
*/
function serializeViewState(view) {
return view.toProps();
}
/** Instantiate a ViewState serialized by [serializeViewState].
* @beta
*/
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
*/
class SaveViewTool extends core_frontend_1.Tool {
_quote = false;
static get minArgs() { return 0; }
static get maxArgs() { return 1; }
static toolId = "SaveView";
parse(inputArgs) {
const args = (0, parseArgs_1.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 = core_frontend_1.IModelApp.viewManager.selectedView;
if (undefined === vp) {
core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, "No viewport"));
return true;
}
try {
let json = JSON.stringify(serializeViewState(vp.view));
if (this._quote)
json = `"${json.replace(/"/g, '""')}"`;
(0, ClipboardUtilities_1.copyStringToClipboard)(json);
core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, "JSON copied to clipboard"));
}
catch (err) {
core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, core_bentley_1.BentleyError.getErrorMessage(err) || "An unknown error occurred."));
}
return true;
}
}
exports.SaveViewTool = SaveViewTool;
/** 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
*/
class ApplyViewTool extends core_frontend_1.Tool {
static toolId = "ApplyView";
static get maxArgs() { return 1; }
static get minArgs() { return 1; }
async run(view) {
const vp = core_frontend_1.IModelApp.viewManager.selectedView;
if (undefined !== view && undefined !== vp)
vp.changeView(view);
return true;
}
async parseAndRun(...args) {
const vp = core_frontend_1.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) {
core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, core_bentley_1.BentleyError.getErrorMessage(err) || "An unknown error occurred."));
}
return true;
}
}
exports.ApplyViewTool = ApplyViewTool;
/** Given the Id of a persistent ViewDefinition, applies that view to the active viewport.
* @beta
*/
class ApplyViewByIdTool extends core_frontend_1.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 = core_frontend_1.IModelApp.viewManager.selectedView;
if (!vp)
return false;
vp.iModel.views.load(viewId).then((view) => {
vp.changeView(view);
}).catch(() => { });
return true;
}
}
exports.ApplyViewByIdTool = ApplyViewByIdTool;
//# sourceMappingURL=SavedViews.js.map