forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffset-key.js
More file actions
164 lines (136 loc) · 3.3 KB
/
offset-key.js
File metadata and controls
164 lines (136 loc) · 3.3 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Offset key parser regex.
*/
const PARSER = /^(\w+)(?:-(\d+))?$/
/**
* Offset key attribute name.
*/
const ATTRIBUTE = 'data-offset-key'
const SELECTOR = `[${ATTRIBUTE}]`
/**
* Find the start and end bounds from an `offsetKey` and `ranges`.
*
* @param {Number} index
* @param {List} ranges
* @return {Object}
*/
function findBounds(index, ranges) {
const range = ranges.get(index)
const start = ranges
.slice(0, index)
.reduce((memo, r) => {
return memo += r.text.length
}, 0)
return {
start,
end: start + range.text.length
}
}
/**
* From a `element`, find the closest parent's offset key.
*
* @param {Element} element
* @param {Number} offset
* @return {Object}
*/
function findKey(element, offset) {
if (element.nodeType == 3) element = element.parentNode
const parent = element.closest(SELECTOR)
const children = element.querySelectorAll(SELECTOR)
let offsetKey
// Get the key from a parent if one exists.
if (parent) {
offsetKey = parent.getAttribute(ATTRIBUTE)
}
// COMPAT: In Firefox, and potentially other browsers, when performing a
// "select all" action, a parent element is selected instead of the text. In
// this case, we need to select the proper inner text nodes. (2016/07/26)
else if (children.length) {
let child = children[0]
if (offset != 0) {
child = children[children.length - 1]
offset = child.textContent.length
}
offsetKey = child.getAttribute(ATTRIBUTE)
}
// Otherwise, for void node scenarios, a cousin element will be selected, and
// we need to select the first text node cousin we can find.
else {
while (element = element.parentNode) {
const cousin = element.querySelector(SELECTOR)
if (!cousin) continue
offsetKey = cousin.getAttribute(ATTRIBUTE)
offset = cousin.textContent.length
break
}
}
// If we still didn't find an offset key, error. This is a bug.
if (!offsetKey) {
throw new Error(`Unable to find offset key for ${element} with offset "${offset}".`)
}
// Parse the offset key.
const parsed = parse(offsetKey)
return {
key: parsed.key,
index: parsed.index,
offset
}
}
/**
* Find the selection point from an `offsetKey` and `ranges`.
*
* @param {Object} offsetKey
* @param {List} ranges
* @return {Object}
*/
function findPoint(offsetKey, ranges) {
let { key, index, offset } = offsetKey
const { start, end } = findBounds(index, ranges)
// Don't let the offset be outside of the start and end bounds.
offset = start + offset
offset = Math.max(offset, start)
offset = Math.min(offset, end)
return {
key,
index,
start,
end,
offset
}
}
/**
* Parse an offset key `string`.
*
* @param {String} string
* @return {Object}
*/
function parse(string) {
const matches = PARSER.exec(string)
if (!matches) throw new Error(`Invalid offset key string "${string}".`)
const [ original, key, index ] = matches
return {
key,
index: parseInt(index, 10)
}
}
/**
* Stringify an offset key `object`.
*
* @param {Object} object
* @property {String} key
* @property {Number} index
* @return {String}
*/
function stringify(object) {
return `${object.key}-${object.index}`
}
/**
* Export.
*/
export default {
findBounds,
findKey,
findPoint,
parse,
stringify
}