component.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { defineComponent, ref, watch } from "vue";
  2. import { string } from "vue-types";
  3. import { useCompData } from ".";
  4. import { View } from "../View";
  5. import { useEditor } from "@/modules/editor";
  6. import { IconPicture } from "@queenjs/icons";
  7. import { css } from "@linaria/core";
  8. export const Component = defineComponent({
  9. props: {
  10. compId: string().isRequired,
  11. },
  12. setup(props) {
  13. const { store, controls, actions } = useEditor();
  14. const comp = useCompData(props.compId);
  15. const { value } = comp;
  16. const videoRef = ref();
  17. async function changeVal() {
  18. try {
  19. const url = await controls.pickCtrl.pickOneImage();
  20. if (!url) return;
  21. value.url = url;
  22. } catch (error) {
  23. console.log(error);
  24. }
  25. }
  26. watch(
  27. () => [value.ratio],
  28. () => {
  29. const { videoWidth, videoHeight } = videoRef.value;
  30. const t = value.ratio || videoWidth / videoHeight;
  31. comp.setH(comp.getW() / t);
  32. actions.onCompLayoutUpdated(comp);
  33. }
  34. );
  35. return () => {
  36. const options: any = {};
  37. if (value.autoplay) options.autoplay = true;
  38. if (value.loop) options.loop = true;
  39. if (value.controls) options.controls = true;
  40. return (
  41. <View class={rootCls} compId={props.compId}>
  42. <video
  43. ref={videoRef}
  44. key={value.url}
  45. class="w-full object-cover"
  46. style={{ aspectRatio: value.ratio }}
  47. {...options}
  48. >
  49. <source src={value.url}></source>
  50. </video>
  51. {store.isEditMode && (
  52. <IconPicture class={btnCls} onClick={changeVal} />
  53. )}
  54. </View>
  55. );
  56. };
  57. },
  58. });
  59. const btnCls = css`
  60. display: none;
  61. position: absolute;
  62. top: 50%;
  63. left: 50%;
  64. padding: 20px;
  65. font-size: 37.5px;
  66. color: #666;
  67. border-radius: 50%;
  68. background-color: rgba(255, 255, 255, 0.7);
  69. transform: translate(-50%, -50%);
  70. @apply shadow;
  71. `;
  72. const rootCls = css`
  73. &:hover {
  74. .${btnCls} {
  75. display: block;
  76. }
  77. }
  78. `;