mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Added editor to EditForm
This commit is contained in:
parent
1a894e3120
commit
10626d9bf7
2 changed files with 18 additions and 25 deletions
|
@ -1,16 +1,20 @@
|
||||||
import React, {useContext, useState} from 'react';
|
import React, {useContext} from 'react';
|
||||||
import Avatar from './Avatar';
|
import Avatar from './Avatar';
|
||||||
import AppContext from '../AppContext';
|
import AppContext from '../AppContext';
|
||||||
|
import {useEditor, EditorContent} from '@tiptap/react';
|
||||||
|
import {getEditorConfig} from '../utils/editor';
|
||||||
|
|
||||||
const EditForm = (props) => {
|
const EditForm = (props) => {
|
||||||
// todo: we need to convert the HTML back to an editable state instead of putting it into the textarea
|
// todo: we need to convert the HTML back to an editable state instead of putting it into the textarea
|
||||||
const [message, setMessage] = useState(props.comment.html);
|
|
||||||
const {dispatchAction} = useContext(AppContext);
|
const {dispatchAction} = useContext(AppContext);
|
||||||
|
|
||||||
const getHTML = () => {
|
const editor = useEditor({
|
||||||
// Convert newlines to <br> for now (until we add a real editor)
|
...getEditorConfig({
|
||||||
return message.replace('\n', '<br>');
|
placeholder: 'Edit comment',
|
||||||
};
|
autofocus: true,
|
||||||
|
content: props.comment.html
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
const submitForm = async (event) => {
|
const submitForm = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
@ -18,7 +22,7 @@ const EditForm = (props) => {
|
||||||
await dispatchAction('editComment', {
|
await dispatchAction('editComment', {
|
||||||
comment: {
|
comment: {
|
||||||
id: props.comment.id,
|
id: props.comment.id,
|
||||||
html: getHTML()
|
html: editor.getHTML()
|
||||||
},
|
},
|
||||||
parent: props.parent
|
parent: props.parent
|
||||||
});
|
});
|
||||||
|
@ -28,14 +32,10 @@ const EditForm = (props) => {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (event) => {
|
|
||||||
setMessage(event.target.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const comment = props.comment;
|
const comment = props.comment;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={submitForm} className={`comment-form transition duration-200 bg-white dark:bg-[rgba(255,255,255,0.08)] rounded-md px-3 pt-3 pb-2 -ml-[13px] -mr-3 -mt-[13px] mb-10 shadow-lg dark:shadow-transparent hover:shadow-xl`}>
|
<form className={`comment-form transition duration-200 bg-white dark:bg-[rgba(255,255,255,0.08)] rounded-md px-3 pt-3 pb-2 -ml-[13px] -mr-3 -mt-[13px] mb-10 shadow-lg dark:shadow-transparent hover:shadow-xl`}>
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-start items-center">
|
<div className="flex justify-start items-center">
|
||||||
<Avatar saturation={props.avatarSaturation} />
|
<Avatar saturation={props.avatarSaturation} />
|
||||||
|
@ -45,23 +45,16 @@ const EditForm = (props) => {
|
||||||
</div>
|
</div>
|
||||||
<div className="pr-3 font-sans leading-normal dark:text-neutral-300">
|
<div className="pr-3 font-sans leading-normal dark:text-neutral-300">
|
||||||
<div className="relative w-full">
|
<div className="relative w-full">
|
||||||
<textarea
|
<EditorContent
|
||||||
className={`w-full h-40 pl-[56px] border-none resize-none p-0 font-sans text-[16.5px] mb-1 leading-normal placeholder:text-neutral-300 focus:outline-0 bg-transparent dark:placeholder:text-[rgba(255,255,255,0.3)] dark:border-none dark:text-[rgba(255,255,255,0.9)] dark:shadow-transparent`}
|
className={`w-full h-40 pl-[56px] border-none resize-none p-0 font-sans text-[16.5px] mb-1 leading-normal placeholder:text-neutral-300 focus:outline-0 bg-transparent dark:placeholder:text-[rgba(255,255,255,0.3)] dark:border-none dark:text-[rgba(255,255,255,0.9)] dark:shadow-transparent`}
|
||||||
value={message}
|
editor={editor}
|
||||||
onChange={handleChange}
|
|
||||||
onFocus={function (e) {
|
|
||||||
var val = e.target.value;
|
|
||||||
e.target.value = '';
|
|
||||||
e.target.value = val;
|
|
||||||
}}
|
|
||||||
autoFocus
|
|
||||||
placeholder='Join the discussion'
|
|
||||||
/>
|
/>
|
||||||
<div className="flex space-x-4 transition-[opacity] duration-150 absolute -right-3 bottom-2">
|
<div className="flex space-x-4 transition-[opacity] duration-150 absolute -right-3 bottom-2">
|
||||||
<button type="button" className="font-sans text-sm font-medium ml-2.5 text-neutral-500 dark:text-neutral-400" onClick={props.toggle}>Cancel</button>
|
<button type="button" className="font-sans text-sm font-medium ml-2.5 text-neutral-500 dark:text-neutral-400" onClick={props.toggle}>Cancel</button>
|
||||||
<button
|
<button
|
||||||
className={`rounded-md border py-3 px-4 font-sans text-sm text-center bg-black font-semibold text-white dark:bg-[rgba(255,255,255,0.9)] dark:text-neutral-800`}
|
className={`rounded-md border py-3 px-4 font-sans text-sm text-center bg-black font-semibold text-white dark:bg-[rgba(255,255,255,0.9)] dark:text-neutral-800`}
|
||||||
type="submit">
|
type="button"
|
||||||
|
onClick={submitForm}>
|
||||||
Edit comment
|
Edit comment
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -4,7 +4,7 @@ import Link from '@tiptap/extension-link';
|
||||||
import Paragraph from '@tiptap/extension-paragraph';
|
import Paragraph from '@tiptap/extension-paragraph';
|
||||||
import Document from '@tiptap/extension-document';
|
import Document from '@tiptap/extension-document';
|
||||||
|
|
||||||
export function getEditorConfig({placeholder, autofocus = false}) {
|
export function getEditorConfig({placeholder, autofocus = false, content = ''}) {
|
||||||
return {
|
return {
|
||||||
extensions: [
|
extensions: [
|
||||||
Document,
|
Document,
|
||||||
|
@ -22,7 +22,7 @@ export function getEditorConfig({placeholder, autofocus = false}) {
|
||||||
placeholder
|
placeholder
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
content: '',
|
content,
|
||||||
autofocus,
|
autofocus,
|
||||||
editorProps: {
|
editorProps: {
|
||||||
attributes: {
|
attributes: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue