|
| 1 | +import "@blocknote/core/style.css"; |
| 2 | +import { |
| 3 | + BlockNoteView, |
| 4 | + FormattingToolbarPositioner, |
| 5 | + Toolbar, |
| 6 | + ToolbarButton, |
| 7 | + createReactStyleSpec, |
| 8 | + useActiveStyles, |
| 9 | + useBlockNote, |
| 10 | +} from "@blocknote/react"; |
| 11 | + |
| 12 | +import { |
| 13 | + BlockNoteEditor, |
| 14 | + DefaultBlockSchema, |
| 15 | + DefaultInlineContentSchema, |
| 16 | + StyleSchemaFromSpecs, |
| 17 | + defaultStyleSpecs, |
| 18 | +} from "@blocknote/core"; |
| 19 | + |
| 20 | +type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any }; |
| 21 | + |
| 22 | +const small = createReactStyleSpec( |
| 23 | + { |
| 24 | + type: "small", |
| 25 | + propSchema: "boolean", |
| 26 | + }, |
| 27 | + { |
| 28 | + render: (props) => { |
| 29 | + return <small ref={props.contentRef}></small>; |
| 30 | + }, |
| 31 | + } |
| 32 | +); |
| 33 | + |
| 34 | +const fontSize = createReactStyleSpec( |
| 35 | + { |
| 36 | + type: "fontSize", |
| 37 | + propSchema: "string", |
| 38 | + }, |
| 39 | + { |
| 40 | + render: (props) => { |
| 41 | + return ( |
| 42 | + <span ref={props.contentRef} style={{ fontSize: props.value }}></span> |
| 43 | + ); |
| 44 | + }, |
| 45 | + } |
| 46 | +); |
| 47 | + |
| 48 | +const customReactStyles = { |
| 49 | + ...defaultStyleSpecs, |
| 50 | + small, |
| 51 | + fontSize, |
| 52 | +}; |
| 53 | + |
| 54 | +type MyEditorType = BlockNoteEditor< |
| 55 | + DefaultBlockSchema, |
| 56 | + DefaultInlineContentSchema, |
| 57 | + StyleSchemaFromSpecs<typeof customReactStyles> |
| 58 | +>; |
| 59 | + |
| 60 | +const CustomFormattingToolbar = (props: { editor: MyEditorType }) => { |
| 61 | + const activeStyles = useActiveStyles(props.editor); |
| 62 | + |
| 63 | + return ( |
| 64 | + <Toolbar> |
| 65 | + <ToolbarButton |
| 66 | + mainTooltip={"small"} |
| 67 | + onClick={() => { |
| 68 | + props.editor.toggleStyles({ |
| 69 | + small: true, |
| 70 | + }); |
| 71 | + }} |
| 72 | + isSelected={activeStyles.small}> |
| 73 | + Small |
| 74 | + </ToolbarButton> |
| 75 | + <ToolbarButton |
| 76 | + mainTooltip={"font size"} |
| 77 | + onClick={() => { |
| 78 | + props.editor.toggleStyles({ |
| 79 | + fontSize: "30px", |
| 80 | + }); |
| 81 | + }} |
| 82 | + isSelected={!!activeStyles.fontSize}> |
| 83 | + Font size |
| 84 | + </ToolbarButton> |
| 85 | + </Toolbar> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export function ReactStyles() { |
| 90 | + const editor = useBlockNote( |
| 91 | + { |
| 92 | + styleSpecs: customReactStyles, |
| 93 | + onEditorContentChange: (editor) => { |
| 94 | + console.log(editor.topLevelBlocks); |
| 95 | + }, |
| 96 | + domAttributes: { |
| 97 | + editor: { |
| 98 | + class: "editor", |
| 99 | + "data-test": "editor", |
| 100 | + }, |
| 101 | + }, |
| 102 | + initialContent: [ |
| 103 | + { |
| 104 | + type: "paragraph", |
| 105 | + content: [ |
| 106 | + { |
| 107 | + type: "text", |
| 108 | + text: "large text", |
| 109 | + styles: { |
| 110 | + fontSize: "30px", |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + type: "text", |
| 115 | + text: "small text", |
| 116 | + styles: { |
| 117 | + small: true, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + [] |
| 125 | + ); |
| 126 | + |
| 127 | + // Give tests a way to get prosemirror instance |
| 128 | + (window as WindowWithProseMirror).ProseMirror = editor?._tiptapEditor; |
| 129 | + |
| 130 | + return ( |
| 131 | + <BlockNoteView className="root" editor={editor}> |
| 132 | + <FormattingToolbarPositioner |
| 133 | + editor={editor} |
| 134 | + formattingToolbar={CustomFormattingToolbar} |
| 135 | + /> |
| 136 | + </BlockNoteView> |
| 137 | + ); |
| 138 | +} |
0 commit comments