MarkHeader.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="flex items-center p-l-base p-r-base common-mark-header">
  3. <ul class="flex items-center">
  4. <li
  5. v-for="button in usedOperations"
  6. :key="button.type"
  7. :ref="(el) => bindRef(button.type, el)"
  8. class="grid pointer radius-circle fill-light operation-button"
  9. :title="button.title"
  10. @click="onOperationClick(button)"
  11. >
  12. <svg-icon :name="attrs['icon-' + button.type] as string || 'mark-' + button.type"></svg-icon>
  13. </li>
  14. </ul>
  15. <color-picker v-model="frontColor" :virtual-ref="refs['front-color']" virtual-triggering></color-picker>
  16. <color-picker v-model="backgroundColor" :virtual-ref="refs['background-color']" virtual-triggering></color-picker>
  17. <div class="flex flex-1 items-center mark-header">
  18. <slot></slot>
  19. <span class="data-item">
  20. <message :reply-user-id="props.replyUserId" :paper-path="props.paperPath"></message>
  21. </span>
  22. <span class="data-item"><user-info></user-info></span>
  23. </div>
  24. </div>
  25. </template>
  26. <script setup lang="ts" name="MarkHeader">
  27. import { reactive, ref, computed, useAttrs, watch } from 'vue'
  28. import useFetch from '@/hooks/useFetch'
  29. import SvgIcon from '@/components/common/SvgIcon.vue'
  30. import ColorPicker from '@/components/common/ColorPicker.vue'
  31. import Message from '@/components/shared/message/Message.vue'
  32. import UserInfo from '@/components/shared/UserInfo.vue'
  33. type ButtonType =
  34. | 'back'
  35. | 'scale-up'
  36. | 'scale-down'
  37. | 'center'
  38. | 'rotate'
  39. | 'front-color'
  40. | 'background-color'
  41. | 'refresh'
  42. | 'remark'
  43. | 'problem'
  44. | 'example'
  45. | 'standard'
  46. | 'delete'
  47. | 'bookmark'
  48. interface HeaderButton {
  49. title: string
  50. type: ButtonType
  51. }
  52. type EventType = Exclude<ButtonType, 'scale-up' | 'scale-down'> | 'scale-change'
  53. const emits = defineEmits<{
  54. (e: 'click', opt: { type: EventType; value?: number | string | boolean | number[] }): void
  55. (e: EventType, val: any): void
  56. (e: 'back'): void
  57. (e: 'center', val: boolean): void
  58. (e: 'refresh'): void
  59. (e: 'remark'): void
  60. (e: 'problem'): void
  61. (e: 'example'): void
  62. (e: 'standard'): void
  63. (e: 'delete'): void
  64. (e: 'bookmark'): void
  65. (e: 'scale-change', val: number): void
  66. (e: 'rotate', val: number): void
  67. (e: 'front-color', color: number[]): void
  68. (e: 'background-color', color: number[]): void
  69. }>()
  70. const buttons: HeaderButton[] = [
  71. { title: '返回', type: 'back' },
  72. { title: '放大', type: 'scale-up' },
  73. { title: '缩小', type: 'scale-down' },
  74. { title: '居中', type: 'center' },
  75. { title: '旋转', type: 'rotate' },
  76. { title: '前景色', type: 'front-color' },
  77. { title: '背景色', type: 'background-color' },
  78. { title: '刷新', type: 'refresh' },
  79. { title: '回评', type: 'remark' },
  80. { title: '问题卷', type: 'problem' },
  81. { title: '查看样卷', type: 'example' },
  82. { title: '评分标准', type: 'standard' },
  83. { title: '删除当前专家卷', type: 'delete' },
  84. { title: '设置专家卷', type: 'bookmark' },
  85. ]
  86. const props = defineProps<{
  87. excludeOperations?: ButtonType[]
  88. includeOperations?: ButtonType[]
  89. replyUserId?: number | null
  90. paperPath?: string | null
  91. }>()
  92. const refs = reactive<Partial<Record<ButtonType, Element>>>({
  93. 'front-color': void 0,
  94. 'background-color': void 0,
  95. })
  96. const bindRef = (type: ButtonType, el: any) => {
  97. if (el) {
  98. refs[type] = el
  99. }
  100. }
  101. const usedOperations = computed(() => {
  102. const viewButtons = props?.includeOperations
  103. ? buttons.filter((b) => props?.includeOperations?.some((t) => b.type === t))
  104. : buttons
  105. return viewButtons.filter((button) => !props.excludeOperations?.some((type) => button.type === type))
  106. })
  107. const attrs = useAttrs()
  108. /** center */
  109. const center = ref<boolean>(false)
  110. /** ratio */
  111. const ratio = ref<number>(1)
  112. /** rotate */
  113. const rotate = ref<number>(0)
  114. /** front-color */
  115. const frontColor = ref<string>('')
  116. /** background-color */
  117. const backgroundColor = ref<string>('')
  118. watch(center, () => emitEvent('center', center.value))
  119. watch(ratio, () => emitEvent('scale-change', ratio.value))
  120. watch(rotate, () => emitEvent('rotate', rotate.value))
  121. watch(frontColor, () => emitEvent('front-color', frontColor.value))
  122. watch(backgroundColor, () => emitEvent('background-color', backgroundColor.value))
  123. const validVal = (val: number, max: number, min: number) => {
  124. return Math.min(max, Math.max(val, min))
  125. }
  126. const onOperationClick = (button: HeaderButton) => {
  127. if (['front-color', 'background-color'].includes(button.type)) {
  128. return
  129. }
  130. switch (button.type) {
  131. case 'scale-up':
  132. ratio.value = validVal(ratio.value + 0.1, 2, 0.5)
  133. break
  134. case 'scale-down':
  135. ratio.value = validVal(ratio.value - 0.1, 2, 0.5)
  136. break
  137. case 'rotate':
  138. rotate.value = (rotate.value + 90) % 360
  139. break
  140. default:
  141. emitEvent(button.type)
  142. break
  143. }
  144. }
  145. const emitEvent = (type: EventType, val?: string | number | boolean | number[]) => {
  146. if (attrs['on' + type.replace(/^\S/, (s) => s.toUpperCase())]) {
  147. emits(type, val as any)
  148. }
  149. emits('click', { type: type, value: val })
  150. }
  151. type MarkConfig = {
  152. center: boolean
  153. ratio: number
  154. rotate: number
  155. frontColor: string
  156. backgroundColor: string
  157. }
  158. const setCurrentConfig = (config: MarkConfig) => {
  159. center.value = config.center ?? center.value
  160. ratio.value = config.ratio ?? ratio.value
  161. rotate.value = config.rotate ?? rotate.value
  162. frontColor.value = config.frontColor ?? frontColor.value
  163. backgroundColor.value = config.backgroundColor ?? backgroundColor.value
  164. }
  165. const loadUserConfig = async () => {
  166. try {
  167. const configStr = await useFetch('getUserMarkConfig').fetch()
  168. const config = JSON.parse(configStr)
  169. } catch (error) {
  170. console.error(error)
  171. }
  172. }
  173. </script>
  174. <style scoped lang="scss">
  175. .common-mark-header {
  176. height: $MainLayoutHeaderHeight;
  177. background-color: $MainLayoutHeaderBg;
  178. .operation-button {
  179. width: 32px;
  180. height: 32px;
  181. place-items: center;
  182. color: $RegularFontColor;
  183. &:hover {
  184. background: $color--primary;
  185. color: $color--white;
  186. }
  187. }
  188. .mark-header {
  189. margin-left: auto;
  190. color: $RegularFontColor;
  191. font-size: $SmallFont;
  192. ::v-deep(.data-item) {
  193. padding-left: 20px;
  194. display: flex;
  195. align-items: center;
  196. &:first-child {
  197. margin-left: auto;
  198. }
  199. &:not(:last-child):after {
  200. content: '';
  201. margin-left: 20px;
  202. display: inline-block;
  203. width: 1px;
  204. height: 16px;
  205. vertical-align: middle;
  206. background-color: #eeeeee;
  207. }
  208. }
  209. }
  210. }
  211. </style>