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

[lexical][lexical-devtools-core][lexical-playground] Bug Fix: fix TextNode importDom highlight formatting #6518

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/lexical-devtools-core/src/generateContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const FORMAT_PREDICATES = [
node.hasFormat('superscript') && 'Superscript',
(node: TextNode | RangeSelection) =>
node.hasFormat('underline') && 'Underline',
(node: TextNode | RangeSelection) =>
node.hasFormat('highlight') && 'Highlight',
];

const FORMAT_PREDICATES_PARAGRAPH = [
Expand All @@ -78,6 +80,7 @@ const FORMAT_PREDICATES_PARAGRAPH = [
(node: ParagraphNode) => node.hasTextFormat('subscript') && 'Subscript',
(node: ParagraphNode) => node.hasTextFormat('superscript') && 'Superscript',
(node: ParagraphNode) => node.hasTextFormat('underline') && 'Underline',
(node: ParagraphNode) => node.hasTextFormat('highlight') && 'Highlight',
];

const DETAIL_PREDICATES = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,39 @@ test.describe('HTML CopyAndPaste', () => {
`,
);
});

test('Copy + paste html with highlight formatting', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);
const clipboardData = {
'text/html': `<meta charset='utf-8'><p><b>Bold</b><mark>Highlight</mark><b><mark>Bold&amp;Highlight</mark></b></p>`,
};
await pasteFromClipboard(page, clipboardData);
await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<strong
class="PlaygroundEditorTheme__textBold"
data-lexical-text="true">
Bold
</strong>
<mark data-lexical-text="true">
<span class="PlaygroundEditorTheme__textHighlight">Highlight</span>
</mark>
<mark data-lexical-text="true">
<strong
class="PlaygroundEditorTheme__textBold PlaygroundEditorTheme__textHighlight">
Bold&amp;Highlight
</strong>
</mark>
</p>
`,
);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ i.superscript {
background-image: url(images/icons/type-superscript.svg);
}

i.highlight {
background-image: url(images/icons/highlighter.svg);
}

i.link {
background-image: url(images/icons/link.svg);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ export default function ToolbarPlugin({
const [isStrikethrough, setIsStrikethrough] = useState(false);
const [isSubscript, setIsSubscript] = useState(false);
const [isSuperscript, setIsSuperscript] = useState(false);
const [isHighlight, setIsHighlight] = useState(false);
const [isCode, setIsCode] = useState(false);
const [canUndo, setCanUndo] = useState(false);
const [canRedo, setCanRedo] = useState(false);
Expand Down Expand Up @@ -585,6 +586,7 @@ export default function ToolbarPlugin({
setIsStrikethrough(selection.hasFormat('strikethrough'));
setIsSubscript(selection.hasFormat('subscript'));
setIsSuperscript(selection.hasFormat('superscript'));
setIsHighlight(selection.hasFormat('highlight'));
setIsCode(selection.hasFormat('code'));
setIsRTL($isParentElementRTL(selection));

Expand Down Expand Up @@ -1052,6 +1054,16 @@ export default function ToolbarPlugin({
<i className="icon superscript" />
<span className="text">Superscript</span>
</DropDownItem>
<DropDownItem
onClick={() => {
activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'highlight');
}}
className={'item ' + dropDownActiveClass(isHighlight)}
title="Highlight"
aria-label="Format text with a highlight">
<i className="icon highlight" />
<span className="text">Highlight</span>
</DropDownItem>
<DropDownItem
onClick={clearFormatting}
className="item"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
.PlaygroundEditorTheme__textBold {
font-weight: bold;
}
.PlaygroundEditorTheme__paragraph mark {
background-color: unset;
}
.PlaygroundEditorTheme__textHighlight {
background: rgba(255, 212, 0, 0.14);
border-bottom: 2px solid rgba(255, 212, 0, 0.3);
}
.PlaygroundEditorTheme__textItalic {
font-style: italic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const theme: EditorThemeClasses = {
text: {
bold: 'PlaygroundEditorTheme__textBold',
code: 'PlaygroundEditorTheme__textCode',
highlight: 'PlaygroundEditorTheme__textHighlight',
italic: 'PlaygroundEditorTheme__textItalic',
strikethrough: 'PlaygroundEditorTheme__textStrikethrough',
subscript: 'PlaygroundEditorTheme__textSubscript',
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 @@ -579,6 +579,10 @@ export class TextNode extends LexicalNode {
conversion: convertTextFormatElement,
priority: 0,
}),
mark: () => ({
conversion: convertTextFormatElement,
priority: 0,
}),
s: () => ({
conversion: convertTextFormatElement,
priority: 0,
Expand Down Expand Up @@ -1284,6 +1288,7 @@ const nodeNameToTextFormat: Record<string, TextFormatType> = {
code: 'code',
em: 'italic',
i: 'italic',
mark: 'highlight',
s: 'strikethrough',
strong: 'bold',
sub: 'subscript',
Expand Down
Loading