File size: 5,873 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Multi Block Prompt Solution</title>
<style>
:root { --sidebar-width: 240px; --content-max-width: 900px; --sidebar-gap: 32px; }
body { margin: 0; font-family: Arial, sans-serif; }
.app { display: flex; gap: var(--sidebar-gap); height: 100vh; overflow: hidden; background: #f7f8fb; }
[data-testid="sidebar"] { width: var(--sidebar-width); flex-shrink: 0; padding: 16px; background: #1f2937; color: white; overflow-y: auto; box-sizing: border-box; }
[data-testid="sidebar"]::-webkit-scrollbar { width: 8px; }
[data-testid="sidebar"]::-webkit-scrollbar-thumb { background: #64748b; border-radius: 4px; }
[data-testid="content"] { flex: 1; min-width: 0; max-width: var(--content-max-width); margin: 0 auto; padding: 16px; box-sizing: border-box; overflow-y: auto; }
.custom-scrollbar::-webkit-scrollbar { width: 8px; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #64748b; border-radius: 4px; }
@media (max-width: 600px) { .app { gap: 8px; } [data-testid="sidebar"] { width: 120px; padding: 8px; } }
[data-testid="prompt-response-block"] { border: 1px solid #c5cbd5; background: white; padding: 12px; margin-bottom: 12px; border-radius: 6px; }
textarea { width: 100%; min-height: 56px; box-sizing: border-box; }
output { display: block; margin-top: 8px; }
[data-testid="new-block-placeholder"] { display: none; padding: 10px 12px; border: 1px dashed #7c8798; background: white; }
[data-testid="new-block-placeholder"].visible { display: inline-block; }
</style>
</head>
<body>
<div class="app">
<aside class="custom-scrollbar" data-testid="sidebar"><h2>Blocks</h2><ol data-testid="sidebar-list"></ol></aside>
<main class="custom-scrollbar" data-testid="content">
<section data-testid="blocks"></section>
<button data-testid="new-block-placeholder">New block</button>
</main>
</div>
<script>
class PromptBlock {
constructor(id) {
this.id = id;
this.prompt = '';
this.status = 'idle';
this.requestVersion = 0;
this.element = document.createElement('article');
this.element.dataset.testid = 'prompt-response-block';
this.element.dataset.blockId = id;
this.element.innerHTML = '<textarea data-testid="prompt-input" placeholder="Prompt"></textarea><button data-testid="send-button">Send</button><output data-testid="response">not sent</output>';
this.input = this.element.querySelector('[data-testid="prompt-input"]');
this.response = this.element.querySelector('[data-testid="response"]');
this.element.querySelector('[data-testid="send-button"]').addEventListener('click', () => this.send());
this.input.addEventListener('input', () => { blockManager.updateBlockState(this, { prompt: this.input.value }); blockManager.renderPlaceholder(); });
}
async send() {
const prompt = this.input.value.trim();
if (!prompt) return;
const version = this.requestVersion + 1;
blockManager.updateBlockState(this, { prompt, requestVersion: version });
blockManager.transitionBlockState(this, 'loading');
this.response.textContent = 'Loading...';
try {
const result = await blockManager.generate(prompt, this.id);
if (version !== this.requestVersion) return;
this.response.textContent = String(result);
blockManager.transitionBlockState(this, 'sent');
} catch (error) {
if (version !== this.requestVersion) return;
this.response.textContent = `Failed: ${error.message || error}`;
blockManager.transitionBlockState(this, 'error');
}
}
}
class BlockManager {
constructor() {
this.blocks = [];
this.listeners = new Set();
this.container = document.querySelector('[data-testid="blocks"]');
this.sidebar = document.querySelector('[data-testid="sidebar-list"]');
this.placeholder = document.querySelector('[data-testid="new-block-placeholder"]');
this.placeholder.addEventListener('click', () => this.createNewBlock(true));
this.generate = (prompt) => new Promise(resolve =>
setTimeout(() => resolve(`response for ${prompt}`), 40));
this.subscribe(() => { this.renderSidebar(); this.renderPlaceholder(); });
}
subscribe(listener) {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
notify() {
for (const listener of this.listeners) listener(this.blocks);
}
updateBlockState(block, patch) {
Object.assign(block, patch);
this.notify();
}
transitionBlockState(block, status) {
this.updateBlockState(block, { status });
}
createNewBlock(autoFocus = false) {
return this.createBlock(autoFocus);
}
createBlock(autofocus = false) {
const block = new PromptBlock(`block-${this.blocks.length + 1}`);
this.blocks.push(block);
this.container.appendChild(block.element);
this.notify();
if (autofocus) block.input.focus();
return block;
}
renderSidebar() {
this.sidebar.innerHTML = this.blocks.map(block => `<li>${block.id} ${block.status}</li>`).join('');
}
renderPlaceholder() {
const ready = this.blocks.length > 0 && this.blocks.every(block => ['loading', 'sent', 'error'].includes(block.status));
this.placeholder.classList.toggle('visible', ready);
}
}
const blockManager = new BlockManager();
window.BlockManager = BlockManager;
window.blockManager = blockManager;
window.__setMockGenerate = (fn) => { blockManager.generate = fn; };
blockManager.createBlock(false);
</script>
</body>
</html>
|