client/src/components/SlateEditor/index.js
author ymh <ymh.work@gmail.com>
Wed, 05 Dec 2018 23:52:25 +0100
changeset 194 d19ba6045e82
parent 191 3f71ad81a5a9
child 195 669b563563f5
permissions -rw-r--r--
Optimize code a little bit. Get rid of very expensive and useless clone operation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     1
import { Value } from 'slate';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     2
import Plain from 'slate-plain-serializer';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     3
import { Editor } from 'slate-react';
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     4
import React from 'react';
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
     5
import { withNamespaces } from 'react-i18next';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
     6
import { connect } from 'react-redux';
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     7
import HtmlSerializer from './HtmlSerializer';
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
     8
import { now } from '../../utils';
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
     9
import Toolbar from './Toolbar';
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    10
import { getAutoSubmit } from '../../selectors/authSelectors';
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    11
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    12
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
/**
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
 * @type {Component}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
 */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    18
class SlateEditor extends React.Component {
5
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
   * Deserialize the initial editor state.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
   * @type {Object}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
   */
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    25
  constructor(props) {
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    26
    super(props);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    27
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    28
    this.state = {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    29
      value: props.note ? Value.fromJSON(JSON.parse(props.note.raw)) : Plain.deserialize(''),
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    30
      startedAt: null,
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents: 17
diff changeset
    31
      finishedAt: null,
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    32
      enterKeyValue: 0,
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    33
    };
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    34
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    35
    this.editorRef = React.createRef();
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    36
  }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    37
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    38
  get editor() {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    39
    if(this.editorRef) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    40
      return this.editorRef.current;
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    41
    }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    42
    return null;
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    43
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    45
  componentDidMount = () => {
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    46
    this.focus();
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    47
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    49
   /**
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    50
   * On change, save the new state.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
   *
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    52
   * @param {Change} change
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    55
  onChange = ({value, operations}) => {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    56
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    57
    const newState = {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    58
      value
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    59
    };
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    60
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    61
    (operations || []).some((op) => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    62
      if(['insert_text', 'remove_text', 'add_mark', 'remove_mark', 'set_mark', 'insert_node', 'merge_node', 'move_node', 'remove_node', 'set_node', 'split_node'].indexOf(op.type)>=0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    63
        const tsnow = now();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    64
        if(this.state.startedAt == null) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    65
          newState.startedAt = tsnow;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    66
        }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    67
        newState.finishedAt = tsnow;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    68
        return true;
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    69
      }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    70
      return false;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    71
    });
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    72
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    73
    this.setState(newState, () => {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    74
      if (typeof this.props.onChange === 'function') {
194
d19ba6045e82 Optimize code a little bit. Get rid of very expensive and useless clone operation
ymh <ymh.work@gmail.com>
parents: 191
diff changeset
    75
        this.props.onChange(this.state, {value, operations});
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    76
      }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    77
    })
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    78
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    79
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    80
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    81
  asPlain = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    82
    return Plain.serialize(this.state.value);
5
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
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    85
  asRaw = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    86
    return JSON.stringify(this.state.value.toJSON());
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    87
  }
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    88
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    89
  asHtml = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    90
    return HtmlSerializer.serialize(this.state.value);
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    91
  }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    92
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
    93
  asCategories = () => {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    94
    return this.state.value.document.getMarksByType('category').map((mark) => mark.data.toJS()).toArray();
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
    95
  }
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 25
diff changeset
    96
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    97
  clear = () => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    98
    const value = Plain.deserialize('');
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    99
    this.setState({
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   100
      value,
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   101
      enterKeyValue: 0
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   102
    })
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   103
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   104
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   105
  focus = () => {
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   106
    if(this.editor) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   107
      this.editor.focus();
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   108
    }
11
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   109
  }
6fb4de54acea Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
   110
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   111
  submitNote = () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   112
    this.setState({ enterKeyValue: 0 }, () => {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   113
      if (typeof this.props.submitNote === 'function') {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   114
        this.props.submitNote();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   115
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   116
    });
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   117
  }
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   118
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   119
  /**
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   120
   * On key down, if it's a formatting command toggle a mark.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   121
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   122
   * @param {Event} e
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   123
   * @param {Change} change
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   124
   * @return {Change}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   125
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   126
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   127
  onKeyUp = (e, editor, next) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   128
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   129
    const { value } = this.state;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   130
    const noteText = value.document.text.trim();
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   131
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   132
    if(e.key === "Enter" && noteText.length !== 0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   133
      this.setState({ enterKeyValue: this.state.enterKeyValue + 1 });
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   134
    } else if ( e.getModifierState() || (e.key !== "Control" && e.key !== "Shift" && e.key !== "Meta" && e.key !== "Alt") ) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   135
      this.setState({ enterKeyValue: 0 });
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   136
    }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   137
    return next();
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   138
  }
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
   139
8
6f572b6b6be3 try to make tests work again
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   140
  /**
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   141
   * On key down, if it's a formatting command toggle a mark.
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   142
   *
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   143
   * @param {Event} e
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   144
   * @param {Change} change
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   145
   * @return {Change}
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   146
   */
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   147
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   148
  onKeyDown = (e, editor, next) => {
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   149
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   150
    const { value, enterKeyValue } = this.state;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   151
    const { autoSubmit } = this.props;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   152
    const noteText = value.document.text.trim();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   153
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   154
    // we prevent empty first lines
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   155
    if(e.key === "Enter" && noteText.length === 0) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   156
      e.preventDefault();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   157
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   158
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   159
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   160
    // Enter submit the note
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   161
    if(e.key === "Enter" && ( enterKeyValue === 2 || e.ctrlKey || autoSubmit ) && noteText.length !== 0) {
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   162
      e.preventDefault();
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   163
      this.submitNote();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   164
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   165
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   166
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   167
    if (!e.ctrlKey) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   168
      return next();
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   169
    }
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   170
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   171
    e.preventDefault();
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   172
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   173
    // Decide what to do based on the key code...
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   174
    switch (e.key) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   175
      default: {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   176
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   177
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   178
      // When "B" is pressed, add a "bold" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   179
      case 'b': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   180
        editor.toggleMark('bold');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   181
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   182
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   183
      case 'i': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   184
        // When "U" is pressed, add an "italic" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   185
        editor.toggleMark('italic');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   186
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   187
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   188
      case 'u': {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   189
        // When "U" is pressed, add an "underline" mark to the text.
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   190
        editor.toggleMark('underlined');
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   191
        break;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   192
      }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   193
    }
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   194
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   195
    return next();
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   196
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   197
  }
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   198
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   199
  // Add a `renderMark` method to render marks.
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   200
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   201
  renderMark = (props, editor, next) => {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   202
    const { children, mark, attributes } = props
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   203
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   204
    switch (mark.type) {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   205
      case 'bold':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   206
        return <strong {...attributes}>{children}</strong>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   207
      case 'code':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   208
        return <code {...attributes}>{children}</code>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   209
      case 'italic':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   210
        return <em {...attributes}>{children}</em>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   211
      case 'underlined':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   212
        return <ins {...attributes}>{children}</ins>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   213
      case 'category':
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   214
        let spanStyle = {
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   215
          backgroundColor: mark.data.get('color')
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   216
        };
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   217
        return <span {...attributes} style={ spanStyle } >{children}</span>
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   218
      default:
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   219
        return next();
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   220
    }
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   221
  }
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   222
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   223
  renderNode = (props, editor, next) => {
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   224
    const { attributes, children, node } = props
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   225
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   226
    switch (node.type) {
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   227
      case 'block-quote':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   228
        return <blockquote {...attributes}>{children}</blockquote>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   229
      case 'bulleted-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   230
        return <ul {...attributes}>{children}</ul>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   231
      case 'heading-one':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   232
        return <h1 {...attributes}>{children}</h1>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   233
      case 'heading-two':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   234
        return <h2 {...attributes}>{children}</h2>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   235
      case 'list-item':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   236
        return <li {...attributes}>{children}</li>
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   237
      case 'numbered-list':
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   238
        return <ol {...attributes}>{children}</ol>
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   239
      default:
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   240
        return next();
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   241
    }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   242
  }
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   243
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   244
    /**
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   245
   * Render.
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   246
   *
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   247
   * @return {Element}
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   248
   */
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   249
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   250
  render = () => (
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   251
    <div className="bg-secondary mb-5">
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   252
      <div className="sticky-top">
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   253
        <Toolbar
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   254
          value={this.state.value}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   255
          editor={this.editor}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   256
          note={this.props.note}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   257
          annotationCategories={this.props.annotationCategories}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   258
          isButtonDisabled={this.props.isButtonDisabled}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   259
          submitNote={this.submitNote}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   260
        />
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   261
      </div>
159
a4705c2b4544 Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
   262
      <div className="editor-slatejs p-2">
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   263
        <Editor
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
   264
          ref={this.editorRef}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   265
          spellCheck
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   266
          placeholder={this.props.t('slate_editor.placeholder')}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   267
          value={this.state.value}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   268
          onChange={this.onChange}
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   269
          onKeyDown={this.onKeyDown}
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   270
          onKeyUp={this.onKeyUp}
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   271
          renderMark={this.renderMark}
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   272
          renderNode = {this.renderNode}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   273
        />
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   274
      </div>
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   275
    </div>
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   276
  );
21
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   277
284e866f55c7 First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
   278
5
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
/**
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   282
 * Export.
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   283
 */
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   284
function mapStateToProps(state, props) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   285
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   286
  const autoSubmit = getAutoSubmit(state);
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   287
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   288
  return {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   289
    autoSubmit,
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   290
  };
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   291
}
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
   292
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   293
export default withNamespaces("", {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   294
  innerRef: (ref) => {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   295
    if(!ref) {
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   296
      return;
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   297
    }
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   298
    const wrappedRef = ref.getWrappedInstance();
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   299
    const editorRef = (wrappedRef && wrappedRef.props) ? wrappedRef.props.editorRef : null;
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   300
    if(editorRef && editorRef.hasOwnProperty('current')) {
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   301
      editorRef.current = wrappedRef;
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   302
    }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
   303
  }
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
   304
})(connect(mapStateToProps, null, null, { withRef: true })(SlateEditor));