1+ /** @jsx jsx */
2+ import { jsx } from '@emotion/core'
13import React from 'react'
24import { Data } from 'slate'
5+ import Form from './Form'
6+ import Overlay from './Overlay'
37import YouTube from './YouTube'
48import Gist from './Gist'
59
6- const setJSXProps = ( editor , propsObject ) => {
7- const props = Data . create ( propsObject )
8- editor . setBlocks ( { data : { props } } )
10+ const setJSXProps = ( editor , dataObject ) => {
11+ const data = Data . create ( dataObject )
12+ editor . setBlocks ( { data } )
913}
1014
1115const insertJSXBlock = ( editor , type , props ) => {
1216 editor . insertBlock ( {
13- type,
17+ type : 'jsx-void' ,
1418 data : {
19+ type,
1520 props : Data . create ( props )
1621 }
1722 } )
1823}
1924
2025const insertYouTube = editor => {
21- editor . insertJSXBlock ( 'youtube ' , {
26+ editor . insertJSXBlock ( 'YouTube ' , {
2227 videoId : ''
2328 } )
2429}
2530
2631const insertGist = editor => {
27- editor . insertJSXBlock ( 'gist ' , {
32+ editor . insertJSXBlock ( 'Gist ' , {
2833 id : ''
2934 } )
3035}
@@ -35,6 +40,49 @@ const getProps = node => {
3540 return map . toJS ( )
3641}
3742
43+ const Wrapper = ( {
44+ editor,
45+ attributes,
46+ isSelected,
47+ component,
48+ Component,
49+ props,
50+ fields
51+ } ) => {
52+ return (
53+ < div >
54+ < div
55+ { ...attributes }
56+ style = { {
57+ position : 'relative' ,
58+ outline : isSelected ? '2px solid blue' : null
59+ } }
60+ >
61+ < Component { ...props } />
62+ { ! isSelected && < Overlay /> }
63+ </ div >
64+ { isSelected && (
65+ < Form
66+ fields = { fields }
67+ value = { props }
68+ onSubmit = { next => {
69+ editor . setJSXProps ( {
70+ type : component ,
71+ props : next
72+ } )
73+ } }
74+ />
75+ ) }
76+ </ div >
77+ )
78+ }
79+
80+ // placeholder
81+ const components = {
82+ YouTube,
83+ Gist
84+ }
85+
3886export default ( opts = { } ) => ( {
3987 commands : {
4088 insertJSXBlock,
@@ -44,16 +92,23 @@ export default (opts = {}) => ({
4492 } ,
4593 renderNode : ( props , editor , next ) => {
4694 const { node } = props
95+ if ( node . type !== 'jsx-void' ) return next ( )
96+ const type = node . data . get ( 'type' )
4797
48- switch ( node . type ) {
49- case 'youtube' :
50- return < YouTube { ...props } editor = { editor } props = { getProps ( node ) } />
51- break
52- case 'gist' :
53- return < Gist { ...props } editor = { editor } props = { getProps ( node ) } />
54- break
55- default :
56- return next ( )
98+ if ( components [ type ] ) {
99+ const Component = components [ type ]
100+ return (
101+ < Wrapper
102+ { ...props }
103+ editor = { editor }
104+ Component = { Component }
105+ component = { type }
106+ props = { getProps ( node ) }
107+ fields = { Component . propertyControls }
108+ />
109+ )
57110 }
111+
112+ return next ( )
58113 }
59114} )
0 commit comments