Skip to content

Commit 87ab2b1

Browse files
feat: ext 6788 asset sidebar location constants [EXT-6786] (#2338)
* feat: add ASSET_SIDEBAR location constants - Add LOCATION_ASSET_SIDEBAR to locations.ts - Update Locations interface in api.types.ts - Part of EXT-6786: Asset Sidebar App Framework Location * feat: implement asset API integration - Added createAsset import and makeAssetAPI function to handle asset-related messages. - Updated LOCATION_TO_API_PRODUCERS to include asset sidebar API producers. - Enhanced ConnectMessage interface to support asset metadata and system information. - Introduced AssetSys interface for asset type representation. This change supports the new asset sidebar functionality. * refactor: remove deprecated metadata from AssetAPI interface - Removed the optional metadata property from the AssetAPI interface, marking it as deprecated. - Updated createAsset function to eliminate the metadata parameter, streamlining asset creation. This change enhances the clarity and usability of the asset API.
1 parent 1f8e8a4 commit 87ab2b1

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

lib/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { makeField } from './field'
22
import { makeFieldLocale } from './field-locale'
33
import createWindow from './window'
44
import createEntry from './entry'
5+
import createAsset from './asset'
56
import createSpace from './space'
67
import createDialogs from './dialogs'
78
import createEditor from './editor'
@@ -38,6 +39,7 @@ const LOCATION_TO_API_PRODUCERS: { [location: string]: ProducerFunc[] } = {
3839
[locations.LOCATION_ENTRY_FIELD]: DEFAULT_API_PRODUCERS,
3940
[locations.LOCATION_ENTRY_FIELD_SIDEBAR]: DEFAULT_API_PRODUCERS,
4041
[locations.LOCATION_ENTRY_SIDEBAR]: [makeSharedAPI, makeEntryAPI, makeEditorAPI, makeWindowAPI],
42+
[locations.LOCATION_ASSET_SIDEBAR]: [makeSharedAPI, makeAssetAPI, makeWindowAPI],
4143
[locations.LOCATION_ENTRY_EDITOR]: [makeSharedAPI, makeEntryAPI, makeEditorAPI],
4244
[locations.LOCATION_DIALOG]: [makeSharedAPI, makeDialogAPI, makeWindowAPI],
4345
[locations.LOCATION_PAGE]: [makeSharedAPI],
@@ -151,6 +153,12 @@ function makeDialogAPI(channel: Channel) {
151153
}
152154
}
153155

156+
function makeAssetAPI(channel: Channel, { asset }: ConnectMessage) {
157+
return {
158+
asset: createAsset(channel, asset),
159+
}
160+
}
161+
154162
function makeAppAPI(channel: Channel) {
155163
const app = createApp(channel)
156164

lib/asset.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Channel } from './channel'
2+
import { MemoizedSignal } from './signal'
3+
import { ConnectMessage, Metadata } from './types'
4+
import { AssetAPI } from './types/asset.types'
5+
import { AssetSys } from './types/utils'
6+
7+
export default function createAsset(
8+
channel: Channel,
9+
assetData: ConnectMessage['asset'],
10+
): AssetAPI {
11+
if (!assetData) {
12+
throw new Error('Asset data is required')
13+
}
14+
15+
let sys: AssetSys = assetData.sys
16+
const sysChanged = new MemoizedSignal<[AssetSys]>(sys)
17+
let metadata = assetData.metadata
18+
const metadataChanged = new MemoizedSignal<[Metadata | undefined]>(metadata)
19+
20+
channel.addHandler('sysChanged', (newSys: AssetSys) => {
21+
sys = newSys
22+
sysChanged.dispatch(sys)
23+
})
24+
25+
channel.addHandler('metadataChanged', (newMetadata: Metadata) => {
26+
metadata = newMetadata
27+
metadataChanged.dispatch(metadata)
28+
})
29+
30+
return {
31+
getSys() {
32+
return sys
33+
},
34+
publish(options?: { skipUiValidation?: boolean }) {
35+
return channel.call<void>('callAssetMethod', 'publish', [options])
36+
},
37+
unpublish() {
38+
return channel.call<void>('callAssetMethod', 'unpublish')
39+
},
40+
save() {
41+
return channel.call<void>('callAssetMethod', 'save')
42+
},
43+
onSysChanged(handler: (sys: AssetSys) => void) {
44+
return sysChanged.attach(handler)
45+
},
46+
getMetadata() {
47+
return metadata
48+
},
49+
onMetadataChanged(handler: VoidFunction) {
50+
return metadataChanged.attach(handler)
51+
},
52+
}
53+
}

lib/locations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const locations: Locations = {
44
LOCATION_ENTRY_FIELD: 'entry-field',
55
LOCATION_ENTRY_FIELD_SIDEBAR: 'entry-field-sidebar',
66
LOCATION_ENTRY_SIDEBAR: 'entry-sidebar',
7+
LOCATION_ASSET_SIDEBAR: 'asset-sidebar',
78
LOCATION_DIALOG: 'dialog',
89
LOCATION_ENTRY_EDITOR: 'entry-editor',
910
LOCATION_PAGE: 'page',

lib/types/api.types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { EntryAPI } from './entry.types'
1515
import { SpaceAPI } from './space.types'
1616
import { WindowAPI } from './window.types'
17-
import { EntrySys, Link, SerializedJSONValue } from './utils'
17+
import { EntrySys, AssetSys, Link, SerializedJSONValue } from './utils'
1818
import { FieldAPI } from './field-locale.types'
1919
import { DialogsAPI } from './dialogs.types'
2020
import { AppConfigAPI } from './app.types'
@@ -386,6 +386,7 @@ export interface Locations {
386386
LOCATION_ENTRY_FIELD: 'entry-field'
387387
LOCATION_ENTRY_FIELD_SIDEBAR: 'entry-field-sidebar'
388388
LOCATION_ENTRY_SIDEBAR: 'entry-sidebar'
389+
LOCATION_ASSET_SIDEBAR: 'asset-sidebar'
389390
LOCATION_DIALOG: 'dialog'
390391
LOCATION_ENTRY_EDITOR: 'entry-editor'
391392
LOCATION_PAGE: 'page'
@@ -411,6 +412,10 @@ export interface ConnectMessage {
411412
sys: EntrySys
412413
metadata?: Metadata
413414
}
415+
asset?: {
416+
sys: AssetSys
417+
metadata?: Metadata
418+
}
414419
fieldInfo: EntryFieldInfo[]
415420
field?: FieldInfo
416421
hostnames: HostnamesAPI

lib/types/asset.types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Metadata } from './entities'
2+
import { AssetSys } from './utils'
3+
4+
export interface AssetAPI {
5+
/** Returns sys for an asset. */
6+
getSys: () => AssetSys
7+
/** Publish the asset */
8+
publish: (options?: { skipUiValidation?: boolean }) => Promise<void>
9+
/** Unpublish the asset */
10+
unpublish: () => Promise<void>
11+
/** Saves the current changes of the asset */
12+
save: () => Promise<void>
13+
/** Calls the callback with sys every time that sys changes. */
14+
onSysChanged: (callback: (sys: AssetSys) => void) => () => void
15+
getMetadata: () => Metadata | undefined
16+
onMetadataChanged: (callback: (metadata?: Metadata) => void) => VoidFunction
17+
}

lib/types/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export interface EntrySys extends ContentEntitySys {
7070
automationTags: Link<'Tag'>[]
7171
}
7272

73+
export interface AssetSys extends ContentEntitySys {
74+
type: 'Asset'
75+
}
76+
7377
export type FieldType =
7478
| 'Symbol'
7579
| 'Text'

0 commit comments

Comments
 (0)