Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: editorState.getImmutable is not a function #1447

Open
Akashmishra1209 opened this issue Sep 16, 2024 · 1 comment
Open

TypeError: editorState.getImmutable is not a function #1447

Akashmishra1209 opened this issue Sep 16, 2024 · 1 comment

Comments

@Akashmishra1209
Copy link

My Code Is :-
import React, { useState } from 'react'
import { Editor } from "react-draft-wysiwyg";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";

export default function BlogEditor() {
const [content, setcontent] = useState("

Welcome

")
return (

{/* <Editor
apiKey="cyamfx5u42wgqop0qpytq20dkluwcnngeefqe36iimrq76xj" // Replace with your TinyMCE API key
initialValue={content}
// onEditorChange={(e)=>setcontent(e.target)}
onChange={(e) => setcontent(e.target.value)}
init={{
selector: '#editor',
plugins: ' searchreplace autolink directionality visualblocks visualchars image link media codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help charmap linkchecker emoticons autosave',
toolbar: 'undo redo print spellcheckdialog formatpainter | blocks fontfamily fontsize | bold italic underline forecolor backcolor | link image | alignleft aligncenter alignright alignjustify',
height: '600px'
}}
/> */}
<Editor
editorState={content}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
onEditorStateChange={(e)=>{setcontent(e.target.value)}}
/>
<button className='btn btn-primary mt-2' onClick={()=>navigator.clipboard.writeText(content).then(alert("Copied To Clipboard"))}>Copy

)
}
Bur When I Run It Says TypeError: editorState.getImmutable is not a function

@vasanthmn1
Copy link

@Akashmishra1209
Initialize the editorState correctly.

import React, { useState } from 'react';
import { EditorState } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import { convertToRaw, ContentState } from 'draft-js';

export default function BlogEditor() {
  const [editorState, setEditorState] = useState(EditorState.createWithContent(ContentState.createFromText("Welcome")));

  const handleCopy = () => {
    const content = convertToRaw(editorState.getCurrentContent());
    const text = content.blocks.map(block => block.text).join('\n');
    navigator.clipboard.writeText(text).then(() => alert("Copied To Clipboard"));
  };

  return (
    <div>
      <Editor
        editorState={editorState}
        toolbarClassName="toolbarClassName"
        wrapperClassName="wrapperClassName"
        editorClassName="editorClassName"
        onEditorStateChange={setEditorState}
      />
      <button className='btn btn-primary mt-2' onClick={handleCopy}>Copy</button>
    </div>
  );
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants