Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update test previewer #2637

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions www/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,30 @@ type TestList = {[key: string]: Test[]};

function onTestChange(browserTests: Test[]) {
if (browserSelector) {
const currentSelection = browserSelector.value;
while (browserSelector.firstChild) {
browserSelector.firstChild.remove();
}

let newSelection;

browserTests.forEach((browser, i) => {
if (i === 0) {
onBrowserChange(browser);
newSelection = browser;
}
const option = document.createElement('option');
option.value = browser.id;
if (browser.id === currentSelection) {
option.selected = true;
newSelection = browser;
}
option.textContent = browser.id.replace(/_/g, ' ');
browserSelector.appendChild(option);
});

if (newSelection) {
onBrowserChange(newSelection);
}
}
}

Expand All @@ -48,13 +60,20 @@ function onBrowserChange(browserTest: Test) {
previewImage.style.transformOrigin = '';
}
}

if (history) {
history.replaceState(null, document.title, `?browser=${browserSelector?.value}&test=${testSelector?.value}`);
}
}

function selectTest(testName: string) {
if (testLink) {
testLink.textContent = testLink.href = testName;
const foundTest = testList[testName];
if (foundTest) {
if (testLink) {
testLink.textContent = testLink.href = testName;
}
onTestChange(foundTest);
}
onTestChange(testList[testName]);
}

const UP_ARROW = 38;
Expand Down Expand Up @@ -116,15 +135,29 @@ if (testSelector && browserSelector) {
false
);

const tests: string[] = Object.keys(testList);
tests.forEach((testName, i) => {
if (i === 0) {
selectTest(testName);
let testFromUrl: string | null = null;

if (URLSearchParams) {
const url = new URLSearchParams(location.search);
testFromUrl = url.get('test');
if (browserSelector) {
const option = document.createElement('option');
browserSelector.appendChild(option);
browserSelector.value = option.value = url.get('browser') ?? '';
}
}

const tests: string[] = Object.keys(testList);
tests.forEach((testName) => {
const option = document.createElement('option');
option.value = testName;
option.textContent = testName;
if (option.value === testFromUrl) {
option.selected = true;
}

testSelector.appendChild(option);
});

selectTest(testSelector.value ?? testSelector.firstChild?.textContent ?? '');
}