/
index.js
175 lines (163 loc) · 5.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import styled, { createGlobalStyle } from "styled-components";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import { Editor as CustomClassicEditor } from "./build/ckeditor";
import { Box } from "@strapi/design-system/Box";
import { prefixFileUrlWithBackendUrl, request, auth } from "@strapi/helper-plugin";
import MediaLib from "../MediaLib";
import pluginId from "../../pluginId";
import styles from "./styles";
import theme from "./theme";
const EditorStyle = createGlobalStyle`
${styles}
${({ strapiTheme }) => strapiTheme}
${({ custom }) => custom}
.ck-editor__styled__container{
position: relative;
width:100%;
}
`;
const Wrapper = styled(Box)``;
const Editor = ({ onChange, name, value, disabled }) => {
//####### strapi media lib connector #############################################################################################
const [mediaLibVisible, setMediaLibVisible] = useState(false);
const [editor, setEditor] = useState();
const toggleMediaLib = (editor) => {
if (editor) {
setEditor(editor);
}
setMediaLibVisible((prev) => !prev);
};
const handleChangeAssets = (assets) => {
let newValue = value ? value : "";
assets.map((asset) => {
if (asset.mime.includes("image")) {
if (uploadCfg?.responsiveDimensions) {
let set = "";
let keys = Object.keys(asset.formats).sort((a, b) => {
return asset.formats[a].width - asset.formats[b].width;
});
keys?.map((k) => {
let str =
prefixFileUrlWithBackendUrl(asset.formats[k].url) + ` ${asset.formats[k].width}w,`;
set = set + str;
});
const imgTag = `<img src="${asset.url}" alt="${asset.alt}" srcset="${set}"></img>`;
newValue = `${newValue}${imgTag}`;
} else {
const imgTag = `<img src="${asset.url}" alt="${asset.alt}"></img>`;
newValue = `${newValue}${imgTag}`;
}
}
// Handle videos and other type of files by adding some code
});
onChange({ target: { name, value: newValue } });
toggleMediaLib();
};
//####### config #############################################################################################
const [config, setConfig] = useState();
const [pluginCfg, setPluginCfg] = useState({});
const [uploadCfg, setUploadCfg] = useState();
const uploadUrl = `${prefixFileUrlWithBackendUrl("/upload")}`;
const headers = { Authorization: "Bearer " + auth.getToken() };
useEffect(() => {
// load the editor config
(async () => {
const editor = await request(`/${pluginId}/config/editor`, { method: "GET" });
const plugin = await request(`/${pluginId}/config/plugin`, { method: "GET" });
const upload = await request(`/${pluginId}/config/uploadcfg`, { method: "GET" });
if (editor) {
setConfig({
...config,
editor: {
...editor,
language: editor.language ? editor.language : auth.getUserInfo().preferedLanguage,
strapiMediaLib: {
onToggle: toggleMediaLib,
label: "Media library",
},
strapiUpload: {
uploadUrl,
headers,
},
},
});
if (editor.language) {
if (editor.language.ui) {
import(
/* webpackMode: "eager" */ `./build/translations/${editor.language.ui}.js`
).catch(() => null);
}
if (editor.language.content) {
import(
/* webpackMode: "eager" */ `./build/translations/${editor.language.content}.js`
).catch(() => null);
}
if (typeof editor.language !== "object") {
import(/* webpackMode: "eager" */ `./build/translations/${editor.language}.js`).catch(
() => null
);
}
}
if (!editor.language) {
import(
/* webpackMode: "eager" */ `./build/translations/${
auth.getUserInfo().preferedLanguage
}.js`
).catch(() => null);
}
}
if (plugin) {
setPluginCfg({
...pluginCfg,
...plugin,
});
if (plugin.setAttribute !== false && localStorage.getItem("STRAPI_THEME")) {
document.documentElement.setAttribute("data-theme", localStorage.getItem("STRAPI_THEME"));
}
}
if (upload) {
setUploadCfg({
...uploadCfg,
...upload,
});
}
})();
return () => {};
}, []);
//###########################################################################################################
return (
<Wrapper className="ck-editor__styled__container">
<EditorStyle
custom={pluginCfg.styles}
strapiTheme={pluginCfg.strapiTheme !== false ? theme : ""}
/>
{config && (
<CKEditor
editor={CustomClassicEditor}
disabled={disabled}
data={value || ""}
onReady={(editor) => editor.setData(value || "")}
onChange={(event, editor) => {
const data = editor.getData();
onChange({ target: { name, value: data } });
}}
config={config?.editor}
/>
)}
<MediaLib isOpen={mediaLibVisible} onChange={handleChangeAssets} onToggle={toggleMediaLib} />
</Wrapper>
);
};
Editor.defaultProps = {
value: "",
disabled: false,
};
Editor.propTypes = {
onChange: PropTypes.func.isRequired,
name: PropTypes.string.isRequired,
value: PropTypes.string,
disabled: PropTypes.bool,
};
export default Editor;