Files
TasGrid/test-embed.html
T
2026-02-27 13:39:32 -06:00

313 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TasGrid Parent App Simulator</title>
<style>
body {
font-family: system-ui;
padding: 20px;
background: #f4f4f5;
max-width: 1200px;
margin: 0 auto;
}
.container {
display: flex;
gap: 20px;
margin-top: 20px;
}
.iframe-container {
flex: 1;
border: 1px solid #ddd;
background: white;
border-radius: 8px;
overflow: hidden;
display: flex;
flex-direction: column;
height: 600px;
}
.controls {
background: white;
padding: 20px;
border-radius: 8px;
border: 1px solid #ddd;
margin-bottom: 20px;
}
.login-form {
display: grid;
gap: 10px;
max-width: 400px;
}
.login-form input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.login-form button {
padding: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
.login-form button:hover {
background: #0056b3;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
display: none;
}
.status.success {
background: #d4edda;
color: #155724;
display: block;
}
.status.error {
background: #f8d7da;
color: #721c24;
display: block;
}
iframe {
border: none;
width: 100%;
height: 100%;
}
button.send-manual {
padding: 8px 16px;
cursor: pointer;
margin-top: 10px;
}
input.manual-token {
padding: 8px;
width: 100%;
box-sizing: border-box;
}
h1 {
margin-top: 0;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/pocketbase/dist/pocketbase.umd.js"></script>
</head>
<body>
<h1>TasGrid Parent App Simulator</h1>
<div class="controls">
<div id="login-section">
<h3>Login to Parent Application</h3>
<p>This simulates the parent application authenticating with the shared backend.</p>
<div class="login-form">
<input type="email" id="email" placeholder="Email" value="">
<input type="password" id="password" placeholder="Password" value="">
<button onclick="login()">Login & Sync Iframes</button>
</div>
<div id="status" class="status"></div>
</div>
<div id="api-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
<h3>Note Creation API</h3>
<div style="display: flex; gap: 10px; align-items: center;">
<button class="send-manual" onclick="createNote()"
style="margin-top: 0; background: #28a745; color: white; border: none; border-radius: 4px; padding: 10px 15px; font-weight: bold; cursor: pointer;">Create
Sample Note</button>
<div id="api-status" style="font-size: 12px; color: #666;"></div>
</div>
<div id="api-result"
style="margin-top: 10px; font-family: monospace; font-size: 11px; background: #eee; padding: 10px; border-radius: 4px; display: none; word-break: break-all;">
</div>
</div>
<div id="manual-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
<p style="font-size: 13px; color: #666;">Or send a token manually:</p>
<input type="text" id="token" class="manual-token" placeholder="Paste PB Token here...">
<button class="send-manual" onclick="sendAuthFromInput()">Send Manual Token</button>
</div>
<div id="docs-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
<details>
<summary style="cursor: pointer; font-weight: bold; color: #007bff;">Developer Integration Guide
</summary>
<div style="font-size: 14px; line-height: 1.6; margin-top: 15px;">
<p><strong>1. Available Routes:</strong></p>
<ul>
<li><code>/embed/notes</code>: Renders the full notepad/editor.</li>
<li><code>/embed/quick-add</code>: Renders a standalone task creation form.</li>
</ul>
<p><strong>2. Authentication Protocol:</strong></p>
<p>The parent application must send a <code>postMessage</code> to the iframe immediately after the
iframe loads (or whenever auth state changes).</p>
<pre
style="background: #f8f9fa; padding: 15px; border-radius: 4px; border: 1px solid #e9ecef; overflow-x: auto;">
iframe.contentWindow.postMessage({
type: "TASGRID_AUTH",
token: "YOUR_POCKETBASE_TOKEN",
user: { /* optional PB user record */ }
}, "*");</pre>
<p><strong>4. Note Creation API (TASGRID_CREATE_NOTE):</strong></p>
<p>Trigger note creation from the parent app. TasGrid will respond with
<code>TASGRID_NOTE_CREATED</code>.</p>
<pre
style="background: #f8f9fa; padding: 15px; border-radius: 4px; border: 1px solid #e9ecef; overflow-x: auto;">
// Request
iframe.contentWindow.postMessage({
type: "TASGRID_CREATE_NOTE",
note: {
title: "Project Alpha",
content: "Initial requirements...",
tags: ["Project", "2024"],
isPrivate: false
}
}, "*");
// Response Listener
window.addEventListener("message", (event) => {
if (event.data.type === "TASGRID_NOTE_CREATED") {
console.log("New Note ID:", event.data.noteId);
}
});</pre>
</div>
</details>
</div>
</div>
<div class="container">
<div class="iframe-container">
<div
style="padding: 10px; background: #eee; border-bottom: 1px solid #ddd; font-weight: bold; font-size: 12px; display: flex; justify-content: space-between;">
<span>Notes Embed</span>
<code style="font-size: 10px;">/embed/notes</code>
</div>
<iframe id="notes-iframe" src="http://localhost:4000/embed/notes"></iframe>
</div>
<div class="iframe-container">
<div
style="padding: 10px; background: #eee; border-bottom: 1px solid #ddd; font-weight: bold; font-size: 12px; display: flex; justify-content: space-between;">
<span>Quick Add Embed</span>
<code style="font-size: 10px;">/embed/quick-add</code>
</div>
<iframe id="quick-add-iframe" src="http://localhost:4000/embed/quick-add"></iframe>
</div>
</div>
<script>
const pb = new PocketBase('https://pocketbase.ccllc.pro');
const statusEl = document.getElementById('status');
async function login() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
statusEl.className = 'status';
statusEl.textContent = 'Logging in...';
statusEl.style.display = 'block';
try {
const authData = await pb.collection('users').authWithPassword(email, password);
statusEl.className = 'status success';
statusEl.textContent = `Logged in as ${authData.record.email}! Syncing iframes...`;
// Automatically send auth to iframes
sendAuth(pb.authStore.token, authData.record);
} catch (err) {
statusEl.className = 'status error';
statusEl.textContent = 'Login failed: ' + err.message;
}
}
function sendAuthFromInput() {
const token = document.getElementById('token').value;
if (!token) {
alert("Please enter a token first");
return;
}
sendAuth(token, null);
}
function sendAuth(token, user) {
const payload = {
type: "TASGRID_AUTH",
token: token,
user: user
};
const notesIframe = document.getElementById('notes-iframe').contentWindow;
const quickAddIframe = document.getElementById('quick-add-iframe').contentWindow;
console.log("Sending auth to iframes...", payload);
notesIframe.postMessage(payload, "*");
quickAddIframe.postMessage(payload, "*");
}
async function createNote() {
const apiStatus = document.getElementById('api-status');
const apiResult = document.getElementById('api-result');
apiStatus.textContent = "Sending creation request...";
apiResult.style.display = 'none';
const payload = {
type: "TASGRID_CREATE_NOTE",
note: {
title: "Test Note from Parent " + new Date().toLocaleTimeString(),
content: "This note was created automatically via the postMessage API.",
tags: ["API-TEST", "SISTER-APP"],
isPrivate: false
}
};
// We can send to either iframe since EmbedAuthWrapper handles it globally for all embedded views
document.getElementById('notes-iframe').contentWindow.postMessage(payload, "*");
}
// Listen for responses
window.addEventListener("message", (event) => {
const { data } = event;
console.log("Parent received message:", data);
if (data.type === "TASGRID_NOTE_CREATED") {
const apiStatus = document.getElementById('api-status');
const apiResult = document.getElementById('api-result');
apiStatus.innerHTML = `<span style="color: green; font-weight: bold;">Note Created! ID: ${data.noteId}</span>`;
apiResult.textContent = JSON.stringify(data, null, 2);
apiResult.style.display = 'block';
}
});
// Handle case where user refreshed but is still logged in to the parent
window.addEventListener('load', () => {
if (pb.authStore.isValid && pb.authStore.model) {
document.getElementById('email').value = pb.authStore.model.email || "";
statusEl.className = 'status success';
statusEl.textContent = 'Session restored. Click login or send manual token to sync.';
statusEl.style.display = 'block';
}
});
</script>
</body>
</html>