Skip to content

Commit

Permalink
Display the floating menu in more places
Browse files Browse the repository at this point in the history
Not sure this is 100% correct yet but is needed for inserting Zettel links
in non-root locations (e.g. menu items). Might want to rework in the future.
  • Loading branch information
IsaacWoods committed Jul 31, 2024
1 parent 4a2f19f commit 3bab9a7
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/src/scenes/zettel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ function ZettelEditor(props: { id: number }) {
}
}, [editor]);

const shouldShowFloatingMenu = ({ editor, view, state, oldState }) => {
const { selection } = state;
const { $anchor, empty } = selection;
const isRootDepth = $anchor.depth === 1;
const isEmptyTextBlock = $anchor.parent.isTextblock && !$anchor.parent.type.spec.code && !$anchor.parent.textContent;

// This differs from the default one in that we don't check the depth, allowing the menu to
// appear in non-root nodes (e.g. list items). We might want to be more specific about where
// idk.
if (!view.hasFocus() || !empty || !isEmptyTextBlock || !editor.isEditable) {
return false;
}

return true;
};

return (
<>
<Header title={zettel ? zettel.title : "Loading Zettel..."} actions={
Expand All @@ -136,7 +152,7 @@ function ZettelEditor(props: { id: number }) {
<Flex auto column>
<Title defaultValue={zettel.title} placeholder="Add a title..." onChange={onChangeTitle} onKeyDown={onTitleKeyDown} />
<StyledEditorContent editor={editor} />
<FloatingMenu editor={editor}>
<FloatingMenu editor={editor} shouldShow={shouldShowFloatingMenu}>
<MenuButton onClick={() => editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run() }>Table</MenuButton>
<MenuButton onClick={addImage}>Image</MenuButton>
<MenuButton onClick={() => editor.chain().focus().setDetails().run() }>Details</MenuButton>
Expand Down Expand Up @@ -203,7 +219,7 @@ const StyledEditorContent = styled(EditorContent)`
padding-left: 1rem;
}
:first-child {
.tiptap :first-child {
margin-top: 0;
}
Expand Down

0 comments on commit 3bab9a7

Please sign in to comment.