-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new plugin configuration method (#191)
BREAKING CHANGE: ckeditor.js|ts file is no longer supported for plugin configuration. The plugin now requires using the setPluginConfig method for configuration. Existing setups must be updated to call this method before the admin panel's bootstrap lifecycle. dontMergePresets and dontMergeTheme options have been removed. User-provided configuration objects now completely overwrite the default ones.
- Loading branch information
1 parent
5a189c4
commit 6625acc
Showing
27 changed files
with
262 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,8 @@ declare global { | |
strapi: { | ||
backendURL: string; | ||
}; | ||
SH_CKE: { | ||
IS_UPLOAD_RESPONSIVE: boolean; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,56 @@ | ||
import type { PluginConfig } from './types'; | ||
import { PLUGIN_ID } from '../utils'; | ||
import { defaultTheme } from '../theme'; | ||
import { defaultPreset } from './defaultPreset'; | ||
import type { PluginConfig, UserPluginConfig } from './types'; | ||
|
||
export async function getPluginConfig(): Promise<PluginConfig | null> { | ||
const config = await loadConfig().catch(error => console.error('CKEditor: ', error)); | ||
const PLUGIN_CONFIG: PluginConfig = { | ||
presets: { | ||
default: defaultPreset, | ||
}, | ||
theme: defaultTheme, | ||
}; | ||
|
||
return config || null; | ||
} | ||
/** | ||
* Sets a configuration for the plugin. | ||
* | ||
* @remarks | ||
* | ||
* - The function must be invoked before the admin panel's bootstrap lifecycle function. | ||
* | ||
* - Provided objects will overwrite the default configuration values. | ||
* | ||
* - The provided configuration will be frozen after the first invocation, preventing further modifications. | ||
* | ||
* @param userConfig - The configuration object provided by the user. | ||
* | ||
* @public | ||
*/ | ||
export function setPluginConfig(userConfig: UserPluginConfig): void { | ||
const { presets, theme } = userConfig || {}; | ||
|
||
async function loadConfig(): Promise<PluginConfig | null> { | ||
return new Promise((resolve, reject) => { | ||
const { backendURL } = window.strapi; | ||
const url = | ||
backendURL !== '/' | ||
? `${backendURL}/${PLUGIN_ID}/config/ckeditor` | ||
: `/${PLUGIN_ID}/config/ckeditor`; | ||
if (presets) { | ||
presets.forEach(preset => { | ||
PLUGIN_CONFIG.presets[preset.name] = preset; | ||
}); | ||
} | ||
|
||
const script = document.createElement('script'); | ||
script.id = 'ckeditor-config'; | ||
script.src = url; | ||
if (theme) { | ||
PLUGIN_CONFIG.theme = theme; | ||
} | ||
|
||
script.onload = () => resolve(window.SH_CKE_CONFIG); | ||
script.onerror = () => reject(new Error('Failed loading config script')); | ||
deepFreeze(PLUGIN_CONFIG); | ||
} | ||
|
||
document.body.appendChild(script); | ||
export function getPluginConfig(): PluginConfig { | ||
if (!Object.isFrozen(PLUGIN_CONFIG)) deepFreeze(PLUGIN_CONFIG); | ||
return PLUGIN_CONFIG; | ||
} | ||
|
||
function deepFreeze(obj: Object): Readonly<Object> { | ||
(Object.keys(obj) as (keyof typeof obj)[]).forEach(p => { | ||
if (typeof obj[p] === 'object' && obj[p] !== null && !Object.isFrozen(obj[p])) { | ||
deepFreeze(obj[p]); | ||
} | ||
}); | ||
|
||
return Object.freeze(obj); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.