@blocknote/core 0.4.0 → 0.4.2

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 (69) hide show
  1. package/dist/blocknote.js +2043 -1918
  2. package/dist/blocknote.js.map +1 -1
  3. package/dist/blocknote.umd.cjs +20 -20
  4. package/dist/blocknote.umd.cjs.map +1 -1
  5. package/dist/style.css +1 -1
  6. package/package.json +2 -2
  7. package/src/BlockNoteEditor.ts +5 -4
  8. package/src/BlockNoteExtensions.ts +11 -11
  9. package/src/api/Editor.ts +90 -6
  10. package/src/api/blockManipulation/__snapshots__/blockManipulation.test.ts.snap +616 -0
  11. package/src/api/blockManipulation/blockManipulation.test.ts +172 -0
  12. package/src/api/blockManipulation/blockManipulation.ts +25 -14
  13. package/src/api/formatConversions/__snapshots__/formatConversions.test.ts.snap +346 -0
  14. package/src/api/formatConversions/formatConversions.test.ts +766 -0
  15. package/src/api/nodeConversions/__snapshots__/nodeConversions.test.ts.snap +268 -0
  16. package/src/api/nodeConversions/nodeConversions.test.ts +244 -0
  17. package/src/api/nodeConversions/nodeConversions.ts +167 -58
  18. package/src/api/nodeConversions/testUtil.ts +61 -0
  19. package/src/api/util/nodeUtil.ts +38 -0
  20. package/src/editor.module.css +1 -0
  21. package/src/extensions/Blocks/api/blockTypes.ts +14 -9
  22. package/src/extensions/Blocks/api/inlineContentTypes.ts +27 -36
  23. package/src/extensions/Blocks/nodes/Block.module.css +39 -36
  24. package/src/extensions/Blocks/nodes/BlockContainer.ts +15 -14
  25. package/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts +149 -87
  26. package/src/shared/utils.ts +6 -0
  27. package/types/src/BlockNoteEditor.d.ts +2 -2
  28. package/types/src/api/Editor.d.ts +26 -6
  29. package/types/src/api/blockManipulation/blockManipulation.d.ts +5 -5
  30. package/types/src/api/blockManipulation/blockManipulation.test.d.ts +1 -0
  31. package/types/src/api/formatConversions/formatConversions.test.d.ts +1 -0
  32. package/types/src/api/nodeConversions/nodeConversions.d.ts +11 -4
  33. package/types/src/api/nodeConversions/nodeConversions.test.d.ts +1 -0
  34. package/types/src/api/nodeConversions/testUtil.d.ts +2 -0
  35. package/types/src/api/util/nodeUtil.d.ts +8 -0
  36. package/types/src/extensions/Blocks/api/blockTypes.d.ts +10 -9
  37. package/types/src/extensions/Blocks/api/inlineContentTypes.d.ts +25 -19
  38. package/types/src/extensions/DraggableBlocks/DraggableBlocksPlugin.d.ts +15 -0
  39. package/types/src/shared/utils.d.ts +3 -0
  40. package/types/src/EditorElement.d.ts +0 -7
  41. package/types/src/api/Document.d.ts +0 -5
  42. package/types/src/api/removeUnderlinesRehypePlugin.d.ts +0 -6
  43. package/types/src/api/simplifyBlocksRehypePlugin.d.ts +0 -16
  44. package/types/src/extensions/Blocks/BlockAttributes.d.ts +0 -2
  45. package/types/src/extensions/Blocks/MultipleNodeSelection.d.ts +0 -24
  46. package/types/src/extensions/Blocks/api/apiTypes.d.ts +0 -18
  47. package/types/src/extensions/Blocks/api/styleTypes.d.ts +0 -22
  48. package/types/src/extensions/Blocks/apiTypes.d.ts +0 -16
  49. package/types/src/extensions/Blocks/nodes/Block.d.ts +0 -24
  50. package/types/src/extensions/Blocks/nodes/BlockContent/BlockContentTypes.d.ts +0 -4
  51. package/types/src/extensions/Blocks/nodes/BlockContent/HeadingBlockContent/HeadingBlockContentTypes.d.ts +0 -4
  52. package/types/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContentTypes.d.ts +0 -2
  53. package/types/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContentTypes.d.ts +0 -2
  54. package/types/src/extensions/Blocks/nodes/BlockContent/ParagraphBlockContent/ParagraphBlockContentTypes.d.ts +0 -2
  55. package/types/src/extensions/Blocks/nodes/BlockTypes/HeadingBlock/HeadingContent.d.ts +0 -8
  56. package/types/src/extensions/Blocks/nodes/BlockTypes/ListItemBlock/ListItemContent.d.ts +0 -8
  57. package/types/src/extensions/Blocks/nodes/BlockTypes/ListItemBlock/OrderedListItemIndexPlugin.d.ts +0 -2
  58. package/types/src/extensions/Blocks/nodes/BlockTypes/TextBlock/TextContent.d.ts +0 -6
  59. package/types/src/extensions/BubbleMenu/BubbleMenuExtension.d.ts +0 -8
  60. package/types/src/extensions/BubbleMenu/BubbleMenuFactoryTypes.d.ts +0 -27
  61. package/types/src/extensions/BubbleMenu/BubbleMenuPlugin.d.ts +0 -44
  62. package/types/src/extensions/DraggableBlocks/BlockMenuFactoryTypes.d.ts +0 -12
  63. package/types/src/extensions/DraggableBlocks/DragMenuFactoryTypes.d.ts +0 -18
  64. package/types/src/extensions/Hyperlinks/HyperlinkMark.d.ts +0 -8
  65. package/types/src/extensions/Hyperlinks/HyperlinkMenuFactoryTypes.d.ts +0 -11
  66. package/types/src/extensions/Hyperlinks/HyperlinkMenuPlugin.d.ts +0 -11
  67. package/types/src/extensions/Paragraph/FixedParagraph.d.ts +0 -1
  68. package/types/src/extensions/SlashMenu/defaultCommands.d.ts +0 -8
  69. package/types/src/utils.d.ts +0 -2
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "homepage": "https://github.com/yousefed/blocknote",
4
4
  "private": false,
5
5
  "license": "MPL-2.0",
6
- "version": "0.4.0",
6
+ "version": "0.4.2",
7
7
  "files": [
8
8
  "dist",
9
9
  "types",
@@ -106,5 +106,5 @@
106
106
  "access": "public",
107
107
  "registry": "https://registry.npmjs.org/"
108
108
  },
109
- "gitHead": "aa88219581e5427fd86c759b827ffc2b2f3af99b"
109
+ "gitHead": "14086236a83e42c1de7e4d9910e6348218e78a98"
110
110
  }
@@ -7,14 +7,15 @@ import styles from "./editor.module.css";
7
7
  import { defaultSlashCommands, SlashCommand } from "./extensions/SlashMenu";
8
8
 
9
9
  export type BlockNoteEditorOptions = {
10
+ // TODO: Figure out if enableBlockNoteExtensions/disableHistoryExtension are needed and document them.
10
11
  enableBlockNoteExtensions: boolean;
11
12
  disableHistoryExtension: boolean;
12
13
  uiFactories: UiFactories;
13
14
  slashCommands: SlashCommand[];
14
15
  parentElement: HTMLElement;
15
16
  editorDOMAttributes: Record<string, string>;
16
- onUpdate: () => void;
17
- onCreate: () => void;
17
+ onUpdate: (editor: BlockNoteEditor) => void;
18
+ onCreate: (editor: BlockNoteEditor) => void;
18
19
 
19
20
  // tiptap options, undocumented
20
21
  _tiptapOptions: any;
@@ -47,10 +48,10 @@ export class BlockNoteEditor extends EditorAPI {
47
48
  ...blockNoteTipTapOptions,
48
49
  ...options._tiptapOptions,
49
50
  onUpdate: () => {
50
- options.onUpdate?.();
51
+ options.onUpdate?.(this);
51
52
  },
52
53
  onCreate: () => {
53
- options.onCreate?.();
54
+ options.onCreate?.(this);
54
55
  },
55
56
  extensions:
56
57
  options.enableBlockNoteExtensions === false
@@ -1,16 +1,16 @@
1
1
  import { Extensions, extensions } from "@tiptap/core";
2
2
 
3
- import Bold from "@tiptap/extension-bold";
4
- import Code from "@tiptap/extension-code";
5
- import DropCursor from "@tiptap/extension-dropcursor";
6
- import GapCursor from "@tiptap/extension-gapcursor";
7
- import HardBreak from "@tiptap/extension-hard-break";
3
+ import { Bold } from "@tiptap/extension-bold";
4
+ import { Code } from "@tiptap/extension-code";
5
+ import { Dropcursor } from "@tiptap/extension-dropcursor";
6
+ import { Gapcursor } from "@tiptap/extension-gapcursor";
7
+ import { HardBreak } from "@tiptap/extension-hard-break";
8
8
  import { History } from "@tiptap/extension-history";
9
- import Italic from "@tiptap/extension-italic";
9
+ import { Italic } from "@tiptap/extension-italic";
10
10
  import { Link } from "@tiptap/extension-link";
11
- import Strike from "@tiptap/extension-strike";
12
- import Text from "@tiptap/extension-text";
13
- import Underline from "@tiptap/extension-underline";
11
+ import { Strike } from "@tiptap/extension-strike";
12
+ import { Text } from "@tiptap/extension-text";
13
+ import { Underline } from "@tiptap/extension-underline";
14
14
  import { BackgroundColorExtension } from "./extensions/BackgroundColor/BackgroundColorExtension";
15
15
  import { BackgroundColorMark } from "./extensions/BackgroundColor/BackgroundColorMark";
16
16
  import { blocks } from "./extensions/Blocks";
@@ -53,7 +53,7 @@ export const getBlockNoteExtensions = (opts: {
53
53
  extensions.Tabindex,
54
54
 
55
55
  // DevTools,
56
- GapCursor,
56
+ Gapcursor,
57
57
 
58
58
  // DropCursor,
59
59
  Placeholder.configure({
@@ -87,7 +87,7 @@ export const getBlockNoteExtensions = (opts: {
87
87
  // custom blocks:
88
88
  ...blocks,
89
89
 
90
- DropCursor.configure({ width: 5, color: "#ddeeff" }),
90
+ Dropcursor.configure({ width: 5, color: "#ddeeff" }),
91
91
  History,
92
92
  // This needs to be at the bottom of this list, because Key events (such as enter, when selecting a /command),
93
93
  // should be handled before Enter handlers in other components like splitListItem
package/src/api/Editor.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import { Editor as TiptapEditor } from "@tiptap/core";
2
2
  import { Node } from "prosemirror-model";
3
3
  import { getBlockInfoFromPos } from "../extensions/Blocks/helpers/getBlockInfoFromPos";
4
- import { Block, PartialBlock } from "../extensions/Blocks/api/blockTypes";
4
+ import {
5
+ Block,
6
+ BlockIdentifier,
7
+ PartialBlock,
8
+ } from "../extensions/Blocks/api/blockTypes";
5
9
  import { TextCursorPosition } from "../extensions/Blocks/api/cursorPositionTypes";
6
10
  import { nodeToBlock } from "./nodeConversions/nodeConversions";
7
11
  import {
@@ -24,9 +28,10 @@ export class Editor {
24
28
  ) {}
25
29
 
26
30
  /**
27
- * Gets a list of all top-level blocks that are in the editor.
31
+ * Gets a snapshot of all top-level blocks that are in the editor.
32
+ * @returns An array containing a snapshot of all top-level (non-nested) blocks in the editor.
28
33
  */
29
- public get allBlocks(): Block[] {
34
+ public get topLevelBlocks(): Block[] {
30
35
  const blocks: Block[] = [];
31
36
 
32
37
  this.tiptapEditor.state.doc.firstChild!.descendants((node) => {
@@ -39,7 +44,82 @@ export class Editor {
39
44
  }
40
45
 
41
46
  /**
42
- * Gets information regarding the position of the text cursor in the editor.
47
+ * Retrieves a snapshot of an existing block from the editor.
48
+ * @param block The identifier of an existing block that should be retrieved.
49
+ * @returns The block that matches the identifier, or undefined if no matching block was found.
50
+ */
51
+ public getBlock(block: BlockIdentifier): Block | undefined {
52
+ const id = typeof block === "string" ? block : block.id;
53
+ let newBlock: Block | undefined = undefined;
54
+
55
+ this.tiptapEditor.state.doc.firstChild!.descendants((node) => {
56
+ if (typeof newBlock !== "undefined") {
57
+ return false;
58
+ }
59
+
60
+ if (node.type.name !== "blockContainer" || node.attrs.id !== id) {
61
+ return true;
62
+ }
63
+
64
+ newBlock = nodeToBlock(node, this.blockCache);
65
+
66
+ return false;
67
+ });
68
+
69
+ return newBlock;
70
+ }
71
+
72
+ /**
73
+ * Traverses all blocks in the editor, including all nested blocks, and executes a callback for each. The traversal is
74
+ * depth-first, which is the same order as blocks appear in the editor by y-coordinate. Stops traversal if the callback
75
+ * returns false;
76
+ * @param callback The callback to execute for each block.
77
+ * @param reverse Whether the blocks should be traversed in reverse order.
78
+ */
79
+ public forEachBlock(
80
+ callback: (block: Block) => boolean,
81
+ reverse: boolean = false
82
+ ): void {
83
+ let stop = false;
84
+
85
+ function helper(blocks: Block[]) {
86
+ if (reverse) {
87
+ for (const block of blocks.reverse()) {
88
+ helper(block.children);
89
+
90
+ if (stop) {
91
+ return;
92
+ }
93
+
94
+ stop = !callback(block);
95
+
96
+ if (stop) {
97
+ return;
98
+ }
99
+ }
100
+ } else {
101
+ for (const block of blocks) {
102
+ stop = !callback(block);
103
+
104
+ if (stop) {
105
+ return;
106
+ }
107
+
108
+ helper(block.children);
109
+
110
+ if (stop) {
111
+ return;
112
+ }
113
+ }
114
+ }
115
+ }
116
+
117
+ helper(this.topLevelBlocks);
118
+ }
119
+
120
+ /**
121
+ * Gets a snapshot of the text cursor position within the editor.
122
+ * @returns A snapshot of the text cursor position.
43
123
  */
44
124
  public get textCursorPosition(): TextCursorPosition {
45
125
  const { node } = getBlockInfoFromPos(
@@ -97,7 +177,7 @@ export class Editor {
97
177
  }
98
178
 
99
179
  /**
100
- * Executes a callback function whenever the editor's content changes.
180
+ * Executes a callback function whenever the editor's contents change.
101
181
  * @param callback The callback function to execute.
102
182
  */
103
183
  public onContentChange(callback: () => void) {
@@ -109,6 +189,7 @@ export class Editor {
109
189
  * is simplified in order to better conform to HTML standards. Block structuring elements are removed, children of
110
190
  * blocks which aren't list items are lifted out of them, and list items blocks are wrapped in `ul`/`ol` tags.
111
191
  * @param blocks The list of blocks to serialize into HTML.
192
+ * @returns An HTML representation of the blocks.
112
193
  */
113
194
  public async blocksToHTML(blocks: Block[]): Promise<string> {
114
195
  return blocksToHTML(blocks, this.tiptapEditor.schema);
@@ -117,6 +198,7 @@ export class Editor {
117
198
  /**
118
199
  * Creates a list of blocks from an HTML string.
119
200
  * @param htmlString The HTML string to create a list of blocks from.
201
+ * @returns A list of blocks parsed from the HTML string.
120
202
  */
121
203
  public async HTMLToBlocks(htmlString: string): Promise<Block[]> {
122
204
  return HTMLToBlocks(htmlString, this.tiptapEditor.schema);
@@ -125,8 +207,9 @@ export class Editor {
125
207
  /**
126
208
  * Serializes a list of blocks into a Markdown string. The output is simplified as Markdown does not support all
127
209
  * features of BlockNote. Block structuring elements are removed, children of blocks which aren't list items are
128
- * lifted out of them, and certain styles are removed.
210
+ * un-nested, and certain styles are removed.
129
211
  * @param blocks The list of blocks to serialize into Markdown.
212
+ * @returns A Markdown representation of the blocks.
130
213
  */
131
214
  public async blocksToMarkdown(blocks: Block[]): Promise<string> {
132
215
  return blocksToMarkdown(blocks, this.tiptapEditor.schema);
@@ -135,6 +218,7 @@ export class Editor {
135
218
  /**
136
219
  * Creates a list of blocks from a Markdown string.
137
220
  * @param markdownString The Markdown string to create a list of blocks from.
221
+ * @returns A list of blocks parsed from the Markdown string.
138
222
  */
139
223
  public async markdownToBlocks(markdownString: string): Promise<Block[]> {
140
224
  return markdownToBlocks(markdownString, this.tiptapEditor.schema);