mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
added milkdown, but its not working
This commit is contained in:
parent
6230a769e0
commit
fd55a1db62
9 changed files with 5620 additions and 172 deletions
|
@ -1,7 +1,7 @@
|
|||
# NEXT_PUBLIC_API_HOST=https://pig.staging.vault48.org/
|
||||
# NEXT_PUBLIC_REMOTE_CURRENT=https://pig.staging.vault48.org/static/
|
||||
# NEXT_PUBLIC_API_HOST=http://localhost:7777/
|
||||
NEXT_PUBLIC_API_HOST=http://localhost:7777/
|
||||
# NEXT_PUBLIC_REMOTE_CURRENT=http://localhost:7777/static/
|
||||
NEXT_PUBLIC_API_HOST=https://pig.vault48.org/
|
||||
# NEXT_PUBLIC_API_HOST=https://pig.vault48.org/
|
||||
NEXT_PUBLIC_REMOTE_CURRENT=https://pig.vault48.org/static/
|
||||
NEXT_PUBLIC_BOT_USERNAME=vault48testbot
|
|
@ -5,6 +5,13 @@
|
|||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.0.5",
|
||||
"@dnd-kit/sortable": "^7.0.1",
|
||||
"@milkdown/core": "^7.1.0",
|
||||
"@milkdown/ctx": "^7.1.0",
|
||||
"@milkdown/plugin-listener": "^7.1.0",
|
||||
"@milkdown/preset-commonmark": "^7.1.0",
|
||||
"@milkdown/prose": "^7.1.0",
|
||||
"@milkdown/react": "^7.1.0",
|
||||
"@milkdown/transformer": "^7.1.0",
|
||||
"@popperjs/core": "^2.11.2",
|
||||
"@testing-library/jest-dom": "^5.11.4",
|
||||
"@testing-library/react": "^11.1.0",
|
||||
|
|
|
@ -13,12 +13,15 @@ import { UploadDropzone } from '~/components/upload/UploadDropzone';
|
|||
import { ERROR_LITERAL } from '~/constants/errors';
|
||||
import { EMPTY_COMMENT } from '~/constants/node';
|
||||
import { UploadSubject, UploadTarget } from '~/constants/uploads';
|
||||
import { useAuth } from '~/hooks/auth/useAuth';
|
||||
import { useCommentFormFormik } from '~/hooks/comments/useCommentFormFormik';
|
||||
import { useUploader } from '~/hooks/data/useUploader';
|
||||
import { useInputPasteUpload } from '~/hooks/dom/useInputPasteUpload';
|
||||
import { IComment, INode } from '~/types';
|
||||
import { UploaderContextProvider } from '~/utils/context/UploaderContextProvider';
|
||||
|
||||
import { WYSIWYGTextaArea } from '../WYSIWYGTextaArea';
|
||||
|
||||
import styles from './styles.module.scss';
|
||||
|
||||
interface IProps {
|
||||
|
@ -28,92 +31,115 @@ interface IProps {
|
|||
onCancelEdit?: () => void;
|
||||
}
|
||||
|
||||
const CommentForm: FC<IProps> = observer(({ comment, nodeId, saveComment, onCancelEdit }) => {
|
||||
const [textarea, setTextArea] = useState<HTMLTextAreaElement | null>(null);
|
||||
const uploader = useUploader(UploadSubject.Comment, UploadTarget.Comments, comment?.files);
|
||||
const formik = useCommentFormFormik(
|
||||
comment || EMPTY_COMMENT,
|
||||
nodeId,
|
||||
uploader,
|
||||
saveComment,
|
||||
onCancelEdit
|
||||
);
|
||||
const isLoading = formik.isSubmitting || uploader.isUploading;
|
||||
const isEditing = !!comment?.id;
|
||||
const CommentForm: FC<IProps> = observer(
|
||||
({ comment, nodeId, saveComment, onCancelEdit }) => {
|
||||
const [textarea, setTextArea] = useState<HTMLTextAreaElement | null>(null);
|
||||
const uploader = useUploader(
|
||||
UploadSubject.Comment,
|
||||
UploadTarget.Comments,
|
||||
comment?.files,
|
||||
);
|
||||
const formik = useCommentFormFormik(
|
||||
comment || EMPTY_COMMENT,
|
||||
nodeId,
|
||||
uploader,
|
||||
saveComment,
|
||||
onCancelEdit,
|
||||
);
|
||||
const isLoading = formik.isSubmitting || uploader.isUploading;
|
||||
const isEditing = !!comment?.id;
|
||||
|
||||
const clearError = useCallback(() => {
|
||||
if (formik.status) {
|
||||
formik.setStatus('');
|
||||
}
|
||||
const clearError = useCallback(() => {
|
||||
if (formik.status) {
|
||||
formik.setStatus('');
|
||||
}
|
||||
|
||||
if (formik.errors.text) {
|
||||
formik.setErrors({
|
||||
...formik.errors,
|
||||
text: '',
|
||||
});
|
||||
}
|
||||
}, [formik]);
|
||||
if (formik.errors.text) {
|
||||
formik.setErrors({
|
||||
...formik.errors,
|
||||
text: '',
|
||||
});
|
||||
}
|
||||
}, [formik]);
|
||||
|
||||
const error = formik.status || formik.errors.text;
|
||||
const onPaste = useInputPasteUpload(uploader.uploadFiles);
|
||||
const error = formik.status || formik.errors.text;
|
||||
const onPaste = useInputPasteUpload(uploader.uploadFiles);
|
||||
|
||||
return (
|
||||
<UploadDropzone onUpload={uploader.uploadFiles}>
|
||||
<form onSubmit={formik.handleSubmit} className={styles.wrap}>
|
||||
<FormikProvider value={formik}>
|
||||
<UploaderContextProvider value={uploader}>
|
||||
<div className={styles.input}>
|
||||
<LocalCommentFormTextarea onPaste={onPaste} ref={setTextArea} />
|
||||
const { isTester } = useAuth();
|
||||
|
||||
{!!error && (
|
||||
<div className={styles.error} onClick={clearError}>
|
||||
{ERROR_LITERAL[error] || error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<CommentFormAttaches />
|
||||
|
||||
<div className={styles.buttons}>
|
||||
<div className={styles.button_column}>
|
||||
<CommentFormAttachButtons onUpload={uploader.uploadFiles} />
|
||||
</div>
|
||||
|
||||
<div className={styles.button_column}>
|
||||
{!!textarea && (
|
||||
<CommentFormFormatButtons
|
||||
element={textarea}
|
||||
handler={formik.handleChange('text')}
|
||||
return (
|
||||
<UploadDropzone onUpload={uploader.uploadFiles}>
|
||||
<form onSubmit={formik.handleSubmit} className={styles.wrap}>
|
||||
<FormikProvider value={formik}>
|
||||
<UploaderContextProvider value={uploader}>
|
||||
<div className={styles.input}>
|
||||
{isTester ? (
|
||||
<WYSIWYGTextaArea
|
||||
value={formik.values.text}
|
||||
onChange={formik.handleChange('value')}
|
||||
/>
|
||||
) : (
|
||||
<LocalCommentFormTextarea
|
||||
onPaste={onPaste}
|
||||
ref={setTextArea}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Filler />
|
||||
|
||||
<div className={styles.button_column}>
|
||||
{isEditing && (
|
||||
<Button size="small" color="link" type="button" onClick={onCancelEdit}>
|
||||
Отмена
|
||||
</Button>
|
||||
{!!error && (
|
||||
<div className={styles.error} onClick={clearError}>
|
||||
{ERROR_LITERAL[error] || error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
size="small"
|
||||
color="gray"
|
||||
iconRight={!isEditing ? 'enter' : 'check'}
|
||||
disabled={isLoading}
|
||||
loading={isLoading}
|
||||
>
|
||||
{!isEditing ? 'Сказать' : 'Сохранить'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</UploaderContextProvider>
|
||||
</FormikProvider>
|
||||
</form>
|
||||
</UploadDropzone>
|
||||
);
|
||||
});
|
||||
|
||||
<CommentFormAttaches />
|
||||
|
||||
<div className={styles.buttons}>
|
||||
<div className={styles.button_column}>
|
||||
<CommentFormAttachButtons onUpload={uploader.uploadFiles} />
|
||||
</div>
|
||||
|
||||
<div className={styles.button_column}>
|
||||
{!!textarea && (
|
||||
<CommentFormFormatButtons
|
||||
element={textarea}
|
||||
handler={formik.handleChange('text')}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Filler />
|
||||
|
||||
<div className={styles.button_column}>
|
||||
{isEditing && (
|
||||
<Button
|
||||
size="small"
|
||||
color="link"
|
||||
type="button"
|
||||
onClick={onCancelEdit}
|
||||
>
|
||||
Отмена
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
size="small"
|
||||
color="gray"
|
||||
iconRight={!isEditing ? 'enter' : 'check'}
|
||||
disabled={isLoading}
|
||||
loading={isLoading}
|
||||
>
|
||||
{!isEditing ? 'Сказать' : 'Сохранить'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</UploaderContextProvider>
|
||||
</FormikProvider>
|
||||
</form>
|
||||
</UploadDropzone>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
export { CommentForm };
|
||||
|
|
52
src/components/comment/WYSIWYGTextaArea/index.tsx
Normal file
52
src/components/comment/WYSIWYGTextaArea/index.tsx
Normal file
|
@ -0,0 +1,52 @@
|
|||
import React, { FC, useState } from 'react';
|
||||
|
||||
import {
|
||||
Editor,
|
||||
rootCtx,
|
||||
defaultValueCtx,
|
||||
editorViewOptionsCtx,
|
||||
} from '@milkdown/core';
|
||||
import { listener, listenerCtx } from '@milkdown/plugin-listener';
|
||||
import { commonmark } from '@milkdown/preset-commonmark';
|
||||
import { Milkdown, MilkdownProvider, useEditor } from '@milkdown/react';
|
||||
|
||||
import styles from './styles.module.scss';
|
||||
|
||||
interface MilkdownEditorProps {
|
||||
value?: string;
|
||||
onChange?: (val: string) => void;
|
||||
}
|
||||
|
||||
const MilkdownEditor: FC<MilkdownEditorProps> = ({ value = '', onChange }) => {
|
||||
useEditor(
|
||||
(root) =>
|
||||
Editor.make()
|
||||
.config((ctx) => {
|
||||
ctx.set(rootCtx, root);
|
||||
ctx.set(defaultValueCtx, value);
|
||||
ctx.get(listenerCtx).markdownUpdated((_, markdown) => {
|
||||
onChange?.(markdown);
|
||||
});
|
||||
|
||||
ctx.update(editorViewOptionsCtx, (prev) => ({
|
||||
...prev,
|
||||
attributes: {
|
||||
class: styles.editor,
|
||||
spellcheck: 'false',
|
||||
},
|
||||
}));
|
||||
})
|
||||
.use(commonmark)
|
||||
.use(listener),
|
||||
[onChange, value],
|
||||
);
|
||||
|
||||
return <Milkdown />;
|
||||
};
|
||||
|
||||
const WYSIWYGTextaArea: FC<MilkdownEditorProps> = ({ value, onChange }) => (
|
||||
<MilkdownProvider>
|
||||
<MilkdownEditor value={value} onChange={onChange} />
|
||||
</MilkdownProvider>
|
||||
);
|
||||
export { WYSIWYGTextaArea };
|
|
@ -0,0 +1,5 @@
|
|||
@import 'src/styles/markdown.scss';
|
||||
|
||||
.editor {
|
||||
@include markdown;
|
||||
}
|
95
src/styles/_markdown.scss
Normal file
95
src/styles/_markdown.scss
Normal file
|
@ -0,0 +1,95 @@
|
|||
@use 'sass:math';
|
||||
|
||||
@import './variables.scss';
|
||||
|
||||
$margin: 1em;
|
||||
|
||||
@mixin markdown {
|
||||
blockquote {
|
||||
border-left: 4px solid $color_primary;
|
||||
padding: $margin * 0.5 0;
|
||||
margin-bottom: $margin;
|
||||
color: $color_primary;
|
||||
font-size: 0.9em;
|
||||
background-color: $content_bg_success;
|
||||
|
||||
p {
|
||||
margin: 0 $margin;
|
||||
}
|
||||
|
||||
& + blockquote {
|
||||
margin-top: -$margin;
|
||||
}
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: $content_bg_info;
|
||||
font-family: monospace;
|
||||
font-size: 0.8em;
|
||||
border-radius: 2px;
|
||||
width: 100%;
|
||||
overflow: auto;
|
||||
padding: $gap;
|
||||
box-sizing: border-box;
|
||||
margin-bottom: $margin;
|
||||
|
||||
code {
|
||||
color: $gray_50;
|
||||
font-family: monospace;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
}
|
||||
|
||||
p,
|
||||
li {
|
||||
code {
|
||||
background-color: $content_bg_info;
|
||||
padding: 2px 4px;
|
||||
border-radius: 0.3em;
|
||||
color: $gray_25;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
}
|
||||
|
||||
img {
|
||||
max-width: 100%;
|
||||
margin-bottom: $margin;
|
||||
}
|
||||
|
||||
h5,
|
||||
h4,
|
||||
h3,
|
||||
h2,
|
||||
h1 {
|
||||
line-height: 1.2em;
|
||||
margin: $margin * 1.5 0 $margin * 0.5;
|
||||
|
||||
&:first-child {
|
||||
margin-top: $margin * 0.25;
|
||||
}
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style: disc;
|
||||
padding-left: 20px;
|
||||
margin-bottom: $margin;
|
||||
|
||||
li {
|
||||
margin: math.div($margin, 3) 0;
|
||||
}
|
||||
}
|
||||
|
||||
strong {
|
||||
font-weight: bold;
|
||||
color: white;
|
||||
}
|
||||
|
||||
em {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
:global(.grey) {
|
||||
color: #666666;
|
||||
white-space: pre-line;
|
||||
}
|
||||
}
|
|
@ -1,95 +1,6 @@
|
|||
@use 'sass:math';
|
||||
|
||||
@import '../variables';
|
||||
|
||||
$margin: 1em;
|
||||
@import '../markdown';
|
||||
|
||||
.wrapper {
|
||||
blockquote {
|
||||
border-left: 4px solid $color_primary;
|
||||
padding: $margin * 0.5 0;
|
||||
margin-bottom: $margin;
|
||||
color: $color_primary;
|
||||
font-size: 0.9em;
|
||||
background-color: $content_bg_success;
|
||||
|
||||
p {
|
||||
margin: 0 $margin;
|
||||
}
|
||||
|
||||
& + blockquote {
|
||||
margin-top: -$margin;
|
||||
}
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: $content_bg_info;
|
||||
font-family: monospace;
|
||||
font-size: 0.8em;
|
||||
border-radius: 2px;
|
||||
width: 100%;
|
||||
overflow: auto;
|
||||
padding: $gap;
|
||||
box-sizing: border-box;
|
||||
margin-bottom: $margin;
|
||||
|
||||
code {
|
||||
color: $gray_50;
|
||||
font-family: monospace;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
}
|
||||
|
||||
p,
|
||||
li {
|
||||
code {
|
||||
background-color: $content_bg_info;
|
||||
padding: 2px 4px;
|
||||
border-radius: 0.3em;
|
||||
color: $gray_25;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
}
|
||||
|
||||
img {
|
||||
max-width: 100%;
|
||||
margin-bottom: $margin;
|
||||
}
|
||||
|
||||
h5,
|
||||
h4,
|
||||
h3,
|
||||
h2,
|
||||
h1 {
|
||||
line-height: 1.2em;
|
||||
margin: $margin * 1.5 0 $margin * 0.5;
|
||||
|
||||
&:first-child {
|
||||
margin-top: $margin * 0.25;
|
||||
}
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style: disc;
|
||||
padding-left: 20px;
|
||||
margin-bottom: $margin;
|
||||
|
||||
li {
|
||||
margin: math.div($margin, 3) 0;
|
||||
}
|
||||
}
|
||||
|
||||
strong {
|
||||
font-weight: bold;
|
||||
color: white;
|
||||
}
|
||||
|
||||
em {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
:global(.grey) {
|
||||
color: #666666;
|
||||
white-space: pre-line;
|
||||
}
|
||||
@include markdown;
|
||||
}
|
||||
|
|
4270
yarn-error.log
Normal file
4270
yarn-error.log
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue