forked from draft-js-plugins/draft-js-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrating counter-plugin (draft-js-plugins#1476)
* removing class components and migrating tests from chai/enzyme to jest/RTL * update mocha to support --exlude flag and rename jest files to .test Co-authored-by: Gabriel <[email protected]>
- Loading branch information
1 parent
dbe3b90
commit ad77f02
Showing
10 changed files
with
131 additions
and
205 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 |
---|---|---|
@@ -1,35 +1,34 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
import punycode from 'punycode'; | ||
|
||
class CharCounter extends Component { | ||
static propTypes = { | ||
theme: PropTypes.any, | ||
}; | ||
|
||
getCharCount(editorState) { | ||
const CharCounter = ({ theme = {}, className, store, limit }) => { | ||
const getCharCount = editorState => { | ||
const decodeUnicode = str => punycode.ucs2.decode(str); // func to handle unicode characters | ||
const plainText = editorState.getCurrentContent().getPlainText(''); | ||
const regex = /(?:\r\n|\r|\n)/g; // new line, carriage return, line feed | ||
const cleanString = plainText.replace(regex, '').trim(); // replace above characters w/ nothing | ||
return decodeUnicode(cleanString).length; | ||
} | ||
}; | ||
|
||
getClassNames(count, limit) { | ||
const { theme = {}, className } = this.props; | ||
const getClassNames = count => { | ||
const defaultStyle = clsx(theme.counter, className); | ||
const overLimitStyle = clsx(theme.counterOverLimit, className); | ||
return count > limit ? overLimitStyle : defaultStyle; | ||
} | ||
}; | ||
|
||
const count = getCharCount(store.getEditorState()); | ||
const classNames = getClassNames(count); | ||
|
||
render() { | ||
const { store, limit } = this.props; | ||
const count = this.getCharCount(store.getEditorState()); | ||
const classNames = this.getClassNames(count, limit); | ||
return <span className={classNames}>{count}</span>; | ||
}; | ||
|
||
return <span className={classNames}>{count}</span>; | ||
} | ||
} | ||
CharCounter.propTypes = { | ||
theme: PropTypes.any, | ||
className: PropTypes.any, | ||
store: PropTypes.any, | ||
limit: PropTypes.any, | ||
}; | ||
|
||
export default CharCounter; |
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,32 +1,36 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
|
||
class CustomCounter extends Component { | ||
static propTypes = { | ||
theme: PropTypes.any, | ||
limit: PropTypes.number, | ||
countFunction: PropTypes.func.isRequired, | ||
}; | ||
|
||
getClassNames(count, limit) { | ||
const { theme = {}, className } = this.props; | ||
const CustomCounter = ({ | ||
store, | ||
limit, | ||
countFunction, | ||
theme = {}, | ||
className, | ||
}) => { | ||
const getClassNames = count => { | ||
const defaultStyle = clsx(theme.counter, className); | ||
const overLimitStyle = clsx(theme.counterOverLimit, className); | ||
return count > limit ? overLimitStyle : defaultStyle; | ||
} | ||
}; | ||
|
||
const plainText = store | ||
.getEditorState() | ||
.getCurrentContent() | ||
.getPlainText(''); | ||
const count = countFunction(plainText); | ||
const classNames = getClassNames(count, limit); | ||
|
||
render() { | ||
const { store, limit, countFunction } = this.props; | ||
const plainText = store | ||
.getEditorState() | ||
.getCurrentContent() | ||
.getPlainText(''); | ||
const count = countFunction(plainText); | ||
const classNames = this.getClassNames(count, limit); | ||
return <span className={classNames}>{count}</span>; | ||
}; | ||
|
||
return <span className={classNames}>{count}</span>; | ||
} | ||
} | ||
CustomCounter.propTypes = { | ||
theme: PropTypes.any, | ||
store: PropTypes.any, | ||
className: PropTypes.any, | ||
limit: PropTypes.number, | ||
countFunction: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default CustomCounter; |
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,32 +1,30 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
|
||
class LineCounter extends Component { | ||
static propTypes = { | ||
theme: PropTypes.any, | ||
limit: PropTypes.number, | ||
}; | ||
|
||
getLineCount(editorState) { | ||
const LineCounter = ({ store, limit, theme = {}, className }) => { | ||
const getLineCount = editorState => { | ||
const blockArray = editorState.getCurrentContent().getBlocksAsArray(); | ||
return blockArray ? blockArray.length : null; | ||
} | ||
}; | ||
|
||
getClassNames(count, limit) { | ||
const { theme = {}, className } = this.props; | ||
const getClassNames = count => { | ||
const defaultStyle = clsx(theme.counter, className); | ||
const overLimitStyle = clsx(theme.counterOverLimit, className); | ||
return count > limit ? overLimitStyle : defaultStyle; | ||
} | ||
}; | ||
|
||
const count = getLineCount(store.getEditorState()); | ||
const classNames = getClassNames(count); | ||
|
||
render() { | ||
const { store, limit } = this.props; | ||
const count = this.getLineCount(store.getEditorState()); | ||
const classNames = this.getClassNames(count, limit); | ||
return <span className={classNames}>{count}</span>; | ||
}; | ||
|
||
return <span className={classNames}>{count}</span>; | ||
} | ||
} | ||
LineCounter.propTypes = { | ||
theme: PropTypes.any, | ||
store: PropTypes.any, | ||
className: PropTypes.any, | ||
limit: PropTypes.number, | ||
}; | ||
|
||
export default LineCounter; |
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,35 +1,31 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
|
||
class WordCounter extends Component { | ||
static propTypes = { | ||
theme: PropTypes.any, | ||
limit: PropTypes.number, | ||
}; | ||
|
||
getWordCount(editorState) { | ||
const WordCounter = ({ store, limit, theme = {}, className }) => { | ||
const getWordCount = editorState => { | ||
const plainText = editorState.getCurrentContent().getPlainText(''); | ||
const regex = /(?:\r\n|\r|\n)/g; // new line, carriage return, line feed | ||
const cleanString = plainText.replace(regex, ' ').trim(); // replace above characters w/ space | ||
const wordArray = cleanString.match(/\S+/g); // matches words according to whitespace | ||
return wordArray ? wordArray.length : 0; | ||
} | ||
}; | ||
|
||
getClassNames(count, limit) { | ||
const { theme = {}, className } = this.props; | ||
const getClassNames = count => { | ||
const defaultStyle = clsx(theme.counter, className); | ||
const overLimitStyle = clsx(theme.counterOverLimit, className); | ||
return count > limit ? overLimitStyle : defaultStyle; | ||
} | ||
}; | ||
|
||
const count = getWordCount(store.getEditorState()); | ||
const classNames = getClassNames(count); | ||
|
||
render() { | ||
const { store, limit } = this.props; | ||
const count = this.getWordCount(store.getEditorState()); | ||
const classNames = this.getClassNames(count, limit); | ||
return <span className={classNames}>{count}</span>; | ||
}; | ||
|
||
return <span className={classNames}>{count}</span>; | ||
} | ||
} | ||
WordCounter.propTypes = { | ||
theme: PropTypes.any, | ||
limit: PropTypes.number, | ||
}; | ||
|
||
export default WordCounter; |
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
Oops, something went wrong.