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

Feature/disable tool elements #122

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var editor = EditorJS({
|--------------|----------|----------------------------------------------------------------|
| defaultStyle | `string` | default list style: `ordered`, `unordered` or `checklist`, default is `unordered` |
| maxLevel | `number` | maximum level of the list nesting, could be set to `1` to disable nesting, unlimited by default |
| displayedEntries | `listStyle[]` | List entries allowed to be displayed `ordered`, `unordered` or `checklist` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets call it styles?


## Output data

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/list",
"version": "2.0.2",
"version": "2.0.3",
"keywords": [
"codex editor",
"list",
Expand Down
148 changes: 87 additions & 61 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { API, BlockAPI, PasteConfig, ToolboxConfig } from '@editorjs/editorjs';
import type { API, BlockAPI, PasteConfig, ToolboxConfig, BlockTool } from '@editorjs/editorjs';
import type {
BlockToolConstructorOptions,
MenuConfigItem,
Expand Down Expand Up @@ -31,7 +31,7 @@ export type ListParams = BlockToolConstructorOptions<ListData | OldListData, Lis
/**
* Default class of the component used in editor
*/
export default class EditorjsList {
export default class EditorjsList implements BlockTool {
/**
* Notify core that read-only mode is supported
*/
Expand All @@ -52,7 +52,7 @@ export default class EditorjsList {
* title - title to show in toolbox
*/
public static get toolbox(): ToolboxConfig {
return [
const entries = [
{
icon: IconListBulleted,
title: 'Unordered List',
Expand All @@ -75,6 +75,10 @@ export default class EditorjsList {
},
},
];

return this.displayedEntries ? entries.filter(
ele => this.displayedEntries?.includes(ele.data.style as ListDataStyle)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot access this.displayedEntries since it is static

) : entries;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improve naming please

}

/**
Expand Down Expand Up @@ -171,6 +175,11 @@ export default class EditorjsList {
*/
private defaultListStyle?: ListConfig['defaultStyle'];

/**
*
*/
private static displayedEntries?: ListDataStyle[];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments missing

/**
* Tool's data
*/
Expand Down Expand Up @@ -210,6 +219,8 @@ export default class EditorjsList {
*/
this.defaultListStyle = this.config?.defaultStyle || 'unordered';

EditorjsList.displayedEntries = this.config?.displayedEntries;

const initialData = {
style: this.defaultListStyle,
meta: {},
Expand Down Expand Up @@ -274,7 +285,7 @@ export default class EditorjsList {
public renderSettings(): MenuConfigItem[] {
const defaultTunes: MenuConfigItem[] = [
{
label: this.api.i18n.t('Unordered'),
title: this.api.i18n.t('Unordered'),
icon: IconListBulleted,
closeOnActivate: true,
isActive: this.listStyle == 'unordered',
Expand All @@ -283,7 +294,7 @@ export default class EditorjsList {
},
},
{
label: this.api.i18n.t('Ordered'),
title: this.api.i18n.t('Ordered'),
icon: IconListNumbered,
closeOnActivate: true,
isActive: this.listStyle == 'ordered',
Expand All @@ -292,7 +303,7 @@ export default class EditorjsList {
},
},
{
label: this.api.i18n.t('Checklist'),
title: this.api.i18n.t('Checklist'),
icon: IconChecklist,
closeOnActivate: true,
isActive: this.listStyle == 'checklist',
Expand All @@ -302,61 +313,18 @@ export default class EditorjsList {
},
];

if (this.listStyle === 'ordered') {
const startWithElement = renderToolboxInput(
(index: string) => this.changeStartWith(Number(index)),
{
value: String((this.data.meta as OrderedListItemMeta).start ?? 1),
placeholder: '',
attributes: {
required: 'true',
},
sanitize: input => stripNumbers(input),
});

const orderedListTunes: MenuConfigItem[] = [
{
label: this.api.i18n.t('Start with'),
icon: IconStartWith,
children: {
items: [
{
element: startWithElement,
// @ts-expect-error ts(2820) can not use PopoverItem enum from editor.js types
type: 'html',
},
],
},
},
];

const orderedListCountersTunes: MenuConfigItem = {
label: this.api.i18n.t('Counter type'),
icon: OlCounterIconsMap.get((this.data.meta as OrderedListItemMeta).counterType!),
children: {
items: [],
},
};

/**
* For each counter type in OlCounterType create toolbox item
*/
OlCounterTypesMap.forEach((_, counterType: string) => {
orderedListCountersTunes.children.items!.push({
title: this.api.i18n.t(counterType),
icon: OlCounterIconsMap.get(OlCounterTypesMap.get(counterType)!),
isActive: (this.data.meta as OrderedListItemMeta).counterType === OlCounterTypesMap.get(counterType),
closeOnActivate: true,
onActivate: () => {
this.changeCounters(OlCounterTypesMap.get(counterType) as OlCounterType);
},
});
});
// @ts-expect-error ts(2820) can not use PopoverItem enum from editor.js types
defaultTunes.push({ type: 'separator' }, ...orderedListTunes, orderedListCountersTunes);
}

return defaultTunes;
if (EditorjsList.displayedEntries){
if (this.listStyle === 'ordered') this.addAdditionalTunes(defaultTunes);
} else if (this.listStyle === 'ordered') this.addAdditionalTunes(defaultTunes);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check for EditorjsList.displayedEntries here

return EditorjsList.displayedEntries ? defaultTunes.filter(
tune => {
if ('title' in tune) {
if(tune.title === 'Start with' || tune.title === 'Counter type') return true;
else return EditorjsList.displayedEntries?.includes(tune.title!.toLowerCase() as ListDataStyle);
} else if (('type' in tune) && tune.type === 'separator') return true;
}
) : defaultTunes;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can firstly filter default tunes, and then add additional ones, then you won't need to check for Start with and Counter type in tunes


/**
Expand Down Expand Up @@ -453,4 +421,62 @@ export default class EditorjsList {
break;
}
}

/**
* Add additional tunes for ordered list
* @param defaultTunes - default tunes list
*/
private addAdditionalTunes(defaultTunes: MenuConfigItem[]): void {
const startWithElement = renderToolboxInput(
(index: string) => this.changeStartWith(Number(index)),
{
value: String((this.data.meta as OrderedListItemMeta).start ?? 1),
placeholder: '',
attributes: {
required: 'true',
},
sanitize: input => stripNumbers(input),
});

const orderedListTunes: MenuConfigItem[] = [
{
title: this.api.i18n.t('Start with'),
icon: IconStartWith,
children: {
items: [
{
element: startWithElement,
// @ts-expect-error ts(2820) can not use PopoverItem enum from editor.js types
type: 'html',
},
],
},
},
];

const orderedListCountersTunes: MenuConfigItem = {
title: this.api.i18n.t('Counter type'),
icon: OlCounterIconsMap.get((this.data.meta as OrderedListItemMeta).counterType!),
children: {
items: [],
},
};

/**
* For each counter type in OlCounterType create toolbox item
*/
OlCounterTypesMap.forEach((_, counterType: string) => {
orderedListCountersTunes.children.items!.push({
title: this.api.i18n.t(counterType),
icon: OlCounterIconsMap.get(OlCounterTypesMap.get(counterType)!),
isActive: (this.data.meta as OrderedListItemMeta).counterType === OlCounterTypesMap.get(counterType),
closeOnActivate: true,
onActivate: () => {
this.changeCounters(OlCounterTypesMap.get(counterType) as OlCounterType);
},
});
});
// @ts-expect-error ts(2820) can not use PopoverItem enum from editor.js types
defaultTunes.push({ type: 'separator' }, ...orderedListTunes, orderedListCountersTunes);
}
}
5 changes: 5 additions & 0 deletions src/types/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ export interface ListConfig {
* If nesting is not needed, it could be set to 1
*/
maxLevel?: number;

/**
* List types allowed to be displayed
*/
displayedEntries?: ListDataStyle[];
}
Loading