Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit c00add8

Browse files
committed
- added block switcher to examples
1 parent 428263a commit c00add8

File tree

12 files changed

+13913
-89
lines changed

12 files changed

+13913
-89
lines changed

examples/add-video-block/src/App.js

Lines changed: 5 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,7 @@
1-
import React, { Component, Fragment } from 'react'
2-
3-
import { EditorState, EditorBlock } from 'draft-js'
4-
import { EditorContainer, Editor, Plugin } from '@djsp/core'
5-
import { replaceWithAtomicBlock } from '@djsp/utils'
6-
import AtomicBlock from '@djsp/atomic-block'
7-
8-
class UnstyledBlock extends Component {
9-
state = {
10-
insertLink: false,
11-
}
12-
13-
render() {
14-
const {
15-
block,
16-
selection,
17-
blockProps: { onChange },
18-
} = this.props
19-
const { insertLink } = this.state
20-
21-
const showMenu =
22-
selection.isCollapsed() &&
23-
selection.getStartKey() === block.getKey() &&
24-
block.getText().length === 0 &&
25-
insertLink === false
26-
27-
return (
28-
<div className="paragraph">
29-
<EditorBlock {...this.props} />
30-
{insertLink === true &&
31-
block.getText().length === 0 && (
32-
<div contentEditable={false} className="insert-link-placeholder">
33-
Paste a youtube link
34-
</div>
35-
)}
36-
{insertLink === true &&
37-
block.getText().length > 0 && (
38-
<Fragment>
39-
<div contentEditable={false} className="insert-link-instructions">
40-
<span className="fa fa-video" /> Press enter when done
41-
</div>
42-
<Plugin
43-
handleReturn={(event, editorState) => {
44-
onChange(
45-
replaceWithAtomicBlock(editorState, 'video', {
46-
src: block.getText(),
47-
})
48-
)
49-
return 'handled'
50-
}}
51-
/>
52-
</Fragment>
53-
)}
54-
{showMenu && (
55-
<aside contentEditable={false} className="block-menu">
56-
<button onClick={() => this.setState({ insertLink: true })}>
57-
<span className="fa fa-video" />
58-
</button>
59-
</aside>
60-
)}
61-
</div>
62-
)
63-
}
64-
}
1+
import React, { Component } from 'react'
2+
import { EditorState } from 'draft-js'
3+
import { EditorContainer, Editor } from '@djsp/core'
4+
import InsertBlock from './InsertBlock'
655

666
export default class App extends Component {
677
state = {
@@ -76,31 +16,7 @@ export default class App extends Component {
7616
<EditorContainer
7717
editorState={this.state.editorState}
7818
onChange={this.onChange}>
79-
<AtomicBlock type="video">
80-
{({ blockProps: { src } }) => (
81-
<iframe
82-
src={src}
83-
frameBorder="0"
84-
title="youtube video"
85-
allow="autoplay; encrypted-media"
86-
allowFullScreen
87-
/>
88-
)}
89-
</AtomicBlock>
90-
91-
<Plugin
92-
blockRendererFn={block => {
93-
if (block.getType() === 'unstyled') {
94-
return {
95-
component: UnstyledBlock,
96-
editable: true,
97-
props: {
98-
onChange: this.onChange,
99-
},
100-
}
101-
}
102-
}}
103-
/>
19+
<InsertBlock onChange={this.onChange} />
10420
<Editor />
10521
</EditorContainer>
10622
</div>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// @flow
2+
3+
import React, { Component, Fragment } from 'react'
4+
import { EditorState, EditorBlock } from 'draft-js'
5+
import { replaceWithAtomicBlock } from '@djsp/utils'
6+
import AtomicBlock from '@djsp/atomic-block'
7+
import { Plugin } from '@djsp/core'
8+
9+
class UnstyledBlock extends Component {
10+
state = {
11+
insertLink: false,
12+
}
13+
14+
render() {
15+
const {
16+
block,
17+
selection,
18+
blockProps: { onChange },
19+
} = this.props
20+
const { insertLink } = this.state
21+
22+
const showMenu =
23+
selection.isCollapsed() &&
24+
selection.getStartKey() === block.getKey() &&
25+
block.getText().length === 0 &&
26+
insertLink === false
27+
28+
return (
29+
<div className="paragraph">
30+
<EditorBlock {...this.props} />
31+
{insertLink === true &&
32+
block.getText().length === 0 && (
33+
<div contentEditable={false} className="insert-link-placeholder">
34+
Paste a youtube link
35+
</div>
36+
)}
37+
{insertLink === true &&
38+
block.getText().length > 0 && (
39+
<Fragment>
40+
<div contentEditable={false} className="insert-link-instructions">
41+
<span className="fa fa-video" /> Press enter when done
42+
</div>
43+
<Plugin
44+
handleReturn={(event, editorState) => {
45+
onChange(
46+
replaceWithAtomicBlock(editorState, 'video', {
47+
src: block.getText(),
48+
})
49+
)
50+
return 'handled'
51+
}}
52+
/>
53+
</Fragment>
54+
)}
55+
{showMenu && (
56+
<aside contentEditable={false} className="block-menu">
57+
<button onClick={() => this.setState({ insertLink: true })}>
58+
<span className="fa fa-video" />
59+
</button>
60+
</aside>
61+
)}
62+
</div>
63+
)
64+
}
65+
}
66+
67+
type Props = {
68+
onChange: EditorState => void,
69+
}
70+
71+
const InsertBlock = ({ onChange }: Props) => (
72+
<Fragment>
73+
<AtomicBlock type="video">
74+
{({ blockProps: { src } }) => (
75+
<iframe
76+
src={src}
77+
frameBorder="0"
78+
title="youtube video"
79+
allow="autoplay; encrypted-media"
80+
allowFullScreen
81+
/>
82+
)}
83+
</AtomicBlock>
84+
<Plugin
85+
blockRendererFn={block => {
86+
if (block.getType() === 'unstyled') {
87+
return {
88+
component: UnstyledBlock,
89+
editable: true,
90+
props: {
91+
onChange,
92+
},
93+
}
94+
}
95+
}}
96+
/>
97+
</Fragment>
98+
)
99+
100+
export default InsertBlock

0 commit comments

Comments
 (0)