ThreePipe
    Preparing search index...

    Class ViewerActions

    The actions of a viewer (viewer.actions): a registry of ViewerActions, each with an id, a label, an icon, a shortcut, a group, enabled() and run(). Plugins register their actions and keys once (in onAdded, and remove them in onRemove); any UI (a toolbar, a context menu, a command palette, a sheet of the keys) lists them with list, shows their keys with formatShortcut and runs them with run. Each viewer has one, and it adds no element to the page. Its uiConfig (an Actions folder: the keys on or off, and the actions with their keys) shows only where an app or a UI adds it.

    • A keydown listener on the window runs the shortcuts of the actions, while it has actions. It leaves a key typed in a text field (an input, a text area, a select, an editable element) and a key that another handler used (defaultPrevented). It calls preventDefault only when an action runs. When two enabled actions have the same key, the one with the higher ViewerAction.priority runs, then the one registered last.
    • The app controls the keys here, in one place: setShortcut changes or removes the key of an action, and enabled turns all the keys off (for an app with its own keymap; run still runs an action).
    • With more viewers on the page, a key is for the viewer that has the focus, else for the one used last (viewerTakesKey).
    • actionRun tells the result of each run (ViewerActionResult), and actionsChange tells that the list or a key changed. The error and the warn of a result show in a toast (toastResults, viewer.dialog.toast).
    • suspend and resume turn the keys off for a while (an app's own modal UI); while the page has an open modal <dialog> the keys are also off, without code (suspended).
    • Without a viewer (new ViewerActions()), for a UI that is not part of one: it takes every key and shows no toast.
    const unregister = viewer.actions.register({
    id: 'resetCamera', label: 'Reset camera', shortcut: 'Shift+R', group: 'Camera',
    run: () => {
    viewer.scene.mainCamera.position.set(0, 0, 5)
    viewer.scene.mainCamera.setDirty()
    },
    })
    viewer.actions.setShortcut('resetCamera', ['Shift+R', 'Home']) // the app sets the keys here
    viewer.actions.list('Camera').map(a => a.label + ' ' + shortcutsOf(viewer.actions.shortcutOf(a.id)).map(k => formatShortcut(k)).join(', '))
    await viewer.actions.run('resetCamera')

    Hierarchy

    Index
    enabled: boolean = true

    The keys run the actions. Off: no key runs an action (run still does).

    toastResults: boolean = true

    Show the error and the warn of each run of an action in a toast (viewer.dialog.toast)

    uiConfig: UiObjectConfig = ...

    For a settings panel: the keys on or off, the toasts on or off, and a button for each action with its key

    viewer?: ThreeViewer

    The viewer, or undefined for actions that are not part of one (they take every key)

    • get suspended(): boolean

      No key runs an action now: a suspend is not resumed, or the page has an open modal <dialog> (one opened with showModal()) that does not hold the container of the viewer. Not enabled, which the app sets.

      Returns boolean

    • Runs an action and sends actionRun with its result. An action that throws has the error in its result.

      Parameters

      • id: string

      Returns Promise<ViewerActionResult | undefined>

      the result, or undefined when there is no such action or it is not enabled (it does not run then)

    • Changes the key of an action, also of one that is registered later or again. It is the one place where an app sets the keys: another key, more keys, or none.

      Parameters

      • id: string

        the id of the action

      • shortcut: string | string[] | null | undefined

        the key or the keys; null: no key; undefined: the key of the action again

      Returns void

    • The key or the keys of an action now: the one set with setShortcut, else its own; undefined: no key

      Parameters

      • id: string

      Returns string | string[] | undefined

    • Turns the keys off until resume (for a modal UI that is not a <dialog>). The calls are counted: the keys run again after one resume for each suspend. run still runs an action.

      Returns void