From da6825edba3b2237f2fcf6d8ef09439398ff7c53 Mon Sep 17 00:00:00 2001 From: praveen-mohan-cs Date: Mon, 21 Oct 2024 15:39:21 +0530 Subject: [PATCH 1/3] Added Content type sidebar UI location --- src/ContentTypeSidebarWidget.ts | 68 +++++++++++++++++++++++++++++++++ src/types.ts | 10 ++++- src/uiLocation.ts | 22 +++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/ContentTypeSidebarWidget.ts diff --git a/src/ContentTypeSidebarWidget.ts b/src/ContentTypeSidebarWidget.ts new file mode 100644 index 0000000..fc41b8e --- /dev/null +++ b/src/ContentTypeSidebarWidget.ts @@ -0,0 +1,68 @@ +import EventEmitter from "wolfy87-eventemitter"; +import { IContentTypeSidebarInitData } from "./types"; +import postRobot from "post-robot"; +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: any }) => { + callback(event.data); + } + ); + } 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 049c187..3d3d30e 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"; @@ -114,6 +115,7 @@ class UiLocation { AssetSidebarWidget: AssetSidebarWidget | null; FullPage: IFullPageLocation | null; FieldModifierLocation: IFieldModifierLocation | null; + ContentTypeSidebarWidget: ContentTypeSidebarWidget | null; }; constructor(initData: InitializationData) { @@ -151,6 +153,7 @@ class UiLocation { AssetSidebarWidget: null, FullPage: null, FieldModifierLocation: null, + ContentTypeSidebarWidget: null, }; window["postRobot"] = postRobot; @@ -258,6 +261,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; @@ -345,6 +358,15 @@ class UiLocation { { data: event.data.data }, ]); } + + if (event.data.name === "contentTypeSave") { + emitter.emitEvent("contentTypeSave", [ + { data: event.data.data }, + ]); + emitter.emitEvent("updateFields", [ + { data: event.data.data }, + ]); + } }); } catch (err) { console.error("Extension Event", err); From 59eb67ef32239a90ac04d1f7606f0d75e0c8df68 Mon Sep 17 00:00:00 2001 From: praveen-mohan-cs Date: Thu, 24 Oct 2024 17:41:04 +0530 Subject: [PATCH 2/3] Addressed PR comments --- src/ContentTypeSidebarWidget.ts | 3 +++ src/uiLocation.ts | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ContentTypeSidebarWidget.ts b/src/ContentTypeSidebarWidget.ts index fc41b8e..8cd5e48 100644 --- a/src/ContentTypeSidebarWidget.ts +++ b/src/ContentTypeSidebarWidget.ts @@ -59,6 +59,9 @@ class ContentTypeSidebarWidget { callback(event.data); } ); + this._emitter.emitEvent("_eventRegistration", [ + { name: "contentTypeSave" }, + ]); } else { throw Error("Callback must be a function"); } diff --git a/src/uiLocation.ts b/src/uiLocation.ts index f060c3d..6780385 100755 --- a/src/uiLocation.ts +++ b/src/uiLocation.ts @@ -364,9 +364,6 @@ class UiLocation { emitter.emitEvent("contentTypeSave", [ { data: event.data.data }, ]); - emitter.emitEvent("updateFields", [ - { data: event.data.data }, - ]); } }); } catch (err) { From 51577bb84aacf80c6bbd282105a7598f8e4868f2 Mon Sep 17 00:00:00 2001 From: praveen-mohan-cs Date: Fri, 25 Oct 2024 10:51:23 +0530 Subject: [PATCH 3/3] Addressed PR comments --- src/ContentTypeSidebarWidget.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ContentTypeSidebarWidget.ts b/src/ContentTypeSidebarWidget.ts index 8cd5e48..8e490d0 100644 --- a/src/ContentTypeSidebarWidget.ts +++ b/src/ContentTypeSidebarWidget.ts @@ -1,6 +1,6 @@ import EventEmitter from "wolfy87-eventemitter"; -import { IContentTypeSidebarInitData } from "./types"; import postRobot from "post-robot"; +import { IContentTypeSidebarInitData } from "./types"; import { ContentType } from "./types/stack.types"; import { GenericObjectType } from "./types/common.types"; @@ -55,7 +55,7 @@ class ContentTypeSidebarWidget { if (callback && typeof callback === "function") { contentTypeObj._emitter.on( "contentTypeSave", - (event: { data: any }) => { + (event: { data: ContentType }) => { callback(event.data); } );