|
| 1 | +import { defaultBlockSpecs, defaultProps } from "@blocknote/core"; |
| 2 | +import "@blocknote/core/style.css"; |
| 3 | +import { |
| 4 | + BlockNoteView, |
| 5 | + createReactBlockSpec, |
| 6 | + useBlockNote, |
| 7 | +} from "@blocknote/react"; |
| 8 | +import "../vanilla-custom-blocks/style.css"; |
| 9 | + |
| 10 | +type WindowWithProseMirror = Window & typeof globalThis & { ProseMirror: any }; |
| 11 | + |
| 12 | +// The types of alerts that users can choose from |
| 13 | +const alertTypes = { |
| 14 | + warning: { |
| 15 | + icon: "⚠️", |
| 16 | + color: "#e69819", |
| 17 | + backgroundColor: "#fff6e6", |
| 18 | + }, |
| 19 | + error: { |
| 20 | + icon: "⛔", |
| 21 | + color: "#d80d0d", |
| 22 | + backgroundColor: "#ffe6e6", |
| 23 | + }, |
| 24 | + info: { |
| 25 | + icon: "ℹ️", |
| 26 | + color: "#507aff", |
| 27 | + backgroundColor: "#e6ebff", |
| 28 | + }, |
| 29 | + success: { |
| 30 | + icon: "✅", |
| 31 | + color: "#0bc10b", |
| 32 | + backgroundColor: "#e6ffe6", |
| 33 | + }, |
| 34 | +}; |
| 35 | + |
| 36 | +export const alertBlock = createReactBlockSpec( |
| 37 | + { |
| 38 | + type: "alert", |
| 39 | + propSchema: { |
| 40 | + textAlignment: defaultProps.textAlignment, |
| 41 | + textColor: defaultProps.textColor, |
| 42 | + type: { |
| 43 | + default: "warning" as const, |
| 44 | + values: ["warning", "error", "info", "success"] as const, |
| 45 | + }, |
| 46 | + }, |
| 47 | + content: "inline", |
| 48 | + }, |
| 49 | + { |
| 50 | + render: (props) => ( |
| 51 | + <div |
| 52 | + className={"alert"} |
| 53 | + style={{ |
| 54 | + backgroundColor: alertTypes[props.block.props.type].backgroundColor, |
| 55 | + }}> |
| 56 | + <select |
| 57 | + contentEditable={false} |
| 58 | + value={props.block.props.type} |
| 59 | + onChange={(event) => { |
| 60 | + props.editor.updateBlock(props.block, { |
| 61 | + type: "alert", |
| 62 | + props: { type: event.target.value as keyof typeof alertTypes }, |
| 63 | + }); |
| 64 | + }}> |
| 65 | + <option value="warning">{alertTypes["warning"].icon}</option> |
| 66 | + <option value="error">{alertTypes["error"].icon}</option> |
| 67 | + <option value="info">{alertTypes["info"].icon}</option> |
| 68 | + <option value="success">{alertTypes["success"].icon}</option> |
| 69 | + </select> |
| 70 | + <div className={"inline-content"} ref={props.contentRef} /> |
| 71 | + </div> |
| 72 | + ), |
| 73 | + } |
| 74 | +); |
| 75 | + |
| 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 | + |
| 98 | +export const bracketsParagraphBlock = createReactBlockSpec( |
| 99 | + { |
| 100 | + type: "bracketsParagraph", |
| 101 | + content: "inline", |
| 102 | + propSchema: { |
| 103 | + ...defaultProps, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + render: (props) => ( |
| 108 | + <div className={"brackets-paragraph"}> |
| 109 | + <div contentEditable={"false"}>{"["}</div> |
| 110 | + <span contentEditable={"false"}>{"{"}</span> |
| 111 | + <div className={"inline-content"} ref={props.contentRef} /> |
| 112 | + <span contentEditable={"false"}>{"}"}</span> |
| 113 | + <div contentEditable={"false"}>{"]"}</div> |
| 114 | + </div> |
| 115 | + ), |
| 116 | + } |
| 117 | +); |
| 118 | + |
| 119 | +export function ReactCustomBlocks() { |
| 120 | + const editor = useBlockNote({ |
| 121 | + domAttributes: { |
| 122 | + editor: { |
| 123 | + class: "editor", |
| 124 | + "data-test": "editor", |
| 125 | + }, |
| 126 | + }, |
| 127 | + blockSpecs: { |
| 128 | + ...defaultBlockSpecs, |
| 129 | + alert: alertBlock, |
| 130 | + simpleImage: simpleImageBlock, |
| 131 | + bracketsParagraph: bracketsParagraphBlock, |
| 132 | + }, |
| 133 | + initialContent: [ |
| 134 | + { |
| 135 | + type: "alert", |
| 136 | + props: { |
| 137 | + type: "success", |
| 138 | + }, |
| 139 | + content: "Alert", |
| 140 | + }, |
| 141 | + { |
| 142 | + type: "simpleImage", |
| 143 | + props: { |
| 144 | + src: "https://t3.ftcdn.net/jpg/02/48/42/64/360_F_248426448_NVKLywWqArG2ADUxDq6QprtIzsF82dMF.jpg", |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + type: "bracketsParagraph", |
| 149 | + content: "Brackets Paragraph", |
| 150 | + }, |
| 151 | + ], |
| 152 | + }); |
| 153 | + |
| 154 | + // Give tests a way to get prosemirror instance |
| 155 | + (window as WindowWithProseMirror).ProseMirror = editor?._tiptapEditor; |
| 156 | + |
| 157 | + return <BlockNoteView className="root" editor={editor} />; |
| 158 | +} |
0 commit comments