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 compatibility): Add normalization for List tool data #96

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 normalizes checks if passed data is related to the List tool and normalizes it
e11sy marked this conversation as resolved.
Show resolved Hide resolved
* @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;
}
};
Loading