Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Jul 28, 2024
1 parent f91d6b4 commit c14cc36
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use server";

import {
CommentCollection,
createComment,
deleteComment,
} from "@/lib/data/atproto/comment";
import { CommentCollection, deleteComment } from "@/lib/data/atproto/comment";
import { DID } from "@/lib/data/atproto/did";
import { deletePost } from "@/lib/data/atproto/post";
import { createVote, deleteVote } from "@/lib/data/atproto/vote";
Expand All @@ -13,19 +9,8 @@ import { getPost } from "@/lib/data/db/post";
import { getVoteForComment } from "@/lib/data/db/vote";
import { ensureUser } from "@/lib/data/user";
import { createHeadlessEditor } from "@lexical/headless";
import {
SerializedEditorState,
$parseSerializedNode,
LexicalEditor,
$getRoot,
EditorState,
LexicalNode,
$isTextNode,
$isElementNode,
RootNode,
} from "lexical";
import { $dfs } from "@lexical/utils";
import { revalidatePath } from "next/cache";
import { SerializedEditorState, RootNode } from "lexical";
import { editorStateToCommentContent } from "./editor-state-to-comment-content";

export async function createCommentAction(input: {
parentRkey?: string;
Expand Down Expand Up @@ -79,54 +64,6 @@ async function waitForComment(rkey: string) {
}
}

function editorStateToCommentContent(editorState: EditorState) {
return editorState.read(() => {
const root = $getRoot();
const content = $nodeToCommentContent(root);
const text = root.getTextContent();
});
}

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");
}
}

export async function deletePostAction(rkey: string) {
await deletePost(rkey);
}
Expand Down
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!",
);
});
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");
}
}

0 comments on commit c14cc36

Please sign in to comment.