Skip to content

Commit

Permalink
[lexical] Feature: Add listStyleType support to ListNode for customiz…
Browse files Browse the repository at this point in the history
…able list styles
  • Loading branch information
Tiscs committed Mar 8, 2025
1 parent 9fb1b3c commit bcece21
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion packages/lexical-list/src/LexicalListNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {$getListDepth, $wrapInListItem} from './utils';
export type SerializedListNode = Spread<
{
listType: ListType;
listStyleType?: ListStyleType;
start: number;
tag: ListNodeTagType;
},
Expand All @@ -49,6 +50,26 @@ export type SerializedListNode = Spread<

export type ListType = 'number' | 'bullet' | 'check';

export type ListStyleType =
| undefined // default
| (string & object) // string means custom style
| 'none'
| 'disc'
| 'circle'
| 'square'
| 'decimal'
| 'decimal-leading-zero'
| 'lower-roman'
| 'upper-roman'
| 'lower-greek'
| 'lower-alpha'
| 'lower-latin'
| 'upper-alpha'
| 'upper-latin'
| 'armenian'
| 'georgian'
| 'hebrew';

export type ListNodeTagType = 'ul' | 'ol';

/** @noInheritDoc */
Expand All @@ -59,6 +80,8 @@ export class ListNode extends ElementNode {
__start: number;
/** @internal */
__listType: ListType;
/** @internal */
__listStyleType: ListStyleType;

static getType(): string {
return 'list';
Expand All @@ -70,11 +93,17 @@ export class ListNode extends ElementNode {
return new ListNode(listType, node.__start, node.__key);
}

constructor(listType: ListType = 'number', start: number = 1, key?: NodeKey) {
constructor(
listType: ListType = 'number',
start: number = 1,
key?: NodeKey,
listStyleType?: ListStyleType,
) {
super(key);
const _listType = TAG_TO_LIST_TYPE[listType] || listType;
this.__listType = _listType;
this.__tag = _listType === 'number' ? 'ol' : 'ul';
this.__listStyleType = listStyleType;
this.__start = start;
}

Expand All @@ -93,6 +122,16 @@ export class ListNode extends ElementNode {
return this.__listType;
}

setListStyleType(type: ListStyleType): this {
const writable = this.getWritable();
writable.__listStyleType = type;
return writable;
}

getListStyleType(): ListStyleType {
return this.__listStyleType;
}

getStart(): number {
return this.__start;
}
Expand All @@ -114,6 +153,11 @@ export class ListNode extends ElementNode {
}
// @ts-expect-error Internal field.
dom.__lexicalListType = this.__listType;
if (this.__listStyleType) {
dom.style.setProperty('list-style-type', this.__listStyleType);
} else {
dom.style.removeProperty('list-style-type');
}
$setListThemeClassNames(dom, config.theme, this);

return dom;
Expand All @@ -124,6 +168,11 @@ export class ListNode extends ElementNode {
return true;
}

if (this.__listStyleType) {
dom.style.setProperty('list-style-type', this.__listStyleType);
} else {
dom.style.removeProperty('list-style-type');
}
$setListThemeClassNames(dom, config.theme, this);

return false;
Expand Down Expand Up @@ -158,6 +207,7 @@ export class ListNode extends ElementNode {
return super
.updateFromJSON(serializedNode)
.setListType(serializedNode.listType)
.setListStyleType(serializedNode.listStyleType)
.setStart(serializedNode.start);
}

Expand All @@ -179,6 +229,7 @@ export class ListNode extends ElementNode {
exportJSON(): SerializedListNode {
return {
...super.exportJSON(),
listStyleType: this.getListStyleType(),
listType: this.getListType(),
start: this.getStart(),
tag: this.getTag(),
Expand Down

0 comments on commit bcece21

Please sign in to comment.