aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-26 18:47:16 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-26 18:59:52 -0500
commit96aad50340b4d0374979ac981cd1c481cc8dcd94 (patch)
treed378d53debce57c1d60498d08ff8a537c9e0b313
parentc2ff25b0ec2fdb8764a5e9994c1e37bfed7f05c6 (diff)
Create DOM utility file
-rw-r--r--ext/bg/background.html1
-rw-r--r--ext/bg/context.html1
-rw-r--r--ext/bg/search.html1
-rw-r--r--ext/bg/settings-popup-preview.html2
-rw-r--r--ext/bg/settings.html1
-rw-r--r--ext/fg/float.html1
-rw-r--r--ext/fg/js/document.js19
-rw-r--r--ext/fg/js/frontend.js14
-rw-r--r--ext/manifest.json1
-rw-r--r--ext/mixed/js/dom.js46
10 files changed, 57 insertions, 30 deletions
diff --git a/ext/bg/background.html b/ext/bg/background.html
index 3b337e18..5a6970c3 100644
--- a/ext/bg/background.html
+++ b/ext/bg/background.html
@@ -19,6 +19,7 @@
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
<script src="/bg/js/anki.js"></script>
<script src="/bg/js/mecab.js"></script>
diff --git a/ext/bg/context.html b/ext/bg/context.html
index 52ca255d..eda09a68 100644
--- a/ext/bg/context.html
+++ b/ext/bg/context.html
@@ -179,6 +179,7 @@
</div>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
<script src="/bg/js/api.js"></script>
<script src="/bg/js/options.js"></script>
diff --git a/ext/bg/search.html b/ext/bg/search.html
index 16074022..ef24af89 100644
--- a/ext/bg/search.html
+++ b/ext/bg/search.html
@@ -61,6 +61,7 @@
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
<script src="/bg/js/dictionary.js"></script>
<script src="/bg/js/handlebars.js"></script>
diff --git a/ext/bg/settings-popup-preview.html b/ext/bg/settings-popup-preview.html
index 574b6707..9ca59e44 100644
--- a/ext/bg/settings-popup-preview.html
+++ b/ext/bg/settings-popup-preview.html
@@ -118,6 +118,8 @@
</div></div></div>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
+
<script src="/fg/js/api.js"></script>
<script src="/fg/js/document.js"></script>
<script src="/fg/js/frontend-api-receiver.js"></script>
diff --git a/ext/bg/settings.html b/ext/bg/settings.html
index b2af3759..908c618c 100644
--- a/ext/bg/settings.html
+++ b/ext/bg/settings.html
@@ -865,6 +865,7 @@
<script src="/mixed/lib/wanakana.min.js"></script>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
<script src="/mixed/js/japanese.js"></script>
<script src="/bg/js/anki.js"></script>
diff --git a/ext/fg/float.html b/ext/fg/float.html
index e04c1402..38439c79 100644
--- a/ext/fg/float.html
+++ b/ext/fg/float.html
@@ -32,6 +32,7 @@
</div>
<script src="/mixed/js/core.js"></script>
+ <script src="/mixed/js/dom.js"></script>
<script src="/fg/js/api.js"></script>
<script src="/fg/js/util.js"></script>
diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js
index 8161c85a..3dd12a40 100644
--- a/ext/fg/js/document.js
+++ b/ext/fg/js/document.js
@@ -223,7 +223,7 @@ function isPointInRange(x, y, range) {
const {node, offset, content} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1);
range.setEnd(node, offset);
- if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) {
+ if (!isWhitespace(content) && DOM.isPointInAnyRect(x, y, range.getClientRects())) {
return true;
}
} finally {
@@ -234,7 +234,7 @@ function isPointInRange(x, y, range) {
const {node, offset, content} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1);
range.setStart(node, offset);
- if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) {
+ if (!isWhitespace(content) && DOM.isPointInAnyRect(x, y, range.getClientRects())) {
// This purposefully leaves the starting offset as modified and sets the range length to 0.
range.setEnd(node, offset);
return true;
@@ -248,21 +248,6 @@ function isWhitespace(string) {
return string.trim().length === 0;
}
-function isPointInAnyRect(x, y, rects) {
- for (const rect of rects) {
- if (isPointInRect(x, y, rect)) {
- return true;
- }
- }
- return false;
-}
-
-function isPointInRect(x, y, rect) {
- return (
- x >= rect.left && x < rect.right &&
- y >= rect.top && y < rect.bottom);
-}
-
const caretRangeFromPoint = (() => {
if (typeof document.caretRangeFromPoint === 'function') {
// Chrome, Edge
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 6002dfcb..81c159db 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -159,7 +159,7 @@ class Frontend {
this.preventNextClick = false;
const primaryTouch = e.changedTouches[0];
- if (Frontend.selectionContainsPoint(window.getSelection(), primaryTouch.clientX, primaryTouch.clientY)) {
+ if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) {
return;
}
@@ -456,18 +456,6 @@ class Frontend {
return -1;
}
- static selectionContainsPoint(selection, x, y) {
- for (let i = 0; i < selection.rangeCount; ++i) {
- const range = selection.getRangeAt(i);
- for (const rect of range.getClientRects()) {
- if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
- return true;
- }
- }
- }
- return false;
- }
-
setTextSourceScanLength(textSource, length) {
textSource.setEndOffset(length);
if (this.ignoreNodes === null || !textSource.range) {
diff --git a/ext/manifest.json b/ext/manifest.json
index 69ee0c4f..dc670633 100644
--- a/ext/manifest.json
+++ b/ext/manifest.json
@@ -19,6 +19,7 @@
"matches": ["http://*/*", "https://*/*", "file://*/*"],
"js": [
"mixed/js/core.js",
+ "mixed/js/dom.js",
"fg/js/api.js",
"fg/js/document.js",
"fg/js/frontend-api-receiver.js",
diff --git a/ext/mixed/js/dom.js b/ext/mixed/js/dom.js
new file mode 100644
index 00000000..4525dace
--- /dev/null
+++ b/ext/mixed/js/dom.js
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
+ * Author: Alex Yatskov <alex@foosoft.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+class DOM {
+ static isPointInRect(x, y, rect) {
+ return (
+ x >= rect.left && x < rect.right &&
+ y >= rect.top && y < rect.bottom
+ );
+ }
+
+ static isPointInAnyRect(x, y, rects) {
+ for (const rect of rects) {
+ if (DOM.isPointInRect(x, y, rect)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static isPointInSelection(x, y, selection) {
+ for (let i = 0; i < selection.rangeCount; ++i) {
+ const range = selection.getRangeAt(i);
+ if (DOM.isPointInAnyRect(x, y, range.getClientRects())) {
+ return true;
+ }
+ }
+ return false;
+ }
+}