register.ts 720 B

123456789101112131415161718192021222324252627282930
  1. import { shallowReadonly } from "vue";
  2. import { Attrib, EntityClass } from "./type";
  3. import { Container } from "./packages/container";
  4. export const register = <T extends string, R extends Attrib>(types: {
  5. [key in T]: EntityClass<R>;
  6. }) => {
  7. const initBoard = (dom: HTMLDivElement, data?: { [key in T]?: R[] }) => {
  8. const container = new Container({
  9. dom,
  10. types,
  11. data,
  12. });
  13. container.init();
  14. return {
  15. stage: container,
  16. setData(newData: { [key in T]?: R[] }) {
  17. container.setData(newData);
  18. },
  19. getData() {
  20. return shallowReadonly(container.data);
  21. },
  22. destory() {
  23. container.destory();
  24. },
  25. };
  26. };
  27. return initBoard;
  28. };