Skip to content

Commit 58e120d

Browse files
committed
feat: resolve the issue of the close button continuously displaying after enlarging the image
1 parent 270a8cd commit 58e120d

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

packages/fluent-editor/src/modules/custom-image/preview/preview-modal.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class ImagePreviewModal {
88
private overlay: HTMLElement | null = null
99
private previewImage: HTMLImageElement | null = null
1010
private scaleTooltip: HTMLElement | null = null
11+
private closeBtn: HTMLButtonElement | null = null
1112
private currentScale: number = 1
1213
private minScale: number = 0.5
1314
private maxScale: number = 3
@@ -64,10 +65,10 @@ export class ImagePreviewModal {
6465
`
6566

6667
// 创建关闭按钮
67-
const closeBtn = document.createElement('button')
68-
closeBtn.className = 'tiny-editor-image-preview-close'
69-
closeBtn.innerHTML = '×'
70-
closeBtn.style.cssText = `
68+
this.closeBtn = document.createElement('button')
69+
this.closeBtn.className = 'tiny-editor-image-preview-close'
70+
this.closeBtn.innerHTML = '×'
71+
this.closeBtn.style.cssText = `
7172
position: fixed;
7273
top: 20px;
7374
right: 20px;
@@ -82,10 +83,10 @@ export class ImagePreviewModal {
8283
line-height: 1;
8384
padding: 0;
8485
`
85-
closeBtn.addEventListener('click', () => this.hide())
86+
this.closeBtn.addEventListener('click', () => this.hide())
8687

8788
this.modal.appendChild(this.previewImage)
88-
document.body.appendChild(closeBtn)
89+
document.body.appendChild(this.closeBtn)
8990

9091
// 创建缩放提示窗口
9192
this.scaleTooltip = document.createElement('div')
@@ -117,7 +118,7 @@ export class ImagePreviewModal {
117118
})
118119

119120
// 绑定滚轮缩放事件
120-
document.addEventListener('wheel', (e) => this.onMouseWheel(e), { passive: false })
121+
document.addEventListener('wheel', e => this.onMouseWheel(e), { passive: false })
121122

122123
// 阻止模态框内的点击事件冒泡到遮罩层
123124
this.modal.addEventListener('click', (e) => {
@@ -226,6 +227,10 @@ export class ImagePreviewModal {
226227
this.modal.style.justifyContent = 'center'
227228
this.overlay.style.display = 'block'
228229

230+
if (this.closeBtn) {
231+
this.closeBtn.style.display = 'block'
232+
}
233+
229234
// 防止页面滚动
230235
document.body.style.overflow = 'hidden'
231236
}
@@ -239,6 +244,9 @@ export class ImagePreviewModal {
239244
this.overlay.style.display = 'none'
240245
document.body.style.overflow = ''
241246
this.resetScale()
247+
if (this.closeBtn) {
248+
this.closeBtn.style.display = 'none'
249+
}
242250
}
243251
}
244252

@@ -253,12 +261,16 @@ export class ImagePreviewModal {
253261
if (this.modal && this.modal.parentNode) {
254262
this.modal.parentNode.removeChild(this.modal)
255263
}
264+
if (this.closeBtn && this.closeBtn.parentNode) {
265+
this.closeBtn.parentNode.removeChild(this.closeBtn)
266+
}
256267
if (this.scaleTooltip && this.scaleTooltip.parentNode) {
257268
this.scaleTooltip.parentNode.removeChild(this.scaleTooltip)
258269
}
259270
this.modal = null
260271
this.overlay = null
261272
this.previewImage = null
273+
this.closeBtn = null
262274
this.scaleTooltip = null
263275
}
264276
}

0 commit comments

Comments
 (0)