Skip to content

Commit

Permalink
Lexical: Added basic list button/support
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Jun 19, 2024
1 parent 9e43e03 commit e2409a5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@lexical/history": "^0.15.0",
"@lexical/html": "^0.15.0",
"@lexical/link": "^0.15.0",
"@lexical/list": "^0.15.0",
"@lexical/rich-text": "^0.15.0",
"@lexical/selection": "^0.15.0",
"@lexical/utils": "^0.15.0",
Expand Down
3 changes: 3 additions & 0 deletions resources/js/wysiwyg/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {CustomParagraphNode} from "./custom-paragraph";
import {LinkNode} from "@lexical/link";
import {ImageNode} from "./image";
import {DetailsNode, SummaryNode} from "./details";
import {ListItemNode, ListNode} from "@lexical/list";

/**
* Load the nodes for lexical.
Expand All @@ -14,6 +15,8 @@ export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> |
CalloutNode, // Todo - Create custom
HeadingNode, // Todo - Create custom
QuoteNode, // Todo - Create custom
ListNode, // Todo - Create custom
ListItemNode,
ImageNode,
DetailsNode, SummaryNode,
CustomParagraphNode,
Expand Down
33 changes: 29 additions & 4 deletions resources/js/wysiwyg/ui/defaults/button-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {EditorBasicButtonDefinition, EditorButtonDefinition} from "../framework/buttons";
import {
$createNodeSelection,
$createParagraphNode, $getRoot, $getSelection, $insertNodes,
$createParagraphNode, $getRoot, $getSelection,
$isParagraphNode, $isTextNode, $setSelection,
BaseSelection, ElementNode, FORMAT_TEXT_COMMAND,
LexicalNode,
REDO_COMMAND, TextFormatType, TextNode,
REDO_COMMAND, TextFormatType,
UNDO_COMMAND
} from "lexical";
import {
Expand All @@ -23,12 +23,12 @@ import {
HeadingNode,
HeadingTagType
} from "@lexical/rich-text";
import {$isLinkNode, $toggleLink, LinkNode} from "@lexical/link";
import {$isLinkNode, LinkNode} from "@lexical/link";
import {EditorUiContext} from "../framework/core";
import {$isImageNode, ImageNode} from "../../nodes/image";
import {$createDetailsNode, $isDetailsNode} from "../../nodes/details";
import {$insertNodeToNearestRoot} from "@lexical/utils";
import {getEditorContentAsHtml} from "../../actions";
import {$isListNode, insertList, ListNode, ListType, removeList} from "@lexical/list";

export const undo: EditorButtonDefinition = {
label: 'Undo',
Expand Down Expand Up @@ -155,6 +155,31 @@ export const clearFormating: EditorButtonDefinition = {
}
};

function buildListButton(label: string, type: ListType): EditorButtonDefinition {
return {
label,
action(context: EditorUiContext) {
context.editor.getEditorState().read(() => {
const selection = $getSelection();
if (this.isActive(selection)) {
removeList(context.editor);
} else {
insertList(context.editor, type);
}
});
},
isActive(selection: BaseSelection|null): boolean {
return selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
return $isListNode(node) && (node as ListNode).getListType() === type;
});
}
};
}

export const bulletList: EditorButtonDefinition = buildListButton('Bullet list', 'bullet');
export const numberList: EditorButtonDefinition = buildListButton('Numbered list', 'number');
export const taskList: EditorButtonDefinition = buildListButton('Task list', 'check');


export const link: EditorButtonDefinition = {
label: 'Insert/edit link',
Expand Down
11 changes: 8 additions & 3 deletions resources/js/wysiwyg/ui/toolbars.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {EditorButton} from "./framework/buttons";
import {
blockquote, bold, clearFormating, code,
blockquote, bold, bulletList, clearFormating, code,
dangerCallout, details,
h2, h3, h4, h5, highlightColor, image,
infoCallout, italic, link, paragraph,
infoCallout, italic, link, numberList, paragraph,
redo, source, strikethrough, subscript,
successCallout, superscript, textColor, underline,
successCallout, superscript, taskList, textColor, underline,
undo,
warningCallout
} from "./defaults/button-definitions";
Expand Down Expand Up @@ -53,6 +53,11 @@ export function getMainEditorFullToolbar(): EditorContainerUiElement {
new EditorButton(code),
new EditorButton(clearFormating),

// Lists
new EditorButton(bulletList),
new EditorButton(numberList),
new EditorButton(taskList),

// Insert types
new EditorButton(link),
new EditorButton(image),
Expand Down
1 change: 1 addition & 0 deletions resources/sass/_editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Main UI elements
.editor-toolbar-main {
display: flex;
flex-wrap: wrap;
}

// Buttons
Expand Down

0 comments on commit e2409a5

Please sign in to comment.