-
Notifications
You must be signed in to change notification settings - Fork 144
/
selectRange.ts
72 lines (64 loc) · 2.59 KB
/
selectRange.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import EditorCore, { Select, SelectRange } from '../interfaces/EditorCore';
import { Browser, contains, createRange } from 'roosterjs-editor-dom';
import { hasFocus } from './hasFocus';
/**
* Change the editor selection to the given range
* @param core The EditorCore object
* @param range The range to select
* @param skipSameRange When set to true, do nothing if the given range is the same with current selection
* in editor, otherwise it will always remove current selection ranage and set to the given one.
* This parameter is always treat as true in Edge to avoid some weird runtime exception.
*/
export const selectRange: SelectRange = (
core: EditorCore,
range: Range,
skipSameRange?: boolean
) => {
let selection: Selection;
let needAddRange = true;
if (
!contains(core.contentDiv, range) ||
!(selection = core.document.defaultView.getSelection())
) {
return false;
}
if (selection.rangeCount > 0) {
// Workaround IE exception 800a025e
try {
let currentRange: Range;
// Do not remove/add range if current selection is the same with target range
// Without this check, execCommand() may fail in Edge since we changed the selection
if (
(skipSameRange || Browser.isEdge) &&
(currentRange = selection.rangeCount == 1 ? selection.getRangeAt(0) : null) &&
currentRange.startContainer == range.startContainer &&
currentRange.startOffset == range.startOffset &&
currentRange.endContainer == range.endContainer &&
currentRange.endOffset == range.endOffset
) {
needAddRange = false;
} else {
selection.removeAllRanges();
}
} catch (e) {}
}
if (needAddRange) {
selection.addRange(range);
}
if (!hasFocus(core)) {
core.cachedSelectionRange = range;
}
if (range.collapsed) {
// If selected, and current selection is collapsed,
// need to restore pending format state if exists.
core.corePlugins.domEvent.restorePendingFormatState();
}
return true;
};
/**
* @deprecated Only for compatibility with existing code, don't use ths function, use selectRange instead
*/
export const select: Select = (core: EditorCore, arg1: any, arg2?: any, arg3?: any, arg4?: any) => {
let range = arg1 instanceof Range ? arg1 : createRange(arg1, arg2, arg3, arg4);
return core.api.selectRange(core, range);
};