Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Conversation

@jankdc
Copy link
Contributor

@jankdc jankdc commented Feb 6, 2019

Summary

If a content block was created with a key that contains the -, it would fail to decode the decoratorKey and the leafKey because the current implementation relies on - to be a parsing delimiter. Here's an example:

class SomeComponent extends React.Component {
  constructor(props) {
    super(props);

    const block = new ContentBlock({
      type: 'paragraph',
      text: 'some-text',
      key: 'some-key'
    });

    const contentState = ContentBlock.createFromBlockArray([block]);

    this.state = {editorState: EditorState.createWithContent(contentState)};

    this.onChange = editorState => this.setState({editorState});
  }

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
      />
    );
  }
}

When you select anywhere on the editor, you'd get this error:

    TypeError: Cannot read property 'getIn' of undefined

      45 |   const focusBlockKey = focusPath.blockKey;
      46 |   const focusLeaf = editorState
    > 47 |     .getBlockTree(focusBlockKey)
         |                              ^
      48 |     .getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
      49 |
      50 |   const anchorLeafStart: number = anchorLeaf.get('start');

      at getUpdatedSelectionState (src/component/selection/getUpdatedSelectionState.js:47:30)
      at getDraftEditorSelectionWithNodes (src/component/selection/getDraftEditorSelectionWithNodes.js:48:58)
      at getDraftEditorSelection (src/component/selection/getDraftEditorSelection.js:37:53)
      at assertGetDraftEditorSelection (src/component/selection/__tests__/getDraftEditorSelection-test.js:55:53)
      at Object.<anonymous> (src/component/selection/__tests__/getDraftEditorSelection-test.js:211:3)

Test Plan

Added a unit test for DraftOffsetKey.js to check for all delimiter cases.

@jankdc jankdc changed the title Fix wrong desctructuring when content block offset key has a - Fix bad destructuring when content block offset key has a - Feb 6, 2019
@jankdc jankdc changed the title Fix bad destructuring when content block offset key has a - Fix bad destructuring when content block key has a - Feb 6, 2019
@claudiopro claudiopro added the bug label Feb 6, 2019
@claudiopro
Copy link
Contributor

Good catch @jankdc, thanks for contributing this fix to Draft.js! I can't find an issue associated with this PR, I'm curious how did you encounter this problem?

Please take a look at my comments inline, I think we can make the fix slightly easier to reason about.

Copy link
Contributor

@claudiopro claudiopro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thanks for adding a test case to show the problem and its fix.

Please take a look at my suggested changes to make the code easier to reason about.

@jankdc
Copy link
Contributor Author

jankdc commented Feb 6, 2019

Oh sorry, I didn't really create an issue for it. We encountered this problem because we were manually mapping our tree data structure to a list of ContentBlocks , where each branch or leaf, had a GUID id, like so:

const data = {
  id: "0dca5e9d-2ebd-4574-921e-2b68793c9e8c",
  sections: [
    {
      id: "4b35e274-81e6-4eb5-8964-ef566517051d",
      text: "Some-text",
      meta: { ... },
      sections: [ ... ]
    }
  ]
}; 

function reduceTree(data): ContentBlock[] { ... }

The reason we're going this route is because we want to keep some metadata in the blocks instead of using convertFromHTML which strips it all away. Anyway, that's where we came across it.

@claudiopro
Copy link
Contributor

claudiopro commented Feb 7, 2019

Ah, that makes sense. Maybe we should make the separator token configurable. Or even better, allow arbitrary payload to be associated with blocks e.g. via data- attributes. Thanks again for contributing this fix!

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claudiopro has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@jankdc
Copy link
Contributor Author

jankdc commented Feb 7, 2019

Or even better, allow arbitrary payload to be associated with blocks e.g. via data- attributes.

Yeah, this would be a really handy feature.

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claudiopro has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@jankdc jankdc deleted the allow-hyphen-content-block-key branch February 7, 2019 11:13
@SeanSilke
Copy link

Hello when this fix will be released? Do you Have "release circle", there is near three months past from last release.

jdecked pushed a commit to twitter-forks/draft-js that referenced this pull request Oct 9, 2019
…ve#1995)

Summary:
**Summary**

If a content block was created with a key that contains the `-`, it would fail to decode the `decoratorKey` and the `leafKey` because the current implementation relies on `-` to be a parsing delimiter. Here's an example:

```js
class SomeComponent extends React.Component {
  constructor(props) {
    super(props);

    const block = new ContentBlock({
      type: 'paragraph',
      text: 'some-text',
      key: 'some-key'
    });

    const contentState = ContentBlock.createFromBlockArray([block]);

    this.state = {editorState: EditorState.createWithContent(contentState)};

    this.onChange = editorState => this.setState({editorState});
  }

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
      />
    );
  }
}
```

When you select anywhere on the editor, you'd get this error:

```
    TypeError: Cannot read property 'getIn' of undefined

      45 |   const focusBlockKey = focusPath.blockKey;
      46 |   const focusLeaf = editorState
    > 47 |     .getBlockTree(focusBlockKey)
         |                              ^
      48 |     .getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
      49 |
      50 |   const anchorLeafStart: number = anchorLeaf.get('start');

      at getUpdatedSelectionState (src/component/selection/getUpdatedSelectionState.js:47:30)
      at getDraftEditorSelectionWithNodes (src/component/selection/getDraftEditorSelectionWithNodes.js:48:58)
      at getDraftEditorSelection (src/component/selection/getDraftEditorSelection.js:37:53)
      at assertGetDraftEditorSelection (src/component/selection/__tests__/getDraftEditorSelection-test.js:55:53)
      at Object.<anonymous> (src/component/selection/__tests__/getDraftEditorSelection-test.js:211:3)
```

**Test Plan**

Added a unit test for `DraftOffsetKey.js` to check for all delimiter cases.
Pull Request resolved: facebookarchive#1995

Differential Revision: D13982004

fbshipit-source-id: 3cd5967ad86041e310c41e7bcbfff4e868062804
alicayan008 pushed a commit to alicayan008/draft-js that referenced this pull request Jul 4, 2023
Summary:
**Summary**

If a content block was created with a key that contains the `-`, it would fail to decode the `decoratorKey` and the `leafKey` because the current implementation relies on `-` to be a parsing delimiter. Here's an example:

```js
class SomeComponent extends React.Component {
  constructor(props) {
    super(props);

    const block = new ContentBlock({
      type: 'paragraph',
      text: 'some-text',
      key: 'some-key'
    });

    const contentState = ContentBlock.createFromBlockArray([block]);

    this.state = {editorState: EditorState.createWithContent(contentState)};

    this.onChange = editorState => this.setState({editorState});
  }

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
      />
    );
  }
}
```

When you select anywhere on the editor, you'd get this error:

```
    TypeError: Cannot read property 'getIn' of undefined

      45 |   const focusBlockKey = focusPath.blockKey;
      46 |   const focusLeaf = editorState
    > 47 |     .getBlockTree(focusBlockKey)
         |                              ^
      48 |     .getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
      49 |
      50 |   const anchorLeafStart: number = anchorLeaf.get('start');

      at getUpdatedSelectionState (src/component/selection/getUpdatedSelectionState.js:47:30)
      at getDraftEditorSelectionWithNodes (src/component/selection/getDraftEditorSelectionWithNodes.js:48:58)
      at getDraftEditorSelection (src/component/selection/getDraftEditorSelection.js:37:53)
      at assertGetDraftEditorSelection (src/component/selection/__tests__/getDraftEditorSelection-test.js:55:53)
      at Object.<anonymous> (src/component/selection/__tests__/getDraftEditorSelection-test.js:211:3)
```

**Test Plan**

Added a unit test for `DraftOffsetKey.js` to check for all delimiter cases.
Pull Request resolved: facebookarchive/draft-js#1995

Differential Revision: D13982004

fbshipit-source-id: 3cd5967ad86041e310c41e7bcbfff4e868062804
aforismesen added a commit to aforismesen/draft-js that referenced this pull request Jul 12, 2024
Summary:
**Summary**

If a content block was created with a key that contains the `-`, it would fail to decode the `decoratorKey` and the `leafKey` because the current implementation relies on `-` to be a parsing delimiter. Here's an example:

```js
class SomeComponent extends React.Component {
  constructor(props) {
    super(props);

    const block = new ContentBlock({
      type: 'paragraph',
      text: 'some-text',
      key: 'some-key'
    });

    const contentState = ContentBlock.createFromBlockArray([block]);

    this.state = {editorState: EditorState.createWithContent(contentState)};

    this.onChange = editorState => this.setState({editorState});
  }

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
      />
    );
  }
}
```

When you select anywhere on the editor, you'd get this error:

```
    TypeError: Cannot read property 'getIn' of undefined

      45 |   const focusBlockKey = focusPath.blockKey;
      46 |   const focusLeaf = editorState
    > 47 |     .getBlockTree(focusBlockKey)
         |                              ^
      48 |     .getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
      49 |
      50 |   const anchorLeafStart: number = anchorLeaf.get('start');

      at getUpdatedSelectionState (src/component/selection/getUpdatedSelectionState.js:47:30)
      at getDraftEditorSelectionWithNodes (src/component/selection/getDraftEditorSelectionWithNodes.js:48:58)
      at getDraftEditorSelection (src/component/selection/getDraftEditorSelection.js:37:53)
      at assertGetDraftEditorSelection (src/component/selection/__tests__/getDraftEditorSelection-test.js:55:53)
      at Object.<anonymous> (src/component/selection/__tests__/getDraftEditorSelection-test.js:211:3)
```

**Test Plan**

Added a unit test for `DraftOffsetKey.js` to check for all delimiter cases.
Pull Request resolved: facebookarchive/draft-js#1995

Differential Revision: D13982004

fbshipit-source-id: 3cd5967ad86041e310c41e7bcbfff4e868062804
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants