@blocknote/core 0.20.0 → 0.21.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.
Files changed (168) hide show
  1. package/dist/blocknote.js +1743 -1734
  2. package/dist/blocknote.js.map +1 -1
  3. package/dist/blocknote.umd.cjs +7 -7
  4. package/dist/blocknote.umd.cjs.map +1 -1
  5. package/dist/src/api/blockManipulation/commands/moveBlocks/moveBlocks.js +1 -1
  6. package/dist/src/api/blockManipulation/commands/moveBlocks/moveBlocks.js.map +1 -1
  7. package/dist/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.js +3 -3
  8. package/dist/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.js.map +1 -1
  9. package/dist/src/api/blockManipulation/commands/splitBlock/splitBlock.test.js +1 -1
  10. package/dist/src/api/blockManipulation/commands/splitBlock/splitBlock.test.js.map +1 -1
  11. package/dist/src/api/blockManipulation/selections/selection.js +4 -4
  12. package/dist/src/api/blockManipulation/selections/selection.js.map +1 -1
  13. package/dist/src/api/clipboard/clipboard.test.js +6 -3
  14. package/dist/src/api/clipboard/clipboard.test.js.map +1 -1
  15. package/dist/src/api/clipboard/fromClipboard/handleFileInsertion.js +1 -1
  16. package/dist/src/api/clipboard/fromClipboard/handleFileInsertion.js.map +1 -1
  17. package/dist/src/api/clipboard/fromClipboard/handleVSCodePaste.js +2 -3
  18. package/dist/src/api/clipboard/fromClipboard/handleVSCodePaste.js.map +1 -1
  19. package/dist/src/api/clipboard/fromClipboard/pasteExtension.js +5 -5
  20. package/dist/src/api/clipboard/fromClipboard/pasteExtension.js.map +1 -1
  21. package/dist/src/api/clipboard/toClipboard/copyExtension.js +4 -2
  22. package/dist/src/api/clipboard/toClipboard/copyExtension.js.map +1 -1
  23. package/dist/src/api/parsers/markdown/parseMarkdown.test.js +4 -1
  24. package/dist/src/api/parsers/markdown/parseMarkdown.test.js.map +1 -1
  25. package/dist/src/blocks/AudioBlockContent/AudioBlockContent.js +14 -7
  26. package/dist/src/blocks/AudioBlockContent/AudioBlockContent.js.map +1 -1
  27. package/dist/src/blocks/AudioBlockContent/{audioBlockHelpers.js → parseAudioElement.js} +1 -1
  28. package/dist/src/blocks/AudioBlockContent/parseAudioElement.js.map +1 -0
  29. package/dist/src/blocks/FileBlockContent/FileBlockContent.js +5 -4
  30. package/dist/src/blocks/FileBlockContent/FileBlockContent.js.map +1 -1
  31. package/dist/src/blocks/FileBlockContent/helpers/parse/parseEmbedElement.js +5 -0
  32. package/dist/src/blocks/FileBlockContent/helpers/parse/parseEmbedElement.js.map +1 -0
  33. package/dist/src/blocks/FileBlockContent/helpers/parse/parseFigureElement.js +10 -0
  34. package/dist/src/blocks/FileBlockContent/helpers/parse/parseFigureElement.js.map +1 -0
  35. package/dist/src/blocks/FileBlockContent/helpers/render/createAddFileButton.js +39 -0
  36. package/dist/src/blocks/FileBlockContent/helpers/render/createAddFileButton.js.map +1 -0
  37. package/dist/src/blocks/FileBlockContent/helpers/render/createFileBlockWrapper.js +51 -0
  38. package/dist/src/blocks/FileBlockContent/helpers/render/createFileBlockWrapper.js.map +1 -0
  39. package/dist/src/blocks/FileBlockContent/helpers/render/createFileNameWithIcon.js +17 -0
  40. package/dist/src/blocks/FileBlockContent/helpers/render/createFileNameWithIcon.js.map +1 -0
  41. package/dist/src/blocks/FileBlockContent/helpers/render/createResizableFileBlockWrapper.js +147 -0
  42. package/dist/src/blocks/FileBlockContent/helpers/render/createResizableFileBlockWrapper.js.map +1 -0
  43. package/dist/src/blocks/FileBlockContent/helpers/toExternalHTML/createFigureWithCaption.js +9 -0
  44. package/dist/src/blocks/FileBlockContent/helpers/toExternalHTML/createFigureWithCaption.js.map +1 -0
  45. package/dist/src/blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.js +11 -0
  46. package/dist/src/blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.js.map +1 -0
  47. package/dist/src/blocks/ImageBlockContent/ImageBlockContent.js +17 -9
  48. package/dist/src/blocks/ImageBlockContent/ImageBlockContent.js.map +1 -1
  49. package/dist/src/blocks/ImageBlockContent/{imageBlockHelpers.js → parseImageElement.js} +1 -1
  50. package/dist/src/blocks/ImageBlockContent/parseImageElement.js.map +1 -0
  51. package/dist/src/blocks/VideoBlockContent/VideoBlockContent.js +18 -7
  52. package/dist/src/blocks/VideoBlockContent/VideoBlockContent.js.map +1 -1
  53. package/dist/src/blocks/VideoBlockContent/{videoBlockHelpers.js → parseVideoElement.js} +1 -1
  54. package/dist/src/blocks/VideoBlockContent/parseVideoElement.js.map +1 -0
  55. package/dist/src/editor/BlockNoteEditor.js +8 -5
  56. package/dist/src/editor/BlockNoteEditor.js.map +1 -1
  57. package/dist/src/editor/BlockNoteExtensions.js +4 -8
  58. package/dist/src/editor/BlockNoteExtensions.js.map +1 -1
  59. package/dist/src/extensions/LinkToolbar/protocols.js +14 -0
  60. package/dist/src/extensions/LinkToolbar/protocols.js.map +1 -0
  61. package/dist/src/extensions/Placeholder/PlaceholderPlugin.js +6 -6
  62. package/dist/src/extensions/Placeholder/PlaceholderPlugin.js.map +1 -1
  63. package/dist/src/extensions/SideMenu/SideMenuPlugin.js +5 -1
  64. package/dist/src/extensions/SideMenu/SideMenuPlugin.js.map +1 -1
  65. package/dist/src/extensions/SideMenu/dragging.js +3 -0
  66. package/dist/src/extensions/SideMenu/dragging.js.map +1 -1
  67. package/dist/src/extensions/SuggestionMenu/SuggestionPlugin.js +3 -3
  68. package/dist/src/extensions/SuggestionMenu/SuggestionPlugin.js.map +1 -1
  69. package/dist/src/extensions/TableHandles/TableHandlesPlugin.js +12 -3
  70. package/dist/src/extensions/TableHandles/TableHandlesPlugin.js.map +1 -1
  71. package/dist/src/index.js +9 -2
  72. package/dist/src/index.js.map +1 -1
  73. package/dist/src/schema/inlineContent/createSpec.js +1 -1
  74. package/dist/src/schema/inlineContent/createSpec.js.map +1 -1
  75. package/dist/style.css +1 -1
  76. package/dist/tsconfig.tsbuildinfo +1 -1
  77. package/dist/webpack-stats.json +1 -1
  78. package/package.json +2 -2
  79. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +3 -3
  80. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +1 -3
  81. package/src/api/blockManipulation/commands/splitBlock/splitBlock.test.ts +1 -1
  82. package/src/api/blockManipulation/selections/selection.ts +5 -5
  83. package/src/api/clipboard/__snapshots__/tableAllCells.html +1 -1
  84. package/src/api/clipboard/__snapshots__/tableCell.html +1 -1
  85. package/src/api/clipboard/__snapshots__/tableRow.html +1 -1
  86. package/src/api/clipboard/clipboard.test.ts +7 -3
  87. package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +1 -1
  88. package/src/api/clipboard/fromClipboard/handleVSCodePaste.ts +7 -14
  89. package/src/api/clipboard/fromClipboard/pasteExtension.ts +6 -6
  90. package/src/api/clipboard/toClipboard/copyExtension.ts +7 -2
  91. package/src/api/exporters/html/__snapshots__/file/basic/internal.html +1 -1
  92. package/src/api/exporters/html/__snapshots__/file/nested/internal.html +1 -1
  93. package/src/api/exporters/html/__snapshots__/file/noCaption/internal.html +1 -1
  94. package/src/api/exporters/html/__snapshots__/file/noName/internal.html +1 -1
  95. package/src/api/exporters/html/__snapshots__/image/basic/internal.html +1 -1
  96. package/src/api/exporters/html/__snapshots__/image/nested/internal.html +1 -1
  97. package/src/api/exporters/html/__snapshots__/image/noCaption/internal.html +1 -1
  98. package/src/api/exporters/html/__snapshots__/image/noName/internal.html +1 -1
  99. package/src/api/exporters/html/__snapshots__/image/noPreview/internal.html +1 -1
  100. package/src/api/exporters/html/__snapshots__/simpleImage/basic/external.html +1 -1
  101. package/src/api/exporters/html/__snapshots__/simpleImage/basic/internal.html +1 -1
  102. package/src/api/exporters/html/__snapshots__/simpleImage/nested/external.html +1 -1
  103. package/src/api/exporters/html/__snapshots__/simpleImage/nested/internal.html +1 -1
  104. package/src/api/exporters/html/__snapshots__/simpleImage/noCaption/external.html +1 -1
  105. package/src/api/exporters/html/__snapshots__/simpleImage/noCaption/internal.html +1 -1
  106. package/src/api/exporters/html/__snapshots__/simpleImage/noName/external.html +1 -1
  107. package/src/api/exporters/html/__snapshots__/simpleImage/noName/internal.html +1 -1
  108. package/src/api/exporters/html/__snapshots__/simpleImage/noPreview/external.html +1 -1
  109. package/src/api/exporters/html/__snapshots__/simpleImage/noPreview/internal.html +1 -1
  110. package/src/api/exporters/markdown/__snapshots__/simpleImage/basic/markdown.md +1 -1
  111. package/src/api/exporters/markdown/__snapshots__/simpleImage/nested/markdown.md +2 -2
  112. package/src/api/exporters/markdown/__snapshots__/simpleImage/noCaption/markdown.md +1 -1
  113. package/src/api/exporters/markdown/__snapshots__/simpleImage/noName/markdown.md +1 -1
  114. package/src/api/parsers/markdown/parseMarkdown.test.ts +5 -7
  115. package/src/blocks/AudioBlockContent/AudioBlockContent.ts +13 -14
  116. package/src/blocks/FileBlockContent/FileBlockContent.ts +5 -12
  117. package/src/blocks/FileBlockContent/helpers/parse/parseEmbedElement.ts +5 -0
  118. package/src/blocks/FileBlockContent/helpers/parse/parseFigureElement.ts +16 -0
  119. package/src/blocks/FileBlockContent/helpers/render/createAddFileButton.ts +63 -0
  120. package/src/blocks/FileBlockContent/helpers/render/createFileBlockWrapper.ts +80 -0
  121. package/src/blocks/FileBlockContent/helpers/render/createFileNameWithIcon.ts +24 -0
  122. package/src/blocks/FileBlockContent/helpers/render/createResizableFileBlockWrapper.ts +204 -0
  123. package/src/blocks/FileBlockContent/helpers/toExternalHTML/createFigureWithCaption.ts +13 -0
  124. package/src/blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.ts +15 -0
  125. package/src/blocks/ImageBlockContent/ImageBlockContent.ts +20 -28
  126. package/src/blocks/VideoBlockContent/VideoBlockContent.ts +20 -27
  127. package/src/editor/Block.css +35 -51
  128. package/src/editor/BlockNoteEditor.ts +14 -7
  129. package/src/editor/BlockNoteExtensions.ts +7 -8
  130. package/src/editor/editor.css +1 -0
  131. package/src/extensions/LinkToolbar/protocols.ts +13 -0
  132. package/src/extensions/Placeholder/PlaceholderPlugin.ts +6 -6
  133. package/src/extensions/SideMenu/SideMenuPlugin.ts +5 -1
  134. package/src/extensions/SideMenu/dragging.ts +3 -0
  135. package/src/extensions/SuggestionMenu/SuggestionPlugin.ts +3 -6
  136. package/src/extensions/TableHandles/TableHandlesPlugin.ts +15 -3
  137. package/src/index.ts +9 -2
  138. package/src/schema/inlineContent/createSpec.ts +2 -2
  139. package/types/src/api/clipboard/fromClipboard/handleVSCodePaste.d.ts +2 -3
  140. package/types/src/api/clipboard/fromClipboard/pasteExtension.d.ts +1 -3
  141. package/types/src/blocks/AudioBlockContent/AudioBlockContent.d.ts +2 -5
  142. package/types/src/blocks/FileBlockContent/FileBlockContent.d.ts +2 -5
  143. package/types/src/blocks/FileBlockContent/helpers/parse/parseEmbedElement.d.ts +3 -0
  144. package/types/src/blocks/FileBlockContent/helpers/parse/parseFigureElement.d.ts +4 -0
  145. package/types/src/blocks/FileBlockContent/helpers/render/createAddFileButton.d.ts +6 -0
  146. package/types/src/blocks/FileBlockContent/helpers/render/createFileBlockWrapper.d.ts +9 -0
  147. package/types/src/blocks/FileBlockContent/helpers/render/createFileNameWithIcon.d.ts +6 -0
  148. package/types/src/blocks/FileBlockContent/helpers/render/createResizableFileBlockWrapper.d.ts +9 -0
  149. package/types/src/blocks/FileBlockContent/helpers/toExternalHTML/createFigureWithCaption.d.ts +3 -0
  150. package/types/src/blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.d.ts +3 -0
  151. package/types/src/blocks/ImageBlockContent/ImageBlockContent.d.ts +2 -5
  152. package/types/src/blocks/VideoBlockContent/VideoBlockContent.d.ts +2 -5
  153. package/types/src/editor/BlockNoteEditor.d.ts +6 -4
  154. package/types/src/extensions/LinkToolbar/protocols.d.ts +2 -0
  155. package/types/src/index.d.ts +9 -2
  156. package/dist/src/blocks/AudioBlockContent/audioBlockHelpers.js.map +0 -1
  157. package/dist/src/blocks/FileBlockContent/fileBlockHelpers.js +0 -317
  158. package/dist/src/blocks/FileBlockContent/fileBlockHelpers.js.map +0 -1
  159. package/dist/src/blocks/ImageBlockContent/imageBlockHelpers.js.map +0 -1
  160. package/dist/src/blocks/VideoBlockContent/videoBlockHelpers.js.map +0 -1
  161. package/src/blocks/FileBlockContent/fileBlockHelpers.ts +0 -456
  162. package/types/src/blocks/FileBlockContent/fileBlockHelpers.d.ts +0 -41
  163. /package/src/blocks/AudioBlockContent/{audioBlockHelpers.ts → parseAudioElement.ts} +0 -0
  164. /package/src/blocks/ImageBlockContent/{imageBlockHelpers.ts → parseImageElement.ts} +0 -0
  165. /package/src/blocks/VideoBlockContent/{videoBlockHelpers.ts → parseVideoElement.ts} +0 -0
  166. /package/types/src/blocks/AudioBlockContent/{audioBlockHelpers.d.ts → parseAudioElement.d.ts} +0 -0
  167. /package/types/src/blocks/ImageBlockContent/{imageBlockHelpers.d.ts → parseImageElement.d.ts} +0 -0
  168. /package/types/src/blocks/VideoBlockContent/{videoBlockHelpers.d.ts → parseVideoElement.d.ts} +0 -0
@@ -1,456 +0,0 @@
1
- import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
2
- import {
3
- BlockFromConfig,
4
- BlockSchemaWithBlock,
5
- FileBlockConfig,
6
- } from "../../schema/index.js";
7
-
8
- export const FILE_ICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z"></path></svg>`;
9
- export const createFileBlockWrapper = (
10
- block: BlockFromConfig<FileBlockConfig, any, any>,
11
- editor: BlockNoteEditor<
12
- BlockSchemaWithBlock<FileBlockConfig["type"], FileBlockConfig>,
13
- any,
14
- any
15
- >,
16
- // TODO: Maybe make optional for default preview
17
- element: { dom: HTMLElement; destroy?: () => void },
18
- buttonText?: string,
19
- buttonIcon?: HTMLElement
20
- ) => {
21
- const wrapper = document.createElement("div");
22
- wrapper.className = "bn-file-block-content-wrapper";
23
-
24
- if (block.props.url === "") {
25
- const addFileButton = createAddFileButton(
26
- block,
27
- editor,
28
- buttonText,
29
- buttonIcon
30
- );
31
- wrapper.appendChild(addFileButton.dom);
32
-
33
- const loading = document.createElement("div");
34
- loading.className = "bn-file-loading-preview";
35
- loading.textContent = "Loading...";
36
-
37
- const destroyUploadStartHandler = editor.onUploadStart((blockId) => {
38
- if (blockId === block.id) {
39
- wrapper.removeChild(addFileButton.dom);
40
- wrapper.appendChild(loading);
41
- }
42
- });
43
- const destroyUploadEndHandler = editor.onUploadEnd((blockId) => {
44
- if (blockId === block.id) {
45
- wrapper.removeChild(loading);
46
- wrapper.appendChild(addFileButton.dom);
47
- }
48
- });
49
-
50
- return {
51
- dom: wrapper,
52
- destroy: () => {
53
- addFileButton.destroy?.();
54
- destroyUploadStartHandler();
55
- destroyUploadEndHandler();
56
- },
57
- };
58
- } else if (block.props.showPreview === false) {
59
- // TODO: Not using the wrapper element here?
60
- const file = createDefaultFilePreview(block).dom;
61
- const element = createFileAndCaptionWrapper(block, file);
62
-
63
- return {
64
- dom: element.dom,
65
- };
66
- } else {
67
- wrapper.appendChild(element.dom);
68
-
69
- return {
70
- dom: wrapper,
71
- destroy: element.destroy,
72
- };
73
- }
74
- };
75
-
76
- // Default file preview, displaying a file icon and file name.
77
- export const createDefaultFilePreview = (
78
- block: BlockFromConfig<FileBlockConfig, any, any>
79
- ): { dom: HTMLElement; destroy?: () => void } => {
80
- const file = document.createElement("div");
81
- file.className = "bn-file-default-preview";
82
-
83
- const icon = document.createElement("div");
84
- icon.className = "bn-file-default-preview-icon";
85
- icon.innerHTML = FILE_ICON_SVG;
86
-
87
- const fileName = document.createElement("p");
88
- fileName.className = "bn-file-default-preview-name";
89
- fileName.textContent = block.props.name || "";
90
-
91
- file.appendChild(icon);
92
- file.appendChild(fileName);
93
-
94
- return {
95
- dom: file,
96
- };
97
- };
98
-
99
- // Wrapper element containing file preview and caption.
100
- export const createFileAndCaptionWrapper = (
101
- block: BlockFromConfig<FileBlockConfig, any, any>,
102
- file: HTMLElement
103
- ) => {
104
- const fileAndCaptionWrapper = document.createElement("div");
105
- fileAndCaptionWrapper.className = "bn-file-and-caption-wrapper";
106
-
107
- const caption = document.createElement("p");
108
- caption.className = "bn-file-caption";
109
- caption.textContent = block.props.caption;
110
-
111
- if (
112
- typeof block.props.previewWidth === "number" &&
113
- block.props.previewWidth > 0 &&
114
- block.props.caption !== undefined
115
- ) {
116
- caption.style.width = `${block.props.previewWidth}px`;
117
- }
118
-
119
- fileAndCaptionWrapper.appendChild(file);
120
- fileAndCaptionWrapper.appendChild(caption);
121
-
122
- return {
123
- dom: fileAndCaptionWrapper,
124
- };
125
- };
126
-
127
- // Button element that acts as a placeholder for files with no src.
128
- export const createAddFileButton = (
129
- block: BlockFromConfig<FileBlockConfig, any, any>,
130
- editor: BlockNoteEditor<any, any, any>,
131
- buttonText?: string,
132
- buttonIcon?: HTMLElement
133
- ) => {
134
- const addFileButton = document.createElement("div");
135
- addFileButton.className = "bn-add-file-button";
136
-
137
- const addFileButtonIcon = document.createElement("div");
138
- addFileButtonIcon.className = "bn-add-file-button-icon";
139
- if (buttonIcon) {
140
- addFileButtonIcon.appendChild(buttonIcon);
141
- } else {
142
- addFileButtonIcon.innerHTML =
143
- '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z"></path></svg>';
144
- }
145
-
146
- const addFileButtonText = document.createElement("p");
147
- addFileButtonText.className = "bn-add-file-button-text";
148
- addFileButtonText.innerHTML =
149
- buttonText || editor.dictionary.file_blocks.file.add_button_text;
150
-
151
- // Prevents focus from moving to the button.
152
- const addFileButtonMouseDownHandler = (event: MouseEvent) => {
153
- event.preventDefault();
154
- };
155
- // Opens the file toolbar.
156
- const addFileButtonClickHandler = () => {
157
- editor.dispatch(
158
- editor._tiptapEditor.state.tr.setMeta(editor.filePanel!.plugin, {
159
- block: block,
160
- })
161
- );
162
- };
163
-
164
- addFileButton.appendChild(addFileButtonIcon);
165
- addFileButton.appendChild(addFileButtonText);
166
-
167
- addFileButton.addEventListener(
168
- "mousedown",
169
- addFileButtonMouseDownHandler,
170
- true
171
- );
172
- addFileButton.addEventListener("click", addFileButtonClickHandler, true);
173
-
174
- return {
175
- dom: addFileButton,
176
- destroy: () => {
177
- addFileButton.removeEventListener(
178
- "mousedown",
179
- addFileButtonMouseDownHandler,
180
- true
181
- );
182
- addFileButton.removeEventListener(
183
- "click",
184
- addFileButtonClickHandler,
185
- true
186
- );
187
- },
188
- };
189
- };
190
-
191
- export const parseEmbedElement = (embedElement: HTMLEmbedElement) => {
192
- const url = embedElement.src || undefined;
193
-
194
- return { url };
195
- };
196
-
197
- export const parseFigureElement = (
198
- figureElement: HTMLElement,
199
- targetTag: string
200
- ) => {
201
- const targetElement = figureElement.querySelector(
202
- targetTag
203
- ) as HTMLElement | null;
204
- if (!targetElement) {
205
- return undefined;
206
- }
207
-
208
- const captionElement = figureElement.querySelector("figcaption");
209
- const caption = captionElement?.textContent ?? undefined;
210
-
211
- return { targetElement, caption };
212
- };
213
-
214
- // Wrapper figure element to display file link with caption. Used for external
215
- // HTML
216
- export const createLinkWithCaption = (
217
- element: HTMLElement,
218
- caption: string
219
- ) => {
220
- const wrapper = document.createElement("div");
221
- const fileCaption = document.createElement("p");
222
- fileCaption.textContent = caption;
223
-
224
- wrapper.appendChild(element);
225
- wrapper.appendChild(fileCaption);
226
-
227
- return {
228
- dom: wrapper,
229
- };
230
- };
231
-
232
- // Wrapper figure element to display file preview with caption. Used for
233
- // external HTML.
234
- export const createFigureWithCaption = (
235
- element: HTMLElement,
236
- caption: string
237
- ) => {
238
- const figure = document.createElement("figure");
239
- const captionElement = document.createElement("figcaption");
240
- captionElement.textContent = caption;
241
-
242
- figure.appendChild(element);
243
- figure.appendChild(captionElement);
244
-
245
- return { dom: figure };
246
- };
247
-
248
- // Wrapper element which adds resize handles & logic for visual media file
249
- // previews.
250
- export const createResizeHandlesWrapper = (
251
- block: BlockFromConfig<FileBlockConfig, any, any>,
252
- editor: BlockNoteEditor<any, any, any>,
253
- element: HTMLElement,
254
- getWidth: () => number,
255
- setWidth: (width: number) => void
256
- ): { dom: HTMLElement; destroy: () => void } => {
257
- if (!block.props.previewWidth) {
258
- throw new Error("Block must have a `previewWidth` prop.");
259
- }
260
-
261
- // Wrapper element for rendered element and resize handles.
262
- const wrapper = document.createElement("div");
263
- wrapper.className = "bn-visual-media-wrapper";
264
-
265
- // Resize handle elements.
266
- const leftResizeHandle = document.createElement("div");
267
- leftResizeHandle.className = "bn-visual-media-resize-handle";
268
- leftResizeHandle.style.left = "4px";
269
- const rightResizeHandle = document.createElement("div");
270
- rightResizeHandle.className = "bn-visual-media-resize-handle";
271
- rightResizeHandle.style.right = "4px";
272
-
273
- // Temporary parameters set when the user begins resizing the element, used to
274
- // calculate the new width of the element.
275
- let resizeParams:
276
- | {
277
- handleUsed: "left" | "right";
278
- initialWidth: number;
279
- initialClientX: number;
280
- }
281
- | undefined;
282
-
283
- // Updates the element width with an updated width depending on the cursor X
284
- // offset from when the resize began, and which resize handle is being used.
285
- const windowMouseMoveHandler = (event: MouseEvent) => {
286
- if (!resizeParams) {
287
- if (
288
- !editor.isEditable &&
289
- wrapper.contains(leftResizeHandle) &&
290
- wrapper.contains(rightResizeHandle)
291
- ) {
292
- wrapper.removeChild(leftResizeHandle);
293
- wrapper.removeChild(rightResizeHandle);
294
- }
295
-
296
- return;
297
- }
298
-
299
- let newWidth: number;
300
-
301
- if (block.props.textAlignment === "center") {
302
- if (resizeParams.handleUsed === "left") {
303
- newWidth =
304
- resizeParams.initialWidth +
305
- (resizeParams.initialClientX - event.clientX) * 2;
306
- } else {
307
- newWidth =
308
- resizeParams.initialWidth +
309
- (event.clientX - resizeParams.initialClientX) * 2;
310
- }
311
- } else {
312
- if (resizeParams.handleUsed === "left") {
313
- newWidth =
314
- resizeParams.initialWidth +
315
- resizeParams.initialClientX -
316
- event.clientX;
317
- } else {
318
- newWidth =
319
- resizeParams.initialWidth +
320
- event.clientX -
321
- resizeParams.initialClientX;
322
- }
323
- }
324
-
325
- // Min element width in px.
326
- const minWidth = 64;
327
-
328
- // Ensures the element is not wider than the editor and not smaller than a
329
- // predetermined minimum width.
330
- if (newWidth < minWidth) {
331
- setWidth(minWidth);
332
- } else if (newWidth > editor.domElement.firstElementChild!.clientWidth) {
333
- setWidth(editor.domElement.firstElementChild!.clientWidth);
334
- } else {
335
- setWidth(newWidth);
336
- }
337
- };
338
- // Stops mouse movements from resizing the element and updates the block's
339
- // `width` prop to the new value.
340
- const windowMouseUpHandler = (event: MouseEvent) => {
341
- // Hides the drag handles if the cursor is no longer over the element.
342
- if (
343
- (!event.target ||
344
- !wrapper.contains(event.target as Node) ||
345
- !editor.isEditable) &&
346
- wrapper.contains(leftResizeHandle) &&
347
- wrapper.contains(rightResizeHandle)
348
- ) {
349
- wrapper.removeChild(leftResizeHandle);
350
- wrapper.removeChild(rightResizeHandle);
351
- }
352
-
353
- if (!resizeParams) {
354
- return;
355
- }
356
-
357
- resizeParams = undefined;
358
-
359
- editor.updateBlock(block, {
360
- props: {
361
- previewWidth: getWidth(),
362
- },
363
- });
364
- };
365
-
366
- // Shows the resize handles when hovering over the element with the cursor.
367
- const elementMouseEnterHandler = () => {
368
- if (editor.isEditable) {
369
- wrapper.appendChild(leftResizeHandle);
370
- wrapper.appendChild(rightResizeHandle);
371
- }
372
- };
373
- // Hides the resize handles when the cursor leaves the element, unless the
374
- // cursor moves to one of the resize handles.
375
- const elementMouseLeaveHandler = (event: MouseEvent) => {
376
- if (
377
- event.relatedTarget === leftResizeHandle ||
378
- event.relatedTarget === rightResizeHandle
379
- ) {
380
- return;
381
- }
382
-
383
- if (resizeParams) {
384
- return;
385
- }
386
-
387
- if (
388
- editor.isEditable &&
389
- wrapper.contains(leftResizeHandle) &&
390
- wrapper.contains(rightResizeHandle)
391
- ) {
392
- wrapper.removeChild(leftResizeHandle);
393
- wrapper.removeChild(rightResizeHandle);
394
- }
395
- };
396
-
397
- // Sets the resize params, allowing the user to begin resizing the element by
398
- // moving the cursor left or right.
399
- const leftResizeHandleMouseDownHandler = (event: MouseEvent) => {
400
- event.preventDefault();
401
-
402
- wrapper.appendChild(leftResizeHandle);
403
- wrapper.appendChild(rightResizeHandle);
404
-
405
- resizeParams = {
406
- handleUsed: "left",
407
- initialWidth: block.props.previewWidth!,
408
- initialClientX: event.clientX,
409
- };
410
- };
411
- const rightResizeHandleMouseDownHandler = (event: MouseEvent) => {
412
- event.preventDefault();
413
-
414
- wrapper.appendChild(leftResizeHandle);
415
- wrapper.appendChild(rightResizeHandle);
416
-
417
- resizeParams = {
418
- handleUsed: "right",
419
- initialWidth: block.props.previewWidth!,
420
- initialClientX: event.clientX,
421
- };
422
- };
423
-
424
- wrapper.appendChild(element);
425
-
426
- window.addEventListener("mousemove", windowMouseMoveHandler);
427
- window.addEventListener("mouseup", windowMouseUpHandler);
428
- element.addEventListener("mouseenter", elementMouseEnterHandler);
429
- element.addEventListener("mouseleave", elementMouseLeaveHandler);
430
- leftResizeHandle.addEventListener(
431
- "mousedown",
432
- leftResizeHandleMouseDownHandler
433
- );
434
- rightResizeHandle.addEventListener(
435
- "mousedown",
436
- rightResizeHandleMouseDownHandler
437
- );
438
-
439
- return {
440
- dom: wrapper,
441
- destroy: () => {
442
- window.removeEventListener("mousemove", windowMouseMoveHandler);
443
- window.removeEventListener("mouseup", windowMouseUpHandler);
444
- element.removeEventListener("mouseenter", elementMouseEnterHandler);
445
- element.removeEventListener("mouseleave", elementMouseLeaveHandler);
446
- leftResizeHandle.removeEventListener(
447
- "mousedown",
448
- leftResizeHandleMouseDownHandler
449
- );
450
- rightResizeHandle.removeEventListener(
451
- "mousedown",
452
- rightResizeHandleMouseDownHandler
453
- );
454
- },
455
- };
456
- };
@@ -1,41 +0,0 @@
1
- import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
2
- import { BlockFromConfig, BlockSchemaWithBlock, FileBlockConfig } from "../../schema/index.js";
3
- export declare const FILE_ICON_SVG = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" fill=\"currentColor\"><path d=\"M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z\"></path></svg>";
4
- export declare const createFileBlockWrapper: (block: BlockFromConfig<FileBlockConfig, any, any>, editor: BlockNoteEditor<BlockSchemaWithBlock<FileBlockConfig["type"], FileBlockConfig>, any, any>, element: {
5
- dom: HTMLElement;
6
- destroy?: () => void;
7
- }, buttonText?: string, buttonIcon?: HTMLElement) => {
8
- dom: HTMLDivElement;
9
- destroy?: undefined;
10
- } | {
11
- dom: HTMLDivElement;
12
- destroy: (() => void) | undefined;
13
- };
14
- export declare const createDefaultFilePreview: (block: BlockFromConfig<FileBlockConfig, any, any>) => {
15
- dom: HTMLElement;
16
- destroy?: () => void;
17
- };
18
- export declare const createFileAndCaptionWrapper: (block: BlockFromConfig<FileBlockConfig, any, any>, file: HTMLElement) => {
19
- dom: HTMLDivElement;
20
- };
21
- export declare const createAddFileButton: (block: BlockFromConfig<FileBlockConfig, any, any>, editor: BlockNoteEditor<any, any, any>, buttonText?: string, buttonIcon?: HTMLElement) => {
22
- dom: HTMLDivElement;
23
- destroy: () => void;
24
- };
25
- export declare const parseEmbedElement: (embedElement: HTMLEmbedElement) => {
26
- url: string | undefined;
27
- };
28
- export declare const parseFigureElement: (figureElement: HTMLElement, targetTag: string) => {
29
- targetElement: HTMLElement;
30
- caption: string | undefined;
31
- } | undefined;
32
- export declare const createLinkWithCaption: (element: HTMLElement, caption: string) => {
33
- dom: HTMLDivElement;
34
- };
35
- export declare const createFigureWithCaption: (element: HTMLElement, caption: string) => {
36
- dom: HTMLElement;
37
- };
38
- export declare const createResizeHandlesWrapper: (block: BlockFromConfig<FileBlockConfig, any, any>, editor: BlockNoteEditor<any, any, any>, element: HTMLElement, getWidth: () => number, setWidth: (width: number) => void) => {
39
- dom: HTMLElement;
40
- destroy: () => void;
41
- };