Skip to content

Commit d67bce5

Browse files
Playground vanilla styles & non-editable custom blocks (#438)
* Added playground for vanilla styles and added simple image block * Fixed imports to make examples standalone
1 parent 40eba12 commit d67bce5

File tree

7 files changed

+251
-67
lines changed

7 files changed

+251
-67
lines changed

examples/editor/examples/basic/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
12
import "@blocknote/core/style.css";
23
import { BlockNoteView, useBlockNote } from "@blocknote/react";
34

4-
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
5-
65
type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };
76

87
export function App() {

examples/editor/examples/collaboration/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
12
import "@blocknote/core/style.css";
23
import { BlockNoteView, useBlockNote } from "@blocknote/react";
34

4-
import { uploadToTmpFilesDotOrg_DEV_ONLY } from "@blocknote/core";
5-
65
import YPartyKitProvider from "y-partykit/provider";
76
import * as Y from "yjs";
87

examples/editor/examples/react-custom-blocks/App.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createReactBlockSpec,
66
useBlockNote,
77
} from "@blocknote/react";
8+
import "../vanilla-custom-blocks/style.css";
89

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

@@ -48,14 +49,8 @@ export const alertBlock = createReactBlockSpec(
4849
{
4950
render: (props) => (
5051
<div
52+
className={"alert"}
5153
style={{
52-
display: "flex",
53-
justifyContent: "center",
54-
alignItems: "center",
55-
flexGrow: "1",
56-
height: "48px",
57-
padding: "4px",
58-
maxWidth: "100%",
5954
backgroundColor: alertTypes[props.block.props.type].backgroundColor,
6055
}}>
6156
<select
@@ -72,12 +67,34 @@ export const alertBlock = createReactBlockSpec(
7267
<option value="info">{alertTypes["info"].icon}</option>
7368
<option value="success">{alertTypes["success"].icon}</option>
7469
</select>
75-
<div style={{ flexGrow: 1 }} ref={props.contentRef} />
70+
<div className={"inline-content"} ref={props.contentRef} />
7671
</div>
7772
),
7873
}
7974
);
8075

76+
const simpleImageBlock = createReactBlockSpec(
77+
{
78+
type: "simpleImage",
79+
propSchema: {
80+
src: {
81+
default:
82+
"https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
83+
},
84+
},
85+
content: "none",
86+
},
87+
{
88+
render: (props) => (
89+
<img
90+
className={"simple-image"}
91+
src={props.block.props.src}
92+
alt="placeholder"
93+
/>
94+
),
95+
}
96+
);
97+
8198
export const bracketsParagraphBlock = createReactBlockSpec(
8299
{
83100
type: "bracketsParagraph",
@@ -88,19 +105,10 @@ export const bracketsParagraphBlock = createReactBlockSpec(
88105
},
89106
{
90107
render: (props) => (
91-
<div
92-
style={{
93-
display: "flex",
94-
justifyContent: "center",
95-
alignItems: "center",
96-
flexGrow: "1",
97-
height: "48px",
98-
padding: "4px",
99-
maxWidth: "100%",
100-
}}>
108+
<div className={"brackets-paragraph"}>
101109
<div contentEditable={"false"}>{"["}</div>
102110
<span contentEditable={"false"}>{"{"}</span>
103-
<div style={{ flexGrow: 1 }} ref={props.contentRef} />
111+
<div className={"inline-content"} ref={props.contentRef} />
104112
<span contentEditable={"false"}>{"}"}</span>
105113
<div contentEditable={"false"}>{"]"}</div>
106114
</div>
@@ -119,6 +127,7 @@ export function ReactCustomBlocks() {
119127
blockSpecs: {
120128
...defaultBlockSpecs,
121129
alert: alertBlock,
130+
simpleImage: simpleImageBlock,
122131
bracketsParagraph: bracketsParagraphBlock,
123132
},
124133
initialContent: [
@@ -129,6 +138,12 @@ export function ReactCustomBlocks() {
129138
},
130139
content: "Alert",
131140
},
141+
{
142+
type: "simpleImage",
143+
props: {
144+
src: "https://t3.ftcdn.net/jpg/02/48/42/64/360_F_248426448_NVKLywWqArG2ADUxDq6QprtIzsF82dMF.jpg",
145+
},
146+
},
132147
{
133148
type: "bracketsParagraph",
134149
content: "Brackets Paragraph",

examples/editor/examples/react-custom-styles/App.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1+
import {
2+
BlockNoteEditor,
3+
DefaultBlockSchema,
4+
DefaultInlineContentSchema,
5+
defaultStyleSpecs,
6+
} from "@blocknote/core";
17
import "@blocknote/core/style.css";
28
import {
39
BlockNoteView,
10+
createReactStyleSpec,
411
FormattingToolbarPositioner,
512
Toolbar,
613
ToolbarButton,
7-
createReactStyleSpec,
814
useActiveStyles,
915
useBlockNote,
1016
} from "@blocknote/react";
1117

12-
import {
13-
BlockNoteEditor,
14-
DefaultBlockSchema,
15-
DefaultInlineContentSchema,
16-
StyleSchemaFromSpecs,
17-
defaultStyleSpecs,
18-
} from "@blocknote/core";
19-
2018
type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any };
2119

2220
const small = createReactStyleSpec(
@@ -45,16 +43,13 @@ const fontSize = createReactStyleSpec(
4543
}
4644
);
4745

48-
const customReactStyles = {
49-
...defaultStyleSpecs,
50-
small,
51-
fontSize,
52-
};
53-
5446
type MyEditorType = BlockNoteEditor<
5547
DefaultBlockSchema,
5648
DefaultInlineContentSchema,
57-
StyleSchemaFromSpecs<typeof customReactStyles>
49+
{
50+
small: (typeof small)["config"];
51+
fontSize: (typeof fontSize)["config"];
52+
}
5853
>;
5954

6055
const CustomFormattingToolbar = (props: { editor: MyEditorType }) => {
@@ -86,6 +81,12 @@ const CustomFormattingToolbar = (props: { editor: MyEditorType }) => {
8681
);
8782
};
8883

84+
const customReactStyles = {
85+
...defaultStyleSpecs,
86+
small,
87+
fontSize,
88+
};
89+
8990
export function ReactStyles() {
9091
const editor = useBlockNote(
9192
{

examples/editor/examples/vanilla-custom-blocks/App.tsx

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "@blocknote/core";
66
import "@blocknote/core/style.css";
77
import { BlockNoteView, useBlockNote } from "@blocknote/react";
8+
import "./style.css";
89

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

@@ -48,9 +49,7 @@ const alertBlock = createBlockSpec(
4849
{
4950
render: (block, editor) => {
5051
const alert = document.createElement("div");
51-
Object.entries(alertStyles).forEach(([key, value]) => {
52-
alert.style[key as any] = value;
53-
});
52+
alert.className = "alert";
5453
alert.style.backgroundColor =
5554
alertTypes[block.props.type].backgroundColor;
5655

@@ -110,16 +109,30 @@ const alertBlock = createBlockSpec(
110109
}
111110
);
112111

113-
// TODO: use CSS?
114-
const alertStyles = {
115-
display: "flex",
116-
justifyContent: "center",
117-
alignItems: "center",
118-
flexGrow: "1",
119-
height: "48px",
120-
padding: "4px",
121-
maxWidth: "100%",
122-
};
112+
const simpleImageBlock = createBlockSpec(
113+
{
114+
type: "simpleImage",
115+
propSchema: {
116+
src: {
117+
default:
118+
"https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
119+
},
120+
},
121+
content: "none",
122+
},
123+
{
124+
render: (block) => {
125+
const image = document.createElement("img");
126+
image.className = "simple-image";
127+
image.src = block.props.src;
128+
image.alt = "placeholder";
129+
130+
return {
131+
dom: image,
132+
};
133+
},
134+
}
135+
);
123136

124137
const bracketsParagraphBlock = createBlockSpec(
125138
{
@@ -132,9 +145,7 @@ const bracketsParagraphBlock = createBlockSpec(
132145
{
133146
render: () => {
134147
const bracketsParagraph = document.createElement("div");
135-
Object.entries(bracketsParagraphStyles).forEach(([key, value]) => {
136-
bracketsParagraph.style[key as any] = value;
137-
});
148+
bracketsParagraph.className = "brackets-paragraph";
138149

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

148159
const inlineContent = document.createElement("div");
149-
inlineContent.style.flexGrow = "1";
160+
inlineContent.className = "inline-content";
150161

151162
bracketsParagraph.appendChild(inlineContent);
152163

@@ -167,17 +178,6 @@ const bracketsParagraphBlock = createBlockSpec(
167178
}
168179
);
169180

170-
// TODO: use CSS
171-
const bracketsParagraphStyles = {
172-
display: "flex",
173-
justifyContent: "center",
174-
alignItems: "center",
175-
flexGrow: "1",
176-
height: "48px",
177-
padding: "4px",
178-
maxWidth: "100%",
179-
};
180-
181181
export function CustomBlocks() {
182182
const editor = useBlockNote({
183183
domAttributes: {
@@ -190,6 +190,7 @@ export function CustomBlocks() {
190190
...defaultBlockSpecs,
191191
alert: alertBlock,
192192
bracketsParagraph: bracketsParagraphBlock,
193+
simpleImage: simpleImageBlock,
193194
},
194195
initialContent: [
195196
{
@@ -199,6 +200,12 @@ export function CustomBlocks() {
199200
},
200201
content: ["Alert"],
201202
},
203+
{
204+
type: "simpleImage",
205+
props: {
206+
src: "https://t3.ftcdn.net/jpg/02/48/42/64/360_F_248426448_NVKLywWqArG2ADUxDq6QprtIzsF82dMF.jpg",
207+
},
208+
},
202209
{
203210
type: "bracketsParagraph",
204211
content: "Brackets Paragraph",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.alert, .brackets-paragraph {
2+
display: flex;
3+
justify-content: center;
4+
align-items: center;
5+
flex-grow: 1;
6+
height: 48px;
7+
padding: 4px;
8+
max-width: 100%;
9+
}
10+
11+
.simple-image {
12+
width: 100%;
13+
}
14+
15+
.inline-content {
16+
flex-grow: 1;
17+
}

0 commit comments

Comments
 (0)