summaryrefslogtreecommitdiff
path: root/ext/fg/js/range.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-04-23 10:10:34 -0700
committerAlex Yatskov <alex@foosoft.net>2016-04-23 10:10:34 -0700
commit8b097221db06cd9119acc405ec84794362899086 (patch)
tree379e2f944f6a9598a71a2711e50aa86944bc437f /ext/fg/js/range.js
parentb8df875e030eabb73b67a9a9c721c6922145a8c9 (diff)
WIP
Diffstat (limited to 'ext/fg/js/range.js')
-rw-r--r--ext/fg/js/range.js39
1 files changed, 30 insertions, 9 deletions
diff --git a/ext/fg/js/range.js b/ext/fg/js/range.js
index 18c0d6bb..473cc449 100644
--- a/ext/fg/js/range.js
+++ b/ext/fg/js/range.js
@@ -45,17 +45,38 @@ class Range {
}
paddedRect() {
- const node = this.range.startContainer;
- const startOffset = this.range.startOffset;
- const endOffset = this.range.endOffset;
+ const range = this.range.cloneRange();
+ const startOffset = range.startOffset;
+ const endOffset = range.endOffset;
+ const node = range.startContainer;
- this.range.setStart(node, Math.max(0, startOffset - 1));
- this.range.setEnd(node, Math.min(node.length, endOffset + 1));
- const rect = range.getBoundingClientRect();
- this.range.setStart(node, startOffset);
- this.range.setEnd(node, endOffset);
+ range.setStart(node, Math.max(0, startOffset - 1));
+ range.setEnd(node, Math.min(node.length, endOffset + 1));
+
+ return range.getBoundingClientRect();
+ }
+
+ select(length) {
+ const range = this.range.cloneRange();
+ range.setEnd(range.startContainer, range.startOffset + length);
+
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(range);
+ }
+
+ deselect() {
+ const selection = window.getSelection();
+ selection.removeRange(this.range);
+ }
+
+ equalTo(range) {
+ const equal =
+ range.compareBoundaryPoints(Range.END_TO_END, this.range) === 0 &&
+ range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0;
+
+ return equal;
- return rect;
}
static fromPos(point) {