Create DOM utility file
This commit is contained in:
parent
c2ff25b0ec
commit
96aad50340
@ -19,6 +19,7 @@
|
|||||||
<script src="/mixed/lib/wanakana.min.js"></script>
|
<script src="/mixed/lib/wanakana.min.js"></script>
|
||||||
|
|
||||||
<script src="/mixed/js/core.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/anki.js"></script>
|
||||||
<script src="/bg/js/mecab.js"></script>
|
<script src="/bg/js/mecab.js"></script>
|
||||||
|
@ -179,6 +179,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/mixed/js/core.js"></script>
|
<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/api.js"></script>
|
||||||
<script src="/bg/js/options.js"></script>
|
<script src="/bg/js/options.js"></script>
|
||||||
|
@ -61,6 +61,7 @@
|
|||||||
<script src="/mixed/lib/wanakana.min.js"></script>
|
<script src="/mixed/lib/wanakana.min.js"></script>
|
||||||
|
|
||||||
<script src="/mixed/js/core.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/dictionary.js"></script>
|
||||||
<script src="/bg/js/handlebars.js"></script>
|
<script src="/bg/js/handlebars.js"></script>
|
||||||
|
@ -118,6 +118,8 @@
|
|||||||
</div></div></div>
|
</div></div></div>
|
||||||
|
|
||||||
<script src="/mixed/js/core.js"></script>
|
<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/api.js"></script>
|
||||||
<script src="/fg/js/document.js"></script>
|
<script src="/fg/js/document.js"></script>
|
||||||
<script src="/fg/js/frontend-api-receiver.js"></script>
|
<script src="/fg/js/frontend-api-receiver.js"></script>
|
||||||
|
@ -865,6 +865,7 @@
|
|||||||
<script src="/mixed/lib/wanakana.min.js"></script>
|
<script src="/mixed/lib/wanakana.min.js"></script>
|
||||||
|
|
||||||
<script src="/mixed/js/core.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="/mixed/js/japanese.js"></script>
|
||||||
|
|
||||||
<script src="/bg/js/anki.js"></script>
|
<script src="/bg/js/anki.js"></script>
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/mixed/js/core.js"></script>
|
<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/api.js"></script>
|
||||||
<script src="/fg/js/util.js"></script>
|
<script src="/fg/js/util.js"></script>
|
||||||
|
@ -223,7 +223,7 @@ function isPointInRange(x, y, range) {
|
|||||||
const {node, offset, content} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1);
|
const {node, offset, content} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1);
|
||||||
range.setEnd(node, offset);
|
range.setEnd(node, offset);
|
||||||
|
|
||||||
if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) {
|
if (!isWhitespace(content) && DOM.isPointInAnyRect(x, y, range.getClientRects())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -234,7 +234,7 @@ function isPointInRange(x, y, range) {
|
|||||||
const {node, offset, content} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1);
|
const {node, offset, content} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1);
|
||||||
range.setStart(node, offset);
|
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.
|
// This purposefully leaves the starting offset as modified and sets the range length to 0.
|
||||||
range.setEnd(node, offset);
|
range.setEnd(node, offset);
|
||||||
return true;
|
return true;
|
||||||
@ -248,21 +248,6 @@ function isWhitespace(string) {
|
|||||||
return string.trim().length === 0;
|
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 = (() => {
|
const caretRangeFromPoint = (() => {
|
||||||
if (typeof document.caretRangeFromPoint === 'function') {
|
if (typeof document.caretRangeFromPoint === 'function') {
|
||||||
// Chrome, Edge
|
// Chrome, Edge
|
||||||
|
@ -159,7 +159,7 @@ class Frontend {
|
|||||||
this.preventNextClick = false;
|
this.preventNextClick = false;
|
||||||
|
|
||||||
const primaryTouch = e.changedTouches[0];
|
const primaryTouch = e.changedTouches[0];
|
||||||
if (Frontend.selectionContainsPoint(window.getSelection(), primaryTouch.clientX, primaryTouch.clientY)) {
|
if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -456,18 +456,6 @@ class Frontend {
|
|||||||
return -1;
|
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) {
|
setTextSourceScanLength(textSource, length) {
|
||||||
textSource.setEndOffset(length);
|
textSource.setEndOffset(length);
|
||||||
if (this.ignoreNodes === null || !textSource.range) {
|
if (this.ignoreNodes === null || !textSource.range) {
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"matches": ["http://*/*", "https://*/*", "file://*/*"],
|
"matches": ["http://*/*", "https://*/*", "file://*/*"],
|
||||||
"js": [
|
"js": [
|
||||||
"mixed/js/core.js",
|
"mixed/js/core.js",
|
||||||
|
"mixed/js/dom.js",
|
||||||
"fg/js/api.js",
|
"fg/js/api.js",
|
||||||
"fg/js/document.js",
|
"fg/js/document.js",
|
||||||
"fg/js/frontend-api-receiver.js",
|
"fg/js/frontend-api-receiver.js",
|
||||||
|
46
ext/mixed/js/dom.js
Normal file
46
ext/mixed/js/dom.js
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user