From 2820429e687917b4e366e194e55e5ee403576b1b Mon Sep 17 00:00:00 2001 From: Andrew Top Date: Thu, 24 Oct 2024 15:40:54 -0400 Subject: [PATCH] #442 Manage templated URL lists --- css/import/import.css | 17 ++++++++++- import-bulk.html | 5 +++- import.html | 5 +++- js/import/import.ui.js | 6 ++++ js/shared/urlmanagement.js | 58 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 js/shared/urlmanagement.js diff --git a/css/import/import.css b/css/import/import.css index 05f9334a..387c1767 100644 --- a/css/import/import.css +++ b/css/import/import.css @@ -29,6 +29,21 @@ height: calc(100vh - 232px); } +.import .spread-contents { + display: flex; + flex-direction: row; + justify-content: space-between; + column-gap: 6px; +} + +.import .spread-contents> *:last-child { + min-width: 6rem; +} + +.import #import-urls textarea { + resize: vertical !important; +} + /* stylelint-disable-next-line selector-class-pattern */ .import .CodeMirror { position: absolute; @@ -192,4 +207,4 @@ .import #import-result sp-icon-alert { color: rgb(215 25 19); -} \ No newline at end of file +} diff --git a/import-bulk.html b/import-bulk.html index c7f9a445..005ebe9b 100644 --- a/import-bulk.html +++ b/import-bulk.html @@ -41,7 +41,10 @@

Import - Bulk

Transformation file URL - +
+ + Set URLs +
Page load timeout diff --git a/import.html b/import.html index ec99f12d..f9addf3c 100644 --- a/import.html +++ b/import.html @@ -48,7 +48,10 @@

Import - Workbench

Transformation file URL - +
+ + Set URL +
Page load timeout diff --git a/js/import/import.ui.js b/js/import/import.ui.js index 02c3704b..03da06fe 100644 --- a/js/import/import.ui.js +++ b/js/import/import.ui.js @@ -12,6 +12,7 @@ /* global CodeMirror, html_beautify, ExcelJS, WebImporter */ import { initOptionFields, attachOptionFieldsListeners } from '../shared/fields.js'; import { getDirectoryHandle, saveFile } from '../shared/filesystem.js'; +import { setURLFromFile } from '../shared/urlmanagement.js'; import { asyncForEach } from '../shared/utils.js'; import PollImporter from '../shared/pollimporter.js'; import alert from '../shared/alert.js'; @@ -23,6 +24,7 @@ const CONFIG_PARENT_SELECTOR = `${PARENT_SELECTOR} form`; const PREVIEW_CONTAINER = document.querySelector(`${PARENT_SELECTOR} .page-preview`); const IMPORTFILEURL_FIELD = document.getElementById('import-file-url'); +const IMPORTURLS_BUTTON = document.getElementById('import-set-urls'); const IMPORT_BUTTON = document.getElementById('import-doimport-button'); // const SAVEASWORD_BUTTON = document.getElementById('saveAsWord'); @@ -614,6 +616,10 @@ const attachListeners = () => { } }); + IMPORTURLS_BUTTON.addEventListener('click', async (event) => { + await setURLFromFile(IMPORTFILEURL_FIELD.value, IS_BULK, event.ctrlKey); + }); + DOWNLOAD_IMPORT_REPORT_BUTTON.addEventListener('click', (async () => { const buffer = await getReport(); const a = document.createElement('a'); diff --git a/js/shared/urlmanagement.js b/js/shared/urlmanagement.js new file mode 100644 index 00000000..33edad46 --- /dev/null +++ b/js/shared/urlmanagement.js @@ -0,0 +1,58 @@ +import alert from './alert.js'; + +/** + * Read a URL file paired with the JS file. If any lines contain a valid URL, add them to the + * URL(s) field. + * @param importJsUrl + * @param isBulk + * @param randomize + * @returns {Promise} + */ +const setURLFromFile = async (importJsUrl, isBulk, randomize) => { + if (!importJsUrl || !importJsUrl.endsWith('.js')) { + alert.error('No valid transition file URL set.'); + return; + } + const urlsField = document.getElementById(isBulk ? 'import-urls' : 'import-url'); + const urlFile = importJsUrl.replace('.js', '.txt'); + const response = await fetch(urlFile); + if (!response.ok) { + alert.error(`Failed to read the URL file: ${urlFile}`); + return; + } + + const urls = await response.text(); + const urlsArray = urls.split('\n') + .map((nextUrl) => nextUrl.trim()) + .filter((url) => { + try { + // eslint-disable-next-line no-new + new URL(url); + } catch (error) { + return false; + } + return true; + }); + + if (urlsArray.length === 0) { + alert.error(`No valid URLs found in the URL file: ${urlFile}`); + return; + } + + if (!isBulk) { + let index = 0; + if (randomize) { + index = Math.floor(Math.random() * urlsArray.length - 0.01); + } + // eslint-disable-next-line prefer-destructuring + urlsField.value = urlsArray[index]; + } else { + urlsField.value = urlsArray.join('\n'); + } + urlsField.dispatchEvent(new Event('change')); +}; + +export { + // eslint-disable-next-line import/prefer-default-export + setURLFromFile, +};