diff --git a/package.json b/package.json index 28a4480..b923a9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/header", - "version": "2.8.4", + "version": "2.8.5", "keywords": [ "codex editor", "header", diff --git a/src/index.ts b/src/index.ts index 07be85f..79fb250 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ interface Level { */ interface ConstructorArgs { /** Previously saved data */ - data: HeaderData; + data: HeaderData | {}; /** User config for the tool */ config: HeaderConfig; /** Editor.js API */ @@ -136,6 +136,17 @@ export default class Header { }; } + /** + * Check if data is valid + * + * @param {any} data - data to check + * @returns {data is HeaderData} + * @private + */ + isHeaderData(data: any): data is HeaderData { + return (data as HeaderData).text !== undefined && (data as HeaderData).level !== undefined; + } + /** * Normalize input data * @@ -144,11 +155,11 @@ export default class Header { * @returns {HeaderData} * @private */ - normalizeData(data: HeaderData): HeaderData { - const newData: HeaderData = {text: '', level: this.defaultLevel.number }; + normalizeData(data: HeaderData | {}): HeaderData { + const newData: HeaderData = { text: '', level: this.defaultLevel.number }; - if (typeof data !== 'object') { - data = {text: '', level: this.defaultLevel.number}; + if (!this.isHeaderData(data)) { + return { text: '', level: this.defaultLevel.number}; } newData.text = data.text || ''; @@ -275,7 +286,7 @@ export default class Header { get data(): HeaderData { this._data.text = this._element.innerHTML; this._data.level = this.currentLevel.number; - + return this._data; }