steven-tey / novel

Notion-style WYSIWYG editor with AI-powered autocompletion.
https://novel.sh
Apache License 2.0
11.8k stars 980 forks source link

feat: Easy way to reference the editor outside the EditorContent #388

Open Nazariy995 opened 2 months ago

Nazariy995 commented 2 months ago

Describe the feature you'd like to request

I am new to this library, so this could be a dumb suggestion.

I am trying to reference editor command outside the EditorContent components provided by Noel

because I want to be able to update text context programmatically outside.

Right now I am doing it like this

        onCreate={({ editor }) => {
          updateEditor(editor);
        }}
        onUpdate={({ editor }) => {
          console.log("Editor JSON:", editor.getJSON());
          updateEditor(editor);
          updateDocumentValue(editor.getJSON());
        }}

Is there a better way to do this! Maybe we can add a Context component to wrap everything!

Describe the solution you'd like to see

I'm thinking we can have access to the ContextProvider if possible so we can place it anywhere and just use

import { useEditor } from  "novel"

Additional information

No response

ishan28mkip commented 1 month ago
const Container = () => {
const [editor, saveEditor] = useState(null)
useEffect(() => {
  if(editor){
    // editor is available here for commands
  }
},[editor])
return <Editor saveEditor={saveEditor}/>
}
onCreate={({ editor }) => {
  saveEditor(editor);
}}
MODSetter commented 1 month ago

How would a hook work if you use more than 1 instance of editor. Novel updates content in localstorage and reads from it. Just manipulate the content there.