component.tsx 2.1 KB

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