Skip to content

Commit eeda3c1

Browse files
authored
Adjustments for Ruby Compatibility with MagicEditor (#58)
* Add fixes * Fixes for advisable app (#57) * Remove classes in collection template * Remove dompurifier * use className instead of class * Update magic-editor version * add react-dom to dependencies * Fix sanitizeHTML issue * remove yarn.lock * ignore yarn lock, dist * Use magic editor recent version * fix collection template * Use magic editor canary * Puse magic editor v1
1 parent 69cc74d commit eeda3c1

13 files changed

+171
-6479
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.env*
22
node_modules
33
package-lock.json
4+
yarn.lock
45
yarn-error.log
5-
.DS_Store
6+
.DS_Store
7+
dist

dist/main.js

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"dependencies": {
2424
"@emotion/hash": "^0.8.0",
2525
"axios": "0.20.0",
26-
"isomorphic-dompurify": "^0.6.0",
2726
"lodash.kebabcase": "^4.1.1",
27+
"react-dom": "^17.0.2",
2828
"react-hotkeys-hook": "^3.3.2",
2929
"react-inlinesvg": "^2.3.0",
3030
"react-router-dom": "^5.2.0"

src/ChunkCollection.jsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,39 @@ export function ChunkCollection({
105105
{typeof children === "function"
106106
? children(getChunk, chunk, index)
107107
: children}
108-
</div>
108+
</div>
109109
</ChunkCollectionContext.Provider>
110110
))}
111111
{chunks.length && (
112112
<ChunkCollectionContext.Provider
113113
key={chunks[0].identifier + "dummy"}
114114
value={placeholderChunk}
115115
>
116-
<template
116+
<Template
117117
className={computeClassName(
118-
itemClass,
119118
"chunks-col-placeholder-wrapper"
120119
)}
121120
>
122121
{typeof children === "function"
123122
? children(getChunk, placeholderChunk, 0)
124123
: children}
125-
</template>
124+
</Template>
126125
</ChunkCollectionContext.Provider>
127126
)}
128127
</div>
129128
);
130129
}
131130

131+
function Template({ children, ...attrs }) {
132+
console.log
133+
return (
134+
<em-template
135+
{...attrs}
136+
style={{display: "none"}}
137+
>
138+
{children}
139+
</em-template>
140+
);
141+
}
142+
132143
export default ChunkCollection;

src/Editmode.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ export function Editmode({
4444
const script = document.createElement("script");
4545

4646
script.src =
47-
"https://unpkg.com/editmode-magic-editor@^0/dist/magic-editor.js";
48-
script.setAttribute("async", "");
49-
document.body.append(script);
50-
47+
"https://unpkg.com/editmode-magic-editor@~1/dist/magic-editor.js";
48+
49+
if (!window["magicEditorInjected"]) {
50+
script.setAttribute("async", "");
51+
document.body.append(script);
52+
window["magicEditorInjected"] = true
53+
}
54+
5155
if (!cachedItem) {
5256
api
5357
.get(`/projects/${projectId}`)

src/useChunk.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import {
88
computeContentKey,
99
getCachedData,
1010
storeCache,
11+
setDefaultContent,
12+
tryParse
1113
} from "./utilities";
1214

1315
export function useChunk(
1416
defaultContent,
1517
{ identifier, type, contentKey, field }
1618
) {
17-
const { projectId, defaultChunks, branch, next } =
18-
useContext(EditmodeContext);
19+
defaultContent = setDefaultContent(defaultContent)
20+
const { projectId, defaultChunks, branch, next } = useContext(EditmodeContext);
1921
let [chunk, setChunk] = useState(undefined);
2022

2123
if (!contentKey) {
@@ -52,22 +54,25 @@ export function useChunk(
5254
if (branchId) cacheId += branchId;
5355
let cachedChunk = getCachedData(cacheId);
5456
let newChunk = cachedChunk
55-
? JSON.parse(cachedChunk)
56-
: fallbackChunk || {
57-
chunk_type: type || "single_line_text",
58-
content: defaultContent,
59-
content_key: contentKey,
60-
};
57+
&& tryParse(cachedChunk)
58+
|| fallbackChunk
59+
|| {
60+
chunk_type: type || "single_line_text",
61+
content: defaultContent,
62+
content_key: contentKey,
63+
};
6164

6265
if (newChunk) setChunk(newChunk);
6366

64-
// Fetch new data
67+
// Fetch new data
6568
let error;
6669
api
6770
.get(url)
6871
.then((res) => {
69-
storeCache(cacheId, res.data);
70-
if (!cachedChunk) setChunk(res.data);
72+
if (!res.data.message) {
73+
storeCache(cacheId, res.data);
74+
if (!cachedChunk) setChunk(res.data);
75+
}
7176
}) // Store chunk to localstorage
7277
.catch((error) => console.log(error)); // Set error state
7378

src/useGetChunk.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { storeCache, getCachedData, api } from "./utilities";
1+
import { storeCache, getCachedData, api, tryParse } from "./utilities";
22
import { EditmodeContext } from "./EditmodeContext";
33
import { useEffect, useState, useContext } from "react";
44

5-
export const useGetChunk = (identifier, field = "") => {
5+
export const useGetChunk = (identifier, field = "", fallback = "") => {
66
const { projectId, defaultChunks, next } = useContext(EditmodeContext);
77
const [project, setProject] = useState(projectId);
88
const [chunk, setChunk] = useState(undefined);
@@ -17,7 +17,11 @@ export const useGetChunk = (identifier, field = "") => {
1717
}
1818

1919
const cachedChunk = getCachedData(cacheId);
20-
if (cachedChunk) setChunk(JSON.parse(cachedChunk));
20+
const newChunk = cachedChunk && tryParse(cachedChunk) || {
21+
chunk_type: "single_line_text",
22+
content: fallback,
23+
}
24+
setChunk(newChunk)
2125

2226
let url = `chunks/${identifier}?project_id=${project}`;
2327

src/utilities/computeClassName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function computeClassName(className, defaultClassName) {
1+
export function computeClassName(className = "", defaultClassName = "") {
22
className = className ? `${className} ${defaultClassName}` : defaultClassName;
33
return className;
44
}

src/utilities/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ export { transformImage } from "./transformImage";
1010
export { computeClassName } from "./computeClassName";
1111
export { api } from "./api";
1212
export { renderChunk } from "./renderChunk.jsx";
13+
export { setDefaultContent } from "./setDefaultContent"
1314
export { filterByTag } from "./filterTags";
15+
16+
export const tryParse = (string) => {
17+
try {
18+
return JSON.parse(string)
19+
} catch (error) {
20+
return null
21+
}
22+
}

src/utilities/renderChunk.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const renderChunk = (data, props, cacheId) => {
1515
"data-chunk-type": chunk.chunk_type,
1616
"data-chunk-cache-id": cacheId,
1717
"key": chunk.identifier,
18-
class: props.className
18+
"class": props.className || ""
1919
}
2020

2121
switch (chunk.chunk_type) {
@@ -31,7 +31,7 @@ export const renderChunk = (data, props, cacheId) => {
3131
{...defaultprops}
3232
dangerouslySetInnerHTML={{__html: parsedChunk}}
3333
{...props}
34-
class={"editmode-richtext-editor " + props.className}
34+
class={"editmode-richtext-editor " + (props.className || "")}
3535
/>);
3636
case "image":
3737
if (isSvg(chunk.content)) {

0 commit comments

Comments
 (0)