Issue with Content Insertion at the Cursor Position in Jodit Editor React

I’m using the Jodit editor in my React project and have encountered a problem with content insertion at the cursor position. When I first click in the editor and insert content, it is added at the correct cursor position. However, subsequent content additions are inserted at the beginning of the editor instead of the cursor’s current position. Only if I click out and back into the editor does the insertion happen at the correct position again.

I am working with nextjs 14.0.3 and
“jodit”: “^4.2.27”,
“jodit-react”: “^4.1.2”,

Here is the relevant part of my code:

  useEffect(() => {
      if (newData && jodit) {
        const tempDiv = document.createElement("div");
        tempDiv.innerHTML = newData;
        const newNode = tempDiv.firstChild;
        if (newNode) {
          jodit.selection.insertNode(newNode as HTMLElement);
        }
      }
    }, [newData, jodit]);

And here the whole code (component).

import React, {
  useEffect,
  useMemo,
  useRef,
  useState,
  forwardRef,
  useImperativeHandle,
} from "react";
import dynamic from "next/dynamic";
import { Jodit } from "jodit";
import { addBorderToTdElements } from "@/utils/tablePostProcessing";

const JoditEditor = dynamic(() => import("jodit-react"), {
  ssr: false,
});

interface Props {
  data?: any;
  extContent?: string;
  setExtContent?: (a: string) => void;
  setTextEditorValue: Function;
  setJoditCallback?: (a: Jodit | undefined) => void;
  disabled?: boolean;
  reset: string | null;
  width?: number;
  font?: string;
  newData?: string;
}

const JoditTextEditor = forwardRef(
  (
    {
      data,
      extContent,
      setExtContent,
      setTextEditorValue,
      setJoditCallback,
      disabled,
      reset,
      width,
      font,
      newData,
    }: Props,
    ref
  ) => {
    const [jodit, setJodit] = useState<Jodit>();
    const [content, setContent] = useState(data);

    useEffect(() => {
      setContent(data);
    }, [data]);

    useEffect(() => {
      setJoditCallback && setJoditCallback(jodit);
    }, [jodit]);

    useEffect(() => {
      if (reset !== null) {
        setContent(reset);
      }
    }, [reset]);

    useImperativeHandle(ref, () => ({
      insertText: (text: string) => {
        if (jodit) {
          jodit.selection.insertHTML(text);
        }
      },
    }));

    const config: any = useMemo(
      () => ({
        statusbar: false,
        tabIndex: 1,
        buttons: ["bold", "italic", "underline", "strikethrough", "align", "indent", "outdent", "ul", "ol", "table", "|", "hr", "eraser", "|", "font", "fontsize", "paragraph", "lineHeight", "brush", "spellcheck", "symbols", "|", "undo", "redo", "image"],
        toolbarAdaptive: false,
        removeButtons: ["fullsize", "preview", "file", "video", "print", "find", "copy", "paste", "selectAll", "link"],
        showTooltip: true,
        showTooltipDelay: 0,
        toolbarSticky: true,
        minHeight: 400,
        style: {
          padding: "5px 58px",
          font: `16px ${!font ? "Times New Roman" : `${font}`}, serif`,
        },
        readonly: !!disabled,
        uploader: {
          insertImageAsBase64URI: true,
        },
        events: {
          afterInit: (editor: Jodit) => {
            setJodit(editor);
          },
        },
        askBeforePasteFromWord: false,
        askBeforePasteHTML: false,
        controls: {
          fontsize: {
            list: {
              "10": "6",
              "12": "8",
              "14": "10",
              "16": "12",
              "18": "14",
              "20": "16",
              "22": "18",
              "24": "20",
              "26": "22",
              "28": "24",
              "30": "26",
              "32": "28",
              "34": "30",
              "36": "32",
              "38": "34",
            },
          },
        },
        disablePlugins: ["add-new-line"],
      }),
      [disabled]
    );

    useEffect(() => {
      if (newData && jodit) {
        const tempDiv = document.createElement("div");
        tempDiv.innerHTML = newData;
        const newNode = tempDiv.firstChild;
        if (newNode) {
          jodit.selection.insertNode(newNode as HTMLElement);
        }
      }
    }, [newData, jodit]);

    return (
      <div className={`shadow ${width ? `w-[${width}px]` : "w-[720px]"}`} style={{ borderRadius: "10px" }}>
        <JoditEditor
          value={extContent ?? content}
          config={config}
          onBlur={(newContent) => {
            newContent = addBorderToTdElements(newContent);
            setExtContent ? setExtContent(newContent) : setContent(newContent);
          }}
          onChange={(newContent) => {
            newContent = addBorderToTdElements(newContent);
            setTextEditorValue(newContent);
          }}
        />
      </div>
    );
  }
);

export default JoditTextEditor;

I’m using the Jodit editor in my React project and have encountered a problem with content insertion at the cursor position. When I first click in the editor and insert content, it is added at the correct cursor position. However, subsequent content additions are inserted at the beginning of the editor instead of the cursor’s current position. Only if I click out and back into the editor does the insertion happen at the correct position again.

New contributor

BongoJo97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật