-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·113 lines (104 loc) · 4.21 KB
/
index.ts
File metadata and controls
executable file
·113 lines (104 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import postRobot from "post-robot";
import { InitializationData } from "./types";
import { IRteParam } from "./RTE/types";
import { PluginDefinition, PluginBuilder, registerPlugins } from "./rtePlugin";
import UiLocation from "./uiLocation";
import { version } from "../package.json";
postRobot.CONFIG.LOG_LEVEL = "error";
postRobot.CONFIG.ACK_TIMEOUT = 10000;
/**
* Class to initialize the App on Contentstack UI.
* Import Contentstack App SDK and then call ContentstackAppSDK.init in your code base
*
* @example <caption>Custom Field UI Location</caption>
* ContentstackAppSDK.init().then(function (sdk) {
* const customField = sdk.location.CustomField;
* })
* @example <caption>Dashboard UI Location</caption>
* ContentstackAppSDK.init().then(function (sdk) {
* const dashboardUILocation = sdk.location.DashboardWidget;
* })
* @return {Promise} A promise object which will be resolved with an instance of the {@link UiLocation} class.
* @hideconstructor
*/
class ContentstackAppSDK {
/**
* A static variable that stores the instance of {@link UiLocation} class after initialization
*/
static _uiLocation: UiLocation;
/**
* Initializes the App SDK and returns an instance of {@link UiLocation} class
*/
static init(): Promise<UiLocation> {
if (this._uiLocation) {
return Promise.resolve<UiLocation>(this._uiLocation);
}
return UiLocation.initialize(version)
.then((initializationData: InitializationData) => {
this._uiLocation = new UiLocation(initializationData);
return Promise.resolve(this._uiLocation);
})
.catch((e: Error) => Promise.reject(e));
}
/**
* Registers RTE plugins with the Contentstack platform.
* This method is the primary entry point for defining and registering custom RTE plugins
* built using the PluginBuilder pattern. It returns a function that the Contentstack
* platform will invoke at runtime, providing the necessary context.
*
* @example
* // In your plugin's entry file (e.g., src/index.ts):
* import ContentstackAppSDK, { PluginBuilder } from '@contentstack/app-sdk';
*
* const MyCustomPlugin = new PluginBuilder("my-plugin-id")
* .title("My Plugin")
* .icon(<MyIconComponent />)
* .elementType("block")
* .display("toolbar")
* .render(()=>{return <Comment />})
* .on("exec", (rte: IRteParam) => {
* // Access SDK via rte.sdk if needed:
* const sdk = rte.sdk;
* // ... plugin execution logic ...
* })
* .build();
*
* export default ContentstackAppSDK.registerRTEPlugins(
* MyCustomPlugin
* );
*
* @param {...PluginDefinition} pluginDefinitions - One or more plugin definitions created using the `PluginBuilder`.
* Each `PluginDefinition` describes the plugin's configuration, callbacks, and any child plugins.
* @returns {Promise<{ __isPluginBuilder__: boolean; version: string; plugins: (context: RTEContext, rte: IRteParam) => Promise<{ [key: string]: RTEPlugin; }>; }>}
* A Promise that resolves to an object containing:
* - `__isPluginBuilder__`: A boolean flag indicating this is a builder-based plugin export.
* - `version`: The version of the SDK that registered the plugins.
* - `plugins`: An asynchronous function. This function is designed to be invoked by the
* Contentstack platform loader, providing the `context` (initialization data) and
* the `rte` instance. When called, it materializes and returns a map of the
* registered `RTEPlugin` instances, keyed by their IDs.
*/
static async registerRTEPlugins(...pluginDefinitions: PluginDefinition[]) {
return {
__isPluginBuilder__: true,
version,
plugins: (context: InitializationData, rte: IRteParam) => {
return registerPlugins(...pluginDefinitions)(context, rte);
}
};
}
/**
* Version of Contentstack App SDK.
*/
static get SDK_VERSION() {
return version;
}
}
export default ContentstackAppSDK;
export { PluginBuilder };
// CommonJS compatibility
if (typeof module !== 'undefined' && module.exports) {
module.exports = ContentstackAppSDK;
module.exports.default = ContentstackAppSDK;
module.exports.PluginBuilder = PluginBuilder;
}