draft-js icon indicating copy to clipboard operation
draft-js copied to clipboard

Is there a way to get the inline styles of current selection?

Open ricardo opened this issue 10 years ago • 8 comments

Do you want to request a feature or report a bug? feature

What is the current behavior? I haven't seen a way to get the styles of the current selection

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar.

What is the expected behavior? Show me a list of the styles from the editorState current selection

Which versions of Draft.js, and which browser / OS are affected by this issue? Did this work in previous versions of Draft.js?

ricardo avatar Aug 18 '16 06:08 ricardo

With current selection you can get startKey, endKey . Base on those keys, you can get all blocks in that selection. With each block, you can use getCharacterList function to get all character in that block . Then with each character, use getStyle it will return style for you Concat all of them, you can get style of current selection Example code block.getCharacterList().reduce( (styles,c) => styles.union(c.getStyle()), Immutable.OrderedSet() ) Hope it will help you

namKolo avatar Aug 18 '16 09:08 namKolo

draftjs-utils has a getSelectionCustomInlineStyle function.

thibaudcolas avatar Jan 19 '18 15:01 thibaudcolas

To anyone coming here from google - there does exist a built in getter method called getCurrentInlineStyle on the editorState object.

So if you for example want to know if the current selection is "BOLD" you could do this:

const { editorState } = this.state;
const inlineStyle = editorState.getCurrentInlineStyle();
const isBold = inlineStyle.has("BOLD");

I hope that helps someone!

markusenglund avatar Aug 15 '18 20:08 markusenglund

The question was, how to get the style of the "current selection", the @markusenglund answer will give style at the cursor location, not the selection. I'm still searching for the answer.

palamccc avatar Nov 13 '18 03:11 palamccc

@palamccc couldnt you get the focus first and pass it into the getCurrentInlineStyle() function?

const currentFocus = editor.getSelection().getFocusKey()
const inlineStyle = editor.getCurrentInlineStyle(currentFocus);
const isBold = inlineStyle.has("BOLD")

promerorgz avatar Jun 18 '19 16:06 promerorgz

@promerorgz, thanks for the comment, we have moved our project to https://github.com/ianstormtaylor/slate I can't confirm whether your solution works or not.

palamccc avatar Jun 19 '19 06:06 palamccc

@promerorgz worked like a charm, thanks!

jtabone16 avatar Aug 09 '19 16:08 jtabone16

const selection 	= editorState.getSelection();
const contentState	= editorState.getCurrentContent();
const styles		= new Set<string>();
if (selection.isCollapsed()) editorState.getCurrentInlineStyle().forEach(style => style && styles.add(style));
else
{
	let 	key		= selection.getStartKey();
	let	startOffset	= selection.getStartOffset();
	const	endKey		= selection.getEndKey();
	const	endOffset	= selection.getEndOffset();
	while(true)
	{
		const lastRound		= (key == endKey);
		const block		= contentState.getBlockForKey(key);
		const offsetEnd		= lastRound ? endOffset : block.getLength();
		const characterList	= block.getCharacterList();
		for(let offsetIndex=startOffset; offsetIndex<offsetEnd; offsetIndex++)
			characterList.get(offsetIndex).getStyle().forEach(style => style && styles.add(style));
		if (lastRound) break;
		key		= contentState.getKeyAfter(key);
		startOffset	= 0;
	}
}

dabretin avatar Feb 11 '20 14:02 dabretin