Accessing Content

TextSync provides an API so you can retrieve the contents of your editor instance programatically.

Retrieving Formatted Contents

You can access a data structure showing your entire document contents including formatting using the getContents method on your editor instance.

1
2
3
4
5
6
7
8
9
textSync
  .createEditor({
    /* ...options... */
  })
  .then(editor => {
    const contents = editor.getContents()
    // ...
    // ...
  });

Example Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[{
  "text": "This is a document, some important things are in ",
  "attributes": {
    "bold": false,
    "italic": false,
    "underline": false,
    "strikethrough": false
  }
}, {
  "text": "bold",
  "attributes": {
    "bold": true,
    "italic": false,
    "underline": false,
    "strikethrough": false
  }
}, {
  "text": " some other important things are in ",
  "attributes": {
    "bold": false,
    "italic": false,
    "underline": false,
    "strikethrough": false
  }
}, {
  "text": "italics",
  "attributes": {
    "bold": false,
    "italic": true,
    "underline": false,
    "strikethrough": false
  }
}]

For full details of the object returned by the getContents method, check the reference

Retrieving Plain Text

If you just want the contents of your document as a string, use thegetText method.

1
2
3
4
5
6
7
8
9
textSync
  .createEditor({
    /* ...options... */
  })
  .then(editor => {
    const text = editor.getText();
    // ...
    // ...
  });

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.