Skip to content

Commit

Permalink
Merge pull request #113 from dependentmadani/migrate-typescript
Browse files Browse the repository at this point in the history
fix(undefined data): undefined issue in normalizeData member function
  • Loading branch information
neSpecc authored Jul 4, 2024
2 parents dbaf97d + 2c0d379 commit 517b6b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/header",
"version": "2.8.4",
"version": "2.8.5",
"keywords": [
"codex editor",
"header",
Expand Down
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
*
Expand All @@ -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 || '';
Expand Down Expand Up @@ -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;
}

Expand Down

1 comment on commit 517b6b3

@DLunaWALB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this commit broke the Headers on my app's editor. Not sure how to fix it.
Everything worked fine for months up until a few hours ago, corresponding with this fix.

Any tips on what to do ?

Attached is a screenshot of the issue.
Header

Please sign in to comment.