diff --git a/src/ContentTypeSidebarWidget.ts b/src/ContentTypeSidebarWidget.ts new file mode 100644 index 0000000..8e490d0 --- /dev/null +++ b/src/ContentTypeSidebarWidget.ts @@ -0,0 +1,71 @@ +import EventEmitter from "wolfy87-eventemitter"; +import postRobot from "post-robot"; +import { IContentTypeSidebarInitData } from "./types"; +import { ContentType } from "./types/stack.types"; +import { GenericObjectType } from "./types/common.types"; + +/** Class representing a Content type Sidebar UI Location from Contentstack UI. */ + +class ContentTypeSidebarWidget { + /** + * @hideconstructor + */ + + currentContentType: ContentType; + _emitter: EventEmitter; + _connection: typeof postRobot; + _changedData?: GenericObjectType; + + constructor( + initializationData: IContentTypeSidebarInitData, + connection: typeof postRobot, + emitter: EventEmitter + ) { + this.currentContentType = initializationData.currentContentType; + + this._emitter = emitter; + + this._connection = connection; + + const thisContentType = this; + + this._emitter.on("contentTypeSave", (event: { data: ContentType }) => { + thisContentType.currentContentType = event.data; + }); + + this.getData = this.getData.bind(this); + this.onSave = this.onSave.bind(this); + } + + /** + * Get the current content type data. + * @returns {ContentTypeData} The current content type data. + */ + getData(): ContentType { + return this.currentContentType; + } + + /** + * Executes the provided callback function every time a content type is saved. + * @param {function} callback - The function to be called when a content type is saved. + * @param {ContentType} arg0 - The content type data passed as an argument to the callback function when a content type is saved. + */ + onSave(callback: (arg0: ContentType) => void) { + const contentTypeObj = this; + if (callback && typeof callback === "function") { + contentTypeObj._emitter.on( + "contentTypeSave", + (event: { data: ContentType }) => { + callback(event.data); + } + ); + this._emitter.emitEvent("_eventRegistration", [ + { name: "contentTypeSave" }, + ]); + } else { + throw Error("Callback must be a function"); + } + } +} + +export default ContentTypeSidebarWidget; diff --git a/src/types.ts b/src/types.ts index ccc68e7..3581fc4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,6 +81,7 @@ export enum LocationType { FULL_PAGE_LOCATION = "FULL_PAGE_LOCATION", RTE = "RTE", WIDGET = "WIDGET", + CONTENT_TYPE_SIDEBAR_WIDGET = "CONTENT_TYPE_SIDEBAR_WIDGET", } // Init data @@ -148,6 +149,12 @@ export declare interface IAssetSidebarInitData extends ICommonInitData { type: LocationType.ASSET_SIDEBAR_WIDGET; } +export declare interface IContentTypeSidebarInitData extends ICommonInitData { + config?: GenericObjectType; + currentContentType: ContentType; + type: LocationType.CONTENT_TYPE_SIDEBAR_WIDGET; +} + export declare interface IFieldModifierLocationInitData extends ICommonInitData { changedData: Entry; @@ -194,7 +201,8 @@ export type InitializationData = | IFieldModifierLocationInitData | IFullPageLocationInitData | IRTEInitData - | ISidebarInitData; + | ISidebarInitData + | IContentTypeSidebarInitData; /** * installation details API response diff --git a/src/uiLocation.ts b/src/uiLocation.ts index 891b43e..6780385 100755 --- a/src/uiLocation.ts +++ b/src/uiLocation.ts @@ -2,6 +2,7 @@ import postRobot from "post-robot"; import EventEmitter from "wolfy87-eventemitter"; import AssetSidebarWidget from "./AssetSidebarWidget"; +import ContentTypeSidebarWidget from "./ContentTypeSidebarWidget"; import { IRTEPluginInitializer } from "./RTE/types"; import { AppConfig } from "./appConfig"; import Entry from "./entry"; @@ -115,6 +116,7 @@ class UiLocation { AssetSidebarWidget: AssetSidebarWidget | null; FullPage: IFullPageLocation | null; FieldModifierLocation: IFieldModifierLocation | null; + ContentTypeSidebarWidget: ContentTypeSidebarWidget | null; }; constructor(initData: InitializationData) { @@ -152,6 +154,7 @@ class UiLocation { AssetSidebarWidget: null, FullPage: null, FieldModifierLocation: null, + ContentTypeSidebarWidget: null, }; window["postRobot"] = postRobot; @@ -259,6 +262,16 @@ class UiLocation { break; } + case LocationType.CONTENT_TYPE_SIDEBAR_WIDGET: { + this.location.ContentTypeSidebarWidget = + new ContentTypeSidebarWidget( + initializationData, + postRobot, + emitter + ); + break; + } + case LocationType.FIELD: default: { initializationData.self = true; @@ -346,6 +359,12 @@ class UiLocation { { data: event.data.data }, ]); } + + if (event.data.name === "contentTypeSave") { + emitter.emitEvent("contentTypeSave", [ + { data: event.data.data }, + ]); + } }); } catch (err) { console.error("Extension Event", err);