Skip to content

Commit

Permalink
Merge pull request #96 from editor-js/add-converter-for-list-tool-data
Browse files Browse the repository at this point in the history
Feat (data compatibility): Add normalization for List tool data
  • Loading branch information
e11sy authored Oct 30, 2024
2 parents 2f46c0e + 62e44c8 commit e04ffe3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
15 changes: 13 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,19 @@
}
]
}
}
]
},
{
type : 'List',
data : {
style : "checklist",
items : [
"This is List tool data",
"That would be displayed",
"In Nested List tool"
],
},
},
],
},
onReady: function(){
saveButton.click();
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ToolConfig
} from '@editorjs/editorjs/types/tools';
import { IconListBulleted, IconListNumbered, IconChecklist } from '@codexteam/icons';
import type { NestedListConfig, ListData, ListDataStyle, ListItem } from './types/ListParams';
import type { NestedListConfig, ListData, ListDataStyle, ListItem, OldListData } from './types/ListParams';
import ListTabulator from './ListTabulator';
import { CheckListRenderer, OrderedListRenderer, UnorderedListRenderer } from './ListRenderer';
import type { ListRenderer } from './types/ListRenderer';
Expand All @@ -18,11 +18,12 @@ import { type OlCounterType, OlCounterTypesMap } from './types/OlCounterType';
import './styles/list.pcss';
import './styles/input.pcss';
import stripNumbers from './utils/stripNumbers';
import normalizeData from './utils/normalizeData';

/**
* Constructor Params for Nested List Tool, use to pass initial data and settings
*/
export type ListParams = BlockToolConstructorOptions<ListData, NestedListConfig>;
export type ListParams = BlockToolConstructorOptions<ListData | OldListData, NestedListConfig>;

/**
* Default class of the component used in editor
Expand Down Expand Up @@ -210,7 +211,7 @@ export default class NestedList {
items: [],
};

this.data = Object.keys(data).length ? data : initialData;
this.data = Object.keys(data).length ? normalizeData(data) : initialData;

/**
* Assign default value of the property for the ordered list
Expand Down
2 changes: 1 addition & 1 deletion src/styles/list.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
--checkbox-background: #fff;
--color-border: #C9C9C9;
--color-bg-checked: #369FFF;
--line-height: 145%;
--line-height: 1.45em;
--color-bg-checked-hover: #0059AB;
--color-tick: #fff;
--size-checkbox: 1.2em;
Expand Down
16 changes: 15 additions & 1 deletion src/types/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { OlCounterType } from './OlCounterType';
export type ListDataStyle = 'ordered' | 'unordered' | 'checklist';

/**
* Output data
* Interface that represents data of the Nested List tool
*/
export interface ListData {
/**
Expand All @@ -33,6 +33,20 @@ export interface ListData {
counterType?: OlCounterType;
}

/**
* Interface that represents data of the List tool
*/
export interface OldListData {
/**
* Style of the List tool
*/
style: 'ordered' | 'unordered';
/**
* Array of items of the List tool
*/
items: string[];
}

/**
* List item within the output data
*/
Expand Down
36 changes: 36 additions & 0 deletions src/utils/normalizeData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { OldListData, ListData, ListItem } 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
*/
function instanceOfListData(data: ListData | OldListData): data is OldListData {
return (typeof data.items[0] === 'string');
}

/**
* 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 {
const normalizedDataItems: ListItem[] = [];

if (instanceOfListData(data)) {
data.items.forEach((item) => {
normalizedDataItems.push({
content: item,
meta: {},
items: [],
});
});

return {
style: data.style,
items: normalizedDataItems,
};
} else {
return data;
}
};

0 comments on commit e04ffe3

Please sign in to comment.