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

ZUI editor refactor #2476

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 0 additions & 90 deletions src/zui/ZUIEditor/BlockMenu.tsx

This file was deleted.

115 changes: 0 additions & 115 deletions src/zui/ZUIEditor/BlockToolbar.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
import { Add } from '@mui/icons-material';
import { IconButton, Paper } from '@mui/material';
import { Box } from '@mui/system';
import { useCommands, useEditorState, useEditorView } from '@remirror/react';
import { FC, useEffect, useState } from 'react';
import { useCommands } from '@remirror/react';
import { FC } from 'react';

type BlockDividerData = {
pos: number;
y: number;
import { BlockDividerData } from './index';

type BlockInsertProps = {
blockDividers: BlockDividerData[];
mouseY: number;
Comment on lines +10 to +11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but I do find it a little bit off-putting to pass mouse state as a prop, for some reason. I think I would prefer the logic that needs this state to also update the blockdividers, e.g. by setting a visible property on them or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like a good idea to keep a property like that on the divider objects! it's a very simple refactor.

};

const BlockInsert: FC = () => {
const view = useEditorView();
const state = useEditorState();
const [mouseY, setMouseY] = useState(-Infinity);
const BlockInsert: FC<BlockInsertProps> = ({ blockDividers, mouseY }) => {
const { insertParagraph, focus } = useCommands();

useEffect(() => {
const handleMouseMove = (ev: Event) => {
if (ev.type == 'mousemove') {
const mouseEvent = ev as MouseEvent;
const editorRect = view.dom.getBoundingClientRect();
setMouseY(mouseEvent.clientY - editorRect.y);
}
};

view.root.addEventListener('mousemove', handleMouseMove);

return () => {
view.root.removeEventListener('mousemove', handleMouseMove);
};
}, [view.root]);

let pos = 0;
const blockDividers: BlockDividerData[] = [
{
pos: 0,
y: 0,
},
...state.doc.children.map((blockNode) => {
pos += blockNode.nodeSize;
const rect = view.coordsAtPos(pos - 1);

const containerRect = view.dom.getBoundingClientRect();

return {
pos: pos,
y: rect.bottom - containerRect.top,
};
}),
];

return (
<Box position="relative">
{blockDividers.map(({ pos, y }, index) => {
Expand Down
37 changes: 37 additions & 0 deletions src/zui/ZUIEditor/EditorOverlays/BlockMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Box, MenuItem, Paper } from '@mui/material';
import { FC } from 'react';

import useBlockMenu from './useBlockMenu';

type Props = {
blocks: {
id: string;
label: string;
}[];
};

const BlockMenu: FC<Props> = ({ blocks }) => {
const { filteredBlocks, menu } = useBlockMenu(blocks);

return (
<Box {...menu.getMenuProps()}>
<Paper>
{filteredBlocks.map((item, index) => {
const props = menu.getItemProps({ index, item });
return (
<MenuItem
key={item.id}
component="a"
selected={!!props['aria-current']}
{...props}
>
{item.label}
</MenuItem>
);
})}
</Paper>
</Box>
);
};

export default BlockMenu;
54 changes: 54 additions & 0 deletions src/zui/ZUIEditor/EditorOverlays/BlockToolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Box, Button, Paper } from '@mui/material';
import { useCommands } from '@remirror/react';
import { FC } from 'react';

type BlockToolbarProps = {
curBlockType: string;
curBlockY: number;
pos: number;
};

const BlockToolbar: FC<BlockToolbarProps> = ({
curBlockType,
curBlockY,
pos,
}) => {
const { convertParagraph, toggleHeading, pickImage } = useCommands();

return (
<Box position="relative">
<Box
sx={{
left: 0,
position: 'absolute',
top: curBlockY - 50,
transition: 'opacity 0.5s',
zIndex: 10000,
}}
>
<Paper elevation={1} sx={{ p: 1 }}>
{curBlockType}
{curBlockType == 'zimage' && (
<Button
onClick={() => {
pickImage(pos);
}}
>
Change image
</Button>
)}
{curBlockType == 'heading' && (
<Button onClick={() => convertParagraph()}>
Convert to paragraph
</Button>
)}
{curBlockType == 'paragraph' && (
<Button onClick={() => toggleHeading()}>Convert to heading</Button>
)}
</Paper>
</Box>
</Box>
);
};

export default BlockToolbar;
Loading
Loading