| 1 |
import { Plugin } from "@html_editor/plugin"; |
| 2 |
import { closestElement, selectElements } from "@html_editor/utils/dom_traversal"; |
| 3 |
import { leftPos, rightPos } from "@html_editor/utils/position"; |
| 4 |
import { QWebPicker } from "./qweb_picker"; |
| 5 |
import { isElement } from "@html_editor/utils/dom_info"; |
| 6 |
import { withSequence } from "@html_editor/utils/resource"; |
| 7 |
|
| 8 |
const isUnsplittableQWebElement = (node) => |
| 9 |
isElement(node) && |
| 10 |
(node.tagName === "T" || |
| 11 |
[ |
| 12 |
"t-field", |
| 13 |
"t-if", |
| 14 |
"t-elif", |
| 15 |
"t-else", |
| 16 |
"t-foreach", |
| 17 |
"t-value", |
| 18 |
"t-esc", |
| 19 |
"t-out", |
| 20 |
"t-raw", |
| 21 |
].some((attr) => node.getAttribute(attr))); |
| 22 |
|
| 23 |
const PROTECTED_QWEB_SELECTOR = "[t-esc], [t-raw], [t-out], [t-field]"; |
| 24 |
const QWEB_DATA_ATTRIBUTES = [ |
| 25 |
"data-oe-t-group", |
| 26 |
"data-oe-t-inline", |
| 27 |
"data-oe-t-selectable", |
| 28 |
"data-oe-t-group-active", |
| 29 |
]; |
| 30 |
const dataAttributesSelector = QWEB_DATA_ATTRIBUTES.map((attr) => `[${attr}]`).join(", "); |
| 31 |
|
| 32 |
export const isUnremovableQWebElement = (node) => |
| 33 |
node.getAttribute?.("t-set") || node.getAttribute?.("t-call"); |
| 34 |
|
| 35 |
export class QWebPlugin extends Plugin { |
| 36 |
static id = "qweb"; |
| 37 |
static dependencies = ["overlay", "protectedNode", "selection"]; |
| 38 |
/** @type {import("plugins").EditorResources} */ |
| 39 |
resources = { |
| 40 |
/** Handlers */ |
| 41 |
selectionchange_handlers: withSequence(8, this.onSelectionChange.bind(this)), |
| 42 |
clean_for_save_handlers: ({ root }) => { |
| 43 |
this.clearDataAttributes(root); |
| 44 |
for (const element of root.querySelectorAll(PROTECTED_QWEB_SELECTOR)) { |
| 45 |
element.removeAttribute("contenteditable"); |
| 46 |
delete element.dataset.oeProtected; |
| 47 |
} |
| 48 |
}, |
| 49 |
normalize_handlers: withSequence(0, this.normalize.bind(this)), |
| 50 |
|
| 51 |
system_attributes: QWEB_DATA_ATTRIBUTES, |
| 52 |
unremovable_node_predicates: isUnremovableQWebElement, |
| 53 |
unsplittable_node_predicates: isUnsplittableQWebElement, |
| 54 |
clipboard_content_processors: this.clearDataAttributes.bind(this), |
| 55 |
legit_empty_link_predicates: (linkEl) => |
| 56 |
linkEl.getAttributeNames().some((name) => name.startsWith("t-")), |
| 57 |
}; |
| 58 |
|
| 59 |
setup() { |
| 60 |
this.editable.classList.add("odoo-editor-qweb"); |
| 61 |
this.picker = this.dependencies.overlay.createOverlay(QWebPicker, { |
| 62 |
positionOptions: { position: "top-start" }, |
| 63 |
}); |
| 64 |
this.addDomListener(this.editable, "click", this.onClick); |
| 65 |
this.groupIndex = 0; |
| 66 |
} |
| 67 |
|
| 68 |
isValidTargetForDomListener(ev) { |
| 69 |
if ( |
| 70 |
ev.type === "click" && |
| 71 |
ev.target && |
| 72 |
closestElement(ev.target, PROTECTED_QWEB_SELECTOR) |
| 73 |
) { |
| 74 |
// Allow clicking on a protected QWEB node to open the custom toolbar. |
| 75 |
return true; |
| 76 |
} |
| 77 |
return super.isValidTargetForDomListener(ev); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param { SelectionData } selectionData |
| 82 |
*/ |
| 83 |
onSelectionChange() { |
| 84 |
if (this.picker.isOpen) { |
| 85 |
this.picker.close(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
normalize(root) { |
| 90 |
this.normalizeInline(root); |
| 91 |
|
| 92 |
for (const element of selectElements(root, PROTECTED_QWEB_SELECTOR)) { |
| 93 |
this.dependencies.protectedNode.setProtectingNode(element, true); |
| 94 |
} |
| 95 |
this.applyGroupQwebBranching(root); |
| 96 |
} |
| 97 |
|
| 98 |
checkAllInline(el) { |
| 99 |
return [...el.children].every((child) => { |
| 100 |
if (child.tagName === "T") { |
| 101 |
return this.checkAllInline(child); |
| 102 |
} else { |
| 103 |
return ( |
| 104 |
child.nodeType !== Node.ELEMENT_NODE || |
| 105 |
this.window.getComputedStyle(child).display === "inline" |
| 106 |
); |
| 107 |
} |
| 108 |
}); |
| 109 |
} |
| 110 |
|
| 111 |
normalizeInline(root) { |
| 112 |
for (const el of selectElements(root, "t")) { |
| 113 |
if (this.checkAllInline(el)) { |
| 114 |
el.setAttribute("data-oe-t-inline", "true"); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
getNodeGroups(node) { |
| 120 |
const branchNode = node.closest("[data-oe-t-group]"); |
| 121 |
if (!branchNode) { |
| 122 |
return []; |
| 123 |
} |
| 124 |
const groupId = branchNode.getAttribute("data-oe-t-group"); |
| 125 |
const group = []; |
| 126 |
for (const node of branchNode.parentElement.querySelectorAll( |
| 127 |
`[data-oe-t-group='${groupId}']` |
| 128 |
)) { |
| 129 |
let label = ""; |
| 130 |
if (node.hasAttribute("t-if")) { |
| 131 |
label = `if: ${node.getAttribute("t-if")}`; |
| 132 |
} else if (node.hasAttribute("t-elif")) { |
| 133 |
label = `elif: ${node.getAttribute("t-elif")}`; |
| 134 |
} else if (node.hasAttribute("t-else")) { |
| 135 |
label = "else"; |
| 136 |
} |
| 137 |
group.push({ |
| 138 |
groupId, |
| 139 |
node, |
| 140 |
label, |
| 141 |
isActive: node.getAttribute("data-oe-t-group-active") === "true", |
| 142 |
}); |
| 143 |
} |
| 144 |
return this.getNodeGroups(branchNode.parentElement).concat([group]); |
| 145 |
} |
| 146 |
|
| 147 |
onClick(ev) { |
| 148 |
if (this.picker.isOpen) { |
| 149 |
this.picker.close(); |
| 150 |
} |
| 151 |
if (ev.detail > 1) { |
| 152 |
const selectionData = this.dependencies.selection.getSelectionData(); |
| 153 |
const selection = selectionData.documentSelection; |
| 154 |
const qwebNode = |
| 155 |
selection && |
| 156 |
selection.anchorNode && |
| 157 |
closestElement(selection.anchorNode, "[t-field],[t-esc],[t-out]"); |
| 158 |
if (qwebNode && this.editable.contains(qwebNode)) { |
| 159 |
// select the whole qweb node |
| 160 |
const [anchorNode, anchorOffset] = leftPos(qwebNode); |
| 161 |
const [focusNode, focusOffset] = rightPos(qwebNode); |
| 162 |
this.dependencies.selection.setSelection({ |
| 163 |
anchorNode, |
| 164 |
anchorOffset, |
| 165 |
focusNode, |
| 166 |
focusOffset, |
| 167 |
}); |
| 168 |
} |
| 169 |
} |
| 170 |
const targetNode = ev.target; |
| 171 |
if (targetNode.closest("[data-oe-t-group]")) { |
| 172 |
this.selectNode(targetNode); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
selectNode(node) { |
| 177 |
const editableSelection = this.dependencies.selection.getSelectionData().editableSelection; |
| 178 |
if (!editableSelection.isCollapsed) { |
| 179 |
return; |
| 180 |
} |
| 181 |
this.selectedNode = node; |
| 182 |
this.picker.open({ |
| 183 |
target: node, |
| 184 |
props: { |
| 185 |
groups: this.getNodeGroups(node), |
| 186 |
select: this.select.bind(this), |
| 187 |
}, |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
applyGroupQwebBranching(root) { |
| 192 |
const tNodes = selectElements(root, "[t-if], [t-elif], [t-else]"); |
| 193 |
const groupsEncounter = new Set(); |
| 194 |
for (const node of tNodes) { |
| 195 |
const prevNode = node.previousElementSibling; |
| 196 |
|
| 197 |
let groupId; |
| 198 |
if (prevNode && !node.hasAttribute("t-if")) { |
| 199 |
// Make the first t-if selectable, if prevNode is not a t-if, |
| 200 |
// it's already data-oe-t-selectable. |
| 201 |
prevNode.setAttribute("data-oe-t-selectable", "true"); |
| 202 |
groupId = parseInt(prevNode.getAttribute("data-oe-t-group")); |
| 203 |
node.setAttribute("data-oe-t-selectable", "true"); |
| 204 |
} else { |
| 205 |
groupId = this.groupIndex++; |
| 206 |
} |
| 207 |
groupsEncounter.add(groupId); |
| 208 |
node.setAttribute("data-oe-t-group", groupId); |
| 209 |
} |
| 210 |
for (const groupId of groupsEncounter) { |
| 211 |
const isOneElementActive = root.querySelector( |
| 212 |
`[data-oe-t-group='${groupId}'][data-oe-t-group-active]` |
| 213 |
); |
| 214 |
// If there is no element in groupId activated, activate the first |
| 215 |
// one. |
| 216 |
if (!isOneElementActive) { |
| 217 |
const firstElementToActivate = selectElements( |
| 218 |
root, |
| 219 |
`[data-oe-t-group='${groupId}']` |
| 220 |
).next().value; |
| 221 |
firstElementToActivate.setAttribute("data-oe-t-group-active", "true"); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
select(node) { |
| 227 |
const groupId = node.getAttribute("data-oe-t-group"); |
| 228 |
const activeElement = node.parentElement.querySelector( |
| 229 |
`[data-oe-t-group='${groupId}'][data-oe-t-group-active]` |
| 230 |
); |
| 231 |
if (activeElement === node) { |
| 232 |
return; |
| 233 |
} |
| 234 |
activeElement.removeAttribute("data-oe-t-group-active"); |
| 235 |
node.setAttribute("data-oe-t-group-active", "true"); |
| 236 |
this.selectedNode = node; |
| 237 |
this.picker.close(); |
| 238 |
this.selectNode(node); |
| 239 |
// Force Chrome to clear the selection. |
| 240 |
// Without this, Chrome's optimization may skip the 'selectionchange' event |
| 241 |
// if the new node is structurally identical to the previous one |
| 242 |
const selection = this.document.getSelection(); |
| 243 |
selection.removeAllRanges(); |
| 244 |
} |
| 245 |
|
| 246 |
clearDataAttributes(root) { |
| 247 |
for (const node of root.querySelectorAll(dataAttributesSelector)) { |
| 248 |
QWEB_DATA_ATTRIBUTES.forEach((attr) => node.removeAttribute(attr)); |
| 249 |
} |
| 250 |
} |
| 251 |
} |