Skip to content

Commit c0c609a

Browse files
add defaultStyles option, reorganize styles (#148)
* add defaultStyles option, reorganize styles * Small changes to docs * Made drag preview inherit editor styling --------- Co-authored-by: Matthew Lipski <[email protected]>
1 parent 37bb63a commit c0c609a

File tree

5 files changed

+88
-34
lines changed

5 files changed

+88
-34
lines changed

packages/core/src/BlockNoteEditor.ts

+42
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,50 @@ export type BlockNoteEditorOptions = {
3434
// TODO: Figure out if enableBlockNoteExtensions/disableHistoryExtension are needed and document them.
3535
enableBlockNoteExtensions: boolean;
3636
disableHistoryExtension: boolean;
37+
/**
38+
* Factories used to create a custom UI for BlockNote
39+
*/
3740
uiFactories: UiFactories;
41+
/**
42+
* TODO: why is this called slashCommands and not slashMenuItems?
43+
*
44+
* @default defaultSlashMenuItems from `./extensions/SlashMenu`
45+
*/
3846
slashCommands: BaseSlashMenuItem[];
47+
48+
/**
49+
* The HTML element that should be used as the parent element for the editor.
50+
*
51+
* @default: undefined, the editor is not attached to the DOM
52+
*/
3953
parentElement: HTMLElement;
54+
/**
55+
* An object containing attributes that should be added to the editor's HTML element.
56+
*
57+
* @example { class: "my-editor-class" }
58+
*/
4059
editorDOMAttributes: Record<string, string>;
60+
/**
61+
* A callback function that runs when the editor is ready to be used.
62+
*/
4163
onEditorReady: (editor: BlockNoteEditor) => void;
64+
/**
65+
* A callback function that runs whenever the editor's contents change.
66+
*/
4267
onEditorContentChange: (editor: BlockNoteEditor) => void;
68+
/**
69+
* A callback function that runs whenever the text cursor position changes.
70+
*/
4371
onTextCursorPositionChange: (editor: BlockNoteEditor) => void;
4472
initialContent: PartialBlock[];
4573

74+
/**
75+
* Use default BlockNote font and reset the styles of <p> <li> <h1> elements etc., that are used in BlockNote.
76+
*
77+
* @default true
78+
*/
79+
defaultStyles: boolean;
80+
4681
// tiptap options, undocumented
4782
_tiptapOptions: any;
4883
};
@@ -62,6 +97,12 @@ export class BlockNoteEditor {
6297
}
6398

6499
constructor(options: Partial<BlockNoteEditorOptions> = {}) {
100+
// apply defaults
101+
options = {
102+
defaultStyles: true,
103+
...options,
104+
};
105+
65106
const blockNoteExtensions = getBlockNoteExtensions({
66107
editor: this,
67108
uiFactories: options.uiFactories || {},
@@ -103,6 +144,7 @@ export class BlockNoteEditor {
103144
class: [
104145
styles.bnEditor,
105146
styles.bnRoot,
147+
options.defaultStyles ? styles.defaultStyles : "",
106148
options.editorDOMAttributes?.class || "",
107149
].join(" "),
108150
},

packages/core/src/editor.module.css

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
.bnEditor {
44
outline: none;
55
margin-left: 50px;
6+
7+
/* Define a set of colors to be used throughout the app for consistency
8+
see https://atlassian.design/foundations/color for more info */
9+
--N800: #172b4d; /* Dark neutral used for tooltips and text on light background */
10+
--N40: #dfe1e6; /* Light neutral used for subtle borders and text on dark background */
611
}
712

813
/*
@@ -25,13 +30,21 @@ Tippy popups that are appended to document.body directly
2530
box-sizing: inherit;
2631
}
2732

28-
.bnEditor,
29-
.dragPreview {
30-
/* Define a set of colors to be used throughout the app for consistency
31-
see https://atlassian.design/foundations/color for more info */
32-
--N800: #172b4d; /* Dark neutral used for tooltips and text on light background */
33-
--N40: #dfe1e6; /* Light neutral used for subtle borders and text on dark background */
33+
/* reset styles, they will be set on blockContent */
34+
.defaultStyles p,
35+
.defaultStyles h1,
36+
.defaultStyles h2,
37+
.defaultStyles h3,
38+
.defaultStyles li {
39+
all: unset;
40+
margin: 0;
41+
padding: 0;
42+
font-size: inherit;
43+
}
3444

45+
.defaultStyles {
46+
font-size: 16px;
47+
font-weight: normal;
3548
font-family: "Inter", "SF Pro Display", -apple-system, BlinkMacSystemFont,
3649
"Open Sans", "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell",
3750
"Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;

packages/core/src/extensions/Blocks/nodes/Block.module.css

+1-23
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ BASIC STYLES
33
*/
44

55
.blockOuter {
6-
/* padding-bottom: 15px; */
7-
font-size: 16px;
86
line-height: 1.5;
9-
transition: all 0.2s;
10-
font-weight: normal;
11-
}
12-
13-
.block {
14-
/* content: ""; */
15-
transition: all 0.2s;
16-
margin: 0px;
7+
transition: margin 0.2s;
178
}
189

1910
.blockContent {
20-
font-size: 1em;
2111
padding: 3px 0;
2212
transition: font-size 0.2s;
2313
/* display: inline-block; */
@@ -29,18 +19,6 @@ BASIC STYLES
2919
/*margin: 0px;*/
3020
}
3121

32-
/* reset styles, they will be set on blockContent */
33-
.blockContent p,
34-
.blockContent h1,
35-
.blockContent h2,
36-
.blockContent h3,
37-
.blockContent li {
38-
all: unset;
39-
margin: 0;
40-
padding: 0;
41-
font-size: inherit;
42-
}
43-
4422
/*
4523
NESTED BLOCKS
4624
*/

packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,26 @@ function setDragImage(view: EditorView, from: number, to = from) {
158158
// dataTransfer.setDragImage(element) only works if element is attached to the DOM.
159159
unsetDragImage();
160160
dragImageElement = parentClone;
161+
162+
// TODO: This is hacky, need a better way of assigning classes to the editor so that they can also be applied to the
163+
// drag preview.
164+
const classes = view.dom.className.split(" ");
165+
const inheritedClasses = classes
166+
.filter(
167+
(className) =>
168+
!className.includes("bn") &&
169+
!className.includes("ProseMirror") &&
170+
!className.includes("editor")
171+
)
172+
.join(" ");
173+
161174
dragImageElement.className =
162-
dragImageElement.className + " " + styles.dragPreview;
175+
dragImageElement.className +
176+
" " +
177+
styles.dragPreview +
178+
" " +
179+
inheritedClasses;
180+
163181
document.body.appendChild(dragImageElement);
164182
}
165183

packages/website/docs/docs/editor.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ While you can get started with BlockNote in minutes, it's likely that you'll wan
77
There are a number of options that you can pass to `useBlockNote()`, which you can use to customize the editor. You can find the full list of these below:
88

99
```typescript
10-
export type BlockNoteEditorOptions = {
10+
export type BlockNoteEditorOptions = Partial<{
1111
initialContent: PartialBlock[];
1212
editorDOMAttributes: Record<string, string>;
1313
onEditorReady: (editor: BlockNoteEditor) => void;
1414
onEditorContentChange: (editor: BlockNoteEditor) => void;
1515
onTextCursorPositionChange: (editor: BlockNoteEditor) => void;
1616
slashMenuItems: ReactSlashMenuItem[];
1717
uiFactories: UiFactories;
18-
};
18+
defaultStyles: boolean;
19+
}>;
1920
```
2021

2122
`initialContent:` The content that should be in the editor when it's created, represented as an array of [partial block objects](/docs/manipulating-blocks#partial-blocks).
2223

23-
`editorDOMAttributes:` An object containing attributes that should be added to the editor's HTML element.
24+
`editorDOMAttributes:` An object containing attributes that should be added to the editor's HTML element. For example, you can pass `{ class: "my-editor-class" }` to set a custom class name.
2425

2526
`onEditorReady:` A callback function that runs when the editor is ready to be used.
2627

@@ -32,6 +33,8 @@ export type BlockNoteEditorOptions = {
3233

3334
`uiFactories:` Factories used to create a custom UI for BlockNote, which you can find out more about in [Creating Your Own UI Elements](/docs/vanilla-js#creating-your-own-ui-elements).
3435

36+
`defaultStyles`: Whether to use the default font and reset the styles of `<p>`, `<li>`, `<h1>`, etc. elements that are used in BlockNote. Defaults to true if undefined.
37+
3538
## Demo: Saving & Restoring Editor Contents
3639

3740
By default, BlockNote doesn't preserve the editor contents when your app is reopened or refreshed. However, using the editor options, you can change this by using the editor options.
@@ -67,4 +70,4 @@ export default function App() {
6770
}
6871
```
6972

70-
:::
73+
:::

0 commit comments

Comments
 (0)