Skip to content

Commit

Permalink
Merge pull request #134 from contentstack/staging
Browse files Browse the repository at this point in the history
Staging -> Main
  • Loading branch information
naveenctstack authored Jan 31, 2025
2 parents 1e23222 + 2e76370 commit 9ea2df9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2024 Contentstack
Copyright (c) 2022-2025 Contentstack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/app-sdk",
"version": "2.1.1",
"version": "2.2.0",
"types": "dist/src/index.d.ts",
"description": "The Contentstack App SDK allows you to customize your Contentstack applications.",
"main": "dist/index.js",
Expand Down
71 changes: 71 additions & 0 deletions src/ContentTypeSidebarWidget.ts
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;
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -194,7 +201,8 @@ export type InitializationData =
| IFieldModifierLocationInitData
| IFullPageLocationInitData
| IRTEInitData
| ISidebarInitData;
| ISidebarInitData
| IContentTypeSidebarInitData;

/**
* installation details API response
Expand Down
19 changes: 19 additions & 0 deletions src/uiLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -115,6 +116,7 @@ class UiLocation {
AssetSidebarWidget: AssetSidebarWidget | null;
FullPage: IFullPageLocation | null;
FieldModifierLocation: IFieldModifierLocation | null;
ContentTypeSidebarWidget: ContentTypeSidebarWidget | null;
};

constructor(initData: InitializationData) {
Expand Down Expand Up @@ -152,6 +154,7 @@ class UiLocation {
AssetSidebarWidget: null,
FullPage: null,
FieldModifierLocation: null,
ContentTypeSidebarWidget: null,
};

window["postRobot"] = postRobot;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9ea2df9

Please sign in to comment.