Skip to content

Commit 5d993e8

Browse files
committed
Add PR preview handling
1 parent 83248b2 commit 5d993e8

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

html-api-debugger/interactivity.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function generate_page( string $html, array $options ): string {
6262
'hoverInfo' => 'breadcrumbs',
6363
'hoverBreadcrumbs' => true,
6464
'hoverInsertion' => false,
65+
'checkingForPRPlaygroundLink' => false,
6566

6667
'htmlApiDoctypeName' => $htmlapi_response['result']['doctypeName'] ?? '[unknown]',
6768
'htmlApiDoctypePublicId' => $htmlapi_response['result']['doctypePublicId'] ?? '[unknown]',
@@ -188,9 +189,22 @@ class="col"
188189
</td>
189190
</tr>
190191
<tr>
191-
<td>
192+
<td colspan="2">
192193
<p>
193-
<button <?php wp_on_directive( 'click', 'handleCopyClick' ); ?> type="button">Copy shareable playground link</button>
194+
<label>
195+
<select id="htmlapi-wp-version">
196+
<option value="latest">latest</option>
197+
<option value="beta">beta</option>
198+
<option value="nightly">nightly</option>
199+
</select>
200+
</label>
201+
<button <?php wp_on_directive( 'click', 'handleCopyClick' ); ?> type="button">Copy shareable playground link</button><br>
202+
<label>
203+
<code>wordpress/develop</code> PR number:
204+
<input type="number" min="1" <?php wp_on_directive( 'input', 'handleCopyPrInput' ); ?>>
205+
</label>
206+
<button <?php wp_on_directive( 'click', 'handleCopyPrClick' ); ?>>Copy shareable playground link to PR</button>
207+
<button <?php wp_on_directive( 'click', 'handleCheckPrClick' ); ?> data-wp-bind--disabled="state.checkingForPRPlaygroundLink">Check PR</button>
194208
</p>
195209
<details>
196210
<summary>debug response</summary>

html-api-debugger/view.js

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ let debounceInputAbortController = null;
5151
* @property {string} htmlPreambleForProcessing
5252
* @property {string} formattedHtmlapiResponse
5353
* @property {HtmlApiResponse} htmlapiResponse
54-
* @property {string} playgroundLink
54+
* @property {URL} playgroundLink
5555
* @property {string} html
5656
* @property {string} htmlForProcessing
5757
* @property {boolean} showClosers
5858
* @property {boolean} showInvisible
5959
* @property {boolean} showVirtual
6060
* @property {boolean} quirksMode
6161
* @property {boolean} fullParser
62+
* @property {number} previewPrNumber
63+
* @property {boolean} checkingForPRPlaygroundLink
6264
*
6365
* @property {'breadcrumbs'|'insertionMode'} hoverInfo
6466
* @property {boolean} hoverBreadcrumbs
@@ -84,6 +86,10 @@ let debounceInputAbortController = null;
8486
* @property {()=>void} handleShowVirtualClick
8587
* @property {()=>Promise<void>} handleQuirksModeClick
8688
* @property {()=>Promise<void>} handleFullParserClick
89+
*
90+
* @property {()=>void} handleCopyClick
91+
* @property {()=>void} handleCopyPrInput
92+
* @property {()=>void} handleCopyPrClick
8793
*/
8894

8995
/** @type {typeof I.store<Store>} */
@@ -143,7 +149,7 @@ const store = createStore(NS, {
143149
'https://playground.wordpress.net/?plugin=html-api-debugger&php-extension-bundle=light',
144150
);
145151
u.searchParams.set('url', `${base}?${searchParams.toString()}`);
146-
return u.href;
152+
return u;
147153
},
148154

149155
get htmlPreambleForProcessing() {
@@ -275,7 +281,17 @@ const store = createStore(NS, {
275281
},
276282

277283
handleCopyClick: function* () {
278-
yield navigator.clipboard.writeText(store.state.playgroundLink);
284+
const url = new URL(store.state.playgroundLink);
285+
286+
// @ts-expect-error This better exist.
287+
const wpVersion = document.getElementById('htmlapi-wp-version').value;
288+
url.searchParams.set('wp', wpVersion);
289+
290+
try {
291+
yield navigator.clipboard.writeText(url.href);
292+
} catch {
293+
alert('Copy failed, make sure the browser window is focused.');
294+
}
279295
},
280296

281297
/** @param {Event} e */
@@ -428,6 +444,71 @@ const store = createStore(NS, {
428444
);
429445
}
430446
},
447+
448+
/** @param {InputEvent} e */
449+
handleCopyPrInput(e) {
450+
const val = /** @type {HTMLInputElement} */ (e.target).valueAsNumber;
451+
if (Number.isFinite(val) && val > 0) {
452+
store.state.previewPrNumber = val;
453+
return;
454+
}
455+
store.state.previewPrNumber = val;
456+
},
457+
458+
handleCopyPrClick: function* () {
459+
const prNumber = store.state.previewPrNumber;
460+
const playgroundLink = new URL(store.state.playgroundLink);
461+
if (!prNumber) {
462+
alert('Please enter a PR number.');
463+
return;
464+
}
465+
const url = new URL(
466+
'https://playground.wordpress.net/plugin-proxy.php?org=WordPress&repo=wordpress-develop&workflow=Test%20Build%20Processes',
467+
);
468+
url.searchParams.set('artifact', `wordpress-build-${prNumber}`);
469+
url.searchParams.set('pr', prNumber.toString(10));
470+
471+
try {
472+
playgroundLink.searchParams.set('wp', url.href);
473+
yield navigator.clipboard.writeText(playgroundLink.href);
474+
} catch {
475+
alert('Copy failed, make sure the browser window is focused.');
476+
}
477+
},
478+
479+
handleCheckPrClick: function* () {
480+
if (store.state.checkingForPRPlaygroundLink) {
481+
return;
482+
}
483+
484+
const prNumber = store.state.previewPrNumber;
485+
if (!prNumber) {
486+
alert('Please enter a PR number.');
487+
return;
488+
}
489+
490+
try {
491+
store.state.checkingForPRPlaygroundLink = true;
492+
493+
const url = new URL(
494+
'https://playground.wordpress.net/plugin-proxy.php?org=WordPress&repo=wordpress-develop&workflow=Test%20Build%20Processes',
495+
);
496+
url.searchParams.set('artifact', `wordpress-build-${prNumber}`);
497+
url.searchParams.set('pr', prNumber.toString(10));
498+
url.searchParams.set('verify_only', 'true');
499+
/** @type {Response} */
500+
const response = yield fetch(url.href, {
501+
method: 'GET',
502+
});
503+
if (!response.ok) {
504+
alert('The PR number is not valid or has not been built yet.');
505+
return;
506+
}
507+
alert('The PR number looks good!');
508+
} finally {
509+
store.state.checkingForPRPlaygroundLink = false;
510+
}
511+
},
431512
});
432513

433514
/** @param {keyof State} stateKey */

0 commit comments

Comments
 (0)