Debian Codegraph @

addons/html_editor/static/src/main/link/link_selection_plugin.js

raw
1 import { Plugin } from "@html_editor/plugin";
2 import { closestElement, selectElements } from "@html_editor/utils/dom_traversal";
3 import { removeClass } from "@html_editor/utils/dom";
4 import { isProtected, isProtecting } from "@html_editor/utils/dom_info";
5
6 /*
7 This plugin solves selection issues around links (allowing the cursor at the
8 inner and outer edges of links).
9
10 Every link receives 4 zero-width non-breaking spaces (unicode FEFF
11 characters, hereafter referred to as ZWNBSP):
12 - one before the link
13 - one as the link's first child
14 - one as the link's last child
15 - one after the link
16 like so: `//ZWNBSP//<a>//ZWNBSP//label//ZWNBSP//</a>//ZWNBSP`.
17
18 A visual indication ( `o_link_in_selection` class) is added to a link when
19 the selection is contained within it.
20
21 This is not applied in the following cases:
22
23 - in a navbar (since its links are managed via the snippets system, not
24 via pure edition) and, similarly, in .nav-link links
25 - in links that have content more complex than simple text
26 - on non-editable links or links that are not within the editable area
27 */
28
29 /**
30 * @typedef { Object } LinkSelectionShared
31 * @property { LinkSelectionPlugin['padLinkWithZwnbsp'] } padLinkWithZwnbsp
32 */
33
34 /**
35 * @typedef {((link: HTMLLinkElement) => boolean)[]} ineligible_link_for_selection_indication_predicates
36 * @typedef {((link: HTMLLinkElement) => boolean)[]} ineligible_link_for_zwnbsp_predicates
37 */
38
39 export class LinkSelectionPlugin extends Plugin {
40 static id = "linkSelection";
41 static dependencies = ["selection", "feff"];
42 // TODO ABD: refactor to handle Knowledge comments inside this plugin without sharing padLinkWithZwnbsp.
43 static shared = ["padLinkWithZwnbsp"];
44 /** @type {import("plugins").EditorResources} */
45 resources = {
46 /** Handlers */
47 selectionchange_handlers: this.resetLinkInSelection.bind(this),
48 clean_for_save_handlers: ({ root }) => this.clearLinkInSelectionClass(root),
49 normalize_handlers: () => this.resetLinkInSelection(),
50 feff_providers: this.addFeffsToLinks.bind(this),
51 system_classes: ["o_link_in_selection"],
52 selection_placeholder_container_predicates: (container) => {
53 if (container.nodeName === "BUTTON" || container.nodeName === "A") {
54 // We sometimes have buttons or links that are blocks with
55 // contenteditable=true but we never want to insert a paragraph
56 // in them.
57 // Note: this can be removed if `allowsParagraphRelatedElements`
58 // is adapted to return false in these cases.
59 return false;
60 }
61 },
62 };
63
64 addFeffsToLinks(root, cursors) {
65 return [...selectElements(root, "a")]
66 .filter(this.isLinkEligibleForZwnbsp.bind(this))
67 .flatMap((link) => this.dependencies.feff.surroundWithFeffs(link, cursors));
68 }
69
70 /**
71 * Take a link and pad it with non-break zero-width spaces to ensure that it
72 * is always possible to place the cursor at its inner and outer edges.
73 *
74 * @param {HTMLAnchorElement} link
75 */
76 padLinkWithZwnbsp(link) {
77 const cursors = this.dependencies.selection.preserveSelection();
78 this.dependencies.feff.surroundWithFeffs(link, cursors);
79 cursors.restore();
80 }
81
82 isLinkEligibleForZwnbsp(link) {
83 const { anchorNode, focusNode } = this.dependencies.selection.getEditableSelection();
84 // we can't rely on `o_link_in_selection` class because it can be
85 // added to siblings while splitting link element.
86 const isLinkSelected = link.contains(anchorNode) || link.contains(focusNode);
87 const linkHasContentOrSelected =
88 isLinkSelected || link.textContent.replaceAll("\ufeff", "");
89 return (
90 linkHasContentOrSelected &&
91 link.isContentEditable &&
92 link.parentElement.isContentEditable &&
93 this.editable.contains(link) &&
94 !isProtected(link) &&
95 !isProtecting(link) &&
96 !this.getResource("ineligible_link_for_zwnbsp_predicates").some((p) => p(link))
97 );
98 }
99
100 isLinkEligibleForVisualIndication(link) {
101 return (
102 this.isLinkEligibleForZwnbsp(link) &&
103 !this.getResource("ineligible_link_for_selection_indication_predicates").some(
104 (predicate) => predicate(link)
105 )
106 );
107 }
108
109 /**
110 * Apply the o_link_in_selection class if the selection is in a single link,
111 * remove it otherwise.
112 *
113 * @param {SelectionData} [selectionData]
114 */
115 resetLinkInSelection(selectionData = this.dependencies.selection.getSelectionData()) {
116 this.clearLinkInSelectionClass(this.editable);
117
118 const { anchorNode, focusNode } = selectionData.editableSelection;
119 const [anchorLink, focusLink] = [anchorNode, focusNode].map((node) =>
120 closestElement(node, "a")
121 );
122 const singleLinkInSelection = anchorLink === focusLink && anchorLink;
123
124 if (
125 singleLinkInSelection &&
126 this.isLinkEligibleForVisualIndication(singleLinkInSelection) &&
127 this.document.activeElement === this.editable
128 ) {
129 singleLinkInSelection.classList.add("o_link_in_selection");
130 }
131 }
132
133 clearLinkInSelectionClass(root) {
134 for (const link of selectElements(root, ".o_link_in_selection")) {
135 removeClass(link, "o_link_in_selection");
136 }
137 }
138 }