-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
InsertText.ts
43 lines (36 loc) · 1.32 KB
/
InsertText.ts
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
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { getElementFromPosition } from '../caret/CaretUtils';
import NodeType from '../dom/NodeType';
import { CaretPosition } from '../caret/CaretPosition';
import { Insert, Element } from '@ephox/sugar';
import { Option, Fun } from '@ephox/katamari';
const insertTextAtPosition = (text: string, pos: CaretPosition): Option<CaretPosition> => {
const container = pos.container();
const offset = pos.offset();
if (NodeType.isText(container)) {
container.insertData(offset, text);
return Option.some(CaretPosition(container, offset + text.length));
} else {
return getElementFromPosition(pos).map((elm) => {
const textNode = Element.fromText(text);
if (pos.isAtEnd()) {
Insert.after(elm, textNode);
} else {
Insert.before(elm, textNode);
}
return CaretPosition(textNode.dom(), text.length);
});
}
};
const insertNbspAtPosition = Fun.curry(insertTextAtPosition, '\u00a0');
const insertSpaceAtPosition = Fun.curry(insertTextAtPosition, ' ');
export {
insertTextAtPosition,
insertNbspAtPosition,
insertSpaceAtPosition
};