| 1 |
import { Plugin } from "@html_editor/plugin"; |
| 2 |
import { _t } from "@web/core/l10n/translation"; |
| 3 |
import { DynamicPlaceholderPopover } from "@web/views/fields/dynamic_placeholder_popover"; |
| 4 |
import { withSequence } from "@html_editor/utils/resource"; |
| 5 |
import { isHtmlContentSupported } from "@html_editor/core/selection_plugin"; |
| 6 |
|
| 7 |
/** |
| 8 |
* @typedef {Object} DynamicPlaceholderShared |
| 9 |
* @property {DynamicPlaceholderPlugin['updateDphDefaultModel']} updateDphDefaultModel |
| 10 |
*/ |
| 11 |
|
| 12 |
export class DynamicPlaceholderPlugin extends Plugin { |
| 13 |
static id = "dynamicPlaceholder"; |
| 14 |
static dependencies = ["overlay", "selection", "history", "dom"]; |
| 15 |
static shared = ["updateDphDefaultModel"]; |
| 16 |
/** @type {import("plugins").EditorResources} */ |
| 17 |
resources = { |
| 18 |
user_commands: [ |
| 19 |
{ |
| 20 |
id: "openDynamicPlaceholder", |
| 21 |
title: _t("Dynamic Placeholder"), |
| 22 |
description: _t("Insert a field"), |
| 23 |
icon: "fa-hashtag", |
| 24 |
run: (params = {}) => this.open(params.resModel || this.defaultResModel), |
| 25 |
isAvailable: isHtmlContentSupported, |
| 26 |
}, |
| 27 |
], |
| 28 |
powerbox_categories: withSequence(60, { |
| 29 |
id: "marketing_tools", |
| 30 |
name: _t("Marketing Tools"), |
| 31 |
}), |
| 32 |
powerbox_items: { |
| 33 |
categoryId: "marketing_tools", |
| 34 |
commandId: "openDynamicPlaceholder", |
| 35 |
}, |
| 36 |
power_buttons: { commandId: "openDynamicPlaceholder" }, |
| 37 |
}; |
| 38 |
setup() { |
| 39 |
this.defaultResModel = this.config.dynamicPlaceholderResModel; |
| 40 |
|
| 41 |
/** @type {import("@html_editor/core/overlay_plugin").Overlay} */ |
| 42 |
this.overlay = this.dependencies.overlay.createOverlay(DynamicPlaceholderPopover, { |
| 43 |
hasAutofocus: true, |
| 44 |
className: "popover", |
| 45 |
}); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param {string} resModel |
| 50 |
*/ |
| 51 |
updateDphDefaultModel(resModel) { |
| 52 |
this.defaultResModel = resModel; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param {string} resModel |
| 57 |
*/ |
| 58 |
open(resModel) { |
| 59 |
if (!resModel) { |
| 60 |
return this.services.notification.add( |
| 61 |
_t("You need to select a model before opening the dynamic placeholder selector."), |
| 62 |
{ type: "danger" } |
| 63 |
); |
| 64 |
} |
| 65 |
this.overlay.open({ |
| 66 |
props: { |
| 67 |
close: this.onClose.bind(this), |
| 68 |
validate: this.onValidate.bind(this), |
| 69 |
resModel: resModel, |
| 70 |
}, |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param {string} chain |
| 76 |
* @param {string} defaultValue |
| 77 |
* @param {string} fieldType |
| 78 |
*/ |
| 79 |
async onValidate(chain, defaultValue, fieldType) { |
| 80 |
if (!chain) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
const dynamicPlaceholder = |
| 85 |
fieldType === "datetime" |
| 86 |
? await this._onValidateDatetime(chain, defaultValue) |
| 87 |
: `object.${chain}`; |
| 88 |
|
| 89 |
const t = document.createElement("T"); |
| 90 |
t.setAttribute("t-out", dynamicPlaceholder); |
| 91 |
if (defaultValue?.length) { |
| 92 |
t.innerText = defaultValue; |
| 93 |
} |
| 94 |
|
| 95 |
this.dependencies.dom.insert(t); |
| 96 |
this.dependencies.history.addStep(); |
| 97 |
} |
| 98 |
|
| 99 |
async _onValidateDatetime(chain, defaultValue) { |
| 100 |
const partnerFields = await this.services.orm.call( |
| 101 |
`${this.defaultResModel}`, |
| 102 |
"mail_get_partner_fields", |
| 103 |
[[]] |
| 104 |
); |
| 105 |
|
| 106 |
let dynamicPlaceholder = partnerFields.length |
| 107 |
? `format_datetime(object.${chain}, tz=object.${partnerFields[0]}.tz)` |
| 108 |
: `format_datetime(object.${chain})`; |
| 109 |
|
| 110 |
if (defaultValue) { |
| 111 |
const safeDefaultValue = defaultValue.replace(/'/g, "\\'"); |
| 112 |
dynamicPlaceholder += ` or '${safeDefaultValue}'`; |
| 113 |
} |
| 114 |
|
| 115 |
return dynamicPlaceholder; |
| 116 |
} |
| 117 |
|
| 118 |
onClose() { |
| 119 |
this.overlay.close(); |
| 120 |
this.dependencies.selection.focusEditable(); |
| 121 |
} |
| 122 |
} |