hooks.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { useEditor } from "@/modules/editor";
  2. import { inject, onMounted, provide, ref } from "vue";
  3. export function useCompRef(compId: string) {
  4. const compRef = ref();
  5. const { store, helper } = useEditor();
  6. const parentId = compId !== "root" ? (inject("compParentId") as string) : "";
  7. provide("compParentId", compId);
  8. store.setCompPid(compId, parentId);
  9. onMounted(() => {
  10. const comp = helper.findComp(compId);
  11. if (comp) {
  12. Object.defineProperties(comp, {
  13. $el: {
  14. value: compRef.value,
  15. configurable: true,
  16. },
  17. });
  18. compRef.value.compId= compId;
  19. compRef.value.compKey= comp.compKey;
  20. }
  21. if (comp?.compKey == "Page" && store.isPreview && store.rootPage.value.pageMode == "short") {
  22. let downY = 0;
  23. compRef.value.addEventListener("touchstart", (e:TouchEvent)=>{
  24. // e.preventDefault();
  25. // e.stopPropagation();
  26. console.log("touch start");
  27. store.shortPage.offset = 0;
  28. downY = e.targetTouches[0].clientY;
  29. store.shortPage.isMoving = false;
  30. })
  31. compRef.value.addEventListener("touchmove", (e:TouchEvent)=>{
  32. e.preventDefault();
  33. e.stopPropagation();
  34. let offset = e.targetTouches[0].clientY - downY
  35. console.log("touch moving", offset);
  36. store.shortPage.isMoving = true;
  37. if (offset < 0 ) {
  38. if (store.shortPage.index == store.streamCardIds.length -1) {
  39. offset = 0;
  40. }
  41. }
  42. if (offset > 0 ) {
  43. if (store.shortPage.index == 0) {
  44. offset = 0;
  45. }
  46. }
  47. store.shortPage.offset = offset;
  48. })
  49. compRef.value.addEventListener("touchend", (e:TouchEvent)=>{
  50. store.shortPage.isMoving = false;
  51. if (store.shortPage.offset < 0 ) {
  52. store.shortPage.index +=1;
  53. }
  54. if (store.shortPage.offset > 0 ) {
  55. store.shortPage.index -=1;
  56. if (store.shortPage.index < 0 ) {
  57. store.shortPage.index = 0;
  58. }
  59. }
  60. store.shortPage.offset = 0;
  61. });
  62. }
  63. });
  64. return compRef;
  65. }
  66. export function useCompEditLayerRef(compId: string) {
  67. const editLayerRef = ref<HTMLElement>();
  68. const { helper, store } = useEditor();
  69. onMounted(() => {
  70. if ( !editLayerRef.value ) return;
  71. const comp = helper.findComp(compId);
  72. if (comp) {
  73. Object.defineProperties(comp, {
  74. $editor: {
  75. value: editLayerRef.value,
  76. configurable: true,
  77. },
  78. });
  79. const dom = editLayerRef.value
  80. helper.setCompEditLayer(compId, ()=>{
  81. const {left, top, width, height} = dom.getBoundingClientRect();
  82. return {
  83. left,top, width,height
  84. }
  85. })
  86. }
  87. });
  88. return editLayerRef;
  89. }