Skip to content

Commit da6825e

Browse files
Added Content type sidebar UI location
1 parent a4c6e82 commit da6825e

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/ContentTypeSidebarWidget.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import EventEmitter from "wolfy87-eventemitter";
2+
import { IContentTypeSidebarInitData } from "./types";
3+
import postRobot from "post-robot";
4+
import { ContentType } from "./types/stack.types";
5+
import { GenericObjectType } from "./types/common.types";
6+
7+
/** Class representing a Content type Sidebar UI Location from Contentstack UI. */
8+
9+
class ContentTypeSidebarWidget {
10+
/**
11+
* @hideconstructor
12+
*/
13+
14+
currentContentType: ContentType;
15+
_emitter: EventEmitter;
16+
_connection: typeof postRobot;
17+
_changedData?: GenericObjectType;
18+
19+
constructor(
20+
initializationData: IContentTypeSidebarInitData,
21+
connection: typeof postRobot,
22+
emitter: EventEmitter
23+
) {
24+
this.currentContentType = initializationData.currentContentType;
25+
26+
this._emitter = emitter;
27+
28+
this._connection = connection;
29+
30+
const thisContentType = this;
31+
32+
this._emitter.on("contentTypeSave", (event: { data: ContentType }) => {
33+
thisContentType.currentContentType = event.data;
34+
});
35+
36+
this.getData = this.getData.bind(this);
37+
this.onSave = this.onSave.bind(this);
38+
}
39+
40+
/**
41+
* Get the current content type data.
42+
* @returns {ContentTypeData} The current content type data.
43+
*/
44+
getData(): ContentType {
45+
return this.currentContentType;
46+
}
47+
48+
/**
49+
* Executes the provided callback function every time a content type is saved.
50+
* @param {function} callback - The function to be called when a content type is saved.
51+
* @param {ContentType} arg0 - The content type data passed as an argument to the callback function when a content type is saved.
52+
*/
53+
onSave(callback: (arg0: ContentType) => void) {
54+
const contentTypeObj = this;
55+
if (callback && typeof callback === "function") {
56+
contentTypeObj._emitter.on(
57+
"contentTypeSave",
58+
(event: { data: any }) => {
59+
callback(event.data);
60+
}
61+
);
62+
} else {
63+
throw Error("Callback must be a function");
64+
}
65+
}
66+
}
67+
68+
export default ContentTypeSidebarWidget;

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export enum LocationType {
8181
FULL_PAGE_LOCATION = "FULL_PAGE_LOCATION",
8282
RTE = "RTE",
8383
WIDGET = "WIDGET",
84+
CONTENT_TYPE_SIDEBAR_WIDGET = "CONTENT_TYPE_SIDEBAR_WIDGET",
8485
}
8586

8687
// Init data
@@ -148,6 +149,12 @@ export declare interface IAssetSidebarInitData extends ICommonInitData {
148149
type: LocationType.ASSET_SIDEBAR_WIDGET;
149150
}
150151

152+
export declare interface IContentTypeSidebarInitData extends ICommonInitData {
153+
config?: GenericObjectType;
154+
currentContentType: ContentType;
155+
type: LocationType.CONTENT_TYPE_SIDEBAR_WIDGET;
156+
}
157+
151158
export declare interface IFieldModifierLocationInitData
152159
extends ICommonInitData {
153160
changedData: Entry;
@@ -194,7 +201,8 @@ export type InitializationData =
194201
| IFieldModifierLocationInitData
195202
| IFullPageLocationInitData
196203
| IRTEInitData
197-
| ISidebarInitData;
204+
| ISidebarInitData
205+
| IContentTypeSidebarInitData;
198206

199207
/**
200208
* installation details API response

src/uiLocation.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import postRobot from "post-robot";
22
import EventEmitter from "wolfy87-eventemitter";
33

44
import AssetSidebarWidget from "./AssetSidebarWidget";
5+
import ContentTypeSidebarWidget from "./ContentTypeSidebarWidget";
56
import { IRTEPluginInitializer } from "./RTE/types";
67
import { AppConfig } from "./appConfig";
78
import Entry from "./entry";
@@ -114,6 +115,7 @@ class UiLocation {
114115
AssetSidebarWidget: AssetSidebarWidget | null;
115116
FullPage: IFullPageLocation | null;
116117
FieldModifierLocation: IFieldModifierLocation | null;
118+
ContentTypeSidebarWidget: ContentTypeSidebarWidget | null;
117119
};
118120

119121
constructor(initData: InitializationData) {
@@ -151,6 +153,7 @@ class UiLocation {
151153
AssetSidebarWidget: null,
152154
FullPage: null,
153155
FieldModifierLocation: null,
156+
ContentTypeSidebarWidget: null,
154157
};
155158

156159
window["postRobot"] = postRobot;
@@ -258,6 +261,16 @@ class UiLocation {
258261
break;
259262
}
260263

264+
case LocationType.CONTENT_TYPE_SIDEBAR_WIDGET: {
265+
this.location.ContentTypeSidebarWidget =
266+
new ContentTypeSidebarWidget(
267+
initializationData,
268+
postRobot,
269+
emitter
270+
);
271+
break;
272+
}
273+
261274
case LocationType.FIELD:
262275
default: {
263276
initializationData.self = true;
@@ -345,6 +358,15 @@ class UiLocation {
345358
{ data: event.data.data },
346359
]);
347360
}
361+
362+
if (event.data.name === "contentTypeSave") {
363+
emitter.emitEvent("contentTypeSave", [
364+
{ data: event.data.data },
365+
]);
366+
emitter.emitEvent("updateFields", [
367+
{ data: event.data.data },
368+
]);
369+
}
348370
});
349371
} catch (err) {
350372
console.error("Extension Event", err);

0 commit comments

Comments
 (0)