Skip to content

Commit

Permalink
Merge pull request #209 from securingsincity/v5
Browse files Browse the repository at this point in the history
V5
  • Loading branch information
securingsincity authored May 28, 2017
2 parents 4db1900 + efac45d commit afe2b53
Show file tree
Hide file tree
Showing 17 changed files with 1,520 additions and 142 deletions.
101 changes: 11 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
[![CDNJS](https://img.shields.io/cdnjs/v/react-ace.svg)](https://cdnjs.com/libraries/react-ace)
[![Coverage Status](https://coveralls.io/repos/github/securingsincity/react-ace/badge.svg?branch=master)](https://coveralls.io/github/securingsincity/react-ace?branch=master)

A react component for Ace / Brace
A set of react components for Ace / Brace

[DEMO](http://securingsincity.github.io/react-ace/)
[DEMO of React Ace](http://securingsincity.github.io/react-ace/)
[DEMO of React Ace Split Editor](http://securingsincity.github.io/react-ace/split.html)

## Install

`npm install react-ace`

## Usage
## Basic Usage

```javascript
import React from 'react';
Expand Down Expand Up @@ -45,94 +46,14 @@ render(

## Examples

* Checkout `example` directory for a working example using webpack.
* [create-react-app](https://github.com/securingsincity/react-ace-create-react-app-example)
* [preact](https://github.com/securingsincity/react-ace-preact-example)
* [webpack](https://github.com/securingsincity/react-ace-webpack-example)


## Available Props

|Prop|Default|Description|
|-----|------|----------|
|name| 'brace-editor'| Unique Id to be used for the editor|
|mode| ''| Language for parsing and code highlighting|
|theme| ''| theme to use|
|height| '500px'| CSS value for height|
|width| '500px'| CSS value for width|
|className| | custom className|
|fontSize| 12| pixel value for font-size|
|showGutter| true| boolean|
|showPrintMargin| true| boolean|
|highlightActiveLine| true| boolean|
|focus| false| boolean|
|cursorStart| 1| number|
|wrapEnabled| false| Wrapping lines|
|readOnly| false| boolean|
|minLines| | Minimum number of lines to be displayed|
|maxLines| | Maximum number of lines to be displayed|
|enableBasicAutocompletion| false| Enable basic autocompletion|
|enableLiveAutocompletion| false| Enable live autocompletion|
|tabSize| 4| tabSize number|
|value | ''| String value you want to populate in the code highlighter|
|defaultValue | ''| Default value of the editor|
|onLoad| | Function onLoad|
|onBeforeLoad| | function that trigger before editor setup|
|onChange| | function that occurs on document change it has 2 arguments the value and the event. see the example above|
|onCopy| | function that trigger by editor `copy` event, and pass text as argument|
|onPaste| | function that trigger by editor `paste` event, and pass text as argument|
|onSelectionChange| | function that trigger by editor `selectionChange` event, and passes a [Selection](https://ace.c9.io/#nav=api&api=selection) as it's first argument and the event as the second|
|onFocus| | function that trigger by editor `focus` event|
|onBlur| | function that trigger by editor `blur` event|
|onScroll| | function that trigger by editor `scroll` event|
|editorProps| | Object of properties to apply directly to the Ace editor instance|
|setOptions| | Object of [options](https://github.com/ajaxorg/ace/wiki/Configuring-Ace) to apply directly to the Ace editor instance|
|keyboardHandler| | String corresponding to the keybinding mode to set (such as vim)|
|commands| | Array of new commands to add to the editor
|annotations| | Array of annotations to show in the editor i.e. `[{ row: 0, column: 2, type: 'error', text: 'Some error.'}]`, displayed in the gutter|
|markers| | Array of [markers](https://ace.c9.io/api/edit_session.html#EditSession.addMarker) to show in the editor, i.e. `[{ startRow: 0, startCol: 2, endRow: 1, endCol: 20, className: 'error-marker', type: 'background' }]`|
|style| | Object with camelCased properties |

## Modes, Themes, and Keyboard Handlers

All modes, themes, and keyboard handlers should be required through ```brace``` directly. Browserify will grab these modes / themes / keyboard handlers through ```brace``` and will be available at run time. See the example above. This prevents bloating the compiled javascript with extra modes and themes for your application.

### Example Modes

* javascript
* java
* python
* xml
* ruby
* sass
* markdown
* mysql
* json
* html
* handlebars
* golang
* csharp
* coffee
* css

### Example Themes

* monokai
* github
* tomorrow
* kuroir
* twilight
* xcode
* textmate
* solarized dark
* solarized light
* terminal

### Example Keyboard Handlers

* vim
* emacs
Checkout the `example` directory for a working example using webpack.

## Documentation

[Ace Editor](https://github.com/securingsincity/react-ace/blob/master/docs/Ace.md)
[Split View Editor](https://github.com/securingsincity/react-ace/blob/master/docs/Split.md)
[How to add modes, themes and keyboard handlers](https://github.com/securingsincity/react-ace/blob/master/docs/Modes.md)
[Frequently Asked Questions](https://github.com/securingsincity/react-ace/blob/master/docs/FAQ.md)

## Backers

Expand Down
45 changes: 45 additions & 0 deletions docs/Ace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Ace Editor

This is the main component of React-Ace. It creates an instance of the Ace Editor.

## Available Props

|Prop|Default|Type|Description|
|-----|------|-----|-----|
|name| 'brace-editor'| String |Unique Id to be used for the editor|
|mode| ''| String |Language for parsing and code highlighting|
|theme| ''| String |theme to use|
|value | ''| String | value you want to populate in the code highlighter|
|defaultValue | ''| String |Default value of the editor|
|height| '500px'| String |CSS value for height|
|width| '500px'| String |CSS value for width|
|className| | String |custom className|
|fontSize| 12| Number |pixel value for font-size|
|showGutter| true| Boolean | show gutter |
|showPrintMargin| true| Boolean| show print margin |
|highlightActiveLine| true| Boolean| highlight active line|
|focus| false| Boolean| whether to focus
|cursorStart| 1| Number| the location of the cursor
|wrapEnabled| false| Boolean | Wrapping lines|
|readOnly| false| Boolean| make the editor read only |
|minLines| | Number |Minimum number of lines to be displayed|
|maxLines| | Number |Maximum number of lines to be displayed|
|enableBasicAutocompletion| false| Boolean | Enable basic autocompletion|
|enableLiveAutocompletion| false| Boolean | Enable live autocompletion|
|tabSize| 4| Number| tabSize|
|onLoad| | Function | called on editor load. The first argument is the instance of the editor |
|onBeforeLoad| | Function | called before editor load. the first argument is an instance of `ace`|
|onChange| | Function | occurs on document change it has 2 arguments the value and the event.|
|onCopy| | Function | triggered by editor `copy` event, and passes text as argument|
|onPaste| | Function | Triggered by editor `paste` event, and passes text as argument|
|onSelectionChange| | Function | triggered by editor `selectionChange` event, and passes a [Selection](https://ace.c9.io/#nav=api&api=selection) as it's first argument and the event as the second|
|onFocus| | Function | triggered by editor `focus` event|
|onBlur| | Function | triggered by editor `blur` event|
|onScroll| | Function | triggered by editor `scroll` event|
|editorProps| | Object | properties to apply directly to the Ace editor instance|
|setOptions| | Object | [options](https://github.com/ajaxorg/ace/wiki/Configuring-Ace) to apply directly to the Ace editor instance|
|keyboardHandler| | String | corresponding to the keybinding mode to set (such as vim or emacs)|
|commands| | Array | new commands to add to the editor
|annotations| | Array | annotations to show in the editor i.e. `[{ row: 0, column: 2, type: 'error', text: 'Some error.'}]`, displayed in the gutter|
|markers| | Array | [markers](https://ace.c9.io/api/edit_session.html#EditSession.addMarker) to show in the editor, i.e. `[{ startRow: 0, startCol: 2, endRow: 1, endCol: 20, className: 'error-marker', type: 'background' }]`|
|style| | Object | camelCased properties |
159 changes: 159 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Frequently Asked Questions

## How do I use it with `preact`? `webpack`? `create-react-app`?

Check out the example applications

* [create-react-app](https://github.com/securingsincity/react-ace-create-react-app-example)
* [preact](https://github.com/securingsincity/react-ace-preact-example)
* [webpack](https://github.com/securingsincity/react-ace-webpack-example)


## How do call methods on the editor? How do I call Undo or Redo?

`ReactAce` has an editor property, which is the wrapped editor. You can use refs to get to the component, and then you should be able to use the editor on the component to run the function you need:

```javascript
const reactAceComponent = parent.refs.reactAceComponent;
const editor = reactAceComponent.editor
editor.find(searchRegex, {
backwards: false,
wrap: true,
caseSensitive: false,
wholeWord: false,
regExp: true,
});
```

Similarly, if you want to redo or undo, you can reference the editor from the refs

```jsx
<button onClick={()=> {this.refs.editor.editor.undo()}}>Undo</button>
<button onClick={()=> {this.refs.editor.editor.redo()}}>Redo</button>
```

## How do I set editor options like setting block scrolling to infinity?

```javascript
<ReactAce
editorProps={{
$blockScrolling: Infinity
}}
/>
```

## How do I add language snippets?

You can import the snippets and mode directly through `brace` along with the language_tools. Here is an example below


```javascript
import React from 'react';
import { render } from 'react-dom';
import brace from 'brace';
import AceEditor from 'react-ace';

import 'brace/mode/python';
import 'brace/snippets/python';
import 'brace/ext/language_tools';
import 'brace/theme/github';

function onChange(newValue) {
console.log('change',newValue);
}

// Render editor
render(
<AceEditor
mode="python"
theme="github"
onChange={onChange}
name="UNIQUE_ID_OF_DIV"
editorProps={{$blockScrolling: true}}
enableBasicAutocompletion={true}
enableLiveAutocompletion={true}
enableSnippets={true}
/>,
document.getElementById('example')
);
```

## How do I get selected text `onSelectionChange`?

How you extract the text from the editor is based on how to call methods on the editor.

Your `onSelectionChange` should look like this:

```javascript
onSelectionChange(selection) {
const content = this.refs.aceEditor.editor.session.getTextRange(selection.getRange());
// use content
}
```

## How do I add markers?
```javascript
const markers = [{
startRow: 3,
type: 'text',
className: 'test-marker'
}];
const wrapper = (<AceEditor markers={markers}/>);
```

## How do I add annotations?
```javascript
const annotations = [{
row: 3, // must be 0 based
column: 4, // must be 0 based
text: 'error.message', // text to show in tooltip
type: 'error'
}]
const editor = (
<AceEditor
annotations={annotations}
/>
)
```

## How do I add the search box?
Add the following line

`import 'brace/ext/searchbox';`

before introducing the component and it will add the search box.

## How do I add a custom mode?

1. Create my custom mode class (pure ES6 code)
2. Initialize the component with an existing mode name (such as "sql")
3. Use the `componentDidMount` function and call `session.setMode` with an instance of my custom mode.

My custom mode is:
```javascript
export default class CustomSqlMode extends ace.acequire('ace/mode/text').Mode {
constructor(){
super();
// Your code goes here
}
}
```

And my react-ace code looks like:
```javascript
render() {
return <div>
<AceEditor
ref="aceEditor"
mode="sql" // Default value since this props must be set.
theme="chrome" // Default value since this props must be set.
/>
</div>;
}

componentDidMount() {
const customMode = new CustomSqlMode();
this.refs.aceEditor.editor.getSession().setMode(customMode);
}
```

39 changes: 39 additions & 0 deletions docs/Modes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Modes, Themes, and Keyboard Handlers

All modes, themes, and keyboard handlers should be required through ```brace``` directly. Browserify will grab these modes / themes / keyboard handlers through ```brace``` and will be available at run time. See the example above. This prevents bloating the compiled javascript with extra modes and themes for your application.

### Example Modes

* javascript
* java
* python
* xml
* ruby
* sass
* markdown
* mysql
* json
* html
* handlebars
* golang
* csharp
* coffee
* css

### Example Themes

* monokai
* github
* tomorrow
* kuroir
* twilight
* xcode
* textmate
* solarized dark
* solarized light
* terminal

### Example Keyboard Handlers

* vim
* emacs
Loading

0 comments on commit afe2b53

Please sign in to comment.