Make passed config immutable (#1553)

* Make passed config immutable

* Update docs/CHANGELOG.md

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>

* Separate this.config initialization

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
Co-authored-by: Taly <vitalik7tv@yandex.ru>
This commit is contained in:
Tomoyuki Hata 2021-02-28 06:31:11 +09:00 committed by GitHub
parent 1772edf912
commit e5fe93eeb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 27 deletions

View file

@ -12,6 +12,7 @@
- `Fix` - Fix an unstable block cut process [#1489](https://github.com/codex-team/editor.js/issues/1489).
- `Fix` - Type definition of the Sanitizer config: the sanitize function now contains param definition [#1491](https://github.com/codex-team/editor.js/pull/1491).
- `Fix` - Fix unexpected behavior on an empty link pasting [#1348](https://github.com/codex-team/editor.js/issues/1348).
- `Fix` - Editor Config now immutable [#1552](https://github.com/codex-team/editor.js/issues/1552).
### 2.19.1

View file

@ -1,6 +1,6 @@
import $ from './dom';
import * as _ from './utils';
import { EditorConfig, OutputData, SanitizerConfig } from '../../types';
import { EditorConfig, SanitizerConfig } from '../../types';
import { EditorModules } from '../types-internal/editor-modules';
import I18n from './i18n';
import { CriticalError } from './errors/critical';
@ -116,11 +116,20 @@ export default class Core {
*/
public set configuration(config: EditorConfig|string) {
/**
* Process zero-configuration or with only holderId
* Make config object
* Place config into the class property
*
* @type {EditorConfig}
*/
if (!_.isObject(config)) {
config = {
if (_.isObject(config)) {
this.config = {
...config,
};
} else {
/**
* Process zero-configuration or with only holderId
* Make config object
*/
this.config = {
holder: config,
};
}
@ -128,19 +137,12 @@ export default class Core {
/**
* If holderId is preset, assign him to holder property and work next only with holder
*/
_.deprecationAssert(!!config.holderId, 'config.holderId', 'config.holder');
if (config.holderId && !config.holder) {
config.holder = config.holderId;
config.holderId = null;
_.deprecationAssert(!!this.config.holderId, 'config.holderId', 'config.holder');
if (this.config.holderId && !this.config.holder) {
this.config.holder = this.config.holderId;
this.config.holderId = null;
}
/**
* Place config into the class property
*
* @type {EditorConfig}
*/
this.config = config;
/**
* If holder is empty then set a default value
*/
@ -188,7 +190,7 @@ export default class Core {
this.config.hideToolbar = this.config.hideToolbar ? this.config.hideToolbar : false;
this.config.tools = this.config.tools || {};
this.config.i18n = this.config.i18n || {};
this.config.data = this.config.data || {} as OutputData;
this.config.data = this.config.data || { blocks: [] };
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.config.onReady = this.config.onReady || ((): void => {});
// eslint-disable-next-line @typescript-eslint/no-empty-function
@ -198,13 +200,8 @@ export default class Core {
/**
* Initialize default Block to pass data to the Renderer
*/
if (_.isEmpty(this.config.data)) {
this.config.data = {} as OutputData;
this.config.data.blocks = [ defaultBlockData ];
} else {
if (!this.config.data.blocks || this.config.data.blocks.length === 0) {
this.config.data.blocks = [ defaultBlockData ];
}
if (_.isEmpty(this.config.data) || !this.config.data.blocks || this.config.data.blocks.length === 0) {
this.config.data = { blocks: [ defaultBlockData ] };
}
this.config.readOnly = this.config.readOnly as boolean || false;
@ -212,14 +209,14 @@ export default class Core {
/**
* Adjust i18n
*/
if (config.i18n?.messages) {
I18n.setDictionary(config.i18n.messages);
if (this.config.i18n?.messages) {
I18n.setDictionary(this.config.i18n.messages);
}
/**
* Text direction. If not set, uses ltr
*/
this.config.i18n.direction = config.i18n?.direction || 'ltr';
this.config.i18n.direction = this.config.i18n?.direction || 'ltr';
}
/**