Skip to content

Commit

Permalink
feat: add line spacing button to editor toolbar
Browse files Browse the repository at this point in the history
fix #128
  • Loading branch information
erichartline committed May 16, 2019
1 parent 580f03a commit c3d91ac
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/editor/PageEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { AlignmentNode } from "./plugins/alignment"
import { DividerNode } from "./plugins/divider"
import { H1Node, H2Node, H3Node } from "./plugins/heading"
import { ImageNode } from "./plugins/image"
import { LineSpacingNode } from "./plugins/linespacing"
import { LinkNode } from "./plugins/link"
import {
ListItemNode,
Expand Down Expand Up @@ -153,6 +154,8 @@ export const renderNode = (props: nodeProps) => {
return <H3Node {...props} />
case "image":
return <ImageNode {...props} />
case "line-spacing":
return <LineSpacingNode {...props} />
case "link":
return <LinkNode {...props} />
case "list-item":
Expand Down
80 changes: 80 additions & 0 deletions src/components/editor/plugins/linespacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @flow
import React from "react"
import Tooltip from "@material-ui/core/Tooltip"
import Menu from "@material-ui/core/Menu"
import MenuItem from "@material-ui/core/MenuItem"
import ToolbarButton from "../toolbar/ToolbarButton"
import LineSpacingIcon from "@material-ui/icons/FormatLineSpacing"

/**
* Functions to set the line spacing blocks.
*/
const lineSpacingNodeStrategy = (value, size) =>
value
.change()
.unwrapBlock("line-spacing")
.wrapBlock({
type: "line-spacing",
data: { size },
})

/**
* Rendering components that provide the actual HTML to use inside the editor.
*/
const LineSpacingNode = ({ children, attributes, node: { data } }) => (
<div style={{ lineHeight: `${data.get("size")}` }} {...attributes}>
{children}
</div>
)

/**
* Dropdown component that connects to the editor.
*/
const LineSpacingButton = ({ value, onChange }) => {
const [anchorEl, setAnchorEl] = React.useState(null)

const handleMenuOpen = event => {
setAnchorEl(event.currentTarget)
}
const handleItemClick = size => {
setAnchorEl(null)
onChange(lineSpacingNodeStrategy(value, size))
}

const renderMenu = (
<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={() => setAnchorEl(null)}>
<MenuItem onClick={() => handleItemClick("1.0")}>1.0</MenuItem>
<MenuItem onClick={() => handleItemClick("1.1")}>1.1</MenuItem>
<MenuItem onClick={() => handleItemClick("1.2")}>1.2</MenuItem>
<MenuItem onClick={() => handleItemClick("1.3")}>1.3</MenuItem>
<MenuItem onClick={() => handleItemClick("1.4")}>1.4</MenuItem>
<MenuItem onClick={() => handleItemClick("1.5")}>1.5</MenuItem>
<MenuItem onClick={() => handleItemClick("1.6")}>1.6 (default)</MenuItem>
<MenuItem onClick={() => handleItemClick("2.0")}>2.0</MenuItem>
<MenuItem onClick={() => handleItemClick("2.5")}>2.5</MenuItem>
<MenuItem onClick={() => handleItemClick("3.0")}>3.0</MenuItem>
</Menu>
)

return (
<>
<Tooltip title="Line Spacing" placement="bottom">
<ToolbarButton
onClick={handleMenuOpen}
aria-owns={anchorEl ? "line-spacing-menu" : undefined}
aria-haspopup="true">
<LineSpacingIcon />
</ToolbarButton>
</Tooltip>
{renderMenu}
</>
)
}

/**
* Export everything needed for the editor.
*/
export { LineSpacingNode, LineSpacingButton }
2 changes: 2 additions & 0 deletions src/components/editor/toolbar/EditorToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FontFamilyDropdown } from "../plugins/fontfamily"
import { FontSizeDropdown } from "../plugins/fontsize"
import { H1Button, H2Button, H3Button } from "../plugins/heading"
import { ImageButton } from "../plugins/image"
import { LineSpacingButton } from "../plugins/linespacing"
import { LinkButton } from "../plugins/link"
import { OrderedListButton, UnorderedListButton } from "../plugins/list"
import {
Expand Down Expand Up @@ -120,6 +121,7 @@ export const EditorToolbar = props => {
<DividerButton {...props} />
<UnorderedListButton {...props} />
<OrderedListButton {...props} />
<LineSpacingButton {...props} />
<H1Button {...props} />
<H2Button {...props} />
<H3Button {...props} />
Expand Down

0 comments on commit c3d91ac

Please sign in to comment.