@blocknote/core 0.4.2 → 0.4.4
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.
- package/dist/blocknote.js +12248 -12269
- package/dist/blocknote.js.map +1 -1
- package/dist/blocknote.umd.cjs +20 -20
- package/dist/blocknote.umd.cjs.map +1 -1
- package/package.json +2 -2
- package/src/BlockNoteEditor.ts +276 -15
- package/src/BlockNoteExtensions.ts +8 -4
- package/src/api/formatConversions/formatConversions.ts +4 -4
- package/src/extensions/Blocks/api/cursorPositionTypes.ts +2 -0
- package/src/extensions/Blocks/nodes/BlockContainer.ts +84 -111
- package/src/extensions/SlashMenu/BaseSlashMenuItem.ts +31 -0
- package/src/extensions/SlashMenu/SlashMenuExtension.ts +10 -7
- package/src/extensions/SlashMenu/{defaultSlashCommands.tsx → defaultSlashMenuItems.tsx} +59 -106
- package/src/extensions/SlashMenu/index.ts +3 -7
- package/src/index.ts +2 -3
- package/src/shared/plugins/suggestion/SuggestionItem.ts +2 -13
- package/src/shared/plugins/suggestion/SuggestionPlugin.ts +31 -18
- package/types/src/BlockNoteEditor.d.ts +100 -8
- package/types/src/BlockNoteExtensions.d.ts +5 -4
- package/types/src/api/formatConversions/formatConversions.d.ts +2 -2
- package/types/src/extensions/Blocks/api/cursorPositionTypes.d.ts +2 -0
- package/types/src/extensions/SlashMenu/BaseSlashMenuItem.d.ts +20 -0
- package/types/src/extensions/SlashMenu/SlashMenuExtension.d.ts +4 -2
- package/types/src/extensions/SlashMenu/defaultSlashMenuItems.d.ts +5 -0
- package/types/src/extensions/SlashMenu/index.d.ts +3 -3
- package/types/src/index.d.ts +2 -3
- package/types/src/shared/plugins/suggestion/SuggestionItem.d.ts +3 -11
- package/types/src/shared/plugins/suggestion/SuggestionPlugin.d.ts +4 -4
- package/src/api/Editor.ts +0 -226
- package/src/extensions/SlashMenu/SlashMenuItem.ts +0 -34
|
@@ -2,6 +2,7 @@ import { Editor, Range } from "@tiptap/core";
|
|
|
2
2
|
import { Plugin, PluginKey } from "prosemirror-state";
|
|
3
3
|
import { SuggestionsMenuFactory } from "./SuggestionsMenuFactoryTypes";
|
|
4
4
|
import { SuggestionItem } from "./SuggestionItem";
|
|
5
|
+
import { BlockNoteEditor } from "../../../BlockNoteEditor";
|
|
5
6
|
export type SuggestionPluginOptions<T extends SuggestionItem> = {
|
|
6
7
|
/**
|
|
7
8
|
* The name of the plugin.
|
|
@@ -10,9 +11,9 @@ export type SuggestionPluginOptions<T extends SuggestionItem> = {
|
|
|
10
11
|
*/
|
|
11
12
|
pluginKey: PluginKey;
|
|
12
13
|
/**
|
|
13
|
-
* The
|
|
14
|
+
* The BlockNote editor.
|
|
14
15
|
*/
|
|
15
|
-
editor:
|
|
16
|
+
editor: BlockNoteEditor;
|
|
16
17
|
/**
|
|
17
18
|
* The character that should trigger the suggestion menu to pop up (e.g. a '/' for commands), when typed by the user.
|
|
18
19
|
*/
|
|
@@ -27,8 +28,7 @@ export type SuggestionPluginOptions<T extends SuggestionItem> = {
|
|
|
27
28
|
*/
|
|
28
29
|
onSelectItem?: (props: {
|
|
29
30
|
item: T;
|
|
30
|
-
editor:
|
|
31
|
-
range: Range;
|
|
31
|
+
editor: BlockNoteEditor;
|
|
32
32
|
}) => void;
|
|
33
33
|
/**
|
|
34
34
|
* A function that should supply the plugin with items to suggest, based on a certain query string.
|
package/src/api/Editor.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { Editor as TiptapEditor } from "@tiptap/core";
|
|
2
|
-
import { Node } from "prosemirror-model";
|
|
3
|
-
import { getBlockInfoFromPos } from "../extensions/Blocks/helpers/getBlockInfoFromPos";
|
|
4
|
-
import {
|
|
5
|
-
Block,
|
|
6
|
-
BlockIdentifier,
|
|
7
|
-
PartialBlock,
|
|
8
|
-
} from "../extensions/Blocks/api/blockTypes";
|
|
9
|
-
import { TextCursorPosition } from "../extensions/Blocks/api/cursorPositionTypes";
|
|
10
|
-
import { nodeToBlock } from "./nodeConversions/nodeConversions";
|
|
11
|
-
import {
|
|
12
|
-
insertBlocks,
|
|
13
|
-
removeBlocks,
|
|
14
|
-
replaceBlocks,
|
|
15
|
-
updateBlock,
|
|
16
|
-
} from "./blockManipulation/blockManipulation";
|
|
17
|
-
import {
|
|
18
|
-
blocksToHTML,
|
|
19
|
-
blocksToMarkdown,
|
|
20
|
-
HTMLToBlocks,
|
|
21
|
-
markdownToBlocks,
|
|
22
|
-
} from "./formatConversions/formatConversions";
|
|
23
|
-
|
|
24
|
-
export class Editor {
|
|
25
|
-
constructor(
|
|
26
|
-
private tiptapEditor: TiptapEditor,
|
|
27
|
-
private blockCache = new WeakMap<Node, Block>()
|
|
28
|
-
) {}
|
|
29
|
-
|
|
30
|
-
/**
|
|
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.
|
|
33
|
-
*/
|
|
34
|
-
public get topLevelBlocks(): Block[] {
|
|
35
|
-
const blocks: Block[] = [];
|
|
36
|
-
|
|
37
|
-
this.tiptapEditor.state.doc.firstChild!.descendants((node) => {
|
|
38
|
-
blocks.push(nodeToBlock(node, this.blockCache));
|
|
39
|
-
|
|
40
|
-
return false;
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return blocks;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
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.
|
|
123
|
-
*/
|
|
124
|
-
public get textCursorPosition(): TextCursorPosition {
|
|
125
|
-
const { node } = getBlockInfoFromPos(
|
|
126
|
-
this.tiptapEditor.state.doc,
|
|
127
|
-
this.tiptapEditor.state.selection.from
|
|
128
|
-
)!;
|
|
129
|
-
|
|
130
|
-
return { block: nodeToBlock(node, this.blockCache) };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Inserts multiple blocks before, after, or nested inside an existing block in the editor.
|
|
135
|
-
* @param blocksToInsert An array of blocks to insert.
|
|
136
|
-
* @param blockToInsertAt An existing block, marking where the new blocks should be inserted at.
|
|
137
|
-
* @param placement Determines whether the blocks should be inserted just before, just after, or nested inside the
|
|
138
|
-
* existing block.
|
|
139
|
-
*/
|
|
140
|
-
public insertBlocks(
|
|
141
|
-
blocksToInsert: PartialBlock[],
|
|
142
|
-
blockToInsertAt: Block,
|
|
143
|
-
placement: "before" | "after" | "nested" = "before"
|
|
144
|
-
): void {
|
|
145
|
-
insertBlocks(blocksToInsert, blockToInsertAt, placement, this.tiptapEditor);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Updates a block in the editor to the given specification.
|
|
150
|
-
* @param blockToUpdate The block that should be updated.
|
|
151
|
-
* @param updatedBlock The specification that the block should be updated to.
|
|
152
|
-
*/
|
|
153
|
-
public updateBlock(blockToUpdate: Block, updatedBlock: PartialBlock) {
|
|
154
|
-
updateBlock(blockToUpdate, updatedBlock, this.tiptapEditor);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Removes multiple blocks from the editor. Throws an error if any of the blocks could not be found.
|
|
159
|
-
* @param blocksToRemove An array of blocks that should be removed.
|
|
160
|
-
*/
|
|
161
|
-
public removeBlocks(blocksToRemove: Block[]) {
|
|
162
|
-
removeBlocks(blocksToRemove, this.tiptapEditor);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Replaces multiple blocks in the editor with several other blocks. If the provided blocks to remove are not adjacent
|
|
167
|
-
* to each other, the new blocks are inserted at the position of the first block in the array. Throws an error if any
|
|
168
|
-
* of the blocks could not be found.
|
|
169
|
-
* @param blocksToRemove An array of blocks that should be replaced.
|
|
170
|
-
* @param blocksToInsert An array of blocks to replace the old ones with.
|
|
171
|
-
*/
|
|
172
|
-
public replaceBlocks(
|
|
173
|
-
blocksToRemove: Block[],
|
|
174
|
-
blocksToInsert: PartialBlock[]
|
|
175
|
-
) {
|
|
176
|
-
replaceBlocks(blocksToRemove, blocksToInsert, this.tiptapEditor);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Executes a callback function whenever the editor's contents change.
|
|
181
|
-
* @param callback The callback function to execute.
|
|
182
|
-
*/
|
|
183
|
-
public onContentChange(callback: () => void) {
|
|
184
|
-
this.tiptapEditor.on("update", callback);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Serializes a list of blocks into an HTML string. The output is not the same as what's rendered by the editor, and
|
|
189
|
-
* is simplified in order to better conform to HTML standards. Block structuring elements are removed, children of
|
|
190
|
-
* blocks which aren't list items are lifted out of them, and list items blocks are wrapped in `ul`/`ol` tags.
|
|
191
|
-
* @param blocks The list of blocks to serialize into HTML.
|
|
192
|
-
* @returns An HTML representation of the blocks.
|
|
193
|
-
*/
|
|
194
|
-
public async blocksToHTML(blocks: Block[]): Promise<string> {
|
|
195
|
-
return blocksToHTML(blocks, this.tiptapEditor.schema);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Creates a list of blocks from an HTML string.
|
|
200
|
-
* @param htmlString The HTML string to create a list of blocks from.
|
|
201
|
-
* @returns A list of blocks parsed from the HTML string.
|
|
202
|
-
*/
|
|
203
|
-
public async HTMLToBlocks(htmlString: string): Promise<Block[]> {
|
|
204
|
-
return HTMLToBlocks(htmlString, this.tiptapEditor.schema);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Serializes a list of blocks into a Markdown string. The output is simplified as Markdown does not support all
|
|
209
|
-
* features of BlockNote. Block structuring elements are removed, children of blocks which aren't list items are
|
|
210
|
-
* un-nested, and certain styles are removed.
|
|
211
|
-
* @param blocks The list of blocks to serialize into Markdown.
|
|
212
|
-
* @returns A Markdown representation of the blocks.
|
|
213
|
-
*/
|
|
214
|
-
public async blocksToMarkdown(blocks: Block[]): Promise<string> {
|
|
215
|
-
return blocksToMarkdown(blocks, this.tiptapEditor.schema);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Creates a list of blocks from a Markdown string.
|
|
220
|
-
* @param markdownString The Markdown string to create a list of blocks from.
|
|
221
|
-
* @returns A list of blocks parsed from the Markdown string.
|
|
222
|
-
*/
|
|
223
|
-
public async markdownToBlocks(markdownString: string): Promise<Block[]> {
|
|
224
|
-
return markdownToBlocks(markdownString, this.tiptapEditor.schema);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Editor, Range } from "@tiptap/core";
|
|
2
|
-
import { SuggestionItem } from "../../shared/plugins/suggestion/SuggestionItem";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A class that defines a slash command (/<command>).
|
|
6
|
-
*
|
|
7
|
-
* Not to be confused with ProseMirror commands nor TipTap commands.
|
|
8
|
-
*/
|
|
9
|
-
export class SlashMenuItem implements SuggestionItem {
|
|
10
|
-
/**
|
|
11
|
-
* Constructs a new slash-command.
|
|
12
|
-
*
|
|
13
|
-
* @param name The name of the command
|
|
14
|
-
* @param execute The callback for creating a new node
|
|
15
|
-
* @param aliases Aliases for this command
|
|
16
|
-
*/
|
|
17
|
-
constructor(
|
|
18
|
-
public readonly name: string,
|
|
19
|
-
public readonly execute: (editor: Editor, range: Range) => void,
|
|
20
|
-
public readonly aliases: string[] = [],
|
|
21
|
-
public readonly group: string,
|
|
22
|
-
public readonly hint?: string,
|
|
23
|
-
public readonly shortcut?: string
|
|
24
|
-
) {}
|
|
25
|
-
|
|
26
|
-
match(query: string): boolean {
|
|
27
|
-
return (
|
|
28
|
-
this.name.toLowerCase().startsWith(query.toLowerCase()) ||
|
|
29
|
-
this.aliases.filter((alias) =>
|
|
30
|
-
alias.toLowerCase().startsWith(query.toLowerCase())
|
|
31
|
-
).length !== 0
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|