Text Alignment
I really like this feature, but it is not able to align text, tell me how to do it? PS with example code please, thanks!
Not sure what you are referring to. If you use the textAlignment prop on Editor, it will align your text for the entire editor.
I think that @Philin-Anton is referring to aligning certain blocks of text and not the entire editor. Any ideas on how one would go about doing that?
Thanks, S,
You would have to do the following, create a getBlockStyle handler:
const getBlockStyle = (block) => {
switch (block.getType()) {
case 'left':
return 'align-left';
case 'center':
return 'align-center';
case 'right':
return 'align-right';
default:
return null;
}
}
pass it to your editor
<Editor
blockStyleFn={getBlockStyle}
...
/>
When clicking on a button that triggers a class 'align-right' or 'align-center' or 'align-left' will be added to your block container. WIth that you can use CSS to:
.align-right div {
text-align: right;
}
.align-center div {
text-align: center;
}
.align-left div {
text-align: left;
}
In order to hook buttons to commands you can have a look into the rich.html example from draftjs.
I generally wouldn't advise using block types that are outside the EditorBlockType enum.
@Philin-Anton I'd say there aren't really supported ways to do this right now on a per-block level. Is it possible that you only need certain block types to be aligned differently? If so, you can certainly use blockStyleFn for that.
@hellendag Wouldn't it be possible to do this now that https://github.com/facebook/draft-js/pull/216 has landed? When aligning a block, custom meta data could be assigned to that block, which can then be used in the blockStyleFn to derive which classes should be added.
@johanneslumpe I think that would enable it, yeah. I've imported that one but haven't merged it yet, though.
Yep we're hoping to use block level metadata for this too.
I did all as say @mitermayer, but i have error.
my code:
onCenter(self){ self.onChange(RichUtils.toggleBlockType(self.state.editorState, 'centerAlign')); }
and that error:
nullthrows.js?2b54:18 Uncaught Error: Got unexpected null or undefined
that my getBlockStyle:
myBlockStyleFn(block) { console.log(block.getType()) switch (block.getType()) { case 'leftAlign': return 'emailAlignLeft'; case 'centerAlign': return 'emailAlignCenter'; case 'rightAlign': return 'emailAlignRight'; default: return null; } }
what i do wrong?
@Zulfia73 I think we will need https://github.com/facebook/draft-js/pull/372/files to be merged
@hellendag I think it's a common issue to set text alignment on a per-block basis.
@ruffle1986 The PR for block level meta data has already been imported so it is just a matter of time now until you can do alignment with meta data.
@johanneslumpe great news! thank you guys, you're awesome! 👍
For reference I found a hackish way around text alignment with the current version. I used custom inline styles and checked for their existence within a block in my blockStyler function to add alignment classes to the whole block. To do so I wrote a modifier which applies a style to all of the blocks within the selection range and removes an arbitrary array of styles, so that a block can only have one alignment at a time.
It's obviously a bit dirty, but works quite well so far.
@deanmcpherson Can you share the code? I'm interested in your workaround :)
@lmgonzalves Just threw the pieces roughly together at https://gist.github.com/deanmcpherson/69f9962b744b273ffb64fe294ab71bc4
Is there any 'api' solution so far? Block alignment seems to be a pretty common use case, so we really lack it in Draft. Thnx
any update about this? i'm getting this error following the steps of @mitermayer DraftEditorContents.react.js:151 Uncaught TypeError: Cannot read property 'wrapper' of undefined
@cinder92 block-level meta data has been included in version 0.8. So you do not need @mitermayer's hack from above - you can now use the meta data to tell your block which kind of text alignment you want. You can then retrieve the alignment in a custom block component and apply it in any way you want to the wrapper.
@johanneslumpe thanks for your quick reply! do you have any example about this? i'm searching since 2 hours ago and there is nothing! 👍
@cinder92 I'm sorry, but I do not have an actual working example of this right now. I might throw one together soon though. The basic gist is to get the current selection's content block instance and then set custom alignment metadata on it. How that metadata is then interpreted is up to the wrapper component. (Could be float or just text-align)