-
Notifications
You must be signed in to change notification settings - Fork 64
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
Changes from all commits
b9716f4
69e822c
c0c8f1a
cd2fa68
f92b870
375a2e7
2209935
d74ef07
3dbfd8e
f55c904
b7678fb
79810c3
2d078a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
- Entities: `Images`, `Embeds`, (`Links`, `Documents`) | ||
- And `HR`, `BR` as special cases | ||
|
||
|
@@ -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 | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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 | ||
|
||
|
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-alert": [0] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.