File size: 9,977 Bytes
89ce340 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
<template>
<div class="image-style-panel">
<div
class="origin-image"
:style="{ backgroundImage: `url(${handleImageElement.src})` }"
></div>
<ElementFlip />
<ButtonGroup class="row" passive>
<Button first style="width: calc(100% / 6 * 5);" @click="clipImage()"><IconTailoring class="btn-icon" /> 裁剪图片</Button>
<Popover trigger="click" v-model:value="clipPanelVisible" style="width: calc(100% / 6);">
<template #content>
<div class="clip">
<div class="title">按形状:</div>
<div class="shape-clip">
<div
class="shape-clip-item"
v-for="(item, key) in shapeClipPathOptions"
:key="key"
@click="presetImageClip(key as string)"
>
<div class="shape" :style="{ clipPath: item.style }"></div>
</div>
</div>
<template v-for="typeItem in ratioClipOptions" :key="typeItem.label">
<div class="title" v-if="typeItem.label">按{{typeItem.label}}:</div>
<ButtonGroup class="row">
<Button
style="flex: 1;"
v-for="item in typeItem.children"
:key="item.key"
@click="presetImageClip('rect', item.ratio)"
>{{item.key}}</Button>
</ButtonGroup>
</template>
</div>
</template>
<Button last class="popover-btn" style="width: 100%;"><IconDown /></Button>
</Popover>
</ButtonGroup>
<div class="row">
<div style="width: 40%;">圆角半径:</div>
<NumberInput
:value="handleImageElement.radius || 0"
@update:value="value => updateImage({ radius: value })"
style="width: 60%;"
/>
</div>
<Divider />
<ElementColorMask />
<Divider />
<ElementFilter />
<Divider />
<ElementOutline />
<Divider />
<ElementShadow />
<Divider />
<FileInput @change="files => replaceImage(files)">
<Button class="full-width-btn"><IconTransform class="btn-icon" /> 替换图片</Button>
</FileInput>
<Button class="full-width-btn" @click="resetImage()"><IconUndo class="btn-icon" /> 重置样式</Button>
<Button class="full-width-btn" @click="setBackgroundImage()"><IconTheme class="btn-icon" /> 设为背景</Button>
</div>
</template>
<script lang="ts" setup>
import { type Ref, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore } from '@/store'
import type { PPTImageElement, SlideBackground } from '@/types/slides'
import { CLIPPATHS } from '@/configs/imageClip'
import { getImageDataURL, getImageSize } from '@/utils/image'
import useHistorySnapshot from '@/hooks/useHistorySnapshot'
import ElementOutline from '../common/ElementOutline.vue'
import ElementShadow from '../common/ElementShadow.vue'
import ElementFlip from '../common/ElementFlip.vue'
import ElementFilter from '../common/ElementFilter.vue'
import ElementColorMask from '../common/ElementColorMask.vue'
import FileInput from '@/components/FileInput.vue'
import Divider from '@/components/Divider.vue'
import Button from '@/components/Button.vue'
import ButtonGroup from '@/components/ButtonGroup.vue'
import Popover from '@/components/Popover.vue'
import NumberInput from '@/components/NumberInput.vue'
const shapeClipPathOptions = CLIPPATHS
const ratioClipOptions = [
{
label: '纵横比(正方形)',
children: [
{ key: '1:1', ratio: 1 / 1 },
],
},
{
label: '纵横比(纵向)',
children: [
{ key: '2:3', ratio: 3 / 2 },
{ key: '3:4', ratio: 4 / 3 },
{ key: '3:5', ratio: 5 / 3 },
{ key: '4:5', ratio: 5 / 4 },
],
},
{
label: '纵横比(横向)',
children: [
{ key: '3:2', ratio: 2 / 3 },
{ key: '4:3', ratio: 3 / 4 },
{ key: '5:3', ratio: 3 / 5 },
{ key: '5:4', ratio: 4 / 5 },
],
},
{
children: [
{ key: '16:9', ratio: 9 / 16 },
{ key: '16:10', ratio: 10 / 16 },
],
},
]
const mainStore = useMainStore()
const slidesStore = useSlidesStore()
const { handleElement, handleElementId } = storeToRefs(mainStore)
const { currentSlide } = storeToRefs(slidesStore)
const handleImageElement = handleElement as Ref<PPTImageElement>
const clipPanelVisible = ref(false)
const { addHistorySnapshot } = useHistorySnapshot()
// 打开自由裁剪
const clipImage = () => {
mainStore.setClipingImageElementId(handleElementId.value)
clipPanelVisible.value = false
}
// 获取原始图片的位置大小
const getImageElementDataBeforeClip = () => {
const _handleElement = handleElement.value as PPTImageElement
// 图片当前的位置大小和裁剪范围
const imgWidth = _handleElement.width
const imgHeight = _handleElement.height
const imgLeft = _handleElement.left
const imgTop = _handleElement.top
const originClipRange: [[number, number], [number, number]] = _handleElement.clip ? _handleElement.clip.range : [[0, 0], [100, 100]]
const originWidth = imgWidth / ((originClipRange[1][0] - originClipRange[0][0]) / 100)
const originHeight = imgHeight / ((originClipRange[1][1] - originClipRange[0][1]) / 100)
const originLeft = imgLeft - originWidth * (originClipRange[0][0] / 100)
const originTop = imgTop - originHeight * (originClipRange[0][1] / 100)
return {
originClipRange,
originWidth,
originHeight,
originLeft,
originTop,
}
}
const updateImage = (props: Partial<PPTImageElement>) => {
if (!handleElement.value) return
slidesStore.updateElement({ id: handleElementId.value, props })
addHistorySnapshot()
}
// 预设裁剪
const presetImageClip = (shape: string, ratio = 0) => {
const _handleElement = handleElement.value as PPTImageElement
const {
originClipRange,
originWidth,
originHeight,
originLeft,
originTop,
} = getImageElementDataBeforeClip()
// 纵横比裁剪(形状固定为矩形)
if (ratio) {
const imageRatio = originHeight / originWidth
const min = 0
const max = 100
let range: [[number, number], [number, number]]
if (imageRatio > ratio) {
const distance = ((1 - ratio / imageRatio) / 2) * 100
range = [[min, distance], [max, max - distance]]
}
else {
const distance = ((1 - imageRatio / ratio) / 2) * 100
range = [[distance, min], [max - distance, max]]
}
updateImage({
clip: { ..._handleElement.clip, shape, range },
left: originLeft + originWidth * (range[0][0] / 100),
top: originTop + originHeight * (range[0][1] / 100),
width: originWidth * (range[1][0] - range[0][0]) / 100,
height: originHeight * (range[1][1] - range[0][1]) / 100,
})
}
// 形状裁剪(保持当前裁剪范围)
else {
const clipData = { ..._handleElement.clip, shape, range: originClipRange }
let props: Partial<PPTImageElement> = { clip: clipData }
if (shape === 'rect') props = { clip: clipData, radius: 0 }
updateImage(props)
}
clipImage()
}
// 替换图片(保持当前的样式)
const replaceImage = (files: FileList) => {
const imageFile = files[0]
if (!imageFile) return
getImageDataURL(imageFile).then(dataURL => {
const originWidth = handleImageElement.value.width
const originHeight = handleImageElement.value.height
const originLeft = handleImageElement.value.left
const originTop = handleImageElement.value.top
const centerX = originLeft + originWidth / 2
const centerY = originTop + originHeight / 2
getImageSize(dataURL).then(({ width, height }) => {
const h = originHeight
const w = width * (originHeight / height)
const l = centerX - w / 2
const t = centerY - h / 2
slidesStore.removeElementProps({
id: handleElementId.value,
propName: 'clip',
})
updateImage({
src: dataURL,
width: w,
height: h,
left: l,
top: t,
})
})
})
}
// 重置图片:清除全部样式
const resetImage = () => {
const _handleElement = handleElement.value as PPTImageElement
if (_handleElement.clip) {
const {
originWidth,
originHeight,
originLeft,
originTop,
} = getImageElementDataBeforeClip()
updateImage({
left: originLeft,
top: originTop,
width: originWidth,
height: originHeight,
})
}
slidesStore.removeElementProps({
id: handleElementId.value,
propName: ['clip', 'outline', 'flip', 'shadow', 'filters', 'colorMask', 'radius'],
})
addHistorySnapshot()
}
// 将图片设置为背景
const setBackgroundImage = () => {
const _handleElement = handleElement.value as PPTImageElement
const background: SlideBackground = {
...currentSlide.value.background,
type: 'image',
image: {
src: _handleElement.src,
size: 'cover'
},
}
slidesStore.updateSlide({ background })
addHistorySnapshot()
}
</script>
<style lang="scss" scoped>
.row {
width: 100%;
display: flex;
align-items: center;
margin-bottom: 10px;
}
.switch-wrapper {
text-align: right;
}
.origin-image {
height: 100px;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
background-color: $lightGray;
margin-bottom: 10px;
}
.full-width-btn {
width: 100%;
margin-bottom: 10px;
}
.btn-icon {
margin-right: 3px;
}
.clip {
width: 260px;
font-size: 12px;
.title {
margin-bottom: 5px;
}
}
.shape-clip {
margin-bottom: 10px;
@include flex-grid-layout();
}
.shape-clip-item {
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
@include flex-grid-layout-children(5, 16%);
&:hover .shape {
background-color: #ccc;
}
.shape {
width: 40px;
height: 40px;
background-color: #e1e1e1;
}
}
.popover-btn {
padding: 0 3px;
}
</style> |