index.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Polygons } from "./polygons";
  2. import { Manage } from "../openlayer";
  3. import { register, BoundQueryPlugin } from "drawing-board";
  4. import { PolygonsAttrib } from "@/request/type";
  5. const initBoard = register(
  6. { polygons: Polygons },
  7. { bound: new BoundQueryPlugin() }
  8. );
  9. export const createBoard = (container: HTMLDivElement, mapManage: Manage) => {
  10. const board = initBoard(container, { polygons: undefined }, false);
  11. const syncBound = () => {
  12. const bound = mapManage.getBound();
  13. board.bound.setSize(container.offsetWidth, container.offsetHeight);
  14. board.bound.setBound([bound[0], bound[3], bound[2], bound[1]]);
  15. };
  16. const { map } = mapManage;
  17. map.addEventListener("moveend", syncBound);
  18. board.bound.enableMove((newBound) => {
  19. mapManage.setBound(newBound);
  20. return false;
  21. });
  22. board.bound.enableWheel((newBound) => {
  23. mapManage.setBound(newBound);
  24. return false;
  25. });
  26. return {
  27. getData() {
  28. return board.getData().polygons;
  29. },
  30. setData(polygons: PolygonsAttrib, id: string) {
  31. board.setData({ polygons: { ...polygons, id } });
  32. },
  33. destory() {
  34. board.destory();
  35. map.removeEventListener("moveend", syncBound);
  36. },
  37. polygon() {
  38. console.log(board.tree);
  39. return board.tree.children[0] as Polygons;
  40. },
  41. getPixelFromCoordinate(point: number[]) {
  42. return board.tree.getPixelFromStage(point);
  43. },
  44. };
  45. };