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

With switching between two editors toolbar save state after link (action) opened to add/edit #378

Closed
demo-igor opened this issue May 18, 2022 · 2 comments

Comments

@demo-igor
Copy link

-- Bug Report --

Expected Behavior

Toolbar should reset state between switching editor

Current Behavior

Toolbar saves state between switching editor

Steps to Reproduce

I added two editors to the one page. And found that during editing if toolbar link (action) opened and than I click to text on second editor toolbar show me previous state (link is opened to edit, state from editor 1)

Code Example

export class RichEditor extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      editor_actions: editorAvailableActions(props.customTools),
      editor_state: editorStateFromHTML(props.rawHtml)
    };
    this.key_bindings = [
      {
        name: 'noop',
        isKeyBound: e => e.keyCode === 85 && e.metaKey,
        action: noop
      }
    ];
    this.callback = debounce(html => {
      this.props.handleChange(html);
    }, 100);
  }

  componentDidMount() {
    if (this.props.auto_focus) {
      this._editor.focus();
    }
  }

  componentWillReceiveProps(nextProps) {
    const editorHtml = converter.stateToHTML(
      this.state.editor_state.getCurrentContent()
    );
    const { rawHtml } = nextProps;
    const isRawHtmlEmpty = rawHtml === '' || rawHtml === emptyHtml;
    const isEditorHtmlEmpty = editorHtml === emptyHtml;

    if ((isRawHtmlEmpty && isEditorHtmlEmpty) || rawHtml === editorHtml) {
      return;
    }
    this.setState({
      editor_state: editorStateFromHTML(nextProps.rawHtml)
    });
  }

  handleChange = editorState => {
    const currentEditorContent = editorState.getCurrentContent();
    const editorHasText = currentEditorContent.hasText();
    const html = editorHasText
      ? converter.stateToHTML(currentEditorContent)
      : '';
    this.setState(
      {
        editor_state: editorState
      },
      () => {
        const editorHtml = converter.stateToHTML(
          this.state.editor_state.getCurrentContent()
        );

        if (this.props.rawHtml !== editorHtml) {
          this.callback(html);
        }
      }
    );
  };

  render() {
    const {
      editor_actions: editorActions,
      editor_state: editorState
    } = this.state;
    const { classes, disabled } = this.props;
    const html = converter.stateToHTML(editorState.getCurrentContent());

    return disabled ? (
      <div
        className={classes.disabled}
        dangerouslySetInnerHTML={{ __html: html }}
        data-test="disabledContent"
      />
    ) : (
      <div
        data-test="enabledContent"
        onClick={() => {
          this._editor.focus();
        }}
        role="presentation"
      >
        <MegadraftEditor
          actions={editorActions}
          editorState={editorState}
          entityInputs={entityInputs}
          keyBindings={[...this.key_bindings, ...this.props.key_bindings]}
          onChange={this.handleChange}
          placeholder={this.props.placeholder}
          ref={editor => {
            this._editor = editor;
          }}
          sidebarRendererFn={noop}
        />
      </div>
    );
  }
}
screencast-2022-05-18.20-14-11.mp4
@demo-igor
Copy link
Author

@marcelometal this bug not reproduced on release 0.5.2 but can be reproduced for highest versions including latest one. I also checked on simpler implementation for MegadraftEditor.

@lucianapepelucio
Copy link
Contributor

This was completed in #427

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