-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_view_live.html
More file actions
389 lines (326 loc) · 11.5 KB
/
diff_view_live.html
File metadata and controls
389 lines (326 loc) · 11.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>SquibView - Live Diff Demo</title>
<link rel="icon"
href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><circle cx='16' cy='16' r='14' fill='%23f57c00' stroke='%23000' stroke-width='1'/><path d='M2 16h28M16 2a14 14 0 0114 14 14 14 0 01-14 14a14 14 0 01-14-14A14 14 0 0116 2zm0 0v28M9 16a7 7 0 0014 0a7 7 0 00-14 0z' fill='none' stroke='%23000' stroke-width='1'/></svg>">
<!-- External dependencies (markdown-it, diff-match-patch, tiny-emitter now bundled) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.4.1/papaparse.min.js"></script>
<script src="https://unpkg.com/turndown@7.1.2/dist/turndown.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/default.min.css">
<link rel="stylesheet" href="../dist/squibview.css">
<!-- Shared example styles -->
<link rel="stylesheet" href="examples.css">
<script type="importmap">
{
"imports": {
"markdown-it": "https://esm.sh/markdown-it@12.3.2",
"three": "https://esm.sh/three@0.164.1",
"three/addons/": "https://esm.sh/three@0.164.1/examples/jsm/"
}
}
</script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
margin: 0;
padding: 20px;
background-color: #f8f9fa;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 8px;
padding: 30px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 10px;
}
h2 {
color: #495057;
padding-bottom: 8px;
margin-top: 40px;
}
.intro {
background: #f8f9fa;
padding: 20px;
border-radius: 6px;
margin: 20px 0;
border: 1px solid #e9ecef;
}
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
margin: 30px 0;
}
@media (max-width: 768px) {
.comparison-grid {
grid-template-columns: 1fr;
gap: 20px;
}
}
.diff-section {
border: 1px solid #e9ecef;
border-radius: 6px;
padding: 20px;
background: #fafafa;
}
.diff-section h3 {
margin-top: 0;
color: #495057;
display: flex;
align-items: center;
gap: 10px;
}
.badge {
background: #2a57d3;
color: white;
padding: 4px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.editor-section {
margin: 30px 0;
}
.editor-container {
height: 300px;
margin: 20px 0;
border-radius: 6px;
overflow: hidden;
}
.controls {
margin: 20px 0;
display: flex;
gap: 15px;
flex-wrap: wrap;
align-items: center;
}
.controls button {
background: #2a57d3;
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: background-color 0.2s;
}
.controls button:hover:not(:disabled) {
background: #1e3f8f;
}
.controls button:disabled {
background: #ccc;
cursor: not-allowed;
}
.controls .secondary {
background: #6c757d;
}
.controls .secondary:hover:not(:disabled) {
background: #545b62;
}
.controls .success {
background: #28a745;
}
.controls .success:hover:not(:disabled) {
background: #1e7e34;
}
.status {
padding: 10px 15px;
background: #d1ffda;
border: 1px solid #28a745;
border-radius: 4px;
font-size: 14px;
color: #155724;
margin: 15px 0;
}
.diff-container {
margin: 15px 0;
border: 1px solid #ddd;
border-radius: 6px;
background: white;
max-height: 400px;
overflow: auto;
min-height: 80px;
}
/* Override the internal diff container max-height to fit parent */
.diff-container .squibview-diff,
.diff-container .squibview-diff-inline {
max-height: 100%;
margin: 0;
border: none;
border-radius: 0;
}
.diff-container:empty::before {
content: "Start editing to see live diffs";
display: block;
padding: 30px 20px;
text-align: center;
color: #6c757d;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h1>SquibView Live Diff Demo</h1>
<p><strong>Live Diff Mode:</strong> This demo shows cumulative changes as you edit. All changes are compared against your selected baseline revision.</p>
<div class="editor-section">
<div class="editor-container">
<div id="editor"></div>
</div>
<div class="controls">
<span style="margin-right: 10px;">Baseline:</span>
<select id="baselineRevision" style="margin-right: 15px; padding: 8px 12px; border: 1px solid #ccc; border-radius: 6px;">
</select>
</div>
<div id="status" class="status">
Initializing...
</div>
</div>
<div class="comparison-grid">
<div class="diff-section">
<h3>Inline Diff View</h3>
<p>Blue additions, red deletions with strikethrough inline.</p>
<div id="inlineDiffContainer" class="diff-container"></div>
</div>
<div class="diff-section">
<h3>Side-by-Side Diff View</h3>
<p>Traditional line-by-line comparison with color-coded backgrounds.</p>
<div id="sideBySideDiffContainer" class="diff-container"></div>
</div>
</div>
</div>
<script type="module">
import SquibView from '../dist/squibview.esm.js';
// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
// Create the editor
const editor = new SquibView('#editor', {
titleShow: true,
titleContent: "SquibView Live Diff Demo " + SquibView.version.version,
initialContent: `# Live Diff Demo
Start editing this content to see live diffs appear automatically below.
## How it works
As you type, the diffs update in real-time, showing all changes since your selected baseline revision.
### Try these actions:
- Edit this text
- Add new sections
- Delete content
- Change formatting
The diff views below will update automatically to show your cumulative changes!`,
initialView: 'split'
});
// State
let baselineRevision = -1;
// Update status
function updateStatus() {
const currentIndex = editor.revisionManager.getCurrentIndex();
if (baselineRevision === currentIndex) {
document.getElementById('status').innerHTML =
'⚪ <strong>No changes yet</strong> - Start editing to see live diffs';
} else {
const baselineLabel = baselineRevision === -1 ? 'Initial' : `Revision ${baselineRevision + 1}`;
const currentLabel = currentIndex === -1 ? 'Initial' : `Revision ${currentIndex + 1}`;
document.getElementById('status').innerHTML =
`🟢 <strong>Live diff active</strong> - Comparing ${baselineLabel} → ${currentLabel}`;
}
}
// Populate baseline selector
function populateBaselineSelect() {
const revCount = editor.revisionManager.getRevisionCount();
const currentIndex = editor.revisionManager.getCurrentIndex();
const select = document.getElementById('baselineRevision');
console.log('Populating baseline - revCount:', revCount, 'currentIndex:', currentIndex, 'current baseline:', baselineRevision);
// Save current baseline value
const savedBaseline = baselineRevision;
// Clear options
select.innerHTML = '';
// Always include initial state
const initialOption = new Option('Initial', '-1');
select.add(initialOption);
// Add all revisions up to current
for (let i = 0; i <= currentIndex; i++) {
const label = `Revision ${i + 1}`;
const option = new Option(label, i.toString());
select.add(option);
}
// Keep the baseline fixed - don't change it when updating options
select.value = savedBaseline.toString();
}
// Show live diffs
function showLiveDiffs() {
try {
const currentIndex = editor.revisionManager.getCurrentIndex();
console.log('showLiveDiffs - currentIndex:', currentIndex, 'baselineRevision:', baselineRevision);
// Check if comparing same revision
if (baselineRevision === currentIndex) {
const msg = '<div style="padding: 20px; text-align: center; color: #666;">No changes from baseline (same revision selected)</div>';
document.getElementById('inlineDiffContainer').innerHTML = msg;
document.getElementById('sideBySideDiffContainer').innerHTML = msg;
return;
}
// Show diffs
console.log('Showing diff from', baselineRevision, 'to', currentIndex);
const inlineDiffHTML = editor.getSourceDiffInline({ fromIndex: baselineRevision, toIndex: currentIndex });
document.getElementById('inlineDiffContainer').innerHTML = inlineDiffHTML;
const sideBySideDiffHTML = editor.getSourceDiffHTML({ fromIndex: baselineRevision, toIndex: currentIndex });
document.getElementById('sideBySideDiffContainer').innerHTML = sideBySideDiffHTML;
} catch (e) {
console.error('Diff error:', e);
const errorMsg = `<div style="padding: 20px; color: #dc3545;">Error: ${e.message}</div>`;
document.getElementById('inlineDiffContainer').innerHTML = errorMsg;
document.getElementById('sideBySideDiffContainer').innerHTML = errorMsg;
}
}
// Handle content changes - just re-render diffs
function handleContentChange() {
populateBaselineSelect();
updateStatus();
showLiveDiffs();
}
// Handle baseline selection change
function handleBaselineChange() {
baselineRevision = parseInt(document.getElementById('baselineRevision').value);
updateStatus();
showLiveDiffs();
}
// Initialize
console.log('Initializing live diff demo');
populateBaselineSelect();
updateStatus();
showLiveDiffs();
// Event listeners
editor.events.on('contentChanged', () => {
console.log('Content changed event');
handleContentChange();
});
document.getElementById('baselineRevision').addEventListener('change', () => {
console.log('Baseline changed');
handleBaselineChange();
});
// Fallback timer to ensure updates
setInterval(() => {
const currentIndex = editor.revisionManager.getCurrentIndex();
const select = document.getElementById('baselineRevision');
// Check if we need to update
if (select.options.length - 1 !== currentIndex + 1) {
console.log('Timer update: populating selects');
populateBaselineSelect();
updateStatus();
showLiveDiffs();
}
}, 500);
});
</script>
</body>
</html>