-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from contentstack/staging
Staging -> Main
- Loading branch information
Showing
6 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters