-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from editor-js/add-converter-for-list-tool-data
Feat (data compatibility): Add normalization for List tool data
- Loading branch information
Showing
5 changed files
with
69 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { OldListData, ListData, ListItem } from '../types/ListParams'; | ||
|
||
/** | ||
* Method that checks if data is related to the List or NestedListTool | ||
* @param data - data of the List or NestedListTool | ||
* @returns true if data related to the List tool, false if to Nested List tool | ||
*/ | ||
function instanceOfListData(data: ListData | OldListData): data is OldListData { | ||
return (typeof data.items[0] === 'string'); | ||
} | ||
|
||
/** | ||
* Method that checks if passed data is related to the legacy format and normalizes it | ||
* @param data - data to be checked | ||
* @returns - normalized data, ready to be used by Nested List tool | ||
*/ | ||
export default function normalizeData(data: ListData | OldListData): ListData { | ||
const normalizedDataItems: ListItem[] = []; | ||
|
||
if (instanceOfListData(data)) { | ||
data.items.forEach((item) => { | ||
normalizedDataItems.push({ | ||
content: item, | ||
meta: {}, | ||
items: [], | ||
}); | ||
}); | ||
|
||
return { | ||
style: data.style, | ||
items: normalizedDataItems, | ||
}; | ||
} else { | ||
return data; | ||
} | ||
}; |