ExtractPageSettingDialog.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="extract-page-setting-popup" v-if="show">
  3. <Dialog :show="show" :dialogName="dialogName" :close="false">
  4. <template #header>
  5. <p>{{ $t('documentEditor.extract') }}</p>
  6. </template>
  7. <p class="title">{{ $t('documentEditor.dialog.pageRange') }}</p>
  8. <div class="container">
  9. <div class="select-content">
  10. <div @click="pageRange = 'all'" class="option"><RadioBtnSel v-if="pageRange === 'all'" /><RadioBtnDis v-else />{{ $t('documentEditor.dialog.allPages') }}</div>
  11. <div @click="pageRange = 'odd'" class="option"><RadioBtnSel v-if="pageRange === 'odd'" /><RadioBtnDis v-else />{{ $t('documentEditor.dialog.oddPage') }}</div>
  12. <div @click="pageRange = 'even'" class="option"><RadioBtnSel v-if="pageRange === 'even'" /><RadioBtnDis v-else />{{ $t('documentEditor.dialog.evenPage') }}</div>
  13. <div @click="pageRange = 'custom'" class="option custom-page">
  14. <RadioBtnSel v-if="pageRange === 'custom'" /><RadioBtnDis v-else />{{ $t('documentEditor.dialog.page') }}
  15. <div class="addition">
  16. <input type="text" v-model="customRange" :placeholder="$t('documentEditor.dialog.pageTip')" @blur="validateCustomRange">
  17. <span>/ {{ totalPages }}</span>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="check-content">
  23. <div class="check-box" @click="separateFile = !separateFile">
  24. <div class="check" :class="{'active': separateFile}"><Checkbox v-show="separateFile" /></div>
  25. <span>{{ $t('documentEditor.eachPage') }}</span>
  26. </div>
  27. <div class="check-box" :class="{ 'disabled': pageRange === 'all' }" @click="() => { if (pageRange !== 'all') deleteAfter = !deleteAfter }">
  28. <div class="check" :class="{'active': deleteAfter}"><Checkbox v-show="deleteAfter && pageRange !== 'all'" /></div>
  29. <span>{{ $t('documentEditor.deleteAfter') }}</span>
  30. </div>
  31. </div>
  32. <template #footer>
  33. <div class="rect-button white" @click="closeDialog">{{ $t('cancel') }}</div>
  34. <div class="rect-button blue" :class="{ 'disabled': pageRange === 'pdf' && (!inputFile || (pageRange === 'custom' && !validCustomRange)) }" @click="confirm">{{ $t('documentEditor.extract') }}</div>
  35. </template>
  36. </Dialog>
  37. </div>
  38. </template>
  39. <script setup>
  40. import { ref, computed, watch } from 'vue'
  41. import { useViewerStore } from '@/stores/modules/viewer'
  42. const props = defineProps(['selectedPageList', 'totalPages'])
  43. const emits = defineEmits(['extractPage'])
  44. const useViewer = useViewerStore()
  45. const dialogName = 'extractPageSettingDialog'
  46. const show = computed(() => useViewer.isElementOpen(dialogName))
  47. const pageRange = ref('custom')
  48. const customRange = ref('')
  49. const validCustomRange = ref('')
  50. const separateFile = ref(false)
  51. const deleteAfter = ref(false)
  52. let initialRange = ''
  53. watch(() => show.value, (newVal, oldVal) => {
  54. if (newVal) {
  55. const selectedPageStr = props.selectedPageList.map(function(num) {
  56. return num + 1
  57. }).join(',')
  58. initialRange = formatText(selectedPageStr)
  59. customRange.value = validCustomRange.value = initialRange
  60. }
  61. })
  62. // 关闭弹窗
  63. const closeDialog = () => {
  64. useViewer.closeElement(dialogName)
  65. pageRange.value = 'custom'
  66. }
  67. // 确认
  68. const confirm = async () => {
  69. const data = {
  70. range: pageRange.value === 'custom' ? customRange.value : pageRange.value,
  71. separateFile: separateFile.value,
  72. deleteAfter: pageRange.value === 'all' ? false : deleteAfter.value
  73. }
  74. emits('extractPage', data)
  75. closeDialog()
  76. }
  77. // 输入自定义页面范围校验 格式化
  78. const validateCustomRange = () => {
  79. validCustomRange.value = formatText(customRange.value)
  80. customRange.value = validCustomRange.value
  81. }
  82. const formatText = (inputText) => {
  83. const max = props.totalPages
  84. const text = inputText.replace(/\s/g, '')
  85. const matches = text.match(/\d+-\d+|\d+|\d+/g)
  86. if (matches) {
  87. const sortedNums = matches
  88. .flatMap(match => {
  89. if (match.includes('-')) {
  90. const [start, end] = match.split('-')
  91. const smallest = Math.min(start, end)
  92. const largest = Math.min(Math.max(start, end), max)
  93. return Array.from({ length: largest - smallest + 1 }, (_, i) =>
  94. String(Number(smallest) + i)
  95. )
  96. } else {
  97. return Math.min(match, max)
  98. }
  99. })
  100. .sort((a, b) => a - b)
  101. const formattedText = []
  102. let start = sortedNums[0]
  103. let prev = sortedNums[0]
  104. for (let i = 1; i < sortedNums.length; i++) {
  105. const current = sortedNums[i]
  106. const prevNum = Number(prev)
  107. const currentNum = Number(current)
  108. if (currentNum - prevNum > 1) {
  109. formattedText.push(formatRange(start, prev))
  110. start = current
  111. }
  112. prev = current
  113. }
  114. formattedText.push(formatRange(start, prev))
  115. return formattedText.join(', ')
  116. }
  117. return initialRange
  118. }
  119. const formatRange = (start, end) => {
  120. return (start === end) ? start.toString() : `${start}-${end}`
  121. }
  122. </script>
  123. <style lang="scss">
  124. .extract-page-setting-popup {
  125. .dialog-container {
  126. width: 460px;
  127. box-shadow: 0px 4px 32px 0px var(--c-doc-editor-popup-shadow);
  128. main {
  129. margin-top: 16px;
  130. }
  131. }
  132. .title {
  133. font-size: 14px;
  134. font-weight: 700;
  135. line-height: 16px;
  136. }
  137. .container {
  138. margin-top: 8px;
  139. margin-bottom: 16px;
  140. padding: 8px 16px 16px;
  141. border-radius: 4px;
  142. background: var(--c-toolbar-bg);
  143. .select-content .option:not(:first-child) {
  144. margin-top: 16px;
  145. }
  146. .addition {
  147. position: relative;
  148. display: flex;
  149. align-items: center;
  150. span {
  151. white-space: nowrap;
  152. font-size: 14px;
  153. line-height: 16px;
  154. }
  155. input {
  156. padding: 0 20px 0 8px;
  157. width: 100%;
  158. height: 24px;
  159. background: var(--c-right-side-content-fillbox-bg);
  160. border: 1px solid var(--c-right-side-content-fillbox-border);
  161. border-radius: 1px;
  162. font-size: 14px;
  163. line-height: 16px;
  164. color: var(--c-text);
  165. }
  166. input[type="text"]:focus {
  167. border-color: #0078D7;
  168. }
  169. }
  170. .option.custom-page {
  171. padding: 0;
  172. .addition {
  173. flex: 1;
  174. margin-left: 20px;
  175. span {
  176. margin-left: 8px;
  177. color: #999;
  178. }
  179. }
  180. }
  181. }
  182. .check-content {
  183. padding: 0 16px;
  184. display: flex;
  185. flex-direction: column;
  186. align-items: flex-start;
  187. .check-box {
  188. padding: 5px 0;
  189. display: inline-flex;
  190. align-items: center;
  191. cursor: pointer;
  192. & + .check-box {
  193. margin-top: 8px;
  194. }
  195. .check {
  196. margin-right: 4px;
  197. width: 14px;
  198. height: 14px;
  199. background: var(--c-right-side-content-fillbox-bg);
  200. border: 1px solid var(--c-right-side-content-fillbox-border);
  201. border-radius: 1px;
  202. overflow: hidden;
  203. transition: none;
  204. &.active {
  205. border: none;
  206. }
  207. svg {
  208. vertical-align: top;
  209. }
  210. }
  211. &.disabled {
  212. cursor: not-allowed;
  213. color: #b4b4b4;
  214. .check {
  215. background-color: #b4b4b4;
  216. opacity: 0.5;
  217. }
  218. }
  219. span {
  220. font-size: 14px;
  221. line-height: 16px;
  222. }
  223. }
  224. }
  225. }
  226. </style>