-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: dragHandle menu #163
base: test
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright 2024 OpenBuild | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { useInteractions, useFloating, useClick } from '@floating-ui/react'; | ||
import { useEditor } from 'novel'; | ||
import { useState } from 'react'; | ||
|
||
import type { EditorInstance} from 'novel'; | ||
import type { ReactNode } from 'react'; | ||
|
||
function Item({ children, onClick }: { children: ReactNode; onClick: () => void }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用 |
||
return ( | ||
<div className="cursor-pointer rounded-sm px-2 py-1 hover:bg-gray-900" onClick={onClick}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
function DragHandle() { | ||
const { editor } = useEditor(); | ||
|
||
const [isOpen, setIsOpen] = useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用 |
||
|
||
const { refs, floatingStyles, context } = useFloating({ | ||
placement: 'left-start', | ||
open: isOpen, | ||
onOpenChange: setIsOpen, | ||
}); | ||
const click = useClick(context); | ||
const { getReferenceProps, getFloatingProps } = useInteractions([click]); | ||
|
||
const getDragHandlePos = (editor: EditorInstance) => { | ||
const { left, top } = refs.reference.current!.getBoundingClientRect(); | ||
const posAtCoords = editor.view.posAtCoords({ left: left + 50, top: top + 1 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这 |
||
|
||
return posAtCoords; | ||
}; | ||
|
||
const insertNewBlockBelow = () => { | ||
if (!editor) return; | ||
|
||
return editor | ||
.chain() | ||
.focus() | ||
.command(({ tr }) => { | ||
const posAtCoords = getDragHandlePos(editor); | ||
if (!posAtCoords) return false; | ||
|
||
const $pos = editor.state.doc.resolve(posAtCoords.pos); | ||
const insertPos = $pos.after($pos.depth); | ||
tr.insert(insertPos, editor.schema.nodes.paragraph.create()); | ||
|
||
setIsOpen(false); | ||
return true; | ||
}) | ||
.run(); | ||
}; | ||
const deleteBlock = () => { | ||
if (!editor) return; | ||
|
||
return editor | ||
.chain() | ||
.focus() | ||
.command(({ tr }) => { | ||
const posAtCoords = getDragHandlePos(editor); | ||
if (!posAtCoords) return false; | ||
|
||
const $pos = editor.state.doc.resolve(posAtCoords.pos); | ||
const from = $pos.before($pos.depth); | ||
const to = $pos.after($pos.depth); | ||
tr.delete(from, to); | ||
|
||
setIsOpen(false); | ||
return true; | ||
}) | ||
.run(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="BlockEditor-dragHandle" ref={refs.setReference} {...getReferenceProps()} /> | ||
|
||
{isOpen && ( | ||
<div | ||
className="flex flex-col gap-2 rounded-md border py-1 px-2 border-gray-300 bg-background shadow-md" | ||
ref={refs.setFloating} | ||
style={floatingStyles} | ||
{...getFloatingProps()} | ||
> | ||
<Item onClick={deleteBlock}>Delete</Item> | ||
<Item onClick={insertNewBlockBelow}>Insert</Item> | ||
</div> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default DragHandle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
固定版本号