Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36 - custom inline styles #97

Merged
merged 13 commits into from
Oct 17, 2017
Merged

Conversation

vincentaudebert
Copy link
Contributor

@vincentaudebert vincentaudebert commented Aug 30, 2017

Fixes #36. For your review @thibaudcolas

Added a new draftUtils function and tested it with some edge cases + 100% coverage.

Let me know what you think.

@thibaudcolas
Copy link
Collaborator

Thank you @vincentaudebert 👏 ! Looks good from afar, I'll do a proper review next week.

parseCustomStyleMap(constantInlineStyles, defaultCustomStyleMap, inlineStyles) {
const customStyleMap = defaultCustomStyleMap;
const styleValues = Object.keys(constantInlineStyles).map(k => constantInlineStyles[k]);
const verifiedStyles = inlineStyles.filter(style => styleValues.indexOf(style.type) !== -1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems problematic. If the type isn't in constantInlineStyles, it's not going to be in verifiedStyles and won't be added to the style map, making it impossible to define styles for custom inline styles.

export const CUSTOM_STYLE_MAP = {
[INLINE_STYLE.MARK]: {
backgroundColor: 'yellow',
color: 'black',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will remove the color override – I think it would be better if we retained the normal text color.

marginLeft: '1em',
marginTop: '1em',
marginBottom: '1em',
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like the styles of a blockquote. Here we should target inline styles only, so this should be the q element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q.

@@ -49,6 +49,7 @@ const defaultProps = {
maxListNesting: 1,
// Frequency at which the save callback is triggered (ms).
stateSaveInterval: 250,
customStyleMap: {},
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a leftover from a previous iteration.

@@ -77,6 +78,7 @@ const propTypes = {
type: PropTypes.string.isRequired,
// Represents the inline style in the editor UI.
icon: PropTypes.string,
style: PropTypes.Object,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Every single one of these props must be commented so we can add those comments in the README as documentation.

const decorators = entityTypes.filter(type => !!type.decorator)
.map(type => ({
strategy: type.strategy || DraftUtils.getEntityTypeStrategy(type.type),
component: type.decorator,
}));

const customStyleMapParsed = DraftUtils.parseCustomStyleMap(INLINE_STYLE, CUSTOM_STYLE_MAP, inlineStyles);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be simpler if this was directly done in render, instead of being added to the state? Here this will fall out of date if the editor is re-rendered with new values for inlineStyles.

@thibaudcolas thibaudcolas dismissed their stale review October 17, 2017 07:35

Refactorings

@thibaudcolas thibaudcolas force-pushed the feature/custom-inline-styles branch 2 times, most recently from 1e08cfe to 79810c3 Compare October 17, 2017 10:48
@@ -108,7 +108,7 @@ Draftail, like Draft.js, distinguishes between 3 content formats:
Common formatting options are available out of the box:

- Block types: `H1`, `H2`, `H3`, `H4`, `H5`, `H6`, `Blockquote`, `Code`, `UL`, `OL`, `P`
- Inline styles: `Bold`, `Italic`, `Underline`, `Monospace`, `Strikethrough`
- Inline styles: `Bold`, `Italic`, `Code`, `Underline`, `Strikethrough`, `Mark`, `Keyboard`, `Superscript`, `Subscript`
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are more, but I chose to leave them out because they are seldom used in rich text editors.


Simple blocks are very easy to create. Add a new block type to `blockTypes`, specifying which `element` and `className` to display the block as.

Here is an example, creating a "Terms & conditions" block:
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not my fav example but I hope it's easy enough to relate to the need for this.

More advanced blocks, requiring custom React components, aren't supported at the moment. If you need this, feel free to [create an issue](https://github.com/springload/draftail/issues/new).

#### Custom inline styles

Custom inline styles aren't supported at the moment. This is on the roadmap, please refer to [#36](https://github.com/springload/draftail/issues/36).
Custom inline styles only require a `style` prop, defining which CSS properties to apply when the format is active.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not actually required – but if it's not there, there will be no way to tell whether the styles are active or not.

@thibaudcolas thibaudcolas merged commit 16e2940 into master Oct 17, 2017
@thibaudcolas thibaudcolas deleted the feature/custom-inline-styles branch October 17, 2017 11:02
@thibaudcolas thibaudcolas added this to the v1.0.0 milestone Oct 17, 2017
@thibaudcolas thibaudcolas modified the milestones: v1.0.0, Wagtail 2.0 Dec 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants