question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toggle inline styles to current block(s), even when it is not fully selected

See original GitHub issue

First of all, thank you very much for making public this fantastic editor.

I’m trying to do something simple, at least I think so 😃 I want to toggle inline styles for the entire current block(s), as if fully selected, even when it is not. Even with the clear understanding that I have to play with SelectionState, I can’t understand how to do it.

This is what I have so far (the basic functionality), taken from the RichEditor example:

_toggleInlineStyle(inlineStyle) {
  this.onChange(
    RichUtils.toggleInlineStyle(
      this.state.editorState,
      inlineStyle
    )
  );
}

Thanks again for this awesome project!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

14reactions
pveyescommented, May 29, 2016

First you have to change your selection into entire block. To do that you can set anchorOffset to 0, and focusOffset to content length, then apply using forceSelection which will return new editorstate. You can then use the new editor state to perform the operation you want, like this

const editorState = this.state.editorState;
const currentKey = editorState.getSelection().getAnchorKey();
const currentBlock = editorState.getCurrentContent().getBlockForKey(currentKey);
const selectionState = SelectionState.createEmpty();
const entireBlockSelectionState = selectionState.merge({
  anchorKey: currentKey,
  anchorOffset: 0,
  focusKey: currentKey,
  focusOffset: currentBlock.getText().length
});

const newEditorState = EditorState.forceSelection(
  editorState,
  entireBlockSelectionState
);

// do what you want using newEditorState here, e.g:
this.onChange(
  RichUtils.toggleInlineStyle(
    newEditorState,
    inlineStyle
  )
);
1reaction
lmgonzalvescommented, May 29, 2016

Yeah, it’s solved now! Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to remove inline styles with jQuery?
At first I tried to remove inline style in css, but it does not work and new style like display: none will be...
Read more >
display - CSS: Cascading Style Sheets - MDN Web Docs
The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, ......
Read more >
The CSS Display Property – Display None, Display Table ...
An element with a display property set to inline will not start on a new line and it will take up the remaining/available...
Read more >
Style Sheets in HTML documents - W3C
The syntax of the value of the style attribute is determined by the default style sheet language. For example, for [[CSS2]] inline style,...
Read more >
A Complete Guide to CSS Cascade Layers
As web authors, we often think of !important as a way of increasing specificity, to override inline styles or highly specific selectors. That ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found