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