Skip to content

Fixes #36 - custom inline styles #97

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

Merged
merged 13 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- Entities: `Images`, `Embeds`, (`Links`, `Documents`)
- And `HR`, `BR` as special cases

Expand Down Expand Up @@ -151,6 +151,8 @@ label: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
// Represents the inline style in the editor UI.
icon: PropTypes.string,
// CSS properties (in JS format) to apply for styling within the editor area.
style: PropTypes.Object,
```

#### Entities
Expand Down Expand Up @@ -180,17 +182,59 @@ Here are quick questions to help you determine which formatting to use, dependin
|----------------|--------------------------------------|
| Indicate the structure of the content | Blocks |
| Enter additional data/metadata | Entities |
| Format a portion of the text | Inline styles |
| Format a portion of a line | Inline styles |

#### Blocks
#### Custom blocks

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.


```jsx
blockTypes={[
{
label: 'T&C',
type: 'terms-and-conditions',
element: 'div',
className: 'u-smalltext',
},
]}
```

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.


Here is a basic example:

```jsx
inlineStyles={[
{
label: 'Redacted',
type: 'REDACTED',
style: {
backgroundColor: 'currentcolor',
},
},
]}
```

It is also possible to override the styling of predefined inline styles:

```jsx
inlineStyles={[
{
label: 'Bold',
type: INLINE_STYLE.BOLD,
style: {
fontWeight: 'bold',
textShadow: '1px 1px 1px black',
},
},
]}
```

#### Custom entity types

Expand All @@ -203,7 +247,7 @@ Apart from the usual label/type/icon options, entities need:

##### Sources

TODO
For now, please refer to the examples available with the project.

##### Decorators

Expand Down
37 changes: 37 additions & 0 deletions examples/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import ReactDOM from 'react-dom';

import DraftailEditor, { BLOCK_TYPE, INLINE_STYLE } from '../lib';

import allContentState from './utils/allContentState';

const mount = document.querySelector('[data-mount-all]');

const onSave = contentState => {
sessionStorage.setItem('all:contentState', JSON.stringify(contentState));
};

const allBlockTypes = Object.keys(BLOCK_TYPE).map(type => ({
label: `${type.charAt(0).toUpperCase()}${type
.slice(1)
.toLowerCase()
.replace(/_/g, ' ')}`,
type: BLOCK_TYPE[type],
}));

const allInlineStyles = Object.keys(INLINE_STYLE).map(type => ({
label: `${type.charAt(0).toUpperCase()}${type.slice(1).toLowerCase()}`,
type: INLINE_STYLE[type],
}));

const editor = (
<DraftailEditor
rawContentState={allContentState}
onSave={onSave}
stripPastedStyles={false}
blockTypes={allBlockTypes}
inlineStyles={allInlineStyles}
/>
);

ReactDOM.render(editor, mount);
15 changes: 14 additions & 1 deletion examples/custom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import DraftailEditor, { BLOCK_TYPE } from '../lib';
import DraftailEditor, { BLOCK_TYPE, INLINE_STYLE } from '../lib';

const mount = document.querySelector('[data-mount-custom]');

Expand All @@ -27,6 +27,7 @@ const editor = (
<DraftailEditor
rawContentState={rawContentState}
onSave={onSave}
stripPastedStyles={false}
blockTypes={[
{ label: 'H2', type: BLOCK_TYPE.HEADER_TWO },
{
Expand All @@ -36,6 +37,18 @@ const editor = (
className: 'u-tinytext',
},
]}
inlineStyles={[
{
label: 'Bold',
type: INLINE_STYLE.BOLD,
style: { fontWeight: 'bold', textShadow: '1px 1px 1px black' },
},
{
label: 'Redacted',
type: 'REDACTED',
style: { backgroundColor: 'currentcolor' },
},
]}
/>
);

Expand Down
8 changes: 7 additions & 1 deletion examples/examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ <h4><a href="https://github.com/springload/draftail/blob/master/examples/entitie
</div>

<div id="example-custom" class="example">
<h4><a href="https://github.com/springload/draftail/blob/master/examples/custom.js">Custom block types</a> <a class="anchor" href="#example-custom">#</a></h4>
<h4><a href="https://github.com/springload/draftail/blob/master/examples/custom.js">Custom formats</a> <a class="anchor" href="#example-custom">#</a></h4>
<div data-mount-custom></div>
</div>

<div id="example-all" class="example">
<h4><a href="https://github.com/springload/draftail/blob/master/examples/all.js">All built-in formats</a> <a class="anchor" href="#example-all">#</a></h4>
<div data-mount-all></div>
</div>
</section>
<footer>
<a href="https://www.springload.co.nz/" style="display: block;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 28" style="width:144px; height:24px;"><title>Springload</title><path fill="#4A4A4A" d="M8.9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9-.1 1-.6 1.9-1.8 1.9z"></path></svg></a>
Expand All @@ -40,6 +45,7 @@ <h4><a href="https://github.com/springload/draftail/blob/master/examples/custom.
<script src="assets/basic.bundle.js"></script>
<script src="assets/entities.bundle.js"></script>
<script src="assets/custom.bundle.js"></script>
<script src="assets/all.bundle.js"></script>

<script>
window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
Expand Down
5 changes: 5 additions & 0 deletions examples/sources/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-alert": [0]
}
}
2 changes: 2 additions & 0 deletions examples/utils/allContentState.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/api/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
INLINE_STYLE,
KEY_CODES,
KEYBOARD_SHORTCUTS,
CUSTOM_STYLE_MAP,
} from '../api/constants';

const { hasCommandModifier, isOptionKeyCommand } = KeyBindingUtil;
Expand Down Expand Up @@ -217,4 +218,20 @@ export default {
? blockType
: false;
},

getCustomStyleMap(inlineStyles) {
const customStyleMap = {};

inlineStyles.forEach(style => {
if (style.style) {
customStyleMap[style.type] = style.style;
} else if (CUSTOM_STYLE_MAP[style.type]) {
customStyleMap[style.type] = CUSTOM_STYLE_MAP[style.type];
} else {
customStyleMap[style.type] = {};
}
});

return customStyleMap;
},
};
63 changes: 62 additions & 1 deletion lib/api/behavior.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { List, Repeat } from 'immutable';
import { CharacterMetadata, ContentBlock } from 'draft-js';

import behavior from '../api/behavior';
import { BLOCK_TYPE, KEY_CODES } from '../api/constants';
import {
BLOCK_TYPE,
INLINE_STYLE,
KEY_CODES,
CUSTOM_STYLE_MAP,
} from '../api/constants';

describe('behavior', () => {
describe('#getBlockRenderMap', () => {
Expand Down Expand Up @@ -292,4 +297,60 @@ describe('behavior', () => {
).toBe('ordered-list-item');
});
});

describe('#getCustomStyleMap', () => {
it('exists', () => {
expect(behavior.getCustomStyleMap).toBeDefined();
});

it('existing styles, default styling', () => {
expect(
behavior.getCustomStyleMap([
{ label: 'Bold', type: INLINE_STYLE.BOLD },
{ label: 'Mark', type: INLINE_STYLE.MARK },
]),
).toEqual({
[INLINE_STYLE.BOLD]: CUSTOM_STYLE_MAP[INLINE_STYLE.BOLD],
[INLINE_STYLE.MARK]: CUSTOM_STYLE_MAP[INLINE_STYLE.MARK],
});
});

it('existing styles, custom styling', () => {
expect(
behavior.getCustomStyleMap([
{
label: 'Bold',
type: INLINE_STYLE.BOLD,
style: {
color: 'yellow',
},
},
]),
).toEqual({
[INLINE_STYLE.BOLD]: {
color: 'yellow',
},
});
});

it('custom styles, custom styling', () => {
expect(
behavior.getCustomStyleMap([
{ label: 'Red', type: 'RED', style: { color: 'red' } },
]),
).toEqual({
RED: {
color: 'red',
},
});
});

it('custom styles, undefined styling', () => {
expect(
behavior.getCustomStyleMap([{ label: 'Red', type: 'RED' }]),
).toEqual({
RED: {},
});
});
});
});
73 changes: 73 additions & 0 deletions lib/api/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DefaultDraftInlineStyle } from 'draft-js';

// Originally from https://github.com/draft-js-utils/draft-js-utils/blob/master/src/Constants.js.
export const BLOCK_TYPE = {
// This is used to represent a normal text block (paragraph).
Expand Down Expand Up @@ -32,6 +34,77 @@ export const INLINE_STYLE = {
CODE: 'CODE',
UNDERLINE: 'UNDERLINE',
STRIKETHROUGH: 'STRIKETHROUGH',
MARK: 'MARK',
QUOTATION: 'QUOTATION',
SMALL: 'SMALL',
SAMPLE: 'SAMPLE',
INSERT: 'INSERT',
DELETE: 'DELETE',
KEYBOARD: 'KEYBOARD',
SUPERSCRIPT: 'SUPERSCRIPT',
SUBSCRIPT: 'SUBSCRIPT',
};

export const FONT_FAMILY_MONOSPACE =
'Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, sans-serif';

export const CUSTOM_STYLE_MAP = {
[INLINE_STYLE.BOLD]: DefaultDraftInlineStyle[INLINE_STYLE.BOLD],
[INLINE_STYLE.ITALIC]: DefaultDraftInlineStyle[INLINE_STYLE.ITALIC],
[INLINE_STYLE.STRIKETHROUGH]:
DefaultDraftInlineStyle[INLINE_STYLE.STRIKETHROUGH],
[INLINE_STYLE.UNDERLINE]: DefaultDraftInlineStyle[INLINE_STYLE.UNDERLINE],

[INLINE_STYLE.CODE]: {
padding: '0.2em 0.3125em',
margin: '0',
fontSize: '85%',
backgroundColor: 'rgba(27, 31, 35, 0.05)',
fontFamily: FONT_FAMILY_MONOSPACE,
borderRadius: '3px',
},

[INLINE_STYLE.MARK]: {
backgroundColor: 'yellow',
},
[INLINE_STYLE.QUOTATION]: {
fontStyle: 'italic',
},
[INLINE_STYLE.SMALL]: {
fontSize: 'smaller',
},
[INLINE_STYLE.SAMPLE]: {
fontFamily: FONT_FAMILY_MONOSPACE,
},
[INLINE_STYLE.INSERT]: {
textDecoration: 'underline',
},
[INLINE_STYLE.DELETE]: {
textDecoration: 'line-through',
},
[INLINE_STYLE.KEYBOARD]: {
fontFamily: FONT_FAMILY_MONOSPACE,
padding: '3px 5px',
fontSize: '11px',
lineHeight: '10px',
color: '#444d56',
verticalAlign: 'middle',
backgroundColor: '#fafbfc',
border: 'solid 1px #c6cbd1',
borderBottomColor: '#959da5',
borderRadius: '3px',
boxShadow: 'inset 0 -1px 0 #959da5',
},
[INLINE_STYLE.SUPERSCRIPT]: {
fontSize: '80%',
verticalAlign: 'super',
lineHeight: '1',
},
[INLINE_STYLE.SUBSCRIPT]: {
fontSize: '80%',
verticalAlign: 'sub',
lineHeight: '1',
},
};

export const BR_TYPE = 'BR';
Expand Down
Loading