Skip to content

Commit

Permalink
worked on events
Browse files Browse the repository at this point in the history
  • Loading branch information
LiveDuo committed Nov 5, 2023
1 parent 621a8b2 commit cadb755
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions lib/client/vanilla/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Category = ({ themeIndex, category, components, standaloneServer }) => {
const [show, setShow] = useState(false)

return (
<div>
<div id={category.toLowerCase()}>
<div
onClick={() => setShow((i) => !i)}
className={`h-12 cursor-pointer bg-white border-b last:border-b-0 flex items-center px-2 ${
Expand Down Expand Up @@ -215,15 +215,30 @@ function Editor({ standaloneServer = false }) {

const onCanvasClick = (e) => {
if (isEventOnElement(deleteRef.current, e)) {
canvasRef.current.removeChild(hoveredComponent)
setHoveredComponent()
const clickEvent = new MouseEvent('click', { bubbles: true })
deleteRef.current.dispatchEvent(clickEvent)
} else if (isEventOnElement(moveUpRef.current, e)) {
canvasRef.current.insertBefore(hoveredComponent, hoveredComponent.previousElementSibling)
const clickEvent = new MouseEvent('click', { bubbles: true })
moveUpRef.current.dispatchEvent(clickEvent)
} else if (isEventOnElement(moveDownRef.current, e)) {
canvasRef.current.insertBefore(hoveredComponent.nextElementSibling, hoveredComponent)
const clickEvent = new MouseEvent('click', { bubbles: true })
moveDownRef.current.dispatchEvent(clickEvent)
}
}

const onComponentDelete = () => {
canvasRef.current.removeChild(hoveredComponent)
setHoveredComponent()
}

const onComponentMoveUp = () => {
canvasRef.current.insertBefore(hoveredComponent, hoveredComponent.previousElementSibling)
}

const onComponentMoveDown = () => {
canvasRef.current.insertBefore(hoveredComponent.nextElementSibling, hoveredComponent)
}

const isElementTopHalf = (element, event) => {
const rect = element.getBoundingClientRect()
return rect.top + (rect.bottom - rect.top) / 2 > event.clientY
Expand Down Expand Up @@ -274,12 +289,25 @@ function Editor({ standaloneServer = false }) {
>
<div className="flex flex-row p-1">
{getComponents().indexOf(hoveredComponent) < getComponents().length - 1 && (
<ArrowDownIcon ref={moveDownRef} className="h-7 w-7 text-white p-1" />
<ArrowDownIcon
ref={moveDownRef}
onClick={onComponentMoveDown}
className="h-7 w-7 text-white p-1"
/>
)}
{getComponents().indexOf(hoveredComponent) > 0 && (
<ArrowUpIcon ref={moveUpRef} className="h-7 w-7 text-white p-1" />
<ArrowUpIcon
ref={moveUpRef}
onClick={onComponentMoveUp}
className="h-7 w-7 text-white p-1"
/>
)}
<TrashIcon ref={deleteRef} className="h-7 w-7 text-white p-1" />
<TrashIcon
id={'delete'}
ref={deleteRef}
onClick={onComponentDelete}
className="h-7 w-7 text-white p-1"
/>
</div>
</div>
{!isPreview && (
Expand Down

0 comments on commit cadb755

Please sign in to comment.