Create DOM utility file

This commit is contained in:
toasted-nutbread 2019-11-26 18:47:16 -05:00
parent c2ff25b0ec
commit 96aad50340
10 changed files with 57 additions and 30 deletions

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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

View File

@ -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) {

View File

@ -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",

46
ext/mixed/js/dom.js Normal file
View File

@ -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;
}
}