client/src/components/SlateEditor.js
author Alexandre Segura <mex.zktk@gmail.com>
Tue, 06 Jun 2017 15:56:41 +0200
changeset 19 f1b125b95fe9
parent 17 877d8796b86d
child 21 284e866f55c7
permissions -rw-r--r--
Introduce "annotation" plugin. - Wrap mark around text. - Store selected text in mark data. - Colorize selected text.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
     1
import { Editor, Plain, Raw } from 'slate'
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
import React from 'react'
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
     3
import moment from 'moment';
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
     4
import HtmlSerializer from '../HtmlSerializer'
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
     5
import AnnotationPlugin from '../AnnotationPlugin';
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
     6
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
     7
const plugins = [];
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
 * Define the default node type.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
const DEFAULT_NODE = 'paragraph'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
 * Define a schema.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
 *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
 * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
const schema = {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
  nodes: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
    'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
    'list-item': props => <li {...props.attributes}>{props.children}</li>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
    'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>,
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
  },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
  marks: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
    bold: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
      fontWeight: 'bold'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
    },
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    31
    // TODO Check if we can move this to the plugin using the schema option
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    32
    // https://docs.slatejs.org/reference/plugins/plugin.html#schema
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    33
    annotation: {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    34
      textDecoration: 'underline',
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    35
      textDecorationStyle: 'dotted',
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    36
      backgroundColor: 'yellow',
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
    },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
    italic: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
      fontStyle: 'italic'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
    },
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    underlined: {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
      textDecoration: 'underline'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
 * The rich text example.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
 *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
 * @type {Component}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    53
class SlateEditor extends React.Component {
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
   * Deserialize the initial editor state.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
   * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
   */
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    60
  constructor(props) {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    61
    super(props);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    62
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    63
    const annotationPlugin = AnnotationPlugin({
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    64
      onChange: (text) => {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    65
        this.setState({ currentSelectionText: text });
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    66
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    67
    });
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    68
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    69
    plugins.push(annotationPlugin);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    70
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    71
    this.state = {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    72
      state: Plain.deserialize(''),
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    73
      startedAt: null,
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    74
      finishedAt: null,
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    75
      currentSelectionText: ''
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    76
    };
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    77
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    78
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    79
  componentDidMount() {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    80
    this.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    81
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    82
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    83
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    84
   * Check if the current selection has a mark with `type` in it.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    85
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    86
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    87
   * @return {Boolean}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    88
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    89
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    90
  hasMark = (type) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    91
    const { state } = this.state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    92
    return state.marks.some(mark => mark.type === type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    93
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    94
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    95
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    96
   * Check if the any of the currently selected blocks are of `type`.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    97
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    98
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    99
   * @return {Boolean}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   100
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   101
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   102
  hasBlock = (type) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   103
    const { state } = this.state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   104
    return state.blocks.some(node => node.type === type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   105
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   106
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   107
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   108
   * On change, save the new state.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   109
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   110
   * @param {State} state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   111
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   112
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   113
  onChange = (state) => {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   114
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   115
    let newState = {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   116
      state: state,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   117
      startedAt: this.state.startedAt
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   118
    };
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   119
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   120
    const isEmpty = state.document.length === 0;
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   121
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   122
    // Reset timers when the text is empty
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   123
    if (isEmpty) {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   124
      Object.assign(newState, {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   125
        startedAt: null,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   126
        finishedAt: null
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   127
      });
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   128
    } else {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   129
      Object.assign(newState, { finishedAt: moment().format('H:mm:ss') });
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   130
    }
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   131
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   132
    // Store start time once when the first character is typed
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   133
    if (!isEmpty && this.state.startedAt === null) {
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   134
      Object.assign(newState, { startedAt: moment().format('H:mm:ss') });
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   135
    }
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   136
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   137
    this.setState(newState)
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   138
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   139
    if (typeof this.props.onChange === 'function') {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
   140
      this.props.onChange(newState);
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   141
    }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   142
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   143
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   144
  asPlain = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   145
    return Plain.serialize(this.state.state);
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   146
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   147
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   148
  asRaw = () => {
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   149
    return Raw.serialize(this.state.state);
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   150
  }
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
   151
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   152
  asHtml = () => {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   153
    return HtmlSerializer.serialize(this.state.state);
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   154
  }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
   155
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   156
  clear = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   157
    const state = Plain.deserialize('');
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   158
    this.onChange(state);
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   159
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   160
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   161
  focus = () => {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   162
    this.refs.editor.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   163
  }
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   164
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   165
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   166
   * On key down, if it's a formatting command toggle a mark.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   167
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   168
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   169
   * @param {Object} data
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   170
   * @param {State} state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   171
   * @return {State}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   172
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   173
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   174
  onKeyDown = (e, data, state) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   175
    if (!data.isMod) return
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   176
    let mark
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   177
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   178
    switch (data.key) {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   179
      case 'b':
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   180
        mark = 'bold'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   181
        break
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   182
      case 'i':
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   183
        mark = 'italic'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   184
        break
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   185
      case 'u':
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   186
        mark = 'underlined'
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   187
        break
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   188
      default:
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   189
        return
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   190
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   191
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   192
    state = state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   193
      .transform()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   194
      .toggleMark(mark)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   195
      .apply()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   196
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   197
    e.preventDefault()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   198
    return state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   199
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   200
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   201
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   202
   * When a mark button is clicked, toggle the current mark.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   203
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   204
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   205
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   206
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   207
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   208
  onClickMark = (e, type) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   209
    e.preventDefault()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   210
    let { state } = this.state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   211
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   212
    let toggleMarkOptions;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   213
    if (type === 'annotation') {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   214
      toggleMarkOptions = { type: type, data: { text: this.state.currentSelectionText } }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   215
    } else {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   216
      toggleMarkOptions = type;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   217
    }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   218
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   219
    state = state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   220
      .transform()
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   221
      .toggleMark(toggleMarkOptions)
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   222
      .apply()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   223
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   224
    this.setState({ state })
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   225
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   226
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   227
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   228
   * When a block button is clicked, toggle the block type.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   229
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   230
   * @param {Event} e
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   231
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   232
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   233
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   234
  onClickBlock = (e, type) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   235
    e.preventDefault()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   236
    let { state } = this.state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   237
    const transform = state.transform()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   238
    const { document } = state
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   239
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   240
    // Handle everything but list buttons.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   241
    if (type !== 'bulleted-list' && type !== 'numbered-list') {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   242
      const isActive = this.hasBlock(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   243
      const isList = this.hasBlock('list-item')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   244
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   245
      if (isList) {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   246
        transform
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   247
          .setBlock(isActive ? DEFAULT_NODE : type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   248
          .unwrapBlock('bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   249
          .unwrapBlock('numbered-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   250
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   251
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   252
      else {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   253
        transform
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   254
          .setBlock(isActive ? DEFAULT_NODE : type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   255
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   256
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   257
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   258
    // Handle the extra wrapping required for list buttons.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   259
    else {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   260
      const isList = this.hasBlock('list-item')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   261
      const isType = state.blocks.some((block) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   262
        return !!document.getClosest(block.key, parent => parent.type === type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   263
      })
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   264
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   265
      if (isList && isType) {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   266
        transform
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   267
          .setBlock(DEFAULT_NODE)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   268
          .unwrapBlock('bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   269
          .unwrapBlock('numbered-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   270
      } else if (isList) {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   271
        transform
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   272
          .unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   273
          .wrapBlock(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   274
      } else {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   275
        transform
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   276
          .setBlock('list-item')
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   277
          .wrapBlock(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   278
      }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   279
    }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   280
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   281
    state = transform.apply()
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   282
    this.setState({ state })
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   283
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   284
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   285
  /**
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   286
   * Render.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   287
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   288
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   289
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   290
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   291
  render = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   292
    return (
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   293
      <div>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   294
        {this.renderToolbar()}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   295
        {this.renderEditor()}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   296
      </div>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   297
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   298
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   299
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   300
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   301
   * Render the toolbar.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   302
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   303
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   304
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   305
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   306
  renderToolbar = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   307
    return (
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   308
      <div className="menu toolbar-menu">
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   309
        {this.renderMarkButton('bold', 'format_bold')}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   310
        {this.renderMarkButton('italic', 'format_italic')}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   311
        {this.renderMarkButton('underlined', 'format_underlined')}
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   312
        {this.renderMarkButton('annotation', 'label')}
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   313
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   314
        {this.renderBlockButton('numbered-list', 'format_list_numbered')}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   315
        {this.renderBlockButton('bulleted-list', 'format_list_bulleted')}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   316
      </div>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   317
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   318
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   319
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   320
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   321
   * Render a mark-toggling toolbar button.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   322
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   323
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   324
   * @param {String} icon
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   325
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   326
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   327
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   328
  renderMarkButton = (type, icon) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   329
    const isActive = this.hasMark(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   330
    const onMouseDown = e => this.onClickMark(e, type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   331
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   332
    return (
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   333
      <span className="button" onMouseDown={onMouseDown} data-active={isActive}>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   334
        <span className="material-icons">{icon}</span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   335
      </span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   336
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   337
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   338
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   339
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   340
   * Render a block-toggling toolbar button.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   341
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   342
   * @param {String} type
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   343
   * @param {String} icon
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   344
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   345
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   346
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   347
  renderBlockButton = (type, icon) => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   348
    const isActive = this.hasBlock(type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   349
    const onMouseDown = e => this.onClickBlock(e, type)
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   350
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   351
    return (
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   352
      <span className="button" onMouseDown={onMouseDown} data-active={isActive}>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   353
        <span className="material-icons">{icon}</span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   354
      </span>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   355
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   356
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   357
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   358
  /**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   359
   * Render the Slate editor.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   360
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   361
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   362
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   363
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   364
  renderEditor = () => {
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   365
    return (
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   366
      <div className="editor">
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   367
        <Editor
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   368
          ref="editor"
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   369
          spellCheck
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   370
          placeholder={'Enter some rich text...'}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   371
          schema={schema}
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
   372
          plugins={plugins}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   373
          state={this.state.state}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   374
          onChange={this.onChange}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   375
          onKeyDown={this.onKeyDown}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   376
        />
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   377
      </div>
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   378
    )
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   379
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   380
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   381
}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   382
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   383
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   384
 * Export.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   385
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   386
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   387
export default SlateEditor