Sentence and URL support
This commit is contained in:
parent
d26ecab0b5
commit
f88f8dc97f
@ -171,7 +171,9 @@ class Yomichan {
|
|||||||
'kunyomi',
|
'kunyomi',
|
||||||
'onyomi',
|
'onyomi',
|
||||||
'reading',
|
'reading',
|
||||||
|
'sentence',
|
||||||
'tags',
|
'tags',
|
||||||
|
'url',
|
||||||
];
|
];
|
||||||
|
|
||||||
for (let tag of tags) {
|
for (let tag of tags) {
|
||||||
|
@ -25,6 +25,7 @@ class Client {
|
|||||||
this.lastTextSource = null;
|
this.lastTextSource = null;
|
||||||
this.activateKey = 16;
|
this.activateKey = 16;
|
||||||
this.activateBtn = 2;
|
this.activateBtn = 2;
|
||||||
|
this.sentenceExtent = 200;
|
||||||
this.enabled = false;
|
this.enabled = false;
|
||||||
this.options = {};
|
this.options = {};
|
||||||
this.definitions = null;
|
this.definitions = null;
|
||||||
@ -94,9 +95,15 @@ class Client {
|
|||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
} else {
|
} else {
|
||||||
const sequence = ++this.sequence;
|
|
||||||
textSource.setEndOffset(length);
|
textSource.setEndOffset(length);
|
||||||
|
|
||||||
|
const sentence = Client.extractSentence(textSource, this.sentenceExtent);
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
definition.url = window.location.href;
|
||||||
|
definition.sentence = sentence;
|
||||||
|
});
|
||||||
|
|
||||||
|
const sequence = ++this.sequence;
|
||||||
bgRenderText(
|
bgRenderText(
|
||||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||||
'term-list.html',
|
'term-list.html',
|
||||||
@ -180,6 +187,10 @@ class Client {
|
|||||||
|
|
||||||
api_displayKanji(kanji) {
|
api_displayKanji(kanji) {
|
||||||
bgFindKanji(kanji, (definitions) => {
|
bgFindKanji(kanji, (definitions) => {
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
definition.url = window.location.href;
|
||||||
|
});
|
||||||
|
|
||||||
const sequence = ++this.sequence;
|
const sequence = ++this.sequence;
|
||||||
bgRenderText(
|
bgRenderText(
|
||||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||||
@ -219,11 +230,16 @@ class Client {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static extractSentence(content, position) {
|
static extractSentence(source, extent) {
|
||||||
const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'};
|
const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'};
|
||||||
const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'};
|
const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'};
|
||||||
const terminators = '…。..??!!';
|
const terminators = '…。..??!!';
|
||||||
|
|
||||||
|
const sourceLocal = source.clone();
|
||||||
|
const position = sourceLocal.setStartOffset(extent);
|
||||||
|
sourceLocal.setEndOffset(position + extent);
|
||||||
|
const content = sourceLocal.text();
|
||||||
|
|
||||||
let quoteStack = [];
|
let quoteStack = [];
|
||||||
|
|
||||||
let startPos = 0;
|
let startPos = 0;
|
||||||
|
@ -18,9 +18,13 @@
|
|||||||
|
|
||||||
|
|
||||||
class TextSourceInput {
|
class TextSourceInput {
|
||||||
constructor(input) {
|
constructor(input, length=-1) {
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.length = -1;
|
this.length = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new TextSourceInput(this.input, this.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
|
@ -22,20 +22,26 @@ class TextSourceRange {
|
|||||||
this.rng = range;
|
this.rng = range;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new TextSourceRange(this.rng.cloneRange());
|
||||||
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
return this.rng.toString();
|
return this.rng.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
setEndOffset(length) {
|
setEndOffset(length) {
|
||||||
const end = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset + length);
|
const lengthAdj = length + this.rng.startOffset;
|
||||||
this.rng.setEnd(end.node, end.offset);
|
const state = TextSourceRange.seekForward(this.rng.startContainer, lengthAdj);
|
||||||
return length - end.length;
|
this.rng.setEnd(state.node, state.offset);
|
||||||
|
return length - state.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
setStartOffset(length) {
|
setStartOffset(length) {
|
||||||
const start = TextSourceRange.seekBackward(this.rng.startContainer, length + (this.rng.startContainer.length - this.rng.startOffset));
|
const lengthAdj = length + (this.rng.startContainer.length - this.rng.startOffset);
|
||||||
this.rng.setStart(start.node, start.offset);
|
const state = TextSourceRange.seekBackward(this.rng.startContainer, lengthAdj);
|
||||||
return length - start.length;
|
this.rng.setStart(state.node, state.offset);
|
||||||
|
return length - state.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
containsPoint(point) {
|
containsPoint(point) {
|
||||||
|
Loading…
Reference in New Issue
Block a user