Skip to content

Commit 0ef29d2

Browse files
feat(divider): add a divider block (#2014)
* fix(core): input rules can handle when a new block is empty now * feat(divider): add a divider block * feat: add conversion for divider block * Added tests * Updated e2e snaps --------- Co-authored-by: Matthew Lipski <[email protected]>
1 parent 4837087 commit 0ef29d2

File tree

62 files changed

+446
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+446
-31
lines changed

examples/05-interoperability/05-converting-blocks-to-pdf/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export default function App() {
9898
backgroundColor: "red",
9999
},
100100
},
101+
{ type: "divider" },
101102
{
102103
type: "paragraph",
103104
content: [

examples/05-interoperability/06-converting-blocks-to-docx/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default function App() {
9292
backgroundColor: "red",
9393
},
9494
},
95+
{ type: "divider" },
9596
{
9697
type: "paragraph",
9798
content: [

examples/05-interoperability/07-converting-blocks-to-odt/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default function App() {
9292
backgroundColor: "red",
9393
},
9494
},
95+
{ type: "divider" },
9596
{
9697
type: "paragraph",
9798
content: [

examples/05-interoperability/08-converting-blocks-to-react-email/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export default function App() {
8888
backgroundColor: "red",
8989
},
9090
},
91+
{ type: "divider" },
9192
{
9293
type: "paragraph",
9394
content: [
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createBlockNoteExtension } from "../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
3+
4+
export type DividerBlockConfig = ReturnType<typeof createDividerBlockConfig>;
5+
6+
export const createDividerBlockConfig = createBlockConfig(
7+
() =>
8+
({
9+
type: "divider" as const,
10+
propSchema: {},
11+
content: "none",
12+
}) as const,
13+
);
14+
15+
export const createDividerBlockSpec = createBlockSpec(
16+
createDividerBlockConfig,
17+
{
18+
meta: {
19+
isolating: false,
20+
},
21+
parse(element) {
22+
if (element.tagName === "HR") {
23+
return {};
24+
}
25+
26+
return undefined;
27+
},
28+
render() {
29+
const dom = document.createElement("hr");
30+
31+
return {
32+
dom,
33+
};
34+
},
35+
},
36+
[
37+
createBlockNoteExtension({
38+
key: "divider-block-shortcuts",
39+
inputRules: [
40+
{
41+
find: new RegExp(`^---$`),
42+
replace() {
43+
return { type: "divider", props: {}, content: [] };
44+
},
45+
},
46+
],
47+
}),
48+
],
49+
);

packages/core/src/blocks/defaultBlocks.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ import Code from "@tiptap/extension-code";
33
import Italic from "@tiptap/extension-italic";
44
import Strike from "@tiptap/extension-strike";
55
import Underline from "@tiptap/extension-underline";
6+
import { COLORS_DEFAULT } from "../editor/defaultColors.js";
7+
import {
8+
BlockNoDefaults,
9+
BlockSchema,
10+
InlineContentSchema,
11+
InlineContentSpecs,
12+
PartialBlockNoDefaults,
13+
StyleSchema,
14+
StyleSpecs,
15+
createStyleSpec,
16+
createStyleSpecFromTipTapMark,
17+
getInlineContentSchemaFromSpecs,
18+
getStyleSchemaFromSpecs,
19+
} from "../schema/index.js";
620
import {
721
createAudioBlockSpec,
822
createBulletListItemBlockSpec,
923
createCheckListItemBlockSpec,
1024
createCodeBlockSpec,
25+
createDividerBlockSpec,
1126
createFileBlockSpec,
1227
createHeadingBlockSpec,
1328
createImageBlockSpec,
@@ -18,27 +33,14 @@ import {
1833
createVideoBlockSpec,
1934
defaultProps,
2035
} from "./index.js";
21-
import {
22-
BlockNoDefaults,
23-
BlockSchema,
24-
InlineContentSchema,
25-
InlineContentSpecs,
26-
PartialBlockNoDefaults,
27-
StyleSchema,
28-
StyleSpecs,
29-
createStyleSpec,
30-
createStyleSpecFromTipTapMark,
31-
getInlineContentSchemaFromSpecs,
32-
getStyleSchemaFromSpecs,
33-
} from "../schema/index.js";
3436
import { createTableBlockSpec } from "./Table/block.js";
35-
import { COLORS_DEFAULT } from "../editor/defaultColors.js";
3637

3738
export const defaultBlockSpecs = {
3839
audio: createAudioBlockSpec(),
3940
bulletListItem: createBulletListItemBlockSpec(),
4041
checkListItem: createCheckListItemBlockSpec(),
4142
codeBlock: createCodeBlockSpec(),
43+
divider: createDividerBlockSpec(),
4244
file: createFileBlockSpec(),
4345
heading: createHeadingBlockSpec(),
4446
image: createImageBlockSpec(),

packages/core/src/blocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./Audio/block.js";
22
export * from "./Audio/parseAudioElement.js";
33
export * from "./Code/block.js";
4+
export * from "./Divider/block.js";
45
export * from "./File/block.js";
56
export * from "./Heading/block.js";
67
export * from "./Image/block.js";

packages/core/src/editor/Block.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ NESTED BLOCKS
184184
padding-left: 1em;
185185
}
186186

187+
/* DIVIDERS */
188+
[data-content-type="divider"] hr {
189+
border: none;
190+
border-top: 1px solid rgb(125, 121, 122);
191+
margin: 0.5em 0;
192+
flex: 1;
193+
}
194+
187195
/* LISTS */
188196

189197
.bn-block-content::before {

packages/core/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ export function getDefaultSlashMenuItems<
215215
});
216216
}
217217

218+
if (editorHasBlockWithType(editor, "divider")) {
219+
items.push({
220+
onItemClick: () => {
221+
insertOrUpdateBlock(editor, { type: "divider" });
222+
},
223+
key: "divider",
224+
...editor.dictionary.slash_menu.divider,
225+
});
226+
}
227+
218228
if (editorHasBlockWithType(editor, "table")) {
219229
items.push({
220230
onItemClick: () => {

packages/core/src/i18n/locales/ar.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ export const ar: Dictionary = {
156156
aliases: ["رمز تعبيري", "إيموجي", "إيموت", "عاطفة", "وجه"],
157157
group: "آخرون",
158158
},
159+
divider: {
160+
title: "فاصل",
161+
subtext: "يستخدم لفصل الكتل",
162+
aliases: ["فاصل", "فاصل", "فاصل", "فاصل"],
163+
group: "الكتل الأساسية",
164+
},
159165
},
160166
placeholders: {
161167
default: "أدخل نصًا أو اكتب '/' للأوامر",

0 commit comments

Comments
 (0)