|
| 1 | +/** |
| 2 | + * Copyright 2024 OpenBuild |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useInteractions, useFloating, useClick } from '@floating-ui/react'; |
| 18 | +import { useEditor } from 'novel'; |
| 19 | +import { useState } from 'react'; |
| 20 | + |
| 21 | +import type { ReactNode } from 'react'; |
| 22 | + |
| 23 | +function Item({ children, onClick }: { children: ReactNode; onClick: () => void }) { |
| 24 | + return ( |
| 25 | + <div className="cursor-pointer rounded-sm px-2 py-1 hover:bg-gray-900" onClick={onClick}> |
| 26 | + {children} |
| 27 | + </div> |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +function DragHandle() { |
| 32 | + const { editor } = useEditor(); |
| 33 | + |
| 34 | + const [isOpen, setIsOpen] = useState(false); |
| 35 | + |
| 36 | + const { refs, floatingStyles, context } = useFloating({ |
| 37 | + placement: 'left-start', |
| 38 | + open: isOpen, |
| 39 | + onOpenChange: setIsOpen, |
| 40 | + }); |
| 41 | + const click = useClick(context); |
| 42 | + const { getReferenceProps, getFloatingProps } = useInteractions([click]); |
| 43 | + |
| 44 | + const insertNewBlockBelow = () => { |
| 45 | + if (!editor) return; |
| 46 | + |
| 47 | + return editor |
| 48 | + .chain() |
| 49 | + .focus() |
| 50 | + .command(({ tr }) => { |
| 51 | + const { $from } = tr.selection; |
| 52 | + const insertPos = $from.after($from.depth); |
| 53 | + tr.insert(insertPos, editor.schema.nodes.paragraph.create()); |
| 54 | + |
| 55 | + setIsOpen(false); |
| 56 | + return true; |
| 57 | + }) |
| 58 | + .run(); |
| 59 | + }; |
| 60 | + const deleteBlock = () => { |
| 61 | + if (!editor) return; |
| 62 | + |
| 63 | + return editor |
| 64 | + .chain() |
| 65 | + .focus() |
| 66 | + .command(({ tr }) => { |
| 67 | + const { $from } = tr.selection; |
| 68 | + tr.delete($from.before($from.depth), $from.after($from.depth)); |
| 69 | + |
| 70 | + setIsOpen(false); |
| 71 | + return true; |
| 72 | + }) |
| 73 | + .run(); |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <> |
| 78 | + <div className="BlockEditor-dragHandle" ref={refs.setReference} {...getReferenceProps()} /> |
| 79 | + |
| 80 | + {isOpen && ( |
| 81 | + <div |
| 82 | + className="flex flex-col gap-2 rounded-md border py-1 px-2 border-gray-300 bg-background shadow-md" |
| 83 | + ref={refs.setFloating} |
| 84 | + style={floatingStyles} |
| 85 | + {...getFloatingProps()} |
| 86 | + > |
| 87 | + <Item onClick={deleteBlock}>Delete</Item> |
| 88 | + <Item onClick={insertNewBlockBelow}>Insert</Item> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + </> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +export default DragHandle; |
0 commit comments