@@ -2,13 +2,57 @@ import "./editor.css";
22
33const { __ } = wp . i18n ;
44const { useBlockProps, InspectorControls, InnerBlocks } = wp . blockEditor ;
5- const { useEntityProp } = wp . coreData ;
5+ const { useState , useEffect , useRef } = wp . element ;
66const { PanelBody } = wp . components ;
7+ const apiFetch = wp . apiFetch ;
78
89export default function Edit ( { context : { postType, postId } } ) {
910 const blockProps = useBlockProps ( ) ;
1011
11- const [ meta = { } ] = useEntityProp ( "postType" , postType , "meta" , postId ) ;
12+ const remotesList = useRef ( ) ;
13+
14+ const [ remoteFields , setRemoteFields ] = useState ( [ ] ) ;
15+
16+ useEffect ( ( ) => {
17+ if ( ! postId || ! postType ) return ;
18+
19+ let done = false ;
20+ const abortController = new AbortController ( ) ;
21+ apiFetch ( {
22+ path : `posts-bridge/v1/rcpt/${ postType } /${ postId } ` ,
23+ method : "GET" ,
24+ signal : abortController . signal ,
25+ } )
26+ . then ( ( fields ) => {
27+ setRemoteFields ( fields ) ;
28+ } )
29+ . catch ( ( ) => setRemoteFields ( [ ] ) )
30+ . finally ( ( ) => ( done = true ) ) ;
31+
32+ return ( ) => {
33+ ! done && abortController . abort ( ) ;
34+ } ;
35+ } , [ postId , postType ] ) ;
36+
37+ const copyFieldName = ( index ) => {
38+ const field = remoteFields [ index ] ;
39+ navigator . clipboard . writeText ( `{{${ field . name } }}` ) ;
40+
41+ Array . from ( remotesList . current . children ) . forEach ( ( child , i ) => {
42+ child . children [ 0 ] . style . display = index === i ? "block" : "none" ;
43+ child . children [ 1 ] . style . color =
44+ index === i
45+ ? "var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9))"
46+ : "inherit" ;
47+ } ) ;
48+
49+ setTimeout ( ( ) => {
50+ const child = remotesList . current . children [ index ] ;
51+ if ( ! child ) return ;
52+ child . children [ 0 ] . style . display = "none" ;
53+ child . children [ 1 ] . style . color = "inherit" ;
54+ } , 1500 ) ;
55+ } ;
1256
1357 return (
1458 < >
@@ -22,7 +66,7 @@ export default function Edit({ context: { postType, postId } }) {
2266 </ p >
2367 < p >
2468 { __ (
25- "There where do you want to place some remote value, use the `{{fieldName}}` marks. It doesn't matter where do you place marks, inside a paragraph, or maybe in a link, feel free to play with it." ,
69+ "There where you want to place some remote value, use the `{{fieldName}}` marks. It doesn't matter where do you place marks, inside a paragraph, or maybe in a link, feel free to play with it." ,
2670 "posts-bridge"
2771 ) }
2872 </ p >
@@ -34,12 +78,40 @@ export default function Edit({ context: { postType, postId } }) {
3478 </ p >
3579 </ PanelBody >
3680 < PanelBody
37- title = { __ ( "Registered remote fields" , "posts-bridge" ) }
81+ title = { __ ( "Remote fields" , "posts-bridge" ) }
3882 initialOpen = { false }
3983 >
40- < ul >
41- { Object . keys ( meta ) . map ( ( remote ) => (
42- < li > { remote } </ li >
84+ < ul ref = { remotesList } >
85+ { remoteFields . map ( ( field , index ) => (
86+ < li className = "remote-field" style = { { position : "relative" } } >
87+ < span
88+ style = { {
89+ display : "none" ,
90+ position : "absolute" ,
91+ top : "-2.5em" ,
92+ left : "-0.25em" ,
93+ background :
94+ "var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9))" ,
95+ color : "var(--wp-components-color-accent-inverted,#fff)" ,
96+ padding : "0.35em 0.85em" ,
97+ borderRadius : "12px" ,
98+ boxShadow : "1px 1px 3px #0005" ,
99+ } }
100+ >
101+ { __ ( "Copied!" , "posts-bridge" ) }
102+ </ span >
103+ < span
104+ onClick = { ( ) => copyFieldName ( index ) }
105+ style = { {
106+ cursor : "pointer" ,
107+ fontWeight : 600 ,
108+ transition : "color 300ms ease" ,
109+ } }
110+ >
111+ { field . name }
112+ </ span > { " " }
113+ | { field . schema . type }
114+ </ li >
43115 ) ) }
44116 </ ul >
45117 </ PanelBody >
0 commit comments