[FIX] web_editor: join the peer to peer network on focus

With the new 16.0 feature that open any form in edit mode, the html
editor was trying to connect in collaboration each time a record was
opened in a form view that has a collaborative html field.
The peer to peer connection could trigger a lot of rpc because of it's
communication through the bus.

This commit reduces the number of rpc by only trying to connect to
the collaboration network when the user focus the html editor.

task-3151577

closes odoo/odoo#111247

X-original-commit: 9d7aecec811699199e270a5096f00d5cb6f0b15d
Signed-off-by: Antoine Guenet (age) <age@odoo.com>
Co-authored-by: Nicolas Bayet <nby@odoo.com>
Co-authored-by: Antoine Guenet <age@odoo.com>
This commit is contained in:
Nicolas Bayet
2023-01-27 19:56:24 +01:00
committed by Antoine Guenet
co-authored by Antoine Guenet
parent 0b8d4c0eb1
commit 27c275568f
@@ -96,6 +96,7 @@ const Wysiwyg = Widget.extend({
this.tooltipTimeouts = [];
Wysiwyg.activeWysiwygs.add(this);
this._oNotEditableObservers = new Map();
this._joinPeerToPeer = this._joinPeerToPeer.bind(this);
},
/**
*
@@ -138,6 +139,8 @@ const Wysiwyg = Widget.extend({
this.getSession()['notification_type']
) {
editorCollaborationOptions = this.setupCollaboration(options.collaborationChannel);
// Wait until editor is focused to join the peer to peer network.
this.$editable[0].addEventListener('focus', this._joinPeerToPeer);
}
const getYoutubeVideoElement = async (url) => {
@@ -236,12 +239,6 @@ const Wysiwyg = Widget.extend({
this.$root = this.$root || $wrapwrap;
}
if (this._peerToPeerLoading) {
// Now that the editor is loaded, wait for the peerToPeer to be
// ready to join.
this._peerToPeerLoading.then(() => this.ptp.notifyAllClients('ptp_join'));
}
this.$editable.on('click', '[data-oe-field][data-oe-sanitize-prevent-edition]', () => {
Dialog.alert(this, _t("Someone with escalated rights previously modified this area, you are therefore not able to modify it yourself."));
});
@@ -2515,6 +2512,9 @@ const Wysiwyg = Widget.extend({
dialog.open({shouldFocusButtons:true});
this.resetEditor(dbRecord.body);
// We were in a peer to peer session before the conflict, join
// it again immediately.
this._joinPeerToPeer();
}
this.preSavePromiseResolve();
resetPreSavePromise();
@@ -2531,6 +2531,12 @@ const Wysiwyg = Widget.extend({
this.ptp && this.ptp.stop();
this._collaborationStopBus && this._collaborationStopBus();
},
_joinPeerToPeer: function () {
this.$editable[0].removeEventListener('focus', this._joinPeerToPeer);
if (this._peerToPeerLoading) {
this._peerToPeerLoading.then(() => this.ptp.notifyAllClients('ptp_join'));
}
},
resetEditor: function (value, options) {
if (options) {
this.options = this._getEditorOptions(options);
@@ -2550,7 +2556,8 @@ const Wysiwyg = Widget.extend({
this.odooEditor.collaborationSetClientId(this._currentClientId);
this.setValue(value);
this.odooEditor.historyReset();
this.ptp.notifyAllClients('ptp_join');
// Wait until editor is focused to join the peer to peer network.
this.$editable[0].addEventListener('focus', this._joinPeerToPeer);
},
/**
* Set contenteditable=false for all `.o_not_editable` found within node if