Skip to content

Playground vanilla styles & non-editable custom blocks #438

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
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
3 changes: 1 addition & 2 deletions examples/editor/examples/basic/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
import "@blocknote/core/style.css";
import { BlockNoteView, useBlockNote } from "@blocknote/react";

import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";

type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };

export function App() {
Expand Down
3 changes: 1 addition & 2 deletions examples/editor/examples/collaboration/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
import "@blocknote/core/style.css";
import { BlockNoteView, useBlockNote } from "@blocknote/react";

import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";

import YPartyKitProvider from "y-partykit/provider";
import * as Y from "yjs";

Expand Down
53 changes: 34 additions & 19 deletions examples/editor/examples/react-custom-blocks/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createReactBlockSpec,
useBlockNote,
} from "@blocknote/react";
import "../vanilla-custom-blocks/style.css";

type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };

Expand Down Expand Up @@ -48,14 +49,8 @@ export const alertBlock = createReactBlockSpec(
{
render: (props) => (
<div
className={"alert"}
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
flexGrow: "1",
height: "48px",
padding: "4px",
maxWidth: "100%",
backgroundColor: alertTypes[props.block.props.type].backgroundColor,
}}>
<select
Expand All @@ -72,12 +67,34 @@ export const alertBlock = createReactBlockSpec(
<option value="info">{alertTypes["info"].icon}</option>
<option value="success">{alertTypes["success"].icon}</option>
</select>
<div style={{ flexGrow: 1 }} ref={props.contentRef} />
<div className={"inline-content"} ref={props.contentRef} />
</div>
),
}
);

const simpleImageBlock = createReactBlockSpec(
{
type: "simpleImage",
propSchema: {
src: {
default:
"https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
},
},
content: "none",
},
{
render: (props) => (
<img
className={"simple-image"}
src={props.block.props.src}
alt="placeholder"
/>
),
}
);

export const bracketsParagraphBlock = createReactBlockSpec(
{
type: "bracketsParagraph",
Expand All @@ -88,19 +105,10 @@ export const bracketsParagraphBlock = createReactBlockSpec(
},
{
render: (props) => (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
flexGrow: "1",
height: "48px",
padding: "4px",
maxWidth: "100%",
}}>
<div className={"brackets-paragraph"}>
<div contentEditable={"false"}>{"["}</div>
<span contentEditable={"false"}>{"{"}</span>
<div style={{ flexGrow: 1 }} ref={props.contentRef} />
<div className={"inline-content"} ref={props.contentRef} />
<span contentEditable={"false"}>{"}"}</span>
<div contentEditable={"false"}>{"]"}</div>
</div>
Expand All @@ -119,6 +127,7 @@ export function ReactCustomBlocks() {
blockSpecs: {
...defaultBlockSpecs,
alert: alertBlock,
simpleImage: simpleImageBlock,
bracketsParagraph: bracketsParagraphBlock,
},
initialContent: [
Expand All @@ -129,6 +138,12 @@ export function ReactCustomBlocks() {
},
content: "Alert",
},
{
type: "simpleImage",
props: {
src: "https://t3.ftcdn.net/jpg/02/48/42/64/360_F_248426448_NVKLywWqArG2ADUxDq6QprtIzsF82dMF.jpg",
},
},
{
type: "bracketsParagraph",
content: "Brackets Paragraph",
Expand Down
33 changes: 17 additions & 16 deletions examples/editor/examples/react-custom-styles/App.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import {
BlockNoteEditor,
DefaultBlockSchema,
DefaultInlineContentSchema,
defaultStyleSpecs,
} from "@blocknote/core";
import "@blocknote/core/style.css";
import {
BlockNoteView,
createReactStyleSpec,
FormattingToolbarPositioner,
Toolbar,
ToolbarButton,
createReactStyleSpec,
useActiveStyles,
useBlockNote,
} from "@blocknote/react";

import {
BlockNoteEditor,
DefaultBlockSchema,
DefaultInlineContentSchema,
StyleSchemaFromSpecs,
defaultStyleSpecs,
} from "@blocknote/core";

type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };

const small = createReactStyleSpec(
Expand Down Expand Up @@ -45,16 +43,13 @@ const fontSize = createReactStyleSpec(
}
);

const customReactStyles = {
...defaultStyleSpecs,
small,
fontSize,
};

type MyEditorType = BlockNoteEditor<
DefaultBlockSchema,
DefaultInlineContentSchema,
StyleSchemaFromSpecs<typeof customReactStyles>
{
small: (typeof small)["config"];
fontSize: (typeof fontSize)["config"];
}
>;

const CustomFormattingToolbar = (props: { editor: MyEditorType }) => {
Expand Down Expand Up @@ -86,6 +81,12 @@ const CustomFormattingToolbar = (props: { editor: MyEditorType }) => {
);
};

const customReactStyles = {
...defaultStyleSpecs,
small,
fontSize,
};

export function ReactStyles() {
const editor = useBlockNote(
{
Expand Down
63 changes: 35 additions & 28 deletions examples/editor/examples/vanilla-custom-blocks/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@blocknote/core";
import "@blocknote/core/style.css";
import { BlockNoteView, useBlockNote } from "@blocknote/react";
import "./style.css";

type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };

Expand Down Expand Up @@ -48,9 +49,7 @@ const alertBlock = createBlockSpec(
{
render: (block, editor) => {
const alert = document.createElement("div");
Object.entries(alertStyles).forEach(([key, value]) => {
alert.style[key as any] = value;
});
alert.className = "alert";
alert.style.backgroundColor =
alertTypes[block.props.type].backgroundColor;

Expand Down Expand Up @@ -110,16 +109,30 @@ const alertBlock = createBlockSpec(
}
);

// TODO: use CSS?
const alertStyles = {
display: "flex",
justifyContent: "center",
alignItems: "center",
flexGrow: "1",
height: "48px",
padding: "4px",
maxWidth: "100%",
};
const simpleImageBlock = createBlockSpec(
{
type: "simpleImage",
propSchema: {
src: {
default:
"https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
},
},
content: "none",
},
{
render: (block) => {
const image = document.createElement("img");
image.className = "simple-image";
image.src = block.props.src;
image.alt = "placeholder";

return {
dom: image,
};
},
}
);

const bracketsParagraphBlock = createBlockSpec(
{
Expand All @@ -132,9 +145,7 @@ const bracketsParagraphBlock = createBlockSpec(
{
render: () => {
const bracketsParagraph = document.createElement("div");
Object.entries(bracketsParagraphStyles).forEach(([key, value]) => {
bracketsParagraph.style[key as any] = value;
});
bracketsParagraph.className = "brackets-paragraph";

const leftBracket = document.createElement("div");
leftBracket.contentEditable = "false";
Expand All @@ -146,7 +157,7 @@ const bracketsParagraphBlock = createBlockSpec(
bracketsParagraph.appendChild(leftCurlyBracket);

const inlineContent = document.createElement("div");
inlineContent.style.flexGrow = "1";
inlineContent.className = "inline-content";

bracketsParagraph.appendChild(inlineContent);

Expand All @@ -167,17 +178,6 @@ const bracketsParagraphBlock = createBlockSpec(
}
);

// TODO: use CSS
const bracketsParagraphStyles = {
display: "flex",
justifyContent: "center",
alignItems: "center",
flexGrow: "1",
height: "48px",
padding: "4px",
maxWidth: "100%",
};

export function CustomBlocks() {
const editor = useBlockNote({
domAttributes: {
Expand All @@ -190,6 +190,7 @@ export function CustomBlocks() {
...defaultBlockSpecs,
alert: alertBlock,
bracketsParagraph: bracketsParagraphBlock,
simpleImage: simpleImageBlock,
},
initialContent: [
{
Expand All @@ -199,6 +200,12 @@ export function CustomBlocks() {
},
content: ["Alert"],
},
{
type: "simpleImage",
props: {
src: "https://t3.ftcdn.net/jpg/02/48/42/64/360_F_248426448_NVKLywWqArG2ADUxDq6QprtIzsF82dMF.jpg",
},
},
{
type: "bracketsParagraph",
content: "Brackets Paragraph",
Expand Down
17 changes: 17 additions & 0 deletions examples/editor/examples/vanilla-custom-blocks/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.alert, .brackets-paragraph {
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
height: 48px;
padding: 4px;
max-width: 100%;
}

.simple-image {
width: 100%;
}

.inline-content {
flex-grow: 1;
}
Loading