Blockbench Reference Docs
    Preparing search index...
    SharedActions: {
        actions: Record<string, SharedActionHandler[]>;
        add(action_id: string, handler: SharedActionHandler): Deletable;
        condition(action_id: string): boolean;
        find(action_id: string, event?: Event, context?: any): SharedActionHandler;
        run(action_id: string, event?: Event, context?: any): boolean;
        runSpecific(
            action_id: string,
            subject: string,
            event?: Event,
            context?: any,
            force?: boolean,
        ): boolean;
    }

    Shared Actions is a system in Blockbench to allow actions (including in toolbars, menus, via action control, or keybinding) to run different code in different cases, such as in different modes or different panels. As an example, the "Duplicate" action runs code to duplicate elements when used in the outliner, and duplicates textures when used in the textures panel.

    Handlers can be added for existing actions like this:

       // Duplicate layers when using "Duplicate" in the layers panel
    SharedActions.add('duplicate', {
    subject: 'layer',
    condition: () => Prop.active_panel == 'layers' && TextureLayer.selected,
    run() {
    let texture = Texture.selected;
    let original = texture.getActiveLayer();
    let copy = original.getUndoCopy(true);
    copy.name += '-copy';
    Undo.initEdit({textures: [texture]});
    let layer = new TextureLayer(copy, texture);
    layer.addForEditing();
    Undo.finishEdit('Duplicate layer');
    }
    })

    Type Declaration

    • actions: Record<string, SharedActionHandler[]>
    • add: function
      • Add a method to handle a specific use case of a shared action

        Parameters

        Returns Deletable

    • condition: function
      • Check if there is an active and available handler in the current situation for a shared action

        Parameters

        • action_id: string

        Returns boolean

    • find: function
      • Find the active handler in the current situation for a shared action

        Parameters

        • action_id: string
        • Optionalevent: Event
        • Optionalcontext: any

        Returns SharedActionHandler

    • run: function
      • Run the active handler for a specific subject manually

        Parameters

        • action_id: string

          Action ID

        • Optionalevent: Event

          Event that triggered the interaction

        • Optionalcontext: any

          Optional context variable

        Returns boolean

    • runSpecific: function
      • Run a specific handler manually

        Parameters

        • action_id: string

          Action ID

        • subject: string

          Subject to run on

        • Optionalevent: Event

          Event that triggered the interaction

        • Optionalcontext: any

          Optional context variable

        • Optionalforce: boolean

          Force the specified handler to run and ignore its condition

        Returns boolean