|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 5 | + */ |
| 6 | + |
| 7 | +import ISO from 'iso-639-1' |
| 8 | +import React, { useCallback } from 'react' |
| 9 | +import { Col, Modal, Row } from 'react-bootstrap' |
| 10 | +import { useTranslation } from 'react-i18next' |
| 11 | +import { useSelector } from 'react-redux' |
| 12 | +import { ApplicationState } from '../../../redux' |
| 13 | +import { setNoteFrontmatter } from '../../../redux/note-details/methods' |
| 14 | +import { CommonModal } from '../../common/modals/common-modal' |
| 15 | +import { NoteFrontmatter, NoteType } from '../note-frontmatter/note-frontmatter' |
| 16 | +import { BreaksMetadataInput } from './breaks-metadata-input' |
| 17 | +import { DatalistMetadataInput } from './datalist-metadata-input' |
| 18 | +import { InputLabel } from './input-label' |
| 19 | +import { StringMetadataInput } from './string-metadata-input' |
| 20 | +import { StringMetadataTextarea } from './string-metadata-textarea' |
| 21 | +import { TagsMetadataInput } from './tags-metadata-input' |
| 22 | +import { TextDirectionMetadataInput } from './text-direction-metadata-input' |
| 23 | + |
| 24 | +export interface MetadataEditorProps { |
| 25 | + show: boolean, |
| 26 | + onHide: () => void |
| 27 | +} |
| 28 | + |
| 29 | +export interface MetadataInputFieldProps<T> { |
| 30 | + id: string |
| 31 | + content: T |
| 32 | + onContentChange: (newContent: T) => void |
| 33 | +} |
| 34 | + |
| 35 | +export interface SelectMetadataOptions<T> { |
| 36 | + options: T[] |
| 37 | +} |
| 38 | + |
| 39 | +export const MetadataEditor: React.FC<MetadataEditorProps> = ({ show, onHide }) => { |
| 40 | + const { t } = useTranslation() |
| 41 | + const yamlMetadata = useSelector((state: ApplicationState) => state.noteDetails.frontmatter) |
| 42 | + const noteDetails = useSelector((state: ApplicationState) => state.noteDetails.markdownContent) |
| 43 | + /*const [yamlMetadata, setNoteFrontmatter] = useState<Omit<YAMLMetaData, 'opengraph'>>({ |
| 44 | + title: "Test Title", |
| 45 | + description: "Test Description\nwith two lines", |
| 46 | + tags: ["tag1", "tag2"], |
| 47 | + robots: "", |
| 48 | + lang: "de-at", |
| 49 | + dir: TextDirection.LTR, |
| 50 | + breaks: false, |
| 51 | + GA: "test GA string", |
| 52 | + disqus: "test disqus string", |
| 53 | + type: '', |
| 54 | + deprecatedTagsSyntax: false |
| 55 | + })*/ |
| 56 | + |
| 57 | + const setMarkdown = useCallback((changes: Partial<NoteFrontmatter>) => { |
| 58 | + const newMetadata = Object.assign(yamlMetadata, changes) |
| 59 | + |
| 60 | +// setnoteDetails(noteDetails) |
| 61 | + }, [noteDetails]) |
| 62 | + |
| 63 | + return ( |
| 64 | + <CommonModal |
| 65 | + size='lg' |
| 66 | + show={ show } |
| 67 | + onHide={ onHide } |
| 68 | + closeButton={ true } |
| 69 | + titleI18nKey={ 'editor.modal.metadataEditor.title' }> |
| 70 | + <Modal.Body> |
| 71 | + <Row> |
| 72 | + <Col xs={ 6 }> |
| 73 | + <InputLabel id={ 'title' } label={ t('editor.modal.metadataEditor.labels.title') }> |
| 74 | + <StringMetadataInput id={ 'title' } content={ yamlMetadata.title } |
| 75 | + onContentChange={ title => setNoteFrontmatter({ ...yamlMetadata, title }) }/> |
| 76 | + </InputLabel> |
| 77 | + <InputLabel id={ 'type' } label={ t('editor.modal.metadataEditor.labels.type') }> |
| 78 | + <DatalistMetadataInput id={ 'type' } options={ Object.values(NoteType) } content={ yamlMetadata.type } |
| 79 | + onContentChange={ type => setNoteFrontmatter({ ...yamlMetadata, type: (type as NoteType) }) }/> |
| 80 | + </InputLabel> |
| 81 | + <InputLabel id={ 'dir' } label={ t('editor.modal.metadataEditor.labels.dir') }> |
| 82 | + <TextDirectionMetadataInput id={ 'dir' } content={ yamlMetadata.dir } |
| 83 | + onContentChange={ dir => setNoteFrontmatter({ ...yamlMetadata, dir }) }/> |
| 84 | + </InputLabel> |
| 85 | + <InputLabel id={ 'description' } label={ t('editor.modal.metadataEditor.labels.description') }> |
| 86 | + <StringMetadataTextarea id={ 'description' } content={ yamlMetadata.description } |
| 87 | + onContentChange={ description => setNoteFrontmatter({ ...yamlMetadata, description }) }/> |
| 88 | + </InputLabel> |
| 89 | + <InputLabel id={ 'disqus' } label={ t('editor.modal.metadataEditor.labels.disqus') }> |
| 90 | + <StringMetadataInput id={ 'disqus' } content={ yamlMetadata.disqus } |
| 91 | + onContentChange={ disqus => setNoteFrontmatter({ ...yamlMetadata, disqus }) }/> |
| 92 | + </InputLabel> |
| 93 | + </Col> |
| 94 | + <Col xs={ 6 }> |
| 95 | + <InputLabel id={ 'lang' } label={ t('editor.modal.metadataEditor.labels.lang') }> |
| 96 | + <DatalistMetadataInput id={ 'lang' } options={ ISO.getAllCodes() } content={ yamlMetadata.lang } |
| 97 | + onContentChange={ lang => setNoteFrontmatter({ ...yamlMetadata, lang }) }/> |
| 98 | + </InputLabel> |
| 99 | + <InputLabel id={ 'robots' } label={ t('editor.modal.metadataEditor.labels.robots') }> |
| 100 | + <StringMetadataInput id={ 'robots' } content={ yamlMetadata.robots } |
| 101 | + onContentChange={ robots => setNoteFrontmatter({ ...yamlMetadata, robots }) }/> |
| 102 | + </InputLabel> |
| 103 | + <InputLabel id={ 'breaks' } label={ t('editor.modal.metadataEditor.labels.breaks') }> |
| 104 | + <BreaksMetadataInput id={ 'breaks' } content={ yamlMetadata.breaks } |
| 105 | + onContentChange={ breaks => setNoteFrontmatter({ ...yamlMetadata, breaks }) }/> |
| 106 | + </InputLabel> |
| 107 | + <InputLabel id={ 'tags' } label={ t('editor.modal.metadataEditor.labels.tags') }> |
| 108 | + <TagsMetadataInput id={ 'tags' } content={ yamlMetadata.tags } |
| 109 | + onContentChange={ tags => setNoteFrontmatter({ ...yamlMetadata, tags }) }/> |
| 110 | + </InputLabel> |
| 111 | + <InputLabel id={ 'GA' } label={ t('editor.modal.metadataEditor.labels.GA') }> |
| 112 | + <StringMetadataInput id={ 'GA' } content={ yamlMetadata.GA } |
| 113 | + onContentChange={ GA => setNoteFrontmatter({ ...yamlMetadata, GA }) }/> |
| 114 | + </InputLabel> |
| 115 | + </Col> |
| 116 | + </Row> |
| 117 | + </Modal.Body> |
| 118 | + </CommonModal> |
| 119 | + ) |
| 120 | +} |
0 commit comments