-
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.
Add Editor, handle and hashtag strategies and components
- Loading branch information
Showing
11 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
20 changes: 20 additions & 0 deletions
20
src/components/Home/AutocompleteEditor/HandleSpan/index.jsx
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,20 @@ | ||
import React from 'react'; | ||
|
||
const styles = { | ||
container: { | ||
color: '#03A9F4', | ||
direction: 'ltr', | ||
unicodeBidi: 'bidi-override', | ||
}, | ||
}; | ||
|
||
const HandleSpan = ({ offsetKey, children }) => ( | ||
<span | ||
style={styles.container} | ||
data-offset-key={offsetKey} | ||
> | ||
{children} | ||
</span> | ||
); | ||
|
||
export default HandleSpan; |
18 changes: 18 additions & 0 deletions
18
src/components/Home/AutocompleteEditor/HashtagSpan/index.jsx
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,18 @@ | ||
import React from 'react'; | ||
|
||
const styles = { | ||
container: { | ||
color: '#E91E63', | ||
}, | ||
}; | ||
|
||
const HashtagSpan = ({ offsetKey, children }) => ( | ||
<span | ||
style={styles.container} | ||
data-offset-key={offsetKey} | ||
> | ||
{children} | ||
</span> | ||
); | ||
|
||
export default HashtagSpan; |
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,60 @@ | ||
import React, { Component } from 'react'; | ||
import { CompositeDecorator, Editor, EditorState } from 'draft-js'; | ||
import HandleSpan from './HandleSpan'; | ||
import HashtagSpan from './HashtagSpan'; | ||
import handleStrategy from '../../../utils/decoratorStrategies/handle'; | ||
import hashtagStrategy from '../../../utils/decoratorStrategies/hashtag'; | ||
|
||
const styles = { | ||
root: { | ||
padding: 20, | ||
width: 600, | ||
}, | ||
editor: { | ||
border: '1px solid #ddd', | ||
cursor: 'text', | ||
fontSize: 16, | ||
minHeight: 40, | ||
padding: 10, | ||
}, | ||
}; | ||
|
||
class AutocompleteEditor extends Component { | ||
constructor() { | ||
super(); | ||
const compositeDecorator = new CompositeDecorator([ | ||
{ | ||
strategy: handleStrategy, | ||
component: HandleSpan, | ||
}, | ||
{ | ||
strategy: hashtagStrategy, | ||
component: HashtagSpan, | ||
}, | ||
]); | ||
|
||
this.state = { | ||
editorState: EditorState.createEmpty(compositeDecorator), | ||
}; | ||
|
||
this.focus = () => this.refs.editor.focus(); | ||
this.onChange = editorState => this.setState({ editorState }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div style={styles.root}> | ||
<div style={styles.editor} onClick={this.focus}> | ||
<Editor | ||
editorState={this.state.editorState} | ||
onChange={this.onChange} | ||
ref="editor" | ||
spellCheck | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default AutocompleteEditor; |
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ exports[`Home renders correctly 1`] = ` | |
} | ||
} | ||
> | ||
Hello world! | ||
<AutocompleteEditor /> | ||
</div> | ||
</div> | ||
`; |
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,7 @@ | ||
import { findWithRegex } from '../'; | ||
|
||
const regex = /\@[\w]+/g; | ||
|
||
export default function strategy(contentBlock, callback) { | ||
findWithRegex(regex, contentBlock, callback); | ||
} |
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,7 @@ | ||
import { findWithRegex } from '../'; | ||
|
||
const regex = /\#[\w\u0590-\u05ff]+/g; | ||
|
||
export default function strategy(contentBlock, callback) { | ||
findWithRegex(regex, contentBlock, callback); | ||
} |
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,10 @@ | ||
export function findWithRegex(regex, contentBlock, callback) { | ||
const text = contentBlock.getText(); | ||
let matchArr; | ||
let start; | ||
|
||
while ((matchArr = regex.exec(text)) !== null) { | ||
start = matchArr.index; | ||
callback(start, start + matchArr[0].length); | ||
} | ||
} |