Skip to content

Try adding captions to the loading indicator. #83

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

Merged
merged 7 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
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
50 changes: 43 additions & 7 deletions src/wordpress-wasm/example-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ async function main() {

const progress = wireProgressBar();
const workerThread = await bootWordPress({
onWasmDownloadProgress: progress.partialObserver(bootProgress),
onWasmDownloadProgress: progress.partialObserver(
bootProgress,
'Preparing WordPress...'
),
});
const appMode = query.get('mode') === 'seamless' ? 'seamless' : 'browser';
if (appMode === 'browser') {
Expand All @@ -60,7 +63,12 @@ async function main() {
const fetchPluginFile = async (preinstallPlugin) => {
const response = cloneResponseMonitorProgress(
await fetch('/plugin-proxy?plugin=' + preinstallPlugin),
progress.partialObserver(progressBudgetPerPlugin * 0.66)
progress.partialObserver(
progressBudgetPerPlugin * 0.66,
`Installing ${zipNameToHumanName(
preinstallPlugin
)} plugin...`
)
);
return new File([await response.blob()], preinstallPlugin);
};
Expand Down Expand Up @@ -97,7 +105,10 @@ async function main() {
// Download the theme file
const response = cloneResponseMonitorProgress(
await fetch('/plugin-proxy?theme=' + preinstallTheme),
progress.partialObserver(installThemeProgress - 10)
progress.partialObserver(
installThemeProgress - 10,
`Installing ${zipNameToHumanName(preinstallTheme)} theme...`
)
);
const themeFile = new File([await response.blob()], preinstallTheme);

Expand Down Expand Up @@ -207,14 +218,23 @@ function wireProgressBar() {
};
wpFrame.addEventListener('load', HideProgressBar);

const progress = new ProgressObserver((progress, mode) => {
const progress = new ProgressObserver((progress, mode, caption) => {
const infiniteWrapper = document.querySelector(
'.progress-bar-wrapper.mode-infinite'
);
if (infiniteWrapper) {
infiniteWrapper.classList.remove('mode-infinite');
infiniteWrapper.classList.add('mode-finite');
}
if (caption && caption.length) {
const captionElement = document.querySelector(
'.progress-bar-overlay-caption'
) as HTMLElement;

if (captionElement) {
captionElement.innerText = caption;
}
}

const progressBarEl = document.querySelector(
'.progress-bar.is-finite'
Expand Down Expand Up @@ -247,18 +267,26 @@ const enum ProgressType {
class ProgressObserver {
#observedProgresses: Record<number, number> = {};
#lastObserverId = 0;
#onProgress: (progress: number, mode: ProgressType) => void;
#onProgress: (
progress: number,
mode: ProgressType,
caption?: string
) => void;

constructor(onProgress) {
this.#onProgress = onProgress;
}

partialObserver(progressBudget) {
partialObserver(progressBudget, caption = '') {
const id = ++this.#lastObserverId;
this.#observedProgresses[id] = 0;
return ({ loaded, total }) => {
this.#observedProgresses[id] = (loaded / total) * progressBudget;
this.#onProgress(this.totalProgress, ProgressType.REAL_TIME);
this.#onProgress(
this.totalProgress,
ProgressType.REAL_TIME,
caption
);
};
}

Expand All @@ -276,4 +304,12 @@ class ProgressObserver {
}
}

function zipNameToHumanName(zipName) {
const mixedCaseName = zipName.split('.').shift()!.replace('-', ' ');
return (
mixedCaseName.charAt(0).toUpperCase() +
mixedCaseName.slice(1).toLowerCase()
);
}

main();
14 changes: 14 additions & 0 deletions src/wordpress-wasm/wordpress.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
width: 100%;
margin: 0;
padding: 0;
font-family: arial, sans-serif;
}

body.browser-mode {
Expand Down Expand Up @@ -176,6 +177,11 @@
}

/* Progress bar: */
.progress-bar-overlay {
display: flex;
flex-direction: column;
}

body #wp,
body .progress-bar-overlay {
transition: opacity linear 0.5s;
Expand Down Expand Up @@ -277,6 +283,12 @@
}
}

.progress-bar-overlay-caption {
font-weight: 400;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
font-size: 1.1rem;
}

.ide-panel {
background: rgba(255,255,255,0.6);
max-height: 80vh;
Expand Down Expand Up @@ -323,6 +335,7 @@
</div>
<div class="main-content">
<div class="progress-bar-overlay">
<h3 class="progress-bar-overlay-caption">Preparing WordPress...</h3>
<div class="progress-bar-wrapper mode-infinite">
<div class="progress-bar is-infinite"></div>
<div class="progress-bar is-finite"></div>
Expand All @@ -349,6 +362,7 @@
</script>
<script type="template" id="seamless-mode">
<div class="progress-bar-overlay">
<h3 class="progress-bar-overlay-caption">Preparing WordPress...</h3>
<div class="progress-bar-wrapper mode-infinite">
<div class="progress-bar is-infinite"></div>
<div class="progress-bar is-finite"></div>
Expand Down