Skip to content

Commit 5bce160

Browse files
Add text fallbacks to YouTubeNode and TweetNode on conversion to CodeBlock (#2650)
* Add getTextContent to decorator nodes * Add text content fallbacks
1 parent c85d94d commit 5bce160

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

packages/lexical-playground/src/nodes/YouTubeNode.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export class YouTubeNode extends DecoratorBlockNode {
100100
return false;
101101
}
102102

103+
getId(): string {
104+
return this.__id;
105+
}
106+
103107
decorate(_editor: LexicalEditor, config: EditorConfig): JSX.Element {
104108
const embedBlockTheme = config.theme.embedBlock || {};
105109
const className = {

packages/lexical-playground/src/plugins/ToolbarPlugin.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
} from '@lexical/utils';
5757
import {
5858
$createParagraphNode,
59+
$createTextNode,
5960
$getNodeByKey,
6061
$getRoot,
6162
$getSelection,
@@ -85,6 +86,8 @@ import useModal from '../hooks/useModal';
8586
import catTypingGif from '../images/cat-typing.gif';
8687
import yellowFlowerImage from '../images/yellow-flower.jpg';
8788
import {$createStickyNode} from '../nodes/StickyNode';
89+
import {$isTweetNode} from '../nodes/TweetNode';
90+
import {$isYouTubeNode} from '../nodes/YouTubeNode';
8891
import Button from '../ui/Button';
8992
import ColorPicker from '../ui/ColorPicker';
9093
import DropDown, {DropDownItem} from '../ui/DropDown';
@@ -699,6 +702,23 @@ function BlockFormatDropDown({
699702
if (selection.isCollapsed()) {
700703
$wrapLeafNodesInElements(selection, () => $createCodeNode());
701704
} else {
705+
selection.getNodes().forEach((node) => {
706+
// Explicity set fallback text content for some decorators nodes.
707+
if ($isTweetNode(node)) {
708+
node.replace(
709+
$createTextNode(
710+
`https://twitter.com/i/web/status/${node.getId()}`,
711+
),
712+
);
713+
} else if ($isYouTubeNode(node)) {
714+
node.replace(
715+
$createTextNode(
716+
`https://www.youtube.com/watch?v=${node.getId()}`,
717+
),
718+
);
719+
}
720+
});
721+
702722
const textContent = selection.getTextContent();
703723
const codeNode = $createCodeNode();
704724
selection.insertNodes([codeNode]);

0 commit comments

Comments
 (0)