Skip to content

Commit d47bd92

Browse files
committed
feat: load rcpt payload on remote fields editor
1 parent 44b83e1 commit d47bd92

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

posts-bridge/custom-blocks/remote-fields/src/edit.jsx

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,57 @@ import "./editor.css";
22

33
const { __ } = wp.i18n;
44
const { useBlockProps, InspectorControls, InnerBlocks } = wp.blockEditor;
5-
const { useEntityProp } = wp.coreData;
5+
const { useState, useEffect, useRef } = wp.element;
66
const { PanelBody } = wp.components;
7+
const apiFetch = wp.apiFetch;
78

89
export 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>

posts-bridge/custom-blocks/remote-fields/src/render.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* @param array $attributes - A clean associative array of block attributes.
66
* @param array $block - All the block settings and attributes.
77
* @param string $content - The block inner HTML (usually empty unless using inner blocks).
8+
*
9+
* @package postsbridge
810
*/
911

1012
if ( ! defined( 'ABSPATH' ) ) {
1113
exit();
1214
}
1315

14-
$is_remote_frontend = shortcode_exists( 'posts_bridge_remote_fields' );
15-
if ( $is_remote_frontend ) {
16-
echo do_shortcode(
17-
"[posts_bridge_remote_fields]{$content}[/posts_bridge_remote_fields]"
18-
);
16+
$posts_bridge_is_remote_frontend = shortcode_exists( 'posts_bridge_remote_fields' );
17+
if ( $posts_bridge_is_remote_frontend ) {
18+
echo do_shortcode( "[posts_bridge_remote_fields]{$content}[/posts_bridge_remote_fields]" );
1919
} else {
2020
echo wp_kses_post( $content );
2121
}

0 commit comments

Comments
 (0)