custom-plugin.mdx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Custom Plugin
  2. ## Plugin Lifecycle Explanation
  3. ```tsx pure
  4. /**
  5. * from: https://github.com/bytedance/flowgram.ai/blob/main/packages/canvas-engine/core/src/plugin/plugin.ts
  6. */
  7. import { ContainerModule, interfaces } from 'inversify';
  8. export interface PluginBindConfig {
  9. bind: interfaces.Bind;
  10. unbind: interfaces.Unbind;
  11. isBound: interfaces.IsBound;
  12. rebind: interfaces.Rebind;
  13. }
  14. export interface PluginConfig<Opts, CTX extends PluginContext = PluginContext> {
  15. /**
  16. * Plugin IOC registration, equivalent to containerModule
  17. * @param ctx
  18. */
  19. onBind?: (bindConfig: PluginBindConfig, opts: Opts) => void;
  20. /**
  21. * Canvas registration phase
  22. */
  23. onInit?: (ctx: CTX, opts: Opts) => void;
  24. /**
  25. * Canvas preparation phase, generally used for DOM event registration, etc.
  26. */
  27. onReady?: (ctx: CTX, opts: Opts) => void;
  28. /**
  29. * Canvas destruction phase
  30. */
  31. onDispose?: (ctx: CTX, opts: Opts) => void;
  32. /**
  33. * After all layers of the canvas are rendered
  34. */
  35. onAllLayersRendered?: (ctx: CTX, opts: Opts) => void;
  36. /**
  37. * IOC module, used for more low - level plugin extensions
  38. */
  39. containerModules?: interfaces.ContainerModule[];
  40. }
  41. ```
  42. ## Create a Plugin
  43. ```tsx pure
  44. /**
  45. * If you want the plugin to be usable in both fixed and free layouts, please use
  46. * import { definePluginCreator } from '@flowgram.ai/core'
  47. */
  48. import { definePluginCreator, FixedLayoutPluginContext } from '@flowgram.ai/fixed-layout-editor'
  49. export interface MyPluginOptions {
  50. opt1: string;
  51. }
  52. export const createMyPlugin = definePluginCreator<MyPluginOptions, FixedLayoutPluginContext>({
  53. onBind: (bindConfig, opts) => {
  54. // Register the IOC module. See Custom Service for how to define a Service.
  55. bindConfig.bind(MyService).toSelf().inSingletonScope()
  56. },
  57. onInit: (ctx, opts) => {
  58. // Plugin configuration
  59. console.log(opts.opt1)
  60. // ctx corresponds to FixedLayoutPluginContext or FreeLayoutPluginContext
  61. console.log(ctx.document)
  62. console.log(ctx.playground)
  63. console.log(ctx.get<MyService>(MyService)) // Get the IOC module
  64. },
  65. });
  66. ```
  67. ## Add a Plugin
  68. ```tsx pure title="use-editor-props.ts"
  69. // EditorProps
  70. {
  71. plugins: () => [
  72. createMyPlugin({
  73. opt1: 'xxx'
  74. })
  75. ]
  76. }
  77. ```