client/src/AnnotationPlugin.js
author Alexandre Segura <mex.zktk@gmail.com>
Wed, 28 Jun 2017 13:29:07 +0200
changeset 102 b0e36664f1f2
parent 19 f1b125b95fe9
child 157 5c3af4f10e92
permissions -rw-r--r--
Add TextPositionSelector.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
function AnnotationPlugin(options) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
  const { onChange } = options
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
  return {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
    onSelect(event, data, state, editor) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
      event.preventDefault()
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
      const selection = data.selection;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
      const startOffset = selection.startOffset;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
      const endOffset = selection.endOffset;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
      if (selection.isCollapsed) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
        return;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
      let nodes = [];
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
      let hasStarted = false;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
      let hasEnded = false;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
      // Keep only the relevant nodes,
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
      // i.e. nodes which are contained within selection
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
      state.document.nodes.forEach((node) => {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
        if (selection.hasStartIn(node)) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
          hasStarted = true;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
        }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
        if (hasStarted && !hasEnded) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
          nodes.push(node);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
        }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
        if (selection.hasEndIn(node)) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
          hasEnded = true;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
        }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
      });
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
      let text = '';
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
      // Concatenate the nodes text
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
      if (nodes.length === 1) {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
        text = nodes[0].text.substring(startOffset, endOffset);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
      } else {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
        text = nodes.map((node) => {
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
          if (selection.hasStartIn(node)) return node.text.substring(startOffset);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
          if (selection.hasEndIn(node)) return node.text.substring(0, endOffset);
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
          return node.text;
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
        }).join('\n');
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
      if (onChange) {
102
b0e36664f1f2 Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents: 19
diff changeset
    50
        onChange(text, startOffset, endOffset);
19
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
      }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
    }
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
  };
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
}
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
f1b125b95fe9 Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
export default AnnotationPlugin;