-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: use officially supported React context API (#87)
* Refactor: replace usage of legacy context API to officially supported React context API - Refactor context API usage - Update tests as shallow render does not simulate the actual usage of the new context API - Drop support of older React versions - package.json peerDependencies is updated * major bump as we only support react > 16.3
- Loading branch information
1 parent
626f973
commit ae62ee3
Showing
15 changed files
with
284 additions
and
158 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
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,4 @@ | ||
import React from 'react'; | ||
|
||
export const ReactMobileDocContext = React.createContext({}); | ||
ReactMobileDocContext.displayName = 'ReactMobileDocContext'; |
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 |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { ReactMobileDocContext } from "./Context"; | ||
|
||
class Editor extends React.Component { | ||
static contextTypes = { | ||
editor: PropTypes.object | ||
} | ||
|
||
componentDidMount() { | ||
const { editor } = this.context; | ||
const { editor } = this.props.context; | ||
if (editor) { | ||
editor.render(this.editorEl); | ||
} | ||
} | ||
|
||
render() { | ||
return <div {...this.props} ref={r => (this.editorEl = r)} />; | ||
// eslint-disable-next-line no-unused-vars | ||
const { context: _, ...props } = this.props; | ||
|
||
return <div {...props} ref={r => (this.editorEl = r)} />; | ||
} | ||
} | ||
|
||
export default Editor; | ||
const EditorOuter = React.forwardRef(function EditorOuter(props, ref) { | ||
return <ReactMobileDocContext.Consumer> | ||
{(context) => <Editor {...props} context={context} ref={ref} />} | ||
</ReactMobileDocContext.Consumer>; | ||
}); | ||
|
||
export default EditorOuter; |
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 |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { UI } from 'mobiledoc-kit'; | ||
import { ReactMobileDocContext } from "./Context"; | ||
|
||
const LinkButton = ({ children = "Link", type = "button", handler, className, activeClassName = 'active', ...props }, { editor, activeMarkupTags = []}) => { | ||
const onClick = () => { | ||
if (!editor.hasCursor()) { | ||
return; | ||
} | ||
const LinkButton = ({ children = "Link", type = "button", handler, className, activeClassName = 'active', ...props }) => { | ||
return <ReactMobileDocContext.Consumer> | ||
{({ editor, activeMarkupTags = []}) => { | ||
const onClick = () => { | ||
if (!editor.hasCursor()) { | ||
return; | ||
} | ||
|
||
if (editor.hasActiveMarkup('a')) { | ||
editor.toggleMarkup('a'); | ||
} else { | ||
UI.toggleLink(editor, handler); | ||
} | ||
}; | ||
if (editor.hasActiveMarkup('a')) { | ||
editor.toggleMarkup('a'); | ||
} else { | ||
UI.toggleLink(editor, handler); | ||
} | ||
}; | ||
|
||
className = [className, activeMarkupTags.indexOf('a') > -1 && activeClassName].filter(Boolean).join(' '); | ||
className = [className, activeMarkupTags.indexOf('a') > -1 && activeClassName].filter(Boolean).join(' '); | ||
|
||
props = { type, ...props, onClick, className }; | ||
return <button { ...props }>{children}</button>; | ||
props = { type, ...props, onClick, className }; | ||
return <button { ...props }>{children}</button>; | ||
}} | ||
</ReactMobileDocContext.Consumer>; | ||
}; | ||
|
||
LinkButton.propTypes = { | ||
children: PropTypes.node | ||
}; | ||
|
||
LinkButton.contextTypes = { | ||
editor: PropTypes.object, | ||
activeMarkupTags: PropTypes.array | ||
}; | ||
|
||
export default LinkButton; |
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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import titleCase from '../utils/titleCase'; | ||
import { ReactMobileDocContext } from "./Context"; | ||
|
||
const MarkupButton = ({ tag = '', type = 'button', children = titleCase(tag), className, activeClassName = 'active', ...props }, { editor, activeMarkupTags = []}) => { | ||
const onClick = () => editor.toggleMarkup(tag); | ||
className = [className, activeMarkupTags.indexOf(tag.toLowerCase()) > -1 && activeClassName].filter(Boolean).join(' '); | ||
props = { type, ...props, onClick, className }; | ||
return <button { ...props }>{children}</button>; | ||
const MarkupButton = ({ tag = '', type = 'button', children = titleCase(tag), className, activeClassName = 'active', ...props }) => { | ||
return <ReactMobileDocContext.Consumer> | ||
{({ editor, activeMarkupTags = []}) => { | ||
const onClick = () => editor.toggleMarkup(tag); | ||
className = [className, activeMarkupTags.indexOf(tag.toLowerCase()) > -1 && activeClassName].filter(Boolean).join(' '); | ||
props = { type, ...props, onClick, className }; | ||
return <button { ...props }>{children}</button>; | ||
}} | ||
</ReactMobileDocContext.Consumer>; | ||
}; | ||
|
||
MarkupButton.propTypes = { | ||
tag: PropTypes.string.isRequired, | ||
children: PropTypes.node | ||
}; | ||
|
||
MarkupButton.contextTypes = { | ||
editor: PropTypes.object, | ||
activeMarkupTags: PropTypes.array | ||
}; | ||
|
||
export default MarkupButton; |
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 |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { ReactMobileDocContext } from "./Context"; | ||
|
||
const SectionSelect = ({ tags = [], ...props }, { editor, activeSectionTags = []}) => { | ||
const activeTag = () => tags.find((t) => activeSectionTags.includes(t)); | ||
const SectionSelect = ({ tags = [], ...props }) => { | ||
return <ReactMobileDocContext.Consumer> | ||
{({ editor, activeSectionTags = []}) => { | ||
const activeTag = () => tags.find((t) => activeSectionTags.includes(t)); | ||
|
||
const onChange = (event) => { | ||
const tag = event.target.value || activeTag(); | ||
editor.toggleSection(tag); | ||
}; | ||
const onChange = (event) => { | ||
const tag = event.target.value || activeTag(); | ||
editor.toggleSection(tag); | ||
}; | ||
|
||
return ( | ||
<select value={activeTag() || ""} onChange={onChange} {...props}> | ||
<option value=""></option> | ||
{ tags.map((t) => <option value={t} key={t}>{t.toUpperCase()}</option>) } | ||
</select> | ||
); | ||
return ( | ||
<select value={activeTag() || ""} onChange={onChange} {...props}> | ||
<option value=""></option> | ||
{ tags.map((t) => <option value={t} key={t}>{t.toUpperCase()}</option>) } | ||
</select> | ||
); | ||
}} | ||
</ReactMobileDocContext.Consumer>; | ||
}; | ||
|
||
SectionSelect.propTypes = { | ||
tags: PropTypes.arrayOf(PropTypes.oneOf(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'aside'])).isRequired | ||
}; | ||
|
||
SectionSelect.contextTypes = { | ||
editor: PropTypes.object, | ||
activeSectionTags: PropTypes.array | ||
}; | ||
|
||
export default SectionSelect; |
Oops, something went wrong.