65
Creating a WYSIWYG editor with React @ipeychev (or lets see how deep the rabbit hole goes)

Creating a WYSIWYG Editor with React

  • Upload
    peychevi

  • View
    1.273

  • Download
    10

Embed Size (px)

Citation preview

Page 1: Creating a WYSIWYG Editor with React

Creating a WYSIWYG editor with React

@ipeychev

(or lets see how deep the rabbit hole goes)

Page 2: Creating a WYSIWYG Editor with React

ReactA JavaScript library for building user interfaces

Page 3: Creating a WYSIWYG Editor with React

Why React?

Page 4: Creating a WYSIWYG Editor with React

Why React?

React allows you to concentrate on the UI of your application

Page 5: Creating a WYSIWYG Editor with React

Why React?

Instead to focus on implementation details, fighting with the DOM and resolving performance issues

Page 6: Creating a WYSIWYG Editor with React

React in a nutshell

Page 7: Creating a WYSIWYG Editor with React

React in a nutshell

Library, not a framework

Implements one-way reactive data

flow

Blazingly fast

Page 8: Creating a WYSIWYG Editor with React

React in a nutshell

Virtual DOM

JSX

JavaScript syntax extension (JSX)

Page 9: Creating a WYSIWYG Editor with React

React in a nutshell

Native applications

Isomorphic applications

Client-side applications

Page 10: Creating a WYSIWYG Editor with React

Creating UI with React

Page 11: Creating a WYSIWYG Editor with React

Main UI

Nested Components

Data flow

Page 12: Creating a WYSIWYG Editor with React

Creating a component

Render the component

Update state

Page 13: Creating a WYSIWYG Editor with React

But... Isn't that slow?!

Performance

Page 14: Creating a WYSIWYG Editor with React

And rendering is fast

Virtual DOM rulez

Performance

Page 15: Creating a WYSIWYG Editor with React

Components

Page 16: Creating a WYSIWYG Editor with React

Component are state machines

You render initially the components based on the properties

Components

Then you change their state and they will be automatically re-rendered

Page 17: Creating a WYSIWYG Editor with React

React renders nested components with deep hierarchy

Without compromising the performance

Components performance

(thanks to the Virtual DOM)

Page 18: Creating a WYSIWYG Editor with React

Components example

} Main component with nested components

Render part}

Page 19: Creating a WYSIWYG Editor with React

Reusable code

Page 20: Creating a WYSIWYG Editor with React

1 var HelloWorld = React.createClass({ 2 mixins: [MyMixin, YourMixin], 3 4 render: function() { 5 var a = this.getA(); 6 var b = this.getB(); 7 8 return (a + b); 9 }

Mixins

Page 21: Creating a WYSIWYG Editor with React
Page 22: Creating a WYSIWYG Editor with React

A higher-order component is a function that takes an existing component and returns another component that wraps it

Higher-order components

Page 23: Creating a WYSIWYG Editor with React

Properties

Page 24: Creating a WYSIWYG Editor with React

Unconditionally configure your components

Which will help you to debug and test them

Properties are immutable, they are owned by the parent element

Properties

Page 25: Creating a WYSIWYG Editor with React

Properties

Properties

Page 26: Creating a WYSIWYG Editor with React

State

Page 27: Creating a WYSIWYG Editor with React

State

Change your components based on user actions or data from server

When the state is updated, the component re-renders itself.

State should be considered as private data

Page 28: Creating a WYSIWYG Editor with React

Properties vs State

Page 29: Creating a WYSIWYG Editor with React

Properties are initialized when components are created

State is only seen on the inside of components definitions

Properties vs State

Page 30: Creating a WYSIWYG Editor with React

Events

Page 31: Creating a WYSIWYG Editor with React

Attaching events

<button onClick={this._handleClick} ...

Attach them in DOM 0 way:

Page 32: Creating a WYSIWYG Editor with React

Events are not attached to the element itself

React is listening for all events at the top level using a single event listener

When an event occurs, React dispatches it accordingly

Events delegation

Page 33: Creating a WYSIWYG Editor with React

React autobinds the method to its component instance

Events autobinding

There is no need to write .bind(this):

<button onClick={this._handleClick .bind(this)}

Page 34: Creating a WYSIWYG Editor with React

Let's see how deep the rabbit hole goes

Page 35: Creating a WYSIWYG Editor with React

AlloyEditor

http://alloyeditor.com

Page 36: Creating a WYSIWYG Editor with React

An Open Source

WYSIWYG editor

built with React

Page 37: Creating a WYSIWYG Editor with React
Page 38: Creating a WYSIWYG Editor with React

AlloyEditor design goals

Page 39: Creating a WYSIWYG Editor with React

The developer should be able to replace the UI entirely

!It should be accessible"

Toolbars should appear when needed and where needed

#

The UI should be separated from the core$

The UI should be easy to be styled%

AlloyEditor design goals

It should work on all browsers

Page 40: Creating a WYSIWYG Editor with React

AlloyEditor architecture

Page 41: Creating a WYSIWYG Editor with React

UI core Plugins, low level modules

Engine CKEditor Core

Toolbar Toolbar Toolbar

Button Button } AlloyEditor UI

based on React + our own code around it

AlloyEditor architecture

Page 42: Creating a WYSIWYG Editor with React

Code around React

React provides the rendering part only

That is not enough

Page 43: Creating a WYSIWYG Editor with React

Core, Attributes and Events

Basic stuff is needed, for example:

OOP

Types validation

Configurations

Custom Events

Page 44: Creating a WYSIWYG Editor with React

Instantiating AlloyEditor

Page 45: Creating a WYSIWYG Editor with React

Instantiating AlloyEditor

Many editors can be instantiated on one page

1 <script> 2 var editor1 = AlloyEditor.editable('description'); 3 var editor2 = AlloyEditor.editable('editable'); 4 </script>

Page 46: Creating a WYSIWYG Editor with React

Selections

Page 47: Creating a WYSIWYG Editor with React

Selections

Currently there are four types:

Image Text Table Link

Page 48: Creating a WYSIWYG Editor with React

Selections

Exposed in AlloyEditor.Selections

You can add your own

Page 49: Creating a WYSIWYG Editor with React

Buttons reordering

Page 50: Creating a WYSIWYG Editor with React

Buttons reordering

1 <script> 2 AlloyEditor.Selections[2].buttons = ['bold', 'italic', 'underline', 'link', 'twitter']; 3 </script>

AlloyEditor.Selections[2] is the text selection.

Instead of hardcoding it, you can also retrieve it by enumerating it inside the array

Page 51: Creating a WYSIWYG Editor with React

Buttons reordering

1 <script> 2 AlloyEditor.Selections[2].buttons = ['italic', 'bold', 'underline', 'link', 'twitter']; 3 </script>

AlloyEditor.Selections[2] is the text selection.

Instead of hardcoding it, you can also retrieve it by enumerating it inside the array

Page 52: Creating a WYSIWYG Editor with React

Buttons reordering

1 <script> 2 _.find(AlloyEditor.Selections, function(selection) { 3 var found = selection.name === 'text'; 4 5 if (found) { 6 selection.buttons = ['bold', 'italic', 'underline', 'link', 'twitter']; 7 } 8 9 return found; 10 }); 11 </script>

Page 53: Creating a WYSIWYG Editor with React

Buttons reordering

1 <script> 2 _.find(AlloyEditor.Selections, function(selection) { 3 var found = selection.name === 'text'; 4 5 if (found) { 6 selection.buttons = ['italic', 'bold', 'underline', 'link', 'twitter']; 7 } 8 9 return found; 10 }); 11 </script>

Page 54: Creating a WYSIWYG Editor with React

Adding new buttons

Page 55: Creating a WYSIWYG Editor with React

A button is just a ReactJS module

Page 56: Creating a WYSIWYG Editor with React

1 var ButtonH4 = React.createClass({ 2 mixins: [AlloyEditor.ButtonStyle, AlloyEditor.ButtonStateClasses, AlloyEditor.ButtonActionStyle], 3 4 statics: { 5 key: 'h4' 6 }, 7 8 getDefaultProps: function() { 9 return { 10 style: { 11 element: 'h4' 12 } 13 }; 14 }, 15 16 render: function() { 17 var cssClass = 'alloy-editor-button ' + this.getStateClasses(); 18 19 return ( 20 <button className={cssClass} data-type="button-h4" onClick={this.applyStyle}tabIndex={this.props.tabIndex}> 21 <span className="alloy-editor-icon-h4"></span> 22 </button> 23 ); 24 } 25 }); 26 27 AlloyEditor.Buttons[ButtonH4.key] = AlloyEditor.ButtonH4 = ButtonH4;

Page 57: Creating a WYSIWYG Editor with React

Adding a new button

1 <script> 2 _.find(AlloyEditor.Selections, function(selection) { 3 var found = selection.name === 'text'; 4 5 if (found) { 6 selection.buttons = ['h4', 'italic', 'bold', 'underline', 'link']; 7 } 8 9 return found; 10 }); 11 </script>

Page 58: Creating a WYSIWYG Editor with React

Skins!

Page 59: Creating a WYSIWYG Editor with React
Page 60: Creating a WYSIWYG Editor with React

Wait, there is even more!

Page 61: Creating a WYSIWYG Editor with React

More stuff available!

Drag&Drop images from Desktop to the editor&

Auto link creation&

Placeholder plugin&

Your own toolbars and buttons!&

Page 62: Creating a WYSIWYG Editor with React

Roadmap

Page 63: Creating a WYSIWYG Editor with React

Roadmap

Mobile support♥

Implement more buttons♥

Improve accessibility♥

Any ideas?

Page 64: Creating a WYSIWYG Editor with React

Demo time

Page 65: Creating a WYSIWYG Editor with React

Thanks!

Questions?

ipeychev