From 27c275568f0ee42975d8d402a186da2b1ef22044 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Thu, 26 Jan 2023 13:29:33 +0000 Subject: [PATCH] [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) Co-authored-by: Nicolas Bayet Co-authored-by: Antoine Guenet --- .../static/src/js/wysiwyg/wysiwyg.js | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index 2b8cab95d8d..e758e3b0cb2 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -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