kyle-editor 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +194 -2
- package/dist/keditor-vue.cjs +120 -0
- package/dist/keditor-vue.es.js +118 -0
- package/dist/keditor-vue.umd.js +122 -0
- package/dist/keditor.cjs +4116 -0
- package/dist/keditor.es.js +4116 -0
- package/dist/keditor.umd.js +4120 -0
- package/docs/api.md +223 -0
- package/docs/index.md +8 -0
- package/docs/integration.md +296 -0
- package/docs/npm-publish.md +412 -0
- package/docs/upload-adapter.md +186 -0
- package/package.json +77 -4
- package/src/core/commands.js +311 -0
- package/src/core/editor.js +795 -0
- package/src/core/format.js +64 -0
- package/src/core/history.js +92 -0
- package/src/core/icons.js +49 -0
- package/src/core/paste.js +140 -0
- package/src/core/sanitize.js +182 -0
- package/src/core/selection.js +129 -0
- package/src/core/toolbar.js +465 -0
- package/src/core/utils.js +95 -0
- package/src/index.js +33 -0
- package/src/media/media.js +331 -0
- package/src/ui/bubble.js +270 -0
- package/src/ui/popover.js +507 -0
- package/src/ui/preview.js +59 -0
- package/src/ui/source.js +97 -0
- package/src/ui/styles.js +447 -0
- package/src/ui/toast.js +56 -0
- package/src/upload/adapters.js +206 -0
- package/src/upload/chunk-uploader.js +193 -0
- package/src/upload/fingerprint.js +38 -0
- package/src/vue/index.js +104 -0
- package/types/index.d.ts +345 -0
- package/types/vue.d.ts +73 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/** 编辑指令集:以最小可用为原则,优先使用浏览器原生命令,自定义指令保持输出干净的微信风格 HTML */
|
|
2
|
+
import { getRange, insertNode, closestBlock } from './selection.js'
|
|
3
|
+
import { sanitizeHtml } from './sanitize.js'
|
|
4
|
+
import { h, uid, setMediaAlign } from './utils.js'
|
|
5
|
+
|
|
6
|
+
const MARKS = { bold: 'bold', italic: 'italic', underline: 'underline', strikeThrough: 'strike', through: 'strikethrough' }
|
|
7
|
+
|
|
8
|
+
export class Commands {
|
|
9
|
+
constructor(editor) {
|
|
10
|
+
this.editor = editor
|
|
11
|
+
this.root = editor.root
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 统一入口:确保聚焦 + 开启 styleWithCSS(输出 inline style,符合公众号粘贴习惯) */
|
|
15
|
+
exec(command, value = null) {
|
|
16
|
+
this.editor.focus()
|
|
17
|
+
document.execCommand('styleWithCSS', false, true)
|
|
18
|
+
document.execCommand('defaultParagraphSeparator', false, 'p')
|
|
19
|
+
const ok = document.execCommand(command, false, value)
|
|
20
|
+
this.editor.afterEdit()
|
|
21
|
+
return ok
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
queryState(command) {
|
|
25
|
+
try {
|
|
26
|
+
return document.queryCommandState(command)
|
|
27
|
+
} catch {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
queryValue(command) {
|
|
33
|
+
try {
|
|
34
|
+
return document.queryCommandValue(command)
|
|
35
|
+
} catch {
|
|
36
|
+
return ''
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
hasSelection() {
|
|
41
|
+
const range = getRange(this.root)
|
|
42
|
+
return !!(range && !range.collapsed)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
toggleMark(name) {
|
|
46
|
+
const map = { bold: 'bold', italic: 'italic', underline: 'underline', strike: 'strikeThrough' }
|
|
47
|
+
const cmd = map[name]
|
|
48
|
+
if (!cmd) return
|
|
49
|
+
this.exec(cmd)
|
|
50
|
+
// 原生命令在空段落上会产生无意义节点,做一次清理
|
|
51
|
+
this.root.querySelectorAll('span[style=""]').forEach((n) => n.replaceWith(...n.childNodes))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 字号:用 fontSize=7 作为标记再改写成 font-size:px,避免浏览器字号档位限制 */
|
|
55
|
+
setFontSize(px) {
|
|
56
|
+
this.editor.focus()
|
|
57
|
+
document.execCommand('styleWithCSS', false, false)
|
|
58
|
+
const marker = `__ke_fs_${uid()}__`
|
|
59
|
+
this.root.setAttribute('data-ke-marker', marker)
|
|
60
|
+
document.execCommand('fontSize', false, '7')
|
|
61
|
+
this.root.removeAttribute('data-ke-marker')
|
|
62
|
+
this.root.querySelectorAll('font[size="7"]').forEach((font) => {
|
|
63
|
+
const span = document.createElement('span')
|
|
64
|
+
span.style.fontSize = `${px}px`
|
|
65
|
+
while (font.firstChild) span.appendChild(font.firstChild)
|
|
66
|
+
font.replaceWith(span)
|
|
67
|
+
})
|
|
68
|
+
// styleWithCSS 下浏览器可能生成 font-size:xx-large
|
|
69
|
+
this.root.querySelectorAll('span').forEach((span) => {
|
|
70
|
+
if (/xx-large/i.test(span.style.fontSize)) span.style.fontSize = `${px}px`
|
|
71
|
+
})
|
|
72
|
+
document.execCommand('styleWithCSS', false, true)
|
|
73
|
+
this.editor.afterEdit()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
setColor(mode, color) {
|
|
77
|
+
this.editor.focus()
|
|
78
|
+
document.execCommand('styleWithCSS', false, true)
|
|
79
|
+
if (!color) color = mode === 'fore' ? '#3f3f3f' : 'transparent'
|
|
80
|
+
const ok = document.execCommand(mode === 'fore' ? 'foreColor' : 'hiliteColor', false, color)
|
|
81
|
+
if (!ok) document.execCommand(mode === 'fore' ? 'foreColor' : 'backColor', false, color)
|
|
82
|
+
// 清掉 rgba 背景为 transparent 的冗余节点
|
|
83
|
+
if (mode === 'back' && color === 'transparent') {
|
|
84
|
+
this.root.querySelectorAll('[style*="background-color"]').forEach((node) => {
|
|
85
|
+
if (/background-color:\s*transparent|rgba\(0, 0, 0, 0\)/i.test(node.getAttribute('style') || '')) {
|
|
86
|
+
node.style.backgroundColor = ''
|
|
87
|
+
if (!node.getAttribute('style').trim()) node.replaceWith(...node.childNodes)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
this.editor.afterEdit()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
setAlign(align) {
|
|
95
|
+
// 选中图片时,对齐作用于图片本身(img 是 display:block,靠 margin:auto 居中)。
|
|
96
|
+
// 浏览器原生命令与给 figure 设 text-align 都对 block 图片无效。
|
|
97
|
+
const media = this.editor.bubble?.node
|
|
98
|
+
if (media && media.dataset?.kMedia === 'image' && align !== 'justify') {
|
|
99
|
+
const img = media.querySelector('img')
|
|
100
|
+
if (img) {
|
|
101
|
+
setMediaAlign(img, align)
|
|
102
|
+
this.editor.commitHistory()
|
|
103
|
+
this.editor.bubble.position()
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const map = { left: 'justifyLeft', center: 'justifyCenter', right: 'justifyRight', justify: 'justifyFull' }
|
|
108
|
+
const cmd = map[align]
|
|
109
|
+
if (!cmd) return
|
|
110
|
+
this.editor.focus()
|
|
111
|
+
const range = getRange(this.root)
|
|
112
|
+
const block = closestBlock(range?.commonAncestorContainer || this.root, this.root)
|
|
113
|
+
document.execCommand('styleWithCSS', false, true)
|
|
114
|
+
document.execCommand(cmd)
|
|
115
|
+
// 媒体块(figure/video 卡片)直接改样式,浏览器命令对它们无效
|
|
116
|
+
const node = block || this.root.firstElementChild
|
|
117
|
+
if (node && /^(FIGURE|DIV|VIDEO)/.test(node.tagName || '')) node.style.textAlign = align
|
|
118
|
+
this.editor.afterEdit()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
toggleList(kind) {
|
|
122
|
+
this.exec(kind === 'ordered' ? 'insertOrderedList' : 'insertUnorderedList')
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
setHeading(tag) {
|
|
126
|
+
const value = tag ? `<${tag}>` : '<p>'
|
|
127
|
+
this.exec('formatBlock', value)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
toggleBlockquote() {
|
|
131
|
+
const range = getRange(this.root)
|
|
132
|
+
const block = closestBlock(range?.commonAncestorContainer || this.root, this.root)
|
|
133
|
+
if (block && block.tagName === 'BLOCKQUOTE') this.exec('formatBlock', '<p>')
|
|
134
|
+
else this.exec('formatBlock', '<blockquote>')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 链接:有选区则包裹,无选区则插入完整 a 标签 */
|
|
138
|
+
createLink({ url, text = '', target = false }) {
|
|
139
|
+
this.editor.focus()
|
|
140
|
+
const range = getRange(this.root)
|
|
141
|
+
const hasSel = range && !range.collapsed
|
|
142
|
+
const label = text || (hasSel ? range.toString() : url)
|
|
143
|
+
if (hasSel) {
|
|
144
|
+
document.execCommand('createLink', false, url)
|
|
145
|
+
range.commonAncestorContainer.parentElement?.closest?.('a')
|
|
146
|
+
this.root.querySelectorAll(`a[href="${cssEscape(url)}"]`).forEach((a) => {
|
|
147
|
+
if (target) {
|
|
148
|
+
a.setAttribute('target', '_blank')
|
|
149
|
+
a.setAttribute('rel', 'noopener noreferrer')
|
|
150
|
+
} else {
|
|
151
|
+
a.removeAttribute('target')
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
} else {
|
|
155
|
+
const a = h('a', { href: url }, [label])
|
|
156
|
+
if (target) {
|
|
157
|
+
a.setAttribute('target', '_blank')
|
|
158
|
+
a.setAttribute('rel', 'noopener noreferrer')
|
|
159
|
+
}
|
|
160
|
+
insertNode(this.root, a, { inline: true })
|
|
161
|
+
}
|
|
162
|
+
this.editor.afterEdit()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
removeLink() {
|
|
166
|
+
const range = getRange(this.root)
|
|
167
|
+
const anchors = closestAnchors(range, this.root)
|
|
168
|
+
anchors.forEach((node) => node.replaceWith(...node.childNodes))
|
|
169
|
+
this.editor.afterEdit()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
insertTable(cols, rows) {
|
|
173
|
+
const table = document.createElement('table')
|
|
174
|
+
const tbody = document.createElement('tbody')
|
|
175
|
+
for (let r = 0; r < rows; r += 1) {
|
|
176
|
+
const tr = document.createElement('tr')
|
|
177
|
+
for (let c = 0; c < cols; c += 1) {
|
|
178
|
+
const cell = document.createElement(r === 0 ? 'th' : 'td')
|
|
179
|
+
cell.innerHTML = '<br>'
|
|
180
|
+
tr.appendChild(cell)
|
|
181
|
+
}
|
|
182
|
+
tbody.appendChild(tr)
|
|
183
|
+
}
|
|
184
|
+
table.appendChild(tbody)
|
|
185
|
+
insertNode(this.root, table)
|
|
186
|
+
this.exec('insertHTML', '')
|
|
187
|
+
this.editor.afterEdit()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
insertCodeBlock(code = '') {
|
|
191
|
+
const pre = document.createElement('pre')
|
|
192
|
+
const codeEl = document.createElement('code')
|
|
193
|
+
codeEl.textContent = code || '// 在这里输入代码'
|
|
194
|
+
pre.appendChild(codeEl)
|
|
195
|
+
insertNode(this.root, pre)
|
|
196
|
+
this.editor.afterEdit()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
insertDivider() {
|
|
200
|
+
const hr = document.createElement('hr')
|
|
201
|
+
insertNode(this.root, hr)
|
|
202
|
+
this.editor.afterEdit()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
insertHTML(html, { clean = true } = {}) {
|
|
206
|
+
const value = clean ? sanitizeHtml(html) : html
|
|
207
|
+
this.editor.focus()
|
|
208
|
+
const range = getRange(this.root)
|
|
209
|
+
if (range) {
|
|
210
|
+
range.deleteContents()
|
|
211
|
+
const tmp = document.createElement('div')
|
|
212
|
+
tmp.innerHTML = value
|
|
213
|
+
const frag = document.createDocumentFragment()
|
|
214
|
+
while (tmp.firstChild) frag.appendChild(tmp.firstChild)
|
|
215
|
+
const last = frag.lastChild
|
|
216
|
+
range.insertNode(frag)
|
|
217
|
+
if (last) {
|
|
218
|
+
const after = document.createRange()
|
|
219
|
+
after.setStartAfter(last.nodeType === 3 ? last : last.lastChild || last)
|
|
220
|
+
after.collapse(true)
|
|
221
|
+
const sel = window.getSelection()
|
|
222
|
+
sel.removeAllRanges()
|
|
223
|
+
sel.addRange(after)
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
this.root.insertAdjacentHTML('beforeend', value)
|
|
227
|
+
}
|
|
228
|
+
this.editor.afterEdit()
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** 清除格式:内联样式 + 行内标签 + 段落回归 p */
|
|
232
|
+
clearFormat() {
|
|
233
|
+
this.editor.focus()
|
|
234
|
+
if (this.hasSelection()) {
|
|
235
|
+
document.execCommand('removeFormat')
|
|
236
|
+
document.execCommand('unlink')
|
|
237
|
+
const range = getRange(this.root)
|
|
238
|
+
const block = closestBlock(range?.commonAncestorContainer, this.root)
|
|
239
|
+
if (block && /^(H[1-6]|BLOCKQUOTE|PRE)$/.test(block.tagName)) this.exec('formatBlock', '<p>')
|
|
240
|
+
this.stripInlineStylesInRange(range)
|
|
241
|
+
} else {
|
|
242
|
+
const range = getRange(this.root)
|
|
243
|
+
const block = closestBlock(range?.commonAncestorContainer || this.root, this.root)
|
|
244
|
+
if (block) {
|
|
245
|
+
const html = block.innerHTML
|
|
246
|
+
const p = document.createElement('p')
|
|
247
|
+
p.innerHTML = sanitizeHtml(html, { allowEmpty: true })
|
|
248
|
+
p.innerHTML = p.innerHTML.replace(/<(?!br|img)[^>]+>/gi, '')
|
|
249
|
+
block.replaceWith(p)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
this.editor.afterEdit()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
stripInlineStylesInRange(range) {
|
|
256
|
+
if (!range) return
|
|
257
|
+
const nodes = []
|
|
258
|
+
const walker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT)
|
|
259
|
+
let node = walker.currentNode
|
|
260
|
+
while (node) {
|
|
261
|
+
if (range.intersectsNode(node) && node.nodeType === 1) nodes.push(node)
|
|
262
|
+
node = walker.nextNode()
|
|
263
|
+
}
|
|
264
|
+
nodes.forEach((el) => {
|
|
265
|
+
if (el === this.root) return
|
|
266
|
+
el.removeAttribute('style')
|
|
267
|
+
if (/^(SPAN|FONT)$/.test(el.tagName) && !el.attributes.length) el.replaceWith(...el.childNodes)
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 标题 / 段落的当前状态(供工具栏高亮) */
|
|
272
|
+
currentBlock() {
|
|
273
|
+
const range = getRange(this.root)
|
|
274
|
+
const block = closestBlock(range?.commonAncestorContainer || this.root, this.root)
|
|
275
|
+
return block?.tagName?.toLowerCase() || 'p'
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
currentFontSize() {
|
|
279
|
+
const range = getRange(this.root)
|
|
280
|
+
if (!range) return ''
|
|
281
|
+
let node = range.startContainer
|
|
282
|
+
while (node && node !== this.root) {
|
|
283
|
+
if (node.nodeType === 1 && node.style.fontSize) return node.style.fontSize.replace('px', '')
|
|
284
|
+
node = node.parentNode
|
|
285
|
+
}
|
|
286
|
+
return ''
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function closestAnchors(range, root) {
|
|
291
|
+
if (!range) return []
|
|
292
|
+
const out = new Set()
|
|
293
|
+
const start = range.startContainer.nodeType === 1 ? range.startContainer.querySelector?.('a') || range.startContainer : range.startContainer
|
|
294
|
+
let node = start
|
|
295
|
+
while (node && node !== root) {
|
|
296
|
+
if (node.tagName === 'A') out.add(node)
|
|
297
|
+
node = node.parentNode
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
range.querySelectorAll?.('a').forEach((a) => out.add(a))
|
|
301
|
+
} catch {
|
|
302
|
+
/* older browsers */
|
|
303
|
+
}
|
|
304
|
+
const container = range.commonAncestorContainer
|
|
305
|
+
if (container.nodeType === 1) container.querySelectorAll('a').forEach((a) => (range.intersectsNode(a) ? out.add(a) : null))
|
|
306
|
+
return [...out]
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function cssEscape(value) {
|
|
310
|
+
return window.CSS?.escape ? CSS.escape(value) : String(value).replace(/["\\]/g, '\\$&')
|
|
311
|
+
}
|