-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f91d6b4
commit c14cc36
Showing
3 changed files
with
75 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...tpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/editor-state-to-comment-content.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createHeadlessEditor } from "@lexical/headless"; | ||
import { $createParagraphNode, $createTextNode, $getRoot } from "lexical"; | ||
import { expect, test } from "vitest"; | ||
import { editorStateToCommentContent } from "./editor-state-to-comment-content"; | ||
|
||
test("simple string", () => { | ||
const editor = createHeadlessEditor(); | ||
editor.update(() => | ||
$getRoot().append( | ||
$createParagraphNode().append($createTextNode("Hello, world!")), | ||
), | ||
); | ||
|
||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual( | ||
"Hello, world!", | ||
); | ||
}); |
55 changes: 55 additions & 0 deletions
55
.../frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/editor-state-to-comment-content.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
$getRoot, | ||
$isElementNode, | ||
$isTextNode, | ||
EditorState, | ||
LexicalNode, | ||
} from "lexical"; | ||
|
||
export function editorStateToCommentContent(editorState: EditorState) { | ||
return editorState.read(() => { | ||
const root = $getRoot(); | ||
const content = $nodeToCommentContent(root); | ||
return content; | ||
}); | ||
} | ||
|
||
const FORMATS = ["bold", "italic", "strikethrough"] as const; | ||
|
||
type CommentFacet = | ||
| { | ||
$type: "fyi.frontpage.richtext.facet#format"; | ||
format: (typeof FORMATS)[number]; | ||
} | ||
| { | ||
$type: "fyi.frontpage.richtext.facet#link"; | ||
uri: string; | ||
}; | ||
|
||
type CommentContent = | ||
| string | ||
| { content: CommentContent; facets: CommentFacet[] } | ||
| CommentContent[]; | ||
|
||
function $nodeToCommentContent(node: LexicalNode): CommentContent { | ||
if ($isTextNode(node)) { | ||
console.log("text node", node); | ||
const formats = FORMATS.filter((format) => node.hasFormat(format)); | ||
if (formats.length === 0) { | ||
return node.getTextContent(); | ||
} | ||
|
||
return { | ||
content: node.getTextContent(), | ||
facets: formats.map((format) => ({ | ||
$type: "fyi.frontpage.richtext.facet#format", | ||
format, | ||
})), | ||
}; | ||
} else if ($isElementNode(node)) { | ||
console.log("element node", node); | ||
return node.getChildren().map($nodeToCommentContent); | ||
} else { | ||
throw new Error("Unknown node type"); | ||
} | ||
} |