Skip to content

Commit fc6ac01

Browse files
committed
docs: block note editor class
1 parent 9684a7d commit fc6ac01

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

adr/2025_06_13-packages.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ It contains sub-packages for:
2828
- `@blocknote/react/comments` - Contains the comments package.
2929
- `@blocknote/react/table-handles` - Contains the table handles package.
3030
- etc... (need to figure out what needs to be exported vs. available and how coupled this all is)
31+
32+
## Editor instance
33+
34+
The editor instance has been growing in complexity, and handles too many different concerns. To focus it, we should split it up into something like: the `BlockNoteEditor.ts` file.
35+
36+
The goal would be to group the functionality of the editor instance in such a way that it is easier to navigate and understand.

adr/BlockNoteEditor.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { BlockNoteExtension } from "./BlockNoteExtension";
2+
3+
/**
4+
* Transform is a class which represents a transformation to a block note document (independent of the editor state)
5+
*
6+
* These are higher-level APIs than editor commands, and operate at the block level.
7+
*/
8+
export class Transform {
9+
// low-level operations
10+
public exec: (command: Command) => void;
11+
public canExec: (command: Command) => boolean;
12+
public transact: <T>(callback: (transform: Transform) => T) => T;
13+
14+
// current state
15+
public get document(): Block[];
16+
public set document(document: Block[]);
17+
public get selection(): Location; // likely more complex than this
18+
public set selection(selection: Location);
19+
20+
// Block-level operations
21+
public forEachBlock: (callback: (block: Block) => void) => void;
22+
public getBlock(at: Location): Block | undefined;
23+
public getPrevBlock(at: Location): Block | undefined;
24+
public getNextBlock(at: Location): Block | undefined;
25+
public getParentBlock(at: Location): Block | undefined;
26+
public insertBlocks(ctx: { at: Location; blocks: Block | Block[] });
27+
public updateBlock(ctx: { at: Location; block: PartialBlock });
28+
public replaceBlocks(ctx: { at: Location; with: Block | Block[] });
29+
public nestBlock(ctx: { at: Location }): boolean;
30+
public unnestBlock(ctx: { at: Location }): boolean;
31+
public moveBlocksUp(ctx: { at: Location }): boolean;
32+
public moveBlocksDown(ctx: { at: Location }): boolean;
33+
34+
// Things which operate on the editor state, not just the document
35+
public undo(): boolean;
36+
public redo(): boolean;
37+
public createLink(ctx: { at: Location; url: string; text?: string }): boolean;
38+
public deleteContent(ctx: { at: Location }): boolean;
39+
public replaceContent(ctx: {
40+
at: Location;
41+
with: InlineContent | InlineContent[];
42+
}): boolean;
43+
public getStyles(at?: Location): Styles;
44+
public addStyles(styles: Styles): void;
45+
public removeStyles(styles: Styles): void;
46+
public toggleStyles(styles: Styles): void;
47+
public getText(at?: Location): string;
48+
public pasteHTML(html: string, raw?: boolean): void;
49+
public pasteText(text: string): void;
50+
public pasteMarkdown(markdown: string): void;
51+
}
52+
53+
export type Unsubscribe = () => void;
54+
55+
/**
56+
* EventManager is a class which manages the events of the editor
57+
*/
58+
export class EventManager {
59+
public onChange: (
60+
callback: (ctx: {
61+
editor: BlockNoteEditor;
62+
get changes(): BlocksChanged;
63+
}) => void,
64+
) => Unsubscribe;
65+
public onSelectionChange: (
66+
callback: (ctx: {
67+
editor: BlockNoteEditor;
68+
get selection(): Location;
69+
}) => void,
70+
) => Unsubscribe;
71+
public onMount: (
72+
callback: (ctx: { editor: BlockNoteEditor }) => void,
73+
) => Unsubscribe;
74+
public onUnmount: (
75+
callback: (ctx: { editor: BlockNoteEditor }) => void,
76+
) => Unsubscribe;
77+
}
78+
79+
export class BlockNoteEditor {
80+
public events: EventManager;
81+
public transform: Transform;
82+
public extensions: Record<string, BlockNoteExtension>;
83+
/**
84+
* If {@link BlockNoteEditor.extensions} is untyped, this is a way to get a typed extension
85+
*/
86+
public getExtension: (ext: BlockNoteExtension) => BlockNoteExtension;
87+
public mount: (parentElement: HTMLElement) => void;
88+
public unmount: () => void;
89+
public pm: {
90+
get schema(): Schema;
91+
get state(): EditorState;
92+
get view(): EditorView;
93+
};
94+
public get editable(): boolean;
95+
public set editable(editable: boolean);
96+
public get focused(): boolean;
97+
public set focused(focused: boolean);
98+
public get readOnly(): boolean;
99+
public set readOnly(readOnly: boolean);
100+
101+
public readonly dictionary: Dictionary;
102+
public readonly schema: BlockNoteSchema;
103+
}
104+
105+
// A number of utility functions can be exported from `@blocknote/core/utils`
106+
107+
export function getSelectionBoundingBox(editor: BlockNoteEditor) {
108+
// implementation
109+
}
110+
111+
export function isEmpty(editor: BlockNoteEditor) {
112+
// implementation
113+
}
114+
115+
// Formats can be exported from `@blocknote/core/formats`
116+
117+
export function toHTML(editor: BlockNoteEditor): string {
118+
// implementation
119+
}
120+
121+
export function toMarkdown(editor: BlockNoteEditor): string {
122+
// implementation
123+
}
124+
125+
export function tryParseHTMLToBlocks(html: string): Block[] {
126+
// implementation
127+
}
128+
129+
export function tryParseMarkdownToBlocks(markdown: string): Block[] {
130+
// implementation
131+
}

0 commit comments

Comments
 (0)