tinymce / tinymce-react

Offical TinyMCE React component
MIT License
938 stars 151 forks source link

Get empty content from onInit func when setting value asynchronously #361

Closed Bulala-liu closed 1 year ago

Bulala-liu commented 2 years ago

why is it closed?

Because the question was answered. There is nothing I can change in the integration to "fix" this problem. You simply have to use the tools that the integration provides to solve it.

Is it a way to distinguish which triggers the onEditorChange between onInit and user input?

Simply query the editor state onInit as in the example I gave above. The editor will normalise the input given before the onInit happens so you have access to the normalised version which you can use as the unchanged reference to detect changes caused by user input.

Hi, I still have a problem. When the value is settled asynchronously in the upper form, onInit function return empty content. Here's a demo. https://codesandbox.io/s/tinymce-react-editor-controlled-dirty-with-normalisation-forked-e33h0v?file=/src/index.js

Originally posted by @Bulala-liu in https://github.com/tinymce/tinymce-react/issues/298#issuecomment-1182845919

exalate-issue-sync[bot] commented 2 years ago

Ref: INT-2946

tiny-james commented 1 year ago

I had not anticipated that problem, thanks for the issue!

Here is the solution I came up with: https://codesandbox.io/s/getting-normlised-content-for-async-setup-ztwp60

It uses the fact that when the initialValue is set it resets the undo levels which means the previous undo level is undefined.

<Editor
        onAddUndo={(evt, editor) => {
          if (evt.lastLevel === undefined) {
            setNormalisedContent(editor.getContent());
          }
        }}
/>

I plan to create an event prop for this situation so doing this is less annoying. I will probably call it onInitialValue.

Mario-Eis commented 1 year ago

I dont get it. How is this supposed to work with formik or another form library? Passing the form state to initialValue sets the cursor position to the beginning of the line, while typing. There is no such thing as an initialValue when using a form. The form could be reset or changed all the time. Thats why it is called "controlled"...