|
| 1 | +import { Node } from "prosemirror-model"; |
| 2 | +import { NodeSelection, Selection } from "prosemirror-state"; |
| 3 | +import * as pmView from "prosemirror-view"; |
| 4 | +import { EditorView } from "prosemirror-view"; |
| 5 | + |
| 6 | +import { createExternalHTMLExporter } from "../../api/exporters/html/externalHTMLExporter.js"; |
| 7 | +import { cleanHTMLToMarkdown } from "../../api/exporters/markdown/markdownExporter.js"; |
| 8 | +import { fragmentToBlocks } from "../../api/nodeConversions/fragmentToBlocks.js"; |
| 9 | +import { Block } from "../../blocks/defaultBlocks.js"; |
| 10 | +import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; |
| 11 | +import { UiElementPosition } from "../../extensions-shared/UiElementPosition.js"; |
| 12 | +import { |
| 13 | + BlockSchema, |
| 14 | + InlineContentSchema, |
| 15 | + StyleSchema, |
| 16 | +} from "../../schema/index.js"; |
| 17 | +import { MultipleNodeSelection } from "./MultipleNodeSelection.js"; |
| 18 | + |
| 19 | +let dragImageElement: Element | undefined; |
| 20 | + |
| 21 | +export type SideMenuState< |
| 22 | + BSchema extends BlockSchema, |
| 23 | + I extends InlineContentSchema, |
| 24 | + S extends StyleSchema |
| 25 | +> = UiElementPosition & { |
| 26 | + // The block that the side menu is attached to. |
| 27 | + block: Block<BSchema, I, S>; |
| 28 | +}; |
| 29 | + |
| 30 | +export function getDraggableBlockFromElement( |
| 31 | + element: Element, |
| 32 | + view: EditorView |
| 33 | +) { |
| 34 | + while ( |
| 35 | + element && |
| 36 | + element.parentElement && |
| 37 | + element.parentElement !== view.dom && |
| 38 | + element.getAttribute?.("data-node-type") !== "blockContainer" |
| 39 | + ) { |
| 40 | + element = element.parentElement; |
| 41 | + } |
| 42 | + if (element.getAttribute?.("data-node-type") !== "blockContainer") { |
| 43 | + return undefined; |
| 44 | + } |
| 45 | + return { node: element as HTMLElement, id: element.getAttribute("data-id")! }; |
| 46 | +} |
| 47 | + |
| 48 | +function blockPositionFromElement(element: Element, view: EditorView) { |
| 49 | + const block = getDraggableBlockFromElement(element, view); |
| 50 | + |
| 51 | + if (block && block.node.nodeType === 1) { |
| 52 | + // TODO: this uses undocumented PM APIs? do we need this / let's add docs? |
| 53 | + const docView = (view as any).docView; |
| 54 | + const desc = docView.nearestDesc(block.node, true); |
| 55 | + if (!desc || desc === docView) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + return desc.posBefore; |
| 59 | + } |
| 60 | + return null; |
| 61 | +} |
| 62 | + |
| 63 | +function blockPositionsFromSelection(selection: Selection, doc: Node) { |
| 64 | + // Absolute positions just before the first block spanned by the selection, and just after the last block. Having the |
| 65 | + // selection start and end just before and just after the target blocks ensures no whitespace/line breaks are left |
| 66 | + // behind after dragging & dropping them. |
| 67 | + let beforeFirstBlockPos: number; |
| 68 | + let afterLastBlockPos: number; |
| 69 | + |
| 70 | + // Even the user starts dragging blocks but drops them in the same place, the selection will still be moved just |
| 71 | + // before & just after the blocks spanned by the selection, and therefore doesn't need to change if they try to drag |
| 72 | + // the same blocks again. If this happens, the anchor & head move out of the block content node they were originally |
| 73 | + // in. If the anchor should update but the head shouldn't and vice versa, it means the user selection is outside a |
| 74 | + // block content node, which should never happen. |
| 75 | + const selectionStartInBlockContent = |
| 76 | + doc.resolve(selection.from).node().type.spec.group === "blockContent"; |
| 77 | + const selectionEndInBlockContent = |
| 78 | + doc.resolve(selection.to).node().type.spec.group === "blockContent"; |
| 79 | + |
| 80 | + // Ensures that entire outermost nodes are selected if the selection spans multiple nesting levels. |
| 81 | + const minDepth = Math.min(selection.$anchor.depth, selection.$head.depth); |
| 82 | + |
| 83 | + if (selectionStartInBlockContent && selectionEndInBlockContent) { |
| 84 | + // Absolute positions at the start of the first block in the selection and at the end of the last block. User |
| 85 | + // selections will always start and end in block content nodes, but we want the start and end positions of their |
| 86 | + // parent block nodes, which is why minDepth - 1 is used. |
| 87 | + const startFirstBlockPos = selection.$from.start(minDepth - 1); |
| 88 | + const endLastBlockPos = selection.$to.end(minDepth - 1); |
| 89 | + |
| 90 | + // Shifting start and end positions by one moves them just outside the first and last selected blocks. |
| 91 | + beforeFirstBlockPos = doc.resolve(startFirstBlockPos - 1).pos; |
| 92 | + afterLastBlockPos = doc.resolve(endLastBlockPos + 1).pos; |
| 93 | + } else { |
| 94 | + beforeFirstBlockPos = selection.from; |
| 95 | + afterLastBlockPos = selection.to; |
| 96 | + } |
| 97 | + |
| 98 | + return { from: beforeFirstBlockPos, to: afterLastBlockPos }; |
| 99 | +} |
| 100 | + |
| 101 | +function setDragImage(view: EditorView, from: number, to = from) { |
| 102 | + if (from === to) { |
| 103 | + // Moves to position to be just after the first (and only) selected block. |
| 104 | + to += view.state.doc.resolve(from + 1).node().nodeSize; |
| 105 | + } |
| 106 | + |
| 107 | + // Parent element is cloned to remove all unselected children without affecting the editor content. |
| 108 | + const parentClone = view.domAtPos(from).node.cloneNode(true) as Element; |
| 109 | + const parent = view.domAtPos(from).node as Element; |
| 110 | + |
| 111 | + const getElementIndex = (parentElement: Element, targetElement: Element) => |
| 112 | + Array.prototype.indexOf.call(parentElement.children, targetElement); |
| 113 | + |
| 114 | + const firstSelectedBlockIndex = getElementIndex( |
| 115 | + parent, |
| 116 | + // Expects from position to be just before the first selected block. |
| 117 | + view.domAtPos(from + 1).node.parentElement! |
| 118 | + ); |
| 119 | + const lastSelectedBlockIndex = getElementIndex( |
| 120 | + parent, |
| 121 | + // Expects to position to be just after the last selected block. |
| 122 | + view.domAtPos(to - 1).node.parentElement! |
| 123 | + ); |
| 124 | + |
| 125 | + for (let i = parent.childElementCount - 1; i >= 0; i--) { |
| 126 | + if (i > lastSelectedBlockIndex || i < firstSelectedBlockIndex) { |
| 127 | + parentClone.removeChild(parentClone.children[i]); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // dataTransfer.setDragImage(element) only works if element is attached to the DOM. |
| 132 | + unsetDragImage(view.root); |
| 133 | + dragImageElement = parentClone; |
| 134 | + |
| 135 | + // TODO: This is hacky, need a better way of assigning classes to the editor so that they can also be applied to the |
| 136 | + // drag preview. |
| 137 | + const classes = view.dom.className.split(" "); |
| 138 | + const inheritedClasses = classes |
| 139 | + .filter( |
| 140 | + (className) => |
| 141 | + className !== "ProseMirror" && |
| 142 | + className !== "bn-root" && |
| 143 | + className !== "bn-editor" |
| 144 | + ) |
| 145 | + .join(" "); |
| 146 | + |
| 147 | + dragImageElement.className = |
| 148 | + dragImageElement.className + " bn-drag-preview " + inheritedClasses; |
| 149 | + |
| 150 | + if (view.root instanceof ShadowRoot) { |
| 151 | + view.root.appendChild(dragImageElement); |
| 152 | + } else { |
| 153 | + view.root.body.appendChild(dragImageElement); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +export function unsetDragImage(rootEl: Document | ShadowRoot) { |
| 158 | + if (dragImageElement !== undefined) { |
| 159 | + if (rootEl instanceof ShadowRoot) { |
| 160 | + rootEl.removeChild(dragImageElement); |
| 161 | + } else { |
| 162 | + rootEl.body.removeChild(dragImageElement); |
| 163 | + } |
| 164 | + |
| 165 | + dragImageElement = undefined; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export function dragStart< |
| 170 | + BSchema extends BlockSchema, |
| 171 | + I extends InlineContentSchema, |
| 172 | + S extends StyleSchema |
| 173 | +>( |
| 174 | + e: { dataTransfer: DataTransfer | null; clientY: number }, |
| 175 | + editor: BlockNoteEditor<BSchema, I, S> |
| 176 | +) { |
| 177 | + if (!e.dataTransfer) { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + const view = editor.prosemirrorView; |
| 182 | + |
| 183 | + const editorBoundingBox = view.dom.getBoundingClientRect(); |
| 184 | + |
| 185 | + const coords = { |
| 186 | + left: editorBoundingBox.left + editorBoundingBox.width / 2, // take middle of editor |
| 187 | + top: e.clientY, |
| 188 | + }; |
| 189 | + |
| 190 | + const elements = view.root.elementsFromPoint(coords.left, coords.top); |
| 191 | + let blockEl = undefined; |
| 192 | + |
| 193 | + for (const element of elements) { |
| 194 | + if (view.dom.contains(element)) { |
| 195 | + blockEl = getDraggableBlockFromElement(element, view); |
| 196 | + break; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + if (!blockEl) { |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + const pos = blockPositionFromElement(blockEl.node, view); |
| 205 | + if (pos != null) { |
| 206 | + const selection = view.state.selection; |
| 207 | + const doc = view.state.doc; |
| 208 | + |
| 209 | + const { from, to } = blockPositionsFromSelection(selection, doc); |
| 210 | + |
| 211 | + const draggedBlockInSelection = from <= pos && pos < to; |
| 212 | + const multipleBlocksSelected = |
| 213 | + selection.$anchor.node() !== selection.$head.node() || |
| 214 | + selection instanceof MultipleNodeSelection; |
| 215 | + |
| 216 | + if (draggedBlockInSelection && multipleBlocksSelected) { |
| 217 | + view.dispatch( |
| 218 | + view.state.tr.setSelection(MultipleNodeSelection.create(doc, from, to)) |
| 219 | + ); |
| 220 | + setDragImage(view, from, to); |
| 221 | + } else { |
| 222 | + view.dispatch( |
| 223 | + view.state.tr.setSelection(NodeSelection.create(view.state.doc, pos)) |
| 224 | + ); |
| 225 | + setDragImage(view, pos); |
| 226 | + } |
| 227 | + |
| 228 | + const selectedSlice = view.state.selection.content(); |
| 229 | + const schema = editor.pmSchema; |
| 230 | + |
| 231 | + const clipboardHTML = (pmView as any).__serializeForClipboard( |
| 232 | + view, |
| 233 | + selectedSlice |
| 234 | + ).dom.innerHTML; |
| 235 | + |
| 236 | + const externalHTMLExporter = createExternalHTMLExporter(schema, editor); |
| 237 | + |
| 238 | + const blocks = fragmentToBlocks(selectedSlice.content, editor.schema); |
| 239 | + const externalHTML = externalHTMLExporter.exportBlocks(blocks, {}); |
| 240 | + |
| 241 | + const plainText = cleanHTMLToMarkdown(externalHTML); |
| 242 | + |
| 243 | + e.dataTransfer.clearData(); |
| 244 | + e.dataTransfer.setData("blocknote/html", clipboardHTML); |
| 245 | + e.dataTransfer.setData("text/html", externalHTML); |
| 246 | + e.dataTransfer.setData("text/plain", plainText); |
| 247 | + e.dataTransfer.effectAllowed = "move"; |
| 248 | + e.dataTransfer.setDragImage(dragImageElement!, 0, 0); |
| 249 | + view.dragging = { slice: selectedSlice, move: true }; |
| 250 | + } |
| 251 | +} |
0 commit comments