]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/richtext.js
Merge remote-tracking branch 'upstream/pull/5667'
[rails.git] / app / assets / javascripts / richtext.js
1 (function () {
2   /*
3    * When the text in an edit pane is changed, clear the contents of
4    * the associated preview pne so that it will be regenerated when
5    * the user next switches to it.
6    */
7   $(document).on("change", ".richtext_container textarea", function () {
8     const container = $(this).closest(".richtext_container");
9     const preview = container.find(".tab-pane[id$='_preview']");
10
11     preview.children(".richtext_placeholder").attr("hidden", true).removeClass("delayed-fade-in");
12     preview.children(".richtext").empty();
13   });
14
15   /*
16    * Install a handler to set the minimum preview pane height
17    * when switching away from an edit pane
18    */
19   $(document).on("hide.bs.tab", ".richtext_container button[data-bs-target$='_edit']", function () {
20     const container = $(this).closest(".richtext_container");
21     const editor = container.find("textarea");
22     const preview = container.find(".tab-pane[id$='_preview']");
23     const minHeight = editor.outerHeight() - preview.outerHeight() + preview.height();
24
25     preview.css("min-height", minHeight + "px");
26   });
27
28   /*
29    * Install a handler to switch to preview mode
30    */
31   $(document).on("show.bs.tab", ".richtext_container button[data-bs-target$='_preview']", function () {
32     const container = $(this).closest(".richtext_container");
33     const editor = container.find("textarea");
34     const preview = container.find(".tab-pane[id$='_preview']");
35
36     if (preview.children(".richtext").contents().length === 0) {
37       preview.children(".richtext_placeholder").removeAttr("hidden").addClass("delayed-fade-in");
38
39       fetch(editor.data("previewUrl"), {
40         method: "POST",
41         body: new URLSearchParams({ text: editor.val(), ...OSM.csrf })
42       })
43         .then(r => r.text())
44         .then(html => {
45           preview.children(".richtext").html(html);
46           preview.children(".richtext_placeholder").attr("hidden", true).removeClass("delayed-fade-in");
47         });
48     }
49   });
50
51   $(window).on("resize", updateHelp);
52
53   $(document).on("turbo:load", function () {
54     $(".richtext_container textarea").on("invalid", invalidTextareaListener);
55     updateHelp();
56   });
57
58   function invalidTextareaListener() {
59     const container = $(this).closest(".richtext_container");
60
61     container.find("button[data-bs-target$='_edit']").tab("show");
62   }
63
64   function updateHelp() {
65     $(".richtext_container .richtext_help_sidebar:not(:visible):not(:empty)").each(function () {
66       const container = $(this).closest(".richtext_container");
67       $(this).children().appendTo(container.find(".tab-pane[id$='_help']"));
68     });
69     $(".richtext_container .richtext_help_sidebar:visible:empty").each(function () {
70       const container = $(this).closest(".richtext_container");
71       container.find(".tab-pane[id$='_help']").children().appendTo($(this));
72       if (container.find("button[data-bs-target$='_help'].active").length) {
73         container.find("button[data-bs-target$='_edit']").tab("show");
74       }
75     });
76   }
77 }());