-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-api.html
More file actions
397 lines (353 loc) · 27.8 KB
/
selection-api.html
File metadata and controls
397 lines (353 loc) · 27.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
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SquibView Selection API Guide</title>
<link rel="icon" type="image/svg+xml" 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>">
<style>
/* Additional CSS */
/* SquibView CLI Generated Styles */
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
line-height: 1.6;
color: #333;
max-width: 900px;
margin: 0 auto;
padding: 2rem 3rem;
background-color: #fff;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.5em;
margin-bottom: 0.5em;
font-weight: 600;
line-height: 1.25;
}
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
h3 { font-size: 1.25rem; }
h4 { font-size: 1rem; }
h5 { font-size: 0.875rem; }
h6 { font-size: 0.85rem; color: #666; }
p { margin-bottom: 1rem; }
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
color: #0066cc;
text-decoration: underline;
}
ul, ol {
margin-bottom: 1rem;
padding-left: 2rem;
}
li {
margin-bottom: 0.25rem;
}
blockquote {
margin: 1rem 0;
padding: 0.5rem 1rem;
border-left: 4px solid #ddd;
background-color: #f9f9f9;
color: #666;
}
code {
background-color: #f5f5f5;
padding: 0.2em 0.4em;
border-radius: 3px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.85em;
}
pre {
background-color: #f8f8f8;
padding: 1rem;
margin-bottom: 1rem;
overflow-x: auto;
border-radius: 4px;
}
pre code {
background-color: transparent;
padding: 0;
border-radius: 0;
}
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 1rem;
}
th, td {
border: 1px solid #ddd;
padding: 0.5rem;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
img {
max-width: 100%;
height: auto;
margin-bottom: 1rem;
}
hr {
border: none;
border-top: 1px solid #ddd;
margin: 2rem 0;
}
/* Badge images - inline display */
p img {
display: inline;
margin: 0 2px;
vertical-align: middle;
}
/* Math styling */
.MathJax {
overflow-x: auto;
overflow-y: hidden;
}
/* Responsive design */
@media (max-width: 768px) {
body {
padding: 1rem 1.5rem;
}
table {
font-size: 0.875rem;
}
th, td {
padding: 0.25rem;
}
}
/* Print styles */
@media print {
body {
padding: 0;
color: #000;
}
a {
color: #000;
text-decoration: underline;
}
pre, blockquote {
page-break-inside: avoid;
}
h1, h2, h3, h4, h5, h6 {
page-break-after: avoid;
}
}
</style>
<!-- Highlight.js for syntax highlighting -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<!-- Leaflet for GeoJSON/TopoJSON maps -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://unpkg.com/topojson-client@3.1.0/dist/topojson-client.min.js"></script>
<!-- Three.js for STL 3D models -->
<script src="https://unpkg.com/three@0.171.0/build/three.min.js"></script>
<!-- MathJax for math rendering -->
<script>
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
processEscapes: true,
processEnvironments: true
},
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
svg: {
fontCache: 'global'
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
<!-- Mermaid for diagram rendering -->
<script src="https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"></script>
</head>
<body>
<div><h1>SquibView Selection API Guide</h1><p>The Selection API in SquibView allows you to detect, examine, and manipulate text selections in both the source and rendered panels. This guide explains how to use these powerful features in your applications.</p><h2>Table of Contents</h2><ol><li><a href="#introduction">Introduction</a></li><li><a href="#selection-data-structure">Selection Data Structure</a></li><li><a href="#detecting-selections">Detecting Selections</a></li><li><a href="#manipulating-selected-text">Manipulating Selected Text</a></li><li><a href="#configuring-selection-behavior">Configuring Selection Behavior</a></li><li><a href="#examples">Examples</a></li><li><a href="#best-practices">Best Practices</a></li></ol><h2>Introduction</h2><p>SquibView’s Selection API enables you to:</p><ul><li>Detect when users select text in either panel</li><li>Access information about the selected text</li><li>Replace selected text programmatically</li><li>Control editability of selected text</li><li>Create advanced interactive behaviors</li></ul><h2>Selection Data Structure</h2><p>When text is selected, SquibView creates a selection data object with the following structure:</p><p>For the source panel:</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">{
<span class="hljs-attr">panel</span>: <span class="hljs-string">'source'</span>,
<span class="hljs-attr">text</span>: <span class="hljs-string">'selected text'</span>,
<span class="hljs-attr">range</span>: {
<span class="hljs-attr">start</span>: <span class="hljs-number">10</span>, <span class="hljs-comment">// Character position</span><span class="hljs-attr">end</span>: <span class="hljs-number">22</span><span class="hljs-comment">// Character position</span>
}
}
</code></pre></div><p>For the rendered panel:</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">{
<span class="hljs-attr">panel</span>: <span class="hljs-string">'rendered'</span>,
<span class="hljs-attr">text</span>: <span class="hljs-string">'selected text'</span>,
<span class="hljs-attr">range</span>: <span class="hljs-title class_">DOMRange</span>, <span class="hljs-comment">// DOM Range object</span><span class="hljs-attr">element</span>: <span class="hljs-title class_">HTMLElement</span><span class="hljs-comment">// The contenteditable element</span>
}
</code></pre></div><h2>Detecting Selections</h2><p>There are two main ways to detect and respond to text selections:</p><h3>1. Using the onTextSelected Callback</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Register a callback for selection events</span><span class="hljs-keyword">const</span> unsubscribe = editor.<span class="hljs-title function_">onTextSelected</span>(<span class="hljs-function"><span class="hljs-params">selectionData</span> =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Selected text: <span class="hljs-subst">${selectionData.text}</span>`</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`In panel: <span class="hljs-subst">${selectionData.panel}</span>`</span>);
<span class="hljs-comment">// Do something with the selection</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'TODO'</span>)) {
<span class="hljs-title function_">handleTodo</span>(selectionData);
}
});
<span class="hljs-comment">// Later, to stop listening for selections:</span><span class="hljs-title function_">unsubscribe</span>();
</code></pre></div><h3>2. Using getCurrentSelection Method</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Get current selection at any time</span><span class="hljs-keyword">const</span> button = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'checkSelection'</span>);
button.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function">() =></span> {
<span class="hljs-keyword">const</span> selection = editor.<span class="hljs-title function_">getCurrentSelection</span>();
<span class="hljs-keyword">if</span> (selection) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Currently selected: <span class="hljs-subst">${selection.text}</span>`</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Nothing currently selected'</span>);
}
});
</code></pre></div><h2>Manipulating Selected Text</h2><p>Once you have a selection, you can manipulate it in various ways:</p><h3>Replacing Selected Text</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Replace selected text with new content</span>
editor.<span class="hljs-title function_">replaceSelectedText</span>(<span class="hljs-string">'replacement text'</span>, selectionData);
<span class="hljs-comment">// Add formatting based on panel</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">panel</span> === <span class="hljs-string">'source'</span>) {
editor.<span class="hljs-title function_">replaceSelectedText</span>(<span class="hljs-string">`**<span class="hljs-subst">${selectionData.text}</span>**`</span>, selectionData); <span class="hljs-comment">// Bold in Markdown</span>
} <span class="hljs-keyword">else</span> {
editor.<span class="hljs-title function_">replaceSelectedText</span>(<span class="hljs-string">`<strong><span class="hljs-subst">${selectionData.text}</span></strong>`</span>, selectionData); <span class="hljs-comment">// Bold in HTML</span>
}
</code></pre></div><h3>Controlling Editability</h3><p>You can make text in the rendered panel non-editable (locked) or editable (unlocked):</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Make selected text non-editable (only works in rendered panel)</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">panel</span> === <span class="hljs-string">'rendered'</span>) {
editor.<span class="hljs-title function_">setSelectionEditable</span>(<span class="hljs-literal">false</span>, selectionData); <span class="hljs-comment">// Lock (make non-editable)</span><span class="hljs-comment">// Or make it editable</span>
editor.<span class="hljs-title function_">setSelectionEditable</span>(<span class="hljs-literal">true</span>, selectionData); <span class="hljs-comment">// Unlock (make editable)</span><span class="hljs-comment">// Or use the smart toggle that automatically detects current state</span><span class="hljs-keyword">const</span> newState = editor.<span class="hljs-title function_">toggleSelectionLock</span>(selectionData);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newState ? <span class="hljs-string">"Now editable"</span> : <span class="hljs-string">"Now locked"</span>);
}
</code></pre></div><p>Locked content is visually indicated with:</p><ul><li>A lock icon (🔒) at the top-left corner</li><li>A light gray background</li><li>A dashed border</li><li>“Not allowed” cursor when hovering</li></ul><p>Unlocked content (after previously being locked) shows an unlock icon (🔓).</p><h3>Clearing Selections</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Clear current selection</span>
editor.<span class="hljs-title function_">clearSelection</span>();
</code></pre></div><h2>Configuring Selection Behavior</h2><h3>Using onReplaceSelectedText Handler</h3><p>The <code>onReplaceSelectedText</code> handler provides a powerful way to automatically process selections and optionally replace them:</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Set up a selection handler</span>
editor.<span class="hljs-property">onReplaceSelectedText</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">selectionData</span>) {
<span class="hljs-comment">// Process selection</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span> === <span class="hljs-string">'TODO'</span>) {
<span class="hljs-keyword">return</span><span class="hljs-string">'✅ DONE'</span>; <span class="hljs-comment">// Return text to replace the selection</span>
}
<span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^[a-z]/</span>)) {
<span class="hljs-comment">// Capitalize first letter</span><span class="hljs-keyword">return</span> selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">charAt</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toUpperCase</span>() +
selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">slice</span>(<span class="hljs-number">1</span>);
}
<span class="hljs-comment">// Return undefined or null to not replace anything</span><span class="hljs-comment">// This allows processing selections without modifying them</span><span class="hljs-title function_">logSelectionToAnalytics</span>(selectionData);
};
<span class="hljs-comment">// Remove the handler</span>
editor.<span class="hljs-property">onReplaceSelectedText</span> = <span class="hljs-literal">null</span>;
</code></pre></div><h3>Setting Handler in Constructor</h3><p>You can set up the selection handler when creating the SquibView instance:</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-keyword">const</span> editor = <span class="hljs-keyword">new</span><span class="hljs-title class_">SquibView</span>(<span class="hljs-string">'#editor'</span>, {
<span class="hljs-attr">initialContent</span>: <span class="hljs-string">'# Hello World'</span>,
<span class="hljs-attr">inputContentType</span>: <span class="hljs-string">'md'</span>,
<span class="hljs-attr">onReplaceSelectedText</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">selectionData</span>) {
<span class="hljs-comment">// Process selections from the beginning</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span> === <span class="hljs-string">'hello'</span>) {
<span class="hljs-keyword">return</span><span class="hljs-string">'Hello, world!'</span>;
}
}
});
</code></pre></div><h2>Examples</h2><h3>Basic Text Replacement</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">editor.<span class="hljs-title function_">onTextSelected</span>(<span class="hljs-function"><span class="hljs-params">selectionData</span> =></span> {
<span class="hljs-comment">// Always add exclamation marks to selected text</span><span class="hljs-keyword">const</span> newText = <span class="hljs-string">`<span class="hljs-subst">${selectionData.text}</span>!!!`</span>;
editor.<span class="hljs-title function_">replaceSelectedText</span>(newText, selectionData);
});
</code></pre></div><h3>Smart Formatting</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">editor.<span class="hljs-property">onReplaceSelectedText</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">selectionData</span>) {
<span class="hljs-comment">// Example: Smart formatting based on text content</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^\d+$/</span>)) {
<span class="hljs-comment">// Format numbers with commas</span><span class="hljs-keyword">return</span><span class="hljs-title class_">Number</span>(selectionData.<span class="hljs-property">text</span>).<span class="hljs-title function_">toLocaleString</span>();
}
<span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">toLowerCase</span>() === <span class="hljs-string">'date'</span>) {
<span class="hljs-comment">// Replace "date" with current date</span><span class="hljs-keyword">return</span><span class="hljs-keyword">new</span><span class="hljs-title class_">Date</span>().<span class="hljs-title function_">toLocaleDateString</span>();
}
};
</code></pre></div><h3>Text Analysis Without Replacement</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">editor.<span class="hljs-title function_">onTextSelected</span>(<span class="hljs-function"><span class="hljs-params">selectionData</span> =></span> {
<span class="hljs-comment">// Analyze text without replacing it</span><span class="hljs-keyword">const</span> wordCount = selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">split</span>(<span class="hljs-regexp">/\s+/</span>).<span class="hljs-title function_">filter</span>(<span class="hljs-title class_">Boolean</span>).<span class="hljs-property">length</span>;
<span class="hljs-keyword">const</span> charCount = selectionData.<span class="hljs-property">text</span>.<span class="hljs-property">length</span>;
<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'stats'</span>).<span class="hljs-property">innerHTML</span> = <span class="hljs-string">`
<p>Selection contains <span class="hljs-subst">${wordCount}</span> words and <span class="hljs-subst">${charCount}</span> characters</p>
`</span>;
});
</code></pre></div><h3>Creating a Simple Dictionary Tool</h3><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">editor.<span class="hljs-property">onReplaceSelectedText</span> = <span class="hljs-keyword">async</span><span class="hljs-keyword">function</span>(<span class="hljs-params">selectionData</span>) {
<span class="hljs-comment">// Only process single words</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">trim</span>().<span class="hljs-title function_">split</span>(<span class="hljs-regexp">/\s+/</span>).<span class="hljs-property">length</span> === <span class="hljs-number">1</span>) {
<span class="hljs-keyword">const</span> word = selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">trim</span>();
<span class="hljs-comment">// Show definition in a tooltip (without replacing)</span><span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span><span class="hljs-title function_">fetch</span>(<span class="hljs-string">`https://api.dictionaryapi.dev/api/v2/entries/en/<span class="hljs-subst">${word}</span>`</span>);
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.<span class="hljs-title function_">json</span>();
<span class="hljs-keyword">if</span> (data && data[<span class="hljs-number">0</span>] && data[<span class="hljs-number">0</span>].<span class="hljs-property">meanings</span>) {
<span class="hljs-keyword">const</span> definition = data[<span class="hljs-number">0</span>].<span class="hljs-property">meanings</span>[<span class="hljs-number">0</span>].<span class="hljs-property">definitions</span>[<span class="hljs-number">0</span>].<span class="hljs-property">definition</span>;
<span class="hljs-title function_">showTooltip</span>(definition, selectionData);
}
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Dictionary lookup failed'</span>, err);
}
}
<span class="hljs-comment">// Return undefined to not replace the text</span>
};
</code></pre></div><h2>Best Practices</h2><ol><li><p><strong>Consider Context</strong>: Selection behavior may differ between source and rendered panels</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">panel</span> === <span class="hljs-string">'source'</span>) {
<span class="hljs-comment">// Handle source panel selection (plain text)</span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// Handle rendered panel selection (may contain HTML)</span>
}
</code></pre></div></li><li><p><strong>Clean Up Listeners</strong>: Always store and use the unsubscribe function to prevent memory leaks</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-keyword">const</span> unsubscribe = editor.<span class="hljs-title function_">onTextSelected</span>(handler);
<span class="hljs-comment">// Later:</span><span class="hljs-title function_">unsubscribe</span>();
</code></pre></div></li><li><p><strong>Respect User Intent</strong>: Avoid aggressive text replacement that might disrupt the user’s workflow</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Good: Only replace when there's a clear pattern or intent</span><span class="hljs-keyword">if</span> (selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^todo:/i</span>)) {
<span class="hljs-keyword">return</span><span class="hljs-string">'✅ '</span> + selectionData.<span class="hljs-property">text</span>.<span class="hljs-title function_">substring</span>(<span class="hljs-number">5</span>);
}
</code></pre></div></li><li><p><strong>Improve UX With Visual Feedback</strong>: Indicate when selection handling is active</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript">editor.<span class="hljs-title function_">onTextSelected</span>(<span class="hljs-function"><span class="hljs-params">selectionData</span> =></span> {
<span class="hljs-comment">// Show UI indicator when text is selected</span><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'selection-active'</span>).<span class="hljs-property">style</span>.<span class="hljs-property">display</span> = <span class="hljs-string">'block'</span>;
<span class="hljs-comment">// Hide when selection is cleared</span><span class="hljs-variable language_">window</span>.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
<span class="hljs-keyword">if</span> (!editor.<span class="hljs-title function_">getCurrentSelection</span>()) {
<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'selection-active'</span>).<span class="hljs-property">style</span>.<span class="hljs-property">display</span> = <span class="hljs-string">'none'</span>;
}
}, <span class="hljs-number">100</span>);
});
</code></pre></div></li><li><p><strong>Combine With Other Events</strong>: Create powerful workflows by combining selection with other events</p><div data-source-type="javascript"><pre><code class="hljs language-javascript" data-source-type="code" data-lang="javascript"><span class="hljs-comment">// Track selection</span><span class="hljs-keyword">let</span> lastSelection = <span class="hljs-literal">null</span>;
editor.<span class="hljs-title function_">onTextSelected</span>(<span class="hljs-function"><span class="hljs-params">selectionData</span> =></span> {
lastSelection = selectionData;
});
<span class="hljs-comment">// Use with keyboard shortcuts</span><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'keydown'</span>, <span class="hljs-function"><span class="hljs-params">e</span> =></span> {
<span class="hljs-keyword">if</span> (e.<span class="hljs-property">ctrlKey</span> && e.<span class="hljs-property">key</span> === <span class="hljs-string">'b'</span> && lastSelection) {
<span class="hljs-comment">// Bold on Ctrl+B</span><span class="hljs-keyword">if</span> (lastSelection.<span class="hljs-property">panel</span> === <span class="hljs-string">'source'</span>) {
editor.<span class="hljs-title function_">replaceSelectedText</span>(<span class="hljs-string">`**<span class="hljs-subst">${lastSelection.text}</span>**`</span>, lastSelection);
} <span class="hljs-keyword">else</span> {
editor.<span class="hljs-title function_">replaceSelectedText</span>(<span class="hljs-string">`<strong><span class="hljs-subst">${lastSelection.text}</span></strong>`</span>, lastSelection);
}
e.<span class="hljs-title function_">preventDefault</span>();
}
});
</code></pre></div></li></ol></div>
<script>
// Initialize MathJax after page load
document.addEventListener('DOMContentLoaded', function() {
if (window.MathJax && window.MathJax.typesetPromise) {
window.MathJax.typesetPromise().catch(function(err) {
console.warn('MathJax typesetting failed:', err);
});
}
});
</script>
<script>
// Initialize Mermaid after page load
document.addEventListener('DOMContentLoaded', function() {
if (window.mermaid) {
try {
window.mermaid.initialize({
startOnLoad: false,
theme: 'default',
securityLevel: 'loose',
fontFamily: 'inherit'
});
// Manually render all mermaid diagrams
const mermaidElements = document.querySelectorAll('.mermaid');
mermaidElements.forEach(function(element, index) {
const id = 'mermaid-' + index;
element.id = id;
try {
window.mermaid.render(id + '-svg', element.textContent, function(svgCode) {
element.innerHTML = svgCode;
});
} catch (err) {
console.warn('Mermaid rendering failed for element', index, ':', err);
// Fallback: show the text content
element.innerHTML = '<pre>' + element.textContent + '</pre>';
}
});
} catch (err) {
console.warn('Mermaid initialization failed:', err);
}
}
});
</script>
</body>
</html>