-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaboration-example.html
More file actions
371 lines (327 loc) · 13.3 KB
/
collaboration-example.html
File metadata and controls
371 lines (327 loc) · 13.3 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hive Collaboration Example</title>
<script src="https://unpkg.com/[email protected]/dist/yjs.js"></script>
<script src="https://unpkg.com/@hocuspocus/[email protected]/dist/hocuspocus-provider.umd.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.auth-form {
margin-bottom: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 5px;
border: 1px solid #dee2e6;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 600;
}
input, textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
button:hover {
background: #0056b3;
}
button:disabled {
background: #6c757d;
cursor: not-allowed;
}
.editor {
min-height: 300px;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
background: white;
font-family: serif;
line-height: 1.6;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
font-size: 14px;
}
.status.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.users {
margin-top: 10px;
font-size: 14px;
}
.user-indicator {
display: inline-block;
padding: 2px 8px;
border-radius: 12px;
color: white;
font-size: 12px;
margin-right: 5px;
}
.examples {
margin-top: 20px;
padding: 15px;
background: #e9ecef;
border-radius: 5px;
}
.examples h3 {
margin-top: 0;
}
.examples pre {
background: white;
padding: 10px;
border-radius: 3px;
overflow-x: auto;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 Hive Collaboration Server Example</h1>
<p>This demonstrates real-time collaborative editing using Hocuspocus with Hive blockchain authentication.</p>
<div class="auth-form">
<h3>Authentication & Document</h3>
<div class="form-group">
<label for="account">Hive Account:</label>
<input type="text" id="account" placeholder="your-hive-username" required>
</div>
<div class="form-group">
<label for="privateKey">Private Key (Posting Key):</label>
<input type="password" id="privateKey" placeholder="5JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxKJ" required>
</div>
<div class="form-group">
<label for="documentName">Document (owner/permlink):</label>
<input type="text" id="documentName" placeholder="alice/my-document-2024" required>
</div>
<button onclick="connect()" id="connectBtn">Connect to Document</button>
<button onclick="disconnect()" id="disconnectBtn" disabled>Disconnect</button>
</div>
<div class="editor" id="editor" contenteditable="false" style="opacity: 0.5;">
Connect to start editing...
</div>
<div class="users" id="users">
<strong>Connected Users:</strong> <span id="userList">None</span>
</div>
<div id="status"></div>
<div class="examples">
<h3>How it works:</h3>
<p>1. Enter your Hive account credentials and document path</p>
<p>2. The system generates authentication headers with a signed challenge</p>
<p>3. Connects to the Hocuspocus server via WebSocket</p>
<p>4. Real-time collaborative editing with Y.js CRDT</p>
<h4>Authentication Headers Example:</h4>
<pre id="authExample">Will show after connecting...</pre>
<h4>WebSocket URL:</h4>
<pre id="wsUrl">ws://localhost:1234/{owner}/{permlink}</pre>
</div>
</div>
<script>
let provider = null;
let yDoc = null;
let yText = null;
let connected = false;
// Mock Hive signature function (in real app, use hive-tx library)
async function signChallenge(challenge, privateKey) {
// This is a simplified mock - in production use proper hive-tx signing
// For demo purposes, we'll generate a fake signature
return `mock_signature_${challenge}_${privateKey.slice(-8)}`;
}
// Mock public key derivation (in real app, derive from private key)
function getPublicKey(privateKey) {
// This is a mock - in production derive actual public key from private key
return `STM7MockPublicKey${privateKey.slice(-8)}`;
}
async function generateAuthToken(account, privateKey) {
const challenge = Math.floor(Date.now() / 1000);
const pubkey = getPublicKey(privateKey);
const signature = await signChallenge(challenge.toString(), privateKey);
const authData = {
account,
challenge: challenge.toString(),
pubkey,
signature
};
// Show auth example
document.getElementById('authExample').textContent = JSON.stringify(authData, null, 2);
return JSON.stringify(authData);
}
async function connect() {
const account = document.getElementById('account').value.trim();
const privateKey = document.getElementById('privateKey').value.trim();
const documentName = document.getElementById('documentName').value.trim();
if (!account || !privateKey || !documentName) {
showStatus('Please fill in all fields', 'error');
return;
}
if (!documentName.includes('/')) {
showStatus('Document name must be in format: owner/permlink', 'error');
return;
}
try {
document.getElementById('connectBtn').disabled = true;
showStatus('Generating authentication...', 'success');
// Generate auth token
const token = await generateAuthToken(account, privateKey);
// Update WebSocket URL display
document.getElementById('wsUrl').textContent = `ws://localhost:1234/${documentName}`;
// Create Y.js document
yDoc = new Y.Doc();
yText = yDoc.getText('content');
showStatus('Connecting to collaboration server...', 'success');
// Create Hocuspocus provider
provider = new HocuspocusProvider({
url: 'ws://localhost:1234',
name: documentName,
token: token,
document: yDoc,
});
// Handle connection events
provider.on('connect', () => {
connected = true;
showStatus('✅ Connected! You can now edit collaboratively.', 'success');
document.getElementById('editor').contentEditable = true;
document.getElementById('editor').style.opacity = 1;
document.getElementById('editor').innerHTML = yText.toString() || 'Start typing...';
document.getElementById('disconnectBtn').disabled = false;
// Bind Y.js text to contenteditable
bindTextEditor();
});
provider.on('disconnect', () => {
connected = false;
showStatus('❌ Disconnected from server', 'error');
document.getElementById('editor').contentEditable = false;
document.getElementById('editor').style.opacity = 0.5;
document.getElementById('connectBtn').disabled = false;
document.getElementById('disconnectBtn').disabled = true;
});
provider.on('authenticationFailed', (error) => {
showStatus(`❌ Authentication failed: ${error.reason}`, 'error');
document.getElementById('connectBtn').disabled = false;
});
// Handle awareness (user presence)
provider.awareness.on('change', () => {
updateUserList();
});
} catch (error) {
showStatus(`❌ Connection failed: ${error.message}`, 'error');
document.getElementById('connectBtn').disabled = false;
}
}
function bindTextEditor() {
const editor = document.getElementById('editor');
// Sync Y.js text changes to editor
yText.observe((event) => {
if (!event.target.equals(yText)) return;
// Update editor content without losing cursor position
const content = yText.toString();
if (editor.innerHTML !== content) {
const selection = window.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
editor.innerHTML = content;
// Restore cursor position
if (range) {
try {
selection.removeAllRanges();
selection.addRange(range);
} catch (e) {
// Cursor position might be invalid
}
}
}
});
// Sync editor changes to Y.js
editor.addEventListener('input', () => {
const content = editor.innerText;
if (content !== yText.toString()) {
yText.delete(0, yText.length);
yText.insert(0, content);
}
});
}
function updateUserList() {
if (!provider) return;
const users = Array.from(provider.awareness.getStates().values())
.filter(state => state.user)
.map(state => state.user);
const userList = document.getElementById('userList');
if (users.length === 0) {
userList.innerHTML = 'None';
} else {
userList.innerHTML = users.map(user =>
`<span class="user-indicator" style="background-color: ${user.color}">
${user.name}
</span>`
).join('');
}
}
function disconnect() {
if (provider) {
provider.destroy();
provider = null;
}
if (yDoc) {
yDoc.destroy();
yDoc = null;
}
yText = null;
connected = false;
document.getElementById('editor').contentEditable = false;
document.getElementById('editor').style.opacity = 0.5;
document.getElementById('editor').innerHTML = 'Connect to start editing...';
document.getElementById('connectBtn').disabled = false;
document.getElementById('disconnectBtn').disabled = true;
document.getElementById('userList').innerHTML = 'None';
showStatus('Disconnected', 'error');
}
function showStatus(message, type) {
const status = document.getElementById('status');
status.textContent = message;
status.className = `status ${type}`;
}
// Demo helper: populate with example data
window.addEventListener('load', () => {
document.getElementById('account').value = 'alice';
document.getElementById('documentName').value = 'alice/my-document-2024';
});
</script>
</body>
</html>