use-event.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { Size } from "@/utils/math.ts";
  2. import { listener } from "../../utils/event.ts";
  3. import { globalWatch, installGlobalVar, useStage } from "./use-global-vars.ts";
  4. import { nextTick, reactive, ref, watch, watchEffect } from "vue";
  5. import { KonvaEventObject } from "konva/lib/Node";
  6. import { debounce } from "@/utils/shared.ts";
  7. import { Shape } from "konva/lib/Shape";
  8. import { shapeTreeContain } from "@/utils/shape.ts";
  9. import { EntityShape } from "@/deconstruction.js";
  10. export const useListener = <
  11. T extends HTMLElement,
  12. K extends keyof HTMLElementEventMap
  13. >(
  14. eventName: K,
  15. callback: (
  16. this: T,
  17. ev: HTMLElementEventMap[K],
  18. stageDOM: HTMLDivElement
  19. ) => any,
  20. target?: HTMLElement | Window
  21. ) => {
  22. const stage = useStage();
  23. return watchEffect((onCleanup) => {
  24. if (stage.value) {
  25. const $stage = stage.value!.getStage();
  26. const dom = $stage.container() as any;
  27. onCleanup(
  28. listener(target || dom, eventName, function (ev) {
  29. callback.call(this, ev, dom);
  30. })
  31. );
  32. }
  33. });
  34. };
  35. export const useGlobalResize = installGlobalVar(() => {
  36. const stage = useStage();
  37. const size = ref<Size>();
  38. const setSize = () => {
  39. if (fix.value) return;
  40. const container = stage.value?.getStage().container();
  41. if (container) {
  42. container.style.setProperty("display", "none");
  43. }
  44. const dom = stage.value!.getNode().container().parentElement!;
  45. size.value = {
  46. width: dom.offsetWidth,
  47. height: dom.offsetHeight,
  48. };
  49. if (container) {
  50. container.style.removeProperty("display");
  51. }
  52. };
  53. const stopWatch = watchEffect(() => {
  54. if (stage.value) {
  55. setSize();
  56. nextTick(() => stopWatch());
  57. }
  58. });
  59. const fix = ref(false);
  60. const setFixSize = (fixSize: { width: number; height: number } | null) => {
  61. if (fixSize) {
  62. size.value = { ...fixSize };
  63. const unWatch = watchEffect(() => {
  64. const $stage = stage.value?.getStage();
  65. if ($stage) {
  66. $stage.width(fixSize.width);
  67. $stage.height(fixSize.height);
  68. nextTick(() => unWatch());
  69. }
  70. });
  71. }
  72. fix.value = !!fixSize;
  73. setSize();
  74. };
  75. return {
  76. var: {
  77. setFixSize: setFixSize,
  78. updateSize: setSize,
  79. size,
  80. fix,
  81. },
  82. onDestroy: listener(window, "resize", debounce(setSize, 16)),
  83. };
  84. }, Symbol("resize"));
  85. export const useResize = () => useGlobalResize().size;
  86. export const usePreemptiveListener = installGlobalVar(() => {
  87. const cbs: Record<string, ((ev: Event) => void)[]> = {};
  88. const eventDestorys: Record<string, () => void> = {};
  89. const stage = useStage();
  90. const add = (eventName: string, cb: (ev: Event) => void) => {
  91. if (!cbs[eventName]) {
  92. cbs[eventName] = [];
  93. const $stage = stage.value!.getStage();
  94. const dom = $stage.container() as any;
  95. eventDestorys[eventName] = listener(dom, eventName as any, (ev: any) => {
  96. console.log(cbs[eventName], cbs[eventName][0]);
  97. cbs[eventName][0].call(this, ev);
  98. });
  99. }
  100. cbs[eventName].push(cb);
  101. };
  102. const remove = (eventName: string, cb?: (ev: Event) => void) => {
  103. if (!cbs[eventName]) return;
  104. if (cb) {
  105. const index = cbs[eventName].indexOf(cb);
  106. if (index !== -1) {
  107. cbs[eventName].splice(index, 1);
  108. }
  109. if (cbs[eventName].length === 0) {
  110. delete cbs[eventName];
  111. }
  112. } else {
  113. delete cbs[eventName];
  114. }
  115. if (!cbs[eventName]) {
  116. eventDestorys[eventName]();
  117. }
  118. };
  119. const on = <T extends keyof HTMLElementEventMap>(
  120. eventName: T,
  121. callback: (this: HTMLDivElement, ev: HTMLElementEventMap[T]) => any
  122. ) => {
  123. const stopWatch = watchEffect((onCleanup) => {
  124. if (stage.value) {
  125. add(eventName, callback as any);
  126. onCleanup(() => {
  127. remove(eventName, callback as any);
  128. });
  129. }
  130. });
  131. return stopWatch;
  132. };
  133. return on;
  134. }, Symbol("preemptiveListener"));
  135. export const cancelBubbleEvent = (ev: KonvaEventObject<any>) => {
  136. ev.cancelBubble = true;
  137. ev.evt.stopPropagation;
  138. };
  139. export const useGlobalOnlyRightClickShape = installGlobalVar(() => {
  140. const stage = useStage();
  141. const checkShapes = reactive([]) as EntityShape[];
  142. const events = [] as Array<((ev: MouseEvent) => void)[]>;
  143. const cancels = [] as Array<(((ev?: MouseEvent) => void) | undefined)[]>;
  144. const addShape = (
  145. shape: EntityShape,
  146. fn: (ev: MouseEvent) => void,
  147. cancel?: (ev?: MouseEvent) => void
  148. ) => {
  149. const ndx = checkShapes.indexOf(shape);
  150. if (~ndx) {
  151. events[ndx].push(fn);
  152. cancels[ndx].push(cancel);
  153. } else {
  154. checkShapes.push(shape);
  155. events.push([fn]);
  156. cancels.push([cancel]);
  157. }
  158. return () => delShape(shape, fn);
  159. };
  160. const delShape = (shape: EntityShape, fn: (ev: MouseEvent) => void) => {
  161. if (shape === prevShape) {
  162. cancel()
  163. }
  164. const ndx = checkShapes.indexOf(shape);
  165. if (!~ndx) return;
  166. const evNdx = events[ndx].indexOf(fn);
  167. if (!~evNdx) return;
  168. events[ndx].splice(evNdx, 1);
  169. cancels[ndx].splice(evNdx, 1);
  170. if (events[ndx].length === 0) {
  171. checkShapes.splice(ndx, 1);
  172. cancels.splice(ndx, 1);
  173. events.splice(ndx, 1);
  174. }
  175. };
  176. let prevShape: EntityShape | null = null;
  177. const cancel = () => {
  178. if (prevShape) {
  179. const prevNdx = checkShapes.indexOf(prevShape!);
  180. ~prevNdx && cancels[prevNdx].forEach((fn) => fn && fn());
  181. prevShape = null
  182. }
  183. }
  184. const init = (dom: HTMLDivElement) => {
  185. const hasRClick = (ev: MouseEvent) => {
  186. let clickShape: any
  187. let ndx = -1
  188. if (ev.button === 2) {
  189. const pos = stage.value?.getNode().pointerPos;
  190. if (!pos) return false;
  191. clickShape = stage.value?.getNode().getIntersection(pos);
  192. console.log(clickShape)
  193. do {
  194. ndx = checkShapes.indexOf(clickShape!);
  195. if (~ndx) {
  196. break;
  197. }
  198. } while ((clickShape = clickShape?.parent));
  199. }
  200. cancel()
  201. prevShape = clickShape;
  202. return ~ndx && events[ndx].forEach((fn) => fn(ev));
  203. };
  204. dom.addEventListener("contextmenu", hasRClick);
  205. dom.addEventListener("pointerdown", hasRClick);
  206. return () => {
  207. cancel()
  208. dom.removeEventListener("contextmenu", hasRClick);
  209. dom.removeEventListener("pointerdown", hasRClick);
  210. };
  211. };
  212. globalWatch(
  213. () => stage.value?.getNode().container(),
  214. (dom, _, onCleanup) => {
  215. if (dom) {
  216. onCleanup(init(dom));
  217. }
  218. },
  219. { immediate: true }
  220. );
  221. return {
  222. add: addShape,
  223. remove: delShape,
  224. };
  225. });