Merge pull request #197 from toasted-nutbread/imposter-improvements
Imposter element improvements
This commit is contained in:
commit
037363e3a7
@ -17,75 +17,107 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function docOffsetCalc(element) {
|
function docSetImposterStyle(style, propertyName, value) {
|
||||||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
|
style.setProperty(propertyName, value, 'important');
|
||||||
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
|
|
||||||
|
|
||||||
const clientTop = document.documentElement.clientTop || document.body.clientTop || 0;
|
|
||||||
const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0;
|
|
||||||
|
|
||||||
const rect = element.getBoundingClientRect();
|
|
||||||
const top = Math.round(rect.top + scrollTop - clientTop);
|
|
||||||
const left = Math.round(rect.left + scrollLeft - clientLeft);
|
|
||||||
|
|
||||||
return {top, left};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function docImposterCreate(element) {
|
function docImposterCreate(element, isTextarea) {
|
||||||
const styleProps = window.getComputedStyle(element);
|
const elementStyle = window.getComputedStyle(element);
|
||||||
const stylePairs = [];
|
const elementRect = element.getBoundingClientRect();
|
||||||
for (const key of styleProps) {
|
const documentRect = document.documentElement.getBoundingClientRect();
|
||||||
stylePairs.push(`${key}: ${styleProps[key]};`);
|
const left = elementRect.left - documentRect.left;
|
||||||
}
|
const top = elementRect.top - documentRect.top;
|
||||||
|
|
||||||
const offset = docOffsetCalc(element);
|
// Container
|
||||||
|
const container = document.createElement('div');
|
||||||
|
const containerStyle = container.style;
|
||||||
|
docSetImposterStyle(containerStyle, 'all', 'initial');
|
||||||
|
docSetImposterStyle(containerStyle, 'position', 'absolute');
|
||||||
|
docSetImposterStyle(containerStyle, 'left', '0');
|
||||||
|
docSetImposterStyle(containerStyle, 'top', '0');
|
||||||
|
docSetImposterStyle(containerStyle, 'width', `${documentRect.width}px`);
|
||||||
|
docSetImposterStyle(containerStyle, 'height', `${documentRect.height}px`);
|
||||||
|
docSetImposterStyle(containerStyle, 'overflow', 'hidden');
|
||||||
|
docSetImposterStyle(containerStyle, 'opacity', '0');
|
||||||
|
|
||||||
|
docSetImposterStyle(containerStyle, 'pointer-events', 'none');
|
||||||
|
docSetImposterStyle(containerStyle, 'z-index', '2147483646');
|
||||||
|
|
||||||
|
// Imposter
|
||||||
const imposter = document.createElement('div');
|
const imposter = document.createElement('div');
|
||||||
imposter.className = 'yomichan-imposter';
|
const imposterStyle = imposter.style;
|
||||||
|
|
||||||
imposter.innerText = element.value;
|
imposter.innerText = element.value;
|
||||||
imposter.style.cssText = stylePairs.join('\n');
|
|
||||||
imposter.style.position = 'absolute';
|
for (let i = 0, ii = elementStyle.length; i < ii; ++i) {
|
||||||
imposter.style.top = `${offset.top}px`;
|
const property = elementStyle[i];
|
||||||
imposter.style.left = `${offset.left}px`;
|
docSetImposterStyle(imposterStyle, property, elementStyle.getPropertyValue(property));
|
||||||
imposter.style.opacity = 0;
|
}
|
||||||
imposter.style.zIndex = 2147483646;
|
docSetImposterStyle(imposterStyle, 'position', 'absolute');
|
||||||
if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') {
|
docSetImposterStyle(imposterStyle, 'top', `${top}px`);
|
||||||
imposter.style.overflow = 'auto';
|
docSetImposterStyle(imposterStyle, 'left', `${left}px`);
|
||||||
|
docSetImposterStyle(imposterStyle, 'margin', '0');
|
||||||
|
docSetImposterStyle(imposterStyle, 'pointer-events', 'auto');
|
||||||
|
|
||||||
|
if (isTextarea) {
|
||||||
|
if (elementStyle.overflow === 'visible') {
|
||||||
|
docSetImposterStyle(imposterStyle, 'overflow', 'auto');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
docSetImposterStyle(imposterStyle, 'overflow', 'hidden');
|
||||||
|
docSetImposterStyle(imposterStyle, 'white-space', 'nowrap');
|
||||||
|
docSetImposterStyle(imposterStyle, 'line-height', elementStyle.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
container.appendChild(imposter);
|
||||||
|
document.body.appendChild(container);
|
||||||
|
|
||||||
|
// Adjust size
|
||||||
|
const imposterRect = imposter.getBoundingClientRect();
|
||||||
|
if (imposterRect.width !== elementRect.width || imposterRect.height !== elementRect.height) {
|
||||||
|
const width = parseFloat(elementStyle.width) + (elementRect.width - imposterRect.width);
|
||||||
|
const height = parseFloat(elementStyle.height) + (elementRect.height - imposterRect.height);
|
||||||
|
docSetImposterStyle(imposterStyle, 'width', `${width}px`);
|
||||||
|
docSetImposterStyle(imposterStyle, 'height', `${height}px`);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body.appendChild(imposter);
|
|
||||||
imposter.scrollTop = element.scrollTop;
|
imposter.scrollTop = element.scrollTop;
|
||||||
imposter.scrollLeft = element.scrollLeft;
|
imposter.scrollLeft = element.scrollLeft;
|
||||||
|
|
||||||
return imposter;
|
return [imposter, container];
|
||||||
}
|
|
||||||
|
|
||||||
function docImposterDestroy() {
|
|
||||||
for (const element of document.getElementsByClassName('yomichan-imposter')) {
|
|
||||||
element.parentNode.removeChild(element);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function docRangeFromPoint(point) {
|
function docRangeFromPoint(point) {
|
||||||
const element = document.elementFromPoint(point.x, point.y);
|
const element = document.elementFromPoint(point.x, point.y);
|
||||||
let imposter = null;
|
let imposter = null;
|
||||||
|
let imposterContainer = null;
|
||||||
if (element) {
|
if (element) {
|
||||||
switch (element.nodeName) {
|
switch (element.nodeName) {
|
||||||
case 'IMG':
|
case 'IMG':
|
||||||
case 'BUTTON':
|
case 'BUTTON':
|
||||||
return new TextSourceElement(element);
|
return new TextSourceElement(element);
|
||||||
case 'INPUT':
|
case 'INPUT':
|
||||||
|
[imposter, imposterContainer] = docImposterCreate(element, false);
|
||||||
|
break;
|
||||||
case 'TEXTAREA':
|
case 'TEXTAREA':
|
||||||
imposter = docImposterCreate(element);
|
[imposter, imposterContainer] = docImposterCreate(element, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const range = document.caretRangeFromPoint(point.x, point.y);
|
const range = document.caretRangeFromPoint(point.x, point.y);
|
||||||
if (imposter !== null) {
|
if (range !== null && isPointInRange(point, range)) {
|
||||||
imposter.style.zIndex = -2147483646;
|
if (imposter !== null) {
|
||||||
|
docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646');
|
||||||
|
docSetImposterStyle(imposter.style, 'pointer-events', 'none');
|
||||||
|
}
|
||||||
|
return new TextSourceRange(range, '', imposterContainer);
|
||||||
|
} else {
|
||||||
|
if (imposterContainer !== null) {
|
||||||
|
imposterContainer.parentNode.removeChild(imposterContainer);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return range !== null && isPointInRange(point, range) ? new TextSourceRange(range) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function docSentenceExtract(source, extent) {
|
function docSentenceExtract(source, extent) {
|
||||||
|
@ -307,10 +307,11 @@ class Frontend {
|
|||||||
this.onError(e);
|
this.onError(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
if (textSource !== null) {
|
||||||
|
textSource.cleanup();
|
||||||
|
}
|
||||||
if (hideResults && this.options.scanning.autoHideResults) {
|
if (hideResults && this.options.scanning.autoHideResults) {
|
||||||
this.searchClear();
|
this.searchClear();
|
||||||
} else {
|
|
||||||
docImposterDestroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
@ -371,7 +372,6 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
searchClear() {
|
searchClear() {
|
||||||
docImposterDestroy();
|
|
||||||
this.popup.hide();
|
this.popup.hide();
|
||||||
this.popup.clearAutoPlayTimer();
|
this.popup.clearAutoPlayTimer();
|
||||||
|
|
||||||
|
@ -25,13 +25,20 @@ const IGNORE_TEXT_PATTERN = /\u200c/;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class TextSourceRange {
|
class TextSourceRange {
|
||||||
constructor(range, content='') {
|
constructor(range, content, imposterContainer) {
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
this.imposterContainer = imposterContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone() {
|
clone() {
|
||||||
return new TextSourceRange(this.range.cloneRange(), this.content);
|
return new TextSourceRange(this.range.cloneRange(), this.content, this.imposterContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
if (this.imposterContainer !== null && this.imposterContainer.parentNode !== null) {
|
||||||
|
this.imposterContainer.parentNode.removeChild(this.imposterContainer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
@ -221,6 +228,10 @@ class TextSourceElement {
|
|||||||
return new TextSourceElement(this.element, this.content);
|
return new TextSourceElement(this.element, this.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
// NOP
|
||||||
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
return this.content;
|
return this.content;
|
||||||
}
|
}
|
||||||
|
@ -84,17 +84,23 @@ class Display {
|
|||||||
if (textSource === null) {
|
if (textSource === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
textSource.setEndOffset(this.options.scanning.length);
|
|
||||||
|
|
||||||
const {definitions, length} = await apiTermsFind(textSource.text());
|
let definitions, length, sentence;
|
||||||
if (definitions.length === 0) {
|
try {
|
||||||
return false;
|
textSource.setEndOffset(this.options.scanning.length);
|
||||||
|
|
||||||
|
({definitions, length} = await apiTermsFind(textSource.text()));
|
||||||
|
if (definitions.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
textSource.setEndOffset(length);
|
||||||
|
|
||||||
|
sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
||||||
|
} finally {
|
||||||
|
textSource.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
textSource.setEndOffset(length);
|
|
||||||
|
|
||||||
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
source: {
|
source: {
|
||||||
definitions: this.definitions,
|
definitions: this.definitions,
|
||||||
|
Loading…
Reference in New Issue
Block a user