-
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 subscript button to editor toolbar
- Loading branch information
1 parent
8afebab
commit 14d39ec
Showing
3 changed files
with
60 additions
and
0 deletions.
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 subscriptMarkStrategy = change => change.toggleMark("subscript") | ||
|
||
/** | ||
* Rendering component that provides the actual HTML to use inside the editor. | ||
*/ | ||
const SubscriptMark = ({ children }) => <sub>{children}</sub> | ||
|
||
/** | ||
* Subscript button that uses a click handler to connect the button to the editor. | ||
*/ | ||
const SubscriptButton = ({ value, onChange }) => ( | ||
<Tooltip title="subscript (ctrl+=)" placement="bottom"> | ||
<ToolbarButton | ||
onClick={e => { | ||
onChange(subscriptMarkStrategy(value.change())) | ||
}}> | ||
<FontAwesome name="subscript" /> | ||
</ToolbarButton> | ||
</Tooltip> | ||
) | ||
|
||
/** | ||
* Function that specifies the keyboard shortcut to use for subscript. | ||
* It accepts event and change as arguments. | ||
*/ | ||
const SubscriptKeyboardShortcut = (event, change) => { | ||
if (isMod(event) && event.key === "=") { | ||
return subscriptMarkStrategy(change) | ||
} | ||
return | ||
} | ||
|
||
/** | ||
* Function that represents our actual plugin. | ||
* It takes options in case we want to add more to it in the future. | ||
*/ | ||
const SubscriptPlugin = options => ({ | ||
onKeyDown(...args) { | ||
return SubscriptKeyboardShortcut(...args) | ||
}, | ||
}) | ||
|
||
export { SubscriptPlugin, SubscriptMark, SubscriptButton } |
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