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

wip: toggle / folding heading #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/components/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export const Icons = {
borderTop,
bug: Bug,
check: Check,
chevronDown: ChevronDown,
chevronRight: ChevronRight,
chevronsUpDown: ChevronsUpDown,
clear: X,
Expand Down
29 changes: 29 additions & 0 deletions src/views/edit/editor/elements/ToggleElement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from "react";
import { withRef } from "@udecode/cn";
import { PlateElement, useElement } from "@udecode/plate-common";
import { useToggleButton, useToggleButtonState } from "@udecode/plate-toggle";

import { Icons } from "../../../../components/icons";

export const ToggleElement = withRef<typeof PlateElement>(
({ children, ...props }, ref) => {
const element = useElement();
const state = useToggleButtonState(element.id as string);
const { buttonProps, open } = useToggleButton(state);

return (
<PlateElement asChild ref={ref} {...props}>
<div className="relative pl-6">
<span
className="absolute -left-0.5 -top-0.5 flex cursor-pointer select-none items-center justify-center rounded-sm p-px transition-colors hover:bg-slate-200"
contentEditable={false}
{...buttonProps}
>
{open ? <Icons.chevronDown /> : <Icons.chevronRight />}
</span>
{children}
</div>
</PlateElement>
);
},
);
10 changes: 10 additions & 0 deletions src/views/edit/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ import {
MARK_SUPERSCRIPT,
MARK_UNDERLINE,

// Toggles
createTogglePlugin,
ELEMENT_TOGGLE,

// images
// https://platejs.org/docs/media
createSelectOnBackspacePlugin,
Expand Down Expand Up @@ -151,6 +155,8 @@ export default observer(
},
}),

createTogglePlugin(),

// Plate's media handler turns youtube links, twitter links, etc, into embeds.
// I'm unsure how to trigger the logic, probably via toolbar or shortcut.
// createMediaEmbedPlugin(),
Expand Down Expand Up @@ -267,11 +273,13 @@ export default observer(
ELEMENT_H6,
ELEMENT_BLOCKQUOTE,
ELEMENT_CODE_BLOCK,
ELEMENT_TOGGLE,
],
},
},
}),

// todo: I think I need to configure validTypes, similar to createIndentPlugin
createIndentListPlugin(),
createTrailingBlockPlugin({ type: ELEMENT_PARAGRAPH }),
],
Expand Down Expand Up @@ -317,6 +325,8 @@ export default observer(
[MARK_SUPERSCRIPT]: withProps(PlateLeaf, { as: "sup" }),
[MARK_UNDERLINE]: withProps(PlateLeaf, { as: "u" }),
// [MARK_COMMENT]: CommentLeaf,

[ELEMENT_TOGGLE]: withProps(PlateElement, { as: "details" }),
},
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/views/edit/editor/toolbar/EditorToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import InsertBlockDropdown from "./components/InsertBlockDropdown";
import ChangeBlockDropdown from "./components/ChangeBlockDropdown";
import DebugDropdown from "./components/DebugDropdown";
import { EditorMode } from "../../EditorMode";
import { ToggleToolbarButton } from "./components/ToggleToolbarButton";

interface Props {
selectedEditorMode: EditorMode;
Expand Down Expand Up @@ -85,6 +86,7 @@ export function EditorToolbar({
<MarkToolbarButton tooltip="Code (⌘+E)" nodeType={MARK_CODE}>
<Icons.code />
</MarkToolbarButton>
<ToggleToolbarButton />
</>

{/* NOTE: id prop was removed, probably was for Playground to support multiple editors */}
Expand Down
23 changes: 23 additions & 0 deletions src/views/edit/editor/toolbar/components/ToggleToolbarButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";

import { withRef } from "@udecode/cn";
import {
useToggleToolbarButton,
useToggleToolbarButtonState,
} from "@udecode/plate-toggle";

import { Icons } from "../../../../../../src/components/icons";
import { ToolbarButton } from "../../components/Toolbar";

export const ToggleToolbarButton = withRef<typeof ToolbarButton>(
(rest, ref) => {
const state = useToggleToolbarButtonState();
const { props } = useToggleToolbarButton(state);

return (
<ToolbarButton ref={ref} tooltip="Toggle" {...props} {...rest}>
<Icons.chevronsUpDown />
</ToolbarButton>
);
},
);
Loading