component.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Dict_Imgs } from "@/dict";
  2. import { useEditor } from "@/modules/editor";
  3. import { defineComponent, watch } from "vue";
  4. import { string } from "vue-types";
  5. import { useCompData } from ".";
  6. import { View } from "../View";
  7. export const Component = defineComponent({
  8. props: {
  9. compId: string().isRequired,
  10. },
  11. setup(props) {
  12. const comp = useCompData(props.compId);
  13. const { actions, helper, store, controls } = useEditor();
  14. function setImageSize(url: string) {
  15. if (!helper.isStreamCardChild(props.compId)) return;
  16. const temImg = new Image();
  17. temImg.src = url;
  18. temImg.onload = function () {
  19. comp.setH(comp.getW() / (temImg.width / temImg.height));
  20. actions.onCompLayoutUpdated(comp);
  21. helper.extendStreamCard(store.currStreamCardId);
  22. };
  23. }
  24. async function changeVal() {
  25. try {
  26. const url = await controls.pickCtrl.pickOneImage();
  27. if (!url) return;
  28. comp.value.url = url;
  29. comp.value.x = 0;
  30. comp.value.y = 0;
  31. comp.value.s = 1;
  32. } catch (error) {
  33. console.log(error);
  34. }
  35. }
  36. watch(
  37. () => comp.value.url,
  38. (value, oldValue) => {
  39. if (oldValue !== Dict_Imgs.Default) return;
  40. setImageSize(value);
  41. }
  42. );
  43. return () => {
  44. const value = comp.value;
  45. const scale = value?.s || 1;
  46. const ox = value?.x || 0;
  47. const oy = value?.y || 0;
  48. const objectFit =
  49. scale + "" == "1" && ox + "" == "0" && oy + "" == "0"
  50. ? "cover"
  51. : "contain";
  52. return (
  53. <View
  54. class="overflow-hidden"
  55. compId={props.compId}
  56. onDblclick={store.isEditMode ? changeVal : undefined}
  57. onClick={() => {
  58. if (value.showLink && value.link && !store.isEditMode) {
  59. window.location.href = value.link;
  60. } else {
  61. controls.wxCtrl.setPreviewData(value.url, store.previewImageList);
  62. }
  63. }}
  64. >
  65. <img
  66. crossorigin="anonymous"
  67. class={"w-1/1 h-1/1 object-cover pointer-events-none"}
  68. style={{
  69. transform: `scale(${scale}) translate(${ox}%,${oy}%)`,
  70. objectFit,
  71. }}
  72. src={
  73. value.url.startsWith("data:image/png")
  74. ? value.url
  75. : value.url + "?editMode=" + store.isEditMode
  76. }
  77. />
  78. </View>
  79. );
  80. };
  81. },
  82. });