From e3c871bc004d9ca612d908f0f3848cdcfc286cd3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 13 Feb 2020 20:18:15 -0500 Subject: Remove unused handlebarsRenderStatic --- ext/mixed/js/api.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 0b1e7e4f..8eea0628 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -65,10 +65,6 @@ function apiNoteView(noteId) { return _apiInvoke('noteView', {noteId}); } -function apiTemplateRender(template, data, dynamic) { - return _apiInvoke('templateRender', {data, template, dynamic}); -} - function apiAudioGetUrl(definition, source, optionsContext) { return _apiInvoke('audioGetUrl', {definition, source, optionsContext}); } -- cgit v1.2.3 From 75fbb1565cb9046b83c32df5f4211a62ef355067 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 13 Feb 2020 20:26:48 -0500 Subject: Use Map for audioGetFromSources's cache parameter --- ext/mixed/js/audio.js | 9 ++++++--- ext/mixed/js/display.js | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js index 76a3e7da..47db5c75 100644 --- a/ext/mixed/js/audio.js +++ b/ext/mixed/js/audio.js @@ -114,8 +114,11 @@ function audioGetFromUrl(url, willDownload) { async function audioGetFromSources(expression, sources, optionsContext, willDownload, cache=null) { const key = `${expression.expression}:${expression.reading}`; - if (cache !== null && hasOwn(cache, expression)) { - return cache[key]; + if (cache !== null) { + const cacheValue = cache.get(expression); + if (typeof cacheValue !== 'undefined') { + return cacheValue; + } } for (let i = 0, ii = sources.length; i < ii; ++i) { @@ -133,7 +136,7 @@ async function audioGetFromSources(expression, sources, optionsContext, willDown } const result = {audio, url, source}; if (cache !== null) { - cache[key] = result; + cache.set(key, result); } return result; } catch (e) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index b18e275d..5b868df3 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -32,7 +32,7 @@ class Display { this.index = 0; this.audioPlaying = null; this.audioFallback = null; - this.audioCache = {}; + this.audioCache = new Map(); this.styleNode = null; this.eventListeners = []; -- cgit v1.2.3 From db1da33321a2517cc230c78f2b5c1ce9532aca0c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 13 Feb 2020 20:28:59 -0500 Subject: Use Array.from in toIterable --- ext/mixed/js/core.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index ca9e98e5..c273bb87 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -113,11 +113,7 @@ function toIterable(value) { if (value !== null && typeof value === 'object') { const length = value.length; if (typeof length === 'number' && Number.isFinite(length)) { - const array = []; - for (let i = 0; i < length; ++i) { - array.push(value[i]); - } - return array; + return Array.from(value); } } -- cgit v1.2.3 From 483f7401b749a38673e2abbdbb9d8d16c889d075 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 13 Feb 2020 20:42:25 -0500 Subject: Validate bounds of Display.definitions before using --- ext/mixed/js/display.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 5b868df3..00f6d512 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -179,13 +179,15 @@ class Display { e.preventDefault(); const link = e.currentTarget; const entry = link.closest('.entry'); - const definitionIndex = this.entryIndexFind(entry); + const index = this.entryIndexFind(entry); + if (index < 0 || index >= this.definitions.length) { return; } + const expressionIndex = Display.indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link); this.audioPlay( - this.definitions[definitionIndex], + this.definitions[index], // expressionIndex is used in audioPlay to detect result output mode Math.max(expressionIndex, this.options.general.resultOutputMode === 'merge' ? 0 : -1), - definitionIndex + index ); } @@ -193,6 +195,8 @@ class Display { e.preventDefault(); const link = e.currentTarget; const index = this.entryIndexFind(link); + if (index < 0 || index >= this.definitions.length) { return; } + this.noteAdd(this.definitions[index], link.dataset.mode); } @@ -512,6 +516,8 @@ class Display { } autoPlayAudio() { + if (this.definitions.length === 0) { return; } + this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0); } @@ -611,9 +617,12 @@ class Display { } noteTryAdd(mode) { - const button = this.adderButtonFind(this.index, mode); + const index = this.index; + if (index < 0 || index >= this.definitions.length) { return; } + + const button = this.adderButtonFind(index, mode); if (button !== null && !button.classList.contains('disabled')) { - this.noteAdd(this.definitions[this.index], mode); + this.noteAdd(this.definitions[index], mode); } } @@ -915,9 +924,12 @@ Display._onKeyDownHandlers = new Map([ ['P', (self, e) => { if (e.altKey) { - const entry = self.getEntry(self.index); + const index = self.index; + if (index < 0 || index >= self.definitions.length) { return; } + + const entry = self.getEntry(index); if (entry !== null && entry.dataset.type === 'term') { - self.audioPlay(self.definitions[self.index], self.firstExpressionIndex, self.index); + self.audioPlay(self.definitions[index], self.firstExpressionIndex, index); } return true; } -- cgit v1.2.3 From 61c6a753ce964700e3ef9f6db6bb1137204caf46 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 14 Feb 2020 20:11:40 -0500 Subject: Add apiTemplateRender back to mixed api.js --- ext/mixed/js/api.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 8eea0628..715c8286 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -65,6 +65,10 @@ function apiNoteView(noteId) { return _apiInvoke('noteView', {noteId}); } +function apiTemplateRender(template, data) { + return _apiInvoke('templateRender', {data, template}); +} + function apiAudioGetUrl(definition, source, optionsContext) { return _apiInvoke('audioGetUrl', {definition, source, optionsContext}); } -- cgit v1.2.3