-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_view_inline.html
More file actions
390 lines (328 loc) · 12.8 KB
/
diff_view_inline.html
File metadata and controls
390 lines (328 loc) · 12.8 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
390
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>SquibView - Inline 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">
<!-- No import map needed - markdown-it is bundled in squibview.esm.min.js -->
<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;
}
.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 .secondary {
background: #6c757d;
}
.controls .secondary:hover:not(:disabled) {
background: #545b62;
}
.status {
padding: 10px 15px;
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 4px;
font-size: 14px;
color: #495057;
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: "Diff will appear here";
display: block;
padding: 30px 20px;
text-align: center;
color: #6c757d;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h1>SquibView Inline Diff Demo</h1>
<p><strong>Compare Two Diff Styles:</strong> Select revisions from the dropdowns to see both diff views update automatically.</p>
<div class="editor-section">
<div class="editor-container">
<div id="editor"></div>
</div>
<div class="controls">
<span style="margin-right: 10px;">From:</span>
<select id="fromRevision" style="margin-right: 15px; padding: 8px 12px; border: 1px solid #ccc; border-radius: 6px;">
</select>
<span style="margin-right: 10px;">To:</span>
<select id="toRevision" style="margin-right: 15px; padding: 8px 12px; border: 1px solid #ccc; border-radius: 6px;">
</select>
</div>
<div class="controls">
<button id="makeTextEdit" class="secondary">Edit Some Text</button>
<button id="addParagraph" class="secondary">Add New Paragraph</button>
<button id="deleteSection" class="secondary">Delete a Section</button>
</div>
<div id="status" class="status">
Ready - Make some changes to see both diff styles in action
</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 Inline Diff Demo " + SquibView.version.version,
initialContent: `# SquibView Diff Comparison Demo
Welcome to the **inline diff demonstration**! This text will be used to show different types of changes.
## Original Content
This paragraph contains some original text that we'll modify to demonstrate the diff functionality. You can see how different types of changes appear in each view.
### Features to Test
- Text modifications
- New additions
- Content deletions
- Line changes
## Conclusion
Try the buttons below to make different types of changes and see how they appear in both diff views!`,
initialView: 'split'
});
// Update dropdowns based on current revision state
function updateRevisionSelects() {
const revCount = editor.revisionManager.getRevisionCount();
const currentIndex = editor.revisionManager.getCurrentIndex();
const fromSelect = document.getElementById('fromRevision');
const toSelect = document.getElementById('toRevision');
console.log('updateRevisionSelects - revCount:', revCount, 'currentIndex:', currentIndex);
// Save current selections
const currentFromValue = fromSelect.value;
const currentToValue = toSelect.value;
// Clear options
fromSelect.innerHTML = '';
toSelect.innerHTML = '';
// Build revision list
// Always include initial state
const initialOption1 = new Option('Initial', '-1');
const initialOption2 = new Option('Initial', '-1');
fromSelect.add(initialOption1);
toSelect.add(initialOption2);
// Add all revisions up to current
for (let i = 0; i <= currentIndex; i++) {
const label = `Revision ${i + 1}`;
const option1 = new Option(label, i.toString());
const option2 = new Option(label, i.toString());
fromSelect.add(option1);
toSelect.add(option2);
}
// Restore or set default selections
if (currentFromValue && fromSelect.querySelector(`option[value="${currentFromValue}"]`)) {
fromSelect.value = currentFromValue;
} else {
// Default: from = one before current
fromSelect.value = Math.max(-1, currentIndex - 1);
}
if (currentToValue && toSelect.querySelector(`option[value="${currentToValue}"]`)) {
toSelect.value = currentToValue;
} else {
// Default: to = current
toSelect.value = currentIndex;
}
// Update status
document.getElementById('status').innerHTML =
`Current revision: ${currentIndex + 1} | Total changes: ${revCount}`;
}
// Show diffs based on current selections
function showDiffs() {
try {
const fromIndex = parseInt(document.getElementById('fromRevision').value);
const toIndex = parseInt(document.getElementById('toRevision').value);
if (isNaN(fromIndex) || isNaN(toIndex)) {
return;
}
// Check if same revision
if (fromIndex === toIndex) {
const msg = '<div style="padding: 20px; text-align: center; color: #666;">Same revision selected</div>';
document.getElementById('inlineDiffContainer').innerHTML = msg;
document.getElementById('sideBySideDiffContainer').innerHTML = msg;
return;
}
// Show diffs
const inlineDiffHTML = editor.getSourceDiffInline({ fromIndex, toIndex });
document.getElementById('inlineDiffContainer').innerHTML = inlineDiffHTML;
const sideBySideDiffHTML = editor.getSourceDiffHTML({ fromIndex, toIndex });
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;
}
}
// Initialize
updateRevisionSelects();
showDiffs();
// Update on content changes
editor.events.on('contentChanged', () => {
console.log('Content changed event fired');
updateRevisionSelects();
showDiffs();
});
// Fallback: Also update periodically in case events don't fire
setInterval(() => {
const currentRevCount = editor.revisionManager.getRevisionCount();
const currentIndex = editor.revisionManager.getCurrentIndex();
const fromSelect = document.getElementById('fromRevision');
// Only update if something changed
if (fromSelect.options.length - 1 !== currentIndex + 1) {
console.log('Timer update needed - options:', fromSelect.options.length, 'currentIndex:', currentIndex);
updateRevisionSelects();
showDiffs();
}
}, 500);
// Update on selection changes
document.getElementById('fromRevision').addEventListener('change', showDiffs);
document.getElementById('toRevision').addEventListener('change', showDiffs);
// Sample edit buttons
document.getElementById('makeTextEdit').addEventListener('click', () => {
let content = editor.getContent();
content = content.replace(
'This paragraph contains some original text',
'This paragraph contains some **modified and enhanced** text'
);
content = content.replace(
'You can see how different types of changes appear',
'You can clearly observe how various types of modifications appear'
);
editor.setContent(content);
});
document.getElementById('addParagraph').addEventListener('click', () => {
const timestamp = new Date().toLocaleTimeString();
const newParagraph = `\n\n## ✨ New Section Added at ${timestamp}\n\nThis is a completely new section that demonstrates how **additions** appear in both diff views. Notice how the inline view shows this in blue highlighting, while the side-by-side view shows it as green-highlighted lines.\n\n- New bullet point\n- Another fresh item\n- Third addition for variety`;
editor.setContent(editor.getContent() + newParagraph);
});
document.getElementById('deleteSection').addEventListener('click', () => {
let content = editor.getContent();
content = content.replace(/### Features to Test[\s\S]*?(?=\n##|$)/g, '');
editor.setContent(content);
});
});
</script>
</body>
</html>