packageDocumentation module import { IModelStatus } from "@itwin/core-bentley" ; import { IModelDb , IpcHandler , IpcHost } from "@itwin/core-backend" ; import { IModelError } from "@itwin/core-common" ; import { EditCommandIpc , editorChannel , EditorIpc } from "@itwin/editor-common" ; alpha export type EditCommandType = typeof EditCommand ; alpha export class EditCommand implements EditCommandIpc { /** The unique string that identifies this EditCommand class. This must be overridden in every subclass. */ public static commandId = "" ; public static version = "1.0.0" ; /** The iModel this EditCommand may modify. */ public readonly iModel : IModelDb ; public constructor ( iModel , ... _args ) { this . iModel = iModel ; } public get ctor ( ) : EditCommandType { return this . constructor as EditCommandType ; } public async onStart ( ) { } public async ping ( ) { return { version : this . ctor . version , commandId : this . ctor . commandId } ; } public onCleanup ( ) { } public onFinish ( ) { } } class EditorAppHandler extends IpcHandler implements EditorIpc { public get channelName ( ) { return editorChannel ; } public async startCommand ( commandId , iModelKey , ... args ) { const commandClass = EditCommandAdmin . commands . get ( commandId ) ; if ( undefined === commandClass ) throw new IModelError ( IModelStatus . NotRegistered , `Command not registered [${ commandId }]` ) ; return EditCommandAdmin . runCommand ( new commandClass ( IModelDb . findByKey ( iModelKey ) , ... args ) ) ; } public async callMethod ( methodName , ... args ) { const cmd = EditCommandAdmin . activeCommand ; if ( ! cmd ) throw new IModelError ( IModelStatus . NoActiveCommand , `No active command` ) ; const func = ( cmd as any ) [ methodName ] ; if ( typeof func !== "function" ) throw new IModelError ( IModelStatus . FunctionNotFound , `Method ${ methodName } not found on ${ cmd . ctor . commandId }` ) ; return func . call ( cmd , ... args ) ; } } alpha export class EditCommandAdmin { public static readonly commands = new Map < string , EditCommandType > ( ) ; private static _activeCommand ? : EditCommand ; private static _isInitialized = false ; public static get activeCommand ( ) { return this . _activeCommand ; } public static runCommand ( cmd ? ) { if ( this . _activeCommand ) this . _activeCommand . onFinish ( ) ; this . _activeCommand = cmd ; return cmd ? cmd . onStart ( ) : undefined ; } param commandId public static unRegister ( commandId ) { this . commands . delete ( commandId ) ; } param commandType public static register ( commandType ) { if ( ! this . _isInitialized ) { this . _isInitialized = true ; if ( ! IpcHost . isValid ) throw new Error ( "Edit Commands require IpcHost" ) ; EditorAppHandler . register ( ) ; } if ( commandType . commandId . length !== 0 ) this . commands . set ( commandType . commandId , commandType ) ; } param modelObj public static registerModule ( moduleObj ) { let foundOne = false ; for ( const thisMember in moduleObj ) { const thisCmd = moduleObj [ thisMember ] ; if ( thisCmd . prototype instanceof EditCommand ) { foundOne = true ; this . register ( thisCmd ) ; } } if ( ! foundOne ) throw new Error ( `no EditCommands found - are you sure this is a module? Maybe you meant to call "register"?` ) ; } }