|
| 1 | +import { UMB_CONTENT_WORKSPACE_CONTEXT } from '../constants.js'; |
| 2 | +import { html, customElement, property, state, nothing } from '@umbraco-cms/backoffice/external/lit'; |
| 3 | +import type { UmbPropertyTypeModel } from '@umbraco-cms/backoffice/content-type'; |
| 4 | +import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; |
| 5 | +import { UMB_PROPERTY_DATASET_CONTEXT } from '@umbraco-cms/backoffice/property'; |
| 6 | +import type { UmbVariantId } from '@umbraco-cms/backoffice/variant'; |
| 7 | + |
| 8 | +import '../workspace/views/edit/content-editor-property.element.js'; |
| 9 | + |
| 10 | +@customElement('umb-content-workspace-property') |
| 11 | +export class UmbContentWorkspacePropertyElement extends UmbLitElement { |
| 12 | + private _alias?: string | undefined; |
| 13 | + |
| 14 | + @property() |
| 15 | + public get alias(): string | undefined { |
| 16 | + return this._alias; |
| 17 | + } |
| 18 | + public set alias(value: string | undefined) { |
| 19 | + this._alias = value; |
| 20 | + this.#observePropertyType(); |
| 21 | + } |
| 22 | + |
| 23 | + @state() |
| 24 | + _datasetVariantId?: UmbVariantId; |
| 25 | + |
| 26 | + @state() |
| 27 | + _dataPath?: string; |
| 28 | + |
| 29 | + @state() |
| 30 | + _writeable?: boolean; |
| 31 | + |
| 32 | + @state() |
| 33 | + _workspaceContext?: typeof UMB_CONTENT_WORKSPACE_CONTEXT.TYPE; |
| 34 | + |
| 35 | + @state() |
| 36 | + _propertyType?: UmbPropertyTypeModel; |
| 37 | + |
| 38 | + constructor() { |
| 39 | + super(); |
| 40 | + |
| 41 | + // The Property Dataset is local to the active variant, we use this to retrieve the variant we like to gather the value from. |
| 42 | + this.consumeContext(UMB_PROPERTY_DATASET_CONTEXT, (datasetContext) => { |
| 43 | + this._datasetVariantId = datasetContext?.getVariantId(); |
| 44 | + }); |
| 45 | + |
| 46 | + // The Content Workspace Context is used to retrieve the property type we like to observe. |
| 47 | + // This gives us the configuration from the property type as part of the data type. |
| 48 | + this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, async (workspaceContext) => { |
| 49 | + this._workspaceContext = workspaceContext; |
| 50 | + this.#observePropertyType(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + async #observePropertyType() { |
| 55 | + if (!this._alias || !this._workspaceContext) return; |
| 56 | + |
| 57 | + this.observe(await this._workspaceContext?.structure.propertyStructureByAlias(this._alias), (propertyType) => { |
| 58 | + this._propertyType = propertyType; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + override render() { |
| 63 | + if (!this._datasetVariantId && !this._propertyType) return nothing; |
| 64 | + |
| 65 | + return html`<umb-content-workspace-view-edit-property |
| 66 | + class="property" |
| 67 | + .variantId=${this._datasetVariantId} |
| 68 | + .property=${this._propertyType}></umb-content-workspace-view-edit-property>`; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export default UmbContentWorkspacePropertyElement; |
| 73 | + |
| 74 | +declare global { |
| 75 | + interface HTMLElementTagNameMap { |
| 76 | + 'umb-content-workspace-property': UmbContentWorkspacePropertyElement; |
| 77 | + } |
| 78 | +} |
0 commit comments