@@ -5,7 +5,13 @@ import { omit } from 'lodash';
55import { css } from '@emotion/react' ;
66import { Range , setNodes } from 'slate' ;
77import { zIndex } from 'decap-cms-ui-default' ;
8- import { ParagraphPlugin , PlateElement , useEditorRef } from 'platejs/react' ;
8+ import {
9+ ParagraphPlugin ,
10+ PlateElement ,
11+ useEditorRef ,
12+ useEditorSelection ,
13+ useEditorState ,
14+ } from 'platejs/react' ;
915
1016import { useEditorContext } from '../../editorContext' ;
1117
@@ -28,21 +34,22 @@ function InsertionPoint(props) {
2834
2935function ShortcodeElement ( props ) {
3036 const editor = useEditorRef ( ) ;
31- const { element, dataKey = 'shortcodeData' , children } = props ;
37+ const editorState = useEditorState ( ) ;
38+ const { attributes, element, dataKey = 'shortcodeData' , children } = props ;
3239 const { editorControl : EditorControl , editorComponents } = useEditorContext ( ) ;
33- const plugin = editorComponents . get ( element . id ) ;
40+ const plugin = editorComponents . get ( element . data . shortcode ) ;
3441 const fieldKeys = [ 'id' , 'fromBlock' , 'toBlock' , 'toPreview' , 'pattern' , 'icon' ] ;
3542
3643 const field = fromJS ( omit ( plugin , fieldKeys ) ) ;
3744 const [ value , setValue ] = useState ( fromJS ( element ?. data [ dataKey ] ?? { id : '' } ) ) ;
3845
46+ const selection = useEditorSelection ( ) ;
3947 const path = editor . api . findPath ( element ) ;
4048 const isSelected =
41- editor . selection &&
42- path &&
43- Range . isRange ( editor . selection ) &&
44- Range . includes ( editor . selection , path ) ;
49+ selection && path && Range . isRange ( selection ) && Range . includes ( selection , path ) ;
4550 const insertBefore = path [ 0 ] === 0 ;
51+ const insertAfter =
52+ path [ 0 ] === editorState . children . length - 1 || editor . isVoid ( editorState . children [ path [ 0 ] + 1 ] ) ;
4653
4754 function handleChange ( fieldName , value , metadata ) {
4855 const newProperties = {
@@ -65,32 +72,41 @@ function ShortcodeElement(props) {
6572 ) ;
6673 }
6774
75+ function handleInsertAfter ( e ) {
76+ e . preventDefault ( ) ;
77+ e . stopPropagation ( ) ;
78+
79+ editor . tf . insertNodes (
80+ { type : ParagraphPlugin . key , children : [ { text : '' } ] } ,
81+ { select : true } ,
82+ ) ;
83+ }
84+
6885 return (
69- < >
70- < PlateElement asChild { ...props } contentEditable = { false } >
71- < StyledDiv >
72- { insertBefore && < InsertionPoint onClick = { handleInsertBefore } /> }
73- < EditorControl
74- css = { css `
75- margin-top : 0 ;
76- margin-bottom : 16px ;
86+ < PlateElement asChild { ...props } >
87+ { insertBefore && < InsertionPoint onClick = { handleInsertBefore } /> }
88+ < StyledDiv { ...attributes } contentEditable = { false } >
89+ < EditorControl
90+ css = { css `
91+ margin-top : 0 ;
92+ margin-bottom : 16px ;
7793
78- & : first-of-type {
79- margin-top : 0 ;
80- }
81- ` }
82- value = { value }
83- field = { field }
84- onChange = { handleChange }
85- isEditorComponent = { true }
86- onValidateObject = { ( ) => { } }
87- isNewEditorComponent = { element . data ?. shortcodeNew }
88- isSelected = { isSelected }
89- />
90- { children }
91- </ StyledDiv >
92- </ PlateElement >
93- </ >
94+ & : first-of-type {
95+ margin-top : 0 ;
96+ }
97+ ` }
98+ value = { value }
99+ field = { field }
100+ onChange = { handleChange }
101+ isEditorComponent = { true }
102+ onValidateObject = { ( ) => { } }
103+ isNewEditorComponent = { element . data ?. shortcodeNew }
104+ isSelected = { isSelected }
105+ />
106+ { children }
107+ </ StyledDiv >
108+ { insertAfter && < InsertionPoint onClick = { handleInsertAfter } /> }
109+ </ PlateElement >
94110 ) ;
95111}
96112
0 commit comments