From bbe4afecf6a56f4bdaaafe083a3bf67191fd9cde Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 6 Apr 2017 21:07:55 -0700 Subject: wip --- ext/fg/js/util.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'ext/fg') diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index a1bce660..59060221 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -205,14 +205,10 @@ function docClozeExtract(source, extent) { } const sentence = content.substring(startPos, endPos); - const clozePrefix = sentence.substring(0, position - startPos); - const clozeBody = source.text(); - const clozeSuffix = sentence.substring(position - startPos + clozeBody.length); + const padding = sentence.length - sentence.replace(/^\s+/, ''); return { sentence: sentence.trim(), - prefix: clozePrefix.trim(), - body: clozeBody.trim(), - suffix: clozeSuffix.trim() + offset: position - startPos - padding }; } -- cgit v1.2.3 From c4b1a4a5b4811d7aaa70b3b83f740c9e3d2b86be Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 7 Apr 2017 21:17:13 -0700 Subject: wip --- ext/fg/js/driver.js | 8 ++++---- ext/fg/js/util.js | 8 ++++---- ext/mixed/js/display.js | 8 ++++---- ext/mixed/js/util.js | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'ext/fg') diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js index 49147c8f..e94a4ac2 100644 --- a/ext/fg/js/driver.js +++ b/ext/fg/js/driver.js @@ -172,13 +172,13 @@ window.driver = new class { } else { textSource.setEndOffset(length); - const cloze = docClozeExtract(textSource, this.options.anki.sentenceExt); + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; this.popup.showTermDefs( textSource.getRect(), definitions, this.options, - {cloze, url} + {sentence, url} ); this.lastTextSource = textSource; @@ -198,13 +198,13 @@ window.driver = new class { if (definitions.length === 0) { return false; } else { - const cloze = docClozeExtract(textSource, this.options.anki.sentenceExt); + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; this.popup.showKanjiDefs( textSource.getRect(), definitions, this.options, - {cloze, url} + {sentence, url} ); this.lastTextSource = textSource; diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 59060221..445efa5b 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -152,7 +152,7 @@ function docRangeFromPoint(point, imposter) { return null; } -function docClozeExtract(source, extent) { +function docSentenceExtract(source, extent) { const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'}; const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'}; const terminators = '…。..??!!'; @@ -204,11 +204,11 @@ function docClozeExtract(source, extent) { } } - const sentence = content.substring(startPos, endPos); - const padding = sentence.length - sentence.replace(/^\s+/, ''); + const text = content.substring(startPos, endPos); + const padding = text.length - text.replace(/^\s+/, ''); return { - sentence: sentence.trim(), + text: text.trim(), offset: position - startPos - padding }; } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index e77ca7da..973b7749 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -74,7 +74,7 @@ class Display { if (context) { for (const definition of definitions) { - definition.cloze = clozeBuild(context.cloze); + definition.cloze = clozeBuild(context.sentence, definition.source); definition.url = context.url; } } @@ -108,7 +108,7 @@ class Display { if (context) { for (const definition of definitions) { - definition.cloze = clozeBuild(context.cloze); + definition.cloze = clozeBuild(context.sentence, definition.source); definition.url = context.url; } } @@ -181,7 +181,7 @@ class Display { }; if (this.context) { - context.cloze = this.context.cloze; + context.sentence = this.context.sentence; context.url = this.context.url; } @@ -308,7 +308,7 @@ class Display { if (this.context && this.context.source) { const context = { url: this.context.source.url, - cloze: this.context.source.cloze, + sentence: this.context.source.sentence, index: this.context.source.index }; diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index aeeb830e..eae54f49 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -21,12 +21,12 @@ * Cloze */ -function clozeBuild(sentence, offset, source) { +function clozeBuild(sentence, source) { return { - sentence: sentence.trim(), - prefix: sentence.substring(0, offset).trim(), + sentence: sentence.text.trim(), + prefix: sentence.text.substring(0, sentence.offset).trim(), body: source.trim(), - suffix: sentence.substring(offset + source.length).trim() + suffix: sentence.text.substring(sentence.offset + source.length).trim() }; } -- cgit v1.2.3 From 1ba458ea821b9a39a2cf5eb388f81e4db4763207 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 8 Apr 2017 12:25:18 -0700 Subject: fixing cloze bug --- ext/bg/js/options.js | 3 --- ext/fg/js/util.js | 4 ++-- ext/mixed/js/display.js | 2 +- ext/mixed/js/util.js | 15 ++++++++++----- 4 files changed, 13 insertions(+), 11 deletions(-) (limited to 'ext/fg') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index ad8d83d8..51f242c2 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -368,9 +368,6 @@ function ankiFieldsPopulate(element, options) { ], 'kanji': [ 'character', - 'cloze-body', - 'cloze-prefix', - 'cloze-suffix', 'dictionary', 'glossary', 'kunyomi', diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 445efa5b..e5705ffd 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -182,7 +182,7 @@ function docSentenceExtract(source, extent) { quoteStack = []; - let endPos = content.length - 1; + let endPos = content.length; for (let i = position; i <= endPos; ++i) { const c = content[i]; @@ -205,7 +205,7 @@ function docSentenceExtract(source, extent) { } const text = content.substring(startPos, endPos); - const padding = text.length - text.replace(/^\s+/, ''); + const padding = text.length - text.replace(/^\s+/, '').length; return { text: text.trim(), diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 973b7749..64d462ae 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -108,7 +108,7 @@ class Display { if (context) { for (const definition of definitions) { - definition.cloze = clozeBuild(context.sentence, definition.source); + definition.cloze = clozeBuild(context.sentence); definition.url = context.url; } } diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index eae54f49..62838674 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -22,12 +22,17 @@ */ function clozeBuild(sentence, source) { - return { - sentence: sentence.text.trim(), - prefix: sentence.text.substring(0, sentence.offset).trim(), - body: source.trim(), - suffix: sentence.text.substring(sentence.offset + source.length).trim() + const result = { + sentence: sentence.text.trim() }; + + if (source) { + result.prefix = sentence.text.substring(0, sentence.offset).trim(); + result.body = source.trim(); + result.suffix = sentence.text.substring(sentence.offset + source.length).trim(); + } + + return result; } -- cgit v1.2.3