11/** @jsx jsx */
22import { jsx } from '@emotion/core'
3- import React from 'react'
3+ import React , { useContext } from 'react'
44import { Data } from 'slate'
5+ import { Context } from '../../components/context'
56import Form from './Form'
67import Overlay from './Overlay'
7- import YouTube from './YouTube'
8- import Tweet from './Tweet'
9- import Gist from './Gist'
108
119const hasJSXBlock = ( editor , type ) => {
1210 if ( ! editor . hasBlock ( 'jsx-void' ) && ! editor . hasBlock ( 'jsx' ) ) return false
@@ -30,24 +28,6 @@ const insertJSXBlock = (editor, type, props) => {
3028 } )
3129}
3230
33- const insertYouTube = editor => {
34- editor . insertJSXBlock ( 'YouTube' , {
35- videoId : ''
36- } )
37- }
38-
39- const insertTweet = editor => {
40- editor . insertJSXBlock ( 'Tweet' , {
41- tweetId : ''
42- } )
43- }
44-
45- const insertGist = editor => {
46- editor . insertJSXBlock ( 'Gist' , {
47- id : ''
48- } )
49- }
50-
5131const getProps = node => {
5232 const map = node . data . get ( 'props' )
5333 if ( typeof map . toJS !== 'function' ) return map
@@ -61,7 +41,7 @@ const Wrapper = ({
6141 component,
6242 Component,
6343 props,
64- fields
44+ fields = { }
6545} ) => {
6646 return (
6747 < div >
@@ -91,11 +71,19 @@ const Wrapper = ({
9171 )
9272}
9373
94- // placeholder
95- const components = {
96- YouTube,
97- Gist,
98- Tweet
74+ const Node = ( { type, next, ...props } ) => {
75+ const { components } = useContext ( Context )
76+ const Component = components [ type ]
77+ if ( ! Component ) return next ( )
78+
79+ return (
80+ < Wrapper
81+ { ...props }
82+ Component = { Component }
83+ component = { type }
84+ fields = { Component . propertyControls }
85+ />
86+ )
9987}
10088
10189export default ( opts = { } ) => ( {
@@ -104,30 +92,21 @@ export default (opts = {}) => ({
10492 } ,
10593 commands : {
10694 insertJSXBlock,
107- insertYouTube,
108- insertTweet,
109- insertGist,
11095 setJSXProps
11196 } ,
11297 renderNode : ( props , editor , next ) => {
11398 const { node } = props
11499 if ( node . type !== 'jsx-void' ) return next ( )
115100 const type = node . data . get ( 'type' )
116101
117- if ( components [ type ] ) {
118- const Component = components [ type ]
119- return (
120- < Wrapper
121- { ...props }
122- editor = { editor }
123- Component = { Component }
124- component = { type }
125- props = { getProps ( node ) }
126- fields = { Component . propertyControls }
127- />
128- )
129- }
130-
131- return next ( )
102+ return (
103+ < Node
104+ { ...props }
105+ next = { next }
106+ editor = { editor }
107+ type = { type }
108+ props = { getProps ( node ) }
109+ />
110+ )
132111 }
133112} )
0 commit comments