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

WIP: Embedded Playground #4421

Open
wants to merge 4 commits into
base: 1.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions website/src/_includes/analytics.njk
@@ -0,0 +1,13 @@
<script>
(function(f, a, t, h, o, m){
a[h]=a[h]||function(){
(a[h].q=a[h].q||[]).push(arguments)
};
o=f.createElement('script'),
m=f.getElementsByTagName('script')[0];
o.async=1; o.src=t; o.id='fathom-script';
m.parentNode.insertBefore(o,m)
})(document, window, 'https://cdn.usefathom.com/tracker.js', 'fathom');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefits does this tracker bring?

fathom('set', 'siteId', 'UDESFNRN');
fathom('trackPageview');
</script>
22 changes: 22 additions & 0 deletions website/src/_includes/header.njk
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>{{ title }} | PHPStan</title>
<meta property="og:title" content="{{ title }}" />
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link rel="stylesheet" href="/app.pcss">

{% if ogImage %}
<meta property="og:image" content="{{ ogImage }}" />
<meta name="twitter:image" content="{{ ogImage }}" />
{% else %}
<meta property="og:image" content="/images/logo-big.png" />
<meta name="twitter:image" content="/images/logo-big.png" />
{% endif %}
<link rel="icon" type="image/png" href="/images/favicon.png">

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@phpstan" />
</head>
37 changes: 2 additions & 35 deletions website/src/_layouts/_base.njk
@@ -1,25 +1,4 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>{{ title }} | PHPStan</title>
<meta property="og:title" content="{{ title }}" />
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link rel="stylesheet" href="/app.pcss">

{% if ogImage %}
<meta property="og:image" content="{{ ogImage }}" />
<meta name="twitter:image" content="{{ ogImage }}" />
{% else %}
<meta property="og:image" content="/images/logo-big.png" />
<meta name="twitter:image" content="/images/logo-big.png" />
{% endif %}
<link rel="icon" type="image/png" href="/images/favicon.png">

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@phpstan" />
</head>
{% include './../_includes/header.njk' %}
<body>

<div class="h-screen bg-white flex flex-col main-area">
Expand Down Expand Up @@ -97,19 +76,7 @@

{% block scripts %}{% endblock %}

<script>
(function(f, a, t, h, o, m){
a[h]=a[h]||function(){
(a[h].q=a[h].q||[]).push(arguments)
};
o=f.createElement('script'),
m=f.getElementsByTagName('script')[0];
o.async=1; o.src=t; o.id='fathom-script';
m.parentNode.insertBefore(o,m)
})(document, window, 'https://cdn.usefathom.com/tracker.js', 'fathom');
fathom('set', 'siteId', 'UDESFNRN');
fathom('trackPageview');
</script>
{% include './../_includes/analytics.njk' %}

</body>
</html>
11 changes: 11 additions & 0 deletions website/src/_layouts/_embed.njk
@@ -0,0 +1,11 @@
{% include './../_includes/header.njk' %}
<body class="w-screen min-h-screen">

{% block content %}{% endblock %}

{% block scripts %}{% endblock %}

{% include './../_includes/analytics.njk' %}

</body>
</html>
2 changes: 1 addition & 1 deletion website/src/app.pcss
Expand Up @@ -14,7 +14,7 @@
font-size: 15px;
height: auto;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
@apply border rounded-md my-8;
@apply border rounded-md;
}

@media(max-width: theme('screens.sm')) {
Expand Down
86 changes: 86 additions & 0 deletions website/src/embed/index.njk
@@ -0,0 +1,86 @@
---
title: Embedded Playground
pageType: playground
---

{% extends "./../_layouts/_embed.njk" %}

{% block scripts %}
<script src="../js/playground.ts"></script>
{% endblock %}

{% block content %}
<div class="p-4">
<div data-bind="visible: false" class="mx-auto w-12 h-12 my-12">
<div class="spinner"></div>
</div>
<div style="display: none" data-bind="visible: true">
<form action="#" method="post">
<textarea data-bind="codeMirror: code, codeMirrorLines: currentTab() !== null ? currentTab().errorLines : []"></textarea>
</form>

<!-- ko if: isLoading -->
<div class="m-auto w-12 h-12 mt-4">
<div class="spinner"></div>
</div>
<!-- /ko -->

<!-- ko if: hasServerError -->
<span class="inline-flex text-red-500 mb-8">An error occured. Please try again later.</span>
<!-- /ko -->

<!-- ko ifnot: hasServerError -->

<!-- ko if: tabs().length > 1 && !isLoading() && legacyResult() === null -->
<nav class="flex mt-4 border-b border-gray-400">
<!-- ko foreach: tabs -->
<a href="#" class="relative px-3 py-2 font-medium text-md mr-4 leading-5 rounded-md rounded-b-none text-gray-500 border-l border-t border-r" data-bind="click: $parent.switchTab.bind($parent, $index()), text: title, class: tabClass, style: { 'top': isActive() ? '1px' : '0' }"></a>
<!-- /ko -->
</nav>
<!-- /ko -->

<!-- ko if: currentTab() !== null && !isLoading() && legacyResult() === null -->
<!-- ko with: currentTab() -->
<div class="flex items-stretch mx-4 md:mx-0">
<!-- ko if: errors.length > 0 && $parent.tabs().length < 2 -->
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-red-100 text-red-900 mt-4" data-bind="text: errorsText"></span>
<!-- /ko -->

<!-- ko if: errors.length === 0 -->
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-green-100 text-green-900 mt-4">
No errors!
</span>
<!-- /ko -->
</div>
<!-- ko if: errors.length > 0 -->
<table class="w-full mt-4">
<thead>
<tr>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-base leading-4 font-medium text-gray-500 tracking-wider">
Line
</th>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-base leading-4 font-medium text-gray-500 tracking-wider">
Error
</th>
</tr>
</thead>
<tbody class="bg-white">
<!-- ko foreach: errors -->
<tr>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-base leading-5 font-medium text-gray-500" data-bind="text: line"></td>
<td class="px-6 py-4 border-b border-gray-200 text-base leading-5 text-gray-900" data-bind="text: message"></td>
</tr>
<!-- /ko -->
</tbody>
</table>
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->

<!-- ko if: legacyResult() !== null && !isLoading() -->
<pre data-bind="html: legacyResult" class="bg-gray-800 w-auto p-4 mt-4 leading-none"></pre>
<!-- /ko -->
<!-- /ko -->
</div>
</div>
{% endblock %}
12 changes: 6 additions & 6 deletions website/src/index.njk
Expand Up @@ -96,7 +96,7 @@ pageType: playground
</form>

<!-- ko if: isLoading -->
<div class="m-auto w-12 h-12 mt-8">
<div class="m-auto w-12 h-12 mt-4">
<div class="spinner"></div>
</div>
<!-- /ko -->
Expand All @@ -108,7 +108,7 @@ pageType: playground
<!-- ko ifnot: hasServerError -->

<!-- ko if: tabs().length > 1 && !isLoading() && legacyResult() === null -->
<nav class="flex mt-8 border-b border-gray-400">
<nav class="flex mt-4 border-b border-gray-400">
<!-- ko foreach: tabs -->
<a href="#" class="relative px-3 py-2 font-medium text-md mr-4 leading-5 rounded-md rounded-b-none text-gray-500 border-l border-t border-r" data-bind="click: $parent.switchTab.bind($parent, $index()), text: title, class: tabClass, style: { 'top': isActive() ? '1px' : '0' }"></a>
<!-- /ko -->
Expand All @@ -119,17 +119,17 @@ pageType: playground
<!-- ko with: currentTab() -->
<div class="flex items-stretch mx-4 md:mx-0">
<!-- ko if: errors.length > 0 && $parent.tabs().length < 2 -->
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-red-100 text-red-900 mt-8" data-bind="text: errorsText"></span>
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-red-100 text-red-900 mt-4" data-bind="text: errorsText"></span>
<!-- /ko -->

<!-- ko if: errors.length === 0 -->
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-green-100 text-green-900 mt-8">
<span class="inline-flex flex-grow justify-center md:flex-grow-0 items-center px-4 py-3 h-12 rounded-lg text-lg font-medium leading-4 bg-green-100 text-green-900 mt-4">
No errors!
</span>
<!-- /ko -->
</div>
<!-- ko if: errors.length > 0 -->
<table class="w-full mt-8">
<table class="w-full mt-4">
<thead>
<tr>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-base leading-4 font-medium text-gray-500 tracking-wider">
Expand All @@ -154,7 +154,7 @@ pageType: playground
<!-- /ko -->

<!-- ko if: legacyResult() !== null && !isLoading() -->
<pre data-bind="html: legacyResult" class="bg-gray-800 w-auto p-4 mt-8 leading-none"></pre>
<pre data-bind="html: legacyResult" class="bg-gray-800 w-auto p-4 mt-4 leading-none"></pre>
<!-- /ko -->
<!-- /ko -->

Expand Down
18 changes: 14 additions & 4 deletions website/src/js/PlaygroundViewModel.ts
Expand Up @@ -28,6 +28,8 @@ export class PlaygroundViewModel {
id: string | null;
hasServerError: ko.Observable<boolean>;

embed: ko.Observable<boolean>;

apiBaseUrl: string = 'https://api.phpstan.org';

constructor() {
Expand Down Expand Up @@ -63,6 +65,8 @@ export class PlaygroundViewModel {
this.shareXhr = null;
this.id = null;
this.hasServerError = ko.observable(false);

this.embed = ko.observable(false);
}

switchTab(index: number): void {
Expand All @@ -81,10 +85,11 @@ export class PlaygroundViewModel {

setId(id: string | null): void {
this.id = id;
const prefix = this.embed() ? '/embed' : '';
if (id === null) {
window.history.replaceState({}, '', '/');
window.history.replaceState({}, '', `${prefix}/`);
} else{
window.history.replaceState({}, '', '/r/' + id);
window.history.replaceState({}, '', `${prefix}/r/${id}`);
}
}

Expand Down Expand Up @@ -184,15 +189,20 @@ export class PlaygroundViewModel {
}

init(path: string, initCallback: () => void): void {
const legacyHashMatch = path.match(/^\/r\/([a-f0-9]{32})$/);
const embedMatch = path.match(/^\/embed\/.*$/);
if (embedMatch !== null) {
this.embed(true);
}

const legacyHashMatch = path.match(/^\/(?:embed\/)?r\/([a-f0-9]{32})$/);
let resultUrl = null;
let id: string | null = null;
if (legacyHashMatch !== null) {
id = legacyHashMatch[1];
resultUrl = this.apiBaseUrl + '/legacyResult?id=' + id;
}

const hashMatch = path.match(/^\/r\/([0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})$/i);
const hashMatch = path.match(/^\/(?:embed\/)?r\/([0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})$/i);
if (hashMatch !== null) {
id = hashMatch[1];
resultUrl = this.apiBaseUrl + '/result?id=' + id;
Expand Down