1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-10 00:10:18 +01:00
slate/docs/walkthroughs/applying-custom-formatting.md

184 lines
4.2 KiB
Markdown
Raw Normal View History

2016-07-08 14:10:06 -07:00
<br/>
<p align="center"><strong>Previous:</strong><br/><a href="./defining-custom-block-nodes.md">Defining Custom Block Nodes</a></p>
<br/>
2016-07-12 21:34:31 -07:00
# Applying Custom Formatting
2016-07-08 14:10:06 -07:00
In the previous guide we learned how to create custom block types that render chunks of text inside different containers. But Slate allows for more than just "blocks".
In this guide, we'll show you how to add custom formatting options, like **bold**, _italic_, `code` or ~~strikethrough~~.
So we start with our app from earlier:
```js
class App extends React.Component {
2016-08-14 18:25:12 -07:00
state = {
state: initialState,
schema: {
nodes: {
code: CodeNode
}
2016-07-08 14:10:06 -07:00
}
}
onChange = ({ state }) => {
this.setState({ state })
2016-07-08 14:10:06 -07:00
}
onKeyDown = (event, change) => {
if (event.key != '`' || !event.metaKey) return
2017-02-16 10:16:19 -08:00
event.preventDefault()
const isCode = change.state.blocks.some(block => block.type == 'code')
2016-07-08 14:10:06 -07:00
change.setBlock(isCode ? 'paragraph' : 'code')
return true
2016-07-08 14:10:06 -07:00
}
2017-08-02 18:36:33 +02:00
render() {
return (
<Editor
state={this.state.state}
schema={this.state.schema}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
)
}
2016-07-08 14:10:06 -07:00
}
```
2016-08-25 22:59:36 -04:00
And now, we'll edit the `onKeyDown` handler to make it so that when you press `⌘-B`, it will add a "bold" mark to the currently selected text:
2016-07-08 14:10:06 -07:00
```js
class App extends React.Component {
2016-08-14 18:25:12 -07:00
state = {
state: initialState,
schema: {
nodes: {
code: CodeNode
}
2016-07-08 14:10:06 -07:00
}
}
onChange = ({ state }) => {
this.setState({ state })
2016-07-08 14:10:06 -07:00
}
onKeyDown = (event, change) => {
2016-07-08 14:10:06 -07:00
if (!event.metaKey) return
// Decide what to do based on the key code...
switch (event.key) {
2016-07-08 14:10:06 -07:00
// When "B" is pressed, add a "bold" mark to the text.
case 'b': {
2017-02-16 10:16:19 -08:00
event.preventDefault()
change.addMark('bold')
return true
2016-07-08 14:10:06 -07:00
}
// When "`" is pressed, keep our existing code block logic.
case '`': {
const isCode = change.state.blocks.some(block => block.type == 'code')
2017-02-16 10:16:19 -08:00
event.preventDefault()
change.setBlock(isCode ? 'paragraph' : 'code')
return true
2016-07-08 14:10:06 -07:00
}
}
}
2017-08-02 18:36:33 +02:00
render() {
return (
<Editor
schema={this.state.schema}
state={this.state.state}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
)
}
2016-07-08 14:10:06 -07:00
}
```
2016-08-25 22:59:36 -04:00
Okay, so we've got the hotkey handler setup... but! If you happen to now try selecting text and hitting `⌘-B`, you'll get an error in your console. That's because we haven't told Slate how to render a "bold" mark.
2016-07-08 14:10:06 -07:00
2016-08-14 18:25:12 -07:00
For every mark type you want to add to your schema, you need to give Slate a "renderer" for that mark, just like nodes. So let's define our `bold` mark:
2016-07-08 14:10:06 -07:00
```js
2016-08-14 18:25:12 -07:00
// Define a React component to render bold text with.
function BoldMark(props) {
return <strong>{props.children}</strong>
2016-07-08 14:10:06 -07:00
}
```
Pretty simple, right?
And now, let's tell Slate about that mark.
To do that, we'll add it to the `schema` object under a `marks` property.
Also, let's allow our mark to be toggled by changing `addMark` to `toggleMark`.
2016-07-08 14:10:06 -07:00
```js
2016-08-14 18:25:12 -07:00
function BoldMark(props) {
return <strong>{props.children}</strong>
2016-07-08 14:10:06 -07:00
}
class App extends React.Component {
2016-08-14 18:25:12 -07:00
state = {
state: initialState,
schema: {
nodes: {
code: CodeNode
},
// Add our "bold" mark to the schema...
marks: {
bold: BoldMark
}
2016-07-08 14:10:06 -07:00
}
}
onChange = ({ state }) => {
this.setState({ state })
2016-07-08 14:10:06 -07:00
}
onKeyDown = (event, change) => {
2016-07-08 14:10:06 -07:00
if (!event.metaKey) return
switch (event.key) {
case 'b': {
2017-02-16 10:16:19 -08:00
event.preventDefault()
change.toggleMark('bold')
return true
2016-07-08 14:10:06 -07:00
}
case '`': {
const isCode = change.state.blocks.some(block => block.type == 'code')
2017-02-16 10:16:19 -08:00
event.preventDefault()
state.setBlock(isCode ? 'paragraph' : 'code')
return true
2016-07-08 14:10:06 -07:00
}
}
}
2017-08-02 18:36:33 +02:00
render() {
return (
<Editor
schema={this.state.schema}
state={this.state.state}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
)
}
2016-07-08 14:10:06 -07:00
}
```
2016-08-25 22:59:36 -04:00
Now, if you try selecting a piece of text and hitting `⌘-B` you should see it turn bold! Magic!
2016-07-08 14:10:06 -07:00
<br/>
<p align="center"><strong>Next:</strong><br/><a href="./using-plugins.md">Using Plugins</a></p>
<br/>