From 6e2bad45311d95d96d0d5610a7cc5207fde2374d Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Sun, 3 Nov 2024 17:23:07 +0300 Subject: [PATCH 1/3] added normalization of the checklist data --- index.html | 8 +++---- src/types/ListParams.ts | 24 ++++++++++++++++++++ src/utils/normalizeData.ts | 46 +++++++++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index a54b7a7..5b62daa 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 8461ca9..8d9fef5 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 6299d8c..b1e9c78 100644 --- a/src/utils/normalizeData.ts +++ b/src/utils/normalizeData.ts @@ -1,20 +1,37 @@ -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 + * @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'); } +/** + * 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 + */ +function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData): data is OldChecklistData { + console.log('check for checklist'); + + 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)) { @@ -30,6 +47,23 @@ export default function normalizeData(data: ListData | OldListData): ListData { style: data.style, items: normalizedDataItems, }; + } else if (instanceOfChecklistData(data)) { + console.log('data normaizedd'); + + data.items.forEach((item) => { + normalizedDataItems.push({ + content: item.text, + meta: { + checked: item.checked, + }, + items: [], + }); + }); + + return { + style: 'checklist', + items: normalizedDataItems, + }; } else { return data; } From bde7158172b93ae74c6bb8bebf300593fb91667b Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Sun, 3 Nov 2024 17:26:05 +0300 Subject: [PATCH 2/3] rm logs --- src/utils/normalizeData.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/utils/normalizeData.ts b/src/utils/normalizeData.ts index b1e9c78..2ca4785 100644 --- a/src/utils/normalizeData.ts +++ b/src/utils/normalizeData.ts @@ -15,8 +15,6 @@ function instanceOfListData(data: ListData | OldListData | OldChecklistData): da * @returns true if data is related to the Checklist tool, false otherwise */ function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData): data is OldChecklistData { - console.log('check for checklist'); - return ( typeof data.items[0] !== 'string' && 'text' in data.items[0] @@ -48,8 +46,6 @@ export default function normalizeData(data: ListData | OldListData | OldChecklis items: normalizedDataItems, }; } else if (instanceOfChecklistData(data)) { - console.log('data normaizedd'); - data.items.forEach((item) => { normalizedDataItems.push({ content: item.text, From 42dbe15e2ed523f968c66b40f1f52c435dbd242c Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:14:50 +0300 Subject: [PATCH 3/3] improve comments --- src/utils/normalizeData.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/normalizeData.ts b/src/utils/normalizeData.ts index 2ca4785..51b2e0a 100644 --- a/src/utils/normalizeData.ts +++ b/src/utils/normalizeData.ts @@ -1,17 +1,17 @@ import type { OldListData, ListData, ListItem, OldChecklistData } from '../types/ListParams'; /** - * Method that checks if data is related to the List Tool - * @param data - data of the List or NestedList 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 | OldChecklistData): data is OldListData { +function instanceOfOldListData(data: ListData | OldListData | OldChecklistData): data is OldListData { return (typeof data.items[0] === 'string'); } /** - * Method that checks if data is related to the Checklist tool - * @param data - data of the Checklist of NestedList tool + * 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 { @@ -32,7 +32,7 @@ function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData 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,