@blocknote/core 0.9.0 → 0.9.3

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 (44) hide show
  1. package/dist/blocknote.js +487 -431
  2. package/dist/blocknote.js.map +1 -1
  3. package/dist/blocknote.umd.cjs +5 -4
  4. package/dist/blocknote.umd.cjs.map +1 -1
  5. package/package.json +2 -2
  6. package/src/BlockNoteEditor.ts +2 -2
  7. package/src/BlockNoteExtensions.ts +24 -22
  8. package/src/api/blockManipulation/blockManipulation.test.ts +2 -2
  9. package/src/api/blockManipulation/blockManipulation.ts +1 -1
  10. package/src/api/formatConversions/formatConversions.test.ts +2 -2
  11. package/src/api/formatConversions/formatConversions.ts +47 -3
  12. package/src/api/nodeConversions/nodeConversions.test.ts +6 -6
  13. package/src/api/nodeConversions/nodeConversions.ts +6 -6
  14. package/src/extensions/Blocks/PreviousBlockTypePlugin.ts +2 -2
  15. package/src/extensions/Blocks/helpers/getBlockInfoFromPos.ts +27 -5
  16. package/src/extensions/Blocks/nodes/BlockContainer.ts +41 -17
  17. package/src/extensions/Blocks/nodes/BlockContent/HeadingBlockContent/HeadingBlockContent.ts +2 -0
  18. package/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.ts +2 -0
  19. package/src/extensions/Blocks/nodes/BlockContent/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.ts +2 -0
  20. package/src/extensions/SideMenu/MultipleNodeSelection.ts +3 -3
  21. package/src/extensions/SideMenu/SideMenuPlugin.ts +9 -9
  22. package/src/extensions/TrailingNode/TrailingNodeExtension.ts +13 -1
  23. package/src/extensions/UniqueID/UniqueID.ts +10 -9
  24. package/src/shared/EventEmitter.ts +1 -0
  25. package/src/shared/plugins/suggestion/SuggestionPlugin.ts +6 -2
  26. package/types/src/extensions/Blocks/NonEditableBlockPlugin.d.ts +2 -0
  27. package/types/src/extensions/Blocks/api/defaultProps.d.ts +14 -0
  28. package/types/src/extensions/Blocks/helpers/getBlockInfoFromPos.d.ts +9 -1
  29. package/types/src/extensions/Blocks/nodes/BlockContent/ImageBlockContent/Image.d.ts +6 -0
  30. package/types/src/extensions/Blocks/nodes/BlockContent/ImageBlockContent/ImageBlockContent.d.ts +37 -0
  31. package/types/src/extensions/ImageToolbar/ImageToolbarPlugin.d.ts +36 -0
  32. package/types/src/extensions/SideMenu/MultipleNodeSelection.d.ts +1 -1
  33. package/types/src/EventEmitter.d.ts +0 -11
  34. package/types/src/extensions/DraggableBlocks/BlockSideMenuFactoryTypes.d.ts +0 -0
  35. package/types/src/extensions/DraggableBlocks/DraggableBlocksExtension.d.ts +0 -16
  36. package/types/src/extensions/DraggableBlocks/DraggableBlocksPlugin.d.ts +0 -55
  37. package/types/src/extensions/DraggableBlocks/MultipleNodeSelection.d.ts +0 -24
  38. package/types/src/extensions/FormattingToolbar/FormattingToolbarExtension.d.ts +0 -11
  39. package/types/src/extensions/FormattingToolbar/FormattingToolbarFactoryTypes.d.ts +0 -10
  40. package/types/src/extensions/HyperlinkToolbar/HyperlinkMark.d.ts +0 -8
  41. package/types/src/extensions/HyperlinkToolbar/HyperlinkToolbarFactoryTypes.d.ts +0 -0
  42. package/types/src/extensions/SlashMenu/SlashMenuExtension.d.ts +0 -13
  43. package/types/src/extensions/SlashMenu/index.d.ts +0 -3
  44. package/types/src/shared/plugins/suggestion/SuggestionsMenuFactoryTypes.d.ts +0 -12
@@ -52,14 +52,15 @@ const UniqueID = Extension.create({
52
52
  types: [],
53
53
  generateID: () => {
54
54
  // Use mock ID if tests are running.
55
- if ((window as any).__TEST_OPTIONS) {
56
- if ((window as any).__TEST_OPTIONS.mockID === undefined) {
57
- (window as any).__TEST_OPTIONS.mockID = 0;
55
+ if (typeof window !== "undefined" && (window as any).__TEST_OPTIONS) {
56
+ const testOptions = (window as any).__TEST_OPTIONS;
57
+ if (testOptions.mockID === undefined) {
58
+ testOptions.mockID = 0;
58
59
  } else {
59
- (window as any).__TEST_OPTIONS.mockID++;
60
+ testOptions.mockID++;
60
61
  }
61
62
 
62
- return (window as any).__TEST_OPTIONS.mockID.toString() as string;
63
+ return testOptions.mockID.toString() as string;
63
64
  }
64
65
 
65
66
  return v4();
@@ -129,7 +130,7 @@ const UniqueID = Extension.create({
129
130
  const filterTransactions =
130
131
  this.options.filterTransaction &&
131
132
  transactions.some((tr) => {
132
- var _a, _b;
133
+ let _a, _b;
133
134
  return !((_b = (_a = this.options).filterTransaction) === null ||
134
135
  _b === void 0
135
136
  ? void 0
@@ -161,7 +162,7 @@ const UniqueID = Extension.create({
161
162
  .filter((id) => id !== null);
162
163
  const duplicatedNewIds = findDuplicates(newIds);
163
164
  newNodes.forEach(({ node, pos }) => {
164
- var _a;
165
+ let _a;
165
166
  // instead of checking `node.attrs[attributeName]` directly
166
167
  // we look at the current state of the node within `tr.doc`.
167
168
  // this helps to prevent adding new ids to the same node
@@ -196,7 +197,7 @@ const UniqueID = Extension.create({
196
197
  // we register a global drag handler to track the current drag source element
197
198
  view(view) {
198
199
  const handleDragstart = (event: any) => {
199
- var _a;
200
+ let _a;
200
201
  dragSourceElement = (
201
202
  (_a = view.dom.parentElement) === null || _a === void 0
202
203
  ? void 0
@@ -219,7 +220,7 @@ const UniqueID = Extension.create({
219
220
  // only create new ids for dropped content while holding `alt`
220
221
  // or content is dragged from another editor
221
222
  drop: (view, event: any) => {
222
- var _a;
223
+ let _a;
223
224
  if (
224
225
  dragSourceElement !== view.dom.parentElement ||
225
226
  ((_a = event.dataTransfer) === null || _a === void 0
@@ -11,6 +11,7 @@ type CallbackFunction<
11
11
  > = (...props: CallbackType<T, EventName>) => any;
12
12
 
13
13
  export class EventEmitter<T extends Record<string, any>> {
14
+ // eslint-disable-next-line @typescript-eslint/ban-types
14
15
  private callbacks: { [key: string]: Function[] } = {};
15
16
 
16
17
  public on<EventName extends StringKeyOf<T>>(
@@ -28,7 +28,9 @@ class SuggestionsMenuView<
28
28
  private readonly pluginKey: PluginKey,
29
29
  updateSuggestionsMenu: (
30
30
  suggestionsMenuState: SuggestionsMenuState<T>
31
- ) => void = () => {}
31
+ ) => void = () => {
32
+ // noop
33
+ }
32
34
  ) {
33
35
  this.pluginState = getDefaultPluginState<T>();
34
36
 
@@ -158,7 +160,9 @@ export const setupSuggestionsMenu = <
158
160
  onSelectItem: (props: {
159
161
  item: T;
160
162
  editor: BlockNoteEditor<BSchema>;
161
- }) => void = () => {}
163
+ }) => void = () => {
164
+ // noop
165
+ }
162
166
  ) => {
163
167
  // Assertions
164
168
  if (defaultTriggerCharacter.length !== 1) {
@@ -0,0 +1,2 @@
1
+ import { Plugin } from "prosemirror-state";
2
+ export declare const NonEditableBlockPlugin: () => Plugin<any>;
@@ -0,0 +1,14 @@
1
+ import { Props } from "./blockTypes";
2
+ export declare const defaultProps: {
3
+ backgroundColor: {
4
+ default: "transparent";
5
+ };
6
+ textColor: {
7
+ default: "black";
8
+ };
9
+ textAlignment: {
10
+ default: "left";
11
+ values: readonly ["left", "center", "right", "justify"];
12
+ };
13
+ };
14
+ export type DefaultProps = Props<typeof defaultProps>;
@@ -1,14 +1,22 @@
1
1
  import { Node, NodeType } from "prosemirror-model";
2
- export type BlockInfo = {
2
+ export type BlockInfoWithoutPositions = {
3
3
  id: string;
4
4
  node: Node;
5
5
  contentNode: Node;
6
6
  contentType: NodeType;
7
7
  numChildBlocks: number;
8
+ };
9
+ export type BlockInfo = BlockInfoWithoutPositions & {
8
10
  startPos: number;
9
11
  endPos: number;
10
12
  depth: number;
11
13
  };
14
+ /**
15
+ * Helper function for `getBlockInfoFromPos`, returns information regarding
16
+ * provided blockContainer node.
17
+ * @param blockContainer The blockContainer node to retrieve info for.
18
+ */
19
+ export declare function getBlockInfo(blockContainer: Node): BlockInfoWithoutPositions;
12
20
  /**
13
21
  * Retrieves information regarding the nearest blockContainer node in a
14
22
  * ProseMirror doc, relative to a position.
@@ -0,0 +1,6 @@
1
+ import "@uppy/core/dist/style.css";
2
+ import "@uppy/dashboard/dist/style.css";
3
+ import "@uppy/drag-drop/dist/style.css";
4
+ import "@uppy/file-input/dist/style.css";
5
+ import "@uppy/progress-bar/dist/style.css";
6
+ export declare const Image: BlockSpec<BType, PSchema, ContainsInlineContent>;
@@ -0,0 +1,37 @@
1
+ import { BlockSpec } from "../../../api/blockTypes";
2
+ export declare const imagePropSchema: {
3
+ textAlignment: {
4
+ default: "left";
5
+ values: readonly ["left", "center", "right", "justify"];
6
+ };
7
+ backgroundColor: {
8
+ default: "transparent";
9
+ };
10
+ url: {
11
+ default: "";
12
+ };
13
+ caption: {
14
+ default: "";
15
+ };
16
+ width: {
17
+ default: 512;
18
+ };
19
+ };
20
+ export declare const Image: BlockSpec<"image", {
21
+ textAlignment: {
22
+ default: "left";
23
+ values: readonly ["left", "center", "right", "justify"];
24
+ };
25
+ backgroundColor: {
26
+ default: "transparent";
27
+ };
28
+ url: {
29
+ default: "";
30
+ };
31
+ caption: {
32
+ default: "";
33
+ };
34
+ width: {
35
+ default: 512;
36
+ };
37
+ }, false>;
@@ -0,0 +1,36 @@
1
+ import { EditorState, Plugin, PluginKey } from "prosemirror-state";
2
+ import { EditorView } from "prosemirror-view";
3
+ import { BaseUiElementCallbacks, BaseUiElementState, BlockNoteEditor, BlockSchema, BlockSpec, SpecificBlock } from "../..";
4
+ import { EventEmitter } from "../../shared/EventEmitter";
5
+ export type ImageToolbarCallbacks = BaseUiElementCallbacks;
6
+ export type ImageToolbarState = BaseUiElementState & {
7
+ block: SpecificBlock<BlockSchema & {
8
+ image: BlockSpec<"image", {
9
+ src: {
10
+ default: string;
11
+ };
12
+ }, false>;
13
+ }, "image">;
14
+ };
15
+ export declare class ImageToolbarView {
16
+ private readonly pluginKey;
17
+ private readonly pmView;
18
+ private imageToolbarState?;
19
+ updateImageToolbar: () => void;
20
+ prevWasEditable: boolean | null;
21
+ shouldShow: (state: EditorState) => boolean;
22
+ constructor(pluginKey: PluginKey, pmView: EditorView, updateImageToolbar: (imageToolbarState: ImageToolbarState) => void);
23
+ mouseDownHandler: () => void;
24
+ dragstartHandler: () => void;
25
+ blurHandler: (event: FocusEvent) => void;
26
+ scrollHandler: () => void;
27
+ update(view: EditorView, prevState: EditorState): void;
28
+ destroy(): void;
29
+ }
30
+ export declare const imageToolbarPluginKey: PluginKey<any>;
31
+ export declare class ImageToolbarProsemirrorPlugin<BSchema extends BlockSchema> extends EventEmitter<any> {
32
+ private view;
33
+ readonly plugin: Plugin;
34
+ constructor(_editor: BlockNoteEditor<BSchema>);
35
+ onUpdate(callback: (state: ImageToolbarState) => void): () => void;
36
+ }
@@ -1,5 +1,5 @@
1
- import { Selection } from "prosemirror-state";
2
1
  import { Node, ResolvedPos, Slice } from "prosemirror-model";
2
+ import { Selection } from "prosemirror-state";
3
3
  import { Mappable } from "prosemirror-transform";
4
4
  /**
5
5
  * This class represents an editor selection which spans multiple nodes/blocks. It's currently only used to allow users
@@ -1,11 +0,0 @@
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 {};
@@ -1,16 +0,0 @@
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>;
@@ -1,55 +0,0 @@
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
- };
@@ -1,24 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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>;
@@ -1,10 +0,0 @@
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>;
@@ -1,8 +0,0 @@
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;
@@ -1,13 +0,0 @@
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>;
@@ -1,3 +0,0 @@
1
- import { getDefaultSlashMenuItems } from "./defaultSlashMenuItems";
2
- import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
3
- export { getDefaultSlashMenuItems, BaseSlashMenuItem };
@@ -1,12 +0,0 @@
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>>;