Skip to content

Commit

Permalink
adobe#442 Manage templated URL lists
Browse files Browse the repository at this point in the history
  • Loading branch information
atopper committed Oct 24, 2024
1 parent 06f86a9 commit 2820429
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
17 changes: 16 additions & 1 deletion css/import/import.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,4 +207,4 @@

.import #import-result sp-icon-alert {
color: rgb(215 25 19);
}
}
5 changes: 4 additions & 1 deletion import-bulk.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ <h2>Import - Bulk</h2>
<sp-accordion-item label="Import Options">
<div>
<sp-field-label for="import-file-url" required>Transformation file URL</sp-field-label>
<sp-textfield class="option-field" id="import-file-url" type="url" value="http://localhost:3001/tools/importer/import.js"></sp-textfield>
<div class="spread-contents">
<sp-textfield class="option-field" id="import-file-url" type="url" value="http://localhost:3001/tools/importer/import.js"></sp-textfield>
<sp-button variant="secondary" id="import-set-urls" title="Set URLs from text file (.txt) paired with the transformation file.">Set URLs</sp-button>
</div>

<sp-field-label for="import-pageload-timeout">Page load timeout</sp-field-label>
<sp-number-field class="option-field" id="import-pageload-timeout" value="100" min="0" step="100" format-options='{ "style": "unit", "unit": "millisecond", "unitDisplay": "short" }'></sp-number-field>
Expand Down
5 changes: 4 additions & 1 deletion import.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ <h2>Import - Workbench</h2>
<sp-accordion-item label="Import Options">
<div>
<sp-field-label for="import-file-url" required>Transformation file URL</sp-field-label>
<sp-textfield class="option-field" id="import-file-url" type="url" value="http://localhost:3001/tools/importer/import.js"></sp-textfield>
<div class="spread-contents">
<sp-textfield class="option-field" id="import-file-url" type="url" value="http://localhost:3001/tools/importer/import.js"></sp-textfield>
<sp-button variant="secondary" id="import-set-urls" title="Set 1 URL from text file (.txt) paired with the transformation file. Press Ctrl to randomize choice.">Set URL</sp-button>
</div>

<sp-field-label for="import-pageload-timeout">Page load timeout</sp-field-label>
<sp-number-field class="option-field" id="import-pageload-timeout" value="100" min="0" step="100" format-options='{ "style": "unit", "unit": "millisecond", "unitDisplay": "short" }'></sp-number-field>
Expand Down
6 changes: 6 additions & 0 deletions js/import/import.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
58 changes: 58 additions & 0 deletions js/shared/urlmanagement.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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,
};

0 comments on commit 2820429

Please sign in to comment.