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.
@@ -0,0 +1,345 @@
1
+ /**
2
+ * KEditor 类型声明(与 src/ 实现保持一致)
3
+ * ESM:import KEditor, { create, mockAdapter } from 'kyle-editor'
4
+ * UMD :window.KEditor 为命名空间对象,使用 KEditor.create(...) / KEditor.KEditor 类
5
+ */
6
+
7
+ /* ------------------------------ 上传 ------------------------------ */
8
+
9
+ export interface UploadResult {
10
+ url: string
11
+ poster?: string
12
+ size?: number
13
+ name?: string
14
+ duration?: number
15
+ extra?: Record<string, unknown>
16
+ }
17
+
18
+ export interface UploadProgressInfo {
19
+ loaded: number
20
+ total: number
21
+ chunks: number
22
+ index?: number
23
+ speed?: number
24
+ instant?: boolean
25
+ }
26
+
27
+ export interface UploadProgressEvent {
28
+ ratio: number
29
+ info: UploadProgressInfo
30
+ }
31
+
32
+ /** 分片上传适配器协议,详见 docs/upload-adapter.md */
33
+ export interface UploadAdapter {
34
+ name?: string
35
+ /** 初始化:返回 uploadId / 分片数 / 是否秒传 */
36
+ init?: (file: File, ctx: { chunkSize: number; signal: AbortSignal; fingerprint: string }) => Promise<{
37
+ uploadId?: string
38
+ totalChunks?: number
39
+ uploadedChunks?: number[]
40
+ instant?: UploadResult | false
41
+ chunkSize?: number
42
+ }>
43
+ /** 上传单个分片 */
44
+ put?: (chunk: Blob, meta: { index: number; total: number; uploadId?: string; file: File; signal: AbortSignal }) => Promise<unknown>
45
+ /** 合并并返回最终地址 */
46
+ complete?: (meta: { uploadId?: string; file: File; total: number; signal: AbortSignal }) => Promise<UploadResult | string>
47
+ /** 单次直传(无分片) */
48
+ upload?: (file: File, ctx: { onProgress: (loaded: number, total: number) => void; signal: AbortSignal }) => Promise<UploadResult | string>
49
+ }
50
+
51
+ export interface UploadOptions {
52
+ /** 分片大小,默认 1MB */
53
+ chunkSize?: number
54
+ /** 并发分片数,默认 3 */
55
+ concurrency?: number
56
+ /** 单片失败重试次数,默认 2 */
57
+ retries?: number
58
+ /** 指纹算法:'none' | 'fast' | 'strong' */
59
+ fingerprint?: 'none' | 'fast' | 'strong'
60
+ }
61
+
62
+ export interface HttpAdapterConfig extends Partial<UploadOptions> {
63
+ type: 'http'
64
+ initUrl?: string
65
+ chunkUrl?: string
66
+ completeUrl?: string
67
+ queryUrl?: string
68
+ headers?: Record<string, string>
69
+ fetchImpl?: typeof fetch
70
+ extra?: ((file: File) => Record<string, unknown>) | Record<string, unknown>
71
+ pickResult?: (json: unknown) => UploadResult
72
+ }
73
+
74
+ export type UploaderOption = 'mock' | UploadAdapter | HttpAdapterConfig | ChunkUploader | ((
75
+ file: File,
76
+ onProgress: (loaded: number, total: number) => void,
77
+ signal?: AbortSignal,
78
+ ) => Promise<UploadResult | string>)
79
+
80
+ export class ChunkUploader {
81
+ constructor(adapter: UploadAdapter, options?: UploadOptions)
82
+ upload(file: File, opts?: { signal?: AbortSignal; onProgress?: (ratio: number, info: UploadProgressInfo) => void }): Promise<UploadResult | string>
83
+ abort(): void
84
+ }
85
+
86
+ export declare function mockAdapter(opts?: {
87
+ speed?: number
88
+ latency?: number
89
+ failRate?: number
90
+ chunkSize?: number
91
+ }): UploadAdapter
92
+
93
+ export declare function httpAdapter(config: HttpAdapterConfig): UploadAdapter
94
+
95
+ export declare function simpleAdapter(
96
+ fn: (file: File, onProgress: (loaded: number, total: number) => void, signal?: AbortSignal) => Promise<UploadResult | string>,
97
+ ): UploadAdapter
98
+
99
+ export declare function createUploader(option: UploaderOption, config?: UploadOptions): ChunkUploader
100
+
101
+ /* ------------------------------ 配置 ------------------------------ */
102
+
103
+ export interface ImageCompressOptions {
104
+ maxWidth?: number
105
+ quality?: number
106
+ /** 小于该字节数不压缩 */
107
+ minSize?: number
108
+ }
109
+
110
+ export interface ImageOptions {
111
+ /** 上传前压缩,false 关闭 */
112
+ compress?: boolean | ImageCompressOptions
113
+ }
114
+
115
+ export interface PasteOptions {
116
+ enabled?: boolean
117
+ clean?: boolean
118
+ /** 粘贴截图/文件直接上传 */
119
+ uploadFiles?: boolean
120
+ localizeImages?: boolean
121
+ }
122
+
123
+ export interface UploadHookPayload {
124
+ stage: 'uploadstart' | 'uploaddone' | 'uploaderror' | 'uploadcancel' | 'uploadprogress'
125
+ file?: File
126
+ kind?: 'image' | 'video'
127
+ url?: string
128
+ percent?: number
129
+ info?: UploadProgressInfo
130
+ error?: Error
131
+ }
132
+
133
+ /** 工具栏项名称 */
134
+ export type ToolbarItem =
135
+ | 'undo' | 'redo' | 'heading' | 'fontsize'
136
+ | 'bold' | 'italic' | 'underline' | 'strike'
137
+ | 'foreColor' | 'backColor'
138
+ | 'alignLeft' | 'alignCenter' | 'alignRight' | 'alignJustify'
139
+ | 'ul' | 'ol' | 'quote'
140
+ | 'link' | 'image' | 'video' | 'table' | 'code' | 'hr' | 'clear'
141
+ | 'source' | 'preview' | 'fullscreen'
142
+ | '|' | ':tail'
143
+
144
+ export interface KEditorOptions {
145
+ value?: string
146
+ placeholder?: string
147
+ /** 编辑区可视高度(px),0 = 自适应,默认 460 */
148
+ height?: number
149
+ minHeight?: number
150
+ /** 主题色,默认微信绿 #07c160 */
151
+ primaryColor?: string
152
+ /** 正文版心宽度,默认 677(公众号) */
153
+ paperWidth?: number
154
+ /** 工具栏配置;false 隐藏 */
155
+ toolbar?: ToolbarItem[] | boolean
156
+ hideToolbar?: boolean
157
+ hideStatusbar?: boolean
158
+ readOnly?: boolean
159
+ autofocus?: boolean
160
+ /** 最大字符数,0 不限 */
161
+ maxLength?: number
162
+ wordCount?: boolean
163
+ /** 是否对输入内容做白名单净化,默认 true */
164
+ sanitize?: boolean
165
+ historyLimit?: number
166
+ uploader?: UploaderOption
167
+ upload?: UploadOptions
168
+ image?: ImageOptions
169
+ maxImageSize?: number
170
+ maxVideoSize?: number
171
+ imageAccept?: string
172
+ videoAccept?: string
173
+ paste?: PasteOptions
174
+ onChange?: (html: string, editor: KEditor) => void
175
+ onReady?: (editor: KEditor) => void
176
+ onError?: (error: { type: string; error: Error; message: string }) => void
177
+ /** 上传各阶段统一回调 */
178
+ onUpload?: (payload: UploadHookPayload) => void
179
+ /** 上传前拦截:返回改写后的 File 或 false 取消 */
180
+ beforeUpload?: (file: File, kind: 'image' | 'video') => File | false | Promise<File | false>
181
+ }
182
+
183
+ export interface EditorCounts {
184
+ chars: number
185
+ words: number
186
+ images: number
187
+ videos: number
188
+ uploading: number
189
+ }
190
+
191
+ export interface EditorState {
192
+ bold: boolean
193
+ italic: boolean
194
+ underline: boolean
195
+ strikeThrough: boolean
196
+ ul: boolean
197
+ ol: boolean
198
+ align: 'left' | 'center' | 'right' | 'justify'
199
+ block: string
200
+ fontSize: number | ''
201
+ linkUrl: string
202
+ linkTarget: boolean
203
+ selectionText: string
204
+ canUndo: boolean
205
+ canRedo: boolean
206
+ source: boolean
207
+ preview: boolean
208
+ fullscreen: boolean
209
+ readOnly: boolean
210
+ }
211
+
212
+ export interface EditorEventMap {
213
+ ready: [{ editor: KEditor }]
214
+ change: [{ html: string; text: string }]
215
+ focus: []
216
+ blur: []
217
+ save: [{ html: string }]
218
+ error: [{ type: string; error: Error; message: string }]
219
+ uploadstart: [{ file: File; kind: 'image' | 'video' }]
220
+ uploadprogress: [{ file: File; kind: 'image' | 'video'; percent: number; info: UploadProgressInfo }]
221
+ uploaddone: [{ file: File; kind: 'image' | 'video'; url: string }]
222
+ uploaderror: [{ file: File; kind: 'image' | 'video'; error: Error }]
223
+ uploadcancel: [{ file: File; kind: 'image' | 'video' }]
224
+ insertmedia: [{ kind: 'image' | 'video'; url: string; node: HTMLElement }]
225
+ sourcechange: [{ mode: 'source' | 'visual' }]
226
+ }
227
+
228
+ /* ------------------------------ 实例 ------------------------------ */
229
+
230
+ export declare class Commands {
231
+ toggleMark(name: 'bold' | 'italic' | 'underline' | 'strike'): void
232
+ setFontSize(px: number): void
233
+ currentFontSize(): number | ''
234
+ setColor(mode: 'fore' | 'back', color: string | null): void
235
+ setAlign(value: 'left' | 'center' | 'right' | 'justify'): void
236
+ toggleList(kind: 'ordered' | 'unordered'): void
237
+ setHeading(tag: 'p' | 'h1' | 'h2' | 'h3' | 'h4' | 'blockquote' | ''): void
238
+ toggleBlockquote(): void
239
+ createLink(payload: { url: string; text?: string; target?: boolean }): void
240
+ removeLink(): void
241
+ insertTable(cols: number, rows: number): HTMLTableElement
242
+ insertCodeBlock(code?: string): HTMLElement
243
+ insertDivider(): HTMLElement
244
+ insertHTML(html: string): void
245
+ insertNode(node: Node): void
246
+ clearFormat(): void
247
+ }
248
+
249
+ export declare class KEditor {
250
+ constructor(target: HTMLElement | string, options?: KEditorOptions)
251
+
252
+ static create(target: HTMLElement | string, options?: KEditorOptions): KEditor
253
+ static plugin: { install(app: unknown, opts?: { name?: string }): void }
254
+
255
+ readonly id: string
256
+ readonly el: HTMLElement
257
+ readonly root: HTMLElement
258
+ readonly options: Required<KEditorOptions>
259
+ readonly commands: Commands
260
+ readonly media: {
261
+ pickFiles(kind: 'image' | 'video'): void
262
+ addFiles(files: File[]): Promise<number>
263
+ upload(file: File, kind: 'image' | 'video'): Promise<UploadResult | null>
264
+ insertFromUrl(kind: 'image' | 'video', url: string, name?: string): HTMLElement
265
+ insertImageUrl(url: string, alt?: string): HTMLElement
266
+ insertVideoUrl(url: string, label?: string): HTMLElement
267
+ cancelAll(): void
268
+ tasks: Map<string, unknown>
269
+ }
270
+ readonly uploader: ChunkUploader
271
+ readonly toolbar: { el: HTMLElement; buttons: Map<string, HTMLElement>; openLink(): void; openMedia(kind: 'image' | 'video'): void; openTable(): void }
272
+ readonly popover: { el: HTMLElement; open: (...args: unknown[]) => void; close(): void; isOpen: boolean }
273
+ readonly bubble: { el: HTMLElement; show(node: HTMLElement): void; hide(): void }
274
+ readonly source: { el: HTMLElement; show(html: string): void; apply(opts?: { silent?: boolean }): void }
275
+ readonly history: { reset(html: string): void; snapshot(force?: boolean): void; flush(): void }
276
+ readonly sourceMode: boolean
277
+ readonly previewMode: boolean
278
+ readonly fullscreen: boolean
279
+ readonly readOnly: boolean
280
+ readonly destroyed: boolean
281
+
282
+ get element(): HTMLElement
283
+ get editorElement(): HTMLElement
284
+
285
+ getHTML(opts?: { clean?: boolean }): string
286
+ getText(): string
287
+ getJSON(): { html: string; text: string; counts: EditorCounts }
288
+ setContent(html: string, opts?: { sanitize?: boolean; silent?: boolean }): this
289
+ appendHTML(html: string): this
290
+ clear(): this
291
+ counts(): EditorCounts
292
+ getState(): EditorState
293
+ refresh(): void
294
+
295
+ focus(): this
296
+ blur(): this
297
+ saveSelection(): this
298
+ restoreSelection(): this
299
+ setReadOnly(value?: boolean): this
300
+
301
+ undo(): this
302
+ redo(): this
303
+
304
+ upload(file: File, kind?: 'image' | 'video'): Promise<UploadResult | null>
305
+ insertImage(url: string, alt?: string): HTMLElement
306
+ insertVideo(url: string, label?: string): HTMLElement
307
+ cancelUploads(): this
308
+
309
+ toggleSource(): boolean
310
+ enterSource(): void
311
+ exitSource(): void
312
+ togglePreview(): boolean
313
+ openPreview(): void
314
+ closePreview(): void
315
+ toggleFullscreen(): boolean
316
+
317
+ on<K extends keyof EditorEventMap>(type: K, handler: (...args: EditorEventMap[K]) => void): () => void
318
+ on(type: string, handler: (payload: any) => void): () => void
319
+ off(type: string, handler: (...args: any[]) => void): this
320
+ emit(type: string, payload?: unknown): this
321
+ addListener(type: string, handler: (payload: any) => void): this
322
+ removeListener(type: string, handler: (payload: any) => void): this
323
+ destroy(): void
324
+ }
325
+
326
+ /* ------------------------------ 模块导出 ------------------------------ */
327
+
328
+ export declare const version: string
329
+ export declare function create(target: HTMLElement | string, options?: KEditorOptions): KEditor
330
+ export declare const mount: typeof create
331
+ export declare function replace(target: HTMLElement | string, options?: KEditorOptions): KEditor
332
+ export declare const defaults: KEditorOptions
333
+ export declare const DEFAULT_TOOLBAR: ToolbarItem[]
334
+ export declare function exportHtml(html: string): string
335
+
336
+ export declare function sanitizeHtml(html: string, opts?: { allowVideoCard?: boolean }): string
337
+ export declare function escapeHtml(text?: string): string
338
+ export declare function isSafeUrl(url?: string): boolean
339
+ export declare function cleanWordHtml(html: string): string
340
+ export declare function formatHtml(html: string, opts?: { indent?: string }): string
341
+ export declare function fastFingerprint(file: File): Promise<string>
342
+ export declare function strongFingerprint(file: File): Promise<string>
343
+ export declare function maybeCompress(file: File, options?: ImageOptions): Promise<File>
344
+
345
+ export default KEditor
package/types/vue.d.ts ADDED
@@ -0,0 +1,73 @@
1
+ /** KEditor Vue 3 集成类型(kyle-editor/vue) */
2
+ import type { App, ComponentOptionsMixin, ComponentPublicInstance, DefineComponent, Ref } from 'vue'
3
+ import type { EditorCounts, EditorEventMap, KEditor, KEditorOptions, UploadHookPayload } from './index'
4
+
5
+ export interface KEditorVueProps {
6
+ /** v-model 绑定的 HTML */
7
+ modelValue?: string
8
+ /** 其余编辑器配置,与 props 冲突时以 options 为准 */
9
+ options?: KEditorOptions
10
+ /** 只读 */
11
+ disabled?: boolean
12
+ /** 编辑区高度(px),优先级高于 options.height */
13
+ height?: number
14
+ }
15
+
16
+ export interface KEditorVueEmits {
17
+ (e: 'update:modelValue', html: string): void
18
+ (e: 'change', html: string): void
19
+ (e: 'ready', editor: KEditor): void
20
+ (e: 'upload', payload: UploadHookPayload): void
21
+ (e: 'error', error: { type: string; error: Error; message: string }): void
22
+ (e: 'save', payload: { html: string }): void
23
+ (e: 'blur'): void
24
+ (e: 'focus'): void
25
+ }
26
+
27
+ /** ref 上可访问的实例方法 */
28
+ export interface KEditorVueExposed {
29
+ readonly editor: KEditor | null
30
+ getHTML(): string
31
+ getText(): string
32
+ counts(): EditorCounts | null
33
+ focus(): void
34
+ setContent(html: string): void
35
+ enterSource(): void
36
+ openPreview(): void
37
+ }
38
+
39
+ export declare const KEditorVue: DefineComponent<
40
+ KEditorVueProps,
41
+ {},
42
+ {},
43
+ {},
44
+ {},
45
+ ComponentOptionsMixin,
46
+ ComponentOptionsMixin,
47
+ KEditorVueEmits,
48
+ 'update:modelValue' | 'change' | 'ready' | 'upload' | 'error' | 'save' | 'blur' | 'focus'
49
+ >
50
+
51
+ export declare const KEditorPlugin: {
52
+ install(app: App, opts?: { name?: string }): void
53
+ }
54
+
55
+ /** 组合式用法:const { host, editor } = useKEditor(options) */
56
+ export declare function useKEditor(options?: KEditorOptions): {
57
+ host: Ref<HTMLElement | null>
58
+ editor: Ref<KEditor | null>
59
+ }
60
+
61
+ export type KEditorVueInstance = ComponentPublicInstance & KEditorVueExposed
62
+ export { KEditorVue as KEditorComponent }
63
+
64
+ /**
65
+ * 默认导出 = 组件本身,并自带 `install`:
66
+ * - `app.use(KEditorVue)` → 全局注册 `<k-editor>`
67
+ * - `<KEditorVue />` / `import KEditorVue from 'kyle-editor/vue'` 直接当组件用
68
+ */
69
+ declare const KEditorVueWithInstall: typeof KEditorVue & {
70
+ install(app: App, opts?: { name?: string }): void
71
+ }
72
+ export default KEditorVueWithInstall
73
+ export type { KEditor, KEditorOptions, EditorCounts, EditorEventMap }