Skip to content

Commit fc3d496

Browse files
committed
Combine nodes, marks, commands, and queries into markdown plugin
1 parent 7c44f83 commit fc3d496

File tree

10 files changed

+134
-104
lines changed

10 files changed

+134
-104
lines changed

packages/editor/src/components/Editor.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { isUrl, isImageUrl } from '../lib/util'
1111

1212
import theme from './theme'
1313

14-
import NodesPlugin from '../plugins/nodes'
15-
import MarksPlugin from '../plugins/marks'
14+
import MarkdownPlugin from '../plugins/markdown'
15+
1616
import CodePlugin from '../plugins/code'
1717
import LiveJSXPlugin from '../plugins/live-jsx'
1818
import TablePlugin from '../plugins/table'
@@ -24,8 +24,7 @@ import MarkdownShortcutsPlugin from '../plugins/markdown-shortcuts'
2424

2525
const plugins = [
2626
SoftBreak({ shift: true }),
27-
NodesPlugin(),
28-
MarksPlugin(),
27+
MarkdownPlugin(),
2928
CodePlugin(),
3029
LiveJSXPlugin(),
3130
TablePlugin(),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const DEFAULT_BLOCK = 'paragraph'
2+
3+
const toggleBold = editor => {
4+
editor.toggleMark('bold').focus()
5+
}
6+
7+
const toggleItalic = editor => {
8+
editor.toggleMark('italic').focus()
9+
}
10+
11+
const toggleBlock = (editor, type) => {
12+
if (editor.hasBlock(type)) {
13+
editor.setBlocks(DEFAULT_BLOCK)
14+
} else {
15+
editor.setBlocks(type)
16+
}
17+
}
18+
19+
const toggleHeadingOne = editor => toggleBlock(editor, 'heading-one')
20+
const toggleHeadingTwo = editor => toggleBlock(editor, 'heading-two')
21+
22+
export default {
23+
toggleBold,
24+
toggleItalic,
25+
toggleBlock,
26+
toggleHeadingOne,
27+
toggleHeadingTwo
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
3+
import queries from './queries'
4+
import commands from './commands'
5+
import renderMark from './renderMark'
6+
import renderNode from './renderNode'
7+
import onKeyDown from './onKeyDown'
8+
9+
export default (opts = {}) => ({
10+
queries,
11+
commands,
12+
renderMark,
13+
renderNode,
14+
onKeyDown
15+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { keyboardEvent } from '@slate-editor/utils'
2+
3+
export default (event, editor, next) => {
4+
if (!keyboardEvent.isMod(event)) return next()
5+
switch (event.key) {
6+
case 'b':
7+
editor.toggleBold()
8+
break
9+
case 'i':
10+
editor.toggleItalic()
11+
break
12+
default:
13+
next()
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const hasBlock = (editor, type) => {
2+
return editor.value.blocks.some(node => node.type === type)
3+
}
4+
5+
export default {
6+
hasBlock
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
import { Styled } from 'theme-ui'
3+
4+
export default (props, editor, next) => {
5+
const { mark, attributes, children } = props
6+
7+
switch (mark.type) {
8+
case 'bold':
9+
return <Styled.strong {...attributes}>{children}</Styled.strong>
10+
case 'code':
11+
return <Styled.inlineCode {...attributes}>{children}</Styled.inlineCode>
12+
case 'italic':
13+
return <Styled.em {...attributes}>{children}</Styled.em>
14+
case 'underlined':
15+
return <u {...attributes}>{children}</u>
16+
case 'strikethrough':
17+
return <s {...attributes}>{children}</s>
18+
default:
19+
return next()
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react'
2+
import { Styled } from 'theme-ui'
3+
4+
export default (props, editor, next) => {
5+
const { node, attributes, children } = props
6+
7+
switch (node.type) {
8+
case 'block-quote':
9+
return <Styled.blockquote {...attributes}>{children}</Styled.blockquote>
10+
case 'bulleted-list':
11+
return <Styled.ul {...attributes}>{children}</Styled.ul>
12+
case 'numbered-list':
13+
return <Styled.ol {...attributes}>{children}</Styled.ol>
14+
case 'list-item':
15+
return <Styled.li {...attributes}>{children}</Styled.li>
16+
case 'list-item-child':
17+
return <Styled.p {...attributes}>{children}</Styled.p>
18+
case 'heading-one':
19+
return <Styled.h1 {...attributes}>{children}</Styled.h1>
20+
case 'heading-two':
21+
return <Styled.h2 {...attributes}>{children}</Styled.h2>
22+
case 'heading-three':
23+
return <Styled.h3 {...attributes}>{children}</Styled.h3>
24+
case 'heading-four':
25+
return <Styled.h4 {...attributes}>{children}</Styled.h4>
26+
case 'heading-five':
27+
return <Styled.h5 {...attributes}>{children}</Styled.h5>
28+
case 'heading-six':
29+
return <Styled.h6 {...attributes}>{children}</Styled.h6>
30+
case 'list-item':
31+
return <Styled.li {...attributes}>{children}</Styled.li>
32+
case 'paragraph':
33+
return <Styled.p {...attributes}>{children}</Styled.p>
34+
case 'hr':
35+
return <Styled.hr {...attributes} />
36+
case 'link':
37+
return (
38+
<Styled.a {...attributes} href={node.data.get('href')}>
39+
{children}
40+
</Styled.a>
41+
)
42+
default:
43+
return next()
44+
}
45+
}

packages/editor/src/plugins/marks.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

packages/editor/src/plugins/nodes.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/editor/src/plugins/toolbar/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ const toggleBlockQuote = editor => {
4444
}
4545
}
4646

47-
const toggleHeadingOne = editor => toggleBlock(editor, 'heading-one')
48-
const toggleHeadingTwo = editor => toggleBlock(editor, 'heading-two')
4947
const toggleJSX = editor => toggleBlock(editor, 'jsx')
5048
const togglePre = editor => toggleBlock(editor, 'pre')
5149

@@ -83,8 +81,6 @@ export default (opts = {}) => ({
8381
commands: {
8482
toggleBlock,
8583
toggleBlockQuote,
86-
toggleHeadingOne,
87-
toggleHeadingTwo,
8884
toggleBulletedList,
8985
toggleJSX,
9086
togglePre

0 commit comments

Comments
 (0)