From 35d185a971973db557c8ddbafd4ef2f8665e322a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 6 Jul 2024 10:43:22 -0400 Subject: [PATCH] wip --- app/components/ContextMenu/MenuItem.tsx | 2 +- app/models/Document.ts | 11 +- .../Document/components/DocumentMeta.tsx | 87 ++++-- app/scenes/Document/components/Properties.tsx | 286 +++++++++++------- app/utils/DataAttributesHelper.tsx | 4 +- shared/models/types.ts | 8 + shared/utils/IconLibrary.tsx | 4 +- 7 files changed, 251 insertions(+), 151 deletions(-) diff --git a/app/components/ContextMenu/MenuItem.tsx b/app/components/ContextMenu/MenuItem.tsx index 939f84f87b..b6787bccb6 100644 --- a/app/components/ContextMenu/MenuItem.tsx +++ b/app/components/ContextMenu/MenuItem.tsx @@ -22,7 +22,7 @@ type Props = { as?: string | React.ComponentType; hide?: () => void; level?: number; - icon?: React.ReactElement; + icon?: React.ReactElement | null; children?: React.ReactNode; ref?: React.LegacyRef | undefined; }; diff --git a/app/models/Document.ts b/app/models/Document.ts index 746a177531..b0b70fb66e 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -7,7 +7,10 @@ import { Node, Schema } from "prosemirror-model"; import { Primitive } from "utility-types"; import ExtensionManager from "@shared/editor/lib/ExtensionManager"; import { richExtensions, withComments } from "@shared/editor/nodes"; -import { DataAttributeDataType } from "@shared/models/types"; +import { + DataAttributeDataType, + DocumentDataAttribute, +} from "@shared/models/types"; import type { JSONObject, NavigationNode, @@ -66,11 +69,7 @@ export default class Document extends ParanoidModel { @Field @observable - dataAttributes: { - dataAttributeId: string; - value: Primitive; - updatedAt: string; - }[]; + dataAttributes: DocumentDataAttribute[]; store: DocumentsStore; diff --git a/app/scenes/Document/components/DocumentMeta.tsx b/app/scenes/Document/components/DocumentMeta.tsx index b30a41d62c..4442871ccb 100644 --- a/app/scenes/Document/components/DocumentMeta.tsx +++ b/app/scenes/Document/components/DocumentMeta.tsx @@ -4,18 +4,22 @@ import { CommentIcon, PlusIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link, useRouteMatch } from "react-router-dom"; +import { MenuButton, useMenuState } from "reakit"; import styled from "styled-components"; import { TeamPreference } from "@shared/types"; import Document from "~/models/Document"; import Revision from "~/models/Revision"; +import ContextMenu from "~/components/ContextMenu"; +import MenuItem from "~/components/ContextMenu/MenuItem"; import DocumentMeta from "~/components/DocumentMeta"; import Fade from "~/components/Fade"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; +import { DataAttributesHelper } from "~/utils/DataAttributesHelper"; import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; import { documentPath, documentInsightsPath } from "~/utils/routeHelpers"; -import { Properties } from "./Properties"; +import { Properties, PropertiesRef } from "./Properties"; type Props = { /* The document to display meta data for */ @@ -35,6 +39,7 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { const onlyYou = totalViewers === 1 && documentViews[0].userId; const viewsLoadedOnMount = React.useRef(totalViewers > 0); const can = usePolicy(document); + const propertiesRef = React.useRef(null); const Wrapper = viewsLoadedOnMount.current ? React.Fragment : Fade; @@ -44,25 +49,8 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { const dataAttributesAvailable = FeatureFlags.isEnabled(Feature.dataAttributes) && dataAttributes.orderedData.length > 0; - const hasDataAttributes = document.dataAttributes?.length > 0; - - const addProperty = ( - - setDraftAttribute((state) => - state - ? null - : { - value: "", - dataAttributeId: dataAttributes.orderedData[0].id, - } - ) - } - > - Property - - ); + const missingDataAttributes = + document.dataAttributes?.length < dataAttributes.orderedData.length; return ( <> @@ -81,8 +69,14 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { )} - {dataAttributesAvailable && !hasDataAttributes ? ( - <> • {addProperty} + {dataAttributesAvailable && missingDataAttributes ? ( + <> +  •  + + ) : null} {totalViewers && can.listViews && @@ -107,11 +101,58 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { ) : null} - + ); } +const AddPropertyMenu = ({ + propertiesRef, + document, +}: { + document: Document; + propertiesRef: React.RefObject; +}) => { + const { dataAttributes } = useStores(); + const { t } = useTranslation(); + const menu = useMenuState({ + modal: true, + }); + + return ( + <> + + {(props) => ( + + {t("Property")} + + )} + + + {dataAttributes.orderedData + .filter( + (a) => + !a.deletedAt && + !document.dataAttributes?.find((d) => d.dataAttributeId === a.id) + ) + .map((attribute) => ( + propertiesRef.current?.addProperty(attribute.id)} + > + {attribute.name} + + ))} + + + ); +}; + const InlineLink = styled(Link)` display: inline-flex; align-items: center; diff --git a/app/scenes/Document/components/Properties.tsx b/app/scenes/Document/components/Properties.tsx index 187f7f83df..9e594d6821 100644 --- a/app/scenes/Document/components/Properties.tsx +++ b/app/scenes/Document/components/Properties.tsx @@ -1,146 +1,194 @@ import { isEmail, isPhoneNumber } from "class-validator"; import { observer } from "mobx-react"; -import { CloseIcon, PlusIcon } from "outline-icons"; +import { CloseIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; 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 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"; import { DataAttributesHelper } from "~/utils/DataAttributesHelper"; -import { documentPath } from "~/utils/routeHelpers"; +import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; type Props = { document: Document; }; -export const Properties = observer(function Properties_({ document }: Props) { - const { dataAttributes } = useStores(); - const { t } = useTranslation(); - const [draftAttribute, setDraftAttribute] = React.useState<{ - dataAttributeId: string; - value: string; - } | null>(null); +export type PropertiesRef = { + addProperty: (dataAttributeId: string) => void; +}; - const addProperty = ( - - setDraftAttribute((state) => - state - ? null - : { - value: "", - dataAttributeId: dataAttributes.orderedData[0].id, - } - ) +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 handleAddProperty = (dataAttributeId: string) => + setDraftAttribute((state) => + state + ? null + : { + value: "", + dataAttributeId, + } + ); + + React.useImperativeHandle(ref, () => ({ + addProperty: handleAddProperty, + })); + + const handleSave = async () => { + if (draftAttribute?.value) { + document.setDataAttribute( + draftAttribute.dataAttributeId, + draftAttribute.value + ); + await document.save(); } - > - -  Property - + }; + + if (!FeatureFlags.isEnabled(Feature.dataAttributes)) { + return null; + } + + const definition = dataAttributes.get(draftAttribute?.dataAttributeId); + + return ( + + {document.dataAttributes?.map((dataAttribute) => ( + + ))} + {draftAttribute && ( + + {definition.name} + {definition.dataType === DataAttributeDataType.List ? ( + ({ + label: option.value, + value: option.value, + }))} + value={draftAttribute?.value ?? ""} + onChange={(event) => { + if (event) { + setDraftAttribute({ + ...draftAttribute, + value: event, + }); + handleSave(); + } + }} + /> + ) : definition.dataType === DataAttributeDataType.Boolean ? ( + { + setDraftAttribute({ + ...draftAttribute, + value: event.currentTarget.checked, + }); + handleSave(); + }} + /> + ) : ( + + setDraftAttribute({ + ...draftAttribute, + value: event.target.value, + }) + } + autoFocus + /> + )} + + )} + + ); + }) +); + +const Property = observer(function Property_({ + document, + dataAttribute, +}: { + document: Document; + dataAttribute: DocumentDataAttribute; +}) { + const { t } = useTranslation(); + const { dataAttributes } = useStores(); + const definition = dataAttributes.get(dataAttribute.dataAttributeId); + const value = String(dataAttribute.value); + + if (!definition) { + return null; + } + + const displayedValue = isUrl(value) ? ( + + {value.replace(/^https?:\/\//, "")} + + ) : isEmail(value) ? ( + + {value} + + ) : isPhoneNumber(value) ? ( + + {value} + + ) : ( + value ); return ( - - {document.dataAttributes?.map((dataAttribute) => { - const definition = dataAttributes.get(dataAttribute.dataAttributeId); - const value = String(dataAttribute.value); - - const displayedValue = isUrl(value) ? ( - - {value} - - ) : isEmail(value) ? ( - - {value} - - ) : isPhoneNumber(value) ? ( - - {value} - - ) : ( - value - ); - - return ( - -
- {definition - ? DataAttributesHelper.getIcon( - definition.dataType, - definition.name, - { - size: 18, - } - ) - : null} - {definition?.name} -
-
- {displayedValue} - { - document.deleteDataAttribute(dataAttribute.dataAttributeId); - void document.save(); - }} - > - - -
-
- ); - })} - {document.dataAttributes?.length > 0 && addProperty} - {draftAttribute && ( - - ({ - label: - attribute.name + - " – " + - DataAttributesHelper.getName(attribute.dataType, t), - value: attribute.id, - }))} - value={ - draftAttribute?.dataAttributeId ?? - dataAttributes.orderedData[0].id - } - onChange={(dataAttributeId) => - dataAttributeId && - setDraftAttribute({ ...draftAttribute, dataAttributeId }) - } - /> - - { - if (draftAttribute.value) { - document.setDataAttribute( - draftAttribute.dataAttributeId, - draftAttribute.value - ); - await document.save(); - } + +
+ {definition + ? DataAttributesHelper.getIcon(definition.dataType, definition.name, { + size: 18, + }) + : null} + {definition?.name} +
+
+ {displayedValue} + + { + document.deleteDataAttribute(dataAttribute.dataAttributeId); + void document.save(); }} - onChange={(event) => - setDraftAttribute({ - ...draftAttribute, - value: event.target.value, - }) - } - /> - - )} - + > + + + +
+
); }); @@ -161,6 +209,7 @@ const Dt = styled(Text)` float: left; clear: left; flex-basis: 20%; + margin-bottom: 2px; svg { position: relative; @@ -171,6 +220,9 @@ const Dt = styled(Text)` const Dd = styled(Text)` flex-basis: 70%; + display: flex; + align-items: center; + margin-bottom: 2px; &:hover { ${RemoveButton} { diff --git a/app/utils/DataAttributesHelper.tsx b/app/utils/DataAttributesHelper.tsx index 6315b95e86..d2cfa0b6fb 100644 --- a/app/utils/DataAttributesHelper.tsx +++ b/app/utils/DataAttributesHelper.tsx @@ -61,14 +61,14 @@ export class DataAttributesHelper { /** * Returns the regex to validate the input of the data attribute * @param dataAttribute - * @returns A regex or null + * @returns A regex or undefined */ public static getValidationRegex(dataAttribute: DataAttribute) { switch (dataAttribute.dataType) { case DataAttributeDataType.Number: return /^[0-9]+$/; default: - return null; + return undefined; } } } diff --git a/shared/models/types.ts b/shared/models/types.ts index b03ef605fc..1eba4d0662 100644 --- a/shared/models/types.ts +++ b/shared/models/types.ts @@ -1,3 +1,5 @@ +import { Primitive } from "utility-types"; + export enum DataAttributeDataType { String = "string", Number = "number", @@ -5,6 +7,12 @@ export enum DataAttributeDataType { List = "list", } +export type DocumentDataAttribute = { + dataAttributeId: string; + value: Primitive; + updatedAt: string; +}; + export type DataAttributeOptions = { /** An icon to display next to the attribute. */ icon?: string; diff --git a/shared/utils/IconLibrary.tsx b/shared/utils/IconLibrary.tsx index 7e8d27dd7d..64b619e24d 100644 --- a/shared/utils/IconLibrary.tsx +++ b/shared/utils/IconLibrary.tsx @@ -249,7 +249,7 @@ export class IconLibrary { }, browser: { component: BrowserIcon, - keywords: "browser web website app", + keywords: "browser web app", }, collection: { component: (props) => , @@ -309,7 +309,7 @@ export class IconLibrary { }, globe: { component: GlobeIcon, - keywords: "world translate", + keywords: "world website translate", }, hashtag: { component: HashtagIcon,