From 5a87c9af8f959c163a334d1c9cda286460971053 Mon Sep 17 00:00:00 2001 From: Virgil B Date: Tue, 26 Jan 2021 11:49:31 +0200 Subject: [PATCH 1/3] Fixed Wysiwyg reversing text After adding a MD formatting block (b, i, u, etc.) the text would be reversed. This fixes that issue as well as automatically selecting the placeholder text when no selection is being made. --- .../admin/src/components/Wysiwyg/index.js | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js index 4c839d2f38..1083e4896a 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js @@ -136,6 +136,11 @@ class Wysiwyg extends React.Component { // Since we need to perform some operations in the reducer the loading phase stops before all the operations // are computed which in some case causes the inputs component to be initialised with a null value. if (!prevProps.value && this.props.value) { + // This is also called if the first thing you add in the editor is + // a markdown formatting block (b, i, u, etc.) which results in + // the selection being pushed to the end after the first character is added. + // Basically, setInitialValue is always called whenever + // you start typing in an empty editor (even after the initial load) this.setInitialValue(this.props); } } @@ -192,24 +197,31 @@ class Wysiwyg extends React.Component { textWithEntity, 'insert-character' ); - // Update the parent reducer - this.sendData(newEditorState); - // Don't handle selection : the user has selected some text to be changed with the appropriate markdown - if (selectedText !== '') { - return this.setState( - { - editorState: newEditorState, - }, - () => { - this.focus(); - } - ); + + if (selectedText.length === 0) { + this.setState({ + // Highlight the text if the selection was empty + editorState: EditorState.forceSelection(newEditorState, updatedSelection), + }, () => { + this.focus(); + // Update the parent reducer + this.sendData(newEditorState); + }); + return; } - return this.setState({ - // Highlight the text if the selection wad empty - editorState: EditorState.forceSelection(newEditorState, updatedSelection), - }); + // Don't handle selection: the user has selected some text to be changed with the appropriate markdown + this.setState( + { + editorState: newEditorState, + }, + () => { + this.focus(); + // Update the parent reducer + this.sendData(newEditorState); + } + ); + return; }; /** From f67ca1688bfaeff4b3b07760efd249afa555aa46 Mon Sep 17 00:00:00 2001 From: Virgil B Date: Tue, 26 Jan 2021 13:26:36 +0200 Subject: [PATCH 2/3] fix reverse text on code/quote blocks --- .../admin/src/components/Wysiwyg/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js index 1083e4896a..aa98b01e78 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js @@ -426,12 +426,15 @@ class Wysiwyg extends React.Component { newEditorState = EditorState.acceptSelection(newEditorState, updatedSelection); - // Update the parent reducer - this.sendData(newEditorState); + return this.setState({ editorState: EditorState.forceSelection(newEditorState, newEditorState.getSelection()), - }); + }, () => { + this.focus(); + // Update the parent reducer + this.sendData(newEditorState); + ); }; /** From 063c9b8ba5cb88d0850b87815143b8be030387b6 Mon Sep 17 00:00:00 2001 From: Virgil B Date: Tue, 26 Jan 2021 13:43:53 +0200 Subject: [PATCH 3/3] Fixed syntax error --- .../admin/src/components/Wysiwyg/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js index aa98b01e78..163a1ae0f7 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/Wysiwyg/index.js @@ -434,7 +434,7 @@ class Wysiwyg extends React.Component { this.focus(); // Update the parent reducer this.sendData(newEditorState); - ); + }); }; /**