-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add superscript button to editor toolbar
fix #122
- Loading branch information
1 parent
14d39ec
commit e0ae45d
Showing
3 changed files
with
61 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 } |
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