From eddd0288643f08d2a2c85f73575bc7ee1c157539 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 19 May 2021 18:24:50 -0400 Subject: Add support for definitions with structured content (#1689) * Add structured content to schema * Add support for generating custom content * Update importer * Update test data * Add verticalAlign property --- ext/js/language/dictionary-importer.js | 50 ++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'ext/js/language/dictionary-importer.js') diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index 051375a0..4d885a74 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -300,20 +300,58 @@ class DictionaryImporter { return data.text; case 'image': return await this._formatDictionaryTermGlossaryImage(data, context, entry); + case 'structured-content': + return await this._formatStructuredContent(data, context, entry); default: throw new Error(`Unhandled data type: ${data.type}`); } } async _formatDictionaryTermGlossaryImage(data, context, entry) { + return await this._createImageData(data, context, entry, {type: 'image'}); + } + + async _formatStructuredContent(data, context, entry) { + const content = await this._prepareStructuredContent(data.content, context, entry); + return { + type: 'structured-content', + content + }; + } + + async _prepareStructuredContent(content, context, entry) { + if (typeof content === 'string' || !(typeof content === 'object' && content !== null)) { + return content; + } + if (Array.isArray(content)) { + for (let i = 0, ii = content.length; i < ii; ++i) { + content[i] = await this._prepareStructuredContent(content[i], context, entry); + } + return content; + } + const {tag} = content; + switch (tag) { + case 'img': + return await this._prepareStructuredContentImage(content, context, entry); + } + const childContent = content.content; + if (typeof childContent !== 'undefined') { + content.content = await this._prepareStructuredContent(childContent, context, entry); + } + return content; + } + + async _prepareStructuredContentImage(content, context, entry) { + const {verticalAlign} = content; + const result = await this._createImageData(content, context, entry, {tag: 'img'}); + if (typeof verticalAlign === 'string') { result.verticalAlign = verticalAlign; } + return result; + } + + async _createImageData(data, context, entry, attributes) { const {path, width: preferredWidth, height: preferredHeight, title, description, pixelated, collapsed, collapsible} = data; const {width, height} = await this._getImageMedia(path, context, entry); - const newData = { - type: 'image', - path, - width, - height - }; + const newData = Object.assign({}, attributes, {path, width, height}); if (typeof preferredWidth === 'number') { newData.preferredWidth = preferredWidth; } if (typeof preferredHeight === 'number') { newData.preferredHeight = preferredHeight; } if (typeof title === 'string') { newData.title = title; } -- cgit v1.2.3