<!--
 * @Author: ChenChanghua
 * @Date: 2023-03-09 15:33:54
 * @Description: file content
-->
<script setup>
import { ref, shallowRef, inject } from "vue";

const props = defineProps({
  data: Object,
  onAction: Object,
});

const focused = ref(false);
const focusSize = inject("focusSize");
const divRef = shallowRef(null);

// 自身的焦点状态自己记录, 通过回调来改变
const onFocus = () => {
  focused.value = true;
  divRef.value?.jsvGetBoundingClientRect().then(
    (data) => {
      focusSize.width = data.width;
      focusSize.height = data.height;
      focusSize.left = data.left;
      focusSize.top = data.top;
    },
    (error) => {
      console.log("get absolute position failed", error);
    }
  );
};
const onBlur = () => {
  focused.value = false;
};
const onClick = () => {
  console.log("item onclick ", props.data);
};

// 一般在create的时候进行回调的注册, 使用 option api的可以在created时进行注册
props.onAction.register("onFocus", onFocus);
props.onAction.register("onBlur", onBlur);
props.onAction.register("onClick", onClick);
</script>

<template>
  <div
    :style="{
      width: data.width,
      height: data.height,
      transform: focused ? 'scale3d(1.2, 1.2, 1.2)' : '',
      transition: 'transform 0.2s linear',
    }"
  >
    <div
      ref="divRef"
      :style="{
        width: data.width,
        height: data.height,
        fontSize: 30,
        color: '#FFFFFF',
        backgroundColor: data.color,
        borderRadius: 10,
      }"
    >
      {{ data.content }}
    </div>
  </div>
</template>