dioxus-nox-markdown 0.13.0

Headless markdown editor, previewer, and display components for Dioxus
Documentation
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
use dioxus::document::Eval;
use dioxus::prelude::document;

/// Adapter boundary for caret/selection interop.
///
/// All DOM/eval behavior should be expressed here, and consumers should call
/// helper functions in this module instead of `document::eval()` directly.
pub trait CaretAdapter: Send + Sync {
    /// JS to read `[selectionStart, selectionEnd]` from a textarea.
    fn read_textarea_selection_js(&self, editor_id: &str) -> String;
    /// JS to read `selectionStart` from a textarea.
    fn read_textarea_cursor_js(&self, editor_id: &str) -> String;
    /// JS to compute UTF-16 offset from block start to current DOM selection.
    fn read_block_visual_offset_js(&self, block_id: &str) -> String;
    /// JS to read contenteditable cursor selection as UTF-16 offset.
    fn read_contenteditable_selection_js(&self, block_id: &str) -> String;
    /// JS to read contenteditable selection details as compact string:
    /// `start<US>end<US>collapsed`.
    fn read_contenteditable_selection_detailed_js(&self, block_id: &str) -> String;
    /// JS to read cached beforeinput metadata as compact string:
    /// `start<US>end<US>collapsed<US>inputType<US>data`.
    fn read_contenteditable_beforeinput_meta_js(&self, block_id: &str) -> String;
    /// JS to focus and set textarea selection, with hydration-safe retries.
    fn mount_active_textarea_js(&self, textarea_id: &str, cursor_utf16: usize) -> String;
    /// JS to read contenteditable plain text.
    fn read_contenteditable_text_js(&self, block_id: &str) -> String;
    /// JS to place caret in a contenteditable block by UTF-16 code-unit index.
    fn set_contenteditable_selection_js(&self, block_id: &str, raw_utf16: usize) -> String;
    /// JS to restore a non-collapsed selection in a contenteditable block by
    /// visible UTF-16 offsets `[start, end]`.
    fn set_contenteditable_selection_range_js(
        &self,
        block_id: &str,
        start_utf16: usize,
        end_utf16: usize,
    ) -> String;
    /// JS hook for contenteditable input/traversal behavior.
    fn bind_contenteditable_input_js(&self, block_id: &str) -> String;
}

#[derive(Debug, Default)]
pub struct WebviewCaretAdapter;

impl CaretAdapter for WebviewCaretAdapter {
    fn read_textarea_selection_js(&self, editor_id: &str) -> String {
        format!(
            "var el = document.getElementById('{editor_id}');\
             if(el) dioxus.send([el.selectionStart ?? 0, el.selectionEnd ?? 0]);\
             else dioxus.send([0, 0]);"
        )
    }

    fn read_textarea_cursor_js(&self, editor_id: &str) -> String {
        format!(
            "var el = document.getElementById('{editor_id}');\
             if(el) dioxus.send(el.selectionStart ?? 0);\
             else dioxus.send(0);"
        )
    }

    fn read_block_visual_offset_js(&self, block_id: &str) -> String {
        format!(
            r#"(function() {{
    var el = document.getElementById('{block_id}');
    if (!el) {{ dioxus.send("0"); return; }}
    var sel = window.getSelection();
    if (!sel || sel.rangeCount === 0) {{ dioxus.send("0"); return; }}
    var range = sel.getRangeAt(0);
    var pre = range.cloneRange();
    pre.selectNodeContents(el);
    pre.setEnd(range.endContainer, range.endOffset);
    dioxus.send(pre.toString().length.toString());
}})();"#
        )
    }

    fn mount_active_textarea_js(&self, textarea_id: &str, cursor_utf16: usize) -> String {
        format!(
            r#"(function() {{
    var el = document.getElementById('{textarea_id}');
    if (!el) return;
    var tryFocus = function(attempts) {{
        if (attempts > 10) return;
        if (el.value.length === 0 && {cursor_utf16} > 0) {{
            setTimeout(function() {{ tryFocus(attempts + 1); }}, 10);
            return;
        }}
        el.focus();
        try {{
            el.setSelectionRange({cursor_utf16}, {cursor_utf16});
        }} catch (e) {{}}
        var resize = function() {{
            el.style.height = 'auto';
            el.style.height = el.scrollHeight + 'px';
        }};
        resize();
        if (!el._noxResizeBound) {{
            el.addEventListener('input', resize);
            el._noxResizeBound = true;
        }}
        if (!el._noxTraversalBound) {{
            el.addEventListener('keydown', function(e) {{
                if (e.key === 'ArrowUp') {{
                    var pos = el.selectionStart;
                    var text = el.value;
                    var isFirstLine = text.lastIndexOf('\n', pos - 1) === -1;
                    if (isFirstLine) {{
                        e.preventDefault();
                        dioxus.send("prev");
                    }}
                }} else if (e.key === 'ArrowDown') {{
                    var pos = el.selectionStart;
                    var text = el.value;
                    var isLastLine = text.indexOf('\n', pos) === -1;
                    if (isLastLine) {{
                        e.preventDefault();
                        dioxus.send("next");
                    }}
                }} else if (e.key === 'Backspace') {{
                    var pos = el.selectionStart;
                    if (pos === 0 && el.selectionStart === el.selectionEnd) {{
                        e.preventDefault();
                        dioxus.send("backjoin");
                    }}
                }} else if (e.key === 'Enter') {{
                    if (!e.shiftKey) {{
                        e.preventDefault();
                        dioxus.send("split:" + (el.selectionStart ?? 0));
                    }}
                }}
            }});
            el._noxTraversalBound = true;
        }}
    }};
    tryFocus(0);
}})();"#
        )
    }

    fn read_contenteditable_selection_js(&self, block_id: &str) -> String {
        self.read_block_visual_offset_js(block_id)
    }

    fn read_contenteditable_selection_detailed_js(&self, block_id: &str) -> String {
        format!(
            r#"(function() {{
    var root = document.getElementById('{block_id}');
    if (!root) {{ dioxus.send("0\u001f0\u001f1"); return; }}
    var sel = window.getSelection();
    if (!sel || sel.rangeCount === 0) {{ dioxus.send("0\u001f0\u001f1"); return; }}
    var range = sel.getRangeAt(0);
    var toOffset = function(node, offset) {{
        if (!node || !root.contains(node)) return 0;
        try {{
            var r = document.createRange();
            r.selectNodeContents(root);
            r.setEnd(node, offset);
            return r.toString().length;
        }} catch (_e) {{
            return 0;
        }}
    }};
    var start = toOffset(range.startContainer, range.startOffset);
    var end = toOffset(range.endContainer, range.endOffset);
    var collapsed = start === end ? "1" : "0";
    dioxus.send(start.toString() + "\u001f" + end.toString() + "\u001f" + collapsed);
}})();"#
        )
    }

    fn read_contenteditable_beforeinput_meta_js(&self, block_id: &str) -> String {
        format!(
            r#"(function() {{
    var root = document.getElementById('{block_id}');
    if (!root || typeof root._noxBeforeInputMeta !== "string") {{
        dioxus.send("");
        return;
    }}
    var meta = root._noxBeforeInputMeta;
    root._noxBeforeInputMeta = null;
    dioxus.send(meta);
}})();"#
        )
    }

    fn read_contenteditable_text_js(&self, block_id: &str) -> String {
        format!(
            "var el = document.getElementById('{block_id}');\
             if(el) dioxus.send(el.innerText ?? '');\
             else dioxus.send('');"
        )
    }

    fn set_contenteditable_selection_js(&self, block_id: &str, raw_utf16: usize) -> String {
        format!(
            r#"(function() {{
    var root = document.getElementById('{block_id}');
    if (!root) return;
    root.focus();
    var walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
    var remaining = {raw_utf16};
    var node = null;
    while ((node = walker.nextNode())) {{
        var len = (node.nodeValue || '').length;
        if (remaining <= len) {{
            try {{
                var range = document.createRange();
                range.setStart(node, remaining);
                range.collapse(true);
                var sel = window.getSelection();
                sel.removeAllRanges();
                sel.addRange(range);
            }} catch (e) {{}}
            return;
        }}
        remaining -= len;
    }}
}})();"#
        )
    }

    fn set_contenteditable_selection_range_js(
        &self,
        block_id: &str,
        start_utf16: usize,
        end_utf16: usize,
    ) -> String {
        format!(
            r#"(function() {{
    var root = document.getElementById('{block_id}');
    if (!root) return;
    root.focus();
    function findPos(target) {{
        var walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
        var remaining = target;
        var node = null;
        while ((node = walker.nextNode())) {{
            var len = (node.nodeValue || '').length;
            if (remaining <= len) return {{ node: node, offset: remaining }};
            remaining -= len;
        }}
        return null;
    }}
    var s = findPos({start_utf16});
    var e = findPos({end_utf16});
    if (!s || !e) return;
    try {{
        var range = document.createRange();
        range.setStart(s.node, s.offset);
        range.setEnd(e.node, e.offset);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    }} catch (ex) {{}}
}})();"#
        )
    }

    fn bind_contenteditable_input_js(&self, block_id: &str) -> String {
        format!(
            r#"(function() {{
    var root = document.getElementById('{block_id}');
    if (!root) {{ dioxus.send("missing"); return; }}
    if (root._noxBeforeInputBound) {{ dioxus.send("bound"); return; }}
    root._noxBeforeInputMeta = null;

    var selectionDetails = function() {{
        var sel = window.getSelection();
        if (!sel || sel.rangeCount === 0) {{
            return {{ start: 0, end: 0, collapsed: true }};
        }}
        var range = sel.getRangeAt(0);
        var toOffset = function(node, offset) {{
            if (!node || !root.contains(node)) return 0;
            try {{
                var r = document.createRange();
                r.selectNodeContents(root);
                r.setEnd(node, offset);
                return r.toString().length;
            }} catch (_e) {{
                return 0;
            }}
        }};
        var start = toOffset(range.startContainer, range.startOffset);
        var end = toOffset(range.endContainer, range.endOffset);
        if (end < start) {{
            var t = start; start = end; end = t;
        }}
        return {{ start: start, end: end, collapsed: start === end }};
    }};

    root.addEventListener('beforeinput', function(e) {{
        var sel = selectionDetails();
        var inputType = (e && typeof e.inputType === 'string') ? e.inputType : '';
        var data = (e && typeof e.data === 'string') ? e.data : '';
        root._noxBeforeInputMeta =
            sel.start.toString() + '\u001f' +
            sel.end.toString() + '\u001f' +
            (sel.collapsed ? '1' : '0') + '\u001f' +
            inputType + '\u001f' +
            data;
    }});

    root._noxBeforeInputBound = true;
    dioxus.send("bound");
}})();"#
        )
    }
}

#[derive(Debug, Default)]
pub struct NoopCaretAdapter;

impl CaretAdapter for NoopCaretAdapter {
    fn read_textarea_selection_js(&self, _editor_id: &str) -> String {
        "dioxus.send([0, 0]);".to_string()
    }

    fn read_textarea_cursor_js(&self, _editor_id: &str) -> String {
        "dioxus.send(0);".to_string()
    }

    fn read_block_visual_offset_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"0\");".to_string()
    }

    fn mount_active_textarea_js(&self, _textarea_id: &str, _cursor_utf16: usize) -> String {
        "dioxus.send(\"noop\");".to_string()
    }

    fn read_contenteditable_selection_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"0\");".to_string()
    }

    fn read_contenteditable_selection_detailed_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"0\\u001f0\\u001f1\");".to_string()
    }

    fn read_contenteditable_beforeinput_meta_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"\");".to_string()
    }

    fn read_contenteditable_text_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"\");".to_string()
    }

    fn set_contenteditable_selection_js(&self, _block_id: &str, _raw_utf16: usize) -> String {
        "dioxus.send(\"noop\");".to_string()
    }

    fn set_contenteditable_selection_range_js(
        &self,
        _block_id: &str,
        _start_utf16: usize,
        _end_utf16: usize,
    ) -> String {
        String::new()
    }

    fn bind_contenteditable_input_js(&self, _block_id: &str) -> String {
        "dioxus.send(\"noop\");".to_string()
    }
}

#[cfg(any(
    target_arch = "wasm32",
    target_os = "windows",
    target_os = "macos",
    target_os = "linux",
    target_os = "ios",
    target_os = "android"
))]
static WEBVIEW_ADAPTER: WebviewCaretAdapter = WebviewCaretAdapter;
#[cfg(not(any(
    target_arch = "wasm32",
    target_os = "windows",
    target_os = "macos",
    target_os = "linux",
    target_os = "ios",
    target_os = "android"
)))]
static NOOP_ADAPTER: NoopCaretAdapter = NoopCaretAdapter;

/// Returns the platform adapter for caret/selection interop.
pub fn caret_adapter() -> &'static dyn CaretAdapter {
    #[cfg(any(
        target_arch = "wasm32",
        target_os = "windows",
        target_os = "macos",
        target_os = "linux",
        target_os = "ios",
        target_os = "android"
    ))]
    {
        &WEBVIEW_ADAPTER
    }

    #[cfg(not(any(
        target_arch = "wasm32",
        target_os = "windows",
        target_os = "macos",
        target_os = "linux",
        target_os = "ios",
        target_os = "android"
    )))]
    {
        &NOOP_ADAPTER
    }
}

/// Start an eval session.
pub fn start_eval(js: &str) -> Eval {
    document::eval(js)
}

/// Evaluate JS and ignore the result.
pub async fn eval_void(js: &str) {
    let _ = document::eval(js).await;
}

/// Receive a string from an eval session.
pub async fn recv_string(eval: &mut Eval) -> Option<String> {
    eval.recv::<String>().await.ok()
}

/// Receive `u64` from an eval session.
pub async fn recv_u64(eval: &mut Eval) -> Option<u64> {
    eval.recv::<u64>().await.ok()
}

/// Receive `f64` from an eval session.
pub async fn recv_f64(eval: &mut Eval) -> Option<f64> {
    eval.recv::<f64>().await.ok()
}

/// Receive `Vec<u64>` from an eval session.
pub async fn recv_vec_u64(eval: &mut Eval) -> Option<Vec<u64>> {
    eval.recv::<Vec<u64>>().await.ok()
}