kyle-editor 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,331 +0,0 @@
1
- /**
2
- * 图片 / 视频上传与插入
3
- * - 拖拽、粘贴、点击三种入口共用 upload(file)
4
- * - 上传中在文档内插入占位块,显示分片进度,可取消、失败可重试
5
- * - 图片支持上传前压缩(options.image.compress)
6
- */
7
- import { formatSize, h, uid } from '../core/utils.js'
8
- import { insertNode, ensureEditableTail } from '../core/selection.js'
9
- import { lightbox } from '../ui/toast.js'
10
-
11
- const IMAGE_EXT = /\.(jpe?g|png|gif|webp|bmp|svg|avif)$/i
12
- const VIDEO_EXT = /\.(mp4|mov|webm|m4v|ogv|avi|mkv)$/i
13
-
14
- export class MediaManager {
15
- constructor(editor) {
16
- this.editor = editor
17
- this.options = editor.options
18
- this.tasks = new Map()
19
- this.input = null
20
- }
21
-
22
- kindOf(file) {
23
- const type = String(file.type || '')
24
- const name = String(file.name || '')
25
- if (type.startsWith('image/') || IMAGE_EXT.test(name)) return 'image'
26
- if (type.startsWith('video/') || VIDEO_EXT.test(name)) return 'video'
27
- return null
28
- }
29
-
30
- check(file, kind) {
31
- if (!kind) return `不支持的文件类型:${file.name || '剪贴板内容'}`
32
- const limit = kind === 'image' ? this.options.maxImageSize : this.options.maxVideoSize
33
- if (limit && file.size > limit) return `${file.name} 超出大小限制(${formatSize(limit)})`
34
- return null
35
- }
36
-
37
- pickFiles(kind) {
38
- if (!this.input) {
39
- this.input = h('input', { type: 'file', multiple: true, style: { display: 'none' } })
40
- this.input.addEventListener('change', () => {
41
- const files = [...(this.input.files || [])]
42
- this.input.value = ''
43
- this.addFiles(files)
44
- })
45
- document.body.appendChild(this.input)
46
- }
47
- this.input.accept = kind === 'image' ? this.options.imageAccept : this.options.videoAccept
48
- this.input.multiple = kind === 'image'
49
- this.input.click()
50
- }
51
-
52
- async addFiles(files) {
53
- const accepted = []
54
- for (const file of files) {
55
- const kind = this.kindOf(file)
56
- const error = this.check(file, kind)
57
- if (error) this.editor.toast.warn(error)
58
- else accepted.push({ file, kind })
59
- }
60
- for (const item of accepted) await this.upload(item.file, item.kind)
61
- return accepted.length
62
- }
63
-
64
- /** 插入占位并上传,返回结果(失败/取消返回 null) */
65
- async upload(file, kind) {
66
- const holder = kind === 'image' ? this.createImageHolder() : this.createVideoHolder()
67
- this.editor.focus()
68
- insertNode(this.editor.root, holder)
69
- const task = {
70
- id: holder.dataset.kId,
71
- file,
72
- kind,
73
- holder,
74
- ctrl: new AbortController(),
75
- previewUrl: URL.createObjectURL(file),
76
- }
77
- this.tasks.set(task.id, task)
78
- this.decorateHolder(task)
79
- const thumb = holder.querySelector('[data-k-preview]')
80
- if (thumb) thumb.src = task.previewUrl
81
- return this.runUpload(task)
82
- }
83
-
84
- /** 真正的上传流可重试(复用同一个占位块,保证位置不变) */
85
- async runUpload(task) {
86
- const editor = this.editor
87
- const { holder, file, kind } = task
88
- editor.emit('uploadstart', { file, kind })
89
- editor.refreshStatus()
90
-
91
- let target = file
92
- try {
93
- if (kind === 'image') target = await maybeCompress(file, this.options.image)
94
- const result = await editor.uploader.upload(target, {
95
- signal: task.ctrl.signal,
96
- onProgress: (ratio, info) => this.updateProgress(holder, ratio, info, target, task),
97
- })
98
- const url = typeof result === 'string' ? { url: result } : result
99
- if (!url?.url) throw new Error('上传结果缺少 url')
100
- this.finish(task, { ...url, size: target.size })
101
- editor.emit('uploaddone', { file, kind, url: url.url })
102
- return url
103
- } catch (error) {
104
- if (error?.name === 'AbortError') {
105
- this.remove(task)
106
- editor.emit('uploadcancel', { file, kind })
107
- return null
108
- }
109
- this.fail(task, error)
110
- editor.emit('uploaderror', { file, kind, error })
111
- editor.toast.error(error?.message || '上传失败')
112
- return null
113
- } finally {
114
- ensureEditableTail(editor.root)
115
- editor.commitHistory()
116
- editor.refreshStatus()
117
- }
118
- }
119
-
120
- createImageHolder() {
121
- const figure = h('figure', { 'data-k-media': 'image', 'data-k-id': uid('m'), 'data-k-state': 'uploading', contenteditable: 'false' })
122
- const holder = h('div.ke-media-holder', {}, [
123
- h('img', { 'data-k-preview': '', alt: '', style: { display: 'none' } }),
124
- h('div.ke-media-name', { text: '图片上传中…' }),
125
- progressRow(),
126
- ])
127
- holder.appendChild(this.actionRow())
128
- figure.appendChild(holder)
129
- return figure
130
- }
131
-
132
- createVideoHolder() {
133
- const card = h('div.ke-video-card', { 'data-k-media': 'video', 'data-k-id': uid('m'), 'data-k-state': 'uploading', contenteditable: 'false' })
134
- const body = h('div.ke-video-body', {}, [
135
- h('div.ke-video-head', {}, [h('span', { text: '视频上传中…' }), h('span.ke-video-size', { text: '' })]),
136
- progressRow(),
137
- ])
138
- body.appendChild(this.actionRow())
139
- card.appendChild(body)
140
- return card
141
- }
142
-
143
- actionRow() {
144
- const self = this
145
- return h('div', {
146
- 'data-k-actions': '',
147
- style: { display: 'flex', alignItems: 'center', gap: '8px', width: '100%', marginTop: '2px' },
148
- onclick: (event) => {
149
- const holder = event.target.closest('[data-k-id]')
150
- const task = holder && self.tasks.get(holder.dataset.kId)
151
- if (!task) return
152
- if (event.target.matches('.ke-media-cancel')) {
153
- task.ctrl.abort()
154
- self.remove(task)
155
- } else if (event.target.matches('.ke-media-retry')) {
156
- task.ctrl = new AbortController()
157
- self.resetHolder(task)
158
- self.runUpload(task)
159
- }
160
- },
161
- }, [h('button.ke-media-cancel', { type: 'button', text: '取消' })])
162
- }
163
-
164
- /** 文件名 / 大小 / 预览图 */
165
- decorateHolder(task) {
166
- const { holder, file } = task
167
- const name = holder.querySelector('.ke-media-name')
168
- if (name) name.textContent = `${file.name || '未命名'}(${formatSize(file.size)})`
169
- const size = holder.querySelector('.ke-video-size')
170
- if (size) size.textContent = formatSize(file.size)
171
- const head = holder.querySelector('.ke-video-head span')
172
- if (head && task.kind === 'video') head.textContent = `${file.name || '视频'} 上传中…`
173
- }
174
-
175
- resetHolder(task) {
176
- const holder = task.holder
177
- holder.dataset.kState = 'uploading'
178
- const bar = holder.querySelector('.ke-progress i')
179
- if (bar) {
180
- bar.style.width = '0%'
181
- bar.style.background = ''
182
- }
183
- const pct = holder.querySelector('.ke-media-pct')
184
- if (pct) pct.textContent = '0%'
185
- const actions = holder.querySelector('[data-k-actions]')
186
- if (actions) actions.innerHTML = '<button class="ke-media-cancel" type="button">取消</button>'
187
- this.decorateHolder(task)
188
- }
189
-
190
- updateProgress(holder, ratio, info, file, task) {
191
- const percent = Math.max(0, Math.min(100, Math.round((ratio || 0) * 100)))
192
- const bar = holder.querySelector('.ke-progress i')
193
- if (bar) bar.style.width = `${percent}%`
194
- const pct = holder.querySelector('.ke-media-pct')
195
- if (pct) pct.textContent = `${percent}%`
196
- if (task) this.editor.emit('uploadprogress', { file: task.file, kind: task.kind, percent, info })
197
- const meta = holder.querySelector('.ke-media-meta')
198
- if (!meta) return
199
- let label = meta.querySelector('[data-k-chunks]')
200
- if (!label) {
201
- label = h('span', { 'data-k-chunks': '', style: { color: '#8a8f99', fontSize: '12px' } })
202
- meta.insertBefore(label, meta.firstChild)
203
- }
204
- if (info?.chunks > 1) label.textContent = `${info.chunks} 片 · ${formatSize(info.loaded)}/${formatSize(info.total || file.size)}`
205
- else if (info?.speed) label.textContent = `${formatSize(info.speed)}/s`
206
- }
207
-
208
- finish(task, result) {
209
- const { holder, kind, file } = task
210
- const url = result.url
211
- holder.dataset.kState = 'done'
212
- holder.dataset.kUrl = url
213
- holder.innerHTML = ''
214
- if (kind === 'image') {
215
- const img = h('img', { src: url, alt: altOf(file), 'data-k-name': file.name || '', draggable: 'false' })
216
- img.addEventListener('dblclick', () => lightbox(img.src))
217
- holder.appendChild(img)
218
- } else {
219
- const video = h('video', { src: url, controls: '', playsinline: '', preload: 'metadata', 'data-k-name': file.name || '' })
220
- const body = h('div.ke-video-body', {}, [
221
- h('div.ke-video-head', {}, [h('span', { text: file.name || '视频' }), h('span.ke-video-size', { text: formatSize(result.size || file.size) })]),
222
- video,
223
- ])
224
- holder.appendChild(body)
225
- }
226
- if (task.previewUrl) URL.revokeObjectURL(task.previewUrl)
227
- this.tasks.delete(task.id)
228
- this.editor.refreshStatus()
229
- }
230
-
231
- fail(task, error) {
232
- const holder = task.holder
233
- holder.dataset.kState = 'error'
234
- const bar = holder.querySelector('.ke-progress i')
235
- if (bar) bar.style.background = '#e53e3e'
236
- const actions = holder.querySelector('[data-k-actions]')
237
- if (actions) {
238
- actions.innerHTML = ''
239
- actions.appendChild(h('span.ke-media-error', { text: error?.message || '上传失败', style: { marginRight: 'auto', fontSize: '12px' } }))
240
- actions.appendChild(h('button.ke-media-retry', { type: 'button', text: '重试' }))
241
- actions.appendChild(h('button.ke-media-cancel', { type: 'button', text: '移除' }))
242
- }
243
- }
244
-
245
- remove(task) {
246
- task.holder?.remove()
247
- if (task.previewUrl) URL.revokeObjectURL(task.previewUrl)
248
- this.tasks.delete(task.id)
249
- ensureEditableTail(this.editor.root)
250
- this.editor.refreshStatus()
251
- }
252
-
253
- /** 从 DOM 节点找到对应任务(用于气泡工具条删除) */
254
- taskFromNode(node) {
255
- const holder = node?.closest?.('[data-k-id]')
256
- return holder ? this.tasks.get(holder.dataset.kId) : null
257
- }
258
-
259
- cancelAll() {
260
- for (const task of [...this.tasks.values()]) {
261
- task.ctrl.abort()
262
- this.remove(task)
263
- }
264
- }
265
-
266
- /** 从网络链接插入(工具栏「网络链接」入口) */
267
- insertFromUrl(kind, url, name = '') {
268
- const node = kind === 'image' ? this.insertImageUrl(url, name) : this.insertVideoUrl(url, name || '网络视频')
269
- this.editor.toast.ok(kind === 'image' ? '已插入网络图片' : '已插入网络视频')
270
- this.editor.emit('insertmedia', { kind, url, node })
271
- return node
272
- }
273
-
274
- insertImageUrl(url, alt = '') {
275
- const img = h('img', { src: url, alt: alt || '', draggable: 'false' })
276
- img.addEventListener('dblclick', () => lightbox(img.src))
277
- const figure = h('figure', { 'data-k-media': 'image', 'data-k-state': 'done', 'data-k-url': url, contenteditable: 'false' }, [img])
278
- insertNode(this.editor.root, figure)
279
- this.editor.commitHistory()
280
- return figure
281
- }
282
-
283
- insertVideoUrl(url, label = '视频') {
284
- const video = h('video', { src: url, controls: '', playsinline: '', preload: 'metadata' })
285
- const card = h('div.ke-video-card', { 'data-k-media': 'video', 'data-k-state': 'done', 'data-k-url': url, contenteditable: 'false' }, [
286
- h('div.ke-video-body', {}, [h('div.ke-video-head', {}, [h('span', { text: label })]), video]),
287
- ])
288
- insertNode(this.editor.root, card)
289
- this.editor.commitHistory()
290
- return card
291
- }
292
-
293
- destroy() {
294
- this.cancelAll()
295
- this.input?.remove()
296
- this.input = null
297
- }
298
- }
299
-
300
- function altOf(file) {
301
- return String(file.name || '').replace(/\.[^.]+$/, '')
302
- }
303
-
304
- function progressRow() {
305
- return h('div.ke-media-meta', {}, [h('div.ke-progress', {}, [h('i', { style: { width: '0%' } })]), h('span.ke-media-pct', { text: '0%' })])
306
- }
307
-
308
- /** 大图压缩:超过 maxWidth 时等比缩放,失败则原样返回 */
309
- export async function maybeCompress(file, options = {}) {
310
- const compress = options?.compress
311
- if (!compress || !/^image\/(jpeg|jpg|png|webp)$/i.test(file.type)) return file
312
- const { maxWidth = 1440, quality = 0.85, minSize = 300 * 1024 } = typeof compress === 'object' ? compress : {}
313
- if (file.size < minSize) return file
314
- try {
315
- const bitmap = await createImageBitmap(file)
316
- if (bitmap.width <= maxWidth) {
317
- bitmap.close?.()
318
- return file
319
- }
320
- const canvas = document.createElement('canvas')
321
- canvas.width = maxWidth
322
- canvas.height = Math.round((bitmap.height * maxWidth) / bitmap.width)
323
- canvas.getContext('2d').drawImage(bitmap, 0, 0, canvas.width, canvas.height)
324
- bitmap.close?.()
325
- const blob = await new Promise((resolve) => canvas.toBlob(resolve, 'image/jpeg', quality))
326
- if (!blob || blob.size >= file.size) return file
327
- return new File([blob], String(file.name).replace(/\.\w+$/, '.jpg'), { type: 'image/jpeg', lastModified: file.lastModified })
328
- } catch {
329
- return file
330
- }
331
- }
package/src/ui/bubble.js DELETED
@@ -1,270 +0,0 @@
1
- /** 媒体气泡工具条:选中图片/视频后浮出的快捷操作 */
2
- import { h, getMediaAlign, setMediaAlign } from '../core/utils.js'
3
- import { icon } from '../core/icons.js'
4
- import { lightbox } from './toast.js'
5
-
6
- export class MediaBubble {
7
- constructor(editor) {
8
- this.editor = editor
9
- this.node = null
10
- this.el = h('div.ke-bubble', { hidden: true, contenteditable: 'false' })
11
- editor.el.appendChild(this.el)
12
- this.el.addEventListener('mousedown', (event) => event.preventDefault())
13
- this.el.addEventListener('click', (event) => this.onClick(event))
14
- this.replaceInput = h('input', { type: 'file', style: { display: 'none' } })
15
- this.replaceInput.addEventListener('change', () => {
16
- const file = this.replaceInput.files?.[0]
17
- this.replaceInput.value = ''
18
- if (file && this.node) this.editor.media.replaceNode(this.node, file)
19
- })
20
- editor.el.appendChild(this.replaceInput)
21
-
22
- // 图片缩放把手(挂在编辑器根容器,不进入内容区,避免污染快照/导出)
23
- this.resizeEl = h('div.ke-img-resize', {
24
- hidden: true,
25
- 'aria-label': '拖动缩放图片',
26
- title: '拖动缩放图片',
27
- html: '<svg viewBox="0 0 10 10"><path d="M7 1 L9 3 M4 4 L6 6 M1 7 L3 9" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>',
28
- })
29
- this.resizeTip = h('div.ke-img-resize-tip', { hidden: true })
30
- this.resizeEl.addEventListener('pointerdown', (event) => this.onResizePointerDown(event))
31
- editor.el.appendChild(this.resizeEl)
32
- editor.el.appendChild(this.resizeTip)
33
- }
34
-
35
- show(node) {
36
- this.node = node
37
- const kind = node.dataset.kMedia === 'video' ? 'video' : 'image'
38
- this.el.textContent = ''
39
- const actions = kind === 'image' ? this.imageActions(node) : this.videoActions(node)
40
- for (const item of actions) {
41
- if (item) this.el.appendChild(item)
42
- }
43
- this.el.hidden = false
44
- if (kind === 'image') this.attachResize()
45
- this.position()
46
- }
47
-
48
- hide() {
49
- this.el.hidden = true
50
- this.detachResize()
51
- this.node = null
52
- }
53
-
54
- attachResize() {
55
- if (!this.node?.querySelector('img')) return
56
- this.resizeEl.hidden = false
57
- this.positionHandle()
58
- }
59
-
60
- detachResize() {
61
- this.resizeEl.hidden = true
62
- this.resizeTip.hidden = true
63
- }
64
-
65
- position() {
66
- if (!this.node) return
67
- this.positionHandle()
68
- if (this.el.hidden) return
69
- const rootBox = this.editor.el.getBoundingClientRect()
70
- const box = this.node.getBoundingClientRect()
71
- const view = this.editor.scrollEl.getBoundingClientRect()
72
- if (!box.height && !box.width) return this.hide()
73
- // 媒体节点滚出可视区域时隐藏气泡
74
- if (box.bottom < view.top || box.top > view.bottom) return this.hide()
75
- const popBox = this.el.getBoundingClientRect()
76
- const left = box.left - rootBox.left + box.width / 2
77
- const spaceAbove = box.top - rootBox.top
78
- const flip = spaceAbove < popBox.height + 18
79
- const top = flip ? box.bottom - rootBox.top + 10 : box.top - rootBox.top - 10
80
- this.el.classList.toggle('ke-is-below', flip)
81
- const halfWidth = popBox.width / 2
82
- this.el.style.left = `${Math.min(Math.max(left, halfWidth + 8), Math.max(halfWidth + 8, rootBox.width - halfWidth - 8))}px`
83
- this.el.style.top = `${Math.max(6, top)}px`
84
- }
85
-
86
- positionHandle() {
87
- const node = this.node
88
- if (!node || this.resizeEl.hidden) return
89
- const img = node.querySelector('img')
90
- if (!img) return
91
- const rootBox = this.editor.el.getBoundingClientRect()
92
- const box = img.getBoundingClientRect()
93
- if (!box.width && !box.height) return
94
- const size = 18
95
- const gap = 6
96
- this.resizeEl.style.left = `${box.right - rootBox.left - size - gap}px`
97
- this.resizeEl.style.top = `${box.bottom - rootBox.top - size - gap}px`
98
- }
99
-
100
- updateResizeTip(pct) {
101
- if (this.resizeTip.hidden || !this.node) return
102
- this.resizeTip.textContent = `${Math.round(pct)}%`
103
- const rootBox = this.editor.el.getBoundingClientRect()
104
- const box = this.node.querySelector('img')?.getBoundingClientRect() || this.node.getBoundingClientRect()
105
- this.resizeTip.style.left = `${box.left - rootBox.left + box.width / 2}px`
106
- this.resizeTip.style.top = `${box.top - rootBox.top - 10}px`
107
- }
108
-
109
- onResizePointerDown(event) {
110
- if (event.button != null && event.button !== 0) return
111
- const img = this.node?.querySelector('img')
112
- if (!img) return
113
- event.preventDefault()
114
- event.stopPropagation()
115
-
116
- const figure = this.node
117
- const figureWidth = figure.getBoundingClientRect().width
118
- const startPct = clamp(currentWidthPercent(img, figureWidth), 15, 100)
119
- const startX = event.clientX
120
-
121
- // 拖拽期间隐藏气泡,结束后恢复
122
- this.el.hidden = true
123
- this.resizeTip.hidden = false
124
- this.updateResizeTip(startPct)
125
- try {
126
- this.resizeEl.setPointerCapture(event.pointerId)
127
- } catch {
128
- /* 模拟派发时可能无活动指针,忽略 */
129
- }
130
-
131
- const move = (e) => {
132
- const dx = e.clientX - startX
133
- const deltaPct = figureWidth ? (dx / figureWidth) * 100 : 0
134
- const pct = clamp(Math.round(startPct + deltaPct), 15, 100)
135
- img.style.width = `${pct}%`
136
- img.style.maxWidth = '100%'
137
- this.updateResizeTip(pct)
138
- this.positionHandle()
139
- }
140
- const up = () => {
141
- this.resizeEl.removeEventListener('pointermove', move)
142
- this.resizeEl.removeEventListener('pointerup', up)
143
- this.resizeEl.removeEventListener('pointercancel', up)
144
- this.resizeTip.hidden = true
145
- this.editor.commitHistory()
146
- this.editor.refreshStatus()
147
- this.el.hidden = false
148
- this.position()
149
- }
150
- this.resizeEl.addEventListener('pointermove', move)
151
- this.resizeEl.addEventListener('pointerup', up)
152
- this.resizeEl.addEventListener('pointercancel', up)
153
- }
154
-
155
- imageActions(node) {
156
- const img = node.querySelector('img')
157
- const align = getMediaAlign(img)
158
- const radius = img && parseFloat(getComputedStyle(img).borderRadius) > 0
159
- const applyAlign = (a) => {
160
- setMediaAlign(img, a)
161
- this.editor.commitHistory()
162
- this.position()
163
- }
164
- const applySize = (w) => {
165
- img.style.width = w
166
- setMediaAlign(img, getMediaAlign(img))
167
- this.editor.commitHistory()
168
- this.position()
169
- }
170
- return [
171
- btn('alignLeft', '左对齐', () => applyAlign('left'), align === 'left'),
172
- btn('alignCenter', '居中', () => applyAlign('center'), align === 'center'),
173
- btn('alignRight', '右对齐', () => applyAlign('right'), align === 'right'),
174
- sep(),
175
- textBtn('30%', '宽度 30%', () => applySize('30%')),
176
- textBtn('60%', '宽度 60%', () => applySize('60%')),
177
- textBtn('100%', '宽度 100%', () => applySize('100%')),
178
- sep(),
179
- btn('rounded', '圆角', () => toggleRadius(img, node), radius),
180
- btn('preview', '查看大图', () => previewImage(img)),
181
- btn('upload', '替换图片', () => this.pick('image')),
182
- sep(),
183
- btn('trash', '删除', () => this.remove()),
184
- ]
185
- }
186
-
187
- videoActions() {
188
- return [
189
- textBtn('小', '窄版', () => this.setVideoWidth('60%')),
190
- textBtn('大', '宽版', () => this.setVideoWidth('100%')),
191
- sep(),
192
- btn('upload', '替换视频', () => this.pick('video')),
193
- sep(),
194
- btn('trash', '删除', () => this.remove()),
195
- ]
196
- }
197
- pick(kind) {
198
- this.replaceInput.accept = kind === 'image' ? this.editor.options.imageAccept : this.editor.options.videoAccept
199
- this.replaceInput.click()
200
- }
201
-
202
- setVideoWidth(width) {
203
- if (!this.node) return
204
- this.node.style.width = width
205
- this.editor.commitHistory()
206
- this.position()
207
- }
208
-
209
- remove() {
210
- if (!this.node) return
211
- const task = this.editor.media.taskFromNode(this.node)
212
- if (task) this.editor.media.remove(task)
213
- else {
214
- const next = this.node.nextElementSibling
215
- this.node.remove()
216
- if (next) this.editor.focus()
217
- this.editor.commitHistory()
218
- }
219
- this.hide()
220
- this.editor.refreshStatus()
221
- }
222
-
223
- onClick() {
224
- /* 行为已在各按钮中处理 */
225
- }
226
-
227
- destroy() {
228
- this.el.remove()
229
- this.replaceInput.remove()
230
- this.resizeEl.remove()
231
- this.resizeTip.remove()
232
- }
233
- }
234
-
235
- function btn(iconName, title, onclick, active = false) {
236
- const node = h('button', { type: 'button', title, html: icon(iconName) })
237
- if (active) node.classList.add('ke-is-active')
238
- node.addEventListener('click', onclick)
239
- return node
240
- }
241
-
242
- function textBtn(label, title, onclick) {
243
- const node = h('button', { type: 'button', title, text: label })
244
- node.addEventListener('click', onclick)
245
- return node
246
- }
247
-
248
- const sep = () => h('span.ke-sep')
249
-
250
- function toggleRadius(img, node) {
251
- if (!img) return
252
- const has = parseFloat(getComputedStyle(img).borderRadius) > 0
253
- img.style.borderRadius = has ? '0' : '10px'
254
- node.dispatchEvent(new Event('change', { bubbles: true }))
255
- }
256
-
257
- function previewImage(img) {
258
- if (img) lightbox(img.src)
259
- }
260
-
261
- function clamp(value, min, max) {
262
- return Math.min(max, Math.max(min, value))
263
- }
264
-
265
- function currentWidthPercent(img, figureWidth) {
266
- if (!figureWidth) return 100
267
- const match = img?.style?.width?.match(/^([\d.]+)%$/)
268
- if (match) return parseFloat(match[1])
269
- return (img.getBoundingClientRect().width / figureWidth) * 100
270
- }