Skip to content

Commit

Permalink
Merge branch 'pr/54'
Browse files Browse the repository at this point in the history
  • Loading branch information
nshenderov committed Jan 12, 2023
2 parents 5276d31 + 0c6ad1e commit fea6a57
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
61 changes: 52 additions & 9 deletions admin/src/components/CKEditor/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useEffect, useState, useRef } from "react";
import { auth, prefixFileUrlWithBackendUrl, request } from "@strapi/helper-plugin";
import styled, { createGlobalStyle } from "styled-components";
import React, {useEffect, useRef, useState} from "react";
import {auth, prefixFileUrlWithBackendUrl, request} from "@strapi/helper-plugin";
import styled, {createGlobalStyle} from "styled-components";

import { Box } from "@strapi/design-system/Box";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import { Editor as CustomClassicEditor } from "./build/ckeditor";
import {Box} from "@strapi/design-system/Box";
import {CKEditor} from "@ckeditor/ckeditor5-react";
import {Editor as CustomClassicEditor} from "./build/ckeditor";
import MediaLib from "../MediaLib";
import PropTypes from "prop-types";
import pluginId from "../../pluginId";
Expand Down Expand Up @@ -66,6 +66,48 @@ const Editor = ({ onChange, name, value, disabled }) => {
toggleMediaLib();
};

const requestConfig = (key) =>
request(`/${pluginId}/config/${key}`, { method: "GET" });

const insertConfigScript = () => {
const url = strapi.backendURL !== '/'
? `${strapi.backendURL}/${pluginId}/editor-config-script.js`
: `/${pluginId}/editor-config-script.js`

var script = document.createElement('script');
script.id = 'editor-config'
script.src = url;
document.body.appendChild(script);
}

const waitForConfigToInitialize = async () => {
return new Promise((resolve) => {
(function checkConfigLoaded() {
if (typeof globalThis.ckEditorConfig !== "undefined") {
resolve(globalThis.ckEditorConfig)
} else
setTimeout(checkConfigLoaded, 5)
})();
});
}

const getEditorConfig = async () => {
// raw config/ckeditor.[js|ts] file
// Can be used with non-JSON serializable properties
insertConfigScript();
const configFromScript = await waitForConfigToInitialize();
if(configFromScript) {
return configFromScript;
}

// ckeditor snippet of config/plugins.[js|ts] serialized as JSON
// Can't be used with non-JSON serializable properties
else {
return requestConfig('editor');
}
}


//####### config #############################################################################################
const [config, setConfig] = useState();
const [pluginCfg, setPluginCfg] = useState({});
Expand All @@ -75,9 +117,10 @@ const Editor = ({ onChange, name, value, disabled }) => {
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" });
const editor = await getEditorConfig();
const plugin = await requestConfig('plugin');
const upload = await requestConfig('uploadcfg');


//read i18n locale
const urlSearchParams = new URLSearchParams(window.location.search);
Expand Down
8 changes: 7 additions & 1 deletion server/controllers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module.exports = {
const config = await strapi.plugin('ckeditor').service('config').getConfig(configKey);
ctx.send(config);
}

},

getEditorConfigScript: async (ctx) => {
const config = await strapi.plugin('ckeditor').service('config').getEditorConfigScript();
ctx.type = 'application/javascript';
ctx.send(config);
}
};
20 changes: 14 additions & 6 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
module.exports = [
{
method: 'GET',
path: '/config/:configKey',
handler: 'config.getConfig',
config: { policies: [] },
}
{
method: 'GET',
path: '/config/:configKey',
handler: 'config.getConfig',
config: {policies: []},
},
{
method: 'GET',
path: '/editor-config-script.js',
handler: 'config.getEditorConfigScript',
config: {
auth: false // Assume CKEditor config is not sensitive. We can't send a JWT token in a static script tag.
},
}
];
12 changes: 12 additions & 0 deletions server/services/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const fs = require("fs");

/**
* config.js configuration service
*/
Expand All @@ -9,6 +11,16 @@ module.exports = ({ strapi }) => {
getConfig(key = 'editor') {
return strapi.plugin('ckeditor').config(key) ?? {};
},
getEditorConfigScript() {
const appDir = process.cwd();
const isTSProject = fs.existsSync(`${appDir}/dist`);
const jsDir = isTSProject ? `${appDir}/dist` : appDir;

const filename = `${jsDir}/config/ckeditor.js`;
return fs.existsSync(filename)
? fs.readFileSync(filename)
: 'globalThis.ckEditorConfig = null' // empty script tag causes no problems
},
getUploadConfig(name) {
return strapi.plugin('upload').service(name) ?? {};
},
Expand Down

0 comments on commit fea6a57

Please sign in to comment.