From 10b4ea84eb09ca1e5896db2ba082269ecc2c5d72 Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Sat, 5 Apr 2025 17:29:26 +0200 Subject: [PATCH] feat(comment edit): if the user manually types "edit: " at the end of an edit, parse it as comment.edit.reason This is needed to make Seedit as compatible as possible with other plebbit clients. --- .../comment-edit-form/comment-edit-form.tsx | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/components/comment-edit-form/comment-edit-form.tsx b/src/components/comment-edit-form/comment-edit-form.tsx index d5bca4ba..a6ee96e1 100644 --- a/src/components/comment-edit-form/comment-edit-form.tsx +++ b/src/components/comment-edit-form/comment-edit-form.tsx @@ -51,6 +51,33 @@ const CommentEditForm = ({ commentCid, hideCommentEditForm }: CommentEditFormPro const [publishCommentEditOptions, setPublishCommentEditOptions] = useState(defaultPublishOptions); const { publishCommentEdit } = usePublishCommentEdit(publishCommentEditOptions); + // the user might manually type "edit: " in the content field + // we want to extract the reason and remove it from the content + const parseEditContent = (content: string) => { + const lines = content.trim().split('\n'); + const lastLine = lines[lines.length - 1]; + + if (lastLine?.toLowerCase().startsWith('edit:')) { + const newContent = lines.slice(0, -1).join('\n').trim(); + return { + content: newContent, + reason: lastLine.substring(5).trim(), // remove "edit:" prefix + }; + } + + return { content, reason: publishCommentEditOptions.reason }; + }; + + const [shouldPublish, setShouldPublish] = useState(false); + + useEffect(() => { + if (shouldPublish) { + publishCommentEdit(); + hideCommentEditForm && hideCommentEditForm(); + setShouldPublish(false); + } + }, [shouldPublish, publishCommentEdit, hideCommentEditForm]); + useEffect(() => { if (textRef.current) { textRef.current.focus(); @@ -125,8 +152,13 @@ const CommentEditForm = ({ commentCid, hideCommentEditForm }: CommentEditFormPro