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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kyle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,195 @@
|
|
|
1
|
-
#
|
|
1
|
+
# KEditor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
轻量、美观的**微信公众号风格富文本编辑器**。纯前端实现,无任何后端依赖:图片/视频走**分片上传**(并发、重试、取消、秒传、断点续传),随时切换 **HTML 源码模式**查看或改写产物。支持 `<script>` 直接引入与 Vue 3 安装集成。
|
|
4
|
+
|
|
5
|
+
- 体积:`dist/keditor.umd.js` 约 139 KB(gzip 37 KB),零运行时依赖,样式随 JS 注入
|
|
6
|
+
- 版式:677px 正文版心、17px/1.75 行距,与公众号观感一致
|
|
7
|
+
- 安全:标签/属性白名单净化,拦截 `on*`、`javascript:`、`script/iframe`
|
|
8
|
+
|
|
9
|
+
## 功能
|
|
10
|
+
|
|
11
|
+
| 分类 | 能力 |
|
|
12
|
+
|------|------|
|
|
13
|
+
| 排版 | 段落样式面板(正文/标题一/标题二/标题三,带真实字号预览与用途说明)、字号面板(12–32px)、字体颜色与背景高亮(主题色 + 标准色 + 最近使用 + 自定义取色)、粗斜下删、四种对齐、有序/无序列表、引用、表格(网格选择)、代码块、分割线、链接、清除格式 |
|
|
14
|
+
| 媒体 | 图片/视频**本地上传**(点击、拖拽、粘贴截图)或**直接填网络链接**插入(自动校验可访问性);分片进度与速度、取消、失败重试、多图多选、大图自动压缩、双击看原图、气泡工具条(对齐/宽度/圆角/替换/删除)、**选中图片后拖拽右下角把手实时缩放** |
|
|
15
|
+
| 上传 | `chunkSize / concurrency / retries / fingerprint` 可配;内置 mock、http、simple 适配器,也可自定义 `init/put/complete` |
|
|
16
|
+
| 源码 | 工具栏右侧常驻「源码」按钮(`⌘/Ctrl+Shift+K`),进入后正文按钮置灰;格式化缩进、复制、直接编辑并应用(回填自动净化) |
|
|
17
|
+
| 其他 | 实时字数/词数/图片视频统计、预览(手机/平板/电脑屏宽)、全屏编辑、撤销重做(保留光标位置)、只读、窄屏工具栏可左右滑动(常显滚动条 + 边缘渐隐 + 首次引导气泡 + 鼠标拖拽平移)且「源码/预览/全屏」固定右端 |
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install kyle-editor
|
|
23
|
+
# 使用 Vue 组件的项目
|
|
24
|
+
npm install kyle-editor vue@^3
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
或直接用 CDN(无需安装):
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src="https://unpkg.com/kyle-editor/dist/keditor.umd.js"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
> 零运行时依赖;样式随 JS 注入,**不需要额外引入 CSS**。Vue 是可选 peerDependency,只用核心 API 的项目不会被强制安装。
|
|
34
|
+
|
|
35
|
+
## 快速开始
|
|
36
|
+
|
|
37
|
+
### 方式一:`<script>` 引入(零构建)
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<div id="editor"></div>
|
|
41
|
+
<script src="https://unpkg.com/kyle-editor/dist/keditor.umd.js"></script>
|
|
42
|
+
<!-- 本地开发时也可用相对路径:<script src="dist/keditor.umd.js"></script> -->
|
|
43
|
+
<script>
|
|
44
|
+
const editor = KEditor.create('#editor', {
|
|
45
|
+
placeholder: '开始撰写内容,支持拖拽 / 粘贴图片与视频…',
|
|
46
|
+
height: 460,
|
|
47
|
+
})
|
|
48
|
+
editor.on('change', ({ html }) => console.log(html))
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 方式二:ESM / 打包器
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import KEditor from 'kyle-editor'
|
|
56
|
+
|
|
57
|
+
const editor = KEditor.create('#editor', { height: 460 })
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 方式三:Vue 3 安装集成
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
// main.js
|
|
64
|
+
import { createApp } from 'vue'
|
|
65
|
+
import KEditorVue from 'kyle-editor/vue'
|
|
66
|
+
|
|
67
|
+
createApp(App).use(KEditorVue).mount('#app')
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```vue
|
|
71
|
+
<template>
|
|
72
|
+
<k-editor v-model="form.content" :options="options" :disabled="saving" @upload="onUpload" />
|
|
73
|
+
</template>
|
|
74
|
+
|
|
75
|
+
<script setup>
|
|
76
|
+
import { ref } from 'vue'
|
|
77
|
+
|
|
78
|
+
const form = ref({ content: '' })
|
|
79
|
+
const options = {
|
|
80
|
+
height: 420,
|
|
81
|
+
// 无后端时保持默认 'mock';上线时换成下面这行即可
|
|
82
|
+
uploader: { type: 'http', initUrl: '/api/upload/init', chunkUrl: '/api/upload/chunk', completeUrl: '/api/upload/complete' },
|
|
83
|
+
upload: { chunkSize: 5 * 1024 * 1024, concurrency: 4 },
|
|
84
|
+
}
|
|
85
|
+
const onUpload = (payload) => console.log(payload.stage, payload.percent)
|
|
86
|
+
</script>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
> `kyle-editor/vue` 的默认导出是**组件本身(自带 `install`)**,所以上面两种写法等价:
|
|
90
|
+
> `app.use(KEditorVue)` 后模板里用全局标签 `<k-editor>`;也可以 `import KEditorVue from 'kyle-editor/vue'` 直接当组件用 `<KEditorVue v-model="html" />`。
|
|
91
|
+
> 只想显式拿插件时用 `import { KEditorPlugin } from 'kyle-editor/vue'`。
|
|
92
|
+
>
|
|
93
|
+
> ESM / Webpack / Vite / React 用法见 [docs/integration.md](docs/integration.md)。
|
|
94
|
+
|
|
95
|
+
## 本地运行
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm install
|
|
99
|
+
npm run build # 产出 dist/(es + cjs + umd,含 Vue 版本)
|
|
100
|
+
npm run dev # 静态服务 http://127.0.0.1:5178
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
打开:
|
|
104
|
+
|
|
105
|
+
- `index.html` — 说明首页
|
|
106
|
+
- `demo/script.html` — UMD 引入 + 分片日志面板
|
|
107
|
+
- `demo/esm.html` — 自定义上传适配器演示
|
|
108
|
+
- `demo/vue.html` — Vue 3 组件 + `v-model`
|
|
109
|
+
- `test/smoke.html` — 40 条内置冒烟用例(可视化结果)
|
|
110
|
+
|
|
111
|
+
## 验证
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
npm run build
|
|
115
|
+
npm run dev & # 启动静态服务
|
|
116
|
+
node test/run-headless.mjs # 冒烟用例(需本机 Chrome)
|
|
117
|
+
node test/check-page.mjs # 三个 demo 页面健康检查
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
当前状态:`40/40` 用例通过,demo 页面无控制台报错。
|
|
121
|
+
|
|
122
|
+
## 文档
|
|
123
|
+
|
|
124
|
+
| 文档 | 内容 |
|
|
125
|
+
|------|------|
|
|
126
|
+
| [docs/integration.md](docs/integration.md) | Script / ESM / Vue3 / React / Nuxt 集成,常见问题 |
|
|
127
|
+
| [docs/api.md](docs/api.md) | 全部配置项、实例方法与事件、工具栏自定义 |
|
|
128
|
+
| [docs/upload-adapter.md](docs/upload-adapter.md) | 分片上传流程、后端接口约定、自定义适配器 |
|
|
129
|
+
|
|
130
|
+
## 目录结构
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
src/
|
|
134
|
+
├── index.js # 入口(导出 create/KEditor/适配器/工具函数)
|
|
135
|
+
├── core/ # 编辑器内核
|
|
136
|
+
│ ├── editor.js # 组装 DOM、事件、状态、对外 API
|
|
137
|
+
│ ├── toolbar.js # 工具栏声明式配置
|
|
138
|
+
│ ├── commands.js # 富文本指令(字号/颜色/对齐/列表/表格/链接…)
|
|
139
|
+
│ ├── sanitize.js # HTML 白名单净化 + Word 粘贴清洗
|
|
140
|
+
│ ├── paste.js # 粘贴/拖拽(截图直传、外链图片本地化)
|
|
141
|
+
│ ├── history.js # 快照式撤销栈(保留光标)
|
|
142
|
+
│ ├── selection.js # 选区与光标工具
|
|
143
|
+
│ ├── format.js # HTML 美化输出
|
|
144
|
+
│ └── icons.js # 内置 SVG 图标
|
|
145
|
+
├── upload/ # 分片上传
|
|
146
|
+
│ ├── chunk-uploader.js # 切片、并发、重试、进度、取消
|
|
147
|
+
│ ├── adapters.js # mock / http / simple 适配器
|
|
148
|
+
│ └── fingerprint.js # 秒传与断点续传指纹
|
|
149
|
+
├── media/media.js # 图片视频插入、占位块、压缩、替换
|
|
150
|
+
├── ui/ # 样式注入、弹层、源码面板、预览、气泡工具条、toast
|
|
151
|
+
└── vue/index.js # Vue 3 插件 + 组件 + useKEditor
|
|
152
|
+
|
|
153
|
+
types/ # 手写 TypeScript 声明(核心 + Vue)
|
|
154
|
+
docs/ # 集成文档(index / integration / api / upload-adapter / npm-publish)
|
|
155
|
+
demo/ # script / esm / vue 三种集成示例页
|
|
156
|
+
test/ # 无头冒烟用例、页面健康检查、发布配置校验、视觉截图
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 开发与验证
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
python3 -m http.server 5178 --bind 127.0.0.1 # 或 npm run dev
|
|
163
|
+
|
|
164
|
+
npm run build # 产出 dist/:keditor.{es.js,cjs,umd.js} + keditor-vue.{es.js,cjs,umd.js}
|
|
165
|
+
npm test # 无头 Chrome 跑 66 项功能冒烟(test/smoke.html)
|
|
166
|
+
npm run test:pages # 4 个 demo 页面健康检查(挂载/控制台错误)
|
|
167
|
+
npm run test:types # .d.ts 语法校验(无需 tsc)
|
|
168
|
+
npm run test:package # 63 项发布配置校验(包名/exports/.cjs 后缀/裸说明符)
|
|
169
|
+
npm run verify # 上述全部一次跑完
|
|
170
|
+
npm run test:visual # 9 个关键界面截图到 test/screenshots/
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
当前状态:构建通过,**86/86 冒烟用例通过**,4 个 demo 页面零控制台错误,63 项发布配置校验通过。覆盖范围包括:输入与统计、全部工具栏指令、列表嵌套、表格与行列增删、颜色面板(选色/最近使用/自定义)、段落样式面板、字号面板、图片与视频的网络链接插入、链接校验、图片拖拽缩放(把手定位/拖拽改变宽度/取消选中隐藏)、图片对齐(缩小后工具栏与气泡两条入口均居中生效且按钮高亮)、粘贴净化(含 XSS 拦截)、拖拽上传、分片进度与取消、历史撤销重做、源码模式双向同步与按钮态、预览、只读、Vue 插件与 v-model、UMD 全局挂载。
|
|
174
|
+
|
|
175
|
+
## 发布到 npm
|
|
176
|
+
|
|
177
|
+
包名 `kyle-editor`(`keditor` 已被占用)。发布配置、本地验证、版本管理、私有源与 CI 自动发布全流程见 **[docs/npm-publish.md](docs/npm-publish.md)**。
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
npm login # 登录(浏览器授权)
|
|
181
|
+
npm run release # 构建 + 全量验证 + npm publish --access public
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
已实测:从 tarball 全新安装后,CJS / ESM × 核心 / `kyle-editor/vue` 四个入口均能正确解析并拿到完整导出。
|
|
185
|
+
|
|
186
|
+
## 已知限制
|
|
187
|
+
|
|
188
|
+
- 依赖浏览器 `contenteditable` + `document.execCommand`(主流浏览器均支持,Chrome/Safari/Edge/Firefox 已验证);不兼容 IE。
|
|
189
|
+
- 表格支持插入与行列增删,未内置单元格合并;需要时可自行扩展 `commands.js`。
|
|
190
|
+
- 默认适配器产生的 `blob:` 地址仅当前会话有效,正式环境请配置真实 `uploader`(见文档)。
|
|
191
|
+
- 强指纹(`fingerprint: 'strong'`)依赖 `crypto.subtle`,需 HTTPS 或 localhost 环境。
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const vue = require("vue");
|
|
4
|
+
const kyleEditor = require("kyle-editor");
|
|
5
|
+
const KEditorVue = {
|
|
6
|
+
name: "KEditor",
|
|
7
|
+
props: {
|
|
8
|
+
modelValue: { type: String, default: "" },
|
|
9
|
+
options: { type: Object, default: () => ({}) },
|
|
10
|
+
disabled: { type: Boolean, default: false },
|
|
11
|
+
height: { type: Number, default: null }
|
|
12
|
+
},
|
|
13
|
+
emits: ["update:modelValue", "ready", "change", "upload", "error", "save", "blur", "focus"],
|
|
14
|
+
setup(props, { emit, expose }) {
|
|
15
|
+
const host = vue.ref(null);
|
|
16
|
+
const instance = vue.shallowRef(null);
|
|
17
|
+
const buildOptions = () => {
|
|
18
|
+
const opts = { ...kyleEditor.defaults, ...props.options, value: props.modelValue || "" };
|
|
19
|
+
if (props.height) opts.height = props.height;
|
|
20
|
+
opts.onChange = (html) => {
|
|
21
|
+
emit("update:modelValue", html);
|
|
22
|
+
emit("change", html);
|
|
23
|
+
};
|
|
24
|
+
opts.onReady = (editor) => emit("ready", editor);
|
|
25
|
+
opts.onError = (error) => emit("error", error);
|
|
26
|
+
opts.onUpload = (payload) => emit("upload", payload);
|
|
27
|
+
return opts;
|
|
28
|
+
};
|
|
29
|
+
expose({
|
|
30
|
+
get editor() {
|
|
31
|
+
return instance.value;
|
|
32
|
+
},
|
|
33
|
+
getHTML: () => {
|
|
34
|
+
var _a;
|
|
35
|
+
return ((_a = instance.value) == null ? void 0 : _a.getHTML()) || "";
|
|
36
|
+
},
|
|
37
|
+
getText: () => {
|
|
38
|
+
var _a;
|
|
39
|
+
return ((_a = instance.value) == null ? void 0 : _a.getText()) || "";
|
|
40
|
+
},
|
|
41
|
+
counts: () => {
|
|
42
|
+
var _a;
|
|
43
|
+
return ((_a = instance.value) == null ? void 0 : _a.counts()) || null;
|
|
44
|
+
},
|
|
45
|
+
focus: () => {
|
|
46
|
+
var _a;
|
|
47
|
+
return (_a = instance.value) == null ? void 0 : _a.focus();
|
|
48
|
+
},
|
|
49
|
+
setContent: (html) => {
|
|
50
|
+
var _a;
|
|
51
|
+
return (_a = instance.value) == null ? void 0 : _a.setContent(html);
|
|
52
|
+
},
|
|
53
|
+
enterSource: () => {
|
|
54
|
+
var _a;
|
|
55
|
+
return (_a = instance.value) == null ? void 0 : _a.enterSource();
|
|
56
|
+
},
|
|
57
|
+
openPreview: () => {
|
|
58
|
+
var _a;
|
|
59
|
+
return (_a = instance.value) == null ? void 0 : _a.openPreview();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
vue.onMounted(() => {
|
|
63
|
+
instance.value = new kyleEditor.KEditor(host.value, buildOptions());
|
|
64
|
+
instance.value.on("save", (payload) => emit("save", payload));
|
|
65
|
+
instance.value.on("blur", () => emit("blur"));
|
|
66
|
+
instance.value.on("focus", () => emit("focus"));
|
|
67
|
+
host.value.__keditor__ = instance.value;
|
|
68
|
+
});
|
|
69
|
+
vue.onBeforeUnmount(() => {
|
|
70
|
+
var _a;
|
|
71
|
+
host.value.__keditor__ = null;
|
|
72
|
+
(_a = instance.value) == null ? void 0 : _a.destroy();
|
|
73
|
+
instance.value = null;
|
|
74
|
+
});
|
|
75
|
+
vue.watch(
|
|
76
|
+
() => props.modelValue,
|
|
77
|
+
(value) => {
|
|
78
|
+
const editor = instance.value;
|
|
79
|
+
if (!editor || editor.sourceMode) return;
|
|
80
|
+
if (value === editor.getHTML()) return;
|
|
81
|
+
editor.setContent(value || "", { silent: true });
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
vue.watch(
|
|
85
|
+
() => props.disabled,
|
|
86
|
+
(value) => {
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = instance.value) == null ? void 0 : _a.setReadOnly(value);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
return () => vue.h("div", { ref: host, class: "ke-vue-host" });
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const KEditorPlugin = {
|
|
95
|
+
install(app, { name = "KEditor" } = {}) {
|
|
96
|
+
app.component(name, KEditorVue);
|
|
97
|
+
app.config.globalProperties.$keditor = { KEditor: kyleEditor.KEditor, defaults: kyleEditor.defaults };
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
function useKEditor(options = {}) {
|
|
101
|
+
const host = vue.ref(null);
|
|
102
|
+
const instance = vue.shallowRef(null);
|
|
103
|
+
vue.onMounted(() => {
|
|
104
|
+
instance.value = new kyleEditor.KEditor(host.value, options);
|
|
105
|
+
});
|
|
106
|
+
vue.onBeforeUnmount(() => {
|
|
107
|
+
var _a;
|
|
108
|
+
return (_a = instance.value) == null ? void 0 : _a.destroy();
|
|
109
|
+
});
|
|
110
|
+
return { host, editor: instance };
|
|
111
|
+
}
|
|
112
|
+
KEditorVue.install = (app, options) => KEditorPlugin.install(app, options);
|
|
113
|
+
Object.defineProperty(exports, "KEditor", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
get: () => kyleEditor.KEditor
|
|
116
|
+
});
|
|
117
|
+
exports.KEditorPlugin = KEditorPlugin;
|
|
118
|
+
exports.KEditorVue = KEditorVue;
|
|
119
|
+
exports.default = KEditorVue;
|
|
120
|
+
exports.useKEditor = useKEditor;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ref, shallowRef, onMounted, onBeforeUnmount, watch, h } from "vue";
|
|
2
|
+
import { KEditor, defaults } from "kyle-editor";
|
|
3
|
+
import { KEditor as KEditor2 } from "kyle-editor";
|
|
4
|
+
const KEditorVue = {
|
|
5
|
+
name: "KEditor",
|
|
6
|
+
props: {
|
|
7
|
+
modelValue: { type: String, default: "" },
|
|
8
|
+
options: { type: Object, default: () => ({}) },
|
|
9
|
+
disabled: { type: Boolean, default: false },
|
|
10
|
+
height: { type: Number, default: null }
|
|
11
|
+
},
|
|
12
|
+
emits: ["update:modelValue", "ready", "change", "upload", "error", "save", "blur", "focus"],
|
|
13
|
+
setup(props, { emit, expose }) {
|
|
14
|
+
const host = ref(null);
|
|
15
|
+
const instance = shallowRef(null);
|
|
16
|
+
const buildOptions = () => {
|
|
17
|
+
const opts = { ...defaults, ...props.options, value: props.modelValue || "" };
|
|
18
|
+
if (props.height) opts.height = props.height;
|
|
19
|
+
opts.onChange = (html) => {
|
|
20
|
+
emit("update:modelValue", html);
|
|
21
|
+
emit("change", html);
|
|
22
|
+
};
|
|
23
|
+
opts.onReady = (editor) => emit("ready", editor);
|
|
24
|
+
opts.onError = (error) => emit("error", error);
|
|
25
|
+
opts.onUpload = (payload) => emit("upload", payload);
|
|
26
|
+
return opts;
|
|
27
|
+
};
|
|
28
|
+
expose({
|
|
29
|
+
get editor() {
|
|
30
|
+
return instance.value;
|
|
31
|
+
},
|
|
32
|
+
getHTML: () => {
|
|
33
|
+
var _a;
|
|
34
|
+
return ((_a = instance.value) == null ? void 0 : _a.getHTML()) || "";
|
|
35
|
+
},
|
|
36
|
+
getText: () => {
|
|
37
|
+
var _a;
|
|
38
|
+
return ((_a = instance.value) == null ? void 0 : _a.getText()) || "";
|
|
39
|
+
},
|
|
40
|
+
counts: () => {
|
|
41
|
+
var _a;
|
|
42
|
+
return ((_a = instance.value) == null ? void 0 : _a.counts()) || null;
|
|
43
|
+
},
|
|
44
|
+
focus: () => {
|
|
45
|
+
var _a;
|
|
46
|
+
return (_a = instance.value) == null ? void 0 : _a.focus();
|
|
47
|
+
},
|
|
48
|
+
setContent: (html) => {
|
|
49
|
+
var _a;
|
|
50
|
+
return (_a = instance.value) == null ? void 0 : _a.setContent(html);
|
|
51
|
+
},
|
|
52
|
+
enterSource: () => {
|
|
53
|
+
var _a;
|
|
54
|
+
return (_a = instance.value) == null ? void 0 : _a.enterSource();
|
|
55
|
+
},
|
|
56
|
+
openPreview: () => {
|
|
57
|
+
var _a;
|
|
58
|
+
return (_a = instance.value) == null ? void 0 : _a.openPreview();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
onMounted(() => {
|
|
62
|
+
instance.value = new KEditor(host.value, buildOptions());
|
|
63
|
+
instance.value.on("save", (payload) => emit("save", payload));
|
|
64
|
+
instance.value.on("blur", () => emit("blur"));
|
|
65
|
+
instance.value.on("focus", () => emit("focus"));
|
|
66
|
+
host.value.__keditor__ = instance.value;
|
|
67
|
+
});
|
|
68
|
+
onBeforeUnmount(() => {
|
|
69
|
+
var _a;
|
|
70
|
+
host.value.__keditor__ = null;
|
|
71
|
+
(_a = instance.value) == null ? void 0 : _a.destroy();
|
|
72
|
+
instance.value = null;
|
|
73
|
+
});
|
|
74
|
+
watch(
|
|
75
|
+
() => props.modelValue,
|
|
76
|
+
(value) => {
|
|
77
|
+
const editor = instance.value;
|
|
78
|
+
if (!editor || editor.sourceMode) return;
|
|
79
|
+
if (value === editor.getHTML()) return;
|
|
80
|
+
editor.setContent(value || "", { silent: true });
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
watch(
|
|
84
|
+
() => props.disabled,
|
|
85
|
+
(value) => {
|
|
86
|
+
var _a;
|
|
87
|
+
return (_a = instance.value) == null ? void 0 : _a.setReadOnly(value);
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
return () => h("div", { ref: host, class: "ke-vue-host" });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const KEditorPlugin = {
|
|
94
|
+
install(app, { name = "KEditor" } = {}) {
|
|
95
|
+
app.component(name, KEditorVue);
|
|
96
|
+
app.config.globalProperties.$keditor = { KEditor, defaults };
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
function useKEditor(options = {}) {
|
|
100
|
+
const host = ref(null);
|
|
101
|
+
const instance = shallowRef(null);
|
|
102
|
+
onMounted(() => {
|
|
103
|
+
instance.value = new KEditor(host.value, options);
|
|
104
|
+
});
|
|
105
|
+
onBeforeUnmount(() => {
|
|
106
|
+
var _a;
|
|
107
|
+
return (_a = instance.value) == null ? void 0 : _a.destroy();
|
|
108
|
+
});
|
|
109
|
+
return { host, editor: instance };
|
|
110
|
+
}
|
|
111
|
+
KEditorVue.install = (app, options) => KEditorPlugin.install(app, options);
|
|
112
|
+
export {
|
|
113
|
+
KEditor2 as KEditor,
|
|
114
|
+
KEditorPlugin,
|
|
115
|
+
KEditorVue,
|
|
116
|
+
KEditorVue as default,
|
|
117
|
+
useKEditor
|
|
118
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("vue"), require("kyle-editor")) : typeof define === "function" && define.amd ? define(["exports", "vue", "kyle-editor"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.KEditorVue = {}, global.Vue, global.KEditor));
|
|
3
|
+
})(this, function(exports2, vue, kyleEditor) {
|
|
4
|
+
"use strict";
|
|
5
|
+
const KEditorVue = {
|
|
6
|
+
name: "KEditor",
|
|
7
|
+
props: {
|
|
8
|
+
modelValue: { type: String, default: "" },
|
|
9
|
+
options: { type: Object, default: () => ({}) },
|
|
10
|
+
disabled: { type: Boolean, default: false },
|
|
11
|
+
height: { type: Number, default: null }
|
|
12
|
+
},
|
|
13
|
+
emits: ["update:modelValue", "ready", "change", "upload", "error", "save", "blur", "focus"],
|
|
14
|
+
setup(props, { emit, expose }) {
|
|
15
|
+
const host = vue.ref(null);
|
|
16
|
+
const instance = vue.shallowRef(null);
|
|
17
|
+
const buildOptions = () => {
|
|
18
|
+
const opts = { ...kyleEditor.defaults, ...props.options, value: props.modelValue || "" };
|
|
19
|
+
if (props.height) opts.height = props.height;
|
|
20
|
+
opts.onChange = (html) => {
|
|
21
|
+
emit("update:modelValue", html);
|
|
22
|
+
emit("change", html);
|
|
23
|
+
};
|
|
24
|
+
opts.onReady = (editor) => emit("ready", editor);
|
|
25
|
+
opts.onError = (error) => emit("error", error);
|
|
26
|
+
opts.onUpload = (payload) => emit("upload", payload);
|
|
27
|
+
return opts;
|
|
28
|
+
};
|
|
29
|
+
expose({
|
|
30
|
+
get editor() {
|
|
31
|
+
return instance.value;
|
|
32
|
+
},
|
|
33
|
+
getHTML: () => {
|
|
34
|
+
var _a;
|
|
35
|
+
return ((_a = instance.value) == null ? void 0 : _a.getHTML()) || "";
|
|
36
|
+
},
|
|
37
|
+
getText: () => {
|
|
38
|
+
var _a;
|
|
39
|
+
return ((_a = instance.value) == null ? void 0 : _a.getText()) || "";
|
|
40
|
+
},
|
|
41
|
+
counts: () => {
|
|
42
|
+
var _a;
|
|
43
|
+
return ((_a = instance.value) == null ? void 0 : _a.counts()) || null;
|
|
44
|
+
},
|
|
45
|
+
focus: () => {
|
|
46
|
+
var _a;
|
|
47
|
+
return (_a = instance.value) == null ? void 0 : _a.focus();
|
|
48
|
+
},
|
|
49
|
+
setContent: (html) => {
|
|
50
|
+
var _a;
|
|
51
|
+
return (_a = instance.value) == null ? void 0 : _a.setContent(html);
|
|
52
|
+
},
|
|
53
|
+
enterSource: () => {
|
|
54
|
+
var _a;
|
|
55
|
+
return (_a = instance.value) == null ? void 0 : _a.enterSource();
|
|
56
|
+
},
|
|
57
|
+
openPreview: () => {
|
|
58
|
+
var _a;
|
|
59
|
+
return (_a = instance.value) == null ? void 0 : _a.openPreview();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
vue.onMounted(() => {
|
|
63
|
+
instance.value = new kyleEditor.KEditor(host.value, buildOptions());
|
|
64
|
+
instance.value.on("save", (payload) => emit("save", payload));
|
|
65
|
+
instance.value.on("blur", () => emit("blur"));
|
|
66
|
+
instance.value.on("focus", () => emit("focus"));
|
|
67
|
+
host.value.__keditor__ = instance.value;
|
|
68
|
+
});
|
|
69
|
+
vue.onBeforeUnmount(() => {
|
|
70
|
+
var _a;
|
|
71
|
+
host.value.__keditor__ = null;
|
|
72
|
+
(_a = instance.value) == null ? void 0 : _a.destroy();
|
|
73
|
+
instance.value = null;
|
|
74
|
+
});
|
|
75
|
+
vue.watch(
|
|
76
|
+
() => props.modelValue,
|
|
77
|
+
(value) => {
|
|
78
|
+
const editor = instance.value;
|
|
79
|
+
if (!editor || editor.sourceMode) return;
|
|
80
|
+
if (value === editor.getHTML()) return;
|
|
81
|
+
editor.setContent(value || "", { silent: true });
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
vue.watch(
|
|
85
|
+
() => props.disabled,
|
|
86
|
+
(value) => {
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = instance.value) == null ? void 0 : _a.setReadOnly(value);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
return () => vue.h("div", { ref: host, class: "ke-vue-host" });
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const KEditorPlugin = {
|
|
95
|
+
install(app, { name = "KEditor" } = {}) {
|
|
96
|
+
app.component(name, KEditorVue);
|
|
97
|
+
app.config.globalProperties.$keditor = { KEditor: kyleEditor.KEditor, defaults: kyleEditor.defaults };
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
function useKEditor(options = {}) {
|
|
101
|
+
const host = vue.ref(null);
|
|
102
|
+
const instance = vue.shallowRef(null);
|
|
103
|
+
vue.onMounted(() => {
|
|
104
|
+
instance.value = new kyleEditor.KEditor(host.value, options);
|
|
105
|
+
});
|
|
106
|
+
vue.onBeforeUnmount(() => {
|
|
107
|
+
var _a;
|
|
108
|
+
return (_a = instance.value) == null ? void 0 : _a.destroy();
|
|
109
|
+
});
|
|
110
|
+
return { host, editor: instance };
|
|
111
|
+
}
|
|
112
|
+
KEditorVue.install = (app, options) => KEditorPlugin.install(app, options);
|
|
113
|
+
Object.defineProperty(exports2, "KEditor", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
get: () => kyleEditor.KEditor
|
|
116
|
+
});
|
|
117
|
+
exports2.KEditorPlugin = KEditorPlugin;
|
|
118
|
+
exports2.KEditorVue = KEditorVue;
|
|
119
|
+
exports2.default = KEditorVue;
|
|
120
|
+
exports2.useKEditor = useKEditor;
|
|
121
|
+
Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
122
|
+
});
|