TinyMCE React cursor back to start after typing a key
Asked Answered
B

5

15

I'm using TinyMCE-React and when I type my text in the TinyMCE Editor with an initial value, the cursor constantly returns at the start of the text...

import { Editor } from "@tinymce/tinymce-react";

  const [formData, setFormData] = useState({
    title: "",
    text: "",
  });


if (post) {
  setFormData((formData) => ({
    ...formData,
    title: post.title,
    text: post.text,
  }));
}

const { title, text } = formData;

My function :

const textChange = (e) => {
setFormData({ ...formData.text, text: e });   };

My Editor :

    <Editor
      name='text'
      initialValue={text}
      onEditorChange={(e) => textChange(e)}
    />

I think it's because of the "setFormData" but I don't know how can I edit the text with a regular cursor which stays at the end of the text...

Buffo answered 29/7, 2021 at 9:49 Comment(1)
You may have more luck getting this answered if you can create a working minimal example as testing this code is difficult in it's current state. stackoverflow.com/help/minimal-reproducible-exampleCarpeting
S
22

Late answer, but here's the fix: https://github.com/tinymce/tinymce-react/issues/267.

Change initialValue={text} to value={text}.

This is the right setup for using the TinyMCE React component as a "controlled" component: https://www.tiny.cloud/docs/integrations/react/#usingthetinymcereactcomponentasacontrolledcomponent.

Saskatchewan answered 30/9, 2021 at 19:25 Comment(0)
S
0

For me it worked when I had two separate variables for initial value and for the editor on change. Ref: https://github.com/instructure-react/react-tinymce/issues/76

Summons answered 8/4, 2022 at 14:38 Comment(0)
S
0

For me it worked with newValue props

<Editor
      apiKey="jeiltvbooyoyr3fe3xt511l45gkbcj9wrq1pj5hhpfrsqmua"
      onInit={(evt, editor) => (editorRef.current = editor)}
      newValue={content}
      onChange={() => setContent(editorRef.current.getContent())}
      automatic_uploads={false}
      init={{
        height: 500,
        menubar: true,
        branding: false,
        images_upload_url: `${API_BASE_URL}/file/storage/upload`,
        plugins: [
          "advlist",
          "autolink",
          "lists",
          "link",
          "image",
          "charmap",
          "preview",
          "anchor",
          "searchreplace",
          "visualblocks",
          "code",
          "fullscreen",
          "insertdatetime",
          "media",
          "table",
          "code",
          "help",
          "wordcount",
        ],
        toolbar:
          "undo redo | blocks | " +
          "bold italic forecolor | alignleft aligncenter " +
          "alignright alignjustify | bullist numlist outdent indent | " +
          "removeformat | help",
        content_style:
          "body { font-family:Helvetica,Arial,sans-serif; font-size:14px }",
      }}
    />
Stoup answered 28/10, 2023 at 3:23 Comment(0)
B
0

I found a solution that is you need to set onInit function when Tinymce load then this function hits and value will load in initialValue which is starting value when Tinymce loaded.

interface ITinyMCE {
 value: string
  setValue: (val: any) => void
}

export default function TinyMCE({ value, setValue }: ITinyMCE) {
  const [initialValue, setInitialValue] = useState<string>('')

  return (
    <Editor
      apiKey='api-key'
      onInit={(evt, editor) => {
        setInitialValue(value)
      }}
      initialValue={initialValue}
      value={value}
      init={{
        height: 500,
        plugins: [
          "advlist",
          "autolink",
          "lists",
          "link",
          "image",
          "charmap",
          "preview",
          "anchor",
          "searchreplace",
          "visualblocks",
          "code",
          "fullscreen",
          "insertdatetime",
          "media",
          "table",
          "code",
          "help",
          "wordcount",
        ],
        toolbar: "undo redo | styles | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code preview",
        images_upload_url: "http://localhost:3001/upload_article_images",
        automatic_uploads: true,
        images_reuse_filename: true,
        images_upload_handler: handleImageUpload
      }}
      onEditorChange={(content) => {
        setValue(content)
      }}
    />
  )
}
Bolero answered 12/2 at 19:57 Comment(0)
F
0

In my case:

onChange={(value) => setEditorState(value?.level?.content)}

To:

onEditorChange={(val) => setEditorState(val)}
Fanatic answered 11/4 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.