kyle-editor 1.0.6 → 1.0.8
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/README.md +232 -30
- package/dist/keditor-vue.cjs +3 -2
- package/dist/keditor-vue.es.js +3 -2
- package/dist/keditor-vue.umd.js +3 -2
- package/dist/keditor.cjs +2814 -521
- package/dist/keditor.es.js +2814 -521
- package/dist/keditor.umd.js +2812 -519
- package/dist/keditor.umd.min.js +109 -6
- package/docs/api.md +108 -15
- package/docs/integration.md +24 -14
- package/docs/upload-adapter.md +203 -94
- package/package.json +2 -3
- package/types/index.d.ts +273 -23
- package/types/vue.d.ts +2 -2
- package/docs/index.md +0 -8
- package/imgs/img.png +0 -0
- package/imgs/img_1.png +0 -0
- package/imgs/img_2.png +0 -0
- package/imgs/img_3.png +0 -0
package/docs/api.md
CHANGED
|
@@ -18,7 +18,7 @@ const editor = KEditor.create(el, options) // el: 选择器 / HTMLElement / Vu
|
|
|
18
18
|
|------|------|--------|------|
|
|
19
19
|
| `value` | `string` | `''` | 初始 HTML;留空时取挂载节点的 `innerHTML` |
|
|
20
20
|
| `placeholder` | `string` | `'开始撰写内容…'` | 空内容占位文案 |
|
|
21
|
-
| `height` | `number` | `
|
|
21
|
+
| `height` | `number \| 'auto'` | `0` | 内容区高度:`0` / `'auto'` / 不写 = **随内容自动撑开**(整页滚动);填 px 才固定高度并内部滚动 |
|
|
22
22
|
| `minHeight` | `number` | `320` | 纸张最小高度 |
|
|
23
23
|
| `paperWidth` | `number` | `677` | 版心基准宽度(与公众号一致);`fluid` 模式下它是最小宽度 |
|
|
24
24
|
| `paperMode` | `'fluid' \| 'fixed'` | `'fluid'` | 宽屏时版心随容器生长(两侧留白恒定)/ 恒定 `paperWidth + 64` |
|
|
@@ -35,12 +35,22 @@ const editor = KEditor.create(el, options) // el: 选择器 / HTMLElement / Vu
|
|
|
35
35
|
| `wordCount` | `boolean` | `true` | 底部字数/媒体统计 |
|
|
36
36
|
| `sanitize` | `boolean` | `true` | 初始化与 `setContent` 时净化 HTML |
|
|
37
37
|
| `historyLimit` | `number` | `60` | 撤销栈深度 |
|
|
38
|
-
| `uploader` | `
|
|
39
|
-
| `
|
|
38
|
+
| `uploader` | `string \| object \| fn \| SimpleUploader` | `'mock'` | **普通上传**(整个文件一次请求):直接给接口地址(如 `'/api/upload'`)即可;也可 `{type:'http', url}` / 自定义 adapter / 函数,见 [upload-adapter.md](upload-adapter.md)。只配本项时不启用分片上传 |
|
|
39
|
+
| `chunkUploader` | `string \| object \| fn \| ChunkUploader` | `'mock'` | **分片上传**(切片并发,只打一个接口,响应里 `url` 非空即成功);`null` 表示不启用分片。与 `uploader` 都配置时按 `upload.mode` 选路;只配本项时 `mode` 自动为 `'chunk'` |
|
|
40
|
+
| `upload` | `object` | `{mode:'auto', chunkThreshold:5MB, chunkSize:1MB, concurrency:3, retries:2, fingerprint:'fast'}` | 链路与分片参数;`mode` 为 `'auto'`(按体积)/ `'normal'`(只普通)/ `'chunk'`(只分片),另支持 `completeOnUrl` 覆盖「响应带 url 即成功」判定 |
|
|
40
41
|
| `image.compress` | `object\|false` | `{maxWidth:1440, quality:0.85, minSize:300KB}` | 上传前大图压缩 |
|
|
41
42
|
| `maxImageSize` | `number` | `20MB` | 图片体积上限,超限拦截 |
|
|
42
43
|
| `maxVideoSize` | `number` | `512MB` | 视频体积上限 |
|
|
43
44
|
| `imageAccept` / `videoAccept` | `string` | 常见图/视频类型 | 文件选择器 `accept` |
|
|
45
|
+
| `code.languages` | `string[]` | `[]` | 代码块可选语言(主名或别名,如 `['js','mysql','yml']`);留空 = 全部 20 种 |
|
|
46
|
+
| `code.defaultLanguage` | `string` | `'plaintext'` | 新建代码块的默认语言;不在 `languages` 内会落到清单第一项 |
|
|
47
|
+
| `code.highlight` | `boolean` | `true` | 是否语法高亮;`false` 时只保留纯文本(语言标记照写) |
|
|
48
|
+
| `markdown.insertMode` | `'replace'\|'append'\|'cursor'` | `'replace'` | Markdown 导入默认落点:替换全文 / 追加文末 / 插入光标(面板里可临时改) |
|
|
49
|
+
| `markdown.maxSize` | `number` | `5MB` | 单个 Markdown 文件体积上限(字节),`0` = 不限制 |
|
|
50
|
+
| `markdown.gfm` | `boolean` | `true` | GFM 扩展:表格 / 删除线 / 任务列表 / 裸链接 |
|
|
51
|
+
| `markdown.breaks` | `boolean` | `false` | 段落内单换行是否转 `<br>` |
|
|
52
|
+
| `markdown.linkTarget` | `boolean\|string` | `false` | 外链自动加 `target` + `rel="noopener noreferrer"` |
|
|
53
|
+
| `markdown.frontMatter` | `boolean` | `true` | 丢弃开头的 YAML front matter |
|
|
44
54
|
| `paste.enabled` | `boolean` | `true` | 接管粘贴 |
|
|
45
55
|
| `paste.clean` | `boolean` | `true` | 粘贴内容净化(Word/WPS 自动清洗) |
|
|
46
56
|
| `paste.uploadFiles` | `boolean` | `true` | 粘贴截图/文件即上传 |
|
|
@@ -50,9 +60,11 @@ const editor = KEditor.create(el, options) // el: 选择器 / HTMLElement / Vu
|
|
|
50
60
|
|
|
51
61
|
`undo` `redo` `heading` `fontsize` `bold` `italic` `underline` `strike` `foreColor` `backColor`
|
|
52
62
|
`alignLeft` `alignCenter` `alignRight` `alignJustify` `ul` `ol` `quote` `link` `image` `video`
|
|
53
|
-
`table` `code` `hr` `clear` `source` `preview` `fullscreen`;分隔符 `'|'`,右对齐标记 `':tail'`。
|
|
63
|
+
`table` `code` `markdown` `hr` `clear` `source` `preview` `fullscreen`;分隔符 `'|'`,右对齐标记 `':tail'`。
|
|
54
64
|
|
|
55
|
-
> `preview`
|
|
65
|
+
> `preview` **不在默认工具栏里**(默认尾栏为「导入MD / 源码 / 全屏」),需要时手动写回 `toolbar`,或直接调 `editor.openPreview()`。
|
|
66
|
+
>
|
|
67
|
+
> `code` 按钮点击展开**语言面板**(见下方面板一览与「代码块」一节),不再直接插入空代码块。
|
|
56
68
|
|
|
57
69
|
此外可放入 `customButtons` 里定义的**按钮 `name`**(如 `toolbar: ['bold', '|', 'tpl']`)。
|
|
58
70
|
**未传 `toolbar` 时,自定义按钮会自动插入工具栏**:写了 `position` 按 `position` 放,没写则追加到主区末尾(插在 `:tail` 之前)。
|
|
@@ -70,7 +82,7 @@ toolbar: ['undo', 'redo', '|', 'bold', 'italic', '|', 'image', 'video', ':tail',
|
|
|
70
82
|
| 左缘渐隐 | 已向右滑出时提示左边还有内容 |
|
|
71
83
|
| 首次引导气泡 | 「工具栏可左右滑动」,3.8s 后或首次滑动后消失,`localStorage` 记忆不重复弹 |
|
|
72
84
|
|
|
73
|
-
鼠标还可**直接按住拖动**平移工具栏(光标变为抓握状);触屏沿用系统原生滑动。`:tail`
|
|
85
|
+
鼠标还可**直接按住拖动**平移工具栏(光标变为抓握状);触屏沿用系统原生滑动。`:tail` 之后的「导入MD / 源码 / 全屏」(以及 `position:'tail'` 的自定义按钮)始终吸附在右端可见。
|
|
74
86
|
|
|
75
87
|
#### 下拉面板一览
|
|
76
88
|
|
|
@@ -83,6 +95,8 @@ toolbar: ['undo', 'redo', '|', 'bold', 'italic', '|', 'image', 'video', ':tail',
|
|
|
83
95
|
| `image` / `video` | 展开插入面板 | 两个页签:**本地上传**(点击或拖拽到虚线区)/ **网络链接**(粘贴地址即可插入,附带可访问性校验提示) |
|
|
84
96
|
| `table` | 展开网格 | 10×8 规格选择,实时显示「N 列 × M 行」 |
|
|
85
97
|
| `link` | 展开表单 | 文字 / 地址 / 新窗口打开,已链接时提供「移除链接」 |
|
|
98
|
+
| `code` | 展开「插入代码块 / 代码块语言」面板 | 20 种语言列表(含一句话说明),支持按名称/别名搜索(`js`、`mysql`、`yml`…)与键盘回车;**光标不在代码块内**=插入新块,**光标在块内**=改写该块语言 |
|
|
99
|
+
| `markdown` | 展开「导入 Markdown」面板(按钮文案:导入MD) | 两个页签:**选择文件**(点击或拖拽到虚线区,`.md/.markdown/.txt`,自动识别编码)/ **粘贴文本**;下方为三种落点(替换全文 / 追加文末 / 插入光标)+ **实时转换预览**与结构统计;标题栏显示字符数,底部「导入到正文」 |
|
|
86
100
|
|
|
87
101
|
面板统一样式:圆角卡片 + 标题栏 + 分区小标题 + 入场动画;打开时会自动记录正文选区,操作后还原,因此**先选文字再点颜色**一定生效。
|
|
88
102
|
|
|
@@ -153,7 +167,7 @@ toolbar: ['undo', 'redo', '|', 'bold', 'italic', '|', 'image', 'video', ':tail',
|
|
|
153
167
|
|------|------|
|
|
154
168
|
| 不写 / `'end'` | 主区末尾,插在右侧吸附尾栏之前(多个未定位按钮共用一个分隔符) |
|
|
155
169
|
| `'start'` | 工具栏第一位 |
|
|
156
|
-
| `'tail'` |
|
|
170
|
+
| `'tail'` | 尾栏末尾(与「导入MD / 源码 / 全屏」同区;未启用 `:tail` 时回退主区末尾) |
|
|
157
171
|
| `number` | 第 n 个按钮之前(0 起,按视觉顺序,分隔符不计数) |
|
|
158
172
|
| `'image'` 等按钮名 | 插到该按钮之前 |
|
|
159
173
|
| `{ before: name }` / `{ after: name }` | 显式锚点;锚点可以是内置按钮,也可以是另一个自定义按钮 |
|
|
@@ -219,10 +233,13 @@ editor.setButtonOptions(name, items) // 外部灌入数据(成为权威数据
|
|
|
219
233
|
editor.getButtonValue(name) // 当前选中项
|
|
220
234
|
editor.setButtonValue(name, value)// 设置选中项,按钮文案同步
|
|
221
235
|
editor.toolbar.openCustom(name) // 代码打开面板
|
|
236
|
+
editor.toolbar.openCode() // 打开代码块语言面板(等同点击工具栏「代码块」)
|
|
222
237
|
editor.toolbar.buttonNodes() // 当前工具栏按钮节点(按视觉顺序,不含分隔符)
|
|
223
238
|
|
|
224
239
|
editor.setPaper({ width, mode, gap, maxWidth }) // 运行时改版心
|
|
225
240
|
editor.paperOptions() // 读当前版心配置
|
|
241
|
+
editor.setHeight(600) // 运行时固定高度;setHeight(0) / setHeight('auto') 回到自适应
|
|
242
|
+
editor.heightOptions() // { height: 0, auto: true }
|
|
226
243
|
```
|
|
227
244
|
|
|
228
245
|
#### 示例
|
|
@@ -242,6 +259,45 @@ KEditor.create('#editor', {
|
|
|
242
259
|
})
|
|
243
260
|
```
|
|
244
261
|
|
|
262
|
+
### 代码块(语言 + 语法高亮)
|
|
263
|
+
|
|
264
|
+
内置 20 种语言(`plaintext` `javascript` `typescript` `html` `css` `json` `python` `java` `go` `rust` `c` `csharp` `php` `ruby` `sql` `bash` `yaml` `ini` `markdown` `diff`),并支持别名(`js` `ts` `py` `golang` `yml` `sh` `md` `c++` `cs` `mysql` `toml` …)。
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
// 插入(lang 可省,用 options.code.defaultLanguage)
|
|
268
|
+
editor.insertCode('const a = 1', 'js') // => <pre data-k-lang="javascript"><code class="language-javascript">…</code></pre>
|
|
269
|
+
|
|
270
|
+
// 改写光标所在块的语言(返回生效语言;不在代码块内返回 null)
|
|
271
|
+
editor.setCodeLanguage('sql')
|
|
272
|
+
editor.getCodeLanguage() // 'sql';不在块内为 ''
|
|
273
|
+
|
|
274
|
+
editor.codeLanguages() // [{ value, label, hint }, …](受 options.code.languages 裁剪)
|
|
275
|
+
editor.highlightCode() // 手动重跑全量高亮,返回处理的代码块数量
|
|
276
|
+
editor.lastCodeLang // 最近一次选择的语言(下次插入默认沿用)
|
|
277
|
+
editor.getState().inCode // 光标是否在代码块内
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
标记与产物约定:
|
|
281
|
+
|
|
282
|
+
- 语言同时写在 `<pre data-k-lang="javascript">` 与 `<code class="language-javascript">`;外部 HTML 只带 `class="language-xxx"` 也能识别并补上标记
|
|
283
|
+
- 块右上角显示语言角标(`data-k-lang`,`plaintext` 不显示),纯 CSS 实现,不进入 `getHTML()`
|
|
284
|
+
- **高亮只在编辑器/预览里显示**:`getHTML()`、源码模式、复制出去的 HTML 都是不含 token `<span>` 的干净代码,重新导入会自动重新高亮;预览面板走 `getHTML({ clean: 'preview' })`(同样清理内部属性,但保留高亮)
|
|
285
|
+
- 在块内打字会(防抖 140ms)重新高亮并保持块内光标;输入法组字期间跳过;单块超过 60 KB 退化为纯文本
|
|
286
|
+
- 样式类名 `ke-tok-{kw,str,com,num,typ,fn,op,var,tag,attr,sel,prop,key,bool,meta,ins,del}`,作用域限定在 `.ke-content` 内,宿主可覆盖配色
|
|
287
|
+
|
|
288
|
+
### 导入 Markdown
|
|
289
|
+
|
|
290
|
+
尾栏「导入MD」按钮打开导入面板(选文件 / 粘文本 + 实时预览 + 结构统计 + 三种落点);把 `.md` / `.markdown` / `.txt` 文件拖进正文也会自动转换(默认替换全文)。转换产物就是正文规范里的普通 HTML,围栅代码块会带上 `data-k-lang` 并高亮。
|
|
291
|
+
|
|
292
|
+
```js
|
|
293
|
+
// 纯函数(不依赖实例)
|
|
294
|
+
markdownToHtml('# 标题\n\n正文', { gfm: true }) // => '<h1>标题</h1>\n<p>正文</p>'
|
|
295
|
+
markdownSummary(html) // => { headings, paragraphs, lists, items, codeBlocks, quotes, tables, images, links }
|
|
296
|
+
isMarkdownFile({ name: 'a.md', type: '' }) // => true(按 MIME + 扩展名,.txt 也算)
|
|
297
|
+
readMarkdownFile(file) // => 文本(严格 UTF-8 失败时回退 gb18030 / gbk / big5)
|
|
298
|
+
normalizeInsertMode('append') // => 'append'(非法值 => 'replace')
|
|
299
|
+
```
|
|
300
|
+
|
|
245
301
|
### 回调
|
|
246
302
|
|
|
247
303
|
| 回调 | 签名 | 说明 |
|
|
@@ -262,14 +318,16 @@ editor.root // contenteditable 节点
|
|
|
262
318
|
editor.options // 合并后的配置
|
|
263
319
|
editor.commands // 指令集(可程序化调用)
|
|
264
320
|
editor.media // 上传/媒体管理
|
|
265
|
-
editor.uploader //
|
|
321
|
+
editor.uploader // UploadPipeline:按 upload.mode / 体积自动选路(.pick(file) 可查询)
|
|
322
|
+
editor.uploaders // { normal: SimpleUploader, chunk: ChunkUploader | null }
|
|
266
323
|
editor.history // 撤销栈
|
|
267
324
|
editor.readOnly / sourceMode / previewMode / fullscreen
|
|
325
|
+
editor.lastCodeLang // 最近选择的代码块语言
|
|
268
326
|
```
|
|
269
327
|
|
|
270
328
|
| 方法 | 说明 |
|
|
271
329
|
|------|------|
|
|
272
|
-
| `getHTML({clean?})` | 取当前 HTML
|
|
330
|
+
| `getHTML({clean?})` | 取当前 HTML,默认剔除编辑器内部属性、未完成占位块;`clean: false` 取原始 HTML(调试用),`clean: 'preview'` 保留代码高亮 |
|
|
273
331
|
| `getText()` | 纯文本 |
|
|
274
332
|
| `getJSON()` | `{html, text, counts}` |
|
|
275
333
|
| `setContent(html, {sanitize?, silent?})` | 覆盖内容 |
|
|
@@ -279,6 +337,15 @@ editor.readOnly / sourceMode / previewMode / fullscreen
|
|
|
279
337
|
| `setReadOnly(bool)` | 切换只读 |
|
|
280
338
|
| `undo()` / `redo()` | 撤销/重做(含光标位置恢复) |
|
|
281
339
|
| `insertImage(url, alt?)` / `insertVideo(url, label?)` | 按地址插入媒体(等同工具栏「网络链接」页签) |
|
|
340
|
+
| `insertCode(code?, lang?)` | 插入代码块,光标落在块内(`lang` 省则用 `options.code.defaultLanguage`) |
|
|
341
|
+
| `setCodeLanguage(lang, target?)` | 改写代码块语言并重新高亮(`target` 省则取光标所在块);返回生效语言,未命中返回 `null` |
|
|
342
|
+
| `getCodeLanguage()` | 光标所在代码块的语言,不在块内返回 `''` |
|
|
343
|
+
| `codeLanguages()` | 语言清单 `[{value,label,hint}]`(受 `options.code.languages` 裁剪) |
|
|
344
|
+
| `highlightCode()` | 手动重跑正文全量高亮,返回处理的代码块数量 |
|
|
345
|
+
| `importMarkdown(source, {mode?, silent?})` | 导入 Markdown(`source` 可为文本 / `File` / `{name,content}`)并转为 HTML,返回 `{ok, html, summary, mode, chars, name}`;失败为 `{ok:false, reason:'readonly'\|'error'}` |
|
|
346
|
+
| `setMarkdown(source)` / `insertMarkdown(source)` / `appendMarkdown(source)` | 导入并分别替换全文 / 插入光标处 / 追加文末(等价于 `importMarkdown` + 指定 `mode`),均可 `await` |
|
|
347
|
+
| `markdownOptions()` | 当前导入配置 `{insertMode, maxSize, auto}` |
|
|
348
|
+
| `moveCaretToEnd()` | 光标 / 选区移到正文末尾(追加导入前的落点) |
|
|
282
349
|
| `upload(file, kind?)` | 手动触发一次上传(自动判断图片/视频) |
|
|
283
350
|
| `cancelUploads()` | 取消全部进行中任务 |
|
|
284
351
|
| `counts()` | `{chars, words, images, videos, uploading}` |
|
|
@@ -287,6 +354,7 @@ editor.readOnly / sourceMode / previewMode / fullscreen
|
|
|
287
354
|
| `toggleFullscreen()` | 全屏编辑 |
|
|
288
355
|
| `addButton(spec, place?)` / `moveButton(name, where)` / `removeButton(name)` | 工具栏按钮增 / 移 / 删(详见自定义工具栏按钮) |
|
|
289
356
|
| `setPaper({width?, mode?, gap?, maxWidth?})` / `paperOptions()` | 运行时改版心(宽屏留白策略) |
|
|
357
|
+
| `setHeight(px \| 0 \| 'auto')` / `heightOptions()` | 运行时切换内容区高度(固定 / 自适应) |
|
|
290
358
|
| `on(type, fn)` / `off(type, fn)` | 事件订阅,返回取消函数 |
|
|
291
359
|
| `destroy()` | 卸载实例、清理监听与临时 URL |
|
|
292
360
|
|
|
@@ -306,6 +374,7 @@ editor.readOnly / sourceMode / previewMode / fullscreen
|
|
|
306
374
|
| `sourcechange` | `{mode: 'source'\|'visual'}` | 源码模式切换 |
|
|
307
375
|
| `customselect` | `{name, item, link, action, editor}` | 自定义下拉按钮选中一项;`action` 为 `'link'\|'html'\|'text'\|'open'\|'none'` |
|
|
308
376
|
| `customclick` | `{name, editor}` | `type:'button'` 自定义按钮被点击 |
|
|
377
|
+
| `markdownimport` | `{mode, name, summary, chars}` | Markdown 导入成功(`mode` 为实际落点,`name` 为文件名/空串) |
|
|
309
378
|
| `error` | `{type, error, message}` | 内部异常(如自定义按钮 `onSelect` 抛错),同时走 `onError` |
|
|
310
379
|
|
|
311
380
|
---
|
|
@@ -323,7 +392,9 @@ editor.commands.toggleBlockquote()
|
|
|
323
392
|
editor.commands.createLink({ url, text, target })
|
|
324
393
|
editor.commands.removeLink()
|
|
325
394
|
editor.commands.insertTable(3, 2) // 列, 行
|
|
326
|
-
editor.commands.insertCodeBlock('const a = 1')
|
|
395
|
+
editor.commands.insertCodeBlock('const a = 1', 'js') // 第二参为语言(可省)
|
|
396
|
+
editor.commands.setCodeLanguage('python') // 改写光标所在块语言
|
|
397
|
+
editor.commands.currentCodeLang() // 光标所在块语言(''=不在块内)
|
|
327
398
|
editor.commands.insertDivider()
|
|
328
399
|
editor.commands.insertHTML('<p>富文本片段</p>')
|
|
329
400
|
editor.commands.clearFormat()
|
|
@@ -334,24 +405,46 @@ editor.getState() // 当前选区状态(工具栏高亮用
|
|
|
334
405
|
|
|
335
406
|
```js
|
|
336
407
|
import { sanitizeHtml, formatHtml, escapeHtml, isSafeUrl, cleanWordHtml, mockAdapter, httpAdapter, simpleAdapter, ChunkUploader } from 'kyle-editor'
|
|
408
|
+
import { highlightCode, listLanguages, normalizeLang, languageLabel, isCodeBlock, LANGUAGES } from 'kyle-editor'
|
|
337
409
|
|
|
338
410
|
sanitizeHtml(dirtyHtml) // 白名单净化,返回安全 HTML
|
|
339
411
|
formatHtml(html) // 缩进美化(编辑器源码模式同款)
|
|
340
412
|
cleanWordHtml(wordHtml) // 粘贴前的 Word/WPS 清洗
|
|
341
413
|
isSafeUrl('javascript:1') // => false
|
|
414
|
+
|
|
415
|
+
highlightCode('const a = 1', 'js') // => '<span class="ke-tok-kw">const</span> a <span class="ke-tok-op">=</span> …'(已转义,可直接 innerHTML)
|
|
416
|
+
listLanguages(['js', 'mysql']) // => [{ value:'javascript', … }, { value:'sql', … }]
|
|
417
|
+
normalizeLang('MySQL') // => 'sql'(未知语言 => 'plaintext')
|
|
418
|
+
languageLabel('js') // => 'JavaScript'
|
|
419
|
+
isCodeBlock(preElement) // => true(<pre> 且带 data-k-lang 或内含 <code>)
|
|
420
|
+
|
|
421
|
+
markdownToHtml(md, options) // Markdown → 正文规范 HTML(默认再过一遍白名单净化)
|
|
422
|
+
markdownSummary(html) // 统计标题/段落/列表/代码块/引用/表格/图片/链接数量
|
|
423
|
+
isMarkdownFile(file) // 按 MIME + 扩展名判断(.md/.markdown/.txt)
|
|
424
|
+
readMarkdownFile(file) // 读文件文本(UTF-8 → gb18030/gbk/big5 回退)
|
|
425
|
+
normalizeInsertMode('cursor') // => 'cursor'(非法值 => 'replace')
|
|
426
|
+
MARKDOWN_ACCEPT // <input accept> 用:'.md,.markdown,.txt,text/markdown,text/plain'
|
|
342
427
|
```
|
|
343
428
|
|
|
344
429
|
UMD / 默认导出上也挂了一批静态成员,`<script>` 引入时直接可用:
|
|
345
430
|
|
|
346
431
|
```js
|
|
347
|
-
KEditor.version // '1.0.
|
|
432
|
+
KEditor.version // '1.0.7'
|
|
348
433
|
KEditor.sanitizeHtml(html) // 白名单净化
|
|
349
434
|
KEditor.formatHtml(html) // 源码美化
|
|
350
435
|
KEditor.cleanWordHtml(html) // Word 清洗
|
|
351
|
-
KEditor.mockAdapter({speed}) //
|
|
352
|
-
KEditor.httpAdapter(config) //
|
|
353
|
-
KEditor.simpleAdapter(fn) //
|
|
354
|
-
KEditor.
|
|
436
|
+
KEditor.mockAdapter({speed}) // 内置模拟适配器(分片 put() + 整包 upload() 都支持)
|
|
437
|
+
KEditor.httpAdapter(config) // 真实后端适配器(只认一个 url,两条链路共用)
|
|
438
|
+
KEditor.simpleAdapter(fn) // 把「一个文件一次请求」的函数包成普通上传适配器
|
|
439
|
+
KEditor.createSimpleUploader(option, uploadOptions) // 造一个 SimpleUploader(普通上传)
|
|
440
|
+
KEditor.createChunkUploader(option, uploadOptions) // 造一个 ChunkUploader(分片上传)
|
|
441
|
+
KEditor.SimpleUploader / KEditor.ChunkUploader / KEditor.UploadPipeline // 对应类
|
|
442
|
+
KEditor.highlightCode(code, lang) // 代码高亮(纯字符串进、HTML 片段出)
|
|
443
|
+
KEditor.listLanguages(only?) // 语言清单,可按主名/别名裁剪
|
|
444
|
+
KEditor.normalizeLang / KEditor.languageLabel / KEditor.isCodeBlock
|
|
445
|
+
KEditor.LANGUAGES // 全部内置语言(value/label/aliases/hint)
|
|
446
|
+
KEditor.markdownToHtml / KEditor.markdownSummary
|
|
447
|
+
KEditor.isMarkdownFile / KEditor.readMarkdownFile / KEditor.normalizeInsertMode / KEditor.MARKDOWN_ACCEPT
|
|
355
448
|
KEditor.utils // { uid, escapeHtml }
|
|
356
449
|
KEditor.create(el, options) // 工厂方法,等价于 new KEditor(...)
|
|
357
450
|
KEditor.plugin // Vue 3 插件(仅 keditor-vue.umd.js)
|
package/docs/integration.md
CHANGED
|
@@ -24,12 +24,12 @@ KEditor 是**纯前端**编辑器,不依赖任何后端接口。三种集成
|
|
|
24
24
|
<body>
|
|
25
25
|
<div id="editor"></div>
|
|
26
26
|
|
|
27
|
-
<!-- 线上用压缩版(
|
|
27
|
+
<!-- 线上用压缩版(192 KB / gzip 61 KB);断点调试换成 dist/keditor.umd.js 即可,API 完全一致 -->
|
|
28
28
|
<script src="dist/keditor.umd.min.js"></script>
|
|
29
29
|
<script>
|
|
30
30
|
const editor = KEditor.create('#editor', {
|
|
31
31
|
placeholder: '开始撰写内容…',
|
|
32
|
-
height: 460
|
|
32
|
+
// 高度默认随内容自动撑开;要固定高度 + 内部滚动就加上 height: 460
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
editor.on('change', ({ html }) => console.log(html))
|
|
@@ -67,8 +67,9 @@ import { KEditor, create, mockAdapter } from 'kyle-editor'
|
|
|
67
67
|
|
|
68
68
|
const editor = create('#editor', {
|
|
69
69
|
value: '<p>hello</p>',
|
|
70
|
-
uploader: mockAdapter({ speed: 8 }), //
|
|
71
|
-
|
|
70
|
+
uploader: mockAdapter({ speed: 8 }), // 演示:不请求后端(普通上传)
|
|
71
|
+
chunkUploader: mockAdapter({ speed: 8 }), // 演示:分片上传
|
|
72
|
+
upload: { mode: 'auto', chunkSize: 256 * 1024, concurrency: 3 },
|
|
72
73
|
})
|
|
73
74
|
|
|
74
75
|
// 组件卸载时务必销毁
|
|
@@ -134,9 +135,11 @@ const form = ref({ content: '<p>初始内容</p>' })
|
|
|
134
135
|
const readonly = ref(false)
|
|
135
136
|
const editorOptions = {
|
|
136
137
|
placeholder: '支持拖拽 / 粘贴上传…',
|
|
137
|
-
height: 420,
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
height: 420, // 不写则随内容自动撑开
|
|
139
|
+
// 上传分两条链路,各自配置;小文件走普通上传,大文件自动切片
|
|
140
|
+
uploader: '/api/upload', // 普通上传:整个文件一次请求
|
|
141
|
+
chunkUploader: '/api/chunk-upload', // 分片上传:响应里 url 非空即成功
|
|
142
|
+
upload: { mode: 'auto', chunkThreshold: 5 * 1024 * 1024, chunkSize: 1024 * 1024, concurrency: 3, retries: 2 },
|
|
140
143
|
}
|
|
141
144
|
const onUpload = (payload) => console.log(payload.stage, payload.percent)
|
|
142
145
|
const onReady = (editor) => console.log('编辑器实例', editor)
|
|
@@ -181,7 +184,7 @@ const { host, editor } = useKEditor({ height: 400 })
|
|
|
181
184
|
| `modelValue` | `string` | HTML 字符串,`v-model` 绑定;外部变更会同步进编辑器 |
|
|
182
185
|
| `options` | `object` | 与 `new KEditor(el, options)` 完全一致的配置 |
|
|
183
186
|
| `disabled` | `boolean` | 只读模式 |
|
|
184
|
-
| `height` | `number` | 覆盖 `options.height
|
|
187
|
+
| `height` | `number` | 覆盖 `options.height`(默认自适应内容高度) |
|
|
185
188
|
|
|
186
189
|
| Emit | 载荷 | 说明 |
|
|
187
190
|
|------|------|------|
|
|
@@ -250,9 +253,10 @@ export default function Page() {
|
|
|
250
253
|
import KEditor, { type KEditorOptions, type UploadAdapter, type UploadResult } from 'kyle-editor'
|
|
251
254
|
|
|
252
255
|
const options: KEditorOptions = {
|
|
253
|
-
height: 480,
|
|
254
|
-
upload: { chunkSize: 2 * 1024 * 1024, concurrency: 4 },
|
|
255
|
-
uploader: myAdapter, //
|
|
256
|
+
height: 480, // 0 / 不写 = 随内容自动撑开
|
|
257
|
+
upload: { mode: 'auto', chunkSize: 2 * 1024 * 1024, concurrency: 4 },
|
|
258
|
+
uploader: myAdapter, // 普通上传(满足 upload() 即可)
|
|
259
|
+
chunkUploader: myChunkAdapter, // 分片上传(满足 put() 即可)
|
|
256
260
|
}
|
|
257
261
|
|
|
258
262
|
const editor = KEditor.create('#editor', options)
|
|
@@ -269,10 +273,16 @@ UMD 方式(无构建)下若需提示,可 `/// <reference types="kyle-edito
|
|
|
269
273
|
## 4. 常见问题
|
|
270
274
|
|
|
271
275
|
**Q:不需要后端,怎么让图片真的显示出来?**
|
|
272
|
-
默认 `uploader
|
|
276
|
+
默认 `uploader` / `chunkUploader` 都是 `'mock'`,在浏览器内完成传输并生成 `blob:` 地址,页面内可直接预览。保存草稿时把 `getHTML()` 存下来即可;正式上线时把两者换成后端接口地址(如 `uploader: '/api/upload'`、`chunkUploader: '/api/chunk-upload'`),接口返回 `url` 就会得到真实(CDN)地址。
|
|
277
|
+
|
|
278
|
+
**Q:只想配一个上传接口,不要分片?**
|
|
279
|
+
只设 `uploader` 即可 —— 分片链路自动不启用(不会用默认 mock 顶上),所有文件都走整包一次请求。
|
|
280
|
+
|
|
281
|
+
**Q:想让所有文件都走分片上传?**
|
|
282
|
+
只设 `chunkUploader` 即可(`upload.mode` 自动为 `'chunk'`);想显式写清楚就加 `upload: { mode: 'chunk' }`。
|
|
273
283
|
|
|
274
284
|
**Q:`blob:` 地址提交到服务器会失效,怎么办?**
|
|
275
|
-
在提交前调用 `editor.getHTML()`,把 `data-k-url` 为 `blob:` 的资源先上传(`beforeUpload`
|
|
285
|
+
在提交前调用 `editor.getHTML()`,把 `data-k-url` 为 `blob:` 的资源先上传(`beforeUpload` 里做映射),或直接配置真实上传接口。
|
|
276
286
|
|
|
277
287
|
**Q:如何精简工具栏?**
|
|
278
288
|
```js
|
|
@@ -281,7 +291,7 @@ KEditor.create('#editor', { toolbar: ['undo', 'redo', '|', 'bold', 'image', 'vid
|
|
|
281
291
|
|
|
282
292
|
`'|'` 为分隔符,`':tail'` 之后的按钮靠右对齐(窄屏下吸附在右端不会被滚走),`toolbar: false` 隐藏工具栏。工具栏放不下时可左右滑动,并有滚动条、边缘渐隐、首次引导气泡等提示,详见 [api.md](api.md#工具栏项)。
|
|
283
293
|
|
|
284
|
-
可用项:`undo` `redo` `heading` `fontsize` `bold` `italic` `underline` `strike` `foreColor` `backColor` `alignLeft` `alignCenter` `alignRight` `alignJustify` `ul` `ol` `quote` `link` `image` `video` `table` `code` `hr` `clear` `source` `preview` `fullscreen`,详见 [api.md](api.md#工具栏项)。
|
|
294
|
+
可用项:`undo` `redo` `heading` `fontsize` `bold` `italic` `underline` `strike` `foreColor` `backColor` `alignLeft` `alignCenter` `alignRight` `alignJustify` `ul` `ol` `quote` `link` `image` `video` `table` `code` `markdown` `hr` `clear` `source` `preview` `fullscreen`,详见 [api.md](api.md#工具栏项)。
|
|
285
295
|
|
|
286
296
|
> `preview` 已从默认工具栏(`KEditor.DEFAULT_TOOLBAR`)里移除,需要它在 `toolbar` 数组里写回,或直接调 `editor.openPreview()`。自定义按钮可用 `position` / `editor.moveButton()` 放到任意位置,包括 `:tail` 吸附区。
|
|
287
297
|
|