Skip to content

Commit 9ea2df9

Browse files
Merge pull request #134 from contentstack/staging
Staging -> Main
2 parents 1e23222 + 2e76370 commit 9ea2df9

File tree

6 files changed

+103
-5
lines changed

6 files changed

+103
-5
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022-2024 Contentstack
3+
Copyright (c) 2022-2025 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/app-sdk",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"types": "dist/src/index.d.ts",
55
"description": "The Contentstack App SDK allows you to customize your Contentstack applications.",
66
"main": "dist/index.js",

src/ContentTypeSidebarWidget.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import EventEmitter from "wolfy87-eventemitter";
2+
import postRobot from "post-robot";
3+
import { IContentTypeSidebarInitData } from "./types";
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: ContentType }) => {
59+
callback(event.data);
60+
}
61+
);
62+
this._emitter.emitEvent("_eventRegistration", [
63+
{ name: "contentTypeSave" },
64+
]);
65+
} else {
66+
throw Error("Callback must be a function");
67+
}
68+
}
69+
}
70+
71+
export default ContentTypeSidebarWidget;

src/types.ts

+9-1
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

+19
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";
@@ -115,6 +116,7 @@ class UiLocation {
115116
AssetSidebarWidget: AssetSidebarWidget | null;
116117
FullPage: IFullPageLocation | null;
117118
FieldModifierLocation: IFieldModifierLocation | null;
119+
ContentTypeSidebarWidget: ContentTypeSidebarWidget | null;
118120
};
119121

120122
constructor(initData: InitializationData) {
@@ -152,6 +154,7 @@ class UiLocation {
152154
AssetSidebarWidget: null,
153155
FullPage: null,
154156
FieldModifierLocation: null,
157+
ContentTypeSidebarWidget: null,
155158
};
156159

157160
window["postRobot"] = postRobot;
@@ -259,6 +262,16 @@ class UiLocation {
259262
break;
260263
}
261264

265+
case LocationType.CONTENT_TYPE_SIDEBAR_WIDGET: {
266+
this.location.ContentTypeSidebarWidget =
267+
new ContentTypeSidebarWidget(
268+
initializationData,
269+
postRobot,
270+
emitter
271+
);
272+
break;
273+
}
274+
262275
case LocationType.FIELD:
263276
default: {
264277
initializationData.self = true;
@@ -346,6 +359,12 @@ class UiLocation {
346359
{ data: event.data.data },
347360
]);
348361
}
362+
363+
if (event.data.name === "contentTypeSave") {
364+
emitter.emitEvent("contentTypeSave", [
365+
{ data: event.data.data },
366+
]);
367+
}
349368
});
350369
} catch (err) {
351370
console.error("Extension Event", err);

0 commit comments

Comments
 (0)