diff --git a/app/scenes/Document/components/Properties.tsx b/app/scenes/Document/components/Properties.tsx index f598346845..8a7bfe5118 100644 --- a/app/scenes/Document/components/Properties.tsx +++ b/app/scenes/Document/components/Properties.tsx @@ -1,21 +1,21 @@ import { isEmail, isPhoneNumber } from "class-validator"; import { observer } from "mobx-react"; -import { CloseIcon } from "outline-icons"; +import { CloseIcon, EditIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import styled from "styled-components"; import { Primitive } from "utility-types"; -import Flex from "@shared/components/Flex"; import { DataAttributeDataType, DocumentDataAttribute, } from "@shared/models/types"; import { isUrl } from "@shared/utils/urls"; import Document from "~/models/Document"; +import { Inner } from "~/components/Button"; +import ButtonSmall from "~/components/ButtonSmall"; import Input from "~/components/Input"; import InputSelect from "~/components/InputSelect"; import NudeButton from "~/components/NudeButton"; -import Switch from "~/components/Switch"; import Text from "~/components/Text"; import Tooltip from "~/components/Tooltip"; import useStores from "~/hooks/useStores"; @@ -32,11 +32,8 @@ export type PropertiesRef = { export const Properties = observer( React.forwardRef(function Properties_({ document }: Props, ref) { - const { dataAttributes } = useStores(); - const [draftAttribute, setDraftAttribute] = React.useState<{ - dataAttributeId: string; - value: Primitive; - } | null>(null); + const [draftAttribute, setDraftAttribute] = + React.useState(null); const handleAddProperty = (dataAttributeId: string) => setDraftAttribute((state) => @@ -45,6 +42,7 @@ export const Properties = observer( : { value: "", dataAttributeId, + updatedAt: new Date().toISOString(), } ); @@ -66,74 +64,41 @@ export const Properties = observer( return null; } - const definition = draftAttribute?.dataAttributeId - ? dataAttributes.get(draftAttribute?.dataAttributeId) - : undefined; - return ( {document.dataAttributes?.map((dataAttribute) => ( { + document.setDataAttribute(dataAttribute.dataAttributeId, value); + }} + onSubmit={() => { + void document.save(); + }} + onRemove={() => { + document.deleteDataAttribute(dataAttribute.dataAttributeId); + void document.save(); + }} /> ))} {draftAttribute && ( - - {definition?.name} - {definition?.dataType === DataAttributeDataType.List ? ( - ({ - label: option.value, - value: option.value, - })) ?? [] - } - value={String(draftAttribute?.value) ?? ""} - onChange={(event) => { - if (event) { - setDraftAttribute({ - ...draftAttribute, - value: event, - }); - void handleSave(); - } - }} - /> - ) : definition?.dataType === DataAttributeDataType.Boolean ? ( - { - setDraftAttribute({ - ...draftAttribute, - value: event.currentTarget.checked, - }); - void handleSave(); - }} - /> - ) : ( - - setDraftAttribute({ - ...draftAttribute, - value: event.target.value, - }) - } - autoFocus - /> - )} - + { + setDraftAttribute({ + ...draftAttribute, + value, + }); + }} + onRemove={() => setDraftAttribute(null)} + onSubmit={() => { + setDraftAttribute(null); + void handleSave(); + }} + isEditing + /> )} ); @@ -141,14 +106,21 @@ export const Properties = observer( ); const Property = observer(function Property_({ - document, dataAttribute, + onChange, + onSubmit, + onRemove, + ...props }: { - document: Document; dataAttribute: DocumentDataAttribute; + isEditing?: boolean; + onChange: (value: Primitive) => void; + onSubmit: () => void; + onRemove: () => void; }) { const { t } = useTranslation(); const { dataAttributes } = useStores(); + const [isEditing, setIsEditing] = React.useState(props.isEditing ?? false); const definition = dataAttributes.get(dataAttribute.dataAttributeId); const value = String(dataAttribute.value); @@ -168,39 +140,140 @@ const Property = observer(function Property_({ {value} + ) : dataAttribute.value === true ? ( + t("Yes") + ) : dataAttribute.value === false ? ( + t("No") ) : ( value ); + const handleSubmit = () => { + setIsEditing(false); + onSubmit(); + }; + + const inputId = `data-attribute-${dataAttribute.dataAttributeId}`; + const displayedInput = isEditing ? ( + <> + {definition?.dataType === DataAttributeDataType.List ? ( + ({ + label: option.value, + value: option.value, + })) ?? [] + } + value={String(dataAttribute?.value) ?? ""} + onChange={onChange} + /> + ) : definition?.dataType === DataAttributeDataType.Boolean ? ( + + ) : ( + onChange(event.target.value)} + autoFocus + /> + )} + + ) : null; + return (
- {definition - ? DataAttributesHelper.getIcon(definition.dataType, definition.name, { - size: 18, - }) - : null} - {definition?.name} +
- {displayedValue} - - { - document.deleteDataAttribute(dataAttribute.dataAttributeId); - void document.save(); - }} - > - - - + {displayedInput ?? displayedValue} + {isEditing ? ( + + {t("Save")} + + ) : ( + <> + + setIsEditing(true)} + $isEditing={isEditing} + > + + + + + + + + + + )}
); }); -const RemoveButton = styled(NudeButton)` - opacity: 0; +const Label = styled.label` + white-space: nowrap; +`; + +const HoverButton = styled(NudeButton)<{ $isEditing: boolean }>` + opacity: ${(props) => (props.$isEditing ? 1 : 0)}; +`; + +const StyledInputSelect = styled(InputSelect)` + padding: 0; + margin: -4px; + margin-right: 8px; + height: 26px; + + ${Inner} { + line-height: 14px; + min-height: auto; + } +`; + +const StyledInput = styled(Input)` + padding: 0; + margin: -4px; + margin-right: 4px; + + input { + padding: 4px 8px; + height: 24px; + } `; const List = styled.dl` @@ -217,6 +290,7 @@ const Dt = styled(Text)` clear: left; flex-basis: 20%; margin-bottom: 2px; + height: 26px; svg { position: relative; @@ -232,7 +306,7 @@ const Dd = styled(Text)` margin-bottom: 2px; &:hover { - ${RemoveButton} { + ${HoverButton} { opacity: 1; } } diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 45d86c7cdc..f0d763e615 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -626,6 +626,8 @@ "Archived by {{userName}}": "Archived by {{userName}}", "Deleted by {{userName}}": "Deleted by {{userName}}", "Observing {{ userName }}": "Observing {{ userName }}", + "Yes": "Yes", + "No": "No", "Backlinks": "Backlinks", "Close": "Close", "{{ teamName }} is using {{ appName }} to share documents, please login to continue.": "{{ teamName }} is using {{ appName }} to share documents, please login to continue.",