detaxine-ui 0.8.29

A UI Library for Leptos
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
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
use icondata::{
    BiStrikethroughRegular, BsBraces, BsCode, BsImage, BsListOl, BsListUl, BsTypeItalic,
    BsTypeUnderline, FiBold, VsMarkdown,
};
use js_sys::wasm_bindgen::prelude::Closure;
use leptos::html::Div;
use leptos::task::spawn_local;
use leptos::wasm_bindgen::JsCast;
use leptos::{ev, prelude::*};
use markdown;
use std::pin::Pin;
use web_sys::{Element, HtmlDivElement, HtmlInputElement, Node, window};

use crate::components::actions::button::BasicButton;
use crate::components::forms::input::{InputField, InputFieldType};
use crate::components::forms::select::{SelectInput, SelectOption};
use crate::components::forms::textarea::Textarea;
use crate::utils::forms::fire_bubbled_and_cancelable_event;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExtraFormatingOption {
    MarkdownUpload,
    ImageUpload,
    Heading,
    InlineCode,
    CodeBlock,
    Lists,
}

pub type InsertImageCallback =
    Callback<web_sys::File, Pin<Box<dyn Future<Output = Option<String>>>>>;

/// A rich text editor with a formatting toolbar supporting bold, italic, underline,
/// strikethrough, and optional extras like headings, inline code, code blocks, lists,
/// image upload, and markdown upload.
///
/// # Props
///
/// - `initial_content` – `RwSignal<String>` containing the initial HTML content. Defaults to `<p><br></p>`.
/// - `id_attr` – HTML `id` applied to the editor and its hidden inputs.
/// - `name` – `name` attribute forwarded to the hidden textarea for form submission.
/// - `placeholder` – Placeholder text shown when the editor is empty.
/// - `extra_formating_options` – Opt-in toolbar features via `Vec<ExtraFormatingOption>`:
///   `MarkdownUpload`, `ImageUpload`, `Heading`, `InlineCode`, `CodeBlock`, `Lists`.
/// - `on_image_insert` – Async callback receiving a `web_sys::File` and returning `Option<String>` (the URL to insert). Defaults to a base64 data-URL reader.
///
/// # Example
///
/// ```
/// use leptos::prelude::*;
/// use detaxine_ui::components::content::richtext_editor::{ExtraFormatingOption, RichTextEditor};
///
/// #[component]
/// fn Example() -> impl IntoView {
///     let content = RwSignal::new("<p><br></p>".to_string());
///     view! {
///         <RichTextEditor
///             initial_content=content
///             id_attr="my-editor"
///             name="body"
///             extra_formating_options=vec![
///                 ExtraFormatingOption::Heading,
///                 ExtraFormatingOption::CodeBlock,
///                 ExtraFormatingOption::Lists,
///             ]
///         />
///     }
/// }
/// ```
#[component]
pub fn RichTextEditor(
    #[prop(into, optional, default = RwSignal::new("<p><br></p>".into()))]
    initial_content: RwSignal<String>,
    #[prop(into, optional)] id_attr: String,
    #[prop(into, optional)] name: String,
    #[prop(into, optional)] placeholder: String,
    #[prop(optional, default = Vec::new())] extra_formating_options: Vec<ExtraFormatingOption>,
    #[prop(optional, default = Callback::new(move |file: web_sys::File| {
        Box::pin(async move {
            gloo_file::futures::read_as_data_url(&file.into())
                .await
                .ok()
        }) as Pin<Box<dyn Future<Output = Option<String>>>>
    }))]
    on_image_insert: InsertImageCallback,
) -> impl IntoView {
    let editor_ref = NodeRef::new();
    let file_input_ref = NodeRef::new();
    let font_options = RwSignal::new(
        [
            ("p", "Normal"),
            ("h1", "H1"),
            ("h2", "H2"),
            ("h3", "H3"),
            ("h4", "H4"),
            ("h5", "H5"),
            ("h6", "H6"),
        ]
        .into_iter()
        .map(|(value, label)| SelectOption::new(value, label))
        .collect::<Vec<_>>(),
    );
    let language_options = RwSignal::new(
        [
            ("plaintext", "Plain Text"),
            ("rust", "Rust"),
            ("surql", "SurrealQL"),
            ("javascript", "JavaScript"),
            ("typescript", "TypeScript"),
            ("python", "Python"),
            ("java", "Java"),
            ("cpp", "C++"),
            ("c", "C"),
            ("csharp", "C#"),
            ("go", "Go"),
            ("ruby", "Ruby"),
            ("php", "PHP"),
            ("html", "HTML"),
            ("css", "CSS"),
            ("json", "JSON"),
            ("sql", "SQL"),
            ("bash", "Bash"),
            ("yaml", "YAML"),
            ("markdown", "Markdown"),
        ]
        .into_iter()
        .map(|(value, label)| SelectOption::new(value, label))
        .collect::<Vec<_>>(),
    );
    let last_enter_empty = RwSignal::new(false);
    let show_language_picker = RwSignal::new(false);
    let (tracked_content, set_tracked_content) = signal(String::new());
    let md_file_input_ref = NodeRef::new();

    // Track active formatting states
    let is_bold = RwSignal::new(false);
    let is_italic = RwSignal::new(false);
    let is_underline = RwSignal::new(false);
    let is_strikethrough = RwSignal::new(false);
    let is_inline_code = RwSignal::new(false);
    let is_code_block = RwSignal::new(false);
    let is_ordered_list = RwSignal::new(false);
    let is_unordered_list = RwSignal::new(false);

    // Create style functions for buttons
    let active_style = move |signal: RwSignal<bool>| {
        Memo::new(move |_| {
            if signal.get() {
                "bg-primary text-contrast-white"
            } else {
                "hover:bg-light-gray"
            }
            .into()
        })
    };

    let bold_style: Memo<String> = active_style(is_bold);
    let italic_style = active_style(is_italic);
    let underline_style = active_style(is_underline);
    let strikethrough_style = active_style(is_strikethrough);
    let inline_code_style = active_style(is_inline_code);
    let code_block_style = active_style(is_code_block);
    let ordered_list_style = active_style(is_ordered_list);
    let unordered_list_style = active_style(is_unordered_list);

    let update_button_states = move || {
        is_bold.set(cursor_inside("b").is_some());
        is_italic.set(cursor_inside("i").is_some());
        is_underline.set(cursor_inside("u").is_some());
        is_strikethrough.set(cursor_inside("s").is_some());
        is_inline_code.set(cursor_inside("code").is_some() && current_code_block().is_none());
        is_code_block.set(current_code_block().is_some());

        if let Some((list, _)) = current_list_item() {
            let tag = list.tag_name().to_lowercase();
            is_ordered_list.set(tag == "ol");
            is_unordered_list.set(tag == "ul");
        } else {
            is_ordered_list.set(false);
            is_unordered_list.set(false);
        }
    };

    let toggle_style = move |tag_name: &'static str| {
        if let Some(el) = cursor_inside(tag_name) {
            // Toggle off: insert a zero-width space after the element and move cursor there
            if let Some(doc) = window().and_then(|w| w.document()) {
                if let Ok(Some(selection)) = doc.get_selection() {
                    // Create a zero-width space to break out of the formatting
                    let space = doc.create_text_node("\u{200B}");

                    // Insert it after the styled element
                    if let Some(parent) = el.parent_node() {
                        if let Some(next_sibling) = el.next_sibling() {
                            parent.insert_before(&space, Some(&next_sibling)).ok();
                        } else {
                            parent.append_child(&space).ok();
                        }

                        // Move cursor after the zero-width space
                        if let Ok(new_range) = doc.create_range() {
                            new_range.set_start(&space, 1).ok();
                            new_range.set_end(&space, 1).ok();
                            selection.remove_all_ranges().ok();
                            selection.add_range(&new_range).ok();
                        }
                    }
                }
            }
        } else {
            // Toggle on: Wrap selection or insert at cursor
            if let Some(doc) = window().and_then(|w| w.document()) {
                if let Ok(Some(selection)) = doc.get_selection() {
                    if let Ok(range) = selection.get_range_at(0) {
                        if let Ok(element) = doc.create_element(tag_name) {
                            if range.collapsed() {
                                // Insert zero-width space so caret can live inside the tag
                                let text = doc.create_text_node("\u{200B}");
                                let _ = element.append_child(&text);
                                let _ = range.insert_node(&element);
                                if let Ok(new_range) = doc.create_range() {
                                    // Place cursor AFTER the zero-width space (inside tag)
                                    let _ = new_range.set_start(&text, 1);
                                    let _ = new_range.set_end(&text, 1);
                                    let _ = selection.remove_all_ranges();
                                    let _ = selection.add_range(&new_range);
                                }
                            } else {
                                // Wrap selected text
                                let contents = range.clone_contents().ok();
                                range.delete_contents().ok();
                                if let Some(contents) = contents {
                                    element.append_child(&contents).ok();
                                }
                                range.insert_node(&element).ok();

                                // Move cursor to the end of the new element
                                if let Ok(new_range) = doc.create_range() {
                                    new_range.select_node_contents(&element).ok();
                                    new_range.collapse_with_to_start(false);
                                    let _ = selection.remove_all_ranges();
                                    let _ = selection.add_range(&new_range);
                                }
                            }
                        }
                    }
                }
            }
        }
        update_button_states();
    };

    let on_keydown = move |ev: web_sys::KeyboardEvent| {
        if ev.key() != "Enter" {
            return;
        }

        // CODE BLOCK
        if let Some((pre, code)) = current_code_block() {
            ev.prevent_default();
            handle_code_enter(&pre, &code, &last_enter_empty);
            update_button_states();
            return;
        }

        // INLINE CODE
        if let Some(code) = current_inline_code() {
            ev.prevent_default();
            handle_inline_code_enter(&code);
            update_button_states();
            return;
        }

        // LIST ITEM
        if let Some((list, li)) = current_list_item() {
            ev.prevent_default();
            handle_list_enter(&list, &li);
            update_button_states();
            return;
        }

        // DEFAULT
        // Let browser handle paragraphs, headings, etc.

        if let Some(window) = window() {
            let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(
                Closure::once_into_js(move || {
                    update_button_states();
                })
                .as_ref()
                .unchecked_ref(),
                0,
            );
        }
    };

    // Update button states on selection change
    let on_selection_change = move |_: ev::MouseEvent| {
        update_button_states();
    };

    let on_keyup = move |_: ev::KeyboardEvent| {
        update_button_states();
    };

    let bold = Callback::new(move |_| toggle_style("b"));
    let italic = Callback::new(move |_| toggle_style("i"));
    let underline = Callback::new(move |_| toggle_style("u"));
    let strikethrough = Callback::new(move |_| toggle_style("s"));

    // Fixed inline code handler - check if we're in a code block first
    let inline_code = Callback::new(move |_| {
        // Don't toggle inline code if we're in a code block
        if current_code_block().is_none() {
            toggle_style("code");
        }
    });

    let code_block = Callback::new(move |_| {
        // If we're already in a code block, exit it
        if let Some((pre, _code)) = current_code_block() {
            exit_code_block(&pre);
            show_language_picker.set(false);
        } else {
            insert_code_block();
            show_language_picker.set(true);
        }
        update_button_states();
    });

    // Insert image callback
    let insert_image = Callback::new(move |_| {
        if let Some(input) = file_input_ref.get() as Option<HtmlInputElement> {
            let _ = input.click();
        }
    });

    // Handle file selection
    let on_file_change = move |_ev: ev::Event| {
        let Some(file_input) = file_input_ref.get() as Option<HtmlInputElement> else {
            return;
        };
        let Some(files) = file_input.files() else {
            return;
        };

        for i in 0..files.length() {
            let Some(file) = files.item(i) else {
                continue;
            };
            let editor_ref = editor_ref.clone();
            spawn_local(async move {
                if let Some(url) = on_image_insert.run(file).await {
                    insert_image_at_cursor(&url, &editor_ref);
                }
            });
        }
    };

    // Add this effect to position the cursor
    Effect::new(move |_| {
        if let Some(editor) = editor_ref.get() as Option<HtmlDivElement> {
            if let Some(doc) = window().and_then(|w| w.document()) {
                if let Ok(Some(selection)) = doc.get_selection() {
                    if let Some(p) = editor.first_element_child() {
                        if let Ok(range) = doc.create_range() {
                            range.set_start(&p, 0).ok();
                            range.set_end(&p, 0).ok();
                            selection.remove_all_ranges().ok();
                            selection.add_range(&range).ok();
                        };
                    }
                }
            }
        }
    });

    let apply_heading = move |ev: ev::Event| {
        let tag = event_target_value(&ev);
        if let Some(editor) = editor_ref.get() as Option<HtmlDivElement> {
            if let Some(doc) = window().and_then(|w| w.document()) {
                if let Ok(Some(selection)) = doc.get_selection() {
                    if let Ok(range) = selection.get_range_at(0) {
                        if let Ok(mut node) = range.start_container() {
                            let editor_node: &Node = editor.as_ref();
                            loop {
                                if let Some(el) = node.dyn_ref::<Element>() {
                                    if let Some(parent) = el.parent_element() {
                                        if parent.is_same_node(Some(editor_node)) {
                                            if ["p", "h1", "h2", "h3", "h4", "h5", "h6"]
                                                .contains(&el.tag_name().to_lowercase().as_str())
                                            {
                                                // let new_el =
                                                //     doc.create_element(&tag).unwrap_or_default();
                                                if let (Ok(new_el), Ok(new_range)) =
                                                    (doc.create_element(&tag), doc.create_range())
                                                {
                                                    while let Some(child) = el.first_child() {
                                                        new_el.append_child(&child).ok();
                                                    }
                                                    parent.replace_child(&new_el, el).ok();
                                                    new_range.select_node_contents(&new_el).ok();
                                                    new_range.collapse();
                                                    selection.remove_all_ranges().ok();
                                                    selection.add_range(&new_range).ok();
                                                    break;
                                                };
                                            }
                                        }
                                    }
                                }
                                match node.parent_node() {
                                    Some(p) => node = p,
                                    None => break,
                                }
                            }
                        };
                    }
                }
            }
        }
    };

    let apply_language = move |ev: ev::Event| {
        let lang = event_target_value(&ev);
        if let Some((_pre, code)) = current_code_block() {
            code.set_attribute("class", &format!("language-{}", lang))
                .ok();
        }
        show_language_picker.set(false);
    };

    let handle_on_input = move |_: ev::Event| {
        if let Some(editor) = editor_ref.get() as Option<HtmlDivElement> {
            set_tracked_content.set(editor.inner_html());
        }
    };

    Effect::new(move |_| {
        let changed_value = initial_content.get();
        set_tracked_content.set(changed_value);
    });

    let upload_md = Callback::new(move |_| {
        if let Some(input) = md_file_input_ref.get() as Option<HtmlInputElement> {
            let _ = input.click();
        }
    });

    let on_md_file_change = move |_ev: ev::Event| {
        if let Some(file_input) = md_file_input_ref.get() as Option<HtmlInputElement> {
            if let Some(files) = file_input.files() {
                if let Some(file) = files.item(0) {
                    spawn_local(async move {
                        match gloo_file::futures::read_as_text(&file.into()).await {
                            Ok(markdown_content) => {
                                // Parse markdown to HTML using markdown crate
                                // let html_output = markdown::to_html(&markdown_content);
                                if let Ok(html_output) = markdown::to_html_with_options(
                                    &markdown_content,
                                    &markdown::Options::gfm(),
                                ) {
                                    // Also update the initial content
                                    initial_content.set(html_output);
                                };
                            }
                            Err(err) => {
                                leptos::logging::error!("Failed to read markdown file: {:?}", err);
                            }
                        }
                    });
                }
            }
        }
    };

    // Ordered list
    let ordered_list = Callback::new(move |_| {
        if let Some(editor) = editor_ref.get() {
            if let Some((list, _)) = current_list_item() {
                // If we're in a list, exit it
                exit_list(&editor, &list);
            } else {
                // Otherwise, insert a new ordered list
                insert_list(&editor, "ol");
            }
        }
        update_button_states();
    });

    // Unordered list
    let unordered_list = Callback::new(move |_| {
        if let Some(editor) = editor_ref.get() {
            if let Some((list, _)) = current_list_item() {
                // If we're in a list, exit it
                exit_list(&editor, &list);
            } else {
                // Otherwise, insert a new unordered list
                insert_list(&editor, "ul");
            }
        }
        update_button_states();
    });

    view! {
        <div class="border-[1px] border-light-gray rounded-[5px]">
            // Toolbar
            <div class="flex gap-2 items-center flex-wrap border-b-[1px] border-light-gray p-[10px]">
                {
                    extra_formating_options.contains(&ExtraFormatingOption::Heading).then(|| view!{
                        <SelectInput initial_value="p" id_attr="font-sizes" options=font_options on:change=apply_heading />
                    })
                }
                <BasicButton
                    icon=Some(FiBold)
                    icon_before=true
                    onclick=bold
                    style_ext=bold_style
                />
                <BasicButton
                    icon=Some(BsTypeItalic)
                    icon_before=true
                    onclick=italic
                    style_ext=italic_style
                />
                <BasicButton
                    icon=Some(BsTypeUnderline)
                    icon_before=true
                    onclick=underline
                    style_ext=underline_style
                />
                <BasicButton
                    icon=Some(BiStrikethroughRegular)
                    icon_before=true
                    onclick=strikethrough
                    style_ext=strikethrough_style
                />
                {
                    extra_formating_options.contains(&ExtraFormatingOption::ImageUpload).then(|| view!{
                        <BasicButton icon=Some(BsImage) icon_before=true onclick=insert_image style_ext="hover:bg-light-gray" />
                    })
                }
                {
                    extra_formating_options.contains(&ExtraFormatingOption::InlineCode).then(|| view!{
                        <BasicButton
                            icon=Some(BsCode)
                            onclick=inline_code
                            style_ext=inline_code_style
                        />
                    })
                }
                {
                    extra_formating_options.contains(&ExtraFormatingOption::CodeBlock).then(|| view!{
                        <BasicButton
                            icon=Some(BsBraces)
                            onclick=code_block
                            style_ext=code_block_style
                        />

                        <Show when=move || show_language_picker.get()>
                            <div class="ml-2">
                                <SelectInput initial_value="plaintext" id_attr="code-language" options=language_options on:change=apply_language />
                            </div>
                        </Show>
                    })
                }
                {
                    extra_formating_options.contains(&ExtraFormatingOption::MarkdownUpload).then(|| view!{
                        <BasicButton
                            icon=Some(VsMarkdown)
                            icon_before=true
                            onclick=upload_md
                            style_ext="hover:bg-light-gray"
                        />
                    })
                }


                {
                    extra_formating_options.contains(&ExtraFormatingOption::Lists).then(|| view!{
                        <BasicButton
                            icon=Some(BsListOl)
                            icon_before=true
                            onclick=ordered_list
                            style_ext=ordered_list_style
                        />
                        <BasicButton
                            icon=Some(BsListUl)
                            icon_before=true
                            onclick=unordered_list
                            style_ext=unordered_list_style
                        />
                    })
                }
            </div>

            // Editor
            <div
                node_ref=editor_ref
                contenteditable="true"
                on:keydown=on_keydown
                on:click=on_selection_change
                on:keyup=on_keyup
                class="min-h-[200px] max-h-[45svh] overflow-y-auto p-3 prose focus:rounded-b-none outline-secondary"
                inner_html=move || initial_content.get()
                on:input=handle_on_input
            />
            <InputField field_type=InputFieldType::File input_node_ref=file_input_ref accept="image/*" on:change=on_file_change ext_input_styles="hidden" id_attr=format!("{}-file-input", id_attr) />


            <Textarea id_attr=format!("{}-text-input", id_attr) ext_input_styles="hidden" initial_value=tracked_content name=name />

            <InputField
                field_type=InputFieldType::File
                input_node_ref=md_file_input_ref
                accept=".md,.markdown"
                on:change=on_md_file_change
                ext_input_styles="hidden"
                id_attr=format!("{}-md-file-input", id_attr)
            />
        </div>
    }
}

fn cursor_inside(tag: &str) -> Option<Element> {
    let doc = window()?.document()?;
    let selection = doc.get_selection().ok()??;
    let range = selection.get_range_at(0).ok()?;

    let container = range.start_container().ok()?;

    let mut node = if let Some(text) = container.dyn_ref::<web_sys::Text>() {
        text.parent_element()?
    } else if let Some(el) = container.dyn_ref::<Element>() {
        el.clone()
    } else {
        return None;
    };

    loop {
        if node.tag_name().eq_ignore_ascii_case(tag) {
            return Some(node);
        }
        match node.parent_element() {
            Some(p) => node = p,
            None => break,
        }
    }

    None
}

fn exit_code_block(pre: &Element) {
    if let (Some(doc),) = (window().and_then(|w| w.document()),) {
        if let (Ok(p), Ok(br), Ok(new_range), Ok(Some(sel))) = (
            doc.create_element("p"),
            doc.create_element("br"),
            doc.create_range(),
            doc.get_selection(),
        ) {
            p.append_child(&br).ok();
            pre.after_with_node_1(&p).ok();
            new_range.set_start(&p, 0).ok();
            new_range.set_end(&p, 0).ok();
            sel.remove_all_ranges().ok();
            sel.add_range(&new_range).ok();
        }
    }
}

fn insert_code_block() {
    if let Some(doc) = window().and_then(|w| w.document()) {
        if let (Ok(Some(selection)), Ok(pre), Ok(code), Ok(new_range)) = (
            doc.get_selection(),
            doc.create_element("pre"),
            doc.create_element("code"),
            doc.create_range(),
        ) {
            if let Ok(range) = selection.get_range_at(0) {
                let text = doc.create_text_node("\n");
                pre.set_attribute("data-block", "code").ok();
                code.set_attribute("class", "language-plaintext").ok();
                code.append_child(&text).ok();
                pre.append_child(&code).ok();
                range.delete_contents().ok();
                range.insert_node(&pre).ok();
                new_range.set_start(&text, 1).ok();
                new_range.set_end(&text, 1).ok();
                selection.remove_all_ranges().ok();
                selection.add_range(&new_range).ok();
            }
        }
    }
}

fn current_code_block() -> Option<(web_sys::Element, web_sys::Element)> {
    let doc = window()?.document()?;
    let sel = doc.get_selection().ok()??;
    let range = sel.get_range_at(0).ok()?;

    let mut node = range.start_container().ok()?;

    loop {
        if let Some(code) = node.dyn_ref::<Element>() {
            if code.tag_name().eq_ignore_ascii_case("code") {
                if let Some(pre) = code.parent_element() {
                    if pre.tag_name().eq_ignore_ascii_case("pre") {
                        return Some((pre, code.clone()));
                    }
                }
            }
        }
        node = node.parent_node()?;
    }
}

fn is_current_line_empty(_code: &Element) -> bool {
    let doc = match window().and_then(|w| w.document()) {
        Some(d) => d,
        None => return false,
    };

    let sel = match doc.get_selection().ok().flatten() {
        Some(s) if s.range_count() > 0 => s,
        _ => return false,
    };

    let range = match sel.get_range_at(0) {
        Ok(r) => r,
        Err(_) => return false,
    };

    let container = match range.start_container() {
        Ok(n) => n,
        Err(_) => return false,
    };

    if container.node_type() != Node::TEXT_NODE {
        return false;
    }

    let text_node: web_sys::Text = container.unchecked_into();
    let value = text_node.data();

    let offset = range.start_offset().unwrap_or(0) as usize;
    let offset = offset.min(value.len());

    let before = &value[..offset];
    let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
    let line = &before[line_start..];

    line.trim().is_empty()
}

fn handle_code_enter(pre: &Element, code: &Element, last_enter_empty: &RwSignal<bool>) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };

    let Ok(Some(sel)) = doc.get_selection() else {
        return;
    };

    if sel.range_count() == 0 {
        return;
    }

    let Ok(range) = sel.get_range_at(0) else {
        return;
    };

    let Ok(container) = range.start_container() else {
        return;
    };

    if !code.contains(Some(&container)) {
        return;
    }

    let empty = is_current_line_empty(code);

    // -------- Empty line logic --------
    if empty {
        if last_enter_empty.get() {
            // Second empty line -> exit code block
            last_enter_empty.set(false);

            if let (Ok(p), Ok(br), Ok(new_range)) = (
                doc.create_element("p"),
                doc.create_element("br"),
                doc.create_range(),
            ) {
                p.append_child(&br).ok();

                // Append paragraph AFTER the code block
                pre.after_with_node_1(&p).ok();

                // Move cursor into the new paragraph
                new_range.set_start(&p, 0).ok();
                new_range.set_end(&p, 0).ok();

                sel.remove_all_ranges().ok();
                sel.add_range(&new_range).ok();
            }

            return;
        } else {
            // Allow first empty line
            last_enter_empty.set(true);
        }
    } else {
        // Reset state if line has content
        last_enter_empty.set(false);
    }

    // Delete selection if not collapsed
    if !range.collapsed() {
        range.delete_contents().ok();
    }

    match container.node_type() {
        Node::TEXT_NODE => {
            let text_node: web_sys::Text = container.unchecked_into();

            let Ok(offset) = range.start_offset() else {
                return;
            };

            let offset = offset as usize;
            let value = text_node.data();
            let offset = offset.min(value.len());

            let (before, after) = value.split_at(offset);

            let new_data = if after.is_empty() {
                format!("{before}\n\u{200B}")
            } else {
                format!("{before}\n{after}")
            };

            text_node.set_data(&new_data);

            if let Ok(new_range) = doc.create_range() {
                new_range.set_start(&text_node, (offset + 1) as u32).ok();
                new_range.set_end(&text_node, (offset + 1) as u32).ok();

                sel.remove_all_ranges().ok();
                sel.add_range(&new_range).ok();
            }
        }

        Node::ELEMENT_NODE => {
            let new_text = doc.create_text_node("\n\u{200B}");
            range.insert_node(&new_text).ok();

            if let Ok(new_range) = doc.create_range() {
                new_range.set_start(&new_text, 1).ok();
                new_range.set_end(&new_text, 1).ok();

                sel.remove_all_ranges().ok();
                sel.add_range(&new_range).ok();
            }
        }

        _ => {}
    }
}

fn current_inline_code() -> Option<Element> {
    let doc = window()?.document()?;
    let sel = doc.get_selection().ok()??;

    if sel.range_count() == 0 {
        return None;
    }

    let range = sel.get_range_at(0).ok()?;
    let container = range.start_container().ok()?;

    let element = match container.node_type() {
        Node::ELEMENT_NODE => container.unchecked_into::<Element>(),
        _ => container.parent_element()?,
    };

    let code = element.closest("code").ok()??;

    // Reject <pre><code>
    if code.closest("pre").ok()?.is_some() {
        return None;
    }

    Some(code)
}

fn handle_inline_code_enter(code: &Element) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };

    let Ok(Some(sel)) = doc.get_selection() else {
        return;
    };

    let Some(parent_p) = code.closest("p").ok().flatten() else {
        return;
    };

    let Ok(p) = doc.create_element("p") else {
        return;
    };

    let Ok(br) = doc.create_element("br") else {
        return;
    };

    p.append_child(&br).ok();

    parent_p.after_with_node_1(&p).ok();

    if let Ok(range) = doc.create_range() {
        range.set_start(&p, 0).ok();
        range.set_end(&p, 0).ok();

        sel.remove_all_ranges().ok();
        sel.add_range(&range).ok();
    }
}

fn insert_list(editor: &HtmlDivElement, list_type: &str) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };
    let Ok(Some(selection)) = doc.get_selection() else {
        return;
    };
    let Ok(range) = selection.get_range_at(0) else {
        return;
    };
    let Ok(list) = doc.create_element(list_type) else {
        return;
    };

    let has_selection = !range.collapsed();

    if has_selection {
        if let Ok(contents) = range.clone_contents() {
            let mut has_blocks = false;
            let mut current_child = contents.first_child();

            while let Some(node) = current_child {
                if let Some(el) = node.dyn_ref::<Element>() {
                    let tag = el.tag_name().to_lowercase();
                    if ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div"].contains(&tag.as_str()) {
                        has_blocks = true;
                        break;
                    }
                }
                current_child = node.next_sibling();
            }

            if has_blocks {
                current_child = contents.first_child();

                while let Some(node) = current_child {
                    let next = node.next_sibling();

                    if let Some(el) = node.dyn_ref::<Element>() {
                        let tag = el.tag_name().to_lowercase();
                        if ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div"].contains(&tag.as_str())
                        {
                            if let Ok(li) = doc.create_element("li") {
                                while let Some(child) = el.first_child() {
                                    li.append_child(&child).ok();
                                }
                                if li.first_child().is_none() {
                                    if let Ok(br) = doc.create_element("br") {
                                        li.append_child(&br).ok();
                                    }
                                }
                                list.append_child(&li).ok();
                            }
                        }
                    } else if node.node_type() == 3 {
                        if let Some(text) = node.text_content() {
                            if !text.trim().is_empty() {
                                if let (Ok(li), Ok(cloned)) =
                                    (doc.create_element("li"), node.clone_node())
                                {
                                    li.append_child(&cloned).ok();
                                    list.append_child(&li).ok();
                                }
                            }
                        }
                    }

                    current_child = next;
                }
            } else {
                if let Ok(li) = doc.create_element("li") {
                    li.append_child(&contents).ok();
                    list.append_child(&li).ok();
                }
            }
        }
    } else {
        if let (Ok(li), Ok(br)) = (doc.create_element("li"), doc.create_element("br")) {
            li.append_child(&br).ok();
            list.append_child(&li).ok();
        }
    }

    let Ok(mut node) = range.start_container() else {
        return;
    };
    let editor_as_node: &web_sys::Node = editor.as_ref();

    loop {
        if let Some(el) = node.dyn_ref::<web_sys::Element>() {
            let tag = el.tag_name().to_lowercase();
            if ["p", "h1", "h2", "h3", "h4", "h5", "h6"].contains(&tag.as_str()) {
                if let Some(parent) = el.parent_element() {
                    if parent.is_same_node(Some(editor_as_node)) {
                        el.after_with_node_1(&list).ok();
                        if let (Some(first_li), Ok(new_range)) =
                            (list.first_element_child(), doc.create_range())
                        {
                            new_range.select_node_contents(&first_li).ok();
                            new_range.collapse_with_to_start(false);
                            selection.remove_all_ranges().ok();
                            selection.add_range(&new_range).ok();
                        }
                        return;
                    }
                }
            }
        }
        match node.parent_node() {
            Some(parent) => node = parent,
            None => break,
        }
    }

    // Fallback: append to editor if no block found
    editor.append_child(&list).ok();
    if let (Some(first_li), Ok(new_range)) = (list.first_element_child(), doc.create_range()) {
        new_range.select_node_contents(&first_li).ok();
        new_range.collapse_with_to_start(false);
        selection.remove_all_ranges().ok();
        selection.add_range(&new_range).ok();
    }
}

fn current_list_item() -> Option<(web_sys::Element, web_sys::Element)> {
    let doc = window()?.document()?;
    let sel = doc.get_selection().ok()??;
    let range = sel.get_range_at(0).ok()?;

    let mut node = range.start_container().ok()?;

    loop {
        if let Some(li) = node.dyn_ref::<Element>() {
            if li.tag_name().eq_ignore_ascii_case("li") {
                if let Some(list) = li.parent_element() {
                    let tag = list.tag_name().to_lowercase();
                    if tag == "ol" || tag == "ul" {
                        return Some((list, li.clone()));
                    }
                }
            }
        }
        node = node.parent_node()?;
    }
}

fn handle_list_enter(list: &Element, li: &Element) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };
    let Ok(Some(sel)) = doc.get_selection() else {
        return;
    };

    let is_empty = li
        .text_content()
        .map(|t| t.trim().is_empty())
        .unwrap_or(true);

    if is_empty {
        if let (Ok(p), Ok(br), Ok(new_range)) = (
            doc.create_element("p"),
            doc.create_element("br"),
            doc.create_range(),
        ) {
            p.append_child(&br).ok();
            list.after_with_node_1(&p).ok();
            li.remove();
            if list.children().length() == 0 {
                list.remove();
            }
            new_range.set_start(&p, 0).ok();
            new_range.set_end(&p, 0).ok();
            sel.remove_all_ranges().ok();
            sel.add_range(&new_range).ok();
        }
    } else {
        if let (Ok(new_li), Ok(br), Ok(new_range)) = (
            doc.create_element("li"),
            doc.create_element("br"),
            doc.create_range(),
        ) {
            new_li.append_child(&br).ok();
            match li.next_sibling() {
                Some(next_sibling) => {
                    list.insert_before(&new_li, Some(&next_sibling)).ok();
                }
                None => {
                    list.append_child(&new_li).ok();
                }
            }
            new_range.set_start(&new_li, 0).ok();
            new_range.set_end(&new_li, 0).ok();
            sel.remove_all_ranges().ok();
            sel.add_range(&new_range).ok();
        }
    }
}

fn exit_list(_editor: &HtmlDivElement, list: &Element) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };
    let Ok(Some(sel)) = doc.get_selection() else {
        return;
    };

    if let (Ok(p), Ok(br), Ok(new_range)) = (
        doc.create_element("p"),
        doc.create_element("br"),
        doc.create_range(),
    ) {
        p.append_child(&br).ok();
        list.after_with_node_1(&p).ok();
        new_range.set_start(&p, 0).ok();
        new_range.set_end(&p, 0).ok();
        sel.remove_all_ranges().ok();
        sel.add_range(&new_range).ok();
    }
}

fn insert_image_at_cursor(src: &str, editor_ref: &NodeRef<Div>) {
    let Some(doc) = window().and_then(|w| w.document()) else {
        return;
    };
    let Ok(Some(selection)) = doc.get_selection() else {
        return;
    };
    let Ok(range) = selection.get_range_at(0) else {
        return;
    };
    let Ok(img) = doc.create_element("img") else {
        return;
    };

    img.set_attribute("src", src).unwrap_or_default();
    img.set_attribute("style", "max-width: 100%; height: auto;")
        .unwrap_or_default();

    range.delete_contents().ok();
    range.insert_node(&img).ok();

    if let Ok(new_range) = doc.create_range() {
        new_range.set_start_after(&img).ok();
        new_range.set_end_after(&img).ok();
        selection.remove_all_ranges().ok();
        selection.add_range(&new_range).ok();
    }

    if let Some(editor) = editor_ref.get_untracked() as Option<HtmlDivElement> {
        fire_bubbled_and_cancelable_event("input", true, true, &editor);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ExtraFormatingOption

    #[test]
    fn extra_formatting_option_eq() {
        assert_eq!(
            ExtraFormatingOption::CodeBlock,
            ExtraFormatingOption::CodeBlock
        );
        assert_ne!(
            ExtraFormatingOption::CodeBlock,
            ExtraFormatingOption::InlineCode
        );
    }

    #[test]
    fn extra_formatting_option_clone() {
        let opt = ExtraFormatingOption::Lists;
        assert_eq!(opt.clone(), ExtraFormatingOption::Lists);
    }

    #[test]
    fn extra_formatting_option_hash() {
        use std::collections::HashSet;
        let set: HashSet<ExtraFormatingOption> = [
            ExtraFormatingOption::Heading,
            ExtraFormatingOption::Lists,
            ExtraFormatingOption::Heading, // duplicate
        ]
        .into_iter()
        .collect();
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn toolbar_contains_check_positive() {
        let opts = vec![
            ExtraFormatingOption::Heading,
            ExtraFormatingOption::CodeBlock,
        ];
        assert!(opts.contains(&ExtraFormatingOption::Heading));
        assert!(opts.contains(&ExtraFormatingOption::CodeBlock));
    }

    #[test]
    fn toolbar_contains_check_negative() {
        let opts = vec![ExtraFormatingOption::Heading];
        assert!(!opts.contains(&ExtraFormatingOption::Lists));
        assert!(!opts.contains(&ExtraFormatingOption::ImageUpload));
        assert!(!opts.contains(&ExtraFormatingOption::InlineCode));
        assert!(!opts.contains(&ExtraFormatingOption::MarkdownUpload));
    }

    #[test]
    fn empty_extra_options_contains_nothing() {
        let opts: Vec<ExtraFormatingOption> = vec![];
        assert!(!opts.contains(&ExtraFormatingOption::CodeBlock));
    }

    // Active style logic

    fn active_style(is_active: bool) -> &'static str {
        if is_active {
            "bg-primary text-contrast-white"
        } else {
            "hover:bg-light-gray"
        }
    }

    #[test]
    fn active_style_when_true() {
        assert_eq!(active_style(true), "bg-primary text-contrast-white");
    }

    #[test]
    fn active_style_when_false() {
        assert_eq!(active_style(false), "hover:bg-light-gray");
    }

    // Reactive active style

    #[test]
    fn active_style_signal_reflects_state() {
        let owner = Owner::new();
        owner.with(|| {
            let is_bold = RwSignal::new(false);
            assert_eq!(active_style(is_bold.get()), "hover:bg-light-gray");

            is_bold.set(true);
            assert_eq!(
                active_style(is_bold.get()),
                "bg-primary text-contrast-white"
            );
        });
    }

    // is_current_line_empty logic

    fn line_before_cursor(text: &str, offset: usize) -> &str {
        let before = &text[..offset.min(text.len())];
        let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
        &before[line_start..]
    }

    #[test]
    fn empty_line_detected_at_start() {
        assert!(line_before_cursor("", 0).trim().is_empty());
    }

    #[test]
    fn empty_line_detected_after_newline() {
        assert!(line_before_cursor("code\n", 5).trim().is_empty());
    }

    #[test]
    fn non_empty_line_not_detected_as_empty() {
        assert!(!line_before_cursor("let x = 1;", 10).trim().is_empty());
    }

    #[test]
    fn line_before_cursor_splits_on_last_newline() {
        let text = "line1\nline2\n";
        assert_eq!(line_before_cursor(text, 11), "line2");
    }

    // handle_list_enter empty-li logic

    fn li_is_empty(text_content: Option<&str>) -> bool {
        text_content.map(|t| t.trim().is_empty()).unwrap_or(true)
    }

    #[test]
    fn empty_li_exits_list() {
        assert!(li_is_empty(Some("")));
        assert!(li_is_empty(Some("   ")));
        assert!(li_is_empty(None));
    }

    #[test]
    fn non_empty_li_appends_new_item() {
        assert!(!li_is_empty(Some("some text")));
    }

    // last_enter_empty double-enter logic

    #[test]
    fn first_empty_enter_sets_flag() {
        let owner = Owner::new();
        owner.with(|| {
            let last_enter_empty = RwSignal::new(false);
            // simulate first empty-line enter
            last_enter_empty.set(true);
            assert!(last_enter_empty.get());
        });
    }

    #[test]
    fn second_empty_enter_exits_and_resets_flag() {
        let owner = Owner::new();
        owner.with(|| {
            let last_enter_empty = RwSignal::new(true);
            // simulate second empty-line enter
            last_enter_empty.set(false);
            assert!(!last_enter_empty.get());
        });
    }

    #[test]
    fn non_empty_line_resets_flag() {
        let owner = Owner::new();
        owner.with(|| {
            let last_enter_empty = RwSignal::new(true);
            last_enter_empty.set(false);
            assert!(!last_enter_empty.get());
        });
    }
}