kyle-editor 1.0.1 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kyle-editor",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "轻量、美观的微信公众号风格富文本编辑器(纯前端,支持图片/视频分片上传、HTML 源码模式),可通过 <script> 引入或作为 Vue 3 组件安装。",
5
5
  "type": "module",
6
6
  "main": "./dist/keditor.cjs",
@@ -39,7 +39,7 @@
39
39
  "test:types": "node test/check-types.mjs",
40
40
  "test:package": "node test/check-package.mjs",
41
41
  "test:install": "node test/check-install.mjs",
42
- "test:visual": "node test/shoot.mjs rich,uploading,bubble,resize,align,source,preview,color,heading,media,link,scroll,scroll2,http://127.0.0.1:5178/demo/vue.html",
42
+ "test:visual": "node test/shoot.mjs rich,uploading,bubble,resize,align,source,preview,color,heading,media,link,custom,customload,customerr,posbtn,wide,widefix,scroll,scroll2,http://127.0.0.1:5178/demo/vue.html",
43
43
  "clean": "rm -rf dist",
44
44
  "verify": "npm run build && npm test && npm run test:pages && npm run test:types && npm run test:package",
45
45
  "pack:check": "npm run build && npm run test:package && npm pack --dry-run",
@@ -52,12 +52,10 @@
52
52
  "editor",
53
53
  "wechat",
54
54
  "富文本编辑器",
55
- "chunk-upload",
56
- "分片上传",
57
55
  "vue3"
58
56
  ],
59
57
  "license": "MIT",
60
- "author": "kyle <zhendong.li@edoctor.cn>",
58
+ "author": "kyle <1399860604@qq.com>",
61
59
  "publishConfig": {
62
60
  "access": "public",
63
61
  "registry": "https://registry.npmjs.org"
package/types/index.d.ts CHANGED
@@ -130,8 +130,8 @@ export interface UploadHookPayload {
130
130
  error?: Error
131
131
  }
132
132
 
133
- /** 工具栏项名称 */
134
- export type ToolbarItem =
133
+ /** 内置工具栏项名称 */
134
+ export type BuiltinToolbarItem =
135
135
  | 'undo' | 'redo' | 'heading' | 'fontsize'
136
136
  | 'bold' | 'italic' | 'underline' | 'strike'
137
137
  | 'foreColor' | 'backColor'
@@ -141,6 +141,143 @@ export type ToolbarItem =
141
141
  | 'source' | 'preview' | 'fullscreen'
142
142
  | '|' | ':tail'
143
143
 
144
+ /**
145
+ * 工具栏项:内置名称、分隔符 `|`、`:tail`,或 customButtons 里定义的自定义按钮 name */
146
+ export type ToolbarItem = BuiltinToolbarItem | (string & {})
147
+
148
+ /**
149
+ * 按钮在工具栏里的位置(自定义按钮的 `position`,addButton / moveButton 的目标位):
150
+ * - `number`:第 n 个按钮之前(0 起,按视觉顺序计数,分隔符不计数)
151
+ * - `'start'`:工具栏最前
152
+ * - `'end'`:主区末尾(尾栏之前,默认)
153
+ * - `'tail'`:右侧吸附尾栏末尾
154
+ * - `'link'` 等按钮名:插到该按钮之前
155
+ * - `{ before: name }` / `{ after: name }`:显式锚点
156
+ */
157
+ export type ButtonPosition = number | 'start' | 'end' | 'tail' | (string & {}) | { before: string } | { after: string }
158
+
159
+ /** addButton / moveButton 的位置参数(at 优先于 before / after / index) */
160
+ export interface ButtonPlaceOptions {
161
+ at?: ButtonPosition
162
+ before?: string
163
+ after?: string
164
+ index?: number
165
+ }
166
+
167
+ /* ------------------------------ 自定义工具栏按钮 ------------------------------ */
168
+
169
+ /** 下拉选项;也接受纯字符串或接口原始对象(自动识别 value/id/key、label/name/title 等常见字段) */
170
+ export interface CustomButtonOption {
171
+ value?: string | number
172
+ label?: string
173
+ /** 副标题说明 */
174
+ desc?: string
175
+ /** 该项自带的跳转地址(link:true 时直接使用) */
176
+ url?: string
177
+ /** 分组名,同组选项会归到一个小标题下 */
178
+ group?: string
179
+ /** 选中后直接插入的 HTML(insert:'html' 或 auto 且无链接时使用) */
180
+ html?: string
181
+ disabled?: boolean
182
+ [key: string]: unknown
183
+ }
184
+
185
+ /** 选中后的跳转链接配置;字符串模板支持 {value} {label} 及选项任意字段占位符 */
186
+ export type CustomButtonLink =
187
+ | boolean
188
+ | string
189
+ | {
190
+ /** insert = 插入到正文(默认);open = 直接新窗口打开 */
191
+ mode?: 'insert' | 'open'
192
+ url?: string | ((item: CustomButtonOption, editor: KEditor) => string)
193
+ text?: string | ((item: CustomButtonOption, editor: KEditor) => string)
194
+ title?: string | ((item: CustomButtonOption, editor: KEditor) => string)
195
+ target?: '_blank' | '_self' | boolean
196
+ }
197
+ | ((item: CustomButtonOption, editor: KEditor) => string | { mode?: 'insert' | 'open'; url?: string; text?: string; target?: '_blank' | '_self' } | null)
198
+
199
+ export interface CustomButtonFetchOptions {
200
+ method?: string
201
+ headers?: Record<string, string>
202
+ body?: BodyInit | null
203
+ credentials?: RequestCredentials
204
+ /** 超时毫秒,默认 10000 */
205
+ timeout?: number
206
+ /** 自定义 fetch 实现(如 axios 包装) */
207
+ fetch?: typeof fetch
208
+ /** 自定义响应解析,默认 res.json() */
209
+ parser?: (res: Response) => unknown | Promise<unknown>
210
+ }
211
+
212
+ export interface CustomButtonSpec {
213
+ /** 唯一标识,用于在 toolbar 数组中定位;也作为事件载荷的 name */
214
+ name: string
215
+ /** select = 下拉选择(默认);button = 普通按钮 */
216
+ type?: 'select' | 'button'
217
+ /** tooltip */
218
+ title?: string
219
+ /** 按钮文字;下拉按钮选中后会替换为选项文字 */
220
+ text?: string
221
+ /** 未选中时的占位文字,默认取 text */
222
+ placeholder?: string
223
+ /** 内置图标名或原始 SVG 字符串 */
224
+ icon?: string
225
+ /** 面板宽度 px(200–420) */
226
+ width?: number
227
+ /** 面板内显示搜索框(客户端过滤) */
228
+ searchable?: boolean
229
+ /** 面板右上角提示文字 */
230
+ note?: string
231
+ /** 空数据提示 */
232
+ emptyText?: string
233
+ /** 缓存接口结果,默认 true;面板内刷新按钮可强制重新请求 */
234
+ cache?: boolean
235
+ /** 记住选中项并在按钮上回显,默认 true */
236
+ keepValue?: boolean
237
+
238
+ /** 按钮在工具栏中的位置,默认追加到主区末尾(尾栏之前) */
239
+ position?: ButtonPosition
240
+ /** position 别名(与 addButton 的 options.at 同义) */
241
+ at?: ButtonPosition
242
+ /** 简写:插到某按钮之前 */
243
+ before?: string
244
+ /** 简写:插到某按钮之后 */
245
+ after?: string
246
+ /** 简写:第 n 个按钮之前 */
247
+ index?: number
248
+
249
+ /** 数据源①:静态配置 */
250
+ options?: CustomButtonOption[] | string[]
251
+ /** 数据源②:自定义加载函数(可返回 Promise) */
252
+ load?: (editor: KEditor) => CustomButtonOption[] | Promise<CustomButtonOption[] | unknown> | unknown
253
+ /** 数据源③:接口地址,内部 fetch;优先级 load > url > options */
254
+ url?: string
255
+ fetchOptions?: CustomButtonFetchOptions
256
+ /** 从接口响应中提取数组;默认自动识别 items/list/data/rows/records/result(含一层嵌套) */
257
+ pick?: (payload: unknown, editor: KEditor) => unknown
258
+
259
+ /** 选中后的跳转链接配置 */
260
+ link?: CustomButtonLink
261
+ /** 插入方式:auto(默认,链接 > html > 纯文本)| link | html | text | false(不改正文) */
262
+ insert?: 'auto' | 'link' | 'html' | 'text' | false | 'none'
263
+ /** 自定义选中处理;返回 false 表示已自行处理,跳过默认插入 */
264
+ onSelect?: (item: CustomButtonOption, ctx: {
265
+ editor: KEditor
266
+ item: CustomButtonOption
267
+ link: { mode: 'insert' | 'open'; url: string; text: string; target: '_blank' | '_self'; title: string } | null
268
+ insertLink: (override?: Record<string, unknown>) => void
269
+ insertHtml: (html: string) => void
270
+ insertText: (text: string) => void
271
+ open: (url?: string, target?: string) => void
272
+ }) => void | boolean | Promise<void | boolean>
273
+ /** type:'button' 的点击处理 */
274
+ onClick?: (ctx: { editor: KEditor; name: string }) => void | Promise<void>
275
+ /** 高亮条件 */
276
+ active?: (state: EditorState, editor: KEditor) => boolean
277
+ /** 可用条件 */
278
+ enabled?: (state: EditorState, editor: KEditor) => boolean
279
+ }
280
+
144
281
  export interface KEditorOptions {
145
282
  value?: string
146
283
  placeholder?: string
@@ -149,10 +286,18 @@ export interface KEditorOptions {
149
286
  minHeight?: number
150
287
  /** 主题色,默认微信绿 #07c160 */
151
288
  primaryColor?: string
152
- /** 正文版心宽度,默认 677(公众号) */
289
+ /** 版心基准宽度,默认 677(公众号);fluid 模式下它是最小宽度 */
153
290
  paperWidth?: number
154
- /** 工具栏配置;false 隐藏 */
291
+ /** 版心宽度模式:'fluid'(默认,宽屏版心随容器生长)| 'fixed'(恒定 paperWidth + 64) */
292
+ paperMode?: 'fluid' | 'fixed'
293
+ /** fluid 模式下单侧留白,默认 24 */
294
+ paperGap?: number
295
+ /** fluid 模式下载心最大宽度,0 = 不限 */
296
+ paperMaxWidth?: number
297
+ /** 工具栏配置;false 隐藏。留空且配置了 customButtons 时,自定义按钮自动追加到默认工具栏 */
155
298
  toolbar?: ToolbarItem[] | boolean
299
+ /** 自定义工具栏按钮(下拉选择,数据可静态配置或从接口读取) */
300
+ customButtons?: CustomButtonSpec[]
156
301
  hideToolbar?: boolean
157
302
  hideStatusbar?: boolean
158
303
  readOnly?: boolean
@@ -223,6 +368,8 @@ export interface EditorEventMap {
223
368
  uploadcancel: [{ file: File; kind: 'image' | 'video' }]
224
369
  insertmedia: [{ kind: 'image' | 'video'; url: string; node: HTMLElement }]
225
370
  sourcechange: [{ mode: 'source' | 'visual' }]
371
+ customselect: [{ name: string; item: CustomButtonOption; link: { mode: 'insert' | 'open'; url: string; text: string; target: string; title: string } | null; action: 'link' | 'html' | 'text' | 'open' | 'none'; editor: KEditor }]
372
+ customclick: [{ name: string; editor: KEditor }]
226
373
  }
227
374
 
228
375
  /* ------------------------------ 实例 ------------------------------ */
@@ -268,7 +415,20 @@ export declare class KEditor {
268
415
  tasks: Map<string, unknown>
269
416
  }
270
417
  readonly uploader: ChunkUploader
271
- readonly toolbar: { el: HTMLElement; buttons: Map<string, HTMLElement>; openLink(): void; openMedia(kind: 'image' | 'video'): void; openTable(): void }
418
+ readonly toolbar: {
419
+ el: HTMLElement
420
+ buttons: Map<string, HTMLElement>
421
+ custom: Map<string, { spec: CustomButtonSpec; button: HTMLElement | null }>
422
+ openLink(): void
423
+ openMedia(kind: 'image' | 'video'): void
424
+ openTable(): void
425
+ openCustom(name: string): void
426
+ addButton(spec: CustomButtonSpec, options?: ButtonPlaceOptions): HTMLElement | null
427
+ moveButton(name: string, where?: ButtonPosition): boolean
428
+ buttonNodes(): HTMLElement[]
429
+ removeButton(name: string): boolean
430
+ setButtonOptions(name: string, items: unknown): CustomButtonOption[]
431
+ }
272
432
  readonly popover: { el: HTMLElement; open: (...args: unknown[]) => void; close(): void; isOpen: boolean }
273
433
  readonly bubble: { el: HTMLElement; show(node: HTMLElement): void; hide(): void }
274
434
  readonly source: { el: HTMLElement; show(html: string): void; apply(opts?: { silent?: boolean }): void }
@@ -306,6 +466,20 @@ export declare class KEditor {
306
466
  insertVideo(url: string, label?: string): HTMLElement
307
467
  cancelUploads(): this
308
468
 
469
+ /** 运行时新增自定义按钮,返回按钮 DOM */
470
+ addButton(spec: CustomButtonSpec, options?: ButtonPlaceOptions): HTMLElement | null
471
+ /** 移动任意按钮(内置或自定义)到指定位置 */
472
+ moveButton(name: string, where?: ButtonPosition): boolean
473
+ removeButton(name: string): boolean
474
+ /** 运行时调整版心:{ width, mode, gap, maxWidth } */
475
+ setPaper(config?: { width?: number; mode?: 'fluid' | 'fixed'; gap?: number; maxWidth?: number }): { width: number; mode: 'fluid' | 'fixed'; gap: number; maxWidth: number }
476
+ paperOptions(): { width: number; mode: 'fluid' | 'fixed'; gap: number; maxWidth: number }
477
+ /** 外部灌入下拉数据(接口在编辑器之外请求时使用) */
478
+ setButtonOptions(name: string, items: CustomButtonOption[] | string[]): CustomButtonOption[]
479
+ getButtonValue(name: string): CustomButtonOption | null
480
+ setButtonValue(name: string, value: string | number): CustomButtonOption | null
481
+ reportError(type: string, error: unknown): this
482
+
309
483
  toggleSource(): boolean
310
484
  enterSource(): void
311
485
  exitSource(): void
@@ -331,6 +505,9 @@ export declare const mount: typeof create
331
505
  export declare function replace(target: HTMLElement | string, options?: KEditorOptions): KEditor
332
506
  export declare const defaults: KEditorOptions
333
507
  export declare const DEFAULT_TOOLBAR: ToolbarItem[]
508
+ export declare function normalizeCustomButtons(list: unknown): CustomButtonSpec[]
509
+ export declare function normalizePosition(spec: unknown): ButtonPosition | null
510
+ export declare function normalizeItems(raw: unknown): CustomButtonOption[]
334
511
  export declare function exportHtml(html: string): string
335
512
 
336
513
  export declare function sanitizeHtml(html: string, opts?: { allowVideoCard?: boolean }): string