diff --git a/index.html b/index.html index a54b7a77..5b62daac 100644 --- a/index.html +++ b/index.html @@ -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 } ] diff --git a/src/types/ListParams.ts b/src/types/ListParams.ts index 8461ca92..8d9fef5b 100644 --- a/src/types/ListParams.ts +++ b/src/types/ListParams.ts @@ -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 */ diff --git a/src/utils/normalizeData.ts b/src/utils/normalizeData.ts index 6299d8c1..51b2e0a1 100644 --- a/src/utils/normalizeData.ts +++ b/src/utils/normalizeData.ts @@ -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, @@ -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; }