Skip to content

Commit

Permalink
Merge pull request #103 from editor-js/improve-normalize-data
Browse files Browse the repository at this point in the history
Feat (data): support checklist data format
  • Loading branch information
e11sy authored Nov 5, 2024
2 parents 45aee3e + 42dbe15 commit 2d84882
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,19 @@
},
},
{
type: 'checklist',
type: 'List',
data: {
items: [
{
text: "This is a block-styled editor",
text: "This is Checklist tool data",
checked: true
},
{
text: "Clean output data",
text: "That would be displayed",
checked: false
},
{
text: "Simple and powerful API",
text: "In Nested List tool",
checked: true
}
]
Expand Down
24 changes: 24 additions & 0 deletions src/types/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ export interface OldListData {
items: string[];
}

/**
* Interface that represents old checklist data format
*/
export interface OldChecklistData {
/**
* Checklist items
*/
items: OldChecklistItem[];
}

/**
* Interface that represents old checklist item format
*/
interface OldChecklistItem {
/**
* Text of the checklist item
*/
text: string;
/**
* Checked state of the checklist item
*/
checked: boolean;
}

/**
* List item within the output data
*/
Expand Down
44 changes: 37 additions & 7 deletions src/utils/normalizeData.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import type { OldListData, ListData, ListItem } from '../types/ListParams';
import type { OldListData, ListData, ListItem, OldChecklistData } 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
* Method that checks if data is result of the Old list tool save mtehod
* @param data - data of the OldList, Checklist or NestedList tool
* @returns true if data related to the List tool, false otherwise
*/
function instanceOfListData(data: ListData | OldListData): data is OldListData {
function instanceOfOldListData(data: ListData | OldListData | OldChecklistData): data is OldListData {
return (typeof data.items[0] === 'string');
}

/**
* Method that checks if data is result of the Old checklist tool save method
* @param data - data of the Checklist, OldList or NestedList tool
* @returns true if data is related to the Checklist tool, false otherwise
*/
function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData): data is OldChecklistData {
return (
typeof data.items[0] !== 'string'
&& 'text' in data.items[0]
&& 'checked' in data.items[0]
&& typeof data.items[0].text === 'string'
&& typeof data.items[0].checked === 'boolean'
);
}

/**
* 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 {
export default function normalizeData(data: ListData | OldListData | OldChecklistData): ListData {
const normalizedDataItems: ListItem[] = [];

if (instanceOfListData(data)) {
if (instanceOfOldListData(data)) {
data.items.forEach((item) => {
normalizedDataItems.push({
content: item,
Expand All @@ -30,6 +45,21 @@ export default function normalizeData(data: ListData | OldListData): ListData {
style: data.style,
items: normalizedDataItems,
};
} else if (instanceOfChecklistData(data)) {
data.items.forEach((item) => {
normalizedDataItems.push({
content: item.text,
meta: {
checked: item.checked,
},
items: [],
});
});

return {
style: 'checklist',
items: normalizedDataItems,
};
} else {
return data;
}
Expand Down

0 comments on commit 2d84882

Please sign in to comment.