|
| 1 | +import type { ManifestPreviewOption } from '../preview-option/preview-option.extension.js'; |
| 2 | +import { customElement, html, property, state, when } from '@umbraco-cms/backoffice/external/lit'; |
| 3 | +import { UmbActionExecutedEvent } from '@umbraco-cms/backoffice/event'; |
| 4 | +import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry'; |
| 5 | +import { UmbExtensionsElementAndApiInitializer } from '@umbraco-cms/backoffice/extension-api'; |
| 6 | +import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; |
| 7 | +import type { UmbWorkspaceAction } from '@umbraco-cms/backoffice/workspace'; |
| 8 | +import type { UmbExtensionElementAndApiInitializer } from '@umbraco-cms/backoffice/extension-api'; |
| 9 | +import type { UUIButtonState } from '@umbraco-cms/backoffice/external/uui'; |
| 10 | + |
| 11 | +@customElement('umb-save-and-preview-workspace-action') |
| 12 | +export class UmbSaveAndPreviewWorkspaceActionElement extends UmbLitElement { |
| 13 | + #buttonStateResetTimeoutId: number | null = null; |
| 14 | + |
| 15 | + #extensionsController?: UmbExtensionsElementAndApiInitializer< |
| 16 | + ManifestPreviewOption, |
| 17 | + 'previewOption', |
| 18 | + ManifestPreviewOption |
| 19 | + >; |
| 20 | + |
| 21 | + @property({ type: Object, attribute: false }) |
| 22 | + public set manifest(value: ManifestPreviewOption | undefined) { |
| 23 | + if (!value) return; |
| 24 | + const oldValue = this.#manifest; |
| 25 | + if (oldValue !== value) { |
| 26 | + this.#manifest = value; |
| 27 | + this.#observeExtensions(); |
| 28 | + } |
| 29 | + } |
| 30 | + public get manifest() { |
| 31 | + return this.#manifest; |
| 32 | + } |
| 33 | + #manifest?: ManifestPreviewOption; |
| 34 | + |
| 35 | + @property({ attribute: false }) |
| 36 | + public set api(api: UmbWorkspaceAction | undefined) { |
| 37 | + this.#api = api; |
| 38 | + this.#observeIsDisabled(); |
| 39 | + } |
| 40 | + public get api(): UmbWorkspaceAction | undefined { |
| 41 | + return this.#api; |
| 42 | + } |
| 43 | + #api?: UmbWorkspaceAction; |
| 44 | + |
| 45 | + @state() |
| 46 | + private _buttonState?: UUIButtonState; |
| 47 | + |
| 48 | + @state() |
| 49 | + private _isDisabled = false; |
| 50 | + |
| 51 | + @state() |
| 52 | + private _actions: Array<UmbExtensionElementAndApiInitializer<ManifestPreviewOption>> = []; |
| 53 | + |
| 54 | + private _primaryAction?: UmbExtensionElementAndApiInitializer<ManifestPreviewOption>; |
| 55 | + |
| 56 | + async #onClick() { |
| 57 | + this._buttonState = 'waiting'; |
| 58 | + |
| 59 | + try { |
| 60 | + if (!this._primaryAction?.api) throw new Error('No api defined'); |
| 61 | + await this._primaryAction.api.execute().catch(() => {}); |
| 62 | + this._buttonState = 'success'; |
| 63 | + } catch (reason) { |
| 64 | + if (reason) { |
| 65 | + console.warn(reason); |
| 66 | + } |
| 67 | + this._buttonState = 'failed'; |
| 68 | + } |
| 69 | + |
| 70 | + this.#initButtonStateReset(); |
| 71 | + this.dispatchEvent(new UmbActionExecutedEvent()); |
| 72 | + } |
| 73 | + |
| 74 | + #observeIsDisabled() { |
| 75 | + this.observe( |
| 76 | + this.#api?.isDisabled, |
| 77 | + (isDisabled) => { |
| 78 | + this._isDisabled = isDisabled || false; |
| 79 | + }, |
| 80 | + 'isDisabledObserver', |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #initButtonStateReset() { |
| 85 | + this.#clearButtonStateResetTimeout(); |
| 86 | + this.#buttonStateResetTimeoutId = window.setTimeout(() => { |
| 87 | + this._buttonState = undefined; |
| 88 | + }, 2000); |
| 89 | + } |
| 90 | + |
| 91 | + #clearButtonStateResetTimeout() { |
| 92 | + if (this.#buttonStateResetTimeoutId !== null) { |
| 93 | + clearTimeout(this.#buttonStateResetTimeoutId); |
| 94 | + this.#buttonStateResetTimeoutId = null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #observeExtensions(): void { |
| 99 | + this.#extensionsController?.destroy(); |
| 100 | + this.#extensionsController = new UmbExtensionsElementAndApiInitializer< |
| 101 | + ManifestPreviewOption, |
| 102 | + 'previewOption', |
| 103 | + ManifestPreviewOption |
| 104 | + >(this, umbExtensionsRegistry, 'previewOption', [], undefined, (extensionControllers) => { |
| 105 | + this._primaryAction = extensionControllers.shift(); |
| 106 | + this._actions = extensionControllers; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + #renderButton() { |
| 111 | + const label = this._primaryAction?.manifest?.meta.label || this.#manifest?.meta.label || this.#manifest?.name; |
| 112 | + return html` |
| 113 | + <uui-button |
| 114 | + data-mark="workspace-action:${this.#manifest?.alias}" |
| 115 | + color=${this.#manifest?.meta.color ?? 'default'} |
| 116 | + look=${this.#manifest?.meta.look ?? 'default'} |
| 117 | + label=${this.localize.string(label)} |
| 118 | + .disabled=${this._isDisabled} |
| 119 | + .state=${this._buttonState} |
| 120 | + @click=${this.#onClick}></uui-button> |
| 121 | + `; |
| 122 | + } |
| 123 | + |
| 124 | + #renderActionMenu() { |
| 125 | + // TODO: [LK] FIXME: The `any` type casting here needs to be resolved with a proper type. |
| 126 | + return html` |
| 127 | + <umb-workspace-action-menu |
| 128 | + color=${this.#manifest?.meta.color ?? 'default'} |
| 129 | + look=${this.#manifest?.meta.look ?? 'default'} |
| 130 | + .items=${this._actions as any}></umb-workspace-action-menu> |
| 131 | + `; |
| 132 | + } |
| 133 | + |
| 134 | + override render() { |
| 135 | + return when( |
| 136 | + this._actions.length, |
| 137 | + () => html`<uui-button-group>${this.#renderButton()}${this.#renderActionMenu()}</uui-button-group>`, |
| 138 | + () => this.#renderButton(), |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + override disconnectedCallback() { |
| 143 | + super.disconnectedCallback(); |
| 144 | + this.#clearButtonStateResetTimeout(); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +export default UmbSaveAndPreviewWorkspaceActionElement; |
| 149 | + |
| 150 | +declare global { |
| 151 | + interface HTMLElementTagNameMap { |
| 152 | + 'umb-save-and-preview-workspace-action': UmbSaveAndPreviewWorkspaceActionElement; |
| 153 | + } |
| 154 | +} |
0 commit comments