ElementPositionPanel.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div class="element-positopn-panel">
  3. <ButtonGroup class="row">
  4. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="置顶层">
  5. <Button style="flex: 1;" @click="orderElement(handleElement, 'top')"><IconSendToBack /></Button>
  6. </Tooltip>
  7. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="置底层">
  8. <Button style="flex: 1;" @click="orderElement(handleElement, 'bottom')"><IconBringToFrontOne /></Button>
  9. </Tooltip>
  10. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="上移一层">
  11. <Button style="flex: 1;" @click="orderElement(handleElement, 'up')"><IconBringToFront /></Button>
  12. </Tooltip>
  13. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="下移一层">
  14. <Button style="flex: 1;" @click="orderElement(handleElement, 'down')"><IconSentToBack /></Button>
  15. </Tooltip>
  16. </ButtonGroup>
  17. <ButtonGroup class="row">
  18. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="左对齐">
  19. <Button style="flex: 1;" @click="alignElementToCanvas('left')"><IconAlignLeft /></Button>
  20. </Tooltip>
  21. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="水平居中">
  22. <Button style="flex: 1;" @click="alignElementToCanvas('horizontal')"><IconAlignVertically /></Button>
  23. </Tooltip>
  24. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="右对齐">
  25. <Button style="flex: 1;" @click="alignElementToCanvas('right')"><IconAlignRight /></Button>
  26. </Tooltip>
  27. </ButtonGroup>
  28. <ButtonGroup class="row">
  29. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="上对齐">
  30. <Button style="flex: 1;" @click="alignElementToCanvas('top')"><IconAlignTop /></Button>
  31. </Tooltip>
  32. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="垂直居中">
  33. <Button style="flex: 1;" @click="alignElementToCanvas('vertical')"><IconAlignHorizontally /></Button>
  34. </Tooltip>
  35. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="下对齐">
  36. <Button style="flex: 1;" @click="alignElementToCanvas('bottom')"><IconAlignBottom /></Button>
  37. </Tooltip>
  38. </ButtonGroup>
  39. <Divider />
  40. <div class="row">
  41. <div style="flex: 3;">位置:</div>
  42. <InputNumber
  43. :step="5"
  44. :value="left"
  45. @change="value => updateLeft(value)"
  46. style="flex: 4;"
  47. />
  48. <div style="flex: 1;"></div>
  49. <InputNumber
  50. :step="5"
  51. :value="top"
  52. @change="value => updateTop(value)"
  53. style="flex: 4;"
  54. />
  55. </div>
  56. <div class="row">
  57. <div style="flex: 3;"></div>
  58. <div style="flex: 4;" class="label">X</div>
  59. <div style="flex: 1;"></div>
  60. <div style="flex: 4;" class="label">Y</div>
  61. </div>
  62. <template v-if="handleElement.type !== 'line'">
  63. <div class="row">
  64. <div style="flex: 3;">大小:</div>
  65. <InputNumber
  66. :min="minSize"
  67. :max="1500"
  68. :step="5"
  69. :value="width"
  70. @change="value => updateWidth(value)"
  71. style="flex: 4;"
  72. />
  73. <template v-if="['image', 'shape'].includes(handleElement.type)">
  74. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="解除宽高比锁定" v-if="fixedRatio">
  75. <IconLock style="flex: 1;" class="icon-btn" @click="updateFixedRatio(false)" />
  76. </Tooltip>
  77. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="宽高比锁定" v-else>
  78. <IconUnlock style="flex: 1;" class="icon-btn" @click="updateFixedRatio(true)" />
  79. </Tooltip>
  80. </template>
  81. <div style="flex: 1;" v-else></div>
  82. <InputNumber
  83. :min="minSize"
  84. :max="800"
  85. :step="5"
  86. :disabled="handleElement.type === 'text'"
  87. :value="height"
  88. @change="value => updateHeight(value)"
  89. style="flex: 4;"
  90. />
  91. </div>
  92. <div class="row">
  93. <div style="flex: 3;"></div>
  94. <div style="flex: 4;" class="label">宽</div>
  95. <div style="flex: 1;"></div>
  96. <div style="flex: 4;" class="label">高</div>
  97. </div>
  98. </template>
  99. <template v-if="['text', 'image', 'shape'].includes(handleElement.type)">
  100. <Divider />
  101. <div class="row">
  102. <div style="flex: 3;">旋转:</div>
  103. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="逆时针旋转">
  104. <IconRotate class="icon-btn" @click="updateRotate45('-')" style="flex: 2;" />
  105. </Tooltip>
  106. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="顺时针旋转">
  107. <IconRotate
  108. class="icon-btn"
  109. @click="updateRotate45('+')"
  110. :style="{
  111. flex: 2,
  112. transform: 'rotateY(180deg)',
  113. }"
  114. />
  115. </Tooltip>
  116. <div style="flex: 1;"></div>
  117. <InputNumber
  118. :min="-180"
  119. :max="180"
  120. :step="5"
  121. :value="rotate"
  122. @change="value => updateRotate(value)"
  123. style="flex: 4;"
  124. />
  125. </div>
  126. </template>
  127. </div>
  128. </template>
  129. <script lang="ts">
  130. import { computed, defineComponent, ref, watch } from 'vue'
  131. import round from 'lodash/round'
  132. import { MutationTypes, useStore } from '@/store'
  133. import { PPTElement } from '@/types/slides'
  134. import { MIN_SIZE } from '@/configs/element'
  135. import useOrderElement from '@/hooks/useOrderElement'
  136. import useAlignElementToCanvas from '@/hooks/useAlignElementToCanvas'
  137. import useHistorySnapshot from '@/hooks/useHistorySnapshot'
  138. export default defineComponent({
  139. name: 'element-positopn-panel',
  140. setup() {
  141. const store = useStore()
  142. const handleElement = computed<PPTElement>(() => store.getters.handleElement)
  143. const left = ref(0)
  144. const top = ref(0)
  145. const width = ref(0)
  146. const height = ref(0)
  147. const rotate = ref(0)
  148. const fixedRatio = ref(false)
  149. const minSize = computed(() => {
  150. if(!handleElement.value) return 20
  151. return MIN_SIZE[handleElement.value.type] || 20
  152. })
  153. watch(handleElement, () => {
  154. if(!handleElement.value) return
  155. left.value = round(handleElement.value.left, 1)
  156. top.value = round(handleElement.value.top, 1)
  157. fixedRatio.value = 'fixedRatio' in handleElement.value && !!handleElement.value.fixedRatio
  158. if(handleElement.value.type !== 'line') {
  159. width.value = round(handleElement.value.width, 1)
  160. height.value = round(handleElement.value.height, 1)
  161. rotate.value = 'rotate' in handleElement.value && handleElement.value.rotate !== undefined ? round(handleElement.value.rotate, 1) : 0
  162. }
  163. }, { deep: true, immediate: true })
  164. const { orderElement } = useOrderElement()
  165. const { alignElementToCanvas } = useAlignElementToCanvas()
  166. const { addHistorySnapshot } = useHistorySnapshot()
  167. const updateLeft = (value: number) => {
  168. const props = { left: value }
  169. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  170. addHistorySnapshot()
  171. }
  172. const updateTop = (value: number) => {
  173. const props = { top: value }
  174. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  175. addHistorySnapshot()
  176. }
  177. const updateWidth = (value: number) => {
  178. const props = { width: value }
  179. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  180. addHistorySnapshot()
  181. }
  182. const updateHeight = (value: number) => {
  183. const props = { height: value }
  184. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  185. addHistorySnapshot()
  186. }
  187. const updateRotate = (value: number) => {
  188. const props = { rotate: value }
  189. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  190. addHistorySnapshot()
  191. }
  192. const updateFixedRatio = (value: boolean) => {
  193. const props = { fixedRatio: value }
  194. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  195. addHistorySnapshot()
  196. }
  197. const updateRotate45 = (command: '+' | '-') => {
  198. let _rotate = Math.floor(rotate.value / 45) * 45
  199. if(command === '+') _rotate = _rotate + 45
  200. else if(command === '-') _rotate = _rotate - 45
  201. if(_rotate < -180) _rotate = -180
  202. if(_rotate > 180) _rotate = 180
  203. const props = { rotate: _rotate }
  204. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  205. addHistorySnapshot()
  206. }
  207. return {
  208. handleElement,
  209. orderElement,
  210. alignElementToCanvas,
  211. left,
  212. top,
  213. width,
  214. height,
  215. rotate,
  216. fixedRatio,
  217. minSize,
  218. updateLeft,
  219. updateTop,
  220. updateWidth,
  221. updateHeight,
  222. updateRotate,
  223. updateFixedRatio,
  224. updateRotate45,
  225. }
  226. },
  227. })
  228. </script>
  229. <style lang="scss" scoped>
  230. .row {
  231. width: 100%;
  232. display: flex;
  233. align-items: center;
  234. margin-bottom: 10px;
  235. }
  236. .label {
  237. text-align: center;
  238. }
  239. .icon-btn {
  240. cursor: pointer;
  241. }
  242. </style>