gpt4 book ai didi

reactjs - Draft.js - 从数据库中检索格式化文本

转载 作者:行者123 更新时间:2023-12-03 17:10:15 25 4
gpt4 key购买 nike

我的draft.js <TextEditor />填充 body带有文本,例如:'{"blocks":[{"key":"3mont","text":"lorem ipsum","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{}}'并在使用 convertToRaw() 后将其持久化到数据库中.
Post.js ,我想检索并显示格式化的text从数据库。
我读过为了做到这一点,我必须使用 convertToRaw()然后 convertFromRaw()从数据库中检索它时,但我遇到了与 this 相同的问题(我收到 cors 错误和 Unexpected token u in JSON at position 0 )每当我使用 convertFromRaw()并尝试检索格式化的 text从数据库。
我已经设置了我的服务器来支持 cors那么为什么我会收到 cors错误?是不是因为我试图将无效响应解析为 JSON ?
我怎样才能得到格式化的 text来自 Post.js 中的数据库?
任何帮助将非常感激!
GitHub
创建Post.js

class CreatePost extends React.Component {
constructor(props) {
super(props);
this.state = {
title: "",
body: EditorState.createEmpty(),
};
}

changeHandler = (e) => {
this.setState({ [e.target.name]: e.target.value });
};

submitHandler = (e) => {
e.preventDefault();
const {
user: { _id },
} = isAuthenticated();
axios({
url: `${API}/post/new-post/${_id}`,
method: "POST",
data: {
...this.state,
body: JSON.stringify(convertToRaw(this.state.body.getCurrentContent())),
},
})
.then((response) => {
// this.setState({ createdPost: this.state.title });
return response
})
.catch((error) => {
if (!this.state.title || !this.state.body) {
this.setState({
error: "This post must contain a title and a body.",
});
}
console.log(error);
});
};

// Attempt to map through blocks
//getText = () => {
// const {body} = this.state;
//const arr = body.blocks.map(({ text }) => text).join(' ')
// console.log(arr)
//}

render() {
const { title, body } = this.state;

return (
<>
<Navbar />
<Tabs>
<TabList>
<Tab>Draft</Tab>
<Tab>Preview</Tab>
</TabList>
<TabPanel>
<div>
<form onSubmit={this.submitHandler}>
<div>
// title input
</div>
<div>
<TextEditor
onChange={(value) => this.setState({ body: value })}
editorState={body}
/>
</div>
<button type="submit">
Publish
</button>
</form>
</div>
</TabPanel>

<TabPanel>
<div>
<h1>{title}</h1>
// display body text value here too
{this.getText()}
</div>
</TabPanel>
</Tabs>
</>
);
}
}
Post.js(显示 body 文本)
  const [post, setPost] = useState({});
const [error, setError] = useState(false);
const id = props.match.params.id;

const loadSinglePost = (slug, id) => {
read(slug, id).then((data) => {
if (error) {
console.log(data.error);
setError(data.error);
} else {
setPost(data)
console.log(data);
}
});
};

useEffect(() => {
const slug = props.match.params.slug;
loadSinglePost(slug, id);
}, [props]);

return (
<>
<div>
<h3>{post.title}</h3>
...
// display text value below
<p>{post.body}</p>
</div>
</div>
</>
);
};
文本编辑器.js
class TextEditor extends React.Component {
constructor(props) {
super(props);
this.plugins = [addLinkPlugin];
}
toggleBlockType = (blockType) => {
this.props.onChange(RichUtils.toggleBlockType(this.props.editorState, blockType));
};
handleKeyCommand = (command) => {
const newState = RichUtils.handleKeyCommand(
this.props.editorState,
command
);
if (newState) {
this.props.onChange(newState);
return "handled";
}
return "not-handled";
};

onUnderlineClick = () => {
this.props.onChange(
RichUtils.toggleInlineStyle(this.props.editorState, "UNDERLINE")
);
};

onBoldClick = (event) => {
this.props.onChange(RichUtils.toggleInlineStyle(this.props.editorState, "BOLD"));
};

onItalicClick = () => {
this.props.onChange(
RichUtils.toggleInlineStyle(this.props.editorState, "ITALIC")
);
};

onAddLink = () => {
const editorState = this.props.editorState;
const selection = editorState.getSelection();
const link = window.prompt("Paste the link -");
if (!link) {
this.props.onChange(RichUtils.toggleLink(editorState, selection, null));
return "handled";
}
const content = editorState.getCurrentContent();
const contentWithEntity = content.createEntity("LINK", "MUTABLE", {
url: link,
});
const newEditorState = EditorState.push(
editorState,
contentWithEntity,
"create-entity"
);
const entityKey = contentWithEntity.getLastCreatedEntityKey();
this.props.onChange(RichUtils.toggleLink(newEditorState, selection, entityKey));
};

toggleBlockType = (blockType) => {
this.props.onChange(RichUtils.toggleBlockType(this.props.editorState, blockType));
};

render() {
return (
<div>
// formatting buttons
<div>
<Editor
blockStyleFn={getBlockStyle}
editorState={this.props.editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.props.onChange}
plugins={this.plugins}
placeholder="Post Content"
/>
</div>
</div>
);
}
}

最佳答案

显然 draft-js没有 html 输出功能,因为它应该对输出没有任何假设,因此人们可以根据需要调整他们的输出( see this )。这意味着我们必须自己实现它,如果您只需要一个 html 或 markdown 输出来保存在数据库中,那么 this mono repo 可以提供帮助。我已经在 this sandbox 中实现了一个如何做的例子.请注意,我使用了 dangerouslySetInnerHTML用于演示,这不是最佳的。您可能希望使用清理和富文本组件来显示帖子。事实上,如果可能的话,我建议放弃 html 并进行 Markdown 。

关于reactjs - Draft.js - 从数据库中检索格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64835060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com