@blocknote/core 0.7.1-alpha.0 → 0.8.1
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 +1711 -1469
- package/dist/blocknote.js.map +1 -1
- package/dist/blocknote.umd.cjs +6 -2
- package/dist/blocknote.umd.cjs.map +1 -1
- package/dist/style.css +1 -1
- package/package.json +3 -3
- package/src/BlockNoteEditor.ts +104 -53
- package/src/BlockNoteExtensions.ts +24 -14
- package/src/api/blockManipulation/blockManipulation.test.ts +6 -3
- package/src/api/blockManipulation/blockManipulation.ts +7 -6
- package/src/api/formatConversions/formatConversions.test.ts +13 -8
- package/src/api/formatConversions/formatConversions.ts +15 -12
- package/src/api/nodeConversions/__snapshots__/nodeConversions.test.ts.snap +292 -0
- package/src/api/nodeConversions/nodeConversions.test.ts +265 -10
- package/src/api/nodeConversions/nodeConversions.ts +199 -47
- package/src/api/nodeConversions/testUtil.ts +8 -4
- package/src/editor.module.css +5 -6
- package/src/extensions/Blocks/api/block.ts +229 -0
- package/src/extensions/Blocks/api/blockTypes.ts +158 -71
- package/src/extensions/Blocks/api/cursorPositionTypes.ts +5 -5
- package/src/extensions/Blocks/api/defaultBlocks.ts +44 -0
- package/src/extensions/Blocks/api/selectionTypes.ts +3 -3
- package/src/extensions/Blocks/api/serialization.ts +29 -0
- package/src/extensions/Blocks/index.ts +0 -8
- package/src/extensions/Blocks/nodes/Block.module.css +28 -16
- package/src/extensions/Blocks/nodes/BlockContainer.ts +8 -4
- package/src/extensions/Blocks/nodes/BlockContent/HeadingBlockContent/HeadingBlockContent.ts +4 -4
- package/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.ts +5 -5
- package/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.ts +100 -97
- package/src/extensions/Blocks/nodes/BlockContent/ParagraphBlockContent/ParagraphBlockContent.ts +4 -4
- package/src/extensions/DraggableBlocks/BlockSideMenuFactoryTypes.ts +11 -9
- package/src/extensions/DraggableBlocks/DraggableBlocksExtension.ts +6 -5
- package/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts +57 -14
- package/src/extensions/FormattingToolbar/FormattingToolbarExtension.ts +21 -16
- package/src/extensions/FormattingToolbar/FormattingToolbarFactoryTypes.ts +9 -5
- package/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts +38 -58
- package/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts +19 -0
- package/src/extensions/Placeholder/PlaceholderExtension.ts +1 -0
- package/src/extensions/SlashMenu/BaseSlashMenuItem.ts +5 -2
- package/src/extensions/SlashMenu/SlashMenuExtension.ts +37 -33
- package/src/extensions/SlashMenu/defaultSlashMenuItems.tsx +14 -10
- package/src/extensions/SlashMenu/index.ts +2 -2
- package/src/index.ts +4 -0
- package/src/shared/plugins/suggestion/SuggestionPlugin.ts +29 -13
- package/types/src/BlockNoteEditor.d.ts +38 -23
- package/types/src/BlockNoteExtensions.d.ts +15 -8
- package/types/src/api/blockManipulation/blockManipulation.d.ts +4 -4
- package/types/src/api/formatConversions/formatConversions.d.ts +5 -5
- package/types/src/api/nodeConversions/nodeConversions.d.ts +3 -3
- package/types/src/api/nodeConversions/testUtil.d.ts +2 -2
- package/types/src/extensions/Blocks/api/block.d.ts +2 -4
- package/types/src/extensions/Blocks/api/blockTypes.d.ts +77 -33
- package/types/src/extensions/Blocks/api/cursorPositionTypes.d.ts +5 -5
- package/types/src/extensions/Blocks/api/defaultBlocks.d.ts +4 -4
- package/types/src/extensions/Blocks/api/selectionTypes.d.ts +3 -3
- package/types/src/extensions/Blocks/api/serialization.d.ts +2 -0
- package/types/src/extensions/Blocks/nodes/BlockContainer.d.ts +3 -3
- package/types/src/extensions/Blocks/nodes/BlockContent/HeadingBlockContent/HeadingBlockContent.d.ts +1 -2
- package/types/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.d.ts +1 -2
- package/types/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.d.ts +1 -2
- package/types/src/extensions/Blocks/nodes/BlockContent/ParagraphBlockContent/ParagraphBlockContent.d.ts +1 -2
- package/types/src/extensions/DraggableBlocks/BlockSideMenuFactoryTypes.d.ts +7 -7
- package/types/src/extensions/DraggableBlocks/DraggableBlocksExtension.d.ts +5 -4
- package/types/src/extensions/DraggableBlocks/DraggableBlocksPlugin.d.ts +12 -11
- package/types/src/extensions/FormattingToolbar/FormattingToolbarExtension.d.ts +6 -5
- package/types/src/extensions/FormattingToolbar/FormattingToolbarFactoryTypes.d.ts +4 -3
- package/types/src/extensions/FormattingToolbar/FormattingToolbarPlugin.d.ts +16 -19
- package/types/src/extensions/Placeholder/localisation/index.d.ts +2 -0
- package/types/src/extensions/Placeholder/localisation/translation.d.ts +51 -0
- package/types/src/extensions/SlashMenu/BaseSlashMenuItem.d.ts +4 -3
- package/types/src/extensions/SlashMenu/SlashMenuExtension.d.ts +5 -4
- package/types/src/extensions/SlashMenu/defaultSlashMenuItems.d.ts +66 -1
- package/types/src/extensions/SlashMenu/index.d.ts +2 -2
- package/types/src/index.d.ts +4 -0
- package/types/src/shared/plugins/suggestion/SuggestionPlugin.d.ts +5 -4
- package/types/src/extensions/Blocks/api/alertBlock.d.ts +0 -13
- package/types/src/extensions/Blocks/api/alertBlock2.d.ts +0 -13
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "@tiptap/core";
|
|
7
7
|
import { EditorState, Plugin, PluginKey } from "prosemirror-state";
|
|
8
8
|
import { EditorView } from "prosemirror-view";
|
|
9
|
-
import { BlockNoteEditor } from "../..";
|
|
9
|
+
import { BlockNoteEditor, BlockSchema } from "../..";
|
|
10
10
|
import {
|
|
11
11
|
FormattingToolbar,
|
|
12
12
|
FormattingToolbarDynamicParams,
|
|
@@ -16,29 +16,20 @@ import {
|
|
|
16
16
|
|
|
17
17
|
// Same as TipTap bubblemenu plugin, but with these changes:
|
|
18
18
|
// https://github.com/ueberdosis/tiptap/pull/2596/files
|
|
19
|
-
export interface FormattingToolbarPluginProps {
|
|
19
|
+
export interface FormattingToolbarPluginProps<BSchema extends BlockSchema> {
|
|
20
20
|
pluginKey: PluginKey;
|
|
21
21
|
tiptapEditor: Editor;
|
|
22
|
-
editor: BlockNoteEditor
|
|
23
|
-
formattingToolbarFactory: FormattingToolbarFactory
|
|
24
|
-
shouldShow?:
|
|
25
|
-
| ((props: {
|
|
26
|
-
editor: BlockNoteEditor;
|
|
27
|
-
view: EditorView;
|
|
28
|
-
state: EditorState;
|
|
29
|
-
oldState?: EditorState;
|
|
30
|
-
from: number;
|
|
31
|
-
to: number;
|
|
32
|
-
}) => boolean)
|
|
33
|
-
| null;
|
|
22
|
+
editor: BlockNoteEditor<BSchema>;
|
|
23
|
+
formattingToolbarFactory: FormattingToolbarFactory<BSchema>;
|
|
34
24
|
}
|
|
35
25
|
|
|
36
|
-
export type FormattingToolbarViewProps
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
export type FormattingToolbarViewProps<BSchema extends BlockSchema> =
|
|
27
|
+
FormattingToolbarPluginProps<BSchema> & {
|
|
28
|
+
view: EditorView;
|
|
29
|
+
};
|
|
39
30
|
|
|
40
|
-
export class FormattingToolbarView {
|
|
41
|
-
public editor: BlockNoteEditor
|
|
31
|
+
export class FormattingToolbarView<BSchema extends BlockSchema> {
|
|
32
|
+
public editor: BlockNoteEditor<BSchema>;
|
|
42
33
|
private ttEditor: Editor;
|
|
43
34
|
|
|
44
35
|
public view: EditorView;
|
|
@@ -53,44 +44,43 @@ export class FormattingToolbarView {
|
|
|
53
44
|
|
|
54
45
|
public prevWasEditable: boolean | null = null;
|
|
55
46
|
|
|
56
|
-
public shouldShow:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
public shouldShow: (props: {
|
|
48
|
+
view: EditorView;
|
|
49
|
+
state: EditorState;
|
|
50
|
+
from: number;
|
|
51
|
+
to: number;
|
|
52
|
+
}) => boolean = ({ view, state, from, to }) => {
|
|
53
|
+
const { doc, selection } = state;
|
|
54
|
+
const { empty } = selection;
|
|
60
55
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
56
|
+
// Sometime check for `empty` is not enough.
|
|
57
|
+
// Doubleclick an empty paragraph returns a node size of 2.
|
|
58
|
+
// So we check also for an empty text size.
|
|
59
|
+
const isEmptyTextBlock =
|
|
60
|
+
!doc.textBetween(from, to).length && isTextSelection(state.selection);
|
|
66
61
|
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
return !(!view.hasFocus() || empty || isEmptyTextBlock);
|
|
63
|
+
};
|
|
69
64
|
|
|
70
65
|
constructor({
|
|
71
66
|
editor,
|
|
72
67
|
tiptapEditor,
|
|
73
68
|
formattingToolbarFactory,
|
|
74
69
|
view,
|
|
75
|
-
|
|
76
|
-
}: FormattingToolbarViewProps) {
|
|
70
|
+
}: FormattingToolbarViewProps<BSchema>) {
|
|
77
71
|
this.editor = editor;
|
|
78
72
|
this.ttEditor = tiptapEditor;
|
|
79
73
|
this.view = view;
|
|
80
74
|
|
|
81
75
|
this.formattingToolbar = formattingToolbarFactory(this.getStaticParams());
|
|
82
76
|
|
|
83
|
-
if (shouldShow) {
|
|
84
|
-
this.shouldShow = shouldShow;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
77
|
this.view.dom.addEventListener("mousedown", this.viewMousedownHandler);
|
|
88
78
|
this.view.dom.addEventListener("mouseup", this.viewMouseupHandler);
|
|
89
79
|
this.view.dom.addEventListener("dragstart", this.dragstartHandler);
|
|
90
80
|
|
|
91
81
|
this.ttEditor.on("focus", this.focusHandler);
|
|
92
82
|
this.ttEditor.on("blur", this.blurHandler);
|
|
93
|
-
|
|
83
|
+
|
|
94
84
|
document.addEventListener("scroll", this.scrollHandler);
|
|
95
85
|
}
|
|
96
86
|
|
|
@@ -120,11 +110,15 @@ export class FormattingToolbarView {
|
|
|
120
110
|
return;
|
|
121
111
|
}
|
|
122
112
|
|
|
113
|
+
// Checks if the focus is moving to an element outside the editor. If it is,
|
|
114
|
+
// the toolbar is hidden.
|
|
123
115
|
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
116
|
+
// An element is clicked.
|
|
117
|
+
event &&
|
|
118
|
+
event.relatedTarget &&
|
|
119
|
+
// Element is outside the toolbar.
|
|
120
|
+
(this.formattingToolbar.element === (event.relatedTarget as Node) ||
|
|
121
|
+
this.formattingToolbar.element?.contains(event.relatedTarget as Node))
|
|
128
122
|
) {
|
|
129
123
|
return;
|
|
130
124
|
}
|
|
@@ -163,10 +157,8 @@ export class FormattingToolbarView {
|
|
|
163
157
|
const to = Math.max(...ranges.map((range) => range.$to.pos));
|
|
164
158
|
|
|
165
159
|
const shouldShow = this.shouldShow?.({
|
|
166
|
-
editor: this.editor,
|
|
167
160
|
view,
|
|
168
161
|
state,
|
|
169
|
-
oldState,
|
|
170
162
|
from,
|
|
171
163
|
to,
|
|
172
164
|
});
|
|
@@ -181,12 +173,6 @@ export class FormattingToolbarView {
|
|
|
181
173
|
this.formattingToolbar.render(this.getDynamicParams(), true);
|
|
182
174
|
this.toolbarIsOpen = true;
|
|
183
175
|
|
|
184
|
-
// TODO: Is this necessary? Also for other menu plugins.
|
|
185
|
-
// Listener stops focus moving to the menu on click.
|
|
186
|
-
this.formattingToolbar.element!.addEventListener("mousedown", (event) =>
|
|
187
|
-
event.preventDefault()
|
|
188
|
-
);
|
|
189
|
-
|
|
190
176
|
return;
|
|
191
177
|
}
|
|
192
178
|
|
|
@@ -209,12 +195,6 @@ export class FormattingToolbarView {
|
|
|
209
195
|
this.formattingToolbar.hide();
|
|
210
196
|
this.toolbarIsOpen = false;
|
|
211
197
|
|
|
212
|
-
// Listener stops focus moving to the menu on click.
|
|
213
|
-
this.formattingToolbar.element!.removeEventListener(
|
|
214
|
-
"mousedown",
|
|
215
|
-
(event) => event.preventDefault()
|
|
216
|
-
);
|
|
217
|
-
|
|
218
198
|
return;
|
|
219
199
|
}
|
|
220
200
|
}
|
|
@@ -250,7 +230,7 @@ export class FormattingToolbarView {
|
|
|
250
230
|
return posToDOMRect(this.ttEditor.view, from, to);
|
|
251
231
|
}
|
|
252
232
|
|
|
253
|
-
getStaticParams(): FormattingToolbarStaticParams {
|
|
233
|
+
getStaticParams(): FormattingToolbarStaticParams<BSchema> {
|
|
254
234
|
return {
|
|
255
235
|
editor: this.editor,
|
|
256
236
|
};
|
|
@@ -263,8 +243,8 @@ export class FormattingToolbarView {
|
|
|
263
243
|
}
|
|
264
244
|
}
|
|
265
245
|
|
|
266
|
-
export const createFormattingToolbarPlugin = (
|
|
267
|
-
options: FormattingToolbarPluginProps
|
|
246
|
+
export const createFormattingToolbarPlugin = <BSchema extends BlockSchema>(
|
|
247
|
+
options: FormattingToolbarPluginProps<BSchema>
|
|
268
248
|
) => {
|
|
269
249
|
return new Plugin({
|
|
270
250
|
key: new PluginKey("FormattingToolbarPlugin"),
|
|
@@ -57,6 +57,7 @@ class HyperlinkToolbarView {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
this.editor.view.dom.addEventListener("mouseover", this.mouseOverHandler);
|
|
60
|
+
document.addEventListener("click", this.clickHandler, true);
|
|
60
61
|
document.addEventListener("scroll", this.scrollHandler);
|
|
61
62
|
}
|
|
62
63
|
|
|
@@ -101,6 +102,24 @@ class HyperlinkToolbarView {
|
|
|
101
102
|
return false;
|
|
102
103
|
};
|
|
103
104
|
|
|
105
|
+
clickHandler = (event: MouseEvent) => {
|
|
106
|
+
if (
|
|
107
|
+
// Toolbar is open.
|
|
108
|
+
this.hyperlinkMark &&
|
|
109
|
+
// An element is clicked.
|
|
110
|
+
event &&
|
|
111
|
+
event.target &&
|
|
112
|
+
// Element is outside the editor.
|
|
113
|
+
this.editor.view.dom !== (event.target as Node) &&
|
|
114
|
+
!this.editor.view.dom.contains(event.target as Node) &&
|
|
115
|
+
// Element is outside the toolbar.
|
|
116
|
+
this.hyperlinkToolbar.element !== (event.target as Node) &&
|
|
117
|
+
!this.hyperlinkToolbar.element?.contains(event.target as Node)
|
|
118
|
+
) {
|
|
119
|
+
this.hyperlinkToolbar.hide();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
104
123
|
scrollHandler = () => {
|
|
105
124
|
if (this.hyperlinkMark !== undefined) {
|
|
106
125
|
this.hyperlinkToolbar.render(this.getDynamicParams(), false);
|
|
@@ -72,6 +72,7 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
|
|
|
72
72
|
if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
|
|
73
73
|
const classes = [this.options.emptyNodeClass];
|
|
74
74
|
|
|
75
|
+
// TODO: Doesn't work?
|
|
75
76
|
if (this.editor.isEmpty) {
|
|
76
77
|
classes.push(this.options.emptyEditorClass);
|
|
77
78
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { SuggestionItem } from "../../shared/plugins/suggestion/SuggestionItem";
|
|
2
2
|
import { BlockNoteEditor } from "../../BlockNoteEditor";
|
|
3
|
+
import { BlockSchema } from "../Blocks/api/blockTypes";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* A class that defines a slash command (/<command>).
|
|
6
7
|
*
|
|
7
8
|
* (Not to be confused with ProseMirror commands nor TipTap commands.)
|
|
8
9
|
*/
|
|
9
|
-
export class BaseSlashMenuItem
|
|
10
|
+
export class BaseSlashMenuItem<
|
|
11
|
+
BSchema extends BlockSchema
|
|
12
|
+
> extends SuggestionItem {
|
|
10
13
|
/**
|
|
11
14
|
* Constructs a new slash-command.
|
|
12
15
|
*
|
|
@@ -16,7 +19,7 @@ export class BaseSlashMenuItem extends SuggestionItem {
|
|
|
16
19
|
*/
|
|
17
20
|
constructor(
|
|
18
21
|
public readonly name: string,
|
|
19
|
-
public readonly execute: (editor: BlockNoteEditor) => void,
|
|
22
|
+
public readonly execute: (editor: BlockNoteEditor<BSchema>) => void,
|
|
20
23
|
public readonly aliases: string[] = []
|
|
21
24
|
) {
|
|
22
25
|
super(name, (query: string): boolean => {
|
|
@@ -4,46 +4,50 @@ import { createSuggestionPlugin } from "../../shared/plugins/suggestion/Suggesti
|
|
|
4
4
|
import { SuggestionsMenuFactory } from "../../shared/plugins/suggestion/SuggestionsMenuFactoryTypes";
|
|
5
5
|
import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
|
|
6
6
|
import { BlockNoteEditor } from "../../BlockNoteEditor";
|
|
7
|
+
import { BlockSchema } from "../Blocks/api/blockTypes";
|
|
7
8
|
|
|
8
|
-
export type SlashMenuOptions = {
|
|
9
|
-
editor: BlockNoteEditor | undefined;
|
|
10
|
-
commands: BaseSlashMenuItem[] | undefined;
|
|
9
|
+
export type SlashMenuOptions<BSchema extends BlockSchema> = {
|
|
10
|
+
editor: BlockNoteEditor<BSchema> | undefined;
|
|
11
|
+
commands: BaseSlashMenuItem<BSchema>[] | undefined;
|
|
11
12
|
slashMenuFactory: SuggestionsMenuFactory<any> | undefined;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
export const SlashMenuPluginKey = new PluginKey("suggestions-slash-commands");
|
|
15
16
|
|
|
16
|
-
export const
|
|
17
|
-
|
|
17
|
+
export const createSlashMenuExtension = <BSchema extends BlockSchema>() =>
|
|
18
|
+
Extension.create<SlashMenuOptions<BSchema>>({
|
|
19
|
+
name: "slash-command",
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
addOptions() {
|
|
22
|
+
return {
|
|
23
|
+
editor: undefined,
|
|
24
|
+
commands: undefined,
|
|
25
|
+
slashMenuFactory: undefined,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
addProseMirrorPlugins() {
|
|
30
|
+
if (!this.options.slashMenuFactory || !this.options.commands) {
|
|
31
|
+
throw new Error("required args not defined for SlashMenuExtension");
|
|
32
|
+
}
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
const commands = this.options.commands;
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
36
|
+
return [
|
|
37
|
+
createSuggestionPlugin<BaseSlashMenuItem<BSchema>, BSchema>({
|
|
38
|
+
pluginKey: SlashMenuPluginKey,
|
|
39
|
+
editor: this.options.editor!,
|
|
40
|
+
defaultTriggerCharacter: "/",
|
|
41
|
+
suggestionsMenuFactory: this.options.slashMenuFactory!,
|
|
42
|
+
items: (query) => {
|
|
43
|
+
return commands.filter((cmd: BaseSlashMenuItem<BSchema>) =>
|
|
44
|
+
cmd.match(query)
|
|
45
|
+
);
|
|
46
|
+
},
|
|
47
|
+
onSelectItem: ({ item, editor }) => {
|
|
48
|
+
item.execute(editor);
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
];
|
|
52
|
+
},
|
|
53
|
+
});
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
|
|
2
|
-
import { PartialBlock } from "../Blocks/api/blockTypes";
|
|
3
1
|
import { BlockNoteEditor } from "../../BlockNoteEditor";
|
|
2
|
+
import { PartialBlock } from "../Blocks/api/blockTypes";
|
|
3
|
+
import { DefaultBlockSchema } from "../Blocks/api/defaultBlocks";
|
|
4
|
+
import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
|
|
4
5
|
|
|
5
|
-
function insertOrUpdateBlock
|
|
6
|
+
function insertOrUpdateBlock<BSchema extends DefaultBlockSchema>(
|
|
7
|
+
editor: BlockNoteEditor<BSchema>,
|
|
8
|
+
block: PartialBlock<BSchema>
|
|
9
|
+
) {
|
|
6
10
|
const currentBlock = editor.getTextCursorPosition().block;
|
|
7
11
|
|
|
8
12
|
if (
|
|
@@ -21,9 +25,9 @@ function insertOrUpdateBlock(editor: BlockNoteEditor, block: PartialBlock) {
|
|
|
21
25
|
/**
|
|
22
26
|
* An array containing commands for creating all default blocks.
|
|
23
27
|
*/
|
|
24
|
-
export const defaultSlashMenuItems
|
|
28
|
+
export const defaultSlashMenuItems = [
|
|
25
29
|
// Command for creating a level 1 heading
|
|
26
|
-
new BaseSlashMenuItem(
|
|
30
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
27
31
|
"Heading",
|
|
28
32
|
(editor) =>
|
|
29
33
|
insertOrUpdateBlock(editor, {
|
|
@@ -34,7 +38,7 @@ export const defaultSlashMenuItems: BaseSlashMenuItem[] = [
|
|
|
34
38
|
),
|
|
35
39
|
|
|
36
40
|
// Command for creating a level 2 heading
|
|
37
|
-
new BaseSlashMenuItem(
|
|
41
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
38
42
|
"Heading 2",
|
|
39
43
|
(editor) =>
|
|
40
44
|
insertOrUpdateBlock(editor, {
|
|
@@ -45,7 +49,7 @@ export const defaultSlashMenuItems: BaseSlashMenuItem[] = [
|
|
|
45
49
|
),
|
|
46
50
|
|
|
47
51
|
// Command for creating a level 3 heading
|
|
48
|
-
new BaseSlashMenuItem(
|
|
52
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
49
53
|
"Heading 3",
|
|
50
54
|
(editor) =>
|
|
51
55
|
insertOrUpdateBlock(editor, {
|
|
@@ -56,7 +60,7 @@ export const defaultSlashMenuItems: BaseSlashMenuItem[] = [
|
|
|
56
60
|
),
|
|
57
61
|
|
|
58
62
|
// Command for creating an ordered list
|
|
59
|
-
new BaseSlashMenuItem(
|
|
63
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
60
64
|
"Numbered List",
|
|
61
65
|
(editor) =>
|
|
62
66
|
insertOrUpdateBlock(editor, {
|
|
@@ -66,7 +70,7 @@ export const defaultSlashMenuItems: BaseSlashMenuItem[] = [
|
|
|
66
70
|
),
|
|
67
71
|
|
|
68
72
|
// Command for creating a bullet list
|
|
69
|
-
new BaseSlashMenuItem(
|
|
73
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
70
74
|
"Bullet List",
|
|
71
75
|
(editor) =>
|
|
72
76
|
insertOrUpdateBlock(editor, {
|
|
@@ -76,7 +80,7 @@ export const defaultSlashMenuItems: BaseSlashMenuItem[] = [
|
|
|
76
80
|
),
|
|
77
81
|
|
|
78
82
|
// Command for creating a paragraph (pretty useless)
|
|
79
|
-
new BaseSlashMenuItem(
|
|
83
|
+
new BaseSlashMenuItem<DefaultBlockSchema>(
|
|
80
84
|
"Paragraph",
|
|
81
85
|
(editor) =>
|
|
82
86
|
insertOrUpdateBlock(editor, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultSlashMenuItems } from "./defaultSlashMenuItems";
|
|
2
|
-
import {
|
|
2
|
+
import { createSlashMenuExtension } from "./SlashMenuExtension";
|
|
3
3
|
import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
|
|
4
4
|
|
|
5
|
-
export { defaultSlashMenuItems, BaseSlashMenuItem,
|
|
5
|
+
export { defaultSlashMenuItems, BaseSlashMenuItem, createSlashMenuExtension };
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from "./BlockNoteEditor";
|
|
2
2
|
export * from "./BlockNoteExtensions";
|
|
3
|
+
export * from "./extensions/Blocks/api/block";
|
|
3
4
|
export * from "./extensions/Blocks/api/blockTypes";
|
|
5
|
+
export * from "./extensions/Blocks/api/defaultBlocks";
|
|
4
6
|
export * from "./extensions/DraggableBlocks/BlockSideMenuFactoryTypes";
|
|
5
7
|
export * from "./extensions/FormattingToolbar/FormattingToolbarFactoryTypes";
|
|
6
8
|
export * from "./extensions/HyperlinkToolbar/HyperlinkToolbarFactoryTypes";
|
|
@@ -10,3 +12,5 @@ export * from "./shared/EditorElement";
|
|
|
10
12
|
export type { SuggestionItem } from "./shared/plugins/suggestion/SuggestionItem";
|
|
11
13
|
export * from "./shared/plugins/suggestion/SuggestionsMenuFactoryTypes";
|
|
12
14
|
export * from "./extensions/Blocks/api/inlineContentTypes";
|
|
15
|
+
export * from "./extensions/Blocks/api/serialization";
|
|
16
|
+
export * as blockStyles from "./extensions/Blocks/nodes/Block.module.css";
|
|
@@ -10,8 +10,12 @@ import {
|
|
|
10
10
|
} from "./SuggestionsMenuFactoryTypes";
|
|
11
11
|
import { SuggestionItem } from "./SuggestionItem";
|
|
12
12
|
import { BlockNoteEditor } from "../../../BlockNoteEditor";
|
|
13
|
+
import { BlockSchema } from "../../../extensions/Blocks/api/blockTypes";
|
|
13
14
|
|
|
14
|
-
export type SuggestionPluginOptions<
|
|
15
|
+
export type SuggestionPluginOptions<
|
|
16
|
+
T extends SuggestionItem,
|
|
17
|
+
BSchema extends BlockSchema
|
|
18
|
+
> = {
|
|
15
19
|
/**
|
|
16
20
|
* The name of the plugin.
|
|
17
21
|
*
|
|
@@ -22,7 +26,7 @@ export type SuggestionPluginOptions<T extends SuggestionItem> = {
|
|
|
22
26
|
/**
|
|
23
27
|
* The BlockNote editor.
|
|
24
28
|
*/
|
|
25
|
-
editor: BlockNoteEditor
|
|
29
|
+
editor: BlockNoteEditor<BSchema>;
|
|
26
30
|
|
|
27
31
|
/**
|
|
28
32
|
* The character that should trigger the suggestion menu to pop up (e.g. a '/' for commands), when typed by the user.
|
|
@@ -38,7 +42,7 @@ export type SuggestionPluginOptions<T extends SuggestionItem> = {
|
|
|
38
42
|
* this should be done manually. The `editor` and `range` properties passed
|
|
39
43
|
* to the callback function might come in handy when doing this.
|
|
40
44
|
*/
|
|
41
|
-
onSelectItem?: (props: { item: T; editor: BlockNoteEditor }) => void;
|
|
45
|
+
onSelectItem?: (props: { item: T; editor: BlockNoteEditor<BSchema> }) => void;
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
48
|
* A function that should supply the plugin with items to suggest, based on a certain query string.
|
|
@@ -81,15 +85,21 @@ function getDefaultPluginState<
|
|
|
81
85
|
};
|
|
82
86
|
}
|
|
83
87
|
|
|
84
|
-
type SuggestionPluginViewOptions<
|
|
85
|
-
|
|
88
|
+
type SuggestionPluginViewOptions<
|
|
89
|
+
T extends SuggestionItem,
|
|
90
|
+
BSchema extends BlockSchema
|
|
91
|
+
> = {
|
|
92
|
+
editor: BlockNoteEditor<BSchema>;
|
|
86
93
|
pluginKey: PluginKey;
|
|
87
|
-
onSelectItem: (props: { item: T; editor: BlockNoteEditor }) => void;
|
|
94
|
+
onSelectItem: (props: { item: T; editor: BlockNoteEditor<BSchema> }) => void;
|
|
88
95
|
suggestionsMenuFactory: SuggestionsMenuFactory<T>;
|
|
89
96
|
};
|
|
90
97
|
|
|
91
|
-
class SuggestionPluginView<
|
|
92
|
-
|
|
98
|
+
class SuggestionPluginView<
|
|
99
|
+
T extends SuggestionItem,
|
|
100
|
+
BSchema extends BlockSchema
|
|
101
|
+
> {
|
|
102
|
+
editor: BlockNoteEditor<BSchema>;
|
|
93
103
|
pluginKey: PluginKey;
|
|
94
104
|
|
|
95
105
|
suggestionsMenu: SuggestionsMenu<T>;
|
|
@@ -102,7 +112,7 @@ class SuggestionPluginView<T extends SuggestionItem> {
|
|
|
102
112
|
pluginKey,
|
|
103
113
|
onSelectItem: selectItemCallback = () => {},
|
|
104
114
|
suggestionsMenuFactory,
|
|
105
|
-
}: SuggestionPluginViewOptions<T>) {
|
|
115
|
+
}: SuggestionPluginViewOptions<T, BSchema>) {
|
|
106
116
|
this.editor = editor;
|
|
107
117
|
this.pluginKey = pluginKey;
|
|
108
118
|
|
|
@@ -214,14 +224,17 @@ class SuggestionPluginView<T extends SuggestionItem> {
|
|
|
214
224
|
* @param options options for configuring the plugin
|
|
215
225
|
* @returns the prosemirror plugin
|
|
216
226
|
*/
|
|
217
|
-
export function createSuggestionPlugin<
|
|
227
|
+
export function createSuggestionPlugin<
|
|
228
|
+
T extends SuggestionItem,
|
|
229
|
+
BSchema extends BlockSchema
|
|
230
|
+
>({
|
|
218
231
|
pluginKey,
|
|
219
232
|
editor,
|
|
220
233
|
defaultTriggerCharacter,
|
|
221
234
|
suggestionsMenuFactory,
|
|
222
235
|
onSelectItem: selectItemCallback = () => {},
|
|
223
236
|
items = () => [],
|
|
224
|
-
}: SuggestionPluginOptions<T>) {
|
|
237
|
+
}: SuggestionPluginOptions<T, BSchema>) {
|
|
225
238
|
// Assertions
|
|
226
239
|
if (defaultTriggerCharacter.length !== 1) {
|
|
227
240
|
throw new Error("'char' should be a single character");
|
|
@@ -236,10 +249,13 @@ export function createSuggestionPlugin<T extends SuggestionItem>({
|
|
|
236
249
|
key: pluginKey,
|
|
237
250
|
|
|
238
251
|
view: (view: EditorView) =>
|
|
239
|
-
new SuggestionPluginView({
|
|
252
|
+
new SuggestionPluginView<T, BSchema>({
|
|
240
253
|
editor: editor,
|
|
241
254
|
pluginKey: pluginKey,
|
|
242
|
-
onSelectItem: (props: {
|
|
255
|
+
onSelectItem: (props: {
|
|
256
|
+
item: T;
|
|
257
|
+
editor: BlockNoteEditor<BSchema>;
|
|
258
|
+
}) => {
|
|
243
259
|
deactivate(view);
|
|
244
260
|
selectItemCallback(props);
|
|
245
261
|
},
|