Skip to content

Custom blocks docs #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/core/src/editor.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Tippy popups that are appended to document.body directly
.defaultStyles h3,
.defaultStyles li {
all: unset !important;
flex-grow: 1 !important;
margin: 0;
padding: 0;
font-size: inherit;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/extensions/Blocks/api/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ export function createBlockSpec<

// Render elements
const rendered = blockConfig.render(block as any, editor);
// Add inlineContent class to inline content
if ("contentDOM" in rendered) {
rendered.contentDOM.className = `${
rendered.contentDOM.className
? rendered.contentDOM.className + " "
: ""
}${styles.inlineContent}`;
}
// Add elements to blockContent
blockContent.appendChild(rendered.dom);

Expand Down
36 changes: 24 additions & 12 deletions packages/core/src/extensions/Blocks/nodes/Block.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ BASIC STYLES
transition: margin 0.2s;
}

/*Ensures blocks & block content spans editor width*/
.block {
display: flex;
flex-direction: column;
}

/*Ensures block content inside React node views spans editor width*/
.reactNodeViewRenderer {
display: flex;
flex-grow: 1;
}

.blockContent {
padding: 3px 0;
flex-grow: 1;
transition: font-size 0.2s;
/*
because the content elements are display: block
we use flex to position them next to list markers
*/
display: flex;
}

.blockContent::before {
Expand Down Expand Up @@ -225,8 +237,8 @@ NESTED BLOCKS

/* PLACEHOLDERS*/

.blockContent.isEmpty > :first-child:before,
.blockContent.isFilter > :first-child:before {
.isEmpty .inlineContent:before,
.isFilter .inlineContent:before {
/*float: left; */
content: "";
pointer-events: none;
Expand All @@ -236,33 +248,33 @@ NESTED BLOCKS
font-style: italic;
}

[data-theme="light"] .blockContent.isEmpty > :first-child:before,
.blockContent.isFilter > :first-child:before {
[data-theme="light"] .isEmpty .inlineContent:before,
.isFilter .inlineContent:before {
color: #cccccc;
}

[data-theme="dark"] .blockContent.isEmpty > :first-child:before,
.blockContent.isFilter > :first-child:before {
[data-theme="dark"] .isEmpty .inlineContent:before,
.isFilter .inlineContent:before {
color: #999999;
}

/* TODO: would be nicer if defined from code */

.blockContent.isEmpty.hasAnchor > :first-child:before {
.isEmpty.hasAnchor .inlineContent:before {
content: "Enter text or type '/' for commands";
}

.blockContent.isFilter.hasAnchor > :first-child:before {
.blockContent.isFilter.hasAnchor .inlineContent:before {
content: "Type to filter";
}

.blockContent[data-content-type="heading"].isEmpty > :first-child::before {
.blockContent[data-content-type="heading"].isEmpty .inlineContent:before {
content: "Heading";
}

.blockContent[data-content-type="bulletListItem"].isEmpty > :first-child:before,
.blockContent[data-content-type="bulletListItem"].isEmpty .inlineContent:before,
.blockContent[data-content-type="numberedListItem"].isEmpty
> :first-child:before {
.inlineContent:before {
content: "List";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const HeadingBlockContent = createTipTapBlock<"heading">({
class: styles.blockContent,
"data-content-type": this.name,
}),
["h" + node.attrs.level, 0],
["h" + node.attrs.level, { class: styles.inlineContent }, 0],
];
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const BulletListItemBlockContent = createTipTapBlock<"bulletListItem">({
class: styles.blockContent,
"data-content-type": this.name,
}),
["p", 0],
["p", { class: styles.inlineContent }, 0],
];
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const NumberedListItemBlockContent =
}),
// we use a <p> tag, because for <li> tags we'd need to add a <ul> parent for around siblings to be semantically correct,
// which would be quite cumbersome
["p", 0],
["p", { class: styles.inlineContent }, 0],
];
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ParagraphBlockContent = createTipTapBlock<"paragraph">({
class: styles.blockContent,
"data-content-type": this.name,
}),
["p", 0],
["p", { class: styles.inlineContent }, 0],
];
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
const classes = [this.options.emptyNodeClass];

// TODO: Doesn't work?
if (this.editor.isEmpty) {
classes.push(this.options.emptyEditorClass);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
BlockNoteEditor,
DefaultBlockSchema,
BlockSchema,
DefaultProps,
PartialBlock,
} from "@blocknote/core";
import { useCallback } from "react";
import { useCallback, useMemo } from "react";
import { IconType } from "react-icons";
import {
RiAlignCenter,
Expand All @@ -22,10 +23,30 @@ const icons: Record<TextAlignment, IconType> = {
justify: RiAlignJustify,
};

export const TextAlignButton = (props: {
editor: BlockNoteEditor<DefaultBlockSchema>;
export const TextAlignButton = <BSchema extends BlockSchema>(props: {
editor: BlockNoteEditor<BSchema>;
textAlignment: TextAlignment;
}) => {
const show = useMemo(() => {
const selection = props.editor.getSelection();

if (selection) {
for (const block of selection.blocks) {
if (!("textAlignment" in block.props)) {
return false;
}
}
} else {
const block = props.editor.getTextCursorPosition().block;

if (!("textAlignment" in block.props)) {
return false;
}
}

return true;
}, [props.editor]);

const setTextAlignment = useCallback(
(textAlignment: TextAlignment) => {
props.editor.focus();
Expand All @@ -36,17 +57,23 @@ export const TextAlignButton = (props: {
for (const block of selection.blocks) {
props.editor.updateBlock(block, {
props: { textAlignment: textAlignment },
});
} as PartialBlock<BSchema>);
}
} else {
props.editor.updateBlock(props.editor.getTextCursorPosition().block, {
const block = props.editor.getTextCursorPosition().block;

props.editor.updateBlock(block, {
props: { textAlignment: textAlignment },
});
} as PartialBlock<BSchema>);
}
},
[props.editor]
);

if (!show) {
return null;
}

return (
<ToolbarButton
onClick={() => setTextAlignment(props.textAlignment)}
Expand Down
16 changes: 11 additions & 5 deletions packages/react/src/ReactBlockSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
} from "@blocknote/core";
import {
NodeViewContent,
NodeViewContentProps,
NodeViewProps,
NodeViewWrapper,
ReactNodeViewRenderer,
} from "@tiptap/react";
import { FC } from "react";
import { FC, HTMLAttributes } from "react";
import { blockStyles } from "@blocknote/core";

// extend BlockConfig but use a react render function
Expand All @@ -39,8 +38,13 @@ export type ReactBlockConfig<
}>;
};

export const ContentDOM = (props: NodeViewContentProps) => (
<NodeViewContent {...props} />
export const InlineContent = (props: HTMLAttributes<HTMLDivElement>) => (
<NodeViewContent
{...props}
className={`${props.className ? props.className + " " : ""}${
blockStyles.inlineContent
}`}
/>
);

// A function to create custom block for API consumers
Expand Down Expand Up @@ -115,7 +119,9 @@ export function createReactBlockSpec<
);
};

return ReactNodeViewRenderer(BlockContent);
return ReactNodeViewRenderer(BlockContent, {
className: blockStyles.reactNodeViewRenderer,
});
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type ToolbarDropdownProps = {
export function ToolbarDropdown(props: ToolbarDropdownProps) {
const activeItem = props.items.filter((p) => p.isSelected)[0];

if (!activeItem) {
return null;
}

return (
<Menu exitTransitionDuration={0} disabled={props.isDisabled}>
<Menu.Target>
Expand Down
Loading