View.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { css } from "@linaria/core";
  2. import { defineComponent } from "vue";
  3. import { string } from "vue-types";
  4. import { useEditor } from "../../..";
  5. import { Transfer } from "./Transfer";
  6. export const View = defineComponent({
  7. props: {
  8. compId: string().isRequired,
  9. },
  10. emits: ["dblclick"],
  11. setup(props, { slots, emit }) {
  12. const { store, actions, helper } = useEditor();
  13. return () => {
  14. const comp = helper.findComp(props.compId);
  15. if (!comp) return store.isEditMode ? <div>无效组件</div> : null;
  16. const isSelected = store.currCompId === props.compId;
  17. return (
  18. <div
  19. class={[
  20. viewStyle,
  21. store.isEditMode && viewEditStyle,
  22. isSelected && viewSelectedStyle,
  23. ]}
  24. style={helper.createStyle(comp.layout)}
  25. onClick={(e) => {
  26. e.stopPropagation();
  27. if (!isSelected) {
  28. actions.pickComp(props.compId as string);
  29. }
  30. }}
  31. onDblclick={() => emit("dblclick")}
  32. >
  33. {slots.default?.()}
  34. {isSelected && <Transfer compId={props.compId} />}
  35. </div>
  36. );
  37. };
  38. },
  39. });
  40. const viewStyle = css`
  41. position: relative;
  42. font-size: 0;
  43. z-index: 1;
  44. > :first-child {
  45. width: 100%;
  46. height: 100%;
  47. }
  48. `;
  49. const viewEditStyle = css`
  50. &:hover {
  51. outline: 2px dashed @inf-primary-color;
  52. }
  53. `;
  54. const viewSelectedStyle = css`
  55. z-index: 2;
  56. `;