SEditorImageAdjust2.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const n = `<!--
  2. SEditor 图片尺寸调整弹窗
  3. 被 SEditor 通过 jodit 自定义按钮的 exec 调起(open 事件),不是业务直接使用的组件。
  4. 【为什么要有】
  5. 源项目(vue2 + wangeditor)用 imageAdjust.js(Slate API 自定义菜单)实现「选中图片 → 弹窗调宽高/比例」。
  6. 换成基座 SEditor(jodit)后 jodit 没有对等 API,此处按源项目同样的交互补齐:
  7. 比例下拉(原图比例/自由比例/1:1/4:3/3:2/16:9/身份证/护照)+ 高宽输入 + 确定。
  8. 【数据契约】
  9. open(payload) 接收 { editor, image }
  10. 确定后直接改 image.style.width/height,再触发 jodit change 事件让 v-model 同步。
  11. -->
  12. <template>
  13. <s-modal
  14. v-model:value="visible"
  15. :title="$l('调整宽高')"
  16. :width="380"
  17. :show-full-screen="false"
  18. :mask-closable="false"
  19. @handCancel="handleCancel"
  20. >
  21. <s-form :model="form" :column="1" labelWidth="70px">
  22. <s-form-item :label="$l('比例')" prop="scale">
  23. <a-select v-model:value="form.scale" :options="scaleOptions" />
  24. </s-form-item>
  25. <s-form-item :label="$l('高度')" prop="height">
  26. <a-input-number v-model:value="form.height" :min="1" :max="4000" style="width: 100%" />
  27. </s-form-item>
  28. <s-form-item :label="$l('宽度')" prop="width">
  29. <a-input-number v-model:value="form.width" :min="1" :max="4000" style="width: 100%" />
  30. </s-form-item>
  31. </s-form>
  32. <template #footer>
  33. <s-button @click="handleCancel">{{ $l('取消') }}</s-button>
  34. <s-button type="primary" @click="handleOk">{{ $l('确定') }}</s-button>
  35. </template>
  36. </s-modal>
  37. </template>
  38. <script setup>
  39. import { ref, reactive, computed, watch } from 'vue'
  40. const emit = defineEmits(['confirm'])
  41. const visible = ref(false)
  42. const editorRef = ref(null)
  43. const imageRef = ref(null)
  44. // 原图天然像素宽高,作为「原图比例」的基准
  45. const natural = reactive({ width: 0, height: 0 })
  46. const form = reactive({ scale: '1', width: 0, height: 0 })
  47. // 与源项目 imageAdjust.js 的比例下拉完全一致
  48. const scaleOptions = [
  49. { label: window.$l('原图比例'), value: '1' },
  50. { label: window.$l('自由比例'), value: '2' },
  51. { label: '1:1', value: '3' },
  52. { label: '4:3', value: '4' },
  53. { label: '3:2', value: '5' },
  54. { label: '16:9', value: '6' },
  55. { label: window.$l('身份证'), value: '7' },
  56. { label: window.$l('护照'), value: '8' }
  57. ]
  58. /** 固定比例:宽高比(身份证 340:420、护照 288:420 与源项目一致) */
  59. const FIXED_RATIO = {
  60. 3: 1 / 1,
  61. 4: 4 / 3,
  62. 5: 3 / 2,
  63. 6: 16 / 9,
  64. 7: 340 / 420,
  65. 8: 288 / 420
  66. }
  67. /** 原图比例 = 天然宽 / 天然高 */
  68. const originRatio = computed(() => {
  69. if (!natural.width || !natural.height) return 1
  70. return natural.width / natural.height
  71. })
  72. /** 由当前比例算宽高比;'1' 原图比例,'2' 自由比例返回 null(互不联动) */
  73. const currentRatio = computed(() => {
  74. if (form.scale === '1') return originRatio.value
  75. if (form.scale === '2') return null
  76. return FIXED_RATIO[form.scale] ?? null
  77. })
  78. // 比例变化时,按当前宽或高反推另一边(源项目是失焦时联动,这里改成实时联动,交互更好)
  79. watch(
  80. () => form.scale,
  81. () => {
  82. const ratio = currentRatio.value
  83. if (!ratio) return
  84. if (form.width) {
  85. form.height = Math.ceil(form.width / ratio)
  86. } else if (form.height) {
  87. form.width = Math.ceil(form.height * ratio)
  88. }
  89. }
  90. )
  91. /** 宽变化时联动高 */
  92. watch(
  93. () => form.width,
  94. (val) => {
  95. const ratio = currentRatio.value
  96. if (!ratio || !val) return
  97. form.height = Math.ceil(val / ratio)
  98. }
  99. )
  100. /** 高变化时联动宽 */
  101. watch(
  102. () => form.height,
  103. (val) => {
  104. const ratio = currentRatio.value
  105. if (!ratio || !val) return
  106. form.width = Math.ceil(val * ratio)
  107. }
  108. )
  109. /** 供 SEditor 的 jodit 按钮 exec 调用 */
  110. function open (payload) {
  111. const { editor, image } = payload || {}
  112. if (!editor || !image) return
  113. editorRef.value = editor
  114. imageRef.value = image
  115. natural.width = image.naturalWidth || image.offsetWidth || 0
  116. natural.height = image.naturalHeight || image.offsetHeight || 0
  117. form.scale = '1'
  118. form.width = image.offsetWidth || natural.width
  119. form.height = image.offsetHeight || natural.height
  120. visible.value = true
  121. }
  122. function handleCancel () {
  123. visible.value = false
  124. }
  125. function handleOk () {
  126. const width = Number(form.width)
  127. const height = Number(form.height)
  128. if (!width || !height) {
  129. window.$message.warning(window.$l('高度或宽度不能为0'))
  130. return
  131. }
  132. const image = imageRef.value
  133. const editor = editorRef.value
  134. if (!image || !editor) return
  135. image.style.width = \`\${width}px\`
  136. image.style.height = \`\${height}px\`
  137. // jodit 的 value setter 在值未变时不会 fire change,这里手动触发一次让 v-model 同步
  138. editor.events.fire('change', editor.value)
  139. visible.value = false
  140. emit('confirm', { width, height })
  141. }
  142. defineExpose({ open })
  143. <\/script>
  144. `;
  145. export {
  146. n as default
  147. };