-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/mkt 12695 json rte #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Amitkanswal
wants to merge
7
commits into
develop
Choose a base branch
from
feat/MKT-12695-json-rte
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7335ace
fix:updated region
Amitkanswal fcfa59a
feat:rte-plugin setup
Amitkanswal ed9bc4c
fix:updated test case
Amitkanswal 3026079
updated readme and review changes
Amitkanswal 84e4fa3
fix:event change issue
Amitkanswal 6f80778
fix:apiAdapter props
Amitkanswal 50b6466
fix:naming changes
Amitkanswal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,186 @@ | ||
import { RTEPlugin as Plugin, rtePluginInitializer } from "./RTE"; | ||
import { | ||
IConfig, | ||
IDisplayOnOptions, | ||
IDynamicFunction, | ||
IElementTypeOptions, | ||
IOnFunction, | ||
IRteElementType, | ||
IRteParam, | ||
} from "./RTE/types"; | ||
import { InitializationData, IRTEInitData } from "./types"; | ||
import UiLocation from "./uiLocation"; | ||
|
||
type PluginConfigCallback = (sdk: UiLocation) => Promise<IConfig> | IConfig; | ||
|
||
interface PluginDefinition { | ||
id: string; | ||
config: Partial<IConfig>; | ||
callbacks: Partial<IOnFunction>; | ||
asyncConfigCallback?: PluginConfigCallback; | ||
childBuilders: PluginBuilder[]; | ||
} | ||
|
||
class PluginBuilder { | ||
private id: string; | ||
private _config: Partial<IConfig> = {}; | ||
private _callbacks: Partial<IOnFunction> = {}; | ||
private _asyncConfigCallback?: PluginConfigCallback; | ||
private _childBuilders: PluginBuilder[] = []; | ||
|
||
constructor(id: string) { | ||
this.id = id; | ||
this._config.title = id; | ||
} | ||
|
||
title(title: string): PluginBuilder { | ||
this._config.title = title; | ||
return this; | ||
} | ||
icon(icon: React.ReactElement | null): PluginBuilder { | ||
this._config.icon = icon; | ||
return this; | ||
} | ||
display(display: IDisplayOnOptions | IDisplayOnOptions[]): PluginBuilder { | ||
this._config.display = display; | ||
return this; | ||
} | ||
elementType( | ||
elementType: | ||
| IElementTypeOptions | ||
| IElementTypeOptions[] | ||
| IDynamicFunction | ||
): PluginBuilder { | ||
this._config.elementType = elementType; | ||
return this; | ||
} | ||
render(renderFn: (...params: any) => React.ReactElement): PluginBuilder { | ||
this._config.render = renderFn; | ||
return this; | ||
} | ||
shouldOverride( | ||
shouldOverrideFn: (element: IRteElementType) => boolean | ||
): PluginBuilder { | ||
this._config.shouldOverride = shouldOverrideFn; | ||
return this; | ||
} | ||
on<T extends keyof IOnFunction>( | ||
type: T, | ||
callback: IOnFunction[T] | ||
): PluginBuilder { | ||
this._callbacks[type] = callback; | ||
return this; | ||
} | ||
configure(callback: PluginConfigCallback): PluginBuilder { | ||
this._asyncConfigCallback = callback; | ||
return this; | ||
} | ||
addPlugins(...builders: PluginBuilder[]): PluginBuilder { | ||
this._childBuilders.push(...builders); | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds and returns a definition of the RTE Plugin, ready to be materialized | ||
* into a concrete RTEPlugin instance later when the SDK and Plugin Factory are available. | ||
* This method no longer performs the actual creation of RTEPlugin instances. | ||
*/ | ||
build(): PluginDefinition { | ||
return { | ||
id: this.id, | ||
config: this._config, | ||
callbacks: this._callbacks, | ||
asyncConfigCallback: this._asyncConfigCallback, | ||
childBuilders: this._childBuilders, | ||
}; | ||
} | ||
} | ||
|
||
async function materializePlugin( | ||
pluginDef: PluginDefinition, | ||
sdk: UiLocation | ||
): Promise<Plugin> { | ||
let finalConfig: Partial<IConfig> = { ...pluginDef.config }; | ||
if (pluginDef.asyncConfigCallback) { | ||
const dynamicConfig = await Promise.resolve( | ||
pluginDef.asyncConfigCallback(sdk) | ||
); | ||
finalConfig = { ...finalConfig, ...dynamicConfig }; | ||
} | ||
const plugin = rtePluginInitializer( | ||
pluginDef.id, | ||
(rte: IRteParam | void) => { | ||
// The rte parameter is passed when the plugin is actually used | ||
// finalConfig already contains the merged configuration | ||
return finalConfig; | ||
} | ||
); | ||
Object.entries(pluginDef.callbacks).forEach(([type, callback]) => { | ||
// Wrap callbacks with error handling | ||
const wrappedCallback = (params: any) => { | ||
Amitkanswal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try { | ||
return callback(params); | ||
} catch (error) { | ||
console.error(`Error in plugin callback ${type}:`, error); | ||
// Don't re-throw to prevent breaking the RTE | ||
} | ||
}; | ||
plugin.on(type as keyof IOnFunction, wrappedCallback); | ||
}); | ||
if (pluginDef.childBuilders.length > 0) { | ||
const childPlugins = await Promise.all( | ||
pluginDef.childBuilders.map((childBuilder) => | ||
materializePlugin(childBuilder.build(), sdk) | ||
) | ||
); | ||
plugin.addPlugins(...childPlugins); | ||
} | ||
|
||
return plugin; | ||
} | ||
|
||
function registerPlugins( | ||
...pluginDefinitions: PluginDefinition[] | ||
): ( | ||
context: InitializationData, | ||
rte: IRteParam | ||
) => Promise<{ [key: string]: Plugin }> { | ||
const definitionsToProcess = [...pluginDefinitions]; | ||
const plugins = async (context: InitializationData, rte: IRteParam) => { | ||
try { | ||
const sdk = new UiLocation(context); | ||
console.log("sdk", sdk); | ||
Amitkanswal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const materializedPlugins: { [key: string]: Plugin } = {}; | ||
console.log("materializedPlugins", materializedPlugins); | ||
|
||
for (const def of definitionsToProcess) { | ||
const pluginInstance = await materializePlugin(def, sdk); | ||
materializedPlugins[def.id] = pluginInstance; | ||
} | ||
rte.sdk = sdk; | ||
console.log("rte", rte); | ||
console.log("materializedPlugins", materializedPlugins); | ||
|
||
return materializedPlugins; | ||
} catch (err) { | ||
console.error("Error during plugin registration:", err); | ||
throw err; | ||
} | ||
}; | ||
return plugins; | ||
} | ||
|
||
export { | ||
IConfig, | ||
IDisplayOnOptions, | ||
IDynamicFunction, | ||
IElementTypeOptions, | ||
IOnFunction, | ||
IRteElementType, | ||
IRteParam, | ||
Plugin, | ||
PluginBuilder, | ||
PluginDefinition, | ||
registerPlugins | ||
}; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.