From e2472bae0091a590a3abf786a24a03bc84e632cb Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:04:38 +0300 Subject: [PATCH] imp(utils): improved normalize data util - now old nested list data is also normalized (requered meta field added) --- package.json | 2 +- src/types/ListParams.ts | 5 +++++ src/utils/normalizeData.ts | 25 ++++++++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 2dad172..1f50fbd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/list", - "version": "2.0.1", + "version": "2.0.3", "keywords": [ "codex editor", "list", diff --git a/src/types/ListParams.ts b/src/types/ListParams.ts index e377b15..5601545 100644 --- a/src/types/ListParams.ts +++ b/src/types/ListParams.ts @@ -29,6 +29,11 @@ export interface OldListData { items: string[]; } +/** + * Type that represents data of the List tool + */ +export type OldNestedListData = Omit; + /** * Interface that represents old checklist data format */ diff --git a/src/utils/normalizeData.ts b/src/utils/normalizeData.ts index 91221ef..2a4f3f2 100644 --- a/src/utils/normalizeData.ts +++ b/src/utils/normalizeData.ts @@ -1,20 +1,29 @@ -import type { OldListData, ListData, ListItem, OldChecklistData } from '../types/ListParams'; +import type { OldListData, ListData, ListItem, OldChecklistData, OldNestedListData } from '../types/ListParams'; /** * Method that checks if data is result of the Old list tool save mtehod - * @param data - data of the OldList, Checklist or Editorjs List tool + * @param data - data of the OldList, Checklist, OldNestedList or Editorjs List tool * @returns true if data related to the List tool, false otherwise */ -function instanceOfOldListData(data: ListData | OldListData | OldChecklistData): data is OldListData { +function instanceOfOldListData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldListData { return (typeof data.items[0] === 'string'); } +/** + * Method that checks if data is result of the Old nested list tool save method + * @param data - data of the OldList, Checklist, OldNestedList or Editorjs List tool + * @returns true if data is related to the Nested List tool, false otherwise + */ +function instanceOfOldNestedListData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldNestedListData { + return !('meta' in data); +} + /** * Method that checks if data is result of the Old checklist tool save method - * @param data - data of the Checklist, OldList or Editorjs List tool + * @param data - data of the Checklist, OldList, OldNestedList or Editorjs List tool * @returns true if data is related to the Checklist tool, false otherwise */ -function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData): data is OldChecklistData { +function instanceOfChecklistData(data: ListData | OldListData | OldChecklistData | OldNestedListData): data is OldChecklistData { return ( typeof data.items[0] !== 'string' && 'text' in data.items[0] @@ -62,6 +71,12 @@ export default function normalizeData(data: ListData | OldListData | OldChecklis meta: {}, items: normalizedDataItems, }; + } else if (instanceOfOldNestedListData(data)) { + return { + style: data.style, + meta: {}, + items: data.items, + }; } else { return data; }