|
| 1 | +import { Plugin } from "@html_editor/plugin"; |
| 2 | +import { _t } from "@web/core/l10n/translation"; |
| 3 | +import { registry } from "@web/core/registry"; |
| 4 | +import { ScrollButtonOption } from "./scroll_button_option"; |
| 5 | +import { classAction } from "@html_builder/core/core_builder_action_plugin"; |
| 6 | + |
| 7 | +class ScrollButtonOptionPlugin extends Plugin { |
| 8 | + static id = "scrollButtonOption"; |
| 9 | + resources = { |
| 10 | + builder_options: [ |
| 11 | + { |
| 12 | + OptionComponent: ScrollButtonOption, |
| 13 | + selector: "section", |
| 14 | + exclude: |
| 15 | + "[data-snippet] :not(.oe_structure) > [data-snippet],.s_instagram_page,.o_mega_menu > section,.s_appointments .s_dynamic_snippet_content", |
| 16 | + }, |
| 17 | + ], |
| 18 | + builder_actions: { |
| 19 | + addScrollButton: { |
| 20 | + isApplied: ({ editingElement }) => |
| 21 | + !!editingElement.querySelector(":scope > .o_scroll_button"), |
| 22 | + apply: ({ editingElement }) => { |
| 23 | + let button = this.buttonCache.get(editingElement); |
| 24 | + if (!button) { |
| 25 | + const anchor = document.createElement("a"); |
| 26 | + anchor.classList.add( |
| 27 | + "o_scroll_button", |
| 28 | + "mb-3", |
| 29 | + "rounded-circle", |
| 30 | + "align-items-center", |
| 31 | + "justify-content-center", |
| 32 | + "mx-auto", |
| 33 | + "bg-primary", |
| 34 | + "o_not_editable" |
| 35 | + ); |
| 36 | + anchor.href = "#"; |
| 37 | + anchor.contentEditable = "false"; |
| 38 | + anchor.title = _t("Scroll down to next section"); |
| 39 | + const arrow = document.createElement("i"); |
| 40 | + arrow.classList.add("fa", "fa-angle-down", "fa-3x"); |
| 41 | + anchor.appendChild(arrow); |
| 42 | + button = anchor; |
| 43 | + this.buttonCache.set(editingElement, button); |
| 44 | + } |
| 45 | + editingElement.appendChild(button); |
| 46 | + }, |
| 47 | + clean: this.removeButton.bind(this), |
| 48 | + }, |
| 49 | + scrollButtonSectionHeightClassAction: { |
| 50 | + ...classAction, |
| 51 | + apply: (args) => { |
| 52 | + classAction.apply(args); |
| 53 | + const { |
| 54 | + editingElement, |
| 55 | + param: { mainParam }, |
| 56 | + } = args; |
| 57 | + // If a "d-lg-block" class exists on the section (e.g., for |
| 58 | + // mobile visibility option), it should be replaced with a |
| 59 | + // "d-lg-flex" class. This ensures that the section has the |
| 60 | + // "display: flex" property applied, which is the default |
| 61 | + // rule for both "height" option classes. |
| 62 | + if (mainParam) { |
| 63 | + editingElement.classList.replace("d-lg-block", "d-lg-flex"); |
| 64 | + } else if (editingElement.classList.contains("d-lg-flex")) { |
| 65 | + // There are no known cases, but we still make sure that |
| 66 | + // the <section> element doesn't have a "display: flex" |
| 67 | + // originally. |
| 68 | + editingElement.classList.remove("d-lg-flex"); |
| 69 | + const sectionStyle = window.getComputedStyle(editingElement); |
| 70 | + const hasDisplayFlex = sectionStyle.getPropertyValue("display") === "flex"; |
| 71 | + editingElement.classList.add(hasDisplayFlex ? "d-lg-flex" : "d-lg-block"); |
| 72 | + } |
| 73 | + }, |
| 74 | + clean: (args) => { |
| 75 | + classAction.clean(args); |
| 76 | + if (args.param.mainParam === "o_full_screen_height") { |
| 77 | + this.removeButton(args); |
| 78 | + } |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + setup() { |
| 85 | + this.buttonCache = new Map(); |
| 86 | + } |
| 87 | + |
| 88 | + removeButton({ editingElement }) { |
| 89 | + const button = editingElement.querySelector(":scope > .o_scroll_button"); |
| 90 | + if (button) { |
| 91 | + button.remove(); |
| 92 | + this.buttonCache.set(editingElement, button); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +registry.category("website-plugins").add(ScrollButtonOptionPlugin.id, ScrollButtonOptionPlugin); |
0 commit comments