-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bdfae2
commit 7defd22
Showing
9 changed files
with
233 additions
and
68 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
12 changes: 0 additions & 12 deletions
12
frontend/src/modules/common/blocknote/custom-side-menu.tsx
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
frontend/src/modules/common/blocknote/custom-side-menu/drag-handle-button.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,75 @@ | ||
import type { | ||
BlockSchema, | ||
DefaultBlockSchema, | ||
DefaultInlineContentSchema, | ||
DefaultStyleSchema, | ||
InlineContentSchema, | ||
StyleSchema, | ||
} from '@blocknote/core'; | ||
import { DragHandleMenu, type SideMenuProps, useComponentsContext } from '@blocknote/react'; | ||
import { GripVertical } from 'lucide-react'; | ||
|
||
type CustomDragHandleButtonProps< | ||
BSchema extends BlockSchema = DefaultBlockSchema, | ||
I extends InlineContentSchema = DefaultInlineContentSchema, | ||
S extends StyleSchema = DefaultStyleSchema, | ||
> = Omit<SideMenuProps<BSchema, I, S>, 'addBlock'> & { | ||
haveDropDown?: boolean; | ||
position?: 'left' | 'right' | 'top' | 'bottom'; | ||
}; | ||
export const CustomDragHandleButton = < | ||
BSchema extends BlockSchema = DefaultBlockSchema, | ||
I extends InlineContentSchema = DefaultInlineContentSchema, | ||
S extends StyleSchema = DefaultStyleSchema, | ||
>( | ||
props: CustomDragHandleButtonProps<BSchema, I, S>, | ||
) => { | ||
// biome-ignore lint/style/noNonNullAssertion: req by author | ||
const Components = useComponentsContext()!; | ||
|
||
const Content = props.dragHandleMenu || DragHandleMenu; | ||
|
||
// Wrapper to match the signature of onDragStart | ||
const handleDragStart = (e: React.DragEvent<Element>) => { | ||
if (props.blockDragStart) { | ||
const eventData = { | ||
dataTransfer: e.dataTransfer, | ||
clientY: e.clientY, | ||
}; | ||
props.blockDragStart(eventData, props.block); | ||
} | ||
}; | ||
return ( | ||
<Components.Generic.Menu.Root | ||
onOpenChange={(open: boolean) => { | ||
if (open) props.freezeMenu(); | ||
else props.unfreezeMenu(); | ||
}} | ||
position={props.position} | ||
> | ||
{props.haveDropDown ? ( | ||
<Components.Generic.Menu.Trigger> | ||
<Components.SideMenu.Button | ||
label="Open side menu" | ||
draggable={true} | ||
onDragStart={handleDragStart} | ||
onDragEnd={props.blockDragEnd} | ||
className={'bn-button'} | ||
icon={<GripVertical size={22} data-test="dragHandle" />} | ||
/> | ||
</Components.Generic.Menu.Trigger> | ||
) : ( | ||
<Components.SideMenu.Button | ||
label="Drag button" | ||
draggable={true} | ||
onDragStart={handleDragStart} | ||
onDragEnd={props.blockDragEnd} | ||
className={'bn-button'} | ||
icon={<GripVertical size={22} data-test="dragHandle" />} | ||
/> | ||
)} | ||
|
||
<Content block={props.block} /> | ||
</Components.Generic.Menu.Root> | ||
); | ||
}; |
28 changes: 28 additions & 0 deletions
28
frontend/src/modules/common/blocknote/custom-side-menu/index.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,28 @@ | ||
import { DragHandleMenu, SideMenu, SideMenuController } from '@blocknote/react'; | ||
import { CustomDragHandleButton } from './drag-handle-button'; | ||
import { ResetBlockTypeItem } from './reset-block-type'; | ||
|
||
const typeOnSlashMenuAppearance = ['paragraph', 'heading', 'bulletListItem', 'numberedListItem', 'checkListItem']; | ||
|
||
// in this menu we have only drag button | ||
export const CustomSideMenu = () => ( | ||
<SideMenuController | ||
sideMenu={(props) => ( | ||
<SideMenu {...props}> | ||
<CustomDragHandleButton | ||
haveDropDown={typeOnSlashMenuAppearance.includes(props.block.type)} | ||
dragHandleMenu={(props) => ( | ||
<> | ||
{typeOnSlashMenuAppearance.includes(props.block.type) ? ( | ||
<DragHandleMenu {...props}> | ||
<ResetBlockTypeItem {...props} /> | ||
</DragHandleMenu> | ||
) : null} | ||
</> | ||
)} | ||
{...props} | ||
/> | ||
</SideMenu> | ||
)} | ||
/> | ||
); |
71 changes: 71 additions & 0 deletions
71
frontend/src/modules/common/blocknote/custom-side-menu/reset-block-type.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,71 @@ | ||
import type { Block, BlockConfig, BlockSchema, InlineContentSchema, StyleSchema } from '@blocknote/core'; | ||
import { | ||
type BlockTypeSelectItem, | ||
type DragHandleMenuProps, | ||
blockTypeSelectItems, | ||
useBlockNoteEditor, | ||
useComponentsContext, | ||
useDictionary, | ||
} from '@blocknote/react'; | ||
import { useMemo } from 'react'; | ||
import { customBlockTypeSelectItems } from '~/modules/common/blocknote/blocknote-config'; | ||
import type { BlockTypes } from '~/modules/common/blocknote/types'; | ||
|
||
export function ResetBlockTypeItem(props: DragHandleMenuProps) { | ||
// biome-ignore lint/style/noNonNullAssertion: required by author | ||
const Components = useComponentsContext()!; | ||
const dict = useDictionary(); | ||
|
||
const editor = useBlockNoteEditor<BlockSchema, InlineContentSchema, StyleSchema>(); | ||
|
||
const filteredItems = useMemo(() => { | ||
return blockTypeSelectItems(dict).filter((item) => customBlockTypeSelectItems.includes(item.type as BlockTypes)); | ||
}, [editor, dict]); | ||
|
||
const shouldShow: boolean = useMemo( | ||
() => filteredItems.find((item) => item.type === props.block.type) !== undefined, | ||
[props.block.type, filteredItems], | ||
); | ||
|
||
const fullItems = useMemo(() => { | ||
const onClick = (item: BlockTypeSelectItem) => { | ||
editor.focus(); | ||
|
||
editor.updateBlock(props.block, { | ||
type: item.type, | ||
//In our case we pass props cos by it we get heading level: 1 | 2 | 3 | ||
// biome-ignore lint/suspicious/noExplicitAny: required by author | ||
props: item.props as any, | ||
}); | ||
}; | ||
|
||
return filteredItems.map((item) => { | ||
const { icon: Icon, isSelected, name } = item; | ||
return { | ||
type: item.type, | ||
title: name, | ||
icon: <Icon size={16} />, | ||
onClick: () => onClick(item), | ||
isSelected: isSelected(props.block as unknown as Block<Record<string, BlockConfig>, InlineContentSchema, StyleSchema>), | ||
}; | ||
}); | ||
}, [props.block, filteredItems, editor]); | ||
|
||
if (!shouldShow || !editor.isEditable) return null; | ||
|
||
return ( | ||
<> | ||
{fullItems.map((el) => { | ||
let isSelected = false; | ||
if (props.block.type === 'heading') { | ||
isSelected = el.title.includes(props.block.props.level.toString()); | ||
} else isSelected = props.block.type === el.type; | ||
return ( | ||
<Components.Generic.Menu.Item className="bn-menu-item" key={el.title} onClick={el.onClick} icon={el.icon} checked={isSelected}> | ||
{el.title} | ||
</Components.Generic.Menu.Item> | ||
); | ||
})} | ||
</> | ||
); | ||
} |
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