From 4da4827bcbcdd1ef163f635d9b29416ff272b0bb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 12:48:14 -0500 Subject: Add JSDoc type annotations to project (rebased) --- .../anki-template-renderer-content-manager.js | 23 +- ext/js/templates/sandbox/anki-template-renderer.js | 347 +++++++++++++++------ .../sandbox/template-renderer-frame-api.js | 86 ++--- .../sandbox/template-renderer-media-provider.js | 67 +++- ext/js/templates/sandbox/template-renderer.js | 100 ++++-- ext/js/templates/template-patcher.js | 18 ++ ext/js/templates/template-renderer-proxy.js | 84 ++++- 7 files changed, 546 insertions(+), 179 deletions(-) (limited to 'ext/js/templates') diff --git a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js index 596fa499..be80c211 100644 --- a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js +++ b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js @@ -16,18 +16,6 @@ * along with this program. If not, see . */ -/** - * A callback used when a media file has been loaded. - * @callback AnkiTemplateRendererContentManager.OnLoadCallback - * @param {string} url The URL of the media that was loaded. - */ - -/** - * A callback used when a media file should be unloaded. - * @callback AnkiTemplateRendererContentManager.OnUnloadCallback - * @param {boolean} fullyLoaded Whether or not the media was fully loaded. - */ - /** * The content manager which is used when generating content for Anki. */ @@ -35,12 +23,15 @@ export class AnkiTemplateRendererContentManager { /** * Creates a new instance of the class. * @param {TemplateRendererMediaProvider} mediaProvider The media provider for the object. - * @param {object} data The data object passed to the Handlebars template renderer. + * @param {import('anki-templates').NoteData} data The data object passed to the Handlebars template renderer. * See {@link AnkiNoteDataCreator.create}'s return value for structure information. */ constructor(mediaProvider, data) { + /** @type {TemplateRendererMediaProvider} */ this._mediaProvider = mediaProvider; + /** @type {import('anki-templates').NoteData} */ this._data = data; + /** @type {import('anki-template-renderer-content-manager').OnUnloadCallback[]} */ this._onUnloadCallbacks = []; } @@ -48,9 +39,9 @@ export class AnkiTemplateRendererContentManager { * Attempts to load the media file from a given dictionary. * @param {string} path The path to the media file in the dictionary. * @param {string} dictionary The name of the dictionary. - * @param {AnkiTemplateRendererContentManager.OnLoadCallback} onLoad The callback that is executed if the media was loaded successfully. + * @param {import('anki-template-renderer-content-manager').OnLoadCallback} onLoad The callback that is executed if the media was loaded successfully. * No assumptions should be made about the synchronicity of this callback. - * @param {AnkiTemplateRendererContentManager.OnUnloadCallback} onUnload The callback that is executed when the media should be unloaded. + * @param {import('anki-template-renderer-content-manager').OnUnloadCallback} onUnload The callback that is executed when the media should be unloaded. */ loadMedia(path, dictionary, onLoad, onUnload) { const imageUrl = this._mediaProvider.getMedia(this._data, ['dictionaryMedia', path], {dictionary, format: 'fileName', default: null}); @@ -73,7 +64,7 @@ export class AnkiTemplateRendererContentManager { /** * Sets up attributes and events for a link element. - * @param {Element} element The link element. + * @param {HTMLAnchorElement} element The link element. * @param {string} href The URL. * @param {boolean} internal Whether or not the URL is an internal or external link. */ diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 10f69745..ce8e3200 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -36,17 +36,29 @@ export class AnkiTemplateRenderer { * Creates a new instance of the class. */ constructor() { + /** @type {CssStyleApplier} */ this._structuredContentStyleApplier = new CssStyleApplier('/data/structured-content-style.json'); + /** @type {CssStyleApplier} */ this._pronunciationStyleApplier = new CssStyleApplier('/data/pronunciation-style.json'); + /** @type {RegExp} */ this._structuredContentDatasetKeyIgnorePattern = /^sc([^a-z]|$)/; + /** @type {JapaneseUtil} */ this._japaneseUtil = new JapaneseUtil(null); + /** @type {TemplateRenderer} */ this._templateRenderer = new TemplateRenderer(); + /** @type {AnkiNoteDataCreator} */ this._ankiNoteDataCreator = new AnkiNoteDataCreator(this._japaneseUtil); + /** @type {TemplateRendererMediaProvider} */ this._mediaProvider = new TemplateRendererMediaProvider(); + /** @type {PronunciationGenerator} */ this._pronunciationGenerator = new PronunciationGenerator(this._japaneseUtil); + /** @type {?(Map[])} */ this._stateStack = null; + /** @type {?import('anki-note-builder').Requirement[]} */ this._requirements = null; - this._cleanupCallbacks = null; + /** @type {(() => void)[]} */ + this._cleanupCallbacks = []; + /** @type {?HTMLElement} */ this._temporaryElement = null; } @@ -93,7 +105,7 @@ export class AnkiTemplateRenderer { ]); this._templateRenderer.registerDataType('ankiNote', { modifier: ({marker, commonData}) => this._ankiNoteDataCreator.create(marker, commonData), - composeData: (marker, commonData) => ({marker, commonData}) + composeData: ({marker}, commonData) => ({marker, commonData}) }); this._templateRenderer.setRenderCallbacks( this._onRenderSetup.bind(this), @@ -107,40 +119,56 @@ export class AnkiTemplateRenderer { // Private + /** + * @returns {{requirements: import('anki-note-builder').Requirement[]}} + */ _onRenderSetup() { + /** @type {import('anki-note-builder').Requirement[]} */ const requirements = []; this._stateStack = [new Map()]; this._requirements = requirements; this._mediaProvider.requirements = requirements; - this._cleanupCallbacks = []; return {requirements}; } + /** + * @returns {void} + */ _onRenderCleanup() { for (const callback of this._cleanupCallbacks) { callback(); } this._stateStack = null; this._requirements = null; this._mediaProvider.requirements = null; - this._cleanupCallbacks = null; + this._cleanupCallbacks.length = 0; } + /** + * @param {string} text + * @returns {string} + */ _escape(text) { return Handlebars.Utils.escapeExpression(text); } + /** + * + * @param text + */ _safeString(text) { return new Handlebars.SafeString(text); } // Template helpers - _dumpObject(context, object) { + /** @type {import('template-renderer').HelperFunction} */ + _dumpObject(object) { const dump = JSON.stringify(object, null, 4); return this._escape(dump); } - _furigana(context, ...args) { - const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + /** @type {import('template-renderer').HelperFunction} */ + _furigana(args, context, options) { + const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); const segs = this._japaneseUtil.distributeFurigana(expression, reading); let result = ''; @@ -157,8 +185,9 @@ export class AnkiTemplateRenderer { return this._safeString(result); } - _furiganaPlain(context, ...args) { - const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + /** @type {import('template-renderer').HelperFunction} */ + _furiganaPlain(args, context, options) { + const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options); const segs = this._japaneseUtil.distributeFurigana(expression, reading); let result = ''; @@ -174,43 +203,56 @@ export class AnkiTemplateRenderer { return result; } - _getFuriganaExpressionAndReading(context, ...args) { - if (args.length >= 3) { - return {expression: args[0], reading: args[1]}; - } else if (args.length === 2) { - const {expression, reading} = args[0]; - return {expression, reading}; + /** + * @type {import('template-renderer').HelperFunction<{expression: string, reading: string}>} + */ + _getFuriganaExpressionAndReading(args) { + let expression; + let reading; + if (args.length >= 2) { + [expression, reading] = /** @type {[expression?: string, reading?: string]} */ (args); } else { - return void 0; + ({expression, reading} = /** @type {import('core').SerializableObject} */ (args[0])); } + return { + expression: typeof expression === 'string' ? expression : '', + reading: typeof reading === 'string' ? reading : '' + }; } + /** + * + * @param string + */ _stringToMultiLineHtml(string) { return string.split('\n').join('
'); } - _multiLine(context, options) { - return this._stringToMultiLineHtml(options.fn(context)); + /** @type {import('template-renderer').HelperFunction} */ + _multiLine(_args, context, options) { + return this._stringToMultiLineHtml(this._asString(options.fn(context))); } - _regexReplace(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _regexReplace(args, context, options) { // Usage: // {{#regexReplace regex string [flags] [content]...}}content{{/regexReplace}} // regex: regular expression string // string: string to replace // flags: optional flags for regular expression // e.g. "i" for case-insensitive, "g" for replace all - const argCount = args.length - 1; - const options = args[argCount]; - let value = typeof options.fn === 'function' ? options.fn(context) : ''; + const argCount = args.length; + let value = this._asString(options.fn(context)); if (argCount > 3) { value = `${args.slice(3, -1).join('')}${value}`; } if (argCount > 1) { try { - const flags = argCount > 2 ? args[2] : 'g'; - const regex = new RegExp(args[0], flags); - value = value.replace(regex, args[1]); + const [pattern, replacement, flags] = args; + if (typeof pattern !== 'string') { throw new Error('Invalid pattern'); } + if (typeof replacement !== 'string') { throw new Error('Invalid replacement'); } + const regex = new RegExp(pattern, typeof flags === 'string' ? flags : 'g'); + value = value.replace(regex, replacement); } catch (e) { return `${e}`; } @@ -218,24 +260,26 @@ export class AnkiTemplateRenderer { return value; } - _regexMatch(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _regexMatch(args, context, options) { // Usage: // {{#regexMatch regex [flags] [content]...}}content{{/regexMatch}} // regex: regular expression string // flags: optional flags for regular expression // e.g. "i" for case-insensitive, "g" for match all - const argCount = args.length - 1; - const options = args[argCount]; - let value = typeof options.fn === 'function' ? options.fn(context) : ''; + const argCount = args.length; + let value = this._asString(options.fn(context)); if (argCount > 2) { value = `${args.slice(2, -1).join('')}${value}`; } if (argCount > 0) { try { - const flags = argCount > 1 ? args[1] : ''; - const regex = new RegExp(args[0], flags); + const [pattern, flags] = args; + if (typeof pattern !== 'string') { throw new Error('Invalid pattern'); } + const regex = new RegExp(pattern, typeof flags === 'string' ? flags : ''); + /** @type {string[]} */ const parts = []; - value.replace(regex, (g0) => parts.push(g0)); + value.replace(regex, (g0) => { parts.push(g0); return g0; }); value = parts.join(''); } catch (e) { return `${e}`; @@ -244,11 +288,18 @@ export class AnkiTemplateRenderer { return value; } - _mergeTags(context, object, isGroupMode, isMergeMode) { + /** + * @type {import('template-renderer').HelperFunction} + */ + _mergeTags(args) { + const [object, isGroupMode, isMergeMode] = /** @type {[object: import('anki-templates').TermDictionaryEntry, isGroupMode: boolean, isMergeMode: boolean]} */ (args); const tagSources = []; if (isGroupMode || isMergeMode) { - for (const definition of object.definitions) { - tagSources.push(definition.definitionTags); + const {definitions} = object; + if (Array.isArray(definitions)) { + for (const definition of definitions) { + tagSources.push(definition.definitionTags); + } } } else { tagSources.push(object.definitionTags); @@ -265,7 +316,9 @@ export class AnkiTemplateRenderer { return [...tags].join(', '); } - _eachUpTo(context, iterable, maxCount, options) { + /** @type {import('template-renderer').HelperFunction} */ + _eachUpTo(args, context, options) { + const [iterable, maxCount] = /** @type {[iterable: Iterable, maxCount: number]} */ (args); if (iterable) { const results = []; let any = false; @@ -279,14 +332,15 @@ export class AnkiTemplateRenderer { return results.join(''); } } - return options.inverse(context); + return this._asString(options.inverse(context)); } - _spread(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _spread(args) { const result = []; - for (let i = 0, ii = args.length - 1; i < ii; ++i) { + for (const array of /** @type {Iterable[]} */ (args)) { try { - result.push(...args[i]); + result.push(...array); } catch (e) { // NOP } @@ -294,15 +348,22 @@ export class AnkiTemplateRenderer { return result; } - _op(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _op(args) { + const [operator] = /** @type {[operator: string, operand1: import('core').SafeAny, operand2?: import('core').SafeAny, operand3?: import('core').SafeAny]} */ (args); switch (args.length) { - case 3: return this._evaluateUnaryExpression(args[0], args[1]); - case 4: return this._evaluateBinaryExpression(args[0], args[1], args[2]); - case 5: return this._evaluateTernaryExpression(args[0], args[1], args[2], args[3]); + case 2: return this._evaluateUnaryExpression(operator, args[1]); + case 3: return this._evaluateBinaryExpression(operator, args[1], args[2]); + case 4: return this._evaluateTernaryExpression(operator, args[1], args[2], args[3]); default: return void 0; } } + /** + * @param {string} operator + * @param {import('core').SafeAny} operand1 + * @returns {unknown} + */ _evaluateUnaryExpression(operator, operand1) { switch (operator) { case '+': return +operand1; @@ -313,6 +374,12 @@ export class AnkiTemplateRenderer { } } + /** + * @param {string} operator + * @param {import('core').SafeAny} operand1 + * @param {import('core').SafeAny} operand2 + * @returns {unknown} + */ _evaluateBinaryExpression(operator, operand1, operand2) { switch (operator) { case '+': return operand1 + operand2; @@ -341,6 +408,13 @@ export class AnkiTemplateRenderer { } } + /** + * @param {string} operator + * @param {import('core').SafeAny} operand1 + * @param {import('core').SafeAny} operand2 + * @param {import('core').SafeAny} operand3 + * @returns {unknown} + */ _evaluateTernaryExpression(operator, operand1, operand2, operand3) { switch (operator) { case '?:': return operand1 ? operand2 : operand3; @@ -348,8 +422,11 @@ export class AnkiTemplateRenderer { } } - _get(context, key) { + /** @type {import('template-renderer').HelperFunction} */ + _get(args) { + const [key] = /** @type {[key: string]} */ (args); const stateStack = this._stateStack; + if (stateStack === null) { throw new Error('Invalid state'); } for (let i = stateStack.length; --i >= 0;) { const map = stateStack[i]; if (map.has(key)) { @@ -359,19 +436,21 @@ export class AnkiTemplateRenderer { return void 0; } - _set(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _set(args, context, options) { const stateStack = this._stateStack; + if (stateStack === null) { throw new Error('Invalid state'); } switch (args.length) { - case 2: + case 1: { - const [key, options] = args; + const [key] = /** @type {[key: string]} */ (args); const value = options.fn(context); stateStack[stateStack.length - 1].set(key, value); } break; - case 3: + case 2: { - const [key, value] = args; + const [key, value] = /** @type {[key: string, value: unknown]} */ (args); stateStack[stateStack.length - 1].set(key, value); } break; @@ -379,8 +458,10 @@ export class AnkiTemplateRenderer { return ''; } - _scope(context, options) { + /** @type {import('template-renderer').HelperFunction} */ + _scope(_args, context, options) { const stateStack = this._stateStack; + if (stateStack === null) { throw new Error('Invalid state'); } try { stateStack.push(new Map()); return options.fn(context); @@ -391,14 +472,25 @@ export class AnkiTemplateRenderer { } } - _property(context, ...args) { - const ii = args.length - 1; + /** @type {import('template-renderer').HelperFunction} */ + _property(args) { + const ii = args.length; if (ii <= 0) { return void 0; } try { let value = args[0]; for (let i = 1; i < ii; ++i) { - value = value[args[i]]; + if (typeof value !== 'object' || value === null) { throw new Error('Invalid object'); } + const key = args[i]; + switch (typeof key) { + case 'number': + case 'string': + case 'symbol': + break; + default: + throw new Error('Invalid key'); + } + value = /** @type {import('core').UnknownObject} */ (value)[key]; } return value; } catch (e) { @@ -406,38 +498,51 @@ export class AnkiTemplateRenderer { } } - _noop(context, options) { + /** @type {import('template-renderer').HelperFunction} */ + _noop(_args, context, options) { return options.fn(context); } - _isMoraPitchHigh(context, index, position) { + /** @type {import('template-renderer').HelperFunction} */ + _isMoraPitchHigh(args) { + const [index, position] = /** @type {[index: number, position: number]} */ (args); return this._japaneseUtil.isMoraPitchHigh(index, position); } - _getKanaMorae(context, text) { + /** @type {import('template-renderer').HelperFunction} */ + _getKanaMorae(args) { + const [text] = /** @type {[text: string]} */ (args); return this._japaneseUtil.getKanaMorae(`${text}`); } - _getTypeof(context, ...args) { - const ii = args.length - 1; - const value = (ii > 0 ? args[0] : args[ii].fn(context)); + /** @type {import('template-renderer').HelperFunction} */ + _getTypeof(args, context, options) { + const ii = args.length; + const value = (ii > 0 ? args[0] : options.fn(context)); return typeof value; } - _join(context, ...args) { - return args.length > 1 ? args.slice(1, args.length - 1).flat().join(args[0]) : ''; + /** @type {import('template-renderer').HelperFunction} */ + _join(args) { + return args.length > 0 ? args.slice(1, args.length).flat().join(/** @type {string} */ (args[0])) : ''; } - _concat(context, ...args) { + /** @type {import('template-renderer').HelperFunction} */ + _concat(args) { let result = ''; - for (let i = 0, ii = args.length - 1; i < ii; ++i) { + for (let i = 0, ii = args.length; i < ii; ++i) { result += args[i]; } return result; } - _pitchCategories(context, data) { - const {pronunciations, headwords} = data.dictionaryEntry; + /** @type {import('template-renderer').HelperFunction} */ + _pitchCategories(args) { + const [data] = /** @type {[data: import('anki-templates').NoteData]} */ (args); + const {dictionaryEntry} = data; + if (dictionaryEntry.type !== 'term') { return []; } + const {pronunciations, headwords} = dictionaryEntry; + /** @type {Set} */ const categories = new Set(); for (const {headwordIndex, pitches} of pronunciations) { const {reading, wordClasses} = headwords[headwordIndex]; @@ -452,6 +557,9 @@ export class AnkiTemplateRenderer { return [...categories]; } + /** + * @returns {HTMLElement} + */ _getTemporaryElement() { let element = this._temporaryElement; if (element === null) { @@ -461,14 +569,28 @@ export class AnkiTemplateRenderer { return element; } + /** + * @param {Element} node + * @returns {string} + */ _getStructuredContentHtml(node) { return this._getHtml(node, this._structuredContentStyleApplier, this._structuredContentDatasetKeyIgnorePattern); } + /** + * @param {Element} node + * @returns {string} + */ _getPronunciationHtml(node) { return this._getHtml(node, this._pronunciationStyleApplier, null); } + /** + * @param {Element} node + * @param {CssStyleApplier} styleApplier + * @param {?RegExp} datasetKeyIgnorePattern + * @returns {string} + */ _getHtml(node, styleApplier, datasetKeyIgnorePattern) { const container = this._getTemporaryElement(); container.appendChild(node); @@ -478,20 +600,27 @@ export class AnkiTemplateRenderer { return this._safeString(result); } + /** + * @param {Element} root + * @param {CssStyleApplier} styleApplier + * @param {?RegExp} datasetKeyIgnorePattern + */ _normalizeHtml(root, styleApplier, datasetKeyIgnorePattern) { const {ELEMENT_NODE, TEXT_NODE} = Node; const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT); + /** @type {HTMLElement[]} */ const elements = []; + /** @type {Text[]} */ const textNodes = []; while (true) { const node = treeWalker.nextNode(); if (node === null) { break; } switch (node.nodeType) { case ELEMENT_NODE: - elements.push(node); + elements.push(/** @type {HTMLElement} */ (node)); break; case TEXT_NODE: - textNodes.push(node); + textNodes.push(/** @type {Text} */ (node)); break; } } @@ -508,8 +637,11 @@ export class AnkiTemplateRenderer { } } + /** + * @param {Text} textNode + */ _replaceNewlines(textNode) { - const parts = textNode.nodeValue.split('\n'); + const parts = /** @type {string} */ (textNode.nodeValue).split('\n'); if (parts.length <= 1) { return; } const {parentNode} = textNode; if (parentNode === null) { return; } @@ -521,6 +653,10 @@ export class AnkiTemplateRenderer { parentNode.replaceChild(fragment, textNode); } + /** + * @param {import('anki-templates').NoteData} data + * @returns {StructuredContentGenerator} + */ _createStructuredContentGenerator(data) { const contentManager = new AnkiTemplateRendererContentManager(this._mediaProvider, data); const instance = new StructuredContentGenerator(contentManager, this._japaneseUtil, document); @@ -528,44 +664,65 @@ export class AnkiTemplateRenderer { return instance; } - _formatGlossary(context, dictionary, content, options) { + /** + * @type {import('template-renderer').HelperFunction} + */ + _formatGlossary(args, context, options) { + const [dictionary] = /** @type {[dictionary: string]} */ (args); const data = options.data.root; + const content = /** @type {import('dictionary-data').TermGlossary} */ (args[0]); if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } if (!(typeof content === 'object' && content !== null)) { return ''; } switch (content.type) { case 'image': return this._formatGlossaryImage(content, dictionary, data); case 'structured-content': return this._formatStructuredContent(content, dictionary, data); + case 'text': return this._stringToMultiLineHtml(this._escape(content.text)); } return ''; } + /** + * @param {import('dictionary-data').TermGlossaryImage} content + * @param {string} dictionary + * @param {import('anki-templates').NoteData} data + * @returns {string} + */ _formatGlossaryImage(content, dictionary, data) { const structuredContentGenerator = this._createStructuredContentGenerator(data); const node = structuredContentGenerator.createDefinitionImage(content, dictionary); return this._getStructuredContentHtml(node); } + /** + * @param {import('dictionary-data').TermGlossaryStructuredContent} content + * @param {string} dictionary + * @param {import('anki-templates').NoteData} data + * @returns {string} + */ _formatStructuredContent(content, dictionary, data) { const structuredContentGenerator = this._createStructuredContentGenerator(data); const node = structuredContentGenerator.createStructuredContent(content.content, dictionary); return node !== null ? this._getStructuredContentHtml(node) : ''; } - _hasMedia(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; - return this._mediaProvider.hasMedia(options.data.root, args.slice(0, ii), options.hash); + /** + * @type {import('template-renderer').HelperFunction} + */ + _hasMedia(args, _context, options) { + return this._mediaProvider.hasMedia(options.data.root, args, options.hash); } - _getMedia(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; - return this._mediaProvider.getMedia(options.data.root, args.slice(0, ii), options.hash); + /** + * @type {import('template-renderer').HelperFunction} + */ + _getMedia(args, _context, options) { + return this._mediaProvider.getMedia(options.data.root, args, options.hash); } - _pronunciation(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; + /** + * @type {import('template-renderer').HelperFunction} + */ + _pronunciation(_args, _context, options) { let {format, reading, downstepPosition, nasalPositions, devoicePositions} = options.hash; if (typeof reading !== 'string' || reading.length === 0) { return ''; } @@ -586,18 +743,30 @@ export class AnkiTemplateRenderer { } } - _hiragana(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; + /** + * @type {import('template-renderer').HelperFunction} + */ + _hiragana(args, context, options) { + const ii = args.length; const {keepProlongedSoundMarks} = options.hash; const value = (ii > 0 ? args[0] : options.fn(context)); - return this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true); + return typeof value === 'string' ? this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true) : ''; } - _katakana(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; + /** + * @type {import('template-renderer').HelperFunction} + */ + _katakana(args, context, options) { + const ii = args.length; const value = (ii > 0 ? args[0] : options.fn(context)); - return this._japaneseUtil.convertHiraganaToKatakana(value); + return typeof value === 'string' ? this._japaneseUtil.convertHiraganaToKatakana(value) : ''; + } + + /** + * @param {unknown} value + * @returns {string} + */ + _asString(value) { + return typeof value === 'string' ? value : `${value}`; } } diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js index 7ce2d909..31ba4500 100644 --- a/ext/js/templates/sandbox/template-renderer-frame-api.js +++ b/ext/js/templates/sandbox/template-renderer-frame-api.js @@ -17,15 +17,23 @@ */ export class TemplateRendererFrameApi { + /** + * @param {TemplateRenderer} templateRenderer + */ constructor(templateRenderer) { + /** @type {TemplateRenderer} */ this._templateRenderer = templateRenderer; - this._windowMessageHandlers = new Map([ + /** @type {import('core').MessageHandlerMap} */ + this._windowMessageHandlers = new Map(/** @type {import('core').MessageHandlerArray} */ ([ ['render', {async: false, handler: this._onRender.bind(this)}], ['renderMulti', {async: false, handler: this._onRenderMulti.bind(this)}], ['getModifiedData', {async: false, handler: this._onGetModifiedData.bind(this)}] - ]); + ])); } + /** + * @returns {void} + */ prepare() { window.addEventListener('message', this._onWindowMessage.bind(this), false); this._postMessage(window.parent, 'ready', {}, null); @@ -33,14 +41,24 @@ export class TemplateRendererFrameApi { // Private + /** + * @param {MessageEvent} e + */ _onWindowMessage(e) { const {source, data: {action, params, id}} = e; const messageHandler = this._windowMessageHandlers.get(action); if (typeof messageHandler === 'undefined') { return; } - this._onWindowMessageInner(messageHandler, action, params, source, id); + this._onWindowMessageInner(messageHandler, action, params, /** @type {Window} */ (source), id); } + /** + * @param {import('core').MessageHandlerDetails} handlerItem + * @param {string} action + * @param {import('core').SerializableObject} params + * @param {Window} source + * @param {?string} id + */ async _onWindowMessageInner({handler, async}, action, params, source, id) { let response; try { @@ -50,64 +68,54 @@ export class TemplateRendererFrameApi { } response = {result}; } catch (error) { - response = {error: this._serializeError(error)}; + response = {error: ExtensionError.serialize(error)}; } if (typeof id === 'undefined') { return; } this._postMessage(source, `${action}.response`, response, id); } + /** + * @param {{template: string, data: import('template-renderer').PartialOrCompositeRenderData, type: import('anki-templates').RenderMode}} event + * @returns {import('template-renderer').RenderResult} + */ _onRender({template, data, type}) { return this._templateRenderer.render(template, data, type); } + /** + * @param {{items: import('template-renderer').RenderMultiItem[]}} event + * @returns {import('core').Response[]} + */ _onRenderMulti({items}) { - return this._serializeMulti(this._templateRenderer.renderMulti(items)); + return this._templateRenderer.renderMulti(items); } + /** + * @param {{data: import('template-renderer').CompositeRenderData, type: import('anki-templates').RenderMode}} event + * @returns {import('anki-templates').NoteData} + */ _onGetModifiedData({data, type}) { const result = this._templateRenderer.getModifiedData(data, type); return this._clone(result); } - _serializeError(error) { - try { - if (typeof error === 'object' && error !== null) { - const result = { - name: error.name, - message: error.message, - stack: error.stack - }; - if (Object.prototype.hasOwnProperty.call(error, 'data')) { - result.data = error.data; - } - return result; - } - } catch (e) { - // NOP - } - return { - value: error, - hasValue: true - }; - } - - _serializeMulti(array) { - for (let i = 0, ii = array.length; i < ii; ++i) { - const value = array[i]; - const {error} = value; - if (typeof error !== 'undefined') { - value.error = this._serializeError(error); - } - } - return array; - } - + /** + * @template [T=unknown] + * @param {T} value + * @returns {T} + */ _clone(value) { return JSON.parse(JSON.stringify(value)); } + /** + * @param {Window} target + * @param {string} action + * @param {import('core').SerializableObject} params + * @param {?string} id + */ _postMessage(target, action, params, id) { - return target.postMessage({action, params, id}, '*'); + target.postMessage(/** @type {import('template-renderer-frame-api').MessageData} */ ({action, params, id}), '*'); } } diff --git a/ext/js/templates/sandbox/template-renderer-media-provider.js b/ext/js/templates/sandbox/template-renderer-media-provider.js index 33ddec21..d8a0a16d 100644 --- a/ext/js/templates/sandbox/template-renderer-media-provider.js +++ b/ext/js/templates/sandbox/template-renderer-media-provider.js @@ -20,9 +20,11 @@ import {Handlebars} from '../../../lib/handlebars.js'; export class TemplateRendererMediaProvider { constructor() { + /** @type {?import('anki-note-builder').Requirement[]} */ this._requirements = null; } + /** @type {?import('anki-note-builder').Requirement[]} */ get requirements() { return this._requirements; } @@ -31,12 +33,24 @@ export class TemplateRendererMediaProvider { this._requirements = value; } + /** + * @param {import('anki-templates').NoteData} root + * @param {unknown[]} args + * @param {import('core').SerializableObject} namedArgs + * @returns {boolean} + */ hasMedia(root, args, namedArgs) { const {media} = root; const data = this._getMediaData(media, args, namedArgs); return (data !== null); } + /** + * @param {import('anki-templates').NoteData} root + * @param {unknown[]} args + * @param {import('core').SerializableObject} namedArgs + * @returns {?string} + */ getMedia(root, args, namedArgs) { const {media} = root; const data = this._getMediaData(media, args, namedArgs); @@ -45,16 +59,24 @@ export class TemplateRendererMediaProvider { if (typeof result === 'string') { return result; } } const defaultValue = namedArgs.default; - return typeof defaultValue !== 'undefined' ? defaultValue : ''; + return defaultValue === null || typeof defaultValue === 'string' ? defaultValue : ''; } // Private + /** + * @param {import('anki-note-builder').Requirement} value + */ _addRequirement(value) { if (this._requirements === null) { return; } this._requirements.push(value); } + /** + * @param {import('anki-templates').MediaObject} data + * @param {import('core').SerializableObject} namedArgs + * @returns {string} + */ _getFormattedValue(data, namedArgs) { let {value} = data; const {escape=true} = namedArgs; @@ -64,6 +86,12 @@ export class TemplateRendererMediaProvider { return value; } + /** + * @param {import('anki-templates').Media} media + * @param {unknown[]} args + * @param {import('core').SerializableObject} namedArgs + * @returns {?(import('anki-templates').MediaObject)} + */ _getMediaData(media, args, namedArgs) { const type = args[0]; switch (type) { @@ -78,6 +106,11 @@ export class TemplateRendererMediaProvider { } } + /** + * @param {import('anki-templates').Media} media + * @param {import('anki-templates').MediaSimpleType} type + * @returns {?import('anki-templates').MediaObject} + */ _getSimpleMediaData(media, type) { const result = media[type]; if (typeof result === 'object' && result !== null) { return result; } @@ -85,12 +118,19 @@ export class TemplateRendererMediaProvider { return null; } + /** + * @param {import('anki-templates').Media} media + * @param {unknown} path + * @param {import('core').SerializableObject} namedArgs + * @returns {?import('anki-templates').MediaObject} + */ _getDictionaryMedia(media, path, namedArgs) { + if (typeof path !== 'string') { return null; } const {dictionaryMedia} = media; const {dictionary} = namedArgs; + if (typeof dictionary !== 'string') { return null; } if ( typeof dictionaryMedia !== 'undefined' && - typeof dictionary === 'string' && Object.prototype.hasOwnProperty.call(dictionaryMedia, dictionary) ) { const dictionaryMedia2 = dictionaryMedia[dictionary]; @@ -109,8 +149,15 @@ export class TemplateRendererMediaProvider { return null; } + /** + * @param {import('anki-templates').Media} media + * @param {unknown} text + * @param {import('core').SerializableObject} namedArgs + * @returns {?import('anki-templates').MediaObject} + */ _getTextFurigana(media, text, namedArgs) { - const {readingMode=null} = namedArgs; + if (typeof text !== 'string') { return null; } + const readingMode = this._normalizeReadingMode(namedArgs.readingMode); const {textFurigana} = media; if (Array.isArray(textFurigana)) { for (const entry of textFurigana) { @@ -125,4 +172,18 @@ export class TemplateRendererMediaProvider { }); return null; } + + /** + * @param {unknown} value + * @returns {?import('anki-templates').TextFuriganaReadingMode} + */ + _normalizeReadingMode(value) { + switch (value) { + case 'hiragana': + case 'katakana': + return value; + default: + return null; + } + } } diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index 8d8a2765..c7613533 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -20,47 +20,74 @@ import {Handlebars} from '../../../lib/handlebars.js'; export class TemplateRenderer { constructor() { + /** @type {Map>} */ this._cache = new Map(); + /** @type {number} */ this._cacheMaxSize = 5; + /** @type {Map} */ this._dataTypes = new Map(); + /** @type {?((noteData: import('anki-templates').NoteData) => import('template-renderer').SetupCallbackResult)} */ this._renderSetup = null; + /** @type {?((noteData: import('anki-templates').NoteData) => import('template-renderer').CleanupCallbackResult)} */ this._renderCleanup = null; } + /** + * @param {import('template-renderer').HelperFunctionsDescriptor} helpers + */ registerHelpers(helpers) { for (const [name, helper] of helpers) { this._registerHelper(name, helper); } } - registerDataType(name, {modifier=null, composeData=null}) { + /** + * @param {import('anki-templates').RenderMode} name + * @param {import('template-renderer').DataType} details + */ + registerDataType(name, {modifier, composeData}) { this._dataTypes.set(name, {modifier, composeData}); } + /** + * @param {?((noteData: import('anki-templates').NoteData) => import('template-renderer').SetupCallbackResult)} setup + * @param {?((noteData: import('anki-templates').NoteData) => import('template-renderer').CleanupCallbackResult)} cleanup + */ setRenderCallbacks(setup, cleanup) { this._renderSetup = setup; this._renderCleanup = cleanup; } + /** + * @param {string} template + * @param {import('template-renderer').PartialOrCompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {import('template-renderer').RenderResult} + */ render(template, data, type) { const instance = this._getTemplateInstance(template); - data = this._getModifiedData(data, void 0, type); - return this._renderTemplate(instance, data); + const modifiedData = this._getModifiedData(data, void 0, type); + return this._renderTemplate(instance, modifiedData); } + /** + * @param {import('template-renderer').RenderMultiItem[]} items + * @returns {import('core').Response[]} + */ renderMulti(items) { + /** @type {import('core').Response[]} */ const results = []; for (const {template, templateItems} of items) { const instance = this._getTemplateInstance(template); for (const {type, commonData, datas} of templateItems) { - for (let data of datas) { + for (const data of datas) { let result; try { - data = this._getModifiedData(data, commonData, type); - result = this._renderTemplate(instance, data); - result = {result}; + const data2 = this._getModifiedData(data, commonData, type); + const renderResult = this._renderTemplate(instance, data2); + result = {result: renderResult}; } catch (error) { - result = {error}; + result = {error: ExtensionError.serialize(error)}; } results.push(result); } @@ -69,29 +96,46 @@ export class TemplateRenderer { return results; } + /** + * @param {import('template-renderer').CompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {import('anki-templates').NoteData} + */ getModifiedData(data, type) { return this._getModifiedData(data, void 0, type); } // Private + /** + * @param {string} template + * @returns {HandlebarsTemplateDelegate} + */ _getTemplateInstance(template) { const cache = this._cache; let instance = cache.get(template); if (typeof instance === 'undefined') { this._updateCacheSize(this._cacheMaxSize - 1); - instance = Handlebars.compileAST(template); + instance = /** @type {HandlebarsTemplateDelegate} */ (Handlebars.compileAST(template)); cache.set(template, instance); } return instance; } + /** + * @param {HandlebarsTemplateDelegate} instance + * @param {import('anki-templates').NoteData} data + * @returns {import('template-renderer').RenderResult} + */ _renderTemplate(instance, data) { const renderSetup = this._renderSetup; const renderCleanup = this._renderCleanup; + /** @type {string} */ let result; + /** @type {?import('template-renderer').SetupCallbackResult} */ let additions1; + /** @type {?import('template-renderer').CleanupCallbackResult} */ let additions2; try { additions1 = (typeof renderSetup === 'function' ? renderSetup(data) : null); @@ -99,28 +143,36 @@ export class TemplateRenderer { } finally { additions2 = (typeof renderCleanup === 'function' ? renderCleanup(data) : null); } - return Object.assign({result}, additions1, additions2); + return /** @type {import('template-renderer').RenderResult} */ (Object.assign({result}, additions1, additions2)); } + /** + * @param {import('template-renderer').PartialOrCompositeRenderData} data + * @param {import('anki-note-builder').CommonData|undefined} commonData + * @param {import('anki-templates').RenderMode} type + * @returns {import('anki-templates').NoteData} + * @throws {Error} + */ _getModifiedData(data, commonData, type) { if (typeof type === 'string') { const typeInfo = this._dataTypes.get(type); if (typeof typeInfo !== 'undefined') { if (typeof commonData !== 'undefined') { const {composeData} = typeInfo; - if (typeof composeData === 'function') { - data = composeData(data, commonData); - } + data = composeData(data, commonData); + } else if (typeof data.commonData === 'undefined') { + throw new Error('Incomplete data'); } const {modifier} = typeInfo; - if (typeof modifier === 'function') { - data = modifier(data); - } + return modifier(/** @type {import('template-renderer').CompositeRenderData} */ (data)); } } - return data; + throw new Error(`Invalid type: ${type}`); } + /** + * @param {number} maxSize + */ _updateCacheSize(maxSize) { const cache = this._cache; let removeCount = cache.size - maxSize; @@ -132,9 +184,21 @@ export class TemplateRenderer { } } + /** + * @param {string} name + * @param {import('template-renderer').HelperFunction} helper + */ _registerHelper(name, helper) { + /** + * @this {unknown} + * @param {unknown[]} args + * @returns {unknown} + */ function wrapper(...args) { - return helper(this, ...args); + const argCountM1 = Math.max(0, args.length - 1); + const options = /** @type {Handlebars.HelperOptions} */ (args[argCountM1]); + args.length = argCountM1; + return helper(args, this, options); } Handlebars.registerHelper(name, wrapper); } diff --git a/ext/js/templates/template-patcher.js b/ext/js/templates/template-patcher.js index 6bc012bc..33abb08e 100644 --- a/ext/js/templates/template-patcher.js +++ b/ext/js/templates/template-patcher.js @@ -18,12 +18,20 @@ export class TemplatePatcher { constructor() { + /** @type {RegExp} */ this._diffPattern1 = /\n?\{\{<<<<<<<\}\}\n/g; + /** @type {RegExp} */ this._diffPattern2 = /\n\{\{=======\}\}\n/g; + /** @type {RegExp} */ this._diffPattern3 = /\n\{\{>>>>>>>\}\}\n*/g; + /** @type {RegExp} */ this._lookupMarkerPattern = /[ \t]*\{\{~?>\s*\(\s*lookup\s*\.\s*"marker"\s*\)\s*~?\}\}/g; } + /** + * @param {string} content + * @returns {import('template-patcher').Patch} + */ parsePatch(content) { const diffPattern1 = this._diffPattern1; const diffPattern2 = this._diffPattern2; @@ -61,6 +69,11 @@ export class TemplatePatcher { return {addition: content, modifications}; } + /** + * @param {string} template + * @param {import('template-patcher').Patch} patch + * @returns {string} + */ applyPatch(template, patch) { for (const {current, replacement} of patch.modifications) { let fromIndex = 0; @@ -77,6 +90,11 @@ export class TemplatePatcher { // Private + /** + * @param {string} template + * @param {string} addition + * @returns {string} + */ _addFieldTemplatesBeforeEnd(template, addition) { if (addition.length === 0) { return template; } const newline = '\n'; diff --git a/ext/js/templates/template-renderer-proxy.js b/ext/js/templates/template-renderer-proxy.js index c46d05f9..6d019d14 100644 --- a/ext/js/templates/template-renderer-proxy.js +++ b/ext/js/templates/template-renderer-proxy.js @@ -20,31 +20,55 @@ import {deserializeError, generateId} from '../core.js'; export class TemplateRendererProxy { constructor() { + /** @type {?HTMLIFrameElement} */ this._frame = null; + /** @type {boolean} */ this._frameNeedsLoad = true; + /** @type {boolean} */ this._frameLoading = false; + /** @type {?Promise} */ this._frameLoadPromise = null; + /** @type {string} */ this._frameUrl = chrome.runtime.getURL('/template-renderer.html'); + /** @type {Set<{cancel: () => void}>} */ this._invocations = new Set(); } + /** + * @param {string} template + * @param {import('template-renderer').PartialOrCompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {Promise} + */ async render(template, data, type) { await this._prepareFrame(); - return await this._invoke('render', {template, data, type}); + return /** @type {import('template-renderer').RenderResult} */ (await this._invoke('render', {template, data, type})); } + /** + * @param {import('template-renderer').RenderMultiItem[]} items + * @returns {Promise[]>} + */ async renderMulti(items) { await this._prepareFrame(); - return await this._invoke('renderMulti', {items}); + return /** @type {import('core').Response[]} */ (await this._invoke('renderMulti', {items})); } + /** + * @param {import('template-renderer').CompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {Promise} + */ async getModifiedData(data, type) { await this._prepareFrame(); - return await this._invoke('getModifiedData', {data, type}); + return /** @type {import('anki-templates').NoteData} */ (await this._invoke('getModifiedData', {data, type})); } // Private + /** + * @returns {Promise} + */ async _prepareFrame() { if (this._frame === null) { this._frame = document.createElement('iframe'); @@ -68,6 +92,12 @@ export class TemplateRendererProxy { await this._frameLoadPromise; } + /** + * @param {HTMLIFrameElement} frame + * @param {string} url + * @param {number} [timeout] + * @returns {Promise} + */ _loadFrame(frame, url, timeout=5000) { return new Promise((resolve, reject) => { let state = 0x0; // 0x1 = frame added; 0x2 = frame loaded; 0x4 = frame ready @@ -79,6 +109,9 @@ export class TemplateRendererProxy { timer = null; } }; + /** + * @param {number} flags + */ const updateState = (flags) => { state |= flags; if (state !== 0x7) { return; } @@ -89,16 +122,20 @@ export class TemplateRendererProxy { if ((state & 0x3) !== 0x1) { return; } updateState(0x2); }; + /** + * @param {MessageEvent} e + */ const onWindowMessage = (e) => { if ((state & 0x5) !== 0x1) { return; } const frameWindow = frame.contentWindow; if (frameWindow === null || frameWindow !== e.source) { return; } const {data} = e; - if (!(typeof data === 'object' && data !== null && data.action === 'ready')) { return; } + if (!(typeof data === 'object' && data !== null && /** @type {import('core').SerializableObject} */ (data).action === 'ready')) { return; } updateState(0x4); }; - let timer = setTimeout(() => { + /** @type {?number} */ + let timer = window.setTimeout(() => { timer = null; cleanup(); reject(new Error('Timeout')); @@ -111,7 +148,9 @@ export class TemplateRendererProxy { try { document.body.appendChild(frame); state = 0x1; - frame.contentDocument.location.href = url; + const {contentDocument} = frame; + if (contentDocument === null) { throw new Error('Failed to initialize frame URL'); } + contentDocument.location.href = url; } catch (e) { cleanup(); reject(e); @@ -119,6 +158,12 @@ export class TemplateRendererProxy { }); } + /** + * @param {string} action + * @param {import('core').SerializableObject} params + * @param {?number} [timeout] + * @returns {Promise} + */ _invoke(action, params, timeout=null) { return new Promise((resolve, reject) => { const frameWindow = (this._frame !== null ? this._frame.contentWindow : null); @@ -143,22 +188,30 @@ export class TemplateRendererProxy { timer = null; } }; - const onMessage = (e) => { + /** + * @param {MessageEvent} event + */ + const onMessage = (event) => { + if (event.source !== frameWindow) { return; } + const {data} = event; if ( - e.source !== frameWindow || - e.data.id !== id || - e.data.action !== `${action}.response` + typeof data !== 'object' || + data === null || + /** @type {import('core').SerializableObject} */ (data).id !== id || + /** @type {import('core').SerializableObject} */ (data).action !== `${action}.response` ) { return; } - const response = e.data.params; + const response = /** @type {import('core').SerializableObject} */ (data).params; + if (typeof response !== 'object' || response === null) { return; } + cleanup(); - const {error} = response; + const {error} = /** @type {import('core').Response} */ (response); if (error) { - reject(deserializeError(error)); + reject(ExtensionError.deserialize(error)); } else { - resolve(response.result); + resolve(/** @type {import('core').Response} */ (response).result); } }; @@ -174,6 +227,9 @@ export class TemplateRendererProxy { }); } + /** + * @returns {void} + */ _onFrameLoad() { if (this._frameLoading) { return; } this._frameNeedsLoad = true; -- cgit v1.2.3 From aabd761ee9064f6a46703f234e016f31f6441fa0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 13:36:04 -0500 Subject: Remove unneeded references --- ext/js/accessibility/accessibility-controller.js | 5 ++--- ext/js/accessibility/google-docs-util.js | 1 - ext/js/app/frontend.js | 6 ++---- ext/js/app/popup-proxy.js | 14 ++++++-------- ext/js/app/popup-window.js | 9 ++++----- ext/js/background/backend.js | 1 - ext/js/comm/anki-connect.js | 1 - ext/js/dom/document-util.js | 1 - ext/js/dom/text-source-element.js | 1 - ext/js/dom/text-source-range.js | 1 - ext/js/language/dictionary-worker-handler.js | 1 - ext/js/language/dictionary-worker-media-loader.js | 2 +- ext/js/language/translator.js | 1 - ext/js/templates/template-renderer-proxy.js | 2 +- 14 files changed, 16 insertions(+), 30 deletions(-) (limited to 'ext/js/templates') diff --git a/ext/js/accessibility/accessibility-controller.js b/ext/js/accessibility/accessibility-controller.js index a4239947..8250b369 100644 --- a/ext/js/accessibility/accessibility-controller.js +++ b/ext/js/accessibility/accessibility-controller.js @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import {ScriptManager} from '../background/script-manager.js'; import {log} from '../core.js'; /** @@ -25,10 +24,10 @@ import {log} from '../core.js'; export class AccessibilityController { /** * Creates a new instance. - * @param {ScriptManager} scriptManager An instance of the `ScriptManager` class. + * @param {import('../background/script-manager.js').ScriptManager} scriptManager An instance of the `ScriptManager` class. */ constructor(scriptManager) { - /** @type {ScriptManager} */ + /** @type {import('../background/script-manager.js').ScriptManager} */ this._scriptManager = scriptManager; /** @type {?import('core').TokenObject} */ this._updateGoogleDocsAccessibilityToken = null; diff --git a/ext/js/accessibility/google-docs-util.js b/ext/js/accessibility/google-docs-util.js index 9db45cc1..4321f082 100644 --- a/ext/js/accessibility/google-docs-util.js +++ b/ext/js/accessibility/google-docs-util.js @@ -17,7 +17,6 @@ */ import {DocumentUtil} from '../dom/document-util.js'; -import {TextSourceElement} from '../dom/text-source-element.js'; import {TextSourceRange} from '../dom/text-source-range.js'; /** diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index fec933f8..628c504e 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -21,10 +21,8 @@ import {EventListenerCollection, invokeMessageHandler, log, promiseAnimationFram import {DocumentUtil} from '../dom/document-util.js'; import {TextSourceElement} from '../dom/text-source-element.js'; import {TextSourceRange} from '../dom/text-source-range.js'; -import {HotkeyHandler} from '../input/hotkey-handler.js'; import {TextScanner} from '../language/text-scanner.js'; import {yomitan} from '../yomitan.js'; -import {PopupFactory} from './popup-factory.js'; /** * This is the main class responsible for scanning and handling webpage content. @@ -50,7 +48,7 @@ export class Frontend { }) { /** @type {import('frontend').PageType} */ this._pageType = pageType; - /** @type {PopupFactory} */ + /** @type {import('./popup-factory.js').PopupFactory} */ this._popupFactory = popupFactory; /** @type {number} */ this._depth = depth; @@ -70,7 +68,7 @@ export class Frontend { this._allowRootFramePopupProxy = allowRootFramePopupProxy; /** @type {boolean} */ this._childrenSupported = childrenSupported; - /** @type {HotkeyHandler} */ + /** @type {import('../input/hotkey-handler.js').HotkeyHandler} */ this._hotkeyHandler = hotkeyHandler; /** @type {?import('popup').PopupAny} */ this._popup = null; diff --git a/ext/js/app/popup-proxy.js b/ext/js/app/popup-proxy.js index 9b5b0214..924175e2 100644 --- a/ext/js/app/popup-proxy.js +++ b/ext/js/app/popup-proxy.js @@ -16,10 +16,8 @@ * along with this program. If not, see . */ -import {FrameOffsetForwarder} from '../comm/frame-offset-forwarder.js'; import {EventDispatcher, log} from '../core.js'; import {yomitan} from '../yomitan.js'; -import {Popup} from './popup.js'; /** * This class is a proxy for a Popup that is hosted in a different frame. @@ -44,7 +42,7 @@ export class PopupProxy extends EventDispatcher { this._depth = depth; /** @type {number} */ this._frameId = frameId; - /** @type {?FrameOffsetForwarder} */ + /** @type {?import('../comm/frame-offset-forwarder.js').FrameOffsetForwarder} */ this._frameOffsetForwarder = frameOffsetForwarder; /** @type {number} */ @@ -70,7 +68,7 @@ export class PopupProxy extends EventDispatcher { /** * The parent of the popup, which is always `null` for `PopupProxy` instances, * since any potential parent popups are in a different frame. - * @type {?Popup} + * @type {?import('./popup.js').Popup} */ get parent() { return null; @@ -78,7 +76,7 @@ export class PopupProxy extends EventDispatcher { /** * Attempts to set the parent popup. - * @param {Popup} _value The parent to assign. + * @param {import('./popup.js').Popup} _value The parent to assign. * @throws {Error} Throws an error, since this class doesn't support a direct parent. */ set parent(_value) { @@ -88,7 +86,7 @@ export class PopupProxy extends EventDispatcher { /** * The popup child popup, which is always null for `PopupProxy` instances, * since any potential child popups are in a different frame. - * @type {?Popup} + * @type {?import('./popup.js').Popup} */ get child() { return null; @@ -96,7 +94,7 @@ export class PopupProxy extends EventDispatcher { /** * Attempts to set the child popup. - * @param {Popup} _child The child to assign. + * @param {import('./popup.js').Popup} _child The child to assign. * @throws {Error} Throws an error, since this class doesn't support children. */ set child(_child) { @@ -354,7 +352,7 @@ export class PopupProxy extends EventDispatcher { * @param {number} now */ async _updateFrameOffsetInner(now) { - this._frameOffsetPromise = /** @type {FrameOffsetForwarder} */ (this._frameOffsetForwarder).getOffset(); + this._frameOffsetPromise = /** @type {import('../comm/frame-offset-forwarder.js').FrameOffsetForwarder} */ (this._frameOffsetForwarder).getOffset(); try { const offset = await this._frameOffsetPromise; if (offset !== null) { diff --git a/ext/js/app/popup-window.js b/ext/js/app/popup-window.js index af1ac1e4..9a0f8011 100644 --- a/ext/js/app/popup-window.js +++ b/ext/js/app/popup-window.js @@ -18,7 +18,6 @@ import {EventDispatcher} from '../core.js'; import {yomitan} from '../yomitan.js'; -import {Popup} from './popup.js'; /** * This class represents a popup that is hosted in a new native window. @@ -54,7 +53,7 @@ export class PopupWindow extends EventDispatcher { } /** - * @type {?Popup} + * @type {?import('./popup.js').Popup} */ get parent() { return null; @@ -63,7 +62,7 @@ export class PopupWindow extends EventDispatcher { /** * The parent of the popup, which is always `null` for `PopupWindow` instances, * since any potential parent popups are in a different frame. - * @param {Popup} _value The parent to assign. + * @param {import('./popup.js').Popup} _value The parent to assign. * @throws {Error} Throws an error, since this class doesn't support children. */ set parent(_value) { @@ -73,7 +72,7 @@ export class PopupWindow extends EventDispatcher { /** * The popup child popup, which is always null for `PopupWindow` instances, * since any potential child popups are in a different frame. - * @type {?Popup} + * @type {?import('./popup.js').Popup} */ get child() { return null; @@ -81,7 +80,7 @@ export class PopupWindow extends EventDispatcher { /** * Attempts to set the child popup. - * @param {Popup} _value The child to assign. + * @param {import('./popup.js').Popup} _value The child to assign. * @throws Throws an error, since this class doesn't support children. */ set child(_value) { diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index a8683b3f..14877cf1 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -25,7 +25,6 @@ import {Mecab} from '../comm/mecab.js'; import {clone, deferPromise, generateId, invokeMessageHandler, isObject, log, promiseTimeout} from '../core.js'; import {ExtensionError} from '../core/extension-error.js'; import {AnkiUtil} from '../data/anki-util.js'; -import {JsonSchema} from '../data/json-schema.js'; import {OptionsUtil} from '../data/options-util.js'; import {PermissionsUtil} from '../data/permissions-util.js'; import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/comm/anki-connect.js b/ext/js/comm/anki-connect.js index 3262af41..b876703f 100644 --- a/ext/js/comm/anki-connect.js +++ b/ext/js/comm/anki-connect.js @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import {isObject} from '../core.js'; import {AnkiUtil} from '../data/anki-util.js'; /** diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js index f53d55fd..cf58d39f 100644 --- a/ext/js/dom/document-util.js +++ b/ext/js/dom/document-util.js @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import {EventListenerCollection} from '../core.js'; import {DOMTextScanner} from './dom-text-scanner.js'; import {TextSourceElement} from './text-source-element.js'; import {TextSourceRange} from './text-source-range.js'; diff --git a/ext/js/dom/text-source-element.js b/ext/js/dom/text-source-element.js index fbe89a61..47c18e30 100644 --- a/ext/js/dom/text-source-element.js +++ b/ext/js/dom/text-source-element.js @@ -18,7 +18,6 @@ import {StringUtil} from '../data/sandbox/string-util.js'; import {DocumentUtil} from './document-util.js'; -import {TextSourceRange} from './text-source-range.js'; /** * This class represents a text source that is attached to a HTML element, such as an diff --git a/ext/js/dom/text-source-range.js b/ext/js/dom/text-source-range.js index 5c3d4184..5dbbd636 100644 --- a/ext/js/dom/text-source-range.js +++ b/ext/js/dom/text-source-range.js @@ -18,7 +18,6 @@ import {DocumentUtil} from './document-util.js'; import {DOMTextScanner} from './dom-text-scanner.js'; -import {TextSourceElement} from './text-source-element.js'; /** * This class represents a text source that comes from text nodes in the document. diff --git a/ext/js/language/dictionary-worker-handler.js b/ext/js/language/dictionary-worker-handler.js index 8ac342b2..3a85cb71 100644 --- a/ext/js/language/dictionary-worker-handler.js +++ b/ext/js/language/dictionary-worker-handler.js @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import {serializeError} from '../core.js'; import {DictionaryDatabase} from './dictionary-database.js'; import {DictionaryImporter} from './dictionary-importer.js'; import {DictionaryWorkerMediaLoader} from './dictionary-worker-media-loader.js'; diff --git a/ext/js/language/dictionary-worker-media-loader.js b/ext/js/language/dictionary-worker-media-loader.js index 9e3fd67e..2701389e 100644 --- a/ext/js/language/dictionary-worker-media-loader.js +++ b/ext/js/language/dictionary-worker-media-loader.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {deserializeError, generateId} from '../core.js'; +import {generateId} from '../core.js'; /** * Class used for loading and validating media from a worker thread diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index 9b01c1ff..67cc53c6 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -19,7 +19,6 @@ import {RegexUtil} from '../general/regex-util.js'; import {TextSourceMap} from '../general/text-source-map.js'; import {Deinflector} from './deinflector.js'; -import {DictionaryDatabase} from './dictionary-database.js'; /** * Class which finds term and kanji dictionary entries for text. diff --git a/ext/js/templates/template-renderer-proxy.js b/ext/js/templates/template-renderer-proxy.js index 6d019d14..e67b715a 100644 --- a/ext/js/templates/template-renderer-proxy.js +++ b/ext/js/templates/template-renderer-proxy.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {deserializeError, generateId} from '../core.js'; +import {generateId} from '../core.js'; export class TemplateRendererProxy { constructor() { -- cgit v1.2.3 From 6a3dae04de68ab633da15bbc8ec6b350e38e6d2f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 13:54:43 -0500 Subject: Add extension error imports --- ext/js/app/popup.js | 1 + ext/js/comm/anki-connect.js | 1 + ext/js/data/anki-note-builder.js | 1 + ext/js/display/display-generator.js | 1 + ext/js/display/option-toggle-hotkey-handler.js | 1 + ext/js/language/dictionary-worker-handler.js | 1 + ext/js/language/dictionary-worker-media-loader.js | 1 + ext/js/language/dictionary-worker.js | 1 + ext/js/media/audio-downloader.js | 1 + ext/js/pages/settings/anki-controller.js | 1 + ext/js/pages/settings/anki-templates-controller.js | 1 + ext/js/pages/settings/dictionary-import-controller.js | 1 + ext/js/pages/settings/generic-setting-controller.js | 1 + ext/js/templates/sandbox/template-renderer-frame-api.js | 2 ++ ext/js/templates/sandbox/template-renderer.js | 1 + ext/js/templates/template-renderer-proxy.js | 1 + 16 files changed, 17 insertions(+) (limited to 'ext/js/templates') diff --git a/ext/js/app/popup.js b/ext/js/app/popup.js index 31b18f01..4f201fc3 100644 --- a/ext/js/app/popup.js +++ b/ext/js/app/popup.js @@ -18,6 +18,7 @@ import {FrameClient} from '../comm/frame-client.js'; import {DynamicProperty, EventDispatcher, EventListenerCollection, deepEqual} from '../core.js'; +import {ExtensionError} from '../core/extension-error.js'; import {DocumentUtil} from '../dom/document-util.js'; import {dynamicLoader} from '../script/dynamic-loader.js'; import {yomitan} from '../yomitan.js'; diff --git a/ext/js/comm/anki-connect.js b/ext/js/comm/anki-connect.js index b876703f..7ff8d0e1 100644 --- a/ext/js/comm/anki-connect.js +++ b/ext/js/comm/anki-connect.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../core/extension-error.js'; import {AnkiUtil} from '../data/anki-util.js'; /** diff --git a/ext/js/data/anki-note-builder.js b/ext/js/data/anki-note-builder.js index f8296ee0..28809e1f 100644 --- a/ext/js/data/anki-note-builder.js +++ b/ext/js/data/anki-note-builder.js @@ -17,6 +17,7 @@ */ import {deferPromise} from '../core.js'; +import {ExtensionError} from '../core/extension-error.js'; import {TemplateRendererProxy} from '../templates/template-renderer-proxy.js'; import {yomitan} from '../yomitan.js'; import {AnkiUtil} from './anki-util.js'; diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index 9fc700f3..e8be79d9 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -17,6 +17,7 @@ */ import {isObject} from '../core.js'; +import {ExtensionError} from '../core/extension-error.js'; import {HtmlTemplateCollection} from '../dom/html-template-collection.js'; import {DictionaryDataUtil} from '../language/sandbox/dictionary-data-util.js'; import {yomitan} from '../yomitan.js'; diff --git a/ext/js/display/option-toggle-hotkey-handler.js b/ext/js/display/option-toggle-hotkey-handler.js index e73fcf04..9677e86b 100644 --- a/ext/js/display/option-toggle-hotkey-handler.js +++ b/ext/js/display/option-toggle-hotkey-handler.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../core/extension-error.js'; import {yomitan} from '../yomitan.js'; export class OptionToggleHotkeyHandler { diff --git a/ext/js/language/dictionary-worker-handler.js b/ext/js/language/dictionary-worker-handler.js index 3a85cb71..9a724386 100644 --- a/ext/js/language/dictionary-worker-handler.js +++ b/ext/js/language/dictionary-worker-handler.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../core/extension-error.js'; import {DictionaryDatabase} from './dictionary-database.js'; import {DictionaryImporter} from './dictionary-importer.js'; import {DictionaryWorkerMediaLoader} from './dictionary-worker-media-loader.js'; diff --git a/ext/js/language/dictionary-worker-media-loader.js b/ext/js/language/dictionary-worker-media-loader.js index 2701389e..e19a13d3 100644 --- a/ext/js/language/dictionary-worker-media-loader.js +++ b/ext/js/language/dictionary-worker-media-loader.js @@ -17,6 +17,7 @@ */ import {generateId} from '../core.js'; +import {ExtensionError} from '../core/extension-error.js'; /** * Class used for loading and validating media from a worker thread diff --git a/ext/js/language/dictionary-worker.js b/ext/js/language/dictionary-worker.js index b9d0236c..3e78a6ff 100644 --- a/ext/js/language/dictionary-worker.js +++ b/ext/js/language/dictionary-worker.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../core/extension-error.js'; import {DictionaryImporterMediaLoader} from './dictionary-importer-media-loader.js'; export class DictionaryWorker { diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js index 8bd04b2b..7b236790 100644 --- a/ext/js/media/audio-downloader.js +++ b/ext/js/media/audio-downloader.js @@ -17,6 +17,7 @@ */ import {RequestBuilder} from '../background/request-builder.js'; +import {ExtensionError} from '../core/extension-error.js'; import {JsonSchema} from '../data/json-schema.js'; import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; import {NativeSimpleDOMParser} from '../dom/native-simple-dom-parser.js'; diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index 0ccd018d..722459df 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -18,6 +18,7 @@ import {AnkiConnect} from '../../comm/anki-connect.js'; import {EventListenerCollection, log} from '../../core.js'; +import {ExtensionError} from '../../core/extension-error.js'; import {AnkiUtil} from '../../data/anki-util.js'; import {SelectorObserver} from '../../dom/selector-observer.js'; import {ObjectPropertyAccessor} from '../../general/object-property-accessor.js'; diff --git a/ext/js/pages/settings/anki-templates-controller.js b/ext/js/pages/settings/anki-templates-controller.js index d2814880..a0ff96b2 100644 --- a/ext/js/pages/settings/anki-templates-controller.js +++ b/ext/js/pages/settings/anki-templates-controller.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../../core/extension-error.js'; import {AnkiNoteBuilder} from '../../data/anki-note-builder.js'; import {JapaneseUtil} from '../../language/sandbox/japanese-util.js'; import {yomitan} from '../../yomitan.js'; diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 106ecbca..af8c2fcd 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -17,6 +17,7 @@ */ import {log} from '../../core.js'; +import {ExtensionError} from '../../core/extension-error.js'; import {DictionaryWorker} from '../../language/dictionary-worker.js'; import {yomitan} from '../../yomitan.js'; import {DictionaryController} from './dictionary-controller.js'; diff --git a/ext/js/pages/settings/generic-setting-controller.js b/ext/js/pages/settings/generic-setting-controller.js index 3c6104a9..47c0d6fe 100644 --- a/ext/js/pages/settings/generic-setting-controller.js +++ b/ext/js/pages/settings/generic-setting-controller.js @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../../core/extension-error.js'; import {DocumentUtil} from '../../dom/document-util.js'; import {DOMDataBinder} from '../../dom/dom-data-binder.js'; diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js index 31ba4500..91400ab8 100644 --- a/ext/js/templates/sandbox/template-renderer-frame-api.js +++ b/ext/js/templates/sandbox/template-renderer-frame-api.js @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +import {ExtensionError} from '../../core/extension-error.js'; + export class TemplateRendererFrameApi { /** * @param {TemplateRenderer} templateRenderer diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index c7613533..716e3ccc 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -17,6 +17,7 @@ */ import {Handlebars} from '../../../lib/handlebars.js'; +import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { constructor() { diff --git a/ext/js/templates/template-renderer-proxy.js b/ext/js/templates/template-renderer-proxy.js index e67b715a..642eea8b 100644 --- a/ext/js/templates/template-renderer-proxy.js +++ b/ext/js/templates/template-renderer-proxy.js @@ -17,6 +17,7 @@ */ import {generateId} from '../core.js'; +import {ExtensionError} from '../core/extension-error.js'; export class TemplateRendererProxy { constructor() { -- cgit v1.2.3 From c8ac0d45bfb94deabd933117ba55d365f43fb1c5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 14:08:39 -0500 Subject: Updates --- .vscode/settings.json | 2 +- .../templates/__mocks__/template-renderer-proxy.js | 53 ++++++++-------------- .../anki-template-renderer-content-manager.js | 6 +-- ext/js/templates/sandbox/anki-template-renderer.js | 8 ++-- .../sandbox/template-renderer-frame-api.js | 4 +- ext/js/templates/sandbox/template-renderer.js | 8 ++-- 6 files changed, 32 insertions(+), 49 deletions(-) (limited to 'ext/js/templates') diff --git a/.vscode/settings.json b/.vscode/settings.json index ecfe13c5..81cd7b9c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,7 @@ "editor.codeActionsOnSave": { "source.addMissingImports": false, "source.organizeImports": true, - "source.fixAll.eslint": true, + "source.fixAll.eslint": false }, "eslint.format.enable": true, "javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false, diff --git a/ext/js/templates/__mocks__/template-renderer-proxy.js b/ext/js/templates/__mocks__/template-renderer-proxy.js index bcacf13b..8823e8f3 100644 --- a/ext/js/templates/__mocks__/template-renderer-proxy.js +++ b/ext/js/templates/__mocks__/template-renderer-proxy.js @@ -20,20 +20,35 @@ import {AnkiTemplateRenderer} from '../sandbox/anki-template-renderer.js'; export class TemplateRendererProxy { constructor() { + /** @type {?Promise} */ this._preparePromise = null; + /** @type {AnkiTemplateRenderer} */ this._ankiTemplateRenderer = new AnkiTemplateRenderer(); } + /** + * @param {string} template + * @param {import('template-renderer').PartialOrCompositeRenderData} data + * @param {import('anki-templates').RenderMode} type + * @returns {Promise} + */ async render(template, data, type) { await this._prepare(); - return await this._ankiTemplateRenderer.templateRenderer.render(template, data, type); + return this._ankiTemplateRenderer.templateRenderer.render(template, data, type); } + /** + * @param {import('template-renderer').RenderMultiItem[]} items + * @returns {Promise[]>} + */ async renderMulti(items) { await this._prepare(); - return await this._serializeMulti(this._ankiTemplateRenderer.templateRenderer.renderMulti(items)); + return this._ankiTemplateRenderer.templateRenderer.renderMulti(items); } + /** + * @returns {Promise} + */ _prepare() { if (this._preparePromise === null) { this._preparePromise = this._prepareInternal(); @@ -41,40 +56,8 @@ export class TemplateRendererProxy { return this._preparePromise; } + /** */ async _prepareInternal() { await this._ankiTemplateRenderer.prepare(); } - - _serializeError(error) { - try { - if (typeof error === 'object' && error !== null) { - const result = { - name: error.name, - message: error.message, - stack: error.stack - }; - if (Object.prototype.hasOwnProperty.call(error, 'data')) { - result.data = error.data; - } - return result; - } - } catch (e) { - // NOP - } - return { - value: error, - hasValue: true - }; - } - - _serializeMulti(array) { - for (let i = 0, ii = array.length; i < ii; ++i) { - const value = array[i]; - const {error} = value; - if (typeof error !== 'undefined') { - value.error = this._serializeError(error); - } - } - return array; - } } diff --git a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js index be80c211..4989ced3 100644 --- a/ext/js/templates/sandbox/anki-template-renderer-content-manager.js +++ b/ext/js/templates/sandbox/anki-template-renderer-content-manager.js @@ -22,12 +22,12 @@ export class AnkiTemplateRendererContentManager { /** * Creates a new instance of the class. - * @param {TemplateRendererMediaProvider} mediaProvider The media provider for the object. + * @param {import('./template-renderer-media-provider.js').TemplateRendererMediaProvider} mediaProvider The media provider for the object. * @param {import('anki-templates').NoteData} data The data object passed to the Handlebars template renderer. - * See {@link AnkiNoteDataCreator.create}'s return value for structure information. + * See AnkiNoteDataCreator.create's return value for structure information. */ constructor(mediaProvider, data) { - /** @type {TemplateRendererMediaProvider} */ + /** @type {import('./template-renderer-media-provider.js').TemplateRendererMediaProvider} */ this._mediaProvider = mediaProvider; /** @type {import('anki-templates').NoteData} */ this._data = data; diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index ce8e3200..9f4bf6ff 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -151,8 +151,8 @@ export class AnkiTemplateRenderer { } /** - * - * @param text + * @param {string} text + * @returns {string} */ _safeString(text) { return new Handlebars.SafeString(text); @@ -221,8 +221,8 @@ export class AnkiTemplateRenderer { } /** - * - * @param string + * @param {string} string + * @returns {string} */ _stringToMultiLineHtml(string) { return string.split('\n').join('
'); diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js index 91400ab8..94ebf7fe 100644 --- a/ext/js/templates/sandbox/template-renderer-frame-api.js +++ b/ext/js/templates/sandbox/template-renderer-frame-api.js @@ -20,10 +20,10 @@ import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRendererFrameApi { /** - * @param {TemplateRenderer} templateRenderer + * @param {import('./template-renderer.js').TemplateRenderer} templateRenderer */ constructor(templateRenderer) { - /** @type {TemplateRenderer} */ + /** @type {import('./template-renderer.js').TemplateRenderer} */ this._templateRenderer = templateRenderer; /** @type {import('core').MessageHandlerMap} */ this._windowMessageHandlers = new Map(/** @type {import('core').MessageHandlerArray} */ ([ diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index 716e3ccc..fe240b5f 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -21,7 +21,7 @@ import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { constructor() { - /** @type {Map>} */ + /** @type {Map>} */ this._cache = new Map(); /** @type {number} */ this._cacheMaxSize = 5; @@ -110,14 +110,14 @@ export class TemplateRenderer { /** * @param {string} template - * @returns {HandlebarsTemplateDelegate} + * @returns {import('handlebars').TemplateDelegate} */ _getTemplateInstance(template) { const cache = this._cache; let instance = cache.get(template); if (typeof instance === 'undefined') { this._updateCacheSize(this._cacheMaxSize - 1); - instance = /** @type {HandlebarsTemplateDelegate} */ (Handlebars.compileAST(template)); + instance = /** @type {import('handlebars').TemplateDelegate} */ (Handlebars.compileAST(template)); cache.set(template, instance); } @@ -125,7 +125,7 @@ export class TemplateRenderer { } /** - * @param {HandlebarsTemplateDelegate} instance + * @param {import('handlebars').TemplateDelegate} instance * @param {import('anki-templates').NoteData} data * @returns {import('template-renderer').RenderResult} */ -- cgit v1.2.3 From d5b1217df3fe7480fc5f58fe194f5bbf73281094 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 19:23:17 -0500 Subject: Use import map --- dev/jsconfig.json | 1 + ext/action-popup.html | 8 ++++++++ ext/background.html | 8 ++++++++ ext/info.html | 8 ++++++++ ext/issues.html | 8 ++++++++ ext/js/background/backend.js | 2 +- ext/js/background/offscreen.js | 2 +- ext/js/display/search-display-controller.js | 2 +- ext/js/display/search-main.js | 2 +- ext/js/dom/simple-dom-parser.js | 2 +- ext/js/language/dictionary-importer.js | 4 ++-- ext/js/pages/settings/backup-controller.js | 2 +- ext/js/pages/settings/popup-preview-frame.js | 2 +- ext/js/templates/sandbox/anki-template-renderer.js | 2 +- ext/js/templates/sandbox/template-renderer-media-provider.js | 2 +- ext/js/templates/sandbox/template-renderer.js | 2 +- ext/legal.html | 8 ++++++++ ext/offscreen.html | 8 ++++++++ ext/permissions.html | 8 ++++++++ ext/popup-preview.html | 8 ++++++++ ext/popup.html | 8 ++++++++ ext/search.html | 8 ++++++++ ext/settings.html | 8 ++++++++ ext/template-renderer.html | 8 ++++++++ ext/welcome.html | 8 ++++++++ jsconfig.json | 3 ++- test/japanese-util.test.js | 2 +- test/jsconfig.json | 1 + 28 files changed, 121 insertions(+), 14 deletions(-) (limited to 'ext/js/templates') diff --git a/dev/jsconfig.json b/dev/jsconfig.json index a012f32f..518f97ad 100644 --- a/dev/jsconfig.json +++ b/dev/jsconfig.json @@ -12,6 +12,7 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { + "zip.js": ["@zip.js/zip.js"], "anki-templates": ["../types/ext/anki-templates"], "anki-templates-internal": ["../types/ext/anki-templates-internal"], "cache-map": ["../types/ext/cache-map"], diff --git a/ext/action-popup.html b/ext/action-popup.html index b60e7055..5c6bfce4 100644 --- a/ext/action-popup.html +++ b/ext/action-popup.html @@ -12,6 +12,14 @@ + diff --git a/ext/background.html b/ext/background.html index dc88f397..6f9ee5f6 100644 --- a/ext/background.html +++ b/ext/background.html @@ -12,6 +12,14 @@ + diff --git a/ext/info.html b/ext/info.html index cb80053d..9e71ffd4 100644 --- a/ext/info.html +++ b/ext/info.html @@ -13,6 +13,14 @@ + diff --git a/ext/issues.html b/ext/issues.html index 904fbf16..c75683dd 100644 --- a/ext/issues.html +++ b/ext/issues.html @@ -13,6 +13,14 @@ + diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index be68ecf4..44f5a42d 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from '../../lib/wanakana.js'; +import * as wanakana from 'wanakana'; import {AccessibilityController} from '../accessibility/accessibility-controller.js'; import {AnkiConnect} from '../comm/anki-connect.js'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js index 1b68887b..45345c01 100644 --- a/ext/js/background/offscreen.js +++ b/ext/js/background/offscreen.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from '../../lib/wanakana.js'; +import * as wanakana from 'wanakana'; import {ClipboardReader} from '../comm/clipboard-reader.js'; import {invokeMessageHandler} from '../core.js'; import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index a9bf2217..b93757c2 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from '../../lib/wanakana.js'; +import * as wanakana from 'wanakana'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; import {EventListenerCollection, invokeMessageHandler} from '../core.js'; import {yomitan} from '../yomitan.js'; diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index c20cc135..c1445e37 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from '../../lib/wanakana.js'; +import * as wanakana from 'wanakana'; import {log} from '../core.js'; import {DocumentFocusController} from '../dom/document-focus-controller.js'; import {HotkeyHandler} from '../input/hotkey-handler.js'; diff --git a/ext/js/dom/simple-dom-parser.js b/ext/js/dom/simple-dom-parser.js index a1f63890..7ee28d51 100644 --- a/ext/js/dom/simple-dom-parser.js +++ b/ext/js/dom/simple-dom-parser.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as parse5 from '../../lib/parse5.js'; +import * as parse5 from 'parse5'; /** * @augments import('simple-dom-parser').ISimpleDomParser diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 115e0726..5a4d7257 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -16,14 +16,14 @@ * along with this program. If not, see . */ -import * as ajvSchemas0 from '../../lib/validate-schemas.js'; +import * as ajvSchemas0 from 'validate-schemas'; import { BlobWriter as BlobWriter0, TextWriter as TextWriter0, Uint8ArrayReader as Uint8ArrayReader0, ZipReader as ZipReader0, configure -} from '../../lib/zip.js'; +} from 'zip.js'; import {stringReverse} from '../core.js'; import {MediaUtil} from '../media/media-util.js'; import {ExtensionError} from '../core/extension-error.js'; diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index 52c5f418..aeff2a97 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Dexie} from '../../../lib/dexie.js'; +import {Dexie} from 'dexie'; import {isObject, log} from '../../core.js'; import {OptionsUtil} from '../../data/options-util.js'; import {ArrayBufferUtil} from '../../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/pages/settings/popup-preview-frame.js b/ext/js/pages/settings/popup-preview-frame.js index c1a0d706..bb00829f 100644 --- a/ext/js/pages/settings/popup-preview-frame.js +++ b/ext/js/pages/settings/popup-preview-frame.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from '../../../lib/wanakana.js'; +import * as wanakana from 'wanakana'; import {Frontend} from '../../app/frontend.js'; import {TextSourceRange} from '../../dom/text-source-range.js'; import {yomitan} from '../../yomitan.js'; diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 9f4bf6ff..b0dc8223 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from '../../../lib/handlebars.js'; +import {Handlebars} from 'handlebars'; import {AnkiNoteDataCreator} from '../../data/sandbox/anki-note-data-creator.js'; import {PronunciationGenerator} from '../../display/sandbox/pronunciation-generator.js'; import {StructuredContentGenerator} from '../../display/sandbox/structured-content-generator.js'; diff --git a/ext/js/templates/sandbox/template-renderer-media-provider.js b/ext/js/templates/sandbox/template-renderer-media-provider.js index d8a0a16d..23f334e1 100644 --- a/ext/js/templates/sandbox/template-renderer-media-provider.js +++ b/ext/js/templates/sandbox/template-renderer-media-provider.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from '../../../lib/handlebars.js'; +import {Handlebars} from 'handlebars'; export class TemplateRendererMediaProvider { constructor() { diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index fe240b5f..d4aebd64 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from '../../../lib/handlebars.js'; +import {Handlebars} from 'handlebars'; import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { diff --git a/ext/legal.html b/ext/legal.html index 94912c7e..b853f3e8 100644 --- a/ext/legal.html +++ b/ext/legal.html @@ -14,6 +14,14 @@ + diff --git a/ext/offscreen.html b/ext/offscreen.html index afb7eb44..cfab53ee 100644 --- a/ext/offscreen.html +++ b/ext/offscreen.html @@ -12,6 +12,14 @@ + diff --git a/ext/permissions.html b/ext/permissions.html index 61b0d363..7baff14e 100644 --- a/ext/permissions.html +++ b/ext/permissions.html @@ -14,6 +14,14 @@ + diff --git a/ext/popup-preview.html b/ext/popup-preview.html index 15810242..7bd54470 100644 --- a/ext/popup-preview.html +++ b/ext/popup-preview.html @@ -13,6 +13,14 @@ + diff --git a/ext/popup.html b/ext/popup.html index 30e8a8c0..6bc8d690 100644 --- a/ext/popup.html +++ b/ext/popup.html @@ -15,6 +15,14 @@ + diff --git a/ext/search.html b/ext/search.html index 8c595cc4..377a966a 100644 --- a/ext/search.html +++ b/ext/search.html @@ -16,6 +16,14 @@ + diff --git a/ext/settings.html b/ext/settings.html index 346cc1d7..276a7222 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -14,6 +14,14 @@ + diff --git a/ext/template-renderer.html b/ext/template-renderer.html index 116f1a0c..d1747e99 100644 --- a/ext/template-renderer.html +++ b/ext/template-renderer.html @@ -11,6 +11,14 @@ + diff --git a/ext/welcome.html b/ext/welcome.html index 40639881..355bbc5f 100644 --- a/ext/welcome.html +++ b/ext/welcome.html @@ -13,6 +13,14 @@ + diff --git a/jsconfig.json b/jsconfig.json index ace0c2aa..4f316174 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -12,6 +12,7 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { + "zip.js": ["@zip.js/zip.js"], "*": ["./types/ext/*"] }, "types": [ @@ -29,7 +30,7 @@ "include": [ "ext/**/*.js", "types/ext/**/*.ts", - "types/other/web-set-timeout.d.ts" + "types/other/globals.d.ts" ], "exclude": [ "node_modules", diff --git a/test/japanese-util.test.js b/test/japanese-util.test.js index 47da4ccb..ae5b1a16 100644 --- a/test/japanese-util.test.js +++ b/test/japanese-util.test.js @@ -16,10 +16,10 @@ * along with this program. If not, see . */ +import * as wanakana from 'wanakana'; import {expect, test} from 'vitest'; import {TextSourceMap} from '../ext/js/general/text-source-map.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; -import * as wanakana from '../ext/lib/wanakana.js'; const jp = new JapaneseUtil(wanakana); diff --git a/test/jsconfig.json b/test/jsconfig.json index b025918c..261ec345 100644 --- a/test/jsconfig.json +++ b/test/jsconfig.json @@ -12,6 +12,7 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { + "zip.js": ["@zip.js/zip.js"], "*": ["../types/ext/*"], "dev/*": ["../types/dev/*"], "test/*": ["../types/test/*"] -- cgit v1.2.3 From 58ae2ab871591eea82895b1ab2a18753521eab1f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 22:07:28 -0500 Subject: Revert "Use import map" --- dev/jsconfig.json | 1 - ext/action-popup.html | 8 -------- ext/background.html | 8 -------- ext/info.html | 8 -------- ext/issues.html | 8 -------- ext/js/background/backend.js | 2 +- ext/js/background/offscreen.js | 2 +- ext/js/display/search-display-controller.js | 2 +- ext/js/display/search-main.js | 2 +- ext/js/dom/simple-dom-parser.js | 2 +- ext/js/language/dictionary-importer.js | 4 ++-- ext/js/pages/settings/backup-controller.js | 2 +- ext/js/pages/settings/popup-preview-frame.js | 2 +- ext/js/templates/sandbox/anki-template-renderer.js | 2 +- ext/js/templates/sandbox/template-renderer-media-provider.js | 2 +- ext/js/templates/sandbox/template-renderer.js | 2 +- ext/legal.html | 8 -------- ext/offscreen.html | 8 -------- ext/permissions.html | 8 -------- ext/popup-preview.html | 8 -------- ext/popup.html | 8 -------- ext/search.html | 8 -------- ext/settings.html | 8 -------- ext/template-renderer.html | 8 -------- ext/welcome.html | 8 -------- jsconfig.json | 1 - test/japanese-util.test.js | 2 +- test/jsconfig.json | 1 - 28 files changed, 13 insertions(+), 120 deletions(-) (limited to 'ext/js/templates') diff --git a/dev/jsconfig.json b/dev/jsconfig.json index d4efe694..5b1c450c 100644 --- a/dev/jsconfig.json +++ b/dev/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "anki-templates": ["../types/ext/anki-templates"], "anki-templates-internal": ["../types/ext/anki-templates-internal"], "cache-map": ["../types/ext/cache-map"], diff --git a/ext/action-popup.html b/ext/action-popup.html index 5c6bfce4..b60e7055 100644 --- a/ext/action-popup.html +++ b/ext/action-popup.html @@ -12,14 +12,6 @@ - diff --git a/ext/background.html b/ext/background.html index 6f9ee5f6..dc88f397 100644 --- a/ext/background.html +++ b/ext/background.html @@ -12,14 +12,6 @@ - diff --git a/ext/info.html b/ext/info.html index 9e71ffd4..cb80053d 100644 --- a/ext/info.html +++ b/ext/info.html @@ -13,14 +13,6 @@ - diff --git a/ext/issues.html b/ext/issues.html index c75683dd..904fbf16 100644 --- a/ext/issues.html +++ b/ext/issues.html @@ -13,14 +13,6 @@ - diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index f1a47e7f..3eefed53 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {AccessibilityController} from '../accessibility/accessibility-controller.js'; import {AnkiConnect} from '../comm/anki-connect.js'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js index 8da661ad..4b57514d 100644 --- a/ext/js/background/offscreen.js +++ b/ext/js/background/offscreen.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {ClipboardReader} from '../comm/clipboard-reader.js'; import {invokeMessageHandler} from '../core.js'; import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index 98a4666b..2778c4cd 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {ClipboardMonitor} from '../comm/clipboard-monitor.js'; import {EventListenerCollection, invokeMessageHandler} from '../core.js'; import {yomitan} from '../yomitan.js'; diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index c1445e37..c20cc135 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../lib/wanakana.js'; import {log} from '../core.js'; import {DocumentFocusController} from '../dom/document-focus-controller.js'; import {HotkeyHandler} from '../input/hotkey-handler.js'; diff --git a/ext/js/dom/simple-dom-parser.js b/ext/js/dom/simple-dom-parser.js index 7ee28d51..a1f63890 100644 --- a/ext/js/dom/simple-dom-parser.js +++ b/ext/js/dom/simple-dom-parser.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as parse5 from 'parse5'; +import * as parse5 from '../../lib/parse5.js'; /** * @augments import('simple-dom-parser').ISimpleDomParser diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 5a4d7257..115e0726 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -16,14 +16,14 @@ * along with this program. If not, see . */ -import * as ajvSchemas0 from 'validate-schemas'; +import * as ajvSchemas0 from '../../lib/validate-schemas.js'; import { BlobWriter as BlobWriter0, TextWriter as TextWriter0, Uint8ArrayReader as Uint8ArrayReader0, ZipReader as ZipReader0, configure -} from 'zip.js'; +} from '../../lib/zip.js'; import {stringReverse} from '../core.js'; import {MediaUtil} from '../media/media-util.js'; import {ExtensionError} from '../core/extension-error.js'; diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index aeff2a97..52c5f418 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Dexie} from 'dexie'; +import {Dexie} from '../../../lib/dexie.js'; import {isObject, log} from '../../core.js'; import {OptionsUtil} from '../../data/options-util.js'; import {ArrayBufferUtil} from '../../data/sandbox/array-buffer-util.js'; diff --git a/ext/js/pages/settings/popup-preview-frame.js b/ext/js/pages/settings/popup-preview-frame.js index dab21f4d..60d264fa 100644 --- a/ext/js/pages/settings/popup-preview-frame.js +++ b/ext/js/pages/settings/popup-preview-frame.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; +import * as wanakana from '../../../lib/wanakana.js'; import {Frontend} from '../../app/frontend.js'; import {TextSourceRange} from '../../dom/text-source-range.js'; import {yomitan} from '../../yomitan.js'; diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index b0dc8223..9f4bf6ff 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; import {AnkiNoteDataCreator} from '../../data/sandbox/anki-note-data-creator.js'; import {PronunciationGenerator} from '../../display/sandbox/pronunciation-generator.js'; import {StructuredContentGenerator} from '../../display/sandbox/structured-content-generator.js'; diff --git a/ext/js/templates/sandbox/template-renderer-media-provider.js b/ext/js/templates/sandbox/template-renderer-media-provider.js index 23f334e1..d8a0a16d 100644 --- a/ext/js/templates/sandbox/template-renderer-media-provider.js +++ b/ext/js/templates/sandbox/template-renderer-media-provider.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; export class TemplateRendererMediaProvider { constructor() { diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index d4aebd64..fe240b5f 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {Handlebars} from 'handlebars'; +import {Handlebars} from '../../../lib/handlebars.js'; import {ExtensionError} from '../../core/extension-error.js'; export class TemplateRenderer { diff --git a/ext/legal.html b/ext/legal.html index b853f3e8..94912c7e 100644 --- a/ext/legal.html +++ b/ext/legal.html @@ -14,14 +14,6 @@ - diff --git a/ext/offscreen.html b/ext/offscreen.html index cfab53ee..afb7eb44 100644 --- a/ext/offscreen.html +++ b/ext/offscreen.html @@ -12,14 +12,6 @@ - diff --git a/ext/permissions.html b/ext/permissions.html index 7baff14e..61b0d363 100644 --- a/ext/permissions.html +++ b/ext/permissions.html @@ -14,14 +14,6 @@ - diff --git a/ext/popup-preview.html b/ext/popup-preview.html index 7bd54470..15810242 100644 --- a/ext/popup-preview.html +++ b/ext/popup-preview.html @@ -13,14 +13,6 @@ - diff --git a/ext/popup.html b/ext/popup.html index 6bc8d690..30e8a8c0 100644 --- a/ext/popup.html +++ b/ext/popup.html @@ -15,14 +15,6 @@ - diff --git a/ext/search.html b/ext/search.html index 377a966a..8c595cc4 100644 --- a/ext/search.html +++ b/ext/search.html @@ -16,14 +16,6 @@ - diff --git a/ext/settings.html b/ext/settings.html index 276a7222..346cc1d7 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -14,14 +14,6 @@ - diff --git a/ext/template-renderer.html b/ext/template-renderer.html index d1747e99..116f1a0c 100644 --- a/ext/template-renderer.html +++ b/ext/template-renderer.html @@ -11,14 +11,6 @@ - diff --git a/ext/welcome.html b/ext/welcome.html index 355bbc5f..40639881 100644 --- a/ext/welcome.html +++ b/ext/welcome.html @@ -13,14 +13,6 @@ - diff --git a/jsconfig.json b/jsconfig.json index 4f316174..0f780ead 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "*": ["./types/ext/*"] }, "types": [ diff --git a/test/japanese-util.test.js b/test/japanese-util.test.js index ae5b1a16..47da4ccb 100644 --- a/test/japanese-util.test.js +++ b/test/japanese-util.test.js @@ -16,10 +16,10 @@ * along with this program. If not, see . */ -import * as wanakana from 'wanakana'; import {expect, test} from 'vitest'; import {TextSourceMap} from '../ext/js/general/text-source-map.js'; import {JapaneseUtil} from '../ext/js/language/sandbox/japanese-util.js'; +import * as wanakana from '../ext/lib/wanakana.js'; const jp = new JapaneseUtil(wanakana); diff --git a/test/jsconfig.json b/test/jsconfig.json index 934aab81..2461fda9 100644 --- a/test/jsconfig.json +++ b/test/jsconfig.json @@ -12,7 +12,6 @@ "skipLibCheck": false, "baseUrl": ".", "paths": { - "zip.js": ["@zip.js/zip.js"], "*": ["../types/ext/*"], "dev/*": ["../types/dev/*"], "test/*": ["../types/test/*"] -- cgit v1.2.3 From ec67de5c0c4abc11232d3f3a8a8e9bb2fe045daa Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 23:05:37 -0500 Subject: Update library types --- ext/js/display/search-main.js | 4 +--- ext/js/language/sandbox/japanese-util.js | 4 ++-- ext/js/templates/sandbox/template-renderer.js | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) (limited to 'ext/js/templates') diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index c20cc135..5eee08d1 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -44,9 +44,7 @@ import {SearchPersistentStateController} from './search-persistent-state-control const {tabId, frameId} = await yomitan.api.frameInformationGet(); - /** @type {import('wanakana')} */ - const wanakanaLib = wanakana; - const japaneseUtil = new JapaneseUtil(wanakanaLib); + const japaneseUtil = new JapaneseUtil(wanakana); const hotkeyHandler = new HotkeyHandler(); hotkeyHandler.prepare(); diff --git a/ext/js/language/sandbox/japanese-util.js b/ext/js/language/sandbox/japanese-util.js index 4c9c46bd..6f4fc8e0 100644 --- a/ext/js/language/sandbox/japanese-util.js +++ b/ext/js/language/sandbox/japanese-util.js @@ -233,11 +233,11 @@ function getProlongedHiragana(previousCharacter) { export class JapaneseUtil { /** - * @param {?import('wanakana')} wanakana + * @param {?import('wanakana')|import('../../../lib/wanakana.js')} wanakana */ constructor(wanakana=null) { /** @type {?import('wanakana')} */ - this._wanakana = wanakana; + this._wanakana = /** @type {import('wanakana')} */ (wanakana); } // Character code testing functions diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js index fe240b5f..239240b6 100644 --- a/ext/js/templates/sandbox/template-renderer.js +++ b/ext/js/templates/sandbox/template-renderer.js @@ -197,7 +197,7 @@ export class TemplateRenderer { */ function wrapper(...args) { const argCountM1 = Math.max(0, args.length - 1); - const options = /** @type {Handlebars.HelperOptions} */ (args[argCountM1]); + const options = /** @type {import('handlebars').HelperOptions} */ (args[argCountM1]); args.length = argCountM1; return helper(args, this, options); } -- cgit v1.2.3 From 176f3fcc57752cd65100e558801d8f5e4bd4e419 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 29 Nov 2023 22:31:41 -0500 Subject: Fix bug --- ext/js/templates/sandbox/anki-template-renderer.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'ext/js/templates') diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js index 9f4bf6ff..dbf395e9 100644 --- a/ext/js/templates/sandbox/anki-template-renderer.js +++ b/ext/js/templates/sandbox/anki-template-renderer.js @@ -668,9 +668,8 @@ export class AnkiTemplateRenderer { * @type {import('template-renderer').HelperFunction} */ _formatGlossary(args, context, options) { - const [dictionary] = /** @type {[dictionary: string]} */ (args); + const [dictionary, content] = /** @type {[dictionary: string, content: import('dictionary-data').TermGlossary]} */ (args); const data = options.data.root; - const content = /** @type {import('dictionary-data').TermGlossary} */ (args[0]); if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } if (!(typeof content === 'object' && content !== null)) { return ''; } switch (content.type) { -- cgit v1.2.3