| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- const n = `<!--
- SEditor 图片尺寸调整弹窗
- 被 SEditor 通过 jodit 自定义按钮的 exec 调起(open 事件),不是业务直接使用的组件。
- 【为什么要有】
- 源项目(vue2 + wangeditor)用 imageAdjust.js(Slate API 自定义菜单)实现「选中图片 → 弹窗调宽高/比例」。
- 换成基座 SEditor(jodit)后 jodit 没有对等 API,此处按源项目同样的交互补齐:
- 比例下拉(原图比例/自由比例/1:1/4:3/3:2/16:9/身份证/护照)+ 高宽输入 + 确定。
- 【数据契约】
- open(payload) 接收 { editor, image }
- 确定后直接改 image.style.width/height,再触发 jodit change 事件让 v-model 同步。
- -->
- <template>
- <s-modal
- v-model:value="visible"
- :title="$l('调整宽高')"
- :width="380"
- :show-full-screen="false"
- :mask-closable="false"
- @handCancel="handleCancel"
- >
- <s-form :model="form" :column="1" labelWidth="70px">
- <s-form-item :label="$l('比例')" prop="scale">
- <a-select v-model:value="form.scale" :options="scaleOptions" />
- </s-form-item>
- <s-form-item :label="$l('高度')" prop="height">
- <a-input-number v-model:value="form.height" :min="1" :max="4000" style="width: 100%" />
- </s-form-item>
- <s-form-item :label="$l('宽度')" prop="width">
- <a-input-number v-model:value="form.width" :min="1" :max="4000" style="width: 100%" />
- </s-form-item>
- </s-form>
- <template #footer>
- <s-button @click="handleCancel">{{ $l('取消') }}</s-button>
- <s-button type="primary" @click="handleOk">{{ $l('确定') }}</s-button>
- </template>
- </s-modal>
- </template>
- <script setup>
- import { ref, reactive, computed, watch } from 'vue'
- const emit = defineEmits(['confirm'])
- const visible = ref(false)
- const editorRef = ref(null)
- const imageRef = ref(null)
- // 原图天然像素宽高,作为「原图比例」的基准
- const natural = reactive({ width: 0, height: 0 })
- const form = reactive({ scale: '1', width: 0, height: 0 })
- // 与源项目 imageAdjust.js 的比例下拉完全一致
- const scaleOptions = [
- { label: window.$l('原图比例'), value: '1' },
- { label: window.$l('自由比例'), value: '2' },
- { label: '1:1', value: '3' },
- { label: '4:3', value: '4' },
- { label: '3:2', value: '5' },
- { label: '16:9', value: '6' },
- { label: window.$l('身份证'), value: '7' },
- { label: window.$l('护照'), value: '8' }
- ]
- /** 固定比例:宽高比(身份证 340:420、护照 288:420 与源项目一致) */
- const FIXED_RATIO = {
- 3: 1 / 1,
- 4: 4 / 3,
- 5: 3 / 2,
- 6: 16 / 9,
- 7: 340 / 420,
- 8: 288 / 420
- }
- /** 原图比例 = 天然宽 / 天然高 */
- const originRatio = computed(() => {
- if (!natural.width || !natural.height) return 1
- return natural.width / natural.height
- })
- /** 由当前比例算宽高比;'1' 原图比例,'2' 自由比例返回 null(互不联动) */
- const currentRatio = computed(() => {
- if (form.scale === '1') return originRatio.value
- if (form.scale === '2') return null
- return FIXED_RATIO[form.scale] ?? null
- })
- // 比例变化时,按当前宽或高反推另一边(源项目是失焦时联动,这里改成实时联动,交互更好)
- watch(
- () => form.scale,
- () => {
- const ratio = currentRatio.value
- if (!ratio) return
- if (form.width) {
- form.height = Math.ceil(form.width / ratio)
- } else if (form.height) {
- form.width = Math.ceil(form.height * ratio)
- }
- }
- )
- /** 宽变化时联动高 */
- watch(
- () => form.width,
- (val) => {
- const ratio = currentRatio.value
- if (!ratio || !val) return
- form.height = Math.ceil(val / ratio)
- }
- )
- /** 高变化时联动宽 */
- watch(
- () => form.height,
- (val) => {
- const ratio = currentRatio.value
- if (!ratio || !val) return
- form.width = Math.ceil(val * ratio)
- }
- )
- /** 供 SEditor 的 jodit 按钮 exec 调用 */
- function open (payload) {
- const { editor, image } = payload || {}
- if (!editor || !image) return
- editorRef.value = editor
- imageRef.value = image
- natural.width = image.naturalWidth || image.offsetWidth || 0
- natural.height = image.naturalHeight || image.offsetHeight || 0
- form.scale = '1'
- form.width = image.offsetWidth || natural.width
- form.height = image.offsetHeight || natural.height
- visible.value = true
- }
- function handleCancel () {
- visible.value = false
- }
- function handleOk () {
- const width = Number(form.width)
- const height = Number(form.height)
- if (!width || !height) {
- window.$message.warning(window.$l('高度或宽度不能为0'))
- return
- }
- const image = imageRef.value
- const editor = editorRef.value
- if (!image || !editor) return
- image.style.width = \`\${width}px\`
- image.style.height = \`\${height}px\`
- // jodit 的 value setter 在值未变时不会 fire change,这里手动触发一次让 v-model 同步
- editor.events.fire('change', editor.value)
- visible.value = false
- emit('confirm', { width, height })
- }
- defineExpose({ open })
- <\/script>
- `;
- export {
- n as default
- };
|