-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor as dropdown menu, add variable functionality
- Loading branch information
Showing
5 changed files
with
87 additions
and
40 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
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 |
---|---|---|
@@ -1,43 +1,68 @@ | ||
import { type Editor } from '@tiptap/react'; | ||
import { Plus } from 'lucide-react'; | ||
import { createVariableNode } from '~/lib/block-editor/extensions/Variable/utils'; | ||
import { type TiptapContent, contentMap } from '~/lib/block-editor/types'; | ||
import devProtocol from '~/lib/db/sample-data/dev-protocol'; | ||
import { type TVariableDefinition } from '~/schemas/protocol/variables'; | ||
import { Button } from '../Button'; | ||
import Popover from '../Popover'; | ||
import Heading from '../typography/Heading'; | ||
import DropdownMenu from '../DropdownMenu'; | ||
|
||
const Menu = ({ editor }: { editor: Editor | null }) => { | ||
export default function PlusMenu({ editor }: { editor: Editor | null }) { | ||
if (!editor) return null; | ||
const { variables } = devProtocol; | ||
|
||
const handleCommand = (type: TiptapContent) => () => { | ||
const endPos = editor.state.doc.content.size; | ||
editor.chain().focus().insertContentAt(endPos, contentMap[type]).run(); | ||
editor.view.focus(); | ||
}; | ||
|
||
const addVariable = (variable: TVariableDefinition) => { | ||
const endPos = editor.state.doc.content.size; | ||
const variableNode = createVariableNode({ | ||
newVariable: variable, | ||
view: editor.view, | ||
key: variable.label.en, | ||
}); | ||
const transaction = editor.view.state.tr.insert(endPos, variableNode); | ||
editor.view.dispatch(transaction); | ||
}; | ||
|
||
return ( | ||
<div className="grid gap-2 overflow-scroll p-2"> | ||
<Heading variant="h4">Add block</Heading> | ||
{Object.keys(contentMap).map((type) => ( | ||
<Button | ||
key={type} | ||
onClick={handleCommand(type as TiptapContent)} | ||
variant="outline" | ||
size="sm" | ||
color="primary" | ||
> | ||
{type} | ||
<DropdownMenu.Root> | ||
<DropdownMenu.Trigger> | ||
<Button variant="outline" size="icon" color="primary"> | ||
<Plus /> | ||
</Button> | ||
))} | ||
</div> | ||
); | ||
}; | ||
</DropdownMenu.Trigger> | ||
<DropdownMenu.Content> | ||
<DropdownMenu.Label>Add content</DropdownMenu.Label> | ||
{Object.keys(contentMap).map((type) => ( | ||
<DropdownMenu.Item | ||
key={type} | ||
onSelect={handleCommand(type as TiptapContent)} | ||
textValue={type} | ||
> | ||
{type} | ||
</DropdownMenu.Item> | ||
))} | ||
|
||
// plus button that renders after the editor content and between nodes | ||
export default function PlusMenu({ editor }: { editor: Editor | null }) { | ||
return ( | ||
<Popover content={<Menu editor={editor} />}> | ||
<Button variant="outline" size="icon" color="primary"> | ||
<Plus /> | ||
</Button> | ||
</Popover> | ||
<DropdownMenu.Separator /> | ||
|
||
<DropdownMenu.Label>Add variable</DropdownMenu.Label> | ||
{variables && | ||
Object.entries(variables).map(([key, variable]) => ( | ||
<DropdownMenu.Item | ||
key={key} | ||
onSelect={() => { | ||
addVariable(variable); | ||
}} | ||
textValue={variable.label.en} | ||
> | ||
{variable.label.en} | ||
</DropdownMenu.Item> | ||
))} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Root> | ||
); | ||
} |
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