yomichan/ext/fg/js/source-image.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-07-23 04:47:12 +00:00
/*
* Copyright (C) 2016 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/>.
*/
2016-07-23 20:12:24 +00:00
class TextSourceImage {
2016-07-23 04:47:12 +00:00
constructor(image) {
2016-07-23 20:06:09 +00:00
this.img = image;
this.length = -1;
2016-07-23 04:47:12 +00:00
}
text() {
2016-07-23 20:06:09 +00:00
const text = this.textRaw();
return this.length < 0 ? text : text.substring(0, this.length);
}
2016-07-23 04:47:12 +00:00
2016-07-23 20:06:09 +00:00
textRaw() {
return this.img.getAttribute('alt') || '';
2016-07-23 04:47:12 +00:00
}
setLength(length) {
2016-07-23 20:06:09 +00:00
this.length = length;
2016-07-23 04:47:12 +00:00
}
containsPoint(point) {
2016-07-23 20:06:09 +00:00
const rect = this.getRect();
return point.x >= rect.left && point.x <= rect.right;
2016-07-23 04:47:12 +00:00
}
getRect() {
2016-07-23 20:06:09 +00:00
return this.img.getBoundingClientRect();
2016-07-23 04:47:12 +00:00
}
select() {
2016-07-23 20:06:09 +00:00
// NOP
2016-07-23 04:47:12 +00:00
}
deselect() {
2016-07-23 20:06:09 +00:00
// NOP
2016-07-23 04:47:12 +00:00
}
2016-07-23 20:06:09 +00:00
equals(other) {
return other.img && other.textRaw() == this.textRaw();
2016-07-23 04:47:12 +00:00
}
}