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

Error: Editor's content can not be saved in read-only mode #1761

Open
lxtadhamnawito opened this issue Aug 23, 2021 · 7 comments
Open

Error: Editor's content can not be saved in read-only mode #1761

lxtadhamnawito opened this issue Aug 23, 2021 · 7 comments

Comments

@lxtadhamnawito
Copy link

lxtadhamnawito commented Aug 23, 2021

I'm using EditorJS as my rich text editor, but I'm using readOnly option, as I don't want the user to edit what is written unless he press on a specific button.
So whenever the user start to edit it and try to save it, I got this Error: Editor's content can not be saved in read-only mode

read-only option is mentioned in the documentation here, but it doesn't mention how to use it !
So Am I using read-only wrongly, or it should be set in other way?
If yes, how to enable read-only mode for tools.

This is what I'm using:

import Header from '@editorjs/header';
import Paragraph from '@editorjs/paragraph'

const RichGuidelinesEditor = ({ value, onChange, isReadOnly }) => {
const EDITOR_JS_TOOLS = {
    paragraph: {
        class: Paragraph,
        inlineToolbar: true,
        readOnly: true,
    },
    header: {
        class: Header,
        readOnly: true,
    },
}

const handleOnChange = e => { e.saver.save().then(res => { onChange(res.blocks); }

return(
        <EditorJs
            data={{ blocks: value }}
            onChange={handleOnChange}
            tools={EDITOR_JS_TOOLS}
            placeholder="Enter your guidelines"
            readOnly={isReadOnly}

            enableReInitialize={true}
            onCompareBlocks={(...args) => args[0]}
        />
)
}
export default RichGuidelinesEditor;

Browser: Chrome.
OS: Windows 10.

Editor.js version: Latest version.

Plugins: "react-editor-js"

@neSpecc neSpecc removed the bug label Aug 23, 2021
@neSpecc
Copy link
Member

neSpecc commented Aug 23, 2021

It is the correct behavior. Editor in the read-only mode does not allow saving, and throws an error when you programmatically tries to save it.

@lxtadhamnawito
Copy link
Author

@neSpecc So what should I do to allow it save data, and at the same time allow read-only for the Editor itself (as I toggle it when the editor is OnEdit mode read-only = false).
Or what I'm saying above is not doable at all?

@neSpecc
Copy link
Member

neSpecc commented Aug 23, 2021

I can't understand what you want to achieve. Editor in read-only just shows passed data. You can't change it. So what do you want to save if you already have data that you pass to the editor?

@lxtadhamnawito
Copy link
Author

@neSpecc I want to save the data if the user edit it. As I can press edit button for example which sets Editor's "readOnly=false" so it become editable and add some headers and paragraphs then press save.

So I have 2 cases:
1- readOnly = true --> where I display the data only that I have.
2- readOnly = false --> (on pressing edit) where some data are added/deleted and save them back again.

@MakarovCode
Copy link

I had the same need, I forked the library and did a modification to the next file: src/components/modules/api/saver.ts

import { Saver } from '../../../../types/api';
import { OutputData } from '../../../../types';
import * as _ from '../../utils';
import Module from '../../__module';

/**
 * @class SaverAPI
 * provides with methods to save data
 */
export default class SaverAPI extends Module {
  /**
   * Available methods
   *
   * @returns {Saver}
   */
  public get methods(): Saver {
    return {
      save: (force=false): Promise<OutputData> => this.save(force),
    };
  }

  /**
   * Return Editor's data
   *
   * @returns {OutputData}
   */
  public save(force=false): Promise<OutputData> {
    const errorText = `Editor\'s content can not be saved in read-only mode -> ${force}`;

    if (force == false && this.Editor.ReadOnly.isEnabled) {
      _.logLabeled(errorText, 'warn');

      return Promise.reject(new Error(errorText));
    }

    return this.Editor.Saver.save();
  }
}

I basically added a (force=false) to the getter method so you can do:

this.editor.save(true) // For forcing the saving even in readonly mode
//Or
this.editor.save() // To keep current validation

@gohabereg gohabereg added this to Needs triage in Issues triage Mar 21, 2022
@h0lme3
Copy link

h0lme3 commented Jul 1, 2022

You can control boolean value of readOnly. Pass props for it

@norchai
Copy link

norchai commented Oct 25, 2023

Chiming in: I also need to be able to save while in read-only mode: basically for the purposes of programmatically converting HTML from another source into editor blocks JSON for various purposes. renderFromHTML() also errors in read-only mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Issues triage
Needs triage
Development

No branches or pull requests

5 participants