@kerebron/extension-basic-editor 0.4.27 → 0.4.29

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.
Files changed (242) hide show
  1. package/esm/ExtensionBaseKeymap.d.ts +7 -0
  2. package/esm/ExtensionBaseKeymap.d.ts.map +1 -0
  3. package/esm/ExtensionBaseKeymap.js +56 -0
  4. package/esm/ExtensionBaseKeymap.js.map +1 -0
  5. package/esm/ExtensionBasicCodeEditor.d.ts +11 -0
  6. package/esm/ExtensionBasicCodeEditor.d.ts.map +1 -0
  7. package/esm/ExtensionBasicCodeEditor.js +62 -0
  8. package/esm/ExtensionBasicCodeEditor.js.map +1 -0
  9. package/esm/ExtensionBasicEditor.d.ts +49 -0
  10. package/esm/ExtensionBasicEditor.d.ts.map +1 -0
  11. package/esm/ExtensionBasicEditor.js +95 -0
  12. package/esm/ExtensionBasicEditor.js.map +1 -0
  13. package/esm/ExtensionDropcursor.d.ts +19 -0
  14. package/esm/ExtensionDropcursor.d.ts.map +1 -0
  15. package/esm/ExtensionDropcursor.js +187 -0
  16. package/esm/ExtensionDropcursor.js.map +1 -0
  17. package/esm/ExtensionGapcursor.d.ts +32 -0
  18. package/esm/ExtensionGapcursor.d.ts.map +1 -0
  19. package/esm/ExtensionGapcursor.js +250 -0
  20. package/esm/ExtensionGapcursor.js.map +1 -0
  21. package/esm/ExtensionHistory.d.ts +14 -0
  22. package/esm/ExtensionHistory.d.ts.map +1 -0
  23. package/esm/ExtensionHistory.js +35 -0
  24. package/esm/ExtensionHistory.js.map +1 -0
  25. package/esm/ExtensionHtml.d.ts +15 -0
  26. package/esm/ExtensionHtml.d.ts.map +1 -0
  27. package/esm/ExtensionHtml.js +100 -0
  28. package/esm/ExtensionHtml.js.map +1 -0
  29. package/esm/ExtensionMediaUpload.d.ts +24 -0
  30. package/esm/ExtensionMediaUpload.d.ts.map +1 -0
  31. package/esm/ExtensionMediaUpload.js +168 -0
  32. package/esm/ExtensionMediaUpload.js.map +1 -0
  33. package/esm/ExtensionSelection.d.ts +11 -0
  34. package/esm/ExtensionSelection.d.ts.map +1 -0
  35. package/esm/ExtensionSelection.js +230 -0
  36. package/esm/ExtensionSelection.js.map +1 -0
  37. package/esm/ExtensionTextAlign.d.ts +11 -0
  38. package/esm/ExtensionTextAlign.d.ts.map +1 -0
  39. package/esm/ExtensionTextAlign.js +40 -0
  40. package/esm/ExtensionTextAlign.js.map +1 -0
  41. package/esm/MarkBookmark.d.ts +8 -0
  42. package/esm/MarkBookmark.d.ts.map +1 -0
  43. package/esm/MarkBookmark.js +17 -0
  44. package/esm/MarkBookmark.js.map +1 -0
  45. package/esm/MarkChange.d.ts +8 -0
  46. package/esm/MarkChange.d.ts.map +1 -0
  47. package/esm/MarkChange.js +14 -0
  48. package/esm/MarkChange.js.map +1 -0
  49. package/esm/MarkCode.d.ts +11 -0
  50. package/esm/MarkCode.d.ts.map +1 -0
  51. package/esm/MarkCode.js +24 -0
  52. package/esm/MarkCode.js.map +1 -0
  53. package/esm/MarkHighlight.d.ts +8 -0
  54. package/esm/MarkHighlight.d.ts.map +1 -0
  55. package/esm/MarkHighlight.js +36 -0
  56. package/esm/MarkHighlight.js.map +1 -0
  57. package/esm/MarkItalic.d.ts +11 -0
  58. package/esm/MarkItalic.d.ts.map +1 -0
  59. package/esm/MarkItalic.js +30 -0
  60. package/esm/MarkItalic.js.map +1 -0
  61. package/esm/MarkLink.d.ts +8 -0
  62. package/esm/MarkLink.d.ts.map +1 -0
  63. package/esm/MarkLink.js +30 -0
  64. package/esm/MarkLink.js.map +1 -0
  65. package/esm/MarkStrike.d.ts +11 -0
  66. package/esm/MarkStrike.d.ts.map +1 -0
  67. package/esm/MarkStrike.js +27 -0
  68. package/esm/MarkStrike.js.map +1 -0
  69. package/esm/MarkStrong.d.ts +11 -0
  70. package/esm/MarkStrong.d.ts.map +1 -0
  71. package/esm/MarkStrong.js +39 -0
  72. package/esm/MarkStrong.js.map +1 -0
  73. package/esm/MarkSubscript.d.ts +11 -0
  74. package/esm/MarkSubscript.d.ts.map +1 -0
  75. package/esm/MarkSubscript.js +31 -0
  76. package/esm/MarkSubscript.js.map +1 -0
  77. package/esm/MarkSuperscript.d.ts +11 -0
  78. package/esm/MarkSuperscript.d.ts.map +1 -0
  79. package/esm/MarkSuperscript.js +31 -0
  80. package/esm/MarkSuperscript.js.map +1 -0
  81. package/esm/MarkTextColor.d.ts +8 -0
  82. package/esm/MarkTextColor.d.ts.map +1 -0
  83. package/esm/MarkTextColor.js +28 -0
  84. package/esm/MarkTextColor.js.map +1 -0
  85. package/esm/MarkUnderline.d.ts +11 -0
  86. package/esm/MarkUnderline.d.ts.map +1 -0
  87. package/esm/MarkUnderline.js +34 -0
  88. package/esm/MarkUnderline.js.map +1 -0
  89. package/esm/NodeAside.d.ts +8 -0
  90. package/esm/NodeAside.d.ts.map +1 -0
  91. package/esm/NodeAside.js +17 -0
  92. package/esm/NodeAside.js.map +1 -0
  93. package/esm/NodeBlockquote.d.ts +13 -0
  94. package/esm/NodeBlockquote.d.ts.map +1 -0
  95. package/esm/NodeBlockquote.js +35 -0
  96. package/esm/NodeBlockquote.js.map +1 -0
  97. package/esm/NodeBookmark.d.ts +8 -0
  98. package/esm/NodeBookmark.d.ts.map +1 -0
  99. package/esm/NodeBookmark.js +20 -0
  100. package/esm/NodeBookmark.js.map +1 -0
  101. package/esm/NodeBulletList.d.ts +13 -0
  102. package/esm/NodeBulletList.d.ts.map +1 -0
  103. package/esm/NodeBulletList.js +35 -0
  104. package/esm/NodeBulletList.js.map +1 -0
  105. package/esm/NodeCodeBlock.d.ts +9 -0
  106. package/esm/NodeCodeBlock.d.ts.map +1 -0
  107. package/esm/NodeCodeBlock.js +51 -0
  108. package/esm/NodeCodeBlock.js.map +1 -0
  109. package/esm/NodeDefinitionDesc.d.ts +8 -0
  110. package/esm/NodeDefinitionDesc.d.ts.map +1 -0
  111. package/esm/NodeDefinitionDesc.js +17 -0
  112. package/esm/NodeDefinitionDesc.js.map +1 -0
  113. package/esm/NodeDefinitionList.d.ts +13 -0
  114. package/esm/NodeDefinitionList.d.ts.map +1 -0
  115. package/esm/NodeDefinitionList.js +29 -0
  116. package/esm/NodeDefinitionList.js.map +1 -0
  117. package/esm/NodeDefinitionTerm.d.ts +8 -0
  118. package/esm/NodeDefinitionTerm.d.ts.map +1 -0
  119. package/esm/NodeDefinitionTerm.js +17 -0
  120. package/esm/NodeDefinitionTerm.js.map +1 -0
  121. package/esm/NodeDocument.d.ts +7 -0
  122. package/esm/NodeDocument.d.ts.map +1 -0
  123. package/esm/NodeDocument.js +20 -0
  124. package/esm/NodeDocument.js.map +1 -0
  125. package/esm/NodeDocumentCode.d.ts +7 -0
  126. package/esm/NodeDocumentCode.d.ts.map +1 -0
  127. package/esm/NodeDocumentCode.js +30 -0
  128. package/esm/NodeDocumentCode.js.map +1 -0
  129. package/esm/NodeFrontmatter.d.ts +8 -0
  130. package/esm/NodeFrontmatter.d.ts.map +1 -0
  131. package/esm/NodeFrontmatter.js +17 -0
  132. package/esm/NodeFrontmatter.js.map +1 -0
  133. package/esm/NodeHardBreak.d.ts +14 -0
  134. package/esm/NodeHardBreak.d.ts.map +1 -0
  135. package/esm/NodeHardBreak.js +68 -0
  136. package/esm/NodeHardBreak.js.map +1 -0
  137. package/esm/NodeHeading.d.ts +13 -0
  138. package/esm/NodeHeading.d.ts.map +1 -0
  139. package/esm/NodeHeading.js +50 -0
  140. package/esm/NodeHeading.js.map +1 -0
  141. package/esm/NodeHorizontalRule.d.ts +11 -0
  142. package/esm/NodeHorizontalRule.d.ts.map +1 -0
  143. package/esm/NodeHorizontalRule.js +30 -0
  144. package/esm/NodeHorizontalRule.js.map +1 -0
  145. package/esm/NodeImage.d.ts +8 -0
  146. package/esm/NodeImage.d.ts.map +1 -0
  147. package/esm/NodeImage.js +34 -0
  148. package/esm/NodeImage.js.map +1 -0
  149. package/esm/NodeInlineShortCode.d.ts +11 -0
  150. package/esm/NodeInlineShortCode.d.ts.map +1 -0
  151. package/esm/NodeInlineShortCode.js +40 -0
  152. package/esm/NodeInlineShortCode.js.map +1 -0
  153. package/esm/NodeListItem.d.ts +14 -0
  154. package/esm/NodeListItem.d.ts.map +1 -0
  155. package/esm/NodeListItem.js +202 -0
  156. package/esm/NodeListItem.js.map +1 -0
  157. package/esm/NodeMath.d.ts +8 -0
  158. package/esm/NodeMath.d.ts.map +1 -0
  159. package/esm/NodeMath.js +100 -0
  160. package/esm/NodeMath.js.map +1 -0
  161. package/esm/NodeOrderedList.d.ts +23 -0
  162. package/esm/NodeOrderedList.d.ts.map +1 -0
  163. package/esm/NodeOrderedList.js +53 -0
  164. package/esm/NodeOrderedList.js.map +1 -0
  165. package/esm/NodeParagraph.d.ts +11 -0
  166. package/esm/NodeParagraph.d.ts.map +1 -0
  167. package/esm/NodeParagraph.js +45 -0
  168. package/esm/NodeParagraph.js.map +1 -0
  169. package/esm/NodeTaskItem.d.ts +24 -0
  170. package/esm/NodeTaskItem.d.ts.map +1 -0
  171. package/esm/NodeTaskItem.js +147 -0
  172. package/esm/NodeTaskItem.js.map +1 -0
  173. package/esm/NodeTaskList.d.ts +11 -0
  174. package/esm/NodeTaskList.d.ts.map +1 -0
  175. package/esm/NodeTaskList.js +26 -0
  176. package/esm/NodeTaskList.js.map +1 -0
  177. package/esm/NodeText.d.ts +7 -0
  178. package/esm/NodeText.d.ts.map +1 -0
  179. package/esm/NodeText.js +10 -0
  180. package/esm/NodeText.js.map +1 -0
  181. package/esm/NodeVideo.d.ts +8 -0
  182. package/esm/NodeVideo.d.ts.map +1 -0
  183. package/esm/NodeVideo.js +46 -0
  184. package/esm/NodeVideo.js.map +1 -0
  185. package/esm/package.json +3 -0
  186. package/esm/remote-selection/ExtensionRemoteSelection.d.ts +24 -0
  187. package/esm/remote-selection/ExtensionRemoteSelection.d.ts.map +1 -0
  188. package/esm/remote-selection/ExtensionRemoteSelection.js +23 -0
  189. package/esm/remote-selection/ExtensionRemoteSelection.js.map +1 -0
  190. package/esm/remote-selection/remoteSelectionPlugin.d.ts +25 -0
  191. package/esm/remote-selection/remoteSelectionPlugin.d.ts.map +1 -0
  192. package/esm/remote-selection/remoteSelectionPlugin.js +97 -0
  193. package/esm/remote-selection/remoteSelectionPlugin.js.map +1 -0
  194. package/package.json +6 -3
  195. package/src/ExtensionBaseKeymap.ts +64 -0
  196. package/src/ExtensionBasicCodeEditor.ts +82 -0
  197. package/src/ExtensionBasicEditor.ts +97 -0
  198. package/src/ExtensionDropcursor.ts +221 -0
  199. package/src/ExtensionGapcursor.ts +278 -0
  200. package/src/ExtensionHistory.ts +48 -0
  201. package/src/ExtensionHtml.ts +158 -0
  202. package/src/ExtensionMediaUpload.ts +258 -0
  203. package/src/ExtensionSelection.ts +379 -0
  204. package/src/ExtensionTextAlign.ts +50 -0
  205. package/src/MarkBookmark.ts +20 -0
  206. package/src/MarkChange.ts +17 -0
  207. package/src/MarkCode.ts +35 -0
  208. package/src/MarkHighlight.ts +38 -0
  209. package/src/MarkItalic.ts +41 -0
  210. package/src/MarkLink.ts +32 -0
  211. package/src/MarkStrike.ts +38 -0
  212. package/src/MarkStrong.ts +52 -0
  213. package/src/MarkSubscript.ts +42 -0
  214. package/src/MarkSuperscript.ts +42 -0
  215. package/src/MarkTextColor.ts +29 -0
  216. package/src/MarkUnderline.ts +47 -0
  217. package/src/NodeAside.ts +19 -0
  218. package/src/NodeBlockquote.ts +51 -0
  219. package/src/NodeBookmark.ts +23 -0
  220. package/src/NodeBulletList.ts +51 -0
  221. package/src/NodeCodeBlock.ts +60 -0
  222. package/src/NodeDefinitionDesc.ts +19 -0
  223. package/src/NodeDefinitionList.ts +46 -0
  224. package/src/NodeDefinitionTerm.ts +19 -0
  225. package/src/NodeDocument.ts +22 -0
  226. package/src/NodeDocumentCode.ts +33 -0
  227. package/src/NodeFrontmatter.ts +19 -0
  228. package/src/NodeHardBreak.ts +92 -0
  229. package/src/NodeHeading.ts +76 -0
  230. package/src/NodeHorizontalRule.ts +43 -0
  231. package/src/NodeImage.ts +36 -0
  232. package/src/NodeInlineShortCode.ts +55 -0
  233. package/src/NodeListItem.ts +320 -0
  234. package/src/NodeMath.ts +109 -0
  235. package/src/NodeOrderedList.ts +79 -0
  236. package/src/NodeParagraph.ts +60 -0
  237. package/src/NodeTaskItem.ts +190 -0
  238. package/src/NodeTaskList.ts +38 -0
  239. package/src/NodeText.ts +12 -0
  240. package/src/NodeVideo.ts +44 -0
  241. package/src/remote-selection/ExtensionRemoteSelection.ts +45 -0
  242. package/src/remote-selection/remoteSelectionPlugin.ts +157 -0
@@ -0,0 +1,48 @@
1
+ import { history, redo, undo } from 'prosemirror-history';
2
+ import { Plugin } from 'prosemirror-state';
3
+
4
+ import { type CoreEditor, Extension } from '@kerebron/editor';
5
+ import {
6
+ type Command,
7
+ type CommandFactories,
8
+ type CommandShortcuts,
9
+ } from '@kerebron/editor/commands';
10
+
11
+ export class ExtensionHistory extends Extension {
12
+ name = 'history';
13
+
14
+ options = {
15
+ depth: 100,
16
+ newGroupDelay: 500,
17
+ };
18
+
19
+ override getCommandFactories(editor: CoreEditor): Partial<CommandFactories> {
20
+ return {
21
+ 'undo': () => undo,
22
+ 'redo': () => redo,
23
+ };
24
+ }
25
+
26
+ override getKeyboardShortcuts(): Partial<CommandShortcuts> {
27
+ // https://stackoverflow.com/a/73619128
28
+ const mac = typeof navigator != 'undefined'
29
+ ? /Mac|iP(hone|[oa]d)/.test(navigator?.platform)
30
+ : false;
31
+
32
+ const shortcuts = {
33
+ 'Mod-z': 'undo',
34
+ 'Mod-y': 'redo',
35
+ };
36
+ if (!mac) {
37
+ shortcuts['Mod-y'] = 'redo';
38
+ }
39
+
40
+ return shortcuts;
41
+ }
42
+
43
+ override getProseMirrorPlugins(): Plugin[] {
44
+ return [
45
+ history(this.options),
46
+ ];
47
+ }
48
+ }
@@ -0,0 +1,158 @@
1
+ import {
2
+ DOMParser,
3
+ DOMSerializer,
4
+ Fragment,
5
+ Node,
6
+ type ParseOptions,
7
+ Schema,
8
+ } from 'prosemirror-model';
9
+
10
+ import { type Converter, type CoreEditor, Extension } from '@kerebron/editor';
11
+
12
+ export type CreateNodeFromContentOptions = {
13
+ parseOptions?: ParseOptions;
14
+ errorOnInvalidContent?: boolean;
15
+ };
16
+
17
+ export function getHTMLFromFragment(
18
+ fragment: Fragment,
19
+ schema: Schema,
20
+ ): string {
21
+ const document = globalThis.document;
22
+ const documentFragment = DOMSerializer.fromSchema(schema).serializeFragment(
23
+ fragment,
24
+ { document },
25
+ );
26
+
27
+ const temporaryDocument = document.implementation.createHTMLDocument();
28
+ const container = temporaryDocument.createElement('div');
29
+
30
+ container.appendChild(documentFragment);
31
+
32
+ return container.innerHTML;
33
+ }
34
+
35
+ const removeWhitespaces = (node: HTMLElement) => {
36
+ const children = node.childNodes;
37
+
38
+ for (let i = children.length - 1; i >= 0; i -= 1) {
39
+ const child = children[i];
40
+
41
+ if (
42
+ child.nodeType === 3 && child.nodeValue &&
43
+ /^(\n\s\s|\n)$/.test(child.nodeValue)
44
+ ) {
45
+ node.removeChild(child);
46
+ } else if (child.nodeType === 1) {
47
+ removeWhitespaces(child as HTMLElement);
48
+ }
49
+ }
50
+
51
+ return node;
52
+ };
53
+
54
+ export function elementFromString(value: string): HTMLElement {
55
+ // add a wrapper to preserve leading and trailing whitespace
56
+ const wrappedValue = `<html lang="en"><body>${value}</body></html>`;
57
+
58
+ const body =
59
+ new globalThis.DOMParser().parseFromString(wrappedValue, 'text/html').body;
60
+
61
+ return removeWhitespaces(body);
62
+ }
63
+
64
+ function prepareContentCheckSchema(schema: Schema): Schema {
65
+ const contentCheckSchema = new Schema({
66
+ topNode: schema.spec.topNode,
67
+ marks: schema.spec.marks,
68
+ // Prosemirror's schemas are executed such that: the last to execute, matches last
69
+ // This means that we can add a catch-all node at the end of the schema to catch any content that we don't know how to handle
70
+ nodes: schema.spec.nodes.append({
71
+ __unknown__catch__all__node: {
72
+ content: 'inline*',
73
+ group: 'block',
74
+ parseDOM: [
75
+ {
76
+ tag: '*',
77
+ getAttrs: (e) => {
78
+ // Try to stringify the element for a more helpful error message
79
+ const invalidContent = typeof e === 'string' ? e : e.outerHTML;
80
+ throw new Error('Invalid HTML content', {
81
+ cause: new Error(`Invalid element found: ${invalidContent}`),
82
+ });
83
+ },
84
+ },
85
+ ],
86
+ },
87
+ }),
88
+ });
89
+
90
+ return contentCheckSchema;
91
+ }
92
+
93
+ export function createNodeFromHTML(
94
+ content: string,
95
+ schema: Schema,
96
+ options?: CreateNodeFromContentOptions,
97
+ ): Node {
98
+ options = {
99
+ parseOptions: {},
100
+ ...options,
101
+ };
102
+
103
+ if (options.errorOnInvalidContent) {
104
+ const contentCheckSchema = prepareContentCheckSchema(schema);
105
+ DOMParser.fromSchema(contentCheckSchema).parse(
106
+ elementFromString(content),
107
+ options.parseOptions,
108
+ );
109
+ }
110
+
111
+ const parser = DOMParser.fromSchema(schema);
112
+ return parser.parse(elementFromString(content), options.parseOptions);
113
+ }
114
+
115
+ export function createFragmentFromHTML(
116
+ content: string,
117
+ schema: Schema,
118
+ options?: CreateNodeFromContentOptions,
119
+ ): Fragment {
120
+ options = {
121
+ parseOptions: {},
122
+ ...options,
123
+ };
124
+
125
+ if (options.errorOnInvalidContent) {
126
+ const contentCheckSchema = prepareContentCheckSchema(schema);
127
+ DOMParser.fromSchema(contentCheckSchema).parseSlice(
128
+ elementFromString(content),
129
+ options.parseOptions,
130
+ );
131
+ }
132
+
133
+ const parser = DOMParser.fromSchema(schema);
134
+ return parser.parseSlice(elementFromString(content), options.parseOptions)
135
+ .content;
136
+ }
137
+
138
+ export class ExtensionHtml extends Extension {
139
+ name = 'html';
140
+
141
+ override getConverters(
142
+ editor: CoreEditor,
143
+ schema: Schema,
144
+ ): Record<string, Converter> {
145
+ return {
146
+ 'text/html': {
147
+ fromDoc: async (document: Node): Promise<Uint8Array> => {
148
+ const html = getHTMLFromFragment(document.content, editor.schema);
149
+ return new TextEncoder().encode(html);
150
+ },
151
+ toDoc: async (buffer: Uint8Array): Promise<Node> => {
152
+ const html = new TextDecoder().decode(buffer);
153
+ return createNodeFromHTML(html, editor.schema);
154
+ },
155
+ },
156
+ };
157
+ }
158
+ }
@@ -0,0 +1,258 @@
1
+ import { Plugin, PluginKey } from 'prosemirror-state';
2
+ import { EditorView } from 'prosemirror-view';
3
+
4
+ import { Extension } from '@kerebron/editor';
5
+
6
+ export interface MediaUploadOptions {
7
+ /** Maximum file size in bytes (default: 10MB for images) */
8
+ maxFileSize?: number;
9
+
10
+ /** Maximum file size for videos (default: 50MB) */
11
+ maxVideoFileSize?: number;
12
+
13
+ /** Allowed image MIME types */
14
+ allowedImageTypes?: string[];
15
+
16
+ /** Allowed video MIME types */
17
+ allowedVideoTypes?: string[];
18
+
19
+ /** Use object URLs for videos instead of base64 (default: true) */
20
+ useObjectURLForVideos?: boolean;
21
+
22
+ /** Custom upload handler. Returns the URL of uploaded media. */
23
+ uploadHandler?: (file: File) => Promise<string>;
24
+ }
25
+
26
+ const mediaUploadKey = new PluginKey('mediaUpload');
27
+
28
+ /** Convert a File to a base64 data URL */
29
+ function fileToDataURL(file: File): Promise<string> {
30
+ return new Promise((resolve, reject) => {
31
+ const reader = new FileReader();
32
+ reader.onload = () => resolve(reader.result as string);
33
+ reader.onerror = reject;
34
+ reader.readAsDataURL(file);
35
+ });
36
+ }
37
+
38
+ /** Convert a File to an object URL (better for videos) */
39
+ function fileToObjectURL(file: File): string {
40
+ return URL.createObjectURL(file);
41
+ }
42
+
43
+ /** Check if a file is an image */
44
+ function isImage(file: File, allowedTypes: string[]): boolean {
45
+ return allowedTypes.some((type) => file.type.match(type));
46
+ }
47
+
48
+ /** Check if a file is a video */
49
+ function isVideo(file: File, allowedTypes: string[]): boolean {
50
+ return allowedTypes.some((type) => file.type.match(type));
51
+ }
52
+
53
+ /** Insert an image into the editor at the given position */
54
+ function insertImage(
55
+ view: EditorView,
56
+ pos: number,
57
+ src: string,
58
+ alt?: string,
59
+ title?: string,
60
+ ) {
61
+ const { schema } = view.state;
62
+ const imageType = schema.nodes.image;
63
+
64
+ if (!imageType) {
65
+ console.warn('Image node type not found in schema');
66
+ return;
67
+ }
68
+
69
+ const node = imageType.create({ src, alt, title });
70
+ const transaction = view.state.tr.insert(pos, node);
71
+ view.dispatch(transaction);
72
+ }
73
+
74
+ /** Insert a video into the editor at the given position */
75
+ function insertVideo(
76
+ view: EditorView,
77
+ pos: number,
78
+ src: string,
79
+ title?: string,
80
+ width?: string,
81
+ height?: string,
82
+ ) {
83
+ const { schema } = view.state;
84
+ const videoType = schema.nodes.video;
85
+
86
+ if (!videoType) {
87
+ console.warn('Video node type not found in schema');
88
+ return;
89
+ }
90
+
91
+ const node = videoType.create({ src, title, width, height, controls: true });
92
+ const transaction = view.state.tr.insert(pos, node);
93
+ view.dispatch(transaction);
94
+ }
95
+
96
+ /** Handle media files (images and videos) from drop or paste events */
97
+ async function handleMediaFiles(
98
+ view: EditorView,
99
+ files: File[],
100
+ pos: number,
101
+ options: MediaUploadOptions,
102
+ ): Promise<void> {
103
+ const {
104
+ maxFileSize = 10 * 1024 * 1024, // 10MB for images
105
+ maxVideoFileSize = 50 * 1024 * 1024, // 50MB for videos
106
+ allowedImageTypes = ['^image/'],
107
+ allowedVideoTypes = ['^video/'],
108
+ uploadHandler,
109
+ } = options;
110
+
111
+ for (const file of files) {
112
+ const isImageFile = isImage(file, allowedImageTypes);
113
+ const isVideoFile = isVideo(file, allowedVideoTypes);
114
+
115
+ // Skip if not an image or video
116
+ if (!isImageFile && !isVideoFile) {
117
+ continue;
118
+ }
119
+
120
+ // Check file size
121
+ const sizeLimit = isVideoFile ? maxVideoFileSize : maxFileSize;
122
+ if (file.size > sizeLimit) {
123
+ console.warn(
124
+ `${isVideoFile ? 'Video' : 'Image'} file "${file.name}" is too large (${
125
+ (file.size / 1024 / 1024).toFixed(2)
126
+ }MB). Maximum size is ${(sizeLimit / 1024 / 1024).toFixed(2)}MB.`,
127
+ );
128
+ continue;
129
+ }
130
+
131
+ try {
132
+ // Upload or convert to data URL
133
+ console.log(
134
+ `Processing ${isVideoFile ? 'video' : 'image'}: ${file.name} (${
135
+ (file.size / 1024 / 1024).toFixed(2)
136
+ }MB, ${file.type})`,
137
+ );
138
+
139
+ let src: string;
140
+ if (uploadHandler) {
141
+ src = await uploadHandler(file);
142
+ } else if (isVideoFile && options.useObjectURLForVideos !== false) {
143
+ // Use object URL for videos (better performance, doesn't bloat the document)
144
+ src = fileToObjectURL(file);
145
+ console.log('Using object URL for video:', src);
146
+ } else {
147
+ // Use base64 data URL
148
+ src = await fileToDataURL(file);
149
+ console.log(
150
+ `${
151
+ isVideoFile ? 'Video' : 'Image'
152
+ } converted to data URL, length: ${src.length} characters`,
153
+ );
154
+ }
155
+
156
+ // Insert the media
157
+ if (isVideoFile) {
158
+ insertVideo(view, pos, src, file.name);
159
+ console.log('Video inserted into editor');
160
+ } else {
161
+ insertImage(view, pos, src, file.name);
162
+ console.log('Image inserted into editor');
163
+ }
164
+
165
+ // Increment position for next media
166
+ pos += 1;
167
+ } catch (error) {
168
+ console.error(
169
+ `Failed to process ${isVideoFile ? 'video' : 'image'} "${file.name}":`,
170
+ error,
171
+ );
172
+ }
173
+ }
174
+ }
175
+
176
+ /** Create the media upload plugin */
177
+ function createMediaUploadPlugin(options: MediaUploadOptions = {}): Plugin {
178
+ return new Plugin({
179
+ key: mediaUploadKey,
180
+
181
+ props: {
182
+ /** Handle file drops */
183
+ handleDrop(view, event, slice, moved) {
184
+ // If content was moved from within the editor, let the default handler deal with it
185
+ if (moved) return false;
186
+
187
+ const files = Array.from(event.dataTransfer?.files || []);
188
+ if (files.length === 0) return false;
189
+
190
+ // Check if any files are images or videos
191
+ const {
192
+ allowedImageTypes = ['^image/'],
193
+ allowedVideoTypes = ['^video/'],
194
+ } = options;
195
+ const hasMedia = files.some((file) =>
196
+ isImage(file, allowedImageTypes) || isVideo(file, allowedVideoTypes)
197
+ );
198
+ if (!hasMedia) return false;
199
+
200
+ // Prevent default drop behavior
201
+ event.preventDefault();
202
+
203
+ // Get drop position
204
+ const coords = { left: event.clientX, top: event.clientY };
205
+ const pos = view.posAtCoords(coords);
206
+ if (!pos) return false;
207
+
208
+ // Handle the media files
209
+ handleMediaFiles(view, files, pos.pos, options);
210
+
211
+ return true;
212
+ },
213
+
214
+ /** Handle paste events with images (videos typically don't paste) */
215
+ handlePaste(view, event, slice) {
216
+ const items = Array.from(event.clipboardData?.items || []);
217
+ const imageItems = items.filter((item) =>
218
+ item.type.startsWith('image/')
219
+ );
220
+
221
+ if (imageItems.length === 0) return false;
222
+
223
+ // Prevent default paste behavior
224
+ event.preventDefault();
225
+
226
+ // Convert clipboard items to files
227
+ const files: File[] = [];
228
+ for (const item of imageItems) {
229
+ const file = item.getAsFile();
230
+ if (file) files.push(file);
231
+ }
232
+
233
+ if (files.length === 0) return false;
234
+
235
+ // Get current cursor position
236
+ const { from } = view.state.selection;
237
+
238
+ // Handle the image files
239
+ handleMediaFiles(view, files, from, options);
240
+
241
+ return true;
242
+ },
243
+ },
244
+ });
245
+ }
246
+
247
+ /** Extension that adds media upload support via drag & drop and paste */
248
+ export class ExtensionMediaUpload extends Extension {
249
+ name = 'mediaUpload';
250
+
251
+ constructor(protected override config: Partial<MediaUploadOptions> = {}) {
252
+ super(config);
253
+ }
254
+
255
+ override getProseMirrorPlugins(): Plugin[] {
256
+ return [createMediaUploadPlugin(this.config)];
257
+ }
258
+ }