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

Text Alignment

Open Philin-Anton opened this issue 10 years ago • 45 comments

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!

Philin-Anton avatar Mar 30 '16 11:03 Philin-Anton

Not sure what you are referring to. If you use the textAlignment prop on Editor, it will align your text for the entire editor.

hellendag avatar Mar 31 '16 21:03 hellendag

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,

SumeetR avatar Apr 07 '16 14:04 SumeetR

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.

mitermayer avatar Apr 07 '16 23:04 mitermayer

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 avatar Apr 08 '16 01:04 hellendag

@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 avatar Apr 08 '16 18:04 johanneslumpe

@johanneslumpe I think that would enable it, yeah. I've imported that one but haven't merged it yet, though.

hellendag avatar Apr 08 '16 18:04 hellendag

Yep we're hoping to use block level metadata for this too.

joshdover avatar Apr 15 '16 22:04 joshdover

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 avatar May 13 '16 10:05 Zulfia73

@Zulfia73 I think we will need https://github.com/facebook/draft-js/pull/372/files to be merged

mitermayer avatar May 13 '16 12:05 mitermayer

@hellendag I think it's a common issue to set text alignment on a per-block basis.

ruffle1986 avatar May 26 '16 07:05 ruffle1986

@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 avatar May 26 '16 07:05 johanneslumpe

@johanneslumpe great news! thank you guys, you're awesome! 👍

ruffle1986 avatar May 26 '16 07:05 ruffle1986

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 avatar Jun 15 '16 21:06 deanmcpherson

@deanmcpherson Can you share the code? I'm interested in your workaround :)

lmgonzalves avatar Jun 15 '16 22:06 lmgonzalves

@lmgonzalves Just threw the pieces roughly together at https://gist.github.com/deanmcpherson/69f9962b744b273ffb64fe294ab71bc4

deanmcpherson avatar Jun 16 '16 07:06 deanmcpherson

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

N1kto avatar Jun 27 '16 10:06 N1kto

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 avatar Aug 14 '16 18:08 cinder92

@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 avatar Aug 14 '16 21:08 johanneslumpe

@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 avatar Aug 14 '16 22:08 cinder92

@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)

johanneslumpe avatar Aug 15 '16 13:08 johanneslumpe