Skip to content
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

Open
wants to merge 3 commits into
base: test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"fixStyle": "separate-type-imports",
"prefer": "type-imports"
}
]
],
"@typescript-eslint/no-explicit-any": "off"
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@bytemd/react": "^1.21.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/units": "^5.7.0",
"@floating-ui/react": "^0.27.5",
"@headlessui/react": "^1.7.7",
"@heroicons/react": "^2.0.16",
"@mysten/dapp-kit": "^0.11.1",
Expand Down Expand Up @@ -136,7 +137,7 @@
"eslint-plugin-perfectionist": "4.8.0",
"eslint-plugin-react": "7.31.11",
"eslint-plugin-tailwindcss": "3.15.1",
"eslint-plugin-unused-imports": "3.0.0",
"eslint-plugin-unused-imports": "3.2.0",
"husky": "9.1.7",
"ksio": "^0.0.3",
"lint-staged": "15.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
ImageResizer, handleCommandNavigation,
} from 'novel';

import type { EditorInstance} from 'novel';

import { isFunction } from '../../../utils';
import BlockEditorBubble from './bubble';
import DragHandle from './DragHandle';
Expand All @@ -32,8 +34,15 @@ import { slashCommand, suggestionItems } from './slash';

const extensions = [...defaultExtensions, slashCommand];

function BlockEditor({ className, data, onChange, editable = false }) {
const handleUpdate = ({ editor }) => {
interface BlockEditorProps {
className?: string;
data: any;
onChange: (data: any) => void;
editable?: boolean;
}

function BlockEditor({ className, data, onChange, editable = false }: BlockEditorProps) {
const handleUpdate = ({ editor }: { editor: EditorInstance }) => {
isFunction(onChange) && onChange(editor.getJSON());
};

Expand Down Expand Up @@ -64,7 +73,7 @@ function BlockEditor({ className, data, onChange, editable = false }) {
{suggestionItems.map(item => (
<EditorCommandItem
value={item.title}
onCommand={val => item.command(val)}
onCommand={val => item.command?.(val)}
className="flex w-full items-center space-x-2 rounded-md px-2 py-1 text-left text-sm hover:bg-neutral-200 aria-selected:bg-neutral-200"
key={item.title}
>
Expand Down
50 changes: 0 additions & 50 deletions src/shared/components/control/block-editor/DragHandle.js

This file was deleted.

111 changes: 111 additions & 0 deletions src/shared/components/control/block-editor/DragHandle.tsx
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 }) {
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);

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 });

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;
Loading