Skip to content

Commit

Permalink
feat: add superscript button to editor toolbar
Browse files Browse the repository at this point in the history
fix #122
  • Loading branch information
erichartline committed May 7, 2019
1 parent 14d39ec commit e0ae45d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/editor/PageEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { FontColorMark } from "./plugins/fontcolor"
import { FontFamilyMark } from "./plugins/fontfamily"
import { FontSizeMark } from "./plugins/fontsize"
import { ItalicMark } from "./plugins/italic"
import { SubscriptMark } from "./plugins/subscript"
import { StrikethroughMark } from "./plugins/strikethrough"
import { SubscriptMark } from "./plugins/subscript"
import { SuperscriptMark } from "./plugins/superscript"
import { UnderlineMark } from "./plugins/underline"

/** Import node renderers */
Expand Down Expand Up @@ -54,6 +55,7 @@ import { LinkPlugin } from "./plugins/link"
import { ListPlugin } from "./plugins/list"
import { StrikethroughPlugin } from "./plugins/strikethrough"
import { SubscriptPlugin } from "./plugins/subscript"
import { SuperscriptPlugin } from "./plugins/superscript"
import { TablePlugin } from "./plugins/table"
import { UnderlinePlugin } from "./plugins/underline"

Expand Down Expand Up @@ -86,6 +88,7 @@ const plugins = [
ListPlugin,
StrikethroughPlugin(),
SubscriptPlugin(),
SuperscriptPlugin(),
TablePlugin,
UnderlinePlugin(),
]
Expand Down Expand Up @@ -116,6 +119,8 @@ export const renderMark = (props: markProps) => {
return <StrikethroughMark {...props} />
case "subscript":
return <SubscriptMark {...props} />
case "superscript":
return <SuperscriptMark {...props} />
case "underline":
return <UnderlineMark {...props} />

Expand Down
53 changes: 53 additions & 0 deletions src/components/editor/plugins/superscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react"
import Tooltip from "@material-ui/core/Tooltip"
import FontAwesome from "react-fontawesome"

import ToolbarButton from "../toolbar/ToolbarButton"
import { isMod } from "../utils/utils"

/**
* Function that toggles the mark type.
*/
const superscriptMarkStrategy = change => change.toggleMark("superscript")

/**
* Rendering component that provides the actual HTML to use inside the editor.
*/
const SuperscriptMark = ({ children }) => <sup>{children}</sup>

/**
* Superscript button that uses a click handler to connect the button to the editor.
*/
const SuperscriptButton = ({ value, onChange }) => (
<Tooltip title="superscript (ctrl+/)" placement="bottom">
<ToolbarButton
onClick={e => {
onChange(superscriptMarkStrategy(value.change()))
}}>
<FontAwesome name="superscript" />
</ToolbarButton>
</Tooltip>
)

/**
* Function that specifies the keyboard shortcut to use for superscript.
* It accepts event and change as arguments.
*/
const SuperscriptKeyboardShortcut = (event, change) => {
if (isMod(event) && event.key === "/") {
return superscriptMarkStrategy(change)
}
return
}

/**
* Function that represents our actual plugin.
* It takes options in case we want to add more to it in the future.
*/
const SuperscriptPlugin = options => ({
onKeyDown(...args) {
return SuperscriptKeyboardShortcut(...args)
},
})

export { SuperscriptPlugin, SuperscriptMark, SuperscriptButton }
2 changes: 2 additions & 0 deletions src/components/editor/toolbar/EditorToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BoldButton } from "../plugins/bold"
import { ItalicButton } from "../plugins/italic"
import { StrikethroughButton } from "../plugins/strikethrough"
import { SubscriptButton } from "../plugins/subscript"
import { SuperscriptButton } from "../plugins/superscript"
import { UnderlineButton } from "../plugins/underline"

import {
Expand Down Expand Up @@ -104,6 +105,7 @@ export const EditorToolbar = props => {
<UnderlineButton {...props} />
<StrikethroughButton {...props} />
<SubscriptButton {...props} />
<SuperscriptButton {...props} />
<AlignmentLeftButton {...props} />
<AlignmentCenterButton {...props} />
<AlignmentRightButton {...props} />
Expand Down

0 comments on commit e0ae45d

Please sign in to comment.