MentionModal.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <script setup lang="ts">
  2. const emit = defineEmits(['hideMentionModal', 'insertMention'])
  3. const inputRef = ref()
  4. const top = ref('')
  5. const left = ref('')
  6. const searchVal = ref('')
  7. const list = ref([
  8. {id: 'startUser', name: '发起人'},
  9. {id: 'startUserDept', name: '发起人部门'},
  10. {id: 'processName', name: '流程名称'},
  11. {id: 'processNum', name: '流程编号'},
  12. {id: 'startTime', name: '发起时间'},
  13. {id: 'endTime', name: '发起时间'},
  14. {id: 'processStatus', name: '流程状态'},
  15. {id: 'processResult', name: '流程结果'},
  16. {id: 'printUser', name: '打印人'},
  17. {id: 'printTime', name: '打印时间'},
  18. ])
  19. const searchedList = computed(() => {
  20. const searchValStr = searchVal.value.trim().toLowerCase()
  21. return list.value.filter(item => {
  22. const name = item.name.toLowerCase()
  23. return name.indexOf(searchValStr) >= 0;
  24. })
  25. })
  26. const inputKeyupHandler = (event) => {
  27. if (event.key === 'Escape') {
  28. emit('hideMentionModal')
  29. }
  30. if (event.key === 'Enter') {
  31. const firstOne = searchedList.value[0]
  32. if (firstOne) {
  33. const {id, name} = firstOne
  34. insertMentionHandler(id, name)
  35. }
  36. }
  37. }
  38. const insertMentionHandler = (id, name) => {
  39. emit('insertMention', id, name)
  40. emit('hideMentionModal')
  41. }
  42. const formFields = inject('formFieldsObj')
  43. onMounted(()=> {
  44. if (formFields.value && formFields.value.length > 0) {
  45. const cloneFormField = formFields.value.map((item) => {
  46. return {
  47. name: '[表单]'+item.title,
  48. id: item.field
  49. }
  50. })
  51. list.value.push(...cloneFormField)
  52. }
  53. const domSelection = document.getSelection()
  54. const domRange = domSelection?.getRangeAt(0)
  55. if (domRange == null) return
  56. const rect = domRange.getBoundingClientRect()
  57. top.value = `${rect.top + 20}px`
  58. left.value = `${rect.left + 5}px`
  59. inputRef.value.focus()
  60. })
  61. </script>
  62. <template>
  63. <div id="mention-modal" :style="{ top: top, left: left }">
  64. <input id="mention-input" v-model="searchVal" ref="inputRef" @keyup="inputKeyupHandler" />
  65. <ul id="mention-list">
  66. <li
  67. v-for="item in searchedList"
  68. :key="item.id"
  69. @click="insertMentionHandler(item.id, item.name)"
  70. >{{ item.name }}
  71. </li>
  72. </ul>
  73. </div>
  74. </template>
  75. <style>
  76. #mention-modal {
  77. position: absolute;
  78. border: 1px solid #ccc;
  79. background-color: #fff;
  80. padding: 5px;
  81. }
  82. #mention-modal input {
  83. width: 100px;
  84. outline: none;
  85. }
  86. #mention-modal ul {
  87. padding: 0;
  88. margin: 0;
  89. }
  90. #mention-modal ul li {
  91. list-style: none;
  92. cursor: pointer;
  93. padding: 3px 0;
  94. text-align: left;
  95. }
  96. #mention-modal ul li:hover {
  97. text-decoration: underline;
  98. }
  99. </style>