Add Read "raw_content" contentEditable Element & Edit

This commit is contained in:
2024-08-04 18:26:05 +08:00
parent 99d63d4912
commit 945f8ce431
9 changed files with 295 additions and 7 deletions
+10 -3
View File
@@ -1,10 +1,13 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef, useState } from "react";
import { sendToBackground } from "@plasmohq/messaging";
import useForm from "~hooks/useForm";
import Tab from "~components/ui/tab";
import Form from "~components/ui/form";
import Article from "~components/ui/article";
import { add } from "~components/ui/message/utils";
import styles from "./read.module.less";
interface FormValue {
title: string;
link: string;
@@ -14,6 +17,7 @@ interface FormValue {
image: string;
sitename: string;
tags: string;
raw_content?: string;
}
const getInfo = async () => {
@@ -46,6 +50,7 @@ const submitForm = (body: FormValue) => {
function Read() {
const formRef = useRef<HTMLFormElement>();
const [htmlContent, setHtmlContent] = useState("");
const { bindInput, setValues, onSubmit } = useForm<FormValue>({
initialValues: {
@@ -60,13 +65,14 @@ function Read() {
}
setValues(res);
res.raw_content && setHtmlContent(res.raw_content);
})
}, []);
return (
<Tab>
<Tab className={styles.read}>
<Tab.Body>
<Form ref={formRef} onSubmit={onSubmit(submitForm)}>
<Form ref={formRef} onSubmit={onSubmit((values) => submitForm({ ...values, raw_content: htmlContent }))}>
<input {...bindInput("title")} required placeholder="标题" />
<input {...bindInput("link")} required placeholder="链接" />
<div>
@@ -94,6 +100,7 @@ function Read() {
<input {...bindInput("tags")} placeholder="标签" />
</div>
</Form>
<Article className={styles.article} value={htmlContent} onChange={setHtmlContent} />
</Tab.Body>
<Tab.Footer>
<button onClick={() => formRef.current?.requestSubmit()}>提交</button>
+8
View File
@@ -0,0 +1,8 @@
.read {
.article {
padding: .75em;
margin-top: 1em;
border-radius: .5em;
border: 1px solid #eee;
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useRef } from "react";
import { sendToBackground } from "@plasmohq/messaging";
import useForm from "~hooks/useForm";
import Tab from "~components/ui/tab";
+44
View File
@@ -0,0 +1,44 @@
.article {
overflow-x: auto;
a {
color: #28b9be;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
margin-top: 1.5em;
&:first-child {
margin-top: 0;
}
}
h1, h2, h3, h4, h5, h6, p {
margin-bottom: 1em;
}
p {
line-height: 1.7;
}
img {
height: auto;
}
pre {
color: #fff;
padding: .5em;
overflow: auto;
border-radius: .5em;
background-color: #333;
}
:not(pre) > code {
color: brown;
padding: 0 .5em;
border-radius: .5em;
display: inline-block;
background-color: antiquewhite;
}
}
+35
View File
@@ -0,0 +1,35 @@
import { useEffect, useRef } from "react";
import { clsn } from "~utils";
import styles from "./article.module.less";
interface ArticleProps {
className?: string;
value: string | number;
onChange: (value: string) => void;
}
function Article({ className, value, onChange }: ArticleProps) {
const articleRef = useRef<HTMLElement>(null);
useEffect(() => {
if (!articleRef.current) {
return;
}
if (articleRef.current.innerHTML !== String(value)) {
articleRef.current.innerHTML = String(value);
}
}, [value]);
return (
<article
ref={articleRef}
className={clsn(styles.article, className)}
contentEditable
onInput={(ev) => onChange(articleRef.current.innerHTML)}
/>
);
}
export default Article;
+7 -2
View File
@@ -1,16 +1,21 @@
import { clsn } from "~utils";
import { IconBack } from "~assets/icons";
import type { ReactNode, PropsWithChildren } from "react";
import styles from "./tab.module.less";
interface TabProps extends PropsWithChildren {
className: string;
}
interface HeaderProps {
title: ReactNode;
onBack: () => void;
}
function Tab({ children }: PropsWithChildren) {
function Tab({ className, children }: TabProps) {
return (
<div className={styles.tab}>
<div className={clsn(styles.tab, className)}>
{children}
</div>
);