@fastkit/vui 0.7.56 → 0.7.61

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.
@@ -1,3 +1,4 @@
1
1
  export * from './VWysiwygEditor';
2
+ export * from './extensions';
2
3
  export * from './tools';
3
4
  export * from './schemes';
@@ -1,12 +1,116 @@
1
1
  import { type VuiService } from '../../service';
2
- import { type Editor, type Extensions } from '@tiptap/vue-3';
2
+ import {
3
+ type Editor,
4
+ type Extensions,
5
+ type AnyExtension,
6
+ type EditorOptions,
7
+ } from '@tiptap/vue-3';
3
8
  import { type IconName } from '../VIcon';
4
9
 
10
+ const EDITOR_EVENTS = [
11
+ 'beforeCreate',
12
+ 'create',
13
+ 'update',
14
+ 'selectionUpdate',
15
+ 'transaction',
16
+ 'focus',
17
+ 'blur',
18
+ 'destroy',
19
+ ] as const;
20
+
21
+ type PrefixedEventName<S extends string> = `on${Capitalize<S>}`;
22
+
23
+ const prefixedEventName = <S extends string>(
24
+ source: S,
25
+ ): PrefixedEventName<S> => {
26
+ return `on${source.charAt(0).toUpperCase()}${source.slice(1)}` as any;
27
+ };
28
+
29
+ export type WysiwygEditorEvent = typeof EDITOR_EVENTS[number];
30
+
31
+ export type WysiwygEditorPrefixedEvent = PrefixedEventName<WysiwygEditorEvent>;
32
+
33
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
34
+ export interface WysiwygEditorEventsOptions
35
+ extends Partial<Pick<EditorOptions, WysiwygEditorPrefixedEvent>> {}
36
+
37
+ export type WysiwygEditorEventsBucket = {
38
+ [EV in WysiwygEditorEvent]: NonNullable<
39
+ WysiwygEditorEventsOptions[PrefixedEventName<EV>]
40
+ >[];
41
+ };
42
+
43
+ export class WysiwygEditorInitializeContext {
44
+ readonly listeners: WysiwygEditorEventsBucket = {} as any;
45
+
46
+ constructor(opts: WysiwygEditorEventsOptions = {}) {
47
+ EDITOR_EVENTS.forEach((event) => {
48
+ this.listeners[event] = [];
49
+ const prefixed = prefixedEventName(event);
50
+ const fn = opts[prefixed];
51
+ fn && this.listeners[event].push(fn as any);
52
+ });
53
+ }
54
+
55
+ on<EV extends WysiwygEditorEvent>(
56
+ ev: EV,
57
+ handler: NonNullable<WysiwygEditorEventsOptions[PrefixedEventName<EV>]>,
58
+ ) {
59
+ this.listeners[ev].push(handler);
60
+ return () => this.off(ev, handler);
61
+ }
62
+
63
+ off<EV extends WysiwygEditorEvent>(
64
+ ev: EV,
65
+ handler: NonNullable<WysiwygEditorEventsOptions[PrefixedEventName<EV>]>,
66
+ ) {
67
+ this.listeners[ev] = this.listeners[ev].filter(
68
+ (_handler) => _handler !== handler,
69
+ ) as any;
70
+ }
71
+
72
+ editorOptions() {
73
+ const opts: WysiwygEditorEventsOptions = {};
74
+
75
+ EDITOR_EVENTS.forEach((event) => {
76
+ const prefixed = prefixedEventName(event);
77
+ opts[prefixed] = (props) => {
78
+ const handlers = this.listeners[event];
79
+ handlers.forEach((handler) => {
80
+ handler(props as any);
81
+ });
82
+ };
83
+ });
84
+
85
+ return opts;
86
+ }
87
+ }
88
+
5
89
  export interface WysiwygEditorContext {
6
90
  editor: Editor;
7
91
  vui: VuiService;
8
92
  }
9
93
 
94
+ export type WysiwygExtensionFactory = (
95
+ ctx: WysiwygEditorInitializeContext,
96
+ ) => AnyExtension;
97
+
98
+ export type RawWysiwygExtension = AnyExtension | WysiwygExtensionFactory;
99
+
100
+ function resolveRawWysiwygExtension(
101
+ raw: RawWysiwygExtension,
102
+ ctx: WysiwygEditorInitializeContext,
103
+ ): AnyExtension {
104
+ return typeof raw === 'function' ? raw(ctx) : raw;
105
+ }
106
+
107
+ export function resolveRawWysiwygExtensions(
108
+ raws: RawWysiwygExtension[],
109
+ ctx: WysiwygEditorInitializeContext,
110
+ ) {
111
+ return raws.map((raw) => resolveRawWysiwygExtension(raw, ctx));
112
+ }
113
+
10
114
  export interface WysiwygEditorTool {
11
115
  key: string;
12
116
  icon: IconName | ((ctx: WysiwygEditorContext) => IconName);
@@ -39,13 +143,13 @@ export interface ResolvedWysiwygEditorSettings {
39
143
  }
40
144
 
41
145
  export function resolveRawWysiwygEditorTools(
42
- raws: RawWysiwygEditorTool[],
146
+ rawTools: RawWysiwygEditorTool[],
43
147
  vui: VuiService,
44
148
  ): ResolvedWysiwygEditorSettings {
45
149
  const tools: WysiwygEditorTool[] = [];
46
150
  const extensions: Extensions = [];
47
151
 
48
- raws.forEach((raw) => {
152
+ rawTools.forEach((raw) => {
49
153
  let resolved = resolveRawWysiwygEditorTool(raw, vui);
50
154
  if (!Array.isArray(resolved)) {
51
155
  resolved = [resolved];