Skip to content

Commit 521ff53

Browse files
authored
fix: handle image width and height (#1127)
1 parent 1c0c9eb commit 521ff53

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

apps/web/src/utils/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,20 @@ export function solveWeChatImage() {
247247
const images = clipboardDiv.getElementsByTagName(`img`)
248248

249249
Array.from(images).forEach((image) => {
250-
const width = image.getAttribute(`width`)!
251-
const height = image.getAttribute(`height`)!
252-
image.removeAttribute(`width`)
253-
image.removeAttribute(`height`)
254-
image.style.width = width
255-
image.style.height = height
250+
const width = image.getAttribute(`width`)
251+
const height = image.getAttribute(`height`)
252+
253+
if (width) {
254+
image.removeAttribute(`width`)
255+
// 如果是纯数字,添加 px 单位;否则保持原值
256+
image.style.width = /^\d+$/.test(width) ? `${width}px` : width
257+
}
258+
259+
if (height) {
260+
image.removeAttribute(`height`)
261+
// 如果是纯数字,添加 px 单位;否则保持原值
262+
image.style.height = /^\d+$/.test(height) ? `${height}px` : height
263+
}
256264
})
257265
}
258266

0 commit comments

Comments
 (0)