custom-layer.mdx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Custom Layer
  2. We split the canvas into multiple Layers, implementing the concept of interaction layering for better plugin management. For more details, see [Canvas Engine](guide/concepts/canvas-engine.html)
  3. 1. Use `observeEntityDatas`, `observeEntities`, and `observeEntity` to monitor updates to any data module of canvas nodes
  4. 2. Use `onZoom`, `onScroll`, `onViewportChange`, etc. to monitor canvas zooming or scrolling
  5. 3. Use `render` to insert React elements into the canvas, such as drawing SVG lines
  6. ![Aspect-oriented programming](@/public/en-layer-uml.jpg)
  7. ## Creating a Layer
  8. ```tsx pure
  9. import { FreeLayoutPluginContext, inject, injectable, FlowNodeEntity, FlowNodeTransformData, FlowNodeFormData } from '@flowgram.ai/free-layout-editor'
  10. @injectable()
  11. export class MyLayer extends Layer {
  12. @inject(FreeLayoutPluginContext) ctx: FreeLayoutPluginContext
  13. // Can monitor node width, height, and position changes
  14. @observeEntityDatas(FlowNodeEntity, FlowNodeTransformData) transformDatas: FlowNodeTransformData[];
  15. // Can monitor form data changes, connection data can be stored in forms
  16. @observeEntityDatas(FlowNodeEntity, FlowNodeFormData) formDatas: FlowNodeFormData[];
  17. onReady() {
  18. // Can also add styles
  19. // zIndex controls whether to overlay nodes, nodes default to 10, greater than 10 will be above nodes
  20. this.node.style.zIndex = 11;
  21. }
  22. onZoom(scale) {
  23. // Scale with canvas
  24. this.node.style.transform = `scale(${scale})`;
  25. }
  26. render() {
  27. return <div>{...}</div>
  28. }
  29. }
  30. ```
  31. ## Adding to Canvas
  32. - Through use-editor-props
  33. ```ts pure
  34. {
  35. onInit: (ctx) => {
  36. ctx.playground.registerLayer(MyLayer)
  37. }
  38. }
  39. ```
  40. - Through plugin
  41. ```tsx pure
  42. import { FreeLayoutPluginContext } from '@flowgram.ai/free-layout-editor'
  43. export const createMyPlugin = definePluginCreator<{}, FreeLayoutPluginContext>({
  44. onInit: (ctx, opts) => {
  45. ctx.playground.registerLayer(MyLayer)
  46. },
  47. });
  48. ```
  49. ## Layer Lifecycle Description
  50. ```ts
  51. interface Layer {
  52. /**
  53. * Triggered during initialization
  54. */
  55. onReady?(): void;
  56. /**
  57. * Triggered when playground size changes
  58. */
  59. onResize?(size: PipelineDimension): void;
  60. /**
  61. * Triggered when playground is focused
  62. */
  63. onFocus?(): void;
  64. /**
  65. * Triggered when playground loses focus
  66. */
  67. onBlur?(): void;
  68. /**
  69. * Monitor zoom
  70. */
  71. onZoom?(scale: number): void;
  72. /**
  73. * Monitor scroll
  74. */
  75. onScroll?(scroll: { scrollX: number; scrollY: number }): void;
  76. /**
  77. * Triggered when viewport updates
  78. */
  79. onViewportChange?(): void;
  80. /**
  81. * Triggered when readonly or disabled state changes
  82. * @param state
  83. */
  84. onReadonlyOrDisabledChange?(state: { disabled: boolean; readonly: boolean }): void;
  85. /**
  86. * Data updates automatically trigger React render, if not provided React rendering won't be called
  87. */
  88. render?(): JSX.Element
  89. }
  90. ```