aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/(editor)/ai.md
blob: 1528a7bdc609d2f33571bb1e910415cf02790058 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
## to access the editor
```
import { useEditor } from "novel";
const editor = useEditor()
```

## to get previous text
```
import { getPrevText } from "novel/utils";
const pos = editor.state.selection.from;
const text = getPrevText(editor, pos);
```

## selected content into markdown format 
```
const slice = editor.state.selection.content();
const text = editor.storage.markdown.serializer.serialize(slice.content);
```

## replace Selection 
```
const selection = editor.view.state.selection;
editor.chain().focus()
  .insertContentAt(
    {
      from: selection.from,
      to: selection.to,
    },
    completion,
  )
  .run();
```


## to insert after
```
const selection = editor.view.state.selection;
editor
  .chain()
  .focus()
  .insertContentAt(selection.to + 1, completion)
  .run();
```