Skip to content

Commit

Permalink
Add Editor, handle and hashtag strategies and components
Browse files Browse the repository at this point in the history
  • Loading branch information
arisgk committed Sep 15, 2017
1 parent 1871d6d commit e0ec180
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 2 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"draft-js": "^0.10.1",
"es6-shim": "^0.35.3",
"history": "^4.7.2",
"material-ui": "^0.19.1",
"prop-types": "^15.5.10",
Expand Down
20 changes: 20 additions & 0 deletions src/components/Home/AutocompleteEditor/HandleSpan/index.jsx
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 src/components/Home/AutocompleteEditor/HashtagSpan/index.jsx
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;
60 changes: 60 additions & 0 deletions src/components/Home/AutocompleteEditor/index.jsx
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;
3 changes: 2 additions & 1 deletion src/components/Home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import AppBar from 'material-ui/AppBar';
import AutocompleteEditor from './AutocompleteEditor';

const styles = {
container: {
Expand All @@ -17,7 +18,7 @@ const Home = () => (
<div style={styles.container}>
<AppBar title="Autocomplete" />
<div style={styles.main}>
Hello world!
<AutocompleteEditor />
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Home/__snapshots__/Home.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports[`Home renders correctly 1`] = `
}
}
>
Hello world!
<AutocompleteEditor />
</div>
</div>
`;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global document */
/* eslint-disable react/jsx-filename-extension */
import 'es6-shim';
import React from 'react';
import ReactDOM from 'react-dom';
import createHistory from 'history/createBrowserHistory';
Expand Down
7 changes: 7 additions & 0 deletions src/utils/decoratorStrategies/handle/index.js
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);
}
7 changes: 7 additions & 0 deletions src/utils/decoratorStrategies/hashtag/index.js
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);
}
10 changes: 10 additions & 0 deletions src/utils/decoratorStrategies/index.js
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);
}
}

0 comments on commit e0ec180

Please sign in to comment.