root.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import { CanvasElementOptions, CanvasFrameOptions } from 'app/features/canvas';
  3. import { FrameState } from './frame';
  4. import { Scene } from './scene';
  5. export class RootElement extends FrameState {
  6. constructor(public options: CanvasFrameOptions, public scene: Scene, private changeCallback: () => void) {
  7. super(options, scene);
  8. this.sizeStyle = {
  9. height: '100%',
  10. width: '100%',
  11. };
  12. }
  13. isRoot(): this is RootElement {
  14. return true;
  15. }
  16. // root type can not change
  17. onChange(options: CanvasElementOptions) {
  18. this.revId++;
  19. this.options = { ...options } as CanvasFrameOptions;
  20. this.changeCallback();
  21. }
  22. getSaveModel(): CanvasFrameOptions {
  23. const { placement, constraint, ...rest } = this.options;
  24. return {
  25. ...rest, // everything except placement & constraint
  26. elements: this.elements.map((v) => v.getSaveModel()),
  27. };
  28. }
  29. setRootRef = (target: HTMLDivElement) => {
  30. this.div = target;
  31. };
  32. render() {
  33. return (
  34. <div
  35. onContextMenu={(event) => event.preventDefault()}
  36. key={this.UID}
  37. ref={this.setRootRef}
  38. style={{ ...this.sizeStyle, ...this.dataStyle }}
  39. >
  40. {this.elements.map((v) => v.render())}
  41. </div>
  42. );
  43. }
  44. }