Skip to content

Commit

Permalink
Add highlight TextFormatType (#3583)
Browse files Browse the repository at this point in the history
  • Loading branch information
moy2010 authored Jan 31, 2023
1 parent 6b5fc8e commit 77d7730
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/lexical/src/LexicalConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const IS_UNDERLINE = 1 << 3;
export const IS_CODE = 1 << 4;
export const IS_SUBSCRIPT = 1 << 5;
export const IS_SUPERSCRIPT = 1 << 6;
export const IS_HIGHLIGHT = 1 << 7;

export const IS_ALL_FORMATTING =
IS_BOLD |
Expand Down Expand Up @@ -89,6 +90,7 @@ export const LTR_REGEX = new RegExp('^[^' + RTL + ']*[' + LTR + ']');
export const TEXT_TYPE_TO_FORMAT: Record<TextFormatType | string, number> = {
bold: IS_BOLD,
code: IS_CODE,
highlight: IS_HIGHLIGHT,
italic: IS_ITALIC,
strikethrough: IS_STRIKETHROUGH,
subscript: IS_SUBSCRIPT,
Expand Down
5 changes: 5 additions & 0 deletions packages/lexical/src/nodes/LexicalTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
IS_BOLD,
IS_CODE,
IS_DIRECTIONLESS,
IS_HIGHLIGHT,
IS_ITALIC,
IS_SEGMENTED,
IS_STRIKETHROUGH,
Expand Down Expand Up @@ -83,6 +84,7 @@ export type TextFormatType =
| 'underline'
| 'strikethrough'
| 'italic'
| 'highlight'
| 'code'
| 'subscript'
| 'superscript';
Expand All @@ -97,6 +99,9 @@ function getElementOuterTag(node: TextNode, format: number): string | null {
if (format & IS_CODE) {
return 'code';
}
if (format & IS_HIGHLIGHT) {
return 'mark';
}
if (format & IS_SUBSCRIPT) {
return 'sub';
}
Expand Down
31 changes: 31 additions & 0 deletions packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import {
IS_BOLD,
IS_CODE,
IS_HIGHLIGHT,
IS_ITALIC,
IS_STRIKETHROUGH,
IS_UNDERLINE,
Expand All @@ -47,6 +48,7 @@ const editorConfig = Object.freeze({
text: {
bold: 'my-bold-class',
code: 'my-code-class',
highlight: 'my-highlight-class',
italic: 'my-italic-class',
strikethrough: 'my-strikethrough-class',
underline: 'my-underline-class',
Expand Down Expand Up @@ -224,6 +226,12 @@ describe('LexicalTextNode tests', () => {
(node) => node.hasFormat('code'),
(node) => node.toggleFormat('code'),
],
[
'highlight',
IS_HIGHLIGHT,
(node) => node.hasFormat('highlight'),
(node) => node.toggleFormat('highlight'),
],
])(
'%s flag',
(formatFlag: TextFormatType, stateFormat, flagPredicate, flagToggle) => {
Expand Down Expand Up @@ -568,6 +576,12 @@ describe('LexicalTextNode tests', () => {
'My text node',
'<span class="my-strikethrough-class">My text node</span>',
],
[
'highlight',
IS_HIGHLIGHT,
'My text node',
'<mark><span class="my-highlight-class">My text node</span></mark>',
],
[
'italic',
IS_ITALIC,
Expand Down Expand Up @@ -600,12 +614,29 @@ describe('LexicalTextNode tests', () => {
'<code><span class="my-underline-strikethrough-class my-code-class">' +
'My text node</span></code>',
],
[
'highlight + italic',
IS_HIGHLIGHT | IS_ITALIC,
'My text node',
'<mark><em class="my-highlight-class my-italic-class">My text node</em></mark>',
],
[
'code + underline + strikethrough + bold + italic',
IS_CODE | IS_UNDERLINE | IS_STRIKETHROUGH | IS_BOLD | IS_ITALIC,
'My text node',
'<code><strong class="my-underline-strikethrough-class my-bold-class my-code-class my-italic-class">My text node</strong></code>',
],
[
'code + underline + strikethrough + bold + italic + highlight',
IS_CODE |
IS_UNDERLINE |
IS_STRIKETHROUGH |
IS_BOLD |
IS_ITALIC |
IS_HIGHLIGHT,
'My text node',
'<code><strong class="my-underline-strikethrough-class my-bold-class my-code-class my-highlight-class my-italic-class">My text node</strong></code>',
],
])('%s text format type', async (_type, format, contents, expectedHTML) => {
await update(() => {
const textNode = $createTextNode(contents);
Expand Down

0 comments on commit 77d7730

Please sign in to comment.