-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from sereneinserenade/feat/draggable-blocks
feat: draggable blocks
- Loading branch information
Showing
15 changed files
with
517 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,12 @@ | |
pointer-events: none; | ||
} | ||
|
||
h1, | ||
h2, | ||
h3 { | ||
margin: 0; | ||
} | ||
|
||
ul, | ||
ol { | ||
padding: 0 1rem; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/components/editor/extensions/dBlock/DBlockNodeView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
|
||
import { Button, Tooltip, styled } from '@nextui-org/react'; | ||
import { NodeViewContent, NodeViewProps, NodeViewWrapper } from "@tiptap/react"; | ||
import React, { useMemo } from "react"; | ||
import { BiPlus } from 'react-icons/bi'; | ||
import { MdClose, MdDragIndicator } from 'react-icons/md'; | ||
import { RiArrowDownLine, RiArrowUpLine } from 'react-icons/ri'; | ||
|
||
import './DBlockStyle.scss' | ||
|
||
const LeftSection = styled('section', { | ||
display: 'flex', | ||
gap: '4px', | ||
alignItems: 'center' | ||
}) | ||
|
||
const MoveButtonsContainer = styled('div', { | ||
display: 'flex', | ||
justifyContent: 'space-between' | ||
}) | ||
|
||
export const DBlockNodeView: React.FC<NodeViewProps> = ({ | ||
node, | ||
getPos, | ||
editor, | ||
deleteNode | ||
}) => { | ||
const isTable = useMemo(() => { | ||
const { content } = node.content as any; | ||
|
||
return content[0].type.name === "table"; | ||
}, [node.content]); | ||
|
||
const createNodeAfter = () => { | ||
const pos = getPos() + node.nodeSize; | ||
|
||
editor | ||
.chain() | ||
.insertContentAt(pos, { | ||
type: "dBlock", | ||
content: [ | ||
{ | ||
type: "paragraph", | ||
}, | ||
], | ||
}) | ||
.focus(pos + 2) | ||
.run(); | ||
}; | ||
|
||
const onDeleteClicked = () => { | ||
deleteNode() | ||
|
||
setTimeout(() => editor.commands.focus()) | ||
} | ||
|
||
const moveNode = (dir: 'up' | 'down') => { | ||
const { from, to } = editor.state.selection | ||
const [nodeFrom, nodeTo] = [getPos(), getPos() + node.nodeSize] | ||
|
||
if (!(nodeFrom <= from && to <= nodeTo)) editor.commands.focus(getPos() + 2) | ||
|
||
setTimeout(() => editor.chain().moveNode(dir).focus().run()) | ||
} | ||
|
||
return ( | ||
<NodeViewWrapper as="div" className="dBlockWrapper flex"> | ||
{ | ||
editor.isEditable && ( | ||
<LeftSection | ||
aria-label="left-menu" | ||
contentEditable="false" | ||
> | ||
<button | ||
className="d-block-button" | ||
onClick={createNodeAfter} | ||
> | ||
<BiPlus /> | ||
</button> | ||
<Tooltip | ||
content={( | ||
<MoveButtonsContainer> | ||
<Button.Group size='sm' css={{ padding: 0, margin: 0 }} flat> | ||
<Button auto size={'sm'} icon={<RiArrowUpLine />} onPress={() => moveNode('up')} /> | ||
<Button auto size={'sm'} icon={<MdClose />} onPress={onDeleteClicked} /> | ||
<Button auto size={'sm'} icon={<RiArrowDownLine />} onPress={() => moveNode('down')} /> | ||
</Button.Group> | ||
</MoveButtonsContainer> | ||
)} | ||
trigger={'click'} | ||
hideArrow | ||
placement='bottom' | ||
> | ||
<div | ||
className="d-block-button drag-handle" | ||
contentEditable={false} | ||
draggable | ||
data-drag-handle | ||
> | ||
<MdDragIndicator /> | ||
</div> | ||
</Tooltip> | ||
</LeftSection> | ||
) | ||
} | ||
|
||
<NodeViewContent className={`content w-full ${isTable ? "is-table" : ""}`} /> | ||
</NodeViewWrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
.dBlockWrapper { | ||
gap: 8px; | ||
width: 100%; | ||
position: relative; | ||
|
||
&:hover { | ||
.d-block-button { | ||
opacity: 1; | ||
transition: none; | ||
} | ||
} | ||
|
||
.d-block-button { | ||
opacity: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
background: none; | ||
border: none !important; | ||
height: 28px; | ||
padding: 2px; | ||
cursor: pointer; | ||
border-radius: 4px; | ||
transition: all 0.25s ease-in-out; | ||
|
||
&:hover { | ||
background: var(--nextui-colors-selection); | ||
} | ||
|
||
&.drag-handle { | ||
cursor: grab; | ||
|
||
&:hover:active { | ||
cursor: grabbing; | ||
} | ||
} | ||
} | ||
|
||
.content { | ||
width: 100%; | ||
min-width: 4px; | ||
|
||
&.is-table { | ||
margin-left: 2rem; | ||
} | ||
} | ||
} |
Oops, something went wrong.