From 1e8d355bed766ff3f679b5843cd23606a0f03cfb Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Wed, 17 Mar 2021 22:19:45 +0100 Subject: [PATCH] Refactor metadata input and redux action for replacement Signed-off-by: Tilman Vatteroth --- .../metadata-editor/breaks-metadata-input.tsx | 25 ++++---- .../datalist-metadata-input.tsx | 44 ++++++++------ .../metadata-editor/metadata-editor.tsx | 58 +++++++++---------- .../metadata-editor/string-metadata-input.tsx | 8 +-- .../string-metadata-textarea.tsx | 6 +- .../metadata-editor/tags-metadata-input.tsx | 32 +++++----- .../text-direction-metadata-input.tsx | 27 +++++---- .../note-frontmatter/note-frontmatter.ts | 28 ++++----- src/redux/note-details/methods.ts | 10 +++- src/redux/note-details/reducers.ts | 44 ++++++++++++-- src/redux/note-details/types.ts | 10 +++- 11 files changed, 180 insertions(+), 112 deletions(-) diff --git a/src/components/editor-page/metadata-editor/breaks-metadata-input.tsx b/src/components/editor-page/metadata-editor/breaks-metadata-input.tsx index d8fb206786..d8d996a2b9 100644 --- a/src/components/editor-page/metadata-editor/breaks-metadata-input.tsx +++ b/src/components/editor-page/metadata-editor/breaks-metadata-input.tsx @@ -4,38 +4,41 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import React from 'react' +import React, { useCallback } from 'react' import { ToggleButton, ToggleButtonGroup } from 'react-bootstrap' import { Trans, useTranslation } from 'react-i18next' import { MetadataInputFieldProps } from './metadata-editor' enum ButtonState { - ON, - OFF + ON = 1, + OFF = 0 } -export const BreaksMetadataInput: React.FC> = ({ id, content, onContentChange }) => { +export const BreaksMetadataInput: React.FC> = ({ frontmatterKey, content, onContentChange }) => { const { t } = useTranslation() + const toggleButtonClick = useCallback((value: ButtonState) => { + onContentChange({ breaks: value === ButtonState.ON }) + }, [onContentChange]) + return ( + className={ 'd-block' } + onChange={ toggleButtonClick }> onContentChange(true) }> + title={ t('editor.modal.metadataEditor.labels.breaksOn') }> onContentChange(false) }> + title={ t('editor.modal.metadataEditor.labels.breaksOff') }> diff --git a/src/components/editor-page/metadata-editor/datalist-metadata-input.tsx b/src/components/editor-page/metadata-editor/datalist-metadata-input.tsx index b1a89b9e98..968d81dd3b 100644 --- a/src/components/editor-page/metadata-editor/datalist-metadata-input.tsx +++ b/src/components/editor-page/metadata-editor/datalist-metadata-input.tsx @@ -4,26 +4,36 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import React, { Fragment, useCallback } from 'react' +import React, { useCallback } from 'react' import { MetadataInputFieldProps, SelectMetadataOptions } from './metadata-editor' +import { Dropdown, DropdownButton } from 'react-bootstrap' -export const DatalistMetadataInput: React.FC & SelectMetadataOptions> = ({ id, content, onContentChange, options }) => { - const onChange = useCallback((event: React.ChangeEvent) => { - onContentChange(event.currentTarget.value) - }, [onContentChange]) +export const DatalistMetadataInput: React.FC & SelectMetadataOptions> = ({ frontmatterKey, content, onContentChange, options }) => { + const onSelect = useCallback((eventKey: string | null) => { + if (eventKey === null) { + return + } + onContentChange({ [frontmatterKey]: eventKey }) + }, [frontmatterKey, onContentChange]) return ( - - - - { options.map(option => { - return ( - - ) - }) } - - + + { + options.map((option) => { option }) + } + ) } +/* + + + */ diff --git a/src/components/editor-page/metadata-editor/metadata-editor.tsx b/src/components/editor-page/metadata-editor/metadata-editor.tsx index f913f3933c..5a2c8fc8a3 100644 --- a/src/components/editor-page/metadata-editor/metadata-editor.tsx +++ b/src/components/editor-page/metadata-editor/metadata-editor.tsx @@ -10,9 +10,9 @@ import { Col, Modal, Row } from 'react-bootstrap' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import { ApplicationState } from '../../../redux' -import { setNoteFrontmatter } from '../../../redux/note-details/methods' +import { replaceFrontmatterInMarkdownContentAction } from '../../../redux/note-details/methods' import { CommonModal } from '../../common/modals/common-modal' -import { NoteFrontmatter, NoteType } from '../note-frontmatter/note-frontmatter' +import { NoteType, RawNoteFrontmatter } from '../note-frontmatter/note-frontmatter' import { BreaksMetadataInput } from './breaks-metadata-input' import { DatalistMetadataInput } from './datalist-metadata-input' import { InputLabel } from './input-label' @@ -27,9 +27,9 @@ export interface MetadataEditorProps { } export interface MetadataInputFieldProps { - id: string content: T - onContentChange: (newContent: T) => void + frontmatterKey: keyof RawNoteFrontmatter + onContentChange: (frontmatter: RawNoteFrontmatter) => void } export interface SelectMetadataOptions { @@ -54,11 +54,9 @@ export const MetadataEditor: React.FC = ({ show, onHide }) deprecatedTagsSyntax: false })*/ - const setMarkdown = useCallback((changes: Partial) => { - const newMetadata = Object.assign(yamlMetadata, changes) - -// setnoteDetails(noteDetails) - }, [noteDetails]) + const updateFrontmatter = useCallback((frontmatter: RawNoteFrontmatter): void => { + replaceFrontmatterInMarkdownContentAction(frontmatter) + }, []) return ( = ({ show, onHide }) - setNoteFrontmatter({ ...yamlMetadata, title }) }/> + - setNoteFrontmatter({ ...yamlMetadata, type: (type as NoteType) }) }/> + - setNoteFrontmatter({ ...yamlMetadata, dir }) }/> + - setNoteFrontmatter({ ...yamlMetadata, description }) }/> + - setNoteFrontmatter({ ...yamlMetadata, disqus }) }/> + - setNoteFrontmatter({ ...yamlMetadata, lang }) }/> + - setNoteFrontmatter({ ...yamlMetadata, robots }) }/> + - setNoteFrontmatter({ ...yamlMetadata, breaks }) }/> + - setNoteFrontmatter({ ...yamlMetadata, tags }) }/> + - setNoteFrontmatter({ ...yamlMetadata, GA }) }/> + diff --git a/src/components/editor-page/metadata-editor/string-metadata-input.tsx b/src/components/editor-page/metadata-editor/string-metadata-input.tsx index 5d556c9505..aa684a472d 100644 --- a/src/components/editor-page/metadata-editor/string-metadata-input.tsx +++ b/src/components/editor-page/metadata-editor/string-metadata-input.tsx @@ -7,14 +7,14 @@ import React, { useCallback } from 'react' import { MetadataInputFieldProps } from './metadata-editor' -export const StringMetadataInput: React.FC> = ({ id, content, onContentChange }) => { +export const StringMetadataInput: React.FC> = ({ content, onContentChange, frontmatterKey }) => { const onChange = useCallback((event: React.ChangeEvent) => { - onContentChange(event.currentTarget.value) - }, [onContentChange]) + onContentChange({ [frontmatterKey]: event.currentTarget.value }) + }, [frontmatterKey, onContentChange]) return ( > = ({ id, content, onContentChange }) => { +export const StringMetadataTextarea: React.FC> = ({ frontmatterKey, content, onContentChange }) => { const onChange = useCallback((event: React.ChangeEvent) => { - onContentChange(event.currentTarget.value) + onContentChange({ [frontmatterKey]: event.currentTarget.value }) }, [onContentChange]) return (