client/src/components/NoteInput.js
author ymh <ymh.work@gmail.com>
Wed, 05 Dec 2018 23:52:25 +0100
changeset 194 d19ba6045e82
parent 191 3f71ad81a5a9
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:
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 26
diff changeset
     1
import React, { Component } from 'react';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import PropTypes from 'prop-types';
5
5c91bfa8fcde Introduce SlateJS.
Alexandre Segura <mex.zktk@gmail.com>
parents: 2
diff changeset
     3
import SlateEditor from './SlateEditor';
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
     4
import { now } from '../utils';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
class NoteInput extends Component {
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
     8
  constructor(props) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
     9
    super(props);
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    10
    this.editorInst = React.createRef();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    11
  }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    12
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    13
  get editor() {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    14
    return this.editorInst.current;
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    15
  }
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    16
  state = {
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    17
    buttonDisabled: false,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    18
    startedAt: null,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    19
    finishedAt: null,
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    20
  }
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    22
  onEditorChange = (e) => {
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
    23
    let text = '';
d19ba6045e82 Optimize code a little bit. Get rid of very expensive and useless clone operation
ymh <ymh.work@gmail.com>
parents: 191
diff changeset
    24
    if(this.editor) {
d19ba6045e82 Optimize code a little bit. Get rid of very expensive and useless clone operation
ymh <ymh.work@gmail.com>
parents: 191
diff changeset
    25
      text = this.editor.asPlain();
d19ba6045e82 Optimize code a little bit. Get rid of very expensive and useless clone operation
ymh <ymh.work@gmail.com>
parents: 191
diff changeset
    26
    }
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    27
    this.setState({
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
    28
      buttonDisabled: text.length === 0,
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    29
      startedAt: e.startedAt,
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    30
      finishedAt: e.finishedAt
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    31
    });
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
  }
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    34
  submitNote = () => {
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
    35
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    36
    const plain = this.editor.asPlain();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    37
    const raw = this.editor.asRaw();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    38
    const html = this.editor.asHtml();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    39
    const categories = this.editor.asCategories();
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    40
    // const marginComment = this.marginComment.value;
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    41
125
c653f49fabfb remove unecessary dependencies to Redux
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    42
    this.props.addNote(this.props.session, {
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    43
      plain: plain,
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    44
      raw: raw,
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 16
diff changeset
    45
      html: html,
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    46
      startedAt: this.state.startedAt,
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 72
diff changeset
    47
      finishedAt: now(),
26
930e486ad0a8 Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 21
diff changeset
    48
      categories: categories,
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    49
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    50
      // marginComment: marginComment,
15
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    51
    });
4a8bbd314a46 Store note JSON data.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    52
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    53
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    54
    this.editor.clear();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    55
    setTimeout(() => this.editor.focus(), 250);
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
  }
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    58
  componentDidMount() {
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    59
    if(this.editor) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    60
      const text = this.editor.asPlain();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    61
      this.setState({ buttonDisabled: text.length === 0 });
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    62
    }
16
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    63
  }
e67cd18cc594 Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents: 15
diff changeset
    64
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
  render() {
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    return (
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    67
      <form>
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    68
        <div className="editor mb-3">
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 157
diff changeset
    69
          <div className="editor-left sticky-bottom px-2">
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    70
            <SlateEditor editorRef={this.editorInst}
72
7634b424f426 Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 64
diff changeset
    71
              onChange={this.onEditorChange}
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    72
              submitNote={this.submitNote}
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
    73
              isButtonDisabled={this.state.buttonDisabled}
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
    74
              annotationCategories={ this.props.annotationCategories } />
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    75
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 8
diff changeset
    76
          </div>
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    77
          {/* <div className="editor-right w-25 pl-2 border-0 sticky-bottom">
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    78
            <input type="textarea" className="form-control h-100"
35
97106bacb24e Improve session page design.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    79
              name="margin"
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    80
              placeholder="Espace de commentaire pour votre note"
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    81
              // inputRef={ ref => { this.marginComment = ref; } }
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    82
              ref={(marginComment) => { this.marginComment = marginComment; }}
35
97106bacb24e Improve session page design.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    83
            />
157
5c3af4f10e92 Update slate editor
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    84
          </div> */}
35
97106bacb24e Improve session page design.
Alexandre Segura <mex.zktk@gmail.com>
parents: 29
diff changeset
    85
        </div>
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    86
      </form>
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    );
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
  }
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
}
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
NoteInput.propTypes = {
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
  addNote: PropTypes.func.isRequired
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
125
c653f49fabfb remove unecessary dependencies to Redux
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    95
export default NoteInput;