@xuanmo/aieditor 0.0.1

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,547 @@
1
+ import { ChainedCommands } from '@tiptap/core/dist/packages/core/src/types';
2
+ import { ChainedCommands as ChainedCommands_2 } from '@tiptap/core';
3
+ import { Editor } from '@tiptap/core';
4
+ import { EditorEvents } from '@tiptap/core';
5
+ import { EditorOptions } from '@tiptap/core';
6
+ import { Extensions } from '@tiptap/core';
7
+ import { Fragment } from 'prosemirror-model';
8
+ import { Instance } from 'tippy.js';
9
+ import { JSONContent } from '@tiptap/core';
10
+ import { SingleCommands } from '@tiptap/core';
11
+ import { Transaction } from '@tiptap/pm/state';
12
+
13
+ declare class AbstractMenuButton extends HTMLElement implements AiEditorEventListener {
14
+ template: string;
15
+ editor?: Editor;
16
+ options?: AiEditorOptions;
17
+ protected constructor();
18
+ protected registerClickListener(): void;
19
+ connectedCallback(): void;
20
+ onClick(commands: ChainedCommands): void;
21
+ onCreate(props: EditorEvents['create'], options: AiEditorOptions): void;
22
+ onTransaction(event: EditorEvents['transaction']): void;
23
+ onActive(editor: Editor): boolean;
24
+ onEditableChange(editable: boolean): void;
25
+ }
26
+
27
+ export declare type AIBubbleMenuItem = {
28
+ prompt: string;
29
+ icon: string;
30
+ title: string;
31
+ } | string;
32
+
33
+ export declare interface AiClient {
34
+ start: (payload: string) => void;
35
+ stop: () => void;
36
+ }
37
+
38
+ export declare interface AiClientListener {
39
+ onStart: (aiClient: AiClient) => void;
40
+ onStop: () => void;
41
+ onMessage: (bodyString: string) => void;
42
+ }
43
+
44
+ export declare class AiEditor {
45
+ customLayout: boolean;
46
+ innerEditor: InnerEditor;
47
+ container: HTMLDivElement;
48
+ header: Header;
49
+ mainEl: HTMLDivElement;
50
+ footer: Footer;
51
+ options: AiEditorOptions;
52
+ eventComponents: AiEditorEventListener[];
53
+ constructor(_: AiEditorOptions);
54
+ private initI18nAndInnerEditor;
55
+ protected initInnerEditor(): void;
56
+ protected getExtensions(): Extensions;
57
+ protected onCreate(props: EditorEvents['create']): void;
58
+ protected onTransaction(transEvent: EditorEvents['transaction']): void;
59
+ getHtml(): string;
60
+ getJson(): JSONContent;
61
+ getText(): string;
62
+ getSelectedText(): string;
63
+ getMarkdown(): any;
64
+ getOptions(): AiEditorOptions;
65
+ getAttributes(name: string): Record<string, any>;
66
+ setAttributes(name: string, attributes: Record<string, any>): void;
67
+ isActive(nameOrAttrs: any, attrs?: {}): boolean;
68
+ commands(): SingleCommands;
69
+ commandsChain(): ChainedCommands_2;
70
+ getOutline(): any[];
71
+ focus(): this;
72
+ focusPos(pos: number): this;
73
+ focusStart(): this;
74
+ focusEnd(): this;
75
+ isFocused(): boolean;
76
+ blur(): this;
77
+ insert(content: any): this;
78
+ insertMarkdown(content: string): this;
79
+ setEditable(editable: boolean): this;
80
+ setContent(content: string): this;
81
+ setMarkdownContent(content: string): this;
82
+ clear(): this;
83
+ isEmpty(): boolean;
84
+ changeLang(lang: string): this;
85
+ removeRetention(): this;
86
+ destroy(): void;
87
+ isDestroyed(): boolean;
88
+ }
89
+
90
+ export declare interface AiEditorEventListener {
91
+ onCreate: (props: EditorEvents['create'], options: AiEditorOptions) => void;
92
+ onTransaction: (props: EditorEvents['transaction']) => void;
93
+ onEditableChange: (editable: boolean) => void;
94
+ }
95
+
96
+ export declare type AiEditorOptions = {
97
+ element: string | Element;
98
+ content?: string;
99
+ contentIsMarkdown?: boolean;
100
+ contentRetention?: boolean;
101
+ contentRetentionKey?: string;
102
+ lang?: string;
103
+ editable?: boolean;
104
+ i18n?: Record<string, Record<string, string>>;
105
+ placeholder?: string;
106
+ theme?: 'light' | 'dark';
107
+ onMentionQuery?: (query: string) => any[] | Promise<any[]>;
108
+ onCreateBefore?: (editor: AiEditor, extensions: Extensions) => void | Extensions;
109
+ onCreated?: (editor: AiEditor) => void;
110
+ onChange?: (editor: AiEditor) => void;
111
+ onTransaction?: (editor: AiEditor, transaction: Transaction) => void;
112
+ onFocus?: (editor: AiEditor) => void;
113
+ onBlur?: (editor: AiEditor) => void;
114
+ onDestroy?: (editor: AiEditor) => void;
115
+ onSave?: (editor: AiEditor) => boolean;
116
+ toolbarKeys?: (string | CustomMenu | MenuGroup)[];
117
+ toolbarExcludeKeys?: DefaultToolbarKey[];
118
+ toolbarSize?: 'small' | 'medium' | 'large';
119
+ draggable?: boolean;
120
+ htmlPasteConfig?: HtmlPasteConfig;
121
+ codeBlock?: {
122
+ languages?: LanguageItem[];
123
+ codeExplainPrompt?: string;
124
+ codeCommentsPrompt?: string;
125
+ };
126
+ textSelectionBubbleMenu?: {
127
+ enable?: boolean;
128
+ elementTagName?: string;
129
+ items?: (string | BubbleMenuItem)[];
130
+ };
131
+ link?: {
132
+ autolink?: boolean;
133
+ rel?: string;
134
+ class?: string;
135
+ bubbleMenuItems?: (string | BubbleMenuItem)[];
136
+ };
137
+ uploader?: Uploader;
138
+ image?: {
139
+ customMenuInvoke?: (editor: AiEditor) => void;
140
+ uploadUrl?: string;
141
+ uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
142
+ uploadFormName?: string;
143
+ uploader?: Uploader;
144
+ uploaderEvent?: UploaderEvent;
145
+ defaultSize?: number;
146
+ allowBase64?: boolean;
147
+ bubbleMenuEnable?: boolean;
148
+ bubbleMenuItems?: (string | BubbleMenuItem)[];
149
+ };
150
+ video?: {
151
+ customMenuInvoke?: (editor: AiEditor) => void;
152
+ uploadUrl?: string;
153
+ uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
154
+ uploadFormName?: string;
155
+ uploader?: Uploader;
156
+ uploaderEvent?: UploaderEvent;
157
+ };
158
+ attachment?: {
159
+ customMenuInvoke?: (editor: AiEditor) => void;
160
+ uploadUrl?: string;
161
+ uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
162
+ uploadFormName?: string;
163
+ uploader?: Uploader;
164
+ uploaderEvent?: UploaderEvent;
165
+ };
166
+ fontFamily?: {
167
+ values: NameAndValue[];
168
+ };
169
+ fontSize?: {
170
+ defaultValue?: number;
171
+ values?: NameAndValue[];
172
+ };
173
+ lineHeight?: {
174
+ values?: string[];
175
+ };
176
+ emoji?: {
177
+ values?: string[];
178
+ };
179
+ textCounter?: (text: string) => number;
180
+ ai?: AiGlobalConfig;
181
+ };
182
+
183
+ export declare interface AiGlobalConfig {
184
+ models: Record<string, AiModelConfig>;
185
+ modelFactory?: AiModelFactory;
186
+ onTokenConsume?: (modelName: string, modelConfig: AiModelConfig, count: number) => void;
187
+ onCreateClientUrl?: (modelName: string, modelConfig: AiModelConfig, onSuccess: (url: string) => void, onFailure: () => void) => void;
188
+ bubblePanelEnable?: boolean;
189
+ bubblePanelModel?: string;
190
+ bubblePanelMenus?: AIBubbleMenuItem[];
191
+ bubblePanelIcon?: string;
192
+ menus?: AiMenu[];
193
+ commands?: AiMenu[];
194
+ translate?: {
195
+ prompt?: (language: string, selectText: string) => string;
196
+ translateMenuItems?: TranslateMenuItem[];
197
+ };
198
+ codeBlock?: {
199
+ codeComments?: {
200
+ model: string;
201
+ prompt: string;
202
+ };
203
+ codeExplain?: {
204
+ model: string;
205
+ prompt: string;
206
+ };
207
+ };
208
+ }
209
+
210
+ export declare interface AiMenu {
211
+ icon: string;
212
+ name: string;
213
+ prompt?: string;
214
+ text?: 'selected' | 'focusBefore';
215
+ model?: string;
216
+ children?: AiMenu[];
217
+ }
218
+
219
+ export declare interface AiMessage {
220
+ role: string;
221
+ content: string;
222
+ index: number;
223
+ status: 0 | 1 | 2;
224
+ }
225
+
226
+ export declare interface AiMessageListener {
227
+ onStart: (aiClient: AiClient) => void;
228
+ onStop: () => void;
229
+ onMessage: (message: AiMessage) => void;
230
+ }
231
+
232
+ declare abstract class AiModel {
233
+ editor: InnerEditor;
234
+ globalConfig: AiGlobalConfig;
235
+ aiModelName: string;
236
+ aiModelConfig: AiModelConfig;
237
+ protected constructor(editor: InnerEditor, globalConfig: AiGlobalConfig, aiModelName: string);
238
+ chatWithPayload(payload: any, listener: AiMessageListener): void;
239
+ chat(selectedText: string, prompt: string, listener: AiMessageListener): void;
240
+ /**
241
+ * 创建客户端链接 URL
242
+ */
243
+ abstract createAiClientUrl(): string;
244
+ /**
245
+ * 创建客户端
246
+ */
247
+ abstract createAiClient(url: string, listener: AiMessageListener): AiClient;
248
+ /**
249
+ * 封装消息,把 prompt 转换为协议需要的格式
250
+ * @param prompt
251
+ */
252
+ abstract wrapPayload(prompt: string): any;
253
+ }
254
+
255
+ declare interface AiModelConfig {
256
+ temperature?: number;
257
+ maxTokens?: number;
258
+ }
259
+
260
+ export declare interface AiModelFactory {
261
+ create: (name: string, editor: Editor, globalConfig: AiGlobalConfig) => AiModel;
262
+ }
263
+
264
+ export declare class AiModelManager {
265
+ private static models;
266
+ static init(editor: InnerEditor, globalConfig: AiGlobalConfig): void;
267
+ static get(modelName: string): AiModel;
268
+ static set(modelName: string, aiModel: AiModel): void;
269
+ }
270
+
271
+ export declare type BubbleMenuItem = {
272
+ id: string;
273
+ title?: string;
274
+ icon: string;
275
+ holder?: any;
276
+ onInit?: (editor: AiEditor, tippyInstance: Instance, parentEle: HTMLElement) => any;
277
+ onClick?: (editor: AiEditor, tippyInstance: Instance, parentEle: HTMLElement, holder: any) => void;
278
+ };
279
+
280
+ export declare class CustomAiModel extends AiModel {
281
+ constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
282
+ createAiClient(url: string, listener: AiMessageListener): AiClient;
283
+ wrapPayload(promptMessage: string): string;
284
+ createAiClientUrl(): string;
285
+ }
286
+
287
+ export declare interface CustomAiModelConfig extends AiModelConfig {
288
+ url: (() => string) | string;
289
+ method?: string;
290
+ headers?: () => Record<string, any> | undefined;
291
+ wrapPayload: (prompt: string) => string;
292
+ parseMessage: (bodyString: string) => AiMessage | undefined;
293
+ protocol: 'sse' | 'websocket' | 'http';
294
+ }
295
+
296
+ export declare interface CustomMenu {
297
+ id?: string;
298
+ className?: string;
299
+ icon?: string;
300
+ html?: string;
301
+ tip?: string;
302
+ onClick?: (event: MouseEvent, editor: AiEditor) => void;
303
+ onCreate?: (button: HTMLElement, editor: AiEditor) => void;
304
+ }
305
+
306
+ declare type DefaultToolbarKey = (typeof defaultToolbarKeys)[number];
307
+
308
+ declare const defaultToolbarKeys: string[];
309
+
310
+ declare class Footer extends HTMLElement implements AiEditorEventListener {
311
+ count: number;
312
+ draggable: boolean;
313
+ constructor();
314
+ initDraggable(draggable?: boolean): void;
315
+ updateCharacters(): void;
316
+ onCreate(props: EditorEvents['create'], _: AiEditorOptions): void;
317
+ onTransaction(props: EditorEvents['transaction']): void;
318
+ onEditableChange(editable: boolean): void;
319
+ }
320
+
321
+ declare class Header extends HTMLElement implements AiEditorEventListener {
322
+ menuButtons: AbstractMenuButton[];
323
+ constructor();
324
+ connectedCallback(): void;
325
+ onCreate(event: EditorEvents['create'], options: AiEditorOptions): void;
326
+ onTransaction(event: EditorEvents['transaction']): void;
327
+ onEditableChange(editable: boolean): void;
328
+ }
329
+
330
+ export declare interface HtmlPasteConfig {
331
+ pasteAsText?: boolean;
332
+ pasteClean?: boolean;
333
+ removeEmptyParagraphs?: boolean;
334
+ pasteProcessor?: (html: string) => string;
335
+ }
336
+
337
+ export declare class InnerEditor extends Editor {
338
+ aiEditor: AiEditor;
339
+ constructor(aiEditor: AiEditor, options?: Partial<EditorOptions>);
340
+ parseHtml(html: string): Fragment;
341
+ parseMarkdown(markdown: string): Fragment;
342
+ insertMarkdown(markdown: string): void;
343
+ }
344
+
345
+ declare type LanguageItem = {
346
+ name: string;
347
+ value: string;
348
+ alias?: string[];
349
+ };
350
+
351
+ export declare interface MenuGroup {
352
+ title?: string;
353
+ icon?: string;
354
+ toolbarKeys: (string | CustomMenu | MenuGroup)[];
355
+ }
356
+
357
+ export declare interface NameAndValue {
358
+ name: string;
359
+ value: any;
360
+ }
361
+
362
+ export declare class OpenaiAiModel extends AiModel {
363
+ constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
364
+ createAiClient(url: string, listener: AiMessageListener): AiClient;
365
+ wrapPayload(prompt: string): string;
366
+ createAiClientUrl(): string;
367
+ }
368
+
369
+ export declare interface OpenaiModelConfig extends AiModelConfig {
370
+ endpoint?: string;
371
+ customUrl?: string | (() => string);
372
+ apiKey?: string;
373
+ model?: string;
374
+ }
375
+
376
+ export declare class SparkAiModel extends AiModel {
377
+ constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
378
+ createAiClient(url: string, listener: AiMessageListener): AiClient;
379
+ wrapPayload(promptMessage: string): string;
380
+ getDomain(sparkAiModelConfig: SparkAiModelConfig): "4.0Ultra" | "generalv3.5" | "generalv3" | "generalv2" | "lite" | "general";
381
+ createAiClientUrl(): string;
382
+ }
383
+
384
+ export declare interface SparkAiModelConfig extends AiModelConfig {
385
+ appId: string;
386
+ apiKey: string;
387
+ apiSecret: string;
388
+ protocol?: string;
389
+ version?: string;
390
+ }
391
+
392
+ export declare type TranslateMenuItem = {
393
+ title: string;
394
+ language?: string;
395
+ } | string;
396
+
397
+ export declare type Uploader = (file: File, uploadUrl: string, headers: Record<string, any>, formName: string) => Promise<Record<string, any>>;
398
+
399
+ export declare interface UploaderEvent {
400
+ onUploadBefore?: (file: File, uploadUrl: string, headers: Record<string, any>) => void | boolean;
401
+ onSuccess?: (file: File, response: any) => any;
402
+ onFailed?: (file: File, response: any) => void;
403
+ onError?: (file: File, err: any) => void;
404
+ }
405
+
406
+ export declare class WenXinAiModel extends AiModel {
407
+ constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
408
+ createAiClient(url: string, listener: AiMessageListener): AiClient;
409
+ wrapPayload(prompt: string): string;
410
+ createAiClientUrl(): string;
411
+ }
412
+
413
+ export declare interface WenXinAiModelConfig extends AiModelConfig {
414
+ access_token: string;
415
+ protocol?: string;
416
+ version?: string;
417
+ }
418
+
419
+ export { }
420
+
421
+
422
+ declare module '@tiptap/core' {
423
+ interface Commands<ReturnType> {
424
+ attachment: {
425
+ uploadAttachment: (file: File) => ReturnType;
426
+ };
427
+ }
428
+ }
429
+
430
+
431
+ declare module '@tiptap/core' {
432
+ interface Commands<ReturnType> {
433
+ painter: {
434
+ setPainter: (marks: Mark[]) => ReturnType;
435
+ };
436
+ }
437
+ }
438
+
439
+
440
+ declare module '@tiptap/core' {
441
+ interface Commands<ReturnType> {
442
+ fontSize: {
443
+ /**
444
+ * Set the font family
445
+ */
446
+ setFontSize: (fontFamily: string) => ReturnType;
447
+ /**
448
+ * Unset the font family
449
+ */
450
+ unsetFontSize: () => ReturnType;
451
+ };
452
+ }
453
+ }
454
+
455
+
456
+ declare module '@tiptap/core' {
457
+ interface Commands<ReturnType> {
458
+ lineHeight: {
459
+ /**
460
+ * Set the line height attribute
461
+ */
462
+ setLineHeight: (height: string) => ReturnType;
463
+ /**
464
+ * Unset the text align attribute
465
+ */
466
+ unsetLineHeight: () => ReturnType;
467
+ };
468
+ }
469
+ }
470
+
471
+
472
+ declare module '@tiptap/core' {
473
+ interface Commands<ReturnType> {
474
+ indent: {
475
+ indent: () => ReturnType;
476
+ outdent: () => ReturnType;
477
+ };
478
+ }
479
+ }
480
+
481
+
482
+ declare module '@tiptap/core' {
483
+ interface Commands<ReturnType> {
484
+ Image: {
485
+ uploadImage: (file: File) => ReturnType;
486
+ };
487
+ }
488
+ }
489
+
490
+
491
+ declare module '@tiptap/core' {
492
+ interface Commands<ReturnType> {
493
+ CodeBlockExt: {
494
+ /**
495
+ * add comments
496
+ */
497
+ addCodeComments: (node: Node, pos: number) => ReturnType;
498
+ /**
499
+ * add explain
500
+ */
501
+ addCodeExplain: (node: Node, pos: number) => ReturnType;
502
+ };
503
+ }
504
+ }
505
+
506
+
507
+ declare module '@tiptap/core' {
508
+ interface Commands<ReturnType> {
509
+ video: {
510
+ setVideo: (src: string) => ReturnType;
511
+ toggleVideo: (src: string) => ReturnType;
512
+ uploadVideo: (file: File) => ReturnType;
513
+ };
514
+ }
515
+ }
516
+
517
+
518
+ declare module '@tiptap/core' {
519
+ interface Commands<ReturnType> {
520
+ iframe: {
521
+ setIframe: (options: {
522
+ src: string;
523
+ }) => ReturnType;
524
+ };
525
+ }
526
+ }
527
+
528
+
529
+ declare module '@tiptap/core' {
530
+ interface Commands<ReturnType> {
531
+ container: {
532
+ setContainer: (type: string) => ReturnType;
533
+ toggleContainer: (type: string) => ReturnType;
534
+ unsetContainer: () => ReturnType;
535
+ };
536
+ }
537
+ }
538
+
539
+
540
+ declare module '@tiptap/core' {
541
+ interface Commands<ReturnType> {
542
+ className: {
543
+ setClassName: (nodeType: string, className: string) => ReturnType;
544
+ };
545
+ }
546
+ }
547
+