Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (data): support checklist data format #103

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 36 additions & 6 deletions src/utils/normalizeData.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
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 related to the List Tool
* @param data - data of the List or NestedList tool
Copy link
Contributor

Choose a reason for hiding this comment

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

description is unclear

* @returns true if data related to the List tool, false otherwise
*/
function instanceOfListData(data: ListData | OldListData): data is OldListData {
function instanceOfListData(data: ListData | OldListData | OldChecklistData): data is OldListData {
return (typeof data.items[0] === 'string');
Copy link
Contributor

Choose a reason for hiding this comment

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

function name means checking passed value to be ListData (new). But it checks for the OldListData.

}

/**
* Method that checks if data is related to the Checklist tool
* @param data - data of the Checklist of NestedList tool
* @returns true if data is related to the Checklist tool, false otherwise
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param data - data of the Checklist of NestedList tool
* @param data - data of the Checklist or NestedList tool

*/
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)) {
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
Loading