Skip to content

Commit 449b878

Browse files
authored
remove submodule, add referrer to api header (#35)
1 parent 5fc9501 commit 449b878

File tree

10 files changed

+98
-4
lines changed

10 files changed

+98
-4
lines changed

src/ChunkCollection.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React, { useEffect, useState } from "react";
33

44
import { ChunkCollectionContext } from "./ChunkCollectionContext";
5-
import { api, getCachedData, storeCache } from './utils'
5+
import { api, getCachedData, storeCache } from './utilities'
66

77
export function ChunkCollection({
88
children,

src/ChunkFieldValue.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useContext } from "react";
22

3-
import { renderChunk } from "./utils";
3+
import { renderChunk } from "./utilities";
44
import { ChunkCollectionContext } from "./ChunkCollectionContext";
55

66
export function ChunkFieldValue({ children, identifier, ...props }) {

src/useChunk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { useContext, useEffect, useState, useMemo } from "react";
33

44
import { EditmodeContext } from "./EditmodeContext";
5-
import { api, renderChunk, computeContentKey, getCachedData, storeCache } from './utils'
5+
import { api, renderChunk, computeContentKey, getCachedData, storeCache } from './utilities'
66

77
export function useChunk(defaultContent, { identifier, type, contentKey }) {
88
const { projectId, defaultChunks } = useContext(EditmodeContext);

src/utilities/api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import axios from "axios";
2+
3+
export const api = axios.create({
4+
baseURL: "https://api.editmode.com/",
5+
headers: {
6+
Accept: "application/json",
7+
referrer: window.location.href
8+
},
9+
});

src/utilities/caching.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const getCachedData = (id) => {
2+
return localStorage.getItem(id);
3+
}
4+
5+
export const storeCache = (id, data) => {
6+
localStorage.setItem(id, JSON.stringify(data));
7+
}

src/utilities/computeContentKey.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hash from "@emotion/hash";
2+
import kebabCase from "lodash.kebabcase";
3+
4+
export function computeContentKey(content) {
5+
if (typeof content !== "string") {
6+
console.error(
7+
`Cannot compute chunk.content_key. Expected a string, received ${typeof content}.`
8+
);
9+
return;
10+
}
11+
12+
return `${kebabCase(content.slice(0, 32))}-${hash(content)}`;
13+
}

src/utilities/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { getCachedData, storeCache } from './caching'
2+
export { computeContentKey } from './computeContentKey'
3+
export { sanitizeContent } from './sanitizeContent'
4+
export { api } from './api'
5+
6+
export { renderChunk } from './renderChunk.jsx'

src/utilities/renderChunk.jsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from "react";
2+
import { sanitizeContent } from './'
3+
4+
export const renderChunk = (data, props) => {
5+
const { chunk, parsedChunk } = sanitizeContent(data, props)
6+
7+
const defaultprops = {
8+
"data-chunk": chunk.identifier,
9+
"data-chunk-editable": true,
10+
"data-chunk-content-key": chunk.content_key,
11+
"data-chunk-type": chunk.chunk_type,
12+
"key": chunk.identifier
13+
}
14+
15+
switch (chunk.chunk_type) {
16+
case "single_line_text":
17+
case "long_text":
18+
return (<em-span
19+
{...defaultprops}
20+
dangerouslySetInnerHTML={{__html: parsedChunk}}
21+
{...props}
22+
/>);
23+
case "rich_text":
24+
return (<em-span
25+
{...defaultprops}
26+
class="editmode-richtext-editor"
27+
dangerouslySetInnerHTML={{__html: parsedChunk}}
28+
{...props}
29+
/>);
30+
case "image":
31+
return (<img
32+
{...defaultprops}
33+
src={chunk.content}
34+
data-chunk-editable={false}
35+
alt=""
36+
{...props}
37+
/>);
38+
default:
39+
return <span {...props}>{parsedChunk}</span>;
40+
}
41+
};

src/utilities/sanitizeContent.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import DOMpurify from "isomorphic-dompurify";
2+
3+
export const sanitizeContent = (data, props) => {
4+
const sanitizedContent = DOMpurify.sanitize(data.content);
5+
const chunk = { ...data, content: sanitizedContent };
6+
const tokens = (chunk.content.match(/\{{(.*?)\}}/g)|| []).map(t => t.substr(2, t.length-4))
7+
8+
let parsedChunk = chunk.content;
9+
10+
if (tokens) {
11+
tokens.forEach(function(token) {
12+
const value = props.variables && props.variables[token] || ""
13+
const emVar = `<em-var data-chunk-variable="${token}" data-chunk-variable-value="${value}">${value}</em-var>`
14+
parsedChunk = parsedChunk.replace(`{{${token}}}`, emVar);
15+
});
16+
}
17+
18+
return { chunk, parsedChunk }
19+
}

src/utils

Submodule utils deleted from dcf3756

0 commit comments

Comments
 (0)