Skip to content

Commit

Permalink
adding codemirror api events
Browse files Browse the repository at this point in the history
  • Loading branch information
scniro committed Jun 2, 2017
1 parent aaf610d commit 2e36c9b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 14 deletions.
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@
import CodeMirror from 'react-codemirror2'
<CodeMirror
value='foo'
options={{theme: 'material'}}
onChange={(value) => {
console.log(value);
}} />
defaultValue='react-codemirror2'
options={{theme: 'material', lineNumbers: true}}
editorWillMount={(cm) => {
}}
editorDidMount={(cm) => {
}}
editorDidConfigure={(cm) => {
}}
editorWillUnmount={(cm) => {
}}
onSetDefaultValue={(defaultValue) => {
}}
onChange={(internalValue) => {
}}
onCursorActivity={() => {
}}
onViewportChange={(cm, viewportStart, viewportEnd) => {
}}
onGutterClick={(cm, lineNumber, event) => {
}}
onFocus={() => {
}}
onBlur={() => {
}}
onScroll={() => {
}}
onUpdate={() => {
}}
/>
```

// TODO
// better docs coming soon. all props are optional...
31 changes: 28 additions & 3 deletions demo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,32 @@ require('./index.scss');

render(
<CodeMirror
value='foo'
options={{theme: 'material', viewportMargin: Infinity}}
onChange={(value) => console.log(value)}/>
defaultValue='react-codemirror2'
options={{theme: 'material', lineNumbers: true}}
editorWillMount={(cm) => {
}}
editorDidMount={(cm) => {
}}
editorDidConfigure={(cm) => {
}}
editorWillUnmount={(cm) => {
}}
onSetDefaultValue={(defaultValue) => {
}}
onChange={(internalValue) => {
}}
onCursorActivity={() => {
}}
onViewportChange={(cm, viewportStart, viewportEnd) => {
}}
onGutterClick={(cm, lineNumber, event) => {
}}
onFocus={() => {
}}
onBlur={() => {
}}
onScroll={() => {
}}
onUpdate={() => {
}}/>
, document.getElementById('app'));
77 changes: 75 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,96 @@ let codemirror = require('codemirror');

export default class CodeMirror extends React.Component {

constructor(props) {
super(props);

this.initHydration = false;
}

componentWillMount() {
if (this.props.editorWillMount) {
this.props.editorWillMount(codemirror);
}
}

componentDidMount() {

this.editor = codemirror(this.ref);
this.editor.on('change', () => this.props.onChange(this.editor.getValue()));

this.editor.on('change', () => {
if (this.props.onChange && this.initHydration) {
this.props.onChange(this.editor.getValue());
}
});

if (this.props.onCursorActivity) {
this.editor.on('cursorActivity', this.props.onCursorActivity);
}

if (this.props.onViewportChange) {
this.editor.on('viewportChange', (cm, start, end) => {
this.props.onViewportChange(cm, start, end);
});
}

if (this.props.onGutterClick) {
this.editor.on('gutterClick', (cm, lineNumber, event) => {
this.props.onGutterClick(cm, lineNumber, event);
});
}

if (this.props.onFocus) {
this.editor.on('focus', this.props.onFocus);
}

if (this.props.onBlur) {
this.editor.on('blur', this.props.onBlur);
}

if (this.props.onScroll) {
this.editor.on('scroll', this.props.onScroll);
}

if (this.props.onUpdate) {
this.editor.on('update', this.props.onUpdate);
}

this.hydrate(this.props);

if (this.props.editorDidMount) {
this.props.editorDidMount(codemirror);
}
}

componentWillReceiveProps(nextProps) {

this.hydrate(nextProps);
}

componentWillUnmount() {

if (this.props.editorWillUnmount) {
this.props.editorWillUnmount(codemirror);
}
}

hydrate(props) {

Object.keys(props.options || {}).forEach(key => this.editor.setOption(key, props.options[key]));
this.editor.setValue(props.value || '');

if (this.props.editorDidConfigure) {
this.props.editorDidConfigure(codemirror);
}

if (this.props.defaultValue && !this.initHydration) {
this.editor.setValue(props.defaultValue);

if (this.props.onSetDefaultValue) {
this.props.onSetDefaultValue(this.editor.getValue());
}
}

this.initHydration = true;
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-codemirror2",
"version": "0.0.2",
"version": "0.0.3",
"description": "a tiny react codemirror component wrapper",
"main": "index.js",
"scripts": {
Expand Down
77 changes: 75 additions & 2 deletions src/react-codemirror2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,96 @@ let codemirror = require('codemirror');

export default class CodeMirror extends React.Component {

constructor(props) {
super(props);

this.initHydration = false;
}

componentWillMount() {
if (this.props.editorWillMount) {
this.props.editorWillMount(codemirror);
}
}

componentDidMount() {

this.editor = codemirror(this.ref);
this.editor.on('change', () => this.props.onChange(this.editor.getValue()));

this.editor.on('change', () => {
if (this.props.onChange && this.initHydration) {
this.props.onChange(this.editor.getValue());
}
});

if(this.props.onCursorActivity) {
this.editor.on('cursorActivity', this.props.onCursorActivity);
}

if(this.props.onViewportChange) {
this.editor.on('viewportChange', (cm, start, end) => {
this.props.onViewportChange(cm, start, end);
});
}

if(this.props.onGutterClick) {
this.editor.on('gutterClick', (cm, lineNumber, event) => {
this.props.onGutterClick(cm, lineNumber, event);
});
}

if(this.props.onFocus) {
this.editor.on('focus', this.props.onFocus);
}

if(this.props.onBlur) {
this.editor.on('blur', this.props.onBlur);
}

if(this.props.onScroll) {
this.editor.on('scroll', this.props.onScroll);
}

if(this.props.onUpdate) {
this.editor.on('update', this.props.onUpdate);
}

this.hydrate(this.props);

if (this.props.editorDidMount) {
this.props.editorDidMount(codemirror);
}
}

componentWillReceiveProps(nextProps) {

this.hydrate(nextProps);
}

componentWillUnmount() {

if (this.props.editorWillUnmount) {
this.props.editorWillUnmount(codemirror);
}
}

hydrate(props) {

Object.keys(props.options || {}).forEach(key => this.editor.setOption(key, props.options[key]));
this.editor.setValue(props.value || '');

if (this.props.editorDidConfigure) {
this.props.editorDidConfigure(codemirror);
}

if (this.props.defaultValue && !this.initHydration) {
this.editor.setValue(props.defaultValue);

if (this.props.onSetDefaultValue) {
this.props.onSetDefaultValue(this.editor.getValue());
}
}

this.initHydration = true;
}

render() {
Expand Down

0 comments on commit 2e36c9b

Please sign in to comment.