-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_headless.html
More file actions
882 lines (768 loc) · 26.7 KB
/
example_headless.html
File metadata and controls
882 lines (768 loc) · 26.7 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SquibView Headless Mode - Complete API Example</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 14 14 14 0 01-14-14A14 14 0 0116 2zm0 0v28M9 16a7 7 0 0014 0 7 7 0 00-14 0z' fill='none' stroke='%23000' stroke-width='1'/></svg>">
<!-- SquibView CSS -->
<link rel="stylesheet" href="../dist/squibview.min.css">
<!-- Example styles -->
<link rel="stylesheet" href="examples.css">
<style>
.demo-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.custom-toolbar {
display: flex;
flex-wrap: wrap;
gap: 10px;
padding: 15px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px 8px 0 0;
margin-bottom: -1px;
}
.toolbar-group {
display: flex;
gap: 4px;
padding: 4px 8px;
background: white;
border-radius: 4px;
align-items: center;
}
.toolbar-label {
font-size: 11px;
color: #6c757d;
font-weight: 600;
margin-right: 4px;
text-transform: uppercase;
}
.btn {
padding: 6px 12px;
background: white;
border: 1px solid #dee2e6;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
transition: all 0.15s;
}
.btn:hover {
background: #e9ecef;
transform: translateY(-1px);
}
.btn:active {
transform: translateY(0);
}
.btn.active {
background: #007bff;
color: white;
border-color: #007bff;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-primary {
background: #007bff;
color: white;
border-color: #007bff;
}
.btn-primary:hover {
background: #0056b3;
}
.btn-success {
background: #28a745;
color: white;
border-color: #28a745;
}
.btn-danger {
background: #dc3545;
color: white;
border-color: #dc3545;
}
#editor {
border: 1px solid #dee2e6;
border-radius: 0 0 8px 8px;
overflow: hidden;
height: 500px;
}
/* Hide SquibView's default controls */
#editor .squibview-controls {
display: none !important;
}
.status-bar {
display: flex;
justify-content: space-between;
padding: 10px 15px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-top: none;
border-radius: 0 0 8px 8px;
font-size: 12px;
color: #6c757d;
margin-top: -1px;
}
.status-item {
display: flex;
gap: 5px;
}
.status-label {
font-weight: 600;
}
.output-box {
margin-top: 20px;
padding: 15px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
font-family: monospace;
font-size: 12px;
max-height: 200px;
overflow-y: auto;
display: none;
}
.output-box.show {
display: block;
}
.output-line {
margin: 2px 0;
padding: 2px 0;
}
.output-line.success {
color: #28a745;
}
.output-line.info {
color: #007bff;
}
.output-line.error {
color: #dc3545;
}
.checkbox-label {
display: inline-flex;
align-items: center;
gap: 5px;
font-size: 13px;
}
</style>
</head>
<body>
<div class="demo-container">
<header>
<h1>SquibView Headless Mode - Complete API Example</h1>
<p>This example shows how to use SquibView without default UI controls, implementing all features with custom buttons.</p>
</header>
<!-- Custom Toolbar with ALL API Features -->
<div class="custom-toolbar">
<!-- Content Management -->
<div class="toolbar-group">
<span class="toolbar-label">Content:</span>
<button class="btn" onclick="demo.setContent()">Set</button>
<button class="btn" onclick="demo.getContent()">Get</button>
<button class="btn" onclick="demo.getHTML()">Get HTML</button>
<button class="btn" onclick="demo.clearContent()">Clear</button>
</div>
<!-- View Controls -->
<div class="toolbar-group">
<span class="toolbar-label">View:</span>
<button class="btn" onclick="demo.setView('src')" data-view="src">Source</button>
<button class="btn" onclick="demo.setView('html')" data-view="html">Preview</button>
<button class="btn active" onclick="demo.setView('split')" data-view="split">Split</button>
<button class="btn" onclick="demo.toggleView()">Toggle</button>
</div>
<!-- History -->
<div class="toolbar-group">
<span class="toolbar-label">History:</span>
<button class="btn" onclick="demo.undo()" id="undo-btn">↶ Undo</button>
<button class="btn" onclick="demo.redo()" id="redo-btn">↷ Redo</button>
<button class="btn" onclick="demo.showRevisions()">Revisions</button>
</div>
<!-- Copy Functions -->
<div class="toolbar-group">
<span class="toolbar-label">Copy as:</span>
<button class="btn btn-primary" onclick="demo.copy('formatted', event)">Rich</button>
<button class="btn" onclick="demo.copy('markdown', event)">Markdown</button>
<button class="btn" onclick="demo.copy('html', event)">HTML</button>
<button class="btn" onclick="demo.copy('plain', event)">Plain</button>
</div>
<!-- Selection -->
<div class="toolbar-group">
<span class="toolbar-label">Selection:</span>
<button class="btn" onclick="demo.getSelection()">Get</button>
<button class="btn" onclick="demo.replaceSelection()">Replace</button>
</div>
<!-- Content Type -->
<div class="toolbar-group">
<span class="toolbar-label">Type:</span>
<select onchange="demo.setType(this.value)" style="padding: 6px; border: 1px solid #dee2e6; border-radius: 4px;">
<option value="md">Markdown</option>
<option value="html">HTML</option>
<option value="csv">CSV</option>
</select>
</div>
<!-- Options -->
<div class="toolbar-group">
<span class="toolbar-label">Options:</span>
<label class="checkbox-label">
<input type="checkbox" onchange="demo.toggleControls(this.checked)"> Controls
</label>
<label class="checkbox-label">
<input type="checkbox" onchange="demo.toggleTitle(this.checked)"> Title
</label>
<label class="checkbox-label">
<input type="checkbox" onchange="demo.toggleLineNumbers(this.checked)"> Lines
</label>
</div>
<!-- Info -->
<div class="toolbar-group">
<span class="toolbar-label">Info:</span>
<button class="btn" onclick="demo.showInfo()">Version</button>
<button class="btn btn-success" onclick="demo.toggleOutput()">Console</button>
</div>
</div>
<!-- Headless Editor (no built-in controls) -->
<div id="editor"></div>
<!-- Status Bar -->
<div class="status-bar">
<div style="display: flex; gap: 20px;">
<div class="status-item">
<span class="status-label">View:</span>
<span id="status-view">split</span>
</div>
<div class="status-item">
<span class="status-label">Type:</span>
<span id="status-type">md</span>
</div>
<div class="status-item">
<span class="status-label">Words:</span>
<span id="status-words">0</span>
</div>
<div class="status-item">
<span class="status-label">Characters:</span>
<span id="status-chars">0</span>
</div>
</div>
<div style="display: flex; gap: 20px;">
<div class="status-item">
<span class="status-label">Revisions:</span>
<span id="status-revisions">0</span>
</div>
<div class="status-item">
<span class="status-label">Can Undo:</span>
<span id="status-undo">No</span>
</div>
<div class="status-item">
<span class="status-label">Can Redo:</span>
<span id="status-redo">No</span>
</div>
</div>
</div>
<!-- Output Console -->
<div class="output-box" id="output">
<div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
<strong>API Console Output</strong>
<button onclick="document.getElementById('output-content').innerHTML = ''" style="font-size: 11px;">Clear</button>
</div>
<div id="output-content"></div>
</div>
</div>
<script type="module">
import SquibView from '../dist/squibview.esm.js';
// Initialize once DOM is ready
async function init() {
console.log('Initializing SquibView headless example...');
console.log('SquibView class:', SquibView);
/**
* @typedef {Object} EditorConfig
* @property {boolean} showControls - Whether to show built-in controls
* @property {boolean} titleShow - Whether to show title bar
* @property {boolean} showLineNumbers - Whether to show line numbers
* @property {string} initialView - Initial view mode ('src', 'html', 'split')
* @property {string} inputContentType - Content type ('md', 'html', 'csv')
* @property {string} initialContent - Initial markdown content
* @property {Object} autoload_deps - Dependency loading configuration
*/
/**
* Main editor configuration object
* @type {EditorConfig}
*/
let editorConfig = {
// HEADLESS MODE - No built-in controls
showControls: false,
titleShow: false,
showLineNumbers: false,
// Initial configuration
initialView: 'split',
inputContentType: 'md',
// Sample content
initialContent: `# Headless Mode Demo
This editor has **no built-in controls**. All functionality is provided by the custom toolbar above.
## Features Available
All SquibView features work in headless mode:
- ✅ Content management (set, get, clear)
- ✅ View modes (source, preview, split)
- ✅ Undo/Redo with full history
- ✅ Multiple copy formats
- ✅ Text selection and replacement
- ✅ Content type switching
- ✅ All rendering features
## Try It Out
1. Click buttons in the toolbar to test features
2. Select text to test selection APIs
3. Make edits to test undo/redo
4. Switch views to see different modes
## Code Example
\`\`\`javascript
// Creating a headless editor
const editor = new SquibView('#container', {
showControls: false, // Hide default controls
titleShow: false // Hide title bar
});
// Use full API
editor.setContent('# Hello');
editor.copyToClipboard('formatted');
\`\`\`
**All power, your UI!**`,
// Enable all features
autoload_deps: { all: true }
};
/**
* SquibView editor instance - recreated when certain options change
* @type {SquibView}
*/
let editor = new SquibView('#editor', editorConfig);
// Verify editor was created properly
if (!editor) {
console.error('Failed to create SquibView instance');
alert('Failed to create SquibView instance. Check console for errors.');
return;
}
console.log('Editor created:', editor);
/**
* Logs a message to the console output panel
* @param {string} message - Message to log
* @param {string} [type='info'] - Message type ('info', 'success', 'error', 'warning')
*/
function log(message, type = 'info') {
const output = document.getElementById('output-content');
const line = document.createElement('div');
line.className = `output-line ${type}`;
line.textContent = `> ${message}`;
output.appendChild(line);
output.scrollTop = output.scrollHeight;
}
/**
* Updates the status bar with current editor state
* Updates word count, character count, view mode, content type, revision info
* Also updates undo/redo button states
*/
function updateStatus() {
const content = editor.getContent();
const words = content.trim().split(/\s+/).filter(w => w).length;
document.getElementById('status-view').textContent = editor.currentView;
document.getElementById('status-type').textContent = editor.inputContentType;
document.getElementById('status-words').textContent = words;
document.getElementById('status-chars').textContent = content.length;
document.getElementById('status-revisions').textContent = editor.revisionNumRevsions ? editor.revisionNumRevsions() : 0;
const currentIndex = editor.revisionGetCurrentIndex ? editor.revisionGetCurrentIndex() : 0;
const totalRevisions = editor.revisionNumRevsions ? editor.revisionNumRevsions() : 0;
document.getElementById('status-undo').textContent = currentIndex > 0 ? 'Yes' : 'No';
document.getElementById('status-redo').textContent = (totalRevisions > 0 && currentIndex < totalRevisions - 1) ? 'Yes' : 'No';
// Update button states
const canUndo = currentIndex > 0;
const canRedo = totalRevisions > 0 && currentIndex < totalRevisions - 1;
document.getElementById('undo-btn').disabled = !canUndo;
document.getElementById('redo-btn').disabled = !canRedo;
}
/**
* Updates the visual state of view mode buttons
* Adds 'active' class to button matching current view
*/
function updateViewButtons() {
document.querySelectorAll('[data-view]').forEach(btn => {
btn.classList.remove('active');
if (btn.dataset.view === editor.currentView) {
btn.classList.add('active');
}
});
}
/**
* API object exposed globally for button onclick handlers
* Contains all methods for interacting with the headless editor
* @global
* @namespace demo
*/
window.demo = {
/**
* Sets editor content via user prompt
* @memberof demo
*/
setContent() {
const content = prompt('Enter content:', editor.getContent());
if (content !== null) {
editor.setContent(content, 'md');
log(`setContent() called - ${content.length} characters`, 'success');
updateStatus();
}
},
/**
* Gets current markdown content and displays in alert
* @memberof demo
*/
getContent() {
const content = editor.getContent();
log(`getContent() returned ${content.length} characters`, 'info');
console.log('Content:', content);
alert(`Content has ${content.length} characters\n\nFirst 100 chars:\n${content.substring(0, 100)}...`);
},
/**
* Gets rendered HTML output and displays preview
* @memberof demo
*/
getHTML() {
const html = editor.getHTMLSource();
log(`getHTMLSource() returned ${html.length} characters`, 'info');
console.log('HTML:', html);
alert(`Rendered HTML has ${html.length} characters\n\nFirst 200 chars:\n${html.substring(0, 200)}...`);
},
/**
* Clears all editor content after confirmation
* @memberof demo
*/
clearContent() {
if (confirm('Clear all content?')) {
editor.setContent('');
log('Content cleared', 'success');
updateStatus();
}
},
/**
* Sets the editor view mode
* @memberof demo
* @param {'src'|'html'|'split'} view - View mode to set
*/
setView(view) {
editor.setView(view);
log(`View changed to: ${view}`, 'success');
updateViewButtons();
updateStatus();
},
/**
* Cycles through view modes: src -> html -> split -> src
* @memberof demo
*/
toggleView() {
editor.toggleView();
log(`View toggled to: ${editor.currentView}`, 'success');
updateViewButtons();
updateStatus();
},
/**
* Undoes the last edit if possible
* @memberof demo
* @returns {boolean} Whether undo was successful
*/
undo() {
const result = editor.revisionUndo();
if (result) {
log('Undo successful', 'success');
} else {
log('Nothing to undo', 'error');
}
updateStatus();
},
/**
* Redoes the last undone edit if possible
* @memberof demo
* @returns {boolean} Whether redo was successful
*/
redo() {
const result = editor.revisionRedo();
if (result) {
log('Redo successful', 'success');
} else {
log('Nothing to redo', 'error');
}
updateStatus();
},
/**
* Displays revision history information
* Shows total revision count and undo/redo availability
* @memberof demo
*/
showRevisions() {
const count = editor.revisionNumRevsions ? editor.revisionNumRevsions() : 0;
const currentIndex = editor.revisionGetCurrentIndex ? editor.revisionGetCurrentIndex() : 0;
const canUndo = currentIndex > 0;
const canRedo = count > 0 && currentIndex < count - 1;
log(`Revisions: ${count}, Can Undo: ${canUndo}, Can Redo: ${canRedo}`, 'info');
alert(`Revision Information:\n\nTotal Revisions: ${count}\nCan Undo: ${canUndo}\nCan Redo: ${canRedo}`);
},
/**
* Copies editor content to clipboard in specified format
* @memberof demo
* @async
* @param {'formatted'|'markdown'|'html'|'plain'} format - Copy format
* @param {Event} [event] - Click event for button feedback
*/
async copy(format, event) {
try {
let success = false;
switch(format) {
case 'markdown':
case 'plain':
success = await editor.copySource();
break;
case 'html':
case 'formatted':
success = await editor.copyHTML();
break;
default:
throw new Error(`Unknown format: ${format}`);
}
if (success !== false) {
log(`Copied as ${format}`, 'success');
// Show a temporary success indicator if event is provided
if (event && event.target) {
const btn = event.target;
const originalText = btn.textContent;
btn.textContent = '✓ Copied!';
btn.style.background = '#27ae60';
setTimeout(() => {
btn.textContent = originalText;
btn.style.background = '';
}, 1500);
}
} else {
log(`Copy failed for format: ${format}`, 'error');
}
} catch (error) {
log(`Copy failed: ${error.message}`, 'error');
}
},
/**
* Gets currently selected text and displays info
* @memberof demo
*/
getSelection() {
// SquibView doesn't expose a public method to get selection
// We need to get it directly from the DOM
const selection = window.getSelection();
const text = selection ? selection.toString() : '';
if (text) {
log(`Selected text: "${text.substring(0, 50)}..."`, 'info');
alert(`Selected Text:\n"${text}"\n\nLength: ${text.length} characters`);
} else {
log('No text selected', 'info');
alert('No text is currently selected');
}
},
/**
* Replaces currently selected text with user input
* @memberof demo
*/
replaceSelection() {
// Get selected text from DOM
const selection = window.getSelection();
const selected = selection ? selection.toString() : '';
if (selected) {
const replacement = prompt('Replace selected text with:', selected);
if (replacement !== null) {
// Use replaceSelectedText if we have lastSelectionData
if (editor.lastSelectionData) {
editor.replaceSelectedText(replacement, editor.lastSelectionData);
log(`Replaced "${selected.substring(0, 20)}..." with "${replacement.substring(0, 20)}..."`, 'success');
updateStatus();
} else {
log('Unable to replace - selection data not available', 'error');
}
}
} else {
log('No text selected to replace', 'error');
alert('Please select some text first');
}
},
/**
* Sets the content type for parsing
* @memberof demo
* @param {'md'|'html'|'csv'} type - Content type
*/
setType(type) {
editor.setContentType(type);
log(`Content type changed to: ${type}`, 'success');
updateStatus();
},
/**
* Toggles built-in controls visibility
* Requires recreating the editor instance to apply changes
* @memberof demo
* @param {boolean} show - Whether to show controls
*/
toggleControls(show) {
const currentContent = editor.getContent();
const currentView = editor.currentView;
editorConfig.showControls = show;
// Destroy old editor and create new one
editor.destroy();
editor = new SquibView('#editor', { ...editorConfig, initialContent: currentContent, initialView: currentView });
// Update global reference
window.squibEditor = editor;
// Re-attach event listeners
attachEventListeners();
log(`Controls ${show ? 'shown' : 'hidden'}`, 'success');
updateStatus();
updateViewButtons();
},
/**
* Toggles title bar visibility
* Requires recreating the editor instance to apply changes
* @memberof demo
* @param {boolean} show - Whether to show title
*/
toggleTitle(show) {
const currentContent = editor.getContent();
const currentView = editor.currentView;
editorConfig.titleShow = show;
// Destroy old editor and create new one
editor.destroy();
editor = new SquibView('#editor', { ...editorConfig, initialContent: currentContent, initialView: currentView });
// Update global reference
window.squibEditor = editor;
// Re-attach event listeners
attachEventListeners();
log(`Title ${show ? 'shown' : 'hidden'}`, 'success');
updateStatus();
updateViewButtons();
},
/**
* Toggles line numbers visibility in the source editor
* Requires recreating the editor instance to apply changes
* @memberof demo
* @param {boolean} show - Whether to show line numbers
*/
toggleLineNumbers(show) {
const currentContent = editor.getContent();
const currentView = editor.currentView;
editorConfig.showLineNumbers = show;
// Destroy old editor and create new one
editor.destroy();
editor = new SquibView('#editor', { ...editorConfig, initialContent: currentContent, initialView: currentView });
// Update global reference
window.squibEditor = editor;
// Re-attach event listeners
attachEventListeners();
log(`Line numbers ${show ? 'shown' : 'hidden'}`, 'success');
updateStatus();
updateViewButtons();
},
/**
* Displays SquibView version and configuration info
* @memberof demo
*/
showInfo() {
const version = SquibView.version;
log(`SquibView version: ${version.version}`, 'info');
alert(`SquibView Information:\n\nVersion: ${version.version}\nRepository: ${version.url}\nCurrent View: ${editor.currentView}\nContent Type: ${editor.inputContentType}`);
},
/**
* Toggles the visibility of the console output panel
* @memberof demo
*/
toggleOutput() {
const output = document.getElementById('output');
output.classList.toggle('show');
if (output.classList.contains('show')) {
log('Console opened - API calls will be logged here', 'success');
}
}
};
/**
* Attaches all event listeners to the editor instance
* Called after editor creation and recreation
* Listens for: content-change, view-change, selection-change, revision-added
*/
function attachEventListeners() {
// Check if editor exists
if (!editor) {
console.error('Editor not properly initialized', editor);
return;
}
// Check if we have events object
if (!editor.events || typeof editor.events.on !== 'function') {
console.warn('Editor events not available, skipping event listeners');
return;
}
editor.events.on('content:change', () => {
updateStatus();
log('Event: content-change', 'info');
});
editor.events.on('view:change', (view) => {
updateViewButtons();
updateStatus();
log(`Event: view-change to ${view}`, 'info');
});
editor.events.on('text:selected', (data) => {
if (data && data.text) {
log(`Event: text-selected - "${data.text.substring(0, 30)}..."`, 'info');
}
});
editor.events.on('revision:undo', () => {
updateStatus();
log('Event: revision-undo', 'info');
});
editor.events.on('revision:redo', () => {
updateStatus();
log('Event: revision-redo', 'info');
});
}
/**
* Initialize event listeners and UI state
*/
attachEventListeners();
/**
* Set initial UI state
*/
updateStatus();
updateViewButtons();
/**
* Make editor instance available globally for debugging
* Access via browser console: window.squibEditor
* @global
* @type {SquibView}
*/
window.squibEditor = editor;
/**
* Helper to get current editor instance (changes on recreation)
* @returns {SquibView} Current editor instance
*/
window.getEditor = () => editor;
/**
* Update demo methods to use current editor reference
* This ensures they work after editor recreation
*/
Object.keys(window.demo).forEach(key => {
const originalMethod = window.demo[key];
if (key.startsWith('toggle')) {
// Keep toggle methods as-is, they handle editor recreation
return;
}
// Wrap other methods to ensure they use current editor
window.demo[key] = function(...args) {
// Ensure we're using the current editor instance
return originalMethod.call(this, ...args);
};
});
console.log('Headless SquibView ready. Access via window.squibEditor or window.getEditor()');
console.log('All API methods available through custom toolbar');
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
</script>
</body>
</html>