@blocknote/core 0.8.4-alpha.0 → 0.8.5

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "homepage": "https://github.com/TypeCellOS/BlockNote",
4
4
  "private": false,
5
5
  "license": "MPL-2.0",
6
- "version": "0.8.4-alpha.0",
6
+ "version": "0.8.5",
7
7
  "files": [
8
8
  "dist",
9
9
  "types",
@@ -71,6 +71,10 @@
71
71
  "@tiptap/pm": "^2.0.3",
72
72
  "hast-util-from-dom": "^4.2.0",
73
73
  "lodash": "^4.17.21",
74
+ "prosemirror-model": "^1.18.3",
75
+ "prosemirror-state": "^1.4.3",
76
+ "prosemirror-transform": "^1.7.2",
77
+ "prosemirror-view": "^1.31.4",
74
78
  "rehype-parse": "^8.0.4",
75
79
  "rehype-remark": "^9.1.2",
76
80
  "rehype-stringify": "^9.0.3",
@@ -89,7 +93,6 @@
89
93
  "@types/lodash": "^4.14.179",
90
94
  "@types/uuid": "^8.3.4",
91
95
  "eslint": "^8.10.0",
92
- "eslint-config-react-app": "^7.0.0",
93
96
  "jsdom": "^21.1.0",
94
97
  "prettier": "^2.7.1",
95
98
  "typescript": "^5.0.4",
@@ -99,16 +102,12 @@
99
102
  },
100
103
  "eslintConfig": {
101
104
  "extends": [
102
- "react-app",
103
- "react-app/jest"
104
- ],
105
- "rules": {
106
- "curly": 1
107
- }
105
+ "../../.eslintrc.js"
106
+ ]
108
107
  },
109
108
  "publishConfig": {
110
109
  "access": "public",
111
110
  "registry": "https://registry.npmjs.org/"
112
111
  },
113
- "gitHead": "fdb190e0948be3d79b12357db6a56af64413f651"
112
+ "gitHead": "5bdbc6a60d4142a6a6a5d85cc8f8a74fdbecf78f"
114
113
  }
@@ -205,12 +205,16 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
205
205
 
206
206
  this.schema = newOptions.blockSchema;
207
207
 
208
- const initialContent = newOptions.initialContent || [
209
- {
210
- type: "paragraph",
211
- id: UniqueID.options.generateID(),
212
- },
213
- ];
208
+ const initialContent =
209
+ newOptions.initialContent ||
210
+ (options.collaboration
211
+ ? undefined
212
+ : [
213
+ {
214
+ type: "paragraph",
215
+ id: UniqueID.options.generateID(),
216
+ },
217
+ ]);
214
218
 
215
219
  const tiptapOptions: EditorOptions = {
216
220
  ...blockNoteTipTapOptions,
@@ -220,6 +224,10 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
220
224
  this.ready = true;
221
225
  },
222
226
  onBeforeCreate(editor) {
227
+ if (!initialContent) {
228
+ // when using collaboration
229
+ return;
230
+ }
223
231
  // we have to set the initial content here, because now we can use the editor schema
224
232
  // which has been created at this point
225
233
  const schema = editor.editor.schema;
@@ -12,27 +12,45 @@ export type BlockInfo = {
12
12
  };
13
13
 
14
14
  /**
15
- * Retrieves information regarding the most nested block node in a ProseMirror doc, that a given position lies in.
15
+ * Retrieves information regarding the nearest blockContainer node in a
16
+ * ProseMirror doc, relative to a position.
16
17
  * @param doc The ProseMirror doc.
17
- * @param posInBlock A position somewhere within a block node.
18
- * @returns A BlockInfo object for the block the given position is in, or undefined if the position is not in a block
19
- * for the given doc.
18
+ * @param pos An integer position.
19
+ * @returns A BlockInfo object for the nearest blockContainer node.
20
20
  */
21
- export function getBlockInfoFromPos(
22
- doc: Node,
23
- posInBlock: number
24
- ): BlockInfo | undefined {
25
- if (posInBlock < 0 || posInBlock > doc.nodeSize) {
26
- return undefined;
21
+ export function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo {
22
+ // If the position is outside the outer block group, we need to move it to the
23
+ // nearest block. This happens when the collaboration plugin is active, where
24
+ // the selection is placed at the very end of the doc.
25
+ const outerBlockGroupStartPos = 1;
26
+ const outerBlockGroupEndPos = doc.nodeSize - 2;
27
+ if (pos <= outerBlockGroupStartPos) {
28
+ pos = outerBlockGroupStartPos + 1;
29
+
30
+ while (
31
+ doc.resolve(pos).parent.type.name !== "blockContainer" &&
32
+ pos < outerBlockGroupEndPos
33
+ ) {
34
+ pos++;
35
+ }
36
+ } else if (pos >= outerBlockGroupEndPos) {
37
+ pos = outerBlockGroupEndPos - 1;
38
+
39
+ while (
40
+ doc.resolve(pos).parent.type.name !== "blockContainer" &&
41
+ pos > outerBlockGroupStartPos
42
+ ) {
43
+ pos--;
44
+ }
27
45
  }
28
46
 
29
47
  // This gets triggered when a node selection on a block is active, i.e. when
30
48
  // you drag and drop a block.
31
- if (doc.resolve(posInBlock).parent.type.name === "blockGroup") {
32
- posInBlock++;
49
+ if (doc.resolve(pos).parent.type.name === "blockGroup") {
50
+ pos++;
33
51
  }
34
52
 
35
- const $pos = doc.resolve(posInBlock);
53
+ const $pos = doc.resolve(pos);
36
54
 
37
55
  const maxDepth = $pos.depth;
38
56
  let node = $pos.node(maxDepth);
@@ -40,7 +58,9 @@ export function getBlockInfoFromPos(
40
58
 
41
59
  while (true) {
42
60
  if (depth < 0) {
43
- return undefined;
61
+ throw new Error(
62
+ "Could not find blockContainer node. This can only happen if the underlying BlockNote schema has been edited."
63
+ );
44
64
  }
45
65
 
46
66
  if (node.type.name === "blockContainer") {
@@ -0,0 +1 @@
1
+ {"version":"0.28.5","results":[["/api/formatConversions/formatConversions.test.ts",{"duration":109,"failed":false}],["/api/nodeConversions/nodeConversions.test.ts",{"duration":32,"failed":false}],["/api/blockManipulation/blockManipulation.test.ts",{"duration":38,"failed":false}],["/BlockNoteEditor.test.ts",{"duration":4,"failed":false}]]}
@@ -0,0 +1,11 @@
1
+ type StringKeyOf<T> = Extract<keyof T, string>;
2
+ type CallbackType<T extends Record<string, any>, EventName extends StringKeyOf<T>> = T[EventName] extends any[] ? T[EventName] : [T[EventName]];
3
+ type CallbackFunction<T extends Record<string, any>, EventName extends StringKeyOf<T>> = (...props: CallbackType<T, EventName>) => any;
4
+ export declare class EventEmitter<T extends Record<string, any>> {
5
+ private callbacks;
6
+ on<EventName extends StringKeyOf<T>>(event: EventName, fn: CallbackFunction<T, EventName>): this;
7
+ protected emit<EventName extends StringKeyOf<T>>(event: EventName, ...args: CallbackType<T, EventName>): this;
8
+ off<EventName extends StringKeyOf<T>>(event: EventName, fn?: CallbackFunction<T, EventName>): this;
9
+ protected removeAllListeners(): void;
10
+ }
11
+ export {};
@@ -10,10 +10,10 @@ export type BlockInfo = {
10
10
  depth: number;
11
11
  };
12
12
  /**
13
- * Retrieves information regarding the most nested block node in a ProseMirror doc, that a given position lies in.
13
+ * Retrieves information regarding the nearest blockContainer node in a
14
+ * ProseMirror doc, relative to a position.
14
15
  * @param doc The ProseMirror doc.
15
- * @param posInBlock A position somewhere within a block node.
16
- * @returns A BlockInfo object for the block the given position is in, or undefined if the position is not in a block
17
- * for the given doc.
16
+ * @param pos An integer position.
17
+ * @returns A BlockInfo object for the nearest blockContainer node.
18
18
  */
19
- export declare function getBlockInfoFromPos(doc: Node, posInBlock: number): BlockInfo | undefined;
19
+ export declare function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo;
@@ -0,0 +1,16 @@
1
+ import { Editor, Extension } from "@tiptap/core";
2
+ import { BlockSideMenuFactory } from "./BlockSideMenuFactoryTypes";
3
+ import { BlockNoteEditor } from "../../BlockNoteEditor";
4
+ import { BlockSchema } from "../Blocks/api/blockTypes";
5
+ export type DraggableBlocksOptions<BSchema extends BlockSchema> = {
6
+ tiptapEditor: Editor;
7
+ editor: BlockNoteEditor<BSchema>;
8
+ blockSideMenuFactory: BlockSideMenuFactory<BSchema>;
9
+ };
10
+ /**
11
+ * This extension adds a menu to the side of blocks which features various BlockNote functions such as adding and
12
+ * removing blocks. More importantly, it adds a drag handle which allows the user to drag and drop blocks.
13
+ *
14
+ * code based on https://github.com/ueberdosis/tiptap/issues/323#issuecomment-506637799
15
+ */
16
+ export declare const createDraggableBlocksExtension: <BSchema extends Record<string, import("../Blocks/api/blockTypes").BlockSpec<string, import("../Blocks/api/blockTypes").PropSchema>>>() => Extension<DraggableBlocksOptions<BSchema>, any>;
@@ -0,0 +1,55 @@
1
+ import { PluginView } from "@tiptap/pm/state";
2
+ import { Plugin, PluginKey } from "prosemirror-state";
3
+ import { BlockNoteEditor } from "../../BlockNoteEditor";
4
+ import { BaseUiElementCallbacks, BaseUiElementState } from "../../shared/BaseUiElementTypes";
5
+ import { Block, BlockSchema } from "../Blocks/api/blockTypes";
6
+ import { Editor } from "@tiptap/core";
7
+ export type SideMenuCallbacks = BaseUiElementCallbacks & {
8
+ addBlock: () => void;
9
+ freezeMenu: () => void;
10
+ unfreezeMenu: () => void;
11
+ blockDragStart: (event: DragEvent) => void;
12
+ blockDragEnd: () => void;
13
+ };
14
+ export type SideMenuState<BSchema extends BlockSchema> = BaseUiElementState & {
15
+ block: Block<BSchema>;
16
+ };
17
+ export declare class SideMenuView<BSchema extends BlockSchema> implements PluginView {
18
+ editor: BlockNoteEditor<BSchema>;
19
+ ttEditor: Editor;
20
+ private sideMenuState?;
21
+ updateSideMenu: () => void;
22
+ horizontalPosAnchoredAtRoot: boolean;
23
+ horizontalPosAnchor: number;
24
+ hoveredBlock: HTMLElement | undefined;
25
+ isDragging: boolean;
26
+ menuFrozen: boolean;
27
+ constructor(editor: BlockNoteEditor<BSchema>, tiptapEditor: Editor, updateSideMenu: (sideMenuState: SideMenuState<BSchema>) => void);
28
+ /**
29
+ * Sets isDragging when dragging text.
30
+ */
31
+ onDragStart: () => void;
32
+ /**
33
+ * If the event is outside the editor contents,
34
+ * we dispatch a fake event, so that we can still drop the content
35
+ * when dragging / dropping to the side of the editor
36
+ */
37
+ onDrop: (event: DragEvent) => void;
38
+ /**
39
+ * If the event is outside the editor contents,
40
+ * we dispatch a fake event, so that we can still drop the content
41
+ * when dragging / dropping to the side of the editor
42
+ */
43
+ onDragOver: (event: DragEvent) => void;
44
+ onKeyDown: (_event: KeyboardEvent) => void;
45
+ onMouseDown: (_event: MouseEvent) => void;
46
+ onMouseMove: (event: MouseEvent) => void;
47
+ onScroll: () => void;
48
+ destroy(): void;
49
+ addBlock(): void;
50
+ }
51
+ export declare const sideMenuPluginKey: PluginKey<any>;
52
+ export declare function setupSideMenu<BSchema extends BlockSchema>(editor: BlockNoteEditor<BSchema>, tiptapEditor: Editor, updateSideMenu: (sideMenuState: SideMenuState<BSchema>) => void): {
53
+ plugin: Plugin;
54
+ callbacks: Omit<SideMenuCallbacks, "destroy">;
55
+ };
@@ -0,0 +1,24 @@
1
+ import { Selection } from "prosemirror-state";
2
+ import { Node, ResolvedPos, Slice } from "prosemirror-model";
3
+ import { Mappable } from "prosemirror-transform";
4
+ /**
5
+ * This class represents an editor selection which spans multiple nodes/blocks. It's currently only used to allow users
6
+ * to drag multiple blocks at the same time. Expects the selection anchor and head to be between nodes, i.e. just before
7
+ * the first target node and just after the last, and that anchor and head are at the same nesting level.
8
+ *
9
+ * Partially based on ProseMirror's NodeSelection implementation:
10
+ * (https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.ts)
11
+ * MultipleNodeSelection differs from NodeSelection in the following ways:
12
+ * 1. Stores which nodes are included in the selection instead of just a single node.
13
+ * 2. Already expects the selection to start just before the first target node and ends just after the last, while a
14
+ * NodeSelection automatically sets both anchor and head to just before the single target node.
15
+ */
16
+ export declare class MultipleNodeSelection extends Selection {
17
+ nodes: Array<Node>;
18
+ constructor($anchor: ResolvedPos, $head: ResolvedPos);
19
+ static create(doc: Node, from: number, to?: number): MultipleNodeSelection;
20
+ content(): Slice;
21
+ eq(selection: Selection): boolean;
22
+ map(doc: Node, mapping: Mappable): Selection;
23
+ toJSON(): any;
24
+ }
@@ -0,0 +1,11 @@
1
+ import { Extension } from "@tiptap/core";
2
+ import { BlockNoteEditor, BlockSchema } from "../..";
3
+ import { FormattingToolbarFactory } from "./FormattingToolbarFactoryTypes";
4
+ export type FormattingToolbarOptions<BSchema extends BlockSchema> = {
5
+ formattingToolbarFactory: FormattingToolbarFactory<BSchema>;
6
+ editor: BlockNoteEditor<BSchema>;
7
+ };
8
+ /**
9
+ * The menu that is displayed when selecting a piece of text.
10
+ */
11
+ export declare const createFormattingToolbarExtension: <BSchema extends Record<string, import("../..").BlockSpec<string, import("../..").PropSchema>>>() => Extension<FormattingToolbarOptions<BSchema>, any>;
@@ -0,0 +1,10 @@
1
+ import { EditorElement, ElementFactory } from "../../shared/EditorElement";
2
+ import { BlockNoteEditor } from "../../BlockNoteEditor";
3
+ import { BlockSchema } from "../Blocks/api/blockTypes";
4
+ export type FormattingToolbarStaticParams<BSchema extends BlockSchema> = {
5
+ editor: BlockNoteEditor<BSchema>;
6
+ getReferenceRect: () => DOMRect;
7
+ };
8
+ export type FormattingToolbarDynamicParams = {};
9
+ export type FormattingToolbar = EditorElement<FormattingToolbarDynamicParams>;
10
+ export type FormattingToolbarFactory<BSchema extends BlockSchema> = ElementFactory<FormattingToolbarStaticParams<BSchema>, FormattingToolbarDynamicParams>;
@@ -0,0 +1,8 @@
1
+ import { HyperlinkToolbarPluginProps } from "./HyperlinkToolbarPlugin";
2
+ /**
3
+ * This custom link includes a special menu for editing/deleting/opening the link.
4
+ * The menu will be triggered by hovering over the link with the mouse,
5
+ * or by moving the cursor inside the link text
6
+ */
7
+ declare const Hyperlink: import("@tiptap/core").Mark<HyperlinkToolbarPluginProps, any>;
8
+ export default Hyperlink;
@@ -0,0 +1,13 @@
1
+ import { Extension } from "@tiptap/core";
2
+ import { PluginKey } from "prosemirror-state";
3
+ import { SuggestionsMenuFactory } from "../../shared/plugins/suggestion/SuggestionsMenuFactoryTypes";
4
+ import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
5
+ import { BlockNoteEditor } from "../../BlockNoteEditor";
6
+ import { BlockSchema } from "../Blocks/api/blockTypes";
7
+ export type SlashMenuOptions<BSchema extends BlockSchema> = {
8
+ editor: BlockNoteEditor<BSchema> | undefined;
9
+ commands: BaseSlashMenuItem<BSchema>[] | undefined;
10
+ slashMenuFactory: SuggestionsMenuFactory<any> | undefined;
11
+ };
12
+ export declare const SlashMenuPluginKey: PluginKey<any>;
13
+ export declare const createSlashMenuExtension: <BSchema extends Record<string, import("../Blocks/api/blockTypes").BlockSpec<string, import("../Blocks/api/blockTypes").PropSchema>>>() => Extension<SlashMenuOptions<BSchema>, any>;
@@ -0,0 +1,3 @@
1
+ import { getDefaultSlashMenuItems } from "./defaultSlashMenuItems";
2
+ import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
3
+ export { getDefaultSlashMenuItems, BaseSlashMenuItem };
@@ -0,0 +1,12 @@
1
+ import { EditorElement, ElementFactory } from "../../EditorElement";
2
+ import { SuggestionItem } from "./SuggestionItem";
3
+ export type SuggestionsMenuStaticParams<T extends SuggestionItem> = {
4
+ itemCallback: (item: T) => void;
5
+ getReferenceRect: () => DOMRect;
6
+ };
7
+ export type SuggestionsMenuDynamicParams<T extends SuggestionItem> = {
8
+ items: T[];
9
+ keyboardHoveredItemIndex: number;
10
+ };
11
+ export type SuggestionsMenu<T extends SuggestionItem> = EditorElement<SuggestionsMenuDynamicParams<T>>;
12
+ export type SuggestionsMenuFactory<T extends SuggestionItem> = ElementFactory<SuggestionsMenuStaticParams<T>, SuggestionsMenuDynamicParams<T>>;