Skip to content

Commit 5c43347

Browse files
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.
1 parent e89780e commit 5c43347

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
...(metadata ? { metadata } : {}),
47+
getMetadata() {
48+
return metadata
49+
},
50+
onMetadataChanged(handler: VoidFunction) {
51+
return metadataChanged.attach(handler)
52+
},
53+
}
54+
}

lib/types/api.types.ts

Lines changed: 5 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'
@@ -412,6 +412,10 @@ export interface ConnectMessage {
412412
sys: EntrySys
413413
metadata?: Metadata
414414
}
415+
asset?: {
416+
sys: AssetSys
417+
metadata?: Metadata
418+
}
415419
fieldInfo: EntryFieldInfo[]
416420
field?: FieldInfo
417421
hostnames: HostnamesAPI

lib/types/asset.types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
/**
16+
* Optional metadata on an asset
17+
* @deprecated
18+
*/
19+
metadata?: Metadata
20+
getMetadata: () => Metadata | undefined
21+
onMetadataChanged: (callback: (metadata?: Metadata) => void) => VoidFunction
22+
}

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)