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