Skip to content

Commit 76ecbb5

Browse files
authored
Merge pull request #3 from Editmode-app/useChunk
2 parents f495593 + 1fd3776 commit 76ecbb5

15 files changed

+184
-97
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env*
2+
dist
23
node_modules
34
package-lock.json

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

README.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Editmode allows you to turn plain text in your React app into easily inline-editable bits of content that can be managed by anyone with no technical knowledge.
44

55
## Installation
6+
67
Use npm to install Editmode:
8+
79
```
810
npm install editmode-react
911
```
@@ -12,40 +14,46 @@ npm install editmode-react
1214

1315
### Step 1:
1416

15-
Within your React app, navigate to the index file within your src directory.
16-
Import the Editmode wrapper and wrap your App within.
17+
Within your React app, navigate to the index file within your src directory.
18+
Import the Editmode wrapper and wrap your App within.
1719

18-
```
20+
```js
1921
import { Editmode } from "editmode-react";
2022

23+
// 👉 `project_id` can be found in the URL:
24+
// https://editmode.com/projects/{project_id}/chunks
25+
2126
ReactDOM.render(
2227
<React>
23-
<Editmode>
24-
<App />
25-
</Editmode>
28+
<Editmode projectId={project_id}>
29+
<App />
30+
</Editmode>
2631
</React>,
27-
document.getElementById("root")
32+
document.getElementById("root")
2833
);
2934
```
3035

31-
3236
### Step 2:
3337

3438
#### Rendering a chunk:
3539

36-
If you have already created the chunk you would like to render on the Editmode CMS, you can simply pass the identifier as a prop and begin editing.
40+
If you have already created the chunk you would like to render on the Editmode CMS, you can simply pass the identifier as a prop and begin editing.
3741
You can provide default content as a fallback should anything go wrong trying to retrieve the data from the API:
3842

39-
```
43+
```js
4044
import { Chunk } from "editmode-react";
4145

4246
function Example() {
43-
return (
44-
<div>
45-
<p> <Chunk identifier="cnk_123"/> </p>
46-
<p> <Chunk identifier="cnk_321"> I have default content </Chunk> </p>
47-
</div>
48-
);
47+
return (
48+
<div>
49+
<p>
50+
<Chunk identifier="cnk_123" />
51+
</p>
52+
<p>
53+
<Chunk identifier="cnk_321"> I have default content </Chunk>
54+
</p>
55+
</div>
56+
);
4957
}
5058
```
5159

@@ -56,23 +64,24 @@ Alternatively, if you are using one of our **text editor plugins** and would lik
5664
Chunk collections are simply a way to categorise chunks and can be used to render repeatable content.
5765
Each collection can contain many properties and each property can hold different types of information.
5866

59-
A good use case example would be creating a "Team Member" collection. It may have `Full Name`, `Title` and `Headshot` properties. Within your React app, you may want to display the name, title and headshot of all your team members (ie all chunks within the Team Member collection). You can do this by passing the chunk collection identifier as a prop to the ChunkCollection component. To render the name, title and headshot for each team member, pass the identifiers for each property as a prop to the ChunkProperty component:
67+
A good use case example would be creating a "Team Member" collection. It may have `Full Name`, `Title` and `Headshot` properties. Within your React app, you may want to display the name, title and headshot of all your team members (ie all chunks within the Team Member collection). You can do this by passing the chunk collection identifier as a prop to the ChunkCollection component. To render the name, title and headshot for each team member, pass the identifiers for each property as a prop to the ChunkFieldValue component:
6068

61-
```
62-
import { ChunkCollection, ChunkProperty } from "editmode-react";
69+
```js
70+
import { ChunkCollection, ChunkFieldValue } from "editmode-react";
6371

6472
function Example() {
6573
return (
6674
<section className="meet_the_team">
6775
<ChunkCollection identifier="lst_123" className="team_member">
68-
<h2> <ChunkProperty identifier="prop_001" className="name"/> <h2>
69-
<h5> <ChunkProperty identifier="prop_002" className="title"/> </h5>
70-
<ChunkProperty identifier="prop_003" className="headshot"/>
76+
<h2> <ChunkFieldValue identifier="prop_001" className="name"/> <h2>
77+
<h5> <ChunkFieldValue identifier="prop_002" className="title"/> </h5>
78+
<ChunkFieldValue identifier="prop_003" className="headshot"/>
7179
</ChunkCollection>
7280
</section>
7381
);
7482
}
7583
```
84+
7685
This will render editable headings containing the name and title and an image containing the headshot for every person in the "Team Member" collection.
7786

7887
### Step 3:
@@ -94,6 +103,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
94103

95104
<!-- markdownlint-enable -->
96105
<!-- prettier-ignore-end -->
106+
97107
<!-- ALL-CONTRIBUTORS-LIST:END -->
98108

99-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
109+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

dist/main.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
"author": "Megan Ennis",
2424
"license": "ISC",
2525
"dependencies": {
26+
"@emotion/hash": "^0.8.0",
2627
"axios": "^0.19.2",
2728
"dompurify": "^2.0.11",
29+
"lodash-es": "^4.17.15",
2830
"react": "^16.13.1",
2931
"react-dom": "^16.13.1"
3032
},

src/Chunk.jsx

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
1-
import React from "react";
2-
import axios from "axios";
3-
import { renderChunk } from "./utils/renderChunk.jsx";
4-
import { BranchContext } from "./Editmode.jsx";
1+
// @ts-check
52

6-
class Chunk extends React.Component {
7-
constructor(props) {
8-
super();
9-
this.identifier = props.identifier;
10-
this.state = {
11-
chunk_data: {}
12-
};
13-
}
3+
import { useChunk } from "./useChunk";
144

15-
componentDidMount() {
16-
let branch = this.context;
17-
axios
18-
.get(`https://www.editmode.app/api/v1/chunks/${this.identifier}`, branch)
19-
.then(res => {
20-
this.setState({
21-
chunk_data: res.data
22-
});
23-
})
24-
.catch(err =>
25-
console.log(
26-
`Something went wrong trying to retrieve chunk data: ${err}.Have you provided the correct Editmode identifier as a prop to your Chunk component instance?`
27-
)
28-
);
29-
}
5+
export function Chunk({ children, className, identifier }) {
6+
const chunk = useChunk(children, { identifier });
307

31-
render() {
32-
return (
33-
<>
34-
{this.state.chunk_data.identifier
35-
? renderChunk(this.state.chunk_data, this.props.className)
36-
: this.props.children}
37-
</>
38-
);
39-
}
8+
return chunk.element;
409
}
4110

42-
Chunk.contextType = BranchContext;
43-
44-
export default Chunk;
11+
// Here for backwards-compatibility, but named exports are preferred
12+
export default Chunk;

src/ChunkCollection.jsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1+
// @ts-check
2+
13
import React from "react";
24
import axios from "axios";
3-
import { BranchContext } from "./Editmode.jsx";
4-
export const CollectionContext = React.createContext();
5+
import { Context } from "./Context";
6+
7+
export const CollectionContext = React.createContext(null);
58

69
class ChunkCollection extends React.Component {
710
constructor(props) {
8-
super();
11+
super(props);
12+
913
this.identifier = props.identifier;
1014
this.state = {
11-
chunks: []
15+
chunks: [],
1216
};
1317
}
1418

1519
componentDidMount() {
1620
axios
17-
.get(`https://www.editmode.app/api/v1/chunks/`, {
21+
.get(`https://api.editmode.com/`, {
1822
params: { collection_identifier: this.props.identifier },
19-
em_branch: this.context.em_branch
23+
// @ts-ignore
24+
em_branch: this.context.branch,
2025
})
21-
.then(res => {
26+
.then((res) => {
2227
this.setState({ chunks: res.data.chunks });
2328
})
24-
.catch(err =>
29+
.catch((err) =>
2530
console.log(
2631
`Something went wrong trying to retrieve chunk collection: ${err}. Have you provided the correct Editmode identifier as a prop to your ChunkCollection component instance?`
2732
)
@@ -30,7 +35,7 @@ class ChunkCollection extends React.Component {
3035

3136
render() {
3237
return this.state.chunks.length ? (
33-
this.state.chunks.map(cnk => {
38+
this.state.chunks.map((cnk) => {
3439
return (
3540
<CollectionContext.Provider value={cnk.content} key={cnk.identifier}>
3641
<div
@@ -48,6 +53,6 @@ class ChunkCollection extends React.Component {
4853
}
4954
}
5055

51-
ChunkCollection.contextType = BranchContext;
56+
ChunkCollection.contextType = Context;
5257

53-
export default ChunkCollection;
58+
export default ChunkCollection;

src/ChunkProperty.jsx renamed to src/ChunkFieldValue.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { renderChunk } from "./utils/renderChunk.jsx";
33
import { CollectionContext } from "./ChunkCollection.jsx";
44

5-
export default class ChunkProperty extends React.Component {
5+
export default class ChunkFieldValue extends React.Component {
66
constructor(props) {
77
super();
88
this.identifier = props.identifier;
@@ -23,4 +23,4 @@ export default class ChunkProperty extends React.Component {
2323
</CollectionContext.Consumer>
2424
);
2525
}
26-
}
26+
}

src/Context.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createContext } from "react";
2+
3+
export const Context = createContext({
4+
branch: null,
5+
projectId: null,
6+
});

src/Editmode.jsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
import React from "react";
2-
export const BranchContext = React.createContext();
1+
// @ts-check
2+
import React, { useEffect } from "react";
3+
import { Context } from "./Context";
34

4-
function Editmode(props) {
5-
let script = document.createElement("script");
6-
script.src = "https://www.editmode.app/assets/chunks.js";
7-
script.async = true;
8-
document.body.append(script);
5+
export function Editmode({ children, projectId }) {
6+
if (!projectId) {
7+
throw new Error("<Editmode projectId={...}> is missing a valid projectId");
8+
}
99

10-
let params = new URL(document.location).searchParams;
11-
let em_branch = params.get("em_branch");
10+
useEffect(() => {
11+
window["chunksProjectIdentifier"] = "prj_yvskxAScvL8x";
12+
13+
const script = document.createElement("script");
14+
script.src = "https://static.editmode.com/editmode@^1.0.0/dist/editmode.js";
15+
script.async = true;
16+
document.body.append(script);
17+
}, []);
18+
19+
let params = new URL(document.location.href).searchParams;
20+
let branch = params.get("em_branch");
1221

1322
return (
14-
<BranchContext.Provider value={{ em_branch }}>
15-
{props.children}
16-
</BranchContext.Provider>
23+
<Context.Provider value={{ branch, projectId }}>
24+
{children}
25+
</Context.Provider>
1726
);
1827
}
19-
export default Editmode;
28+
export default Editmode;

0 commit comments

Comments
 (0)