|
| 1 | +import "@blocknote/core/fonts/inter.css"; |
| 2 | +import { BlockNoteView } from "@blocknote/mantine"; |
| 3 | +import "@blocknote/mantine/style.css"; |
| 4 | +import { useCreateBlockNote } from "@blocknote/react"; |
| 5 | +import { useState } from "react"; |
| 6 | +import "./styles.css"; |
| 7 | + |
| 8 | +export default function App() { |
| 9 | + // Creates a new editor instance. |
| 10 | + const editor = useCreateBlockNote({ |
| 11 | + initialContent: [ |
| 12 | + { |
| 13 | + type: "paragraph", |
| 14 | + content: "Welcome to the Drag & Drop Exclusion demo!", |
| 15 | + }, |
| 16 | + { |
| 17 | + type: "paragraph", |
| 18 | + content: |
| 19 | + "Try dragging the blocks in the editor - they will work as normal.", |
| 20 | + }, |
| 21 | + { |
| 22 | + type: "paragraph", |
| 23 | + content: |
| 24 | + "Now try dragging the colored boxes on the right - they won't interfere with the editor's drag & drop!", |
| 25 | + }, |
| 26 | + ], |
| 27 | + }); |
| 28 | + |
| 29 | + const [draggedItems, setDraggedItems] = useState([ |
| 30 | + { id: "1", color: "#FF6B6B", label: "Red Item" }, |
| 31 | + { id: "2", color: "#4ECDC4", label: "Teal Item" }, |
| 32 | + { id: "3", color: "#45B7D1", label: "Blue Item" }, |
| 33 | + { id: "4", color: "#FFA07A", label: "Orange Item" }, |
| 34 | + ]); |
| 35 | + |
| 36 | + const [droppedItems, setDroppedItems] = useState<typeof draggedItems>([]); |
| 37 | + |
| 38 | + const handleDragStart = ( |
| 39 | + e: React.DragEvent, |
| 40 | + item: (typeof draggedItems)[0], |
| 41 | + ) => { |
| 42 | + e.dataTransfer.effectAllowed = "move"; |
| 43 | + e.dataTransfer.setData("custom-item", JSON.stringify(item)); |
| 44 | + }; |
| 45 | + |
| 46 | + const handleDragOver = (e: React.DragEvent) => { |
| 47 | + e.preventDefault(); |
| 48 | + e.dataTransfer.dropEffect = "move"; |
| 49 | + }; |
| 50 | + |
| 51 | + const handleDrop = (e: React.DragEvent) => { |
| 52 | + e.preventDefault(); |
| 53 | + const data = e.dataTransfer.getData("custom-item"); |
| 54 | + if (data) { |
| 55 | + const item = JSON.parse(data); |
| 56 | + setDroppedItems((prev) => [...prev, item]); |
| 57 | + setDraggedItems((prev) => prev.filter((i) => i.id !== item.id)); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const handleReset = () => { |
| 62 | + setDraggedItems([ |
| 63 | + { id: "1", color: "#FF6B6B", label: "Red Item" }, |
| 64 | + { id: "2", color: "#4ECDC4", label: "Teal Item" }, |
| 65 | + { id: "3", color: "#45B7D1", label: "Blue Item" }, |
| 66 | + { id: "4", color: "#FFA07A", label: "Orange Item" }, |
| 67 | + ]); |
| 68 | + setDroppedItems([]); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="app-container"> |
| 73 | + <div className="editor-section"> |
| 74 | + <h2>BlockNote Editor</h2> |
| 75 | + <BlockNoteView editor={editor} /> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div className={`drag-demo-section bn-drag-exclude`}> |
| 79 | + <h2>Separate Drag & Drop Area</h2> |
| 80 | + <p className="info-text"> |
| 81 | + This area uses the <code>bn-drag-exclude</code> classname, so dragging |
| 82 | + items here won't interfere with the editor. |
| 83 | + </p> |
| 84 | + |
| 85 | + <div className="drag-columns"> |
| 86 | + <div className="drag-column"> |
| 87 | + <h3>Draggable Items</h3> |
| 88 | + <div className="items-container"> |
| 89 | + {draggedItems.map((item) => ( |
| 90 | + <div |
| 91 | + key={item.id} |
| 92 | + className="draggable-item" |
| 93 | + draggable |
| 94 | + onDragStart={(e) => handleDragStart(e, item)} |
| 95 | + style={{ backgroundColor: item.color }} |
| 96 | + > |
| 97 | + {item.label} |
| 98 | + </div> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + |
| 103 | + <div |
| 104 | + className="drag-column drop-zone" |
| 105 | + onDragOver={handleDragOver} |
| 106 | + onDrop={handleDrop} |
| 107 | + > |
| 108 | + <h3>Drop Zone</h3> |
| 109 | + <div className="items-container"> |
| 110 | + {droppedItems.length === 0 ? ( |
| 111 | + <p className="placeholder">Drop items here</p> |
| 112 | + ) : ( |
| 113 | + droppedItems.map((item) => ( |
| 114 | + <div |
| 115 | + key={item.id} |
| 116 | + className="draggable-item" |
| 117 | + style={{ backgroundColor: item.color }} |
| 118 | + > |
| 119 | + {item.label} |
| 120 | + </div> |
| 121 | + )) |
| 122 | + )} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + |
| 127 | + <button className="reset-button" onClick={handleReset}> |
| 128 | + Reset Items |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ); |
| 133 | +} |
0 commit comments