mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
wip
This commit is contained in:
1 parent
d6c85f0aac
commit
35d185a971
7 files changed
+251
-151
No files matched your search
@@ -22,7 +22,7 @@ type Props = {
|
||||
as?: string | React.ComponentType<any>;
|
||||
hide?: () => void;
|
||||
level?: number;
|
||||
icon?: React.ReactElement;
|
||||
icon?: React.ReactElement | null;
|
||||
children?: React.ReactNode;
|
||||
ref?: React.LegacyRef<HTMLButtonElement> | undefined;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<PropertiesRef>(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 = (
|
||||
<InlineLink
|
||||
to={documentPath(document)}
|
||||
onClick={() =>
|
||||
setDraftAttribute((state) =>
|
||||
state
|
||||
? null
|
||||
: {
|
||||
value: "",
|
||||
dataAttributeId: dataAttributes.orderedData[0].id,
|
||||
}
|
||||
)
|
||||
}
|
||||
>
|
||||
<PlusIcon size={18} /> Property
|
||||
</InlineLink>
|
||||
);
|
||||
const missingDataAttributes =
|
||||
document.dataAttributes?.length < dataAttributes.orderedData.length;
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -81,8 +69,14 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
|
||||
</InlineLink>
|
||||
</>
|
||||
)}
|
||||
{dataAttributesAvailable && !hasDataAttributes ? (
|
||||
<> • {addProperty}</>
|
||||
{dataAttributesAvailable && missingDataAttributes ? (
|
||||
<>
|
||||
•
|
||||
<AddPropertyMenu
|
||||
document={document}
|
||||
propertiesRef={propertiesRef}
|
||||
/>
|
||||
</>
|
||||
) : null}
|
||||
{totalViewers &&
|
||||
can.listViews &&
|
||||
@@ -107,11 +101,58 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
|
||||
</Wrapper>
|
||||
) : null}
|
||||
</Meta>
|
||||
<Properties document={document} />
|
||||
<Properties document={document} ref={propertiesRef} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const AddPropertyMenu = ({
|
||||
propertiesRef,
|
||||
document,
|
||||
}: {
|
||||
document: Document;
|
||||
propertiesRef: React.RefObject<PropertiesRef>;
|
||||
}) => {
|
||||
const { dataAttributes } = useStores();
|
||||
const { t } = useTranslation();
|
||||
const menu = useMenuState({
|
||||
modal: true,
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<MenuButton {...menu}>
|
||||
{(props) => (
|
||||
<InlineLink to={documentPath(document)} {...props}>
|
||||
<PlusIcon size={18} /> {t("Property")}
|
||||
</InlineLink>
|
||||
)}
|
||||
</MenuButton>
|
||||
<ContextMenu {...menu}>
|
||||
{dataAttributes.orderedData
|
||||
.filter(
|
||||
(a) =>
|
||||
!a.deletedAt &&
|
||||
!document.dataAttributes?.find((d) => d.dataAttributeId === a.id)
|
||||
)
|
||||
.map((attribute) => (
|
||||
<MenuItem
|
||||
key={attribute.id}
|
||||
icon={DataAttributesHelper.getIcon(
|
||||
attribute.dataType,
|
||||
attribute.name
|
||||
)}
|
||||
{...menu}
|
||||
onClick={() => propertiesRef.current?.addProperty(attribute.id)}
|
||||
>
|
||||
{attribute.name}
|
||||
</MenuItem>
|
||||
))}
|
||||
</ContextMenu>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const InlineLink = styled(Link)`
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
|
||||
@@ -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 = (
|
||||
<InlineLink
|
||||
to={documentPath(document)}
|
||||
onClick={() =>
|
||||
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();
|
||||
}
|
||||
>
|
||||
<PlusIcon size={18} />
|
||||
Property
|
||||
</InlineLink>
|
||||
};
|
||||
|
||||
if (!FeatureFlags.isEnabled(Feature.dataAttributes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const definition = dataAttributes.get(draftAttribute?.dataAttributeId);
|
||||
|
||||
return (
|
||||
<List>
|
||||
{document.dataAttributes?.map((dataAttribute) => (
|
||||
<Property
|
||||
key={dataAttribute.dataAttributeId}
|
||||
dataAttribute={dataAttribute}
|
||||
document={document}
|
||||
/>
|
||||
))}
|
||||
{draftAttribute && (
|
||||
<Flex gap={8} auto>
|
||||
{definition.name}
|
||||
{definition.dataType === DataAttributeDataType.List ? (
|
||||
<InputSelect
|
||||
required
|
||||
ariaLabel={definition.name}
|
||||
options={definition.options?.options.map((option) => ({
|
||||
label: option.value,
|
||||
value: option.value,
|
||||
}))}
|
||||
value={draftAttribute?.value ?? ""}
|
||||
onChange={(event) => {
|
||||
if (event) {
|
||||
setDraftAttribute({
|
||||
...draftAttribute,
|
||||
value: event,
|
||||
});
|
||||
handleSave();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : definition.dataType === DataAttributeDataType.Boolean ? (
|
||||
<Switch
|
||||
checked={!!draftAttribute?.value}
|
||||
onChange={(event) => {
|
||||
setDraftAttribute({
|
||||
...draftAttribute,
|
||||
value: event.currentTarget.checked,
|
||||
});
|
||||
handleSave();
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<Input
|
||||
value={draftAttribute?.value ?? ""}
|
||||
onBlur={handleSave}
|
||||
pattern={DataAttributesHelper.getValidationRegex(definition)}
|
||||
required
|
||||
onChange={(event) =>
|
||||
setDraftAttribute({
|
||||
...draftAttribute,
|
||||
value: event.target.value,
|
||||
})
|
||||
}
|
||||
autoFocus
|
||||
/>
|
||||
)}
|
||||
</Flex>
|
||||
)}
|
||||
</List>
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
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) ? (
|
||||
<a href={value} target="_blank" rel="noopener noreferrer">
|
||||
{value.replace(/^https?:\/\//, "")}
|
||||
</a>
|
||||
) : isEmail(value) ? (
|
||||
<a href={`mailto:${value}`} target="_blank" rel="noopener noreferrer">
|
||||
{value}
|
||||
</a>
|
||||
) : isPhoneNumber(value) ? (
|
||||
<a href={`tel:${value}`} target="_blank" rel="noopener noreferrer">
|
||||
{value}
|
||||
</a>
|
||||
) : (
|
||||
value
|
||||
);
|
||||
|
||||
return (
|
||||
<List>
|
||||
{document.dataAttributes?.map((dataAttribute) => {
|
||||
const definition = dataAttributes.get(dataAttribute.dataAttributeId);
|
||||
const value = String(dataAttribute.value);
|
||||
|
||||
const displayedValue = isUrl(value) ? (
|
||||
<a href={value} target="_blank" rel="noopener noreferrer">
|
||||
{value}
|
||||
</a>
|
||||
) : isEmail(value) ? (
|
||||
<a href={`mailto:${value}`} target="_blank" rel="noopener noreferrer">
|
||||
{value}
|
||||
</a>
|
||||
) : isPhoneNumber(value) ? (
|
||||
<a href={`tel:${value}`} target="_blank" rel="noopener noreferrer">
|
||||
{value}
|
||||
</a>
|
||||
) : (
|
||||
value
|
||||
);
|
||||
|
||||
return (
|
||||
<React.Fragment key={dataAttribute.dataAttributeId}>
|
||||
<Dt type="tertiary" weight="bold" as="dt">
|
||||
{definition
|
||||
? DataAttributesHelper.getIcon(
|
||||
definition.dataType,
|
||||
definition.name,
|
||||
{
|
||||
size: 18,
|
||||
}
|
||||
)
|
||||
: null}
|
||||
{definition?.name}
|
||||
</Dt>
|
||||
<Dd type="tertiary" as="dd">
|
||||
{displayedValue}
|
||||
<RemoveButton
|
||||
onClick={() => {
|
||||
document.deleteDataAttribute(dataAttribute.dataAttributeId);
|
||||
void document.save();
|
||||
}}
|
||||
>
|
||||
<CloseIcon size={18} />
|
||||
</RemoveButton>
|
||||
</Dd>
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
{document.dataAttributes?.length > 0 && addProperty}
|
||||
{draftAttribute && (
|
||||
<Flex gap={8} auto>
|
||||
<InputSelect
|
||||
ariaLabel="Type"
|
||||
options={dataAttributes.orderedData.map((attribute) => ({
|
||||
label:
|
||||
attribute.name +
|
||||
" – " +
|
||||
DataAttributesHelper.getName(attribute.dataType, t),
|
||||
value: attribute.id,
|
||||
}))}
|
||||
value={
|
||||
draftAttribute?.dataAttributeId ??
|
||||
dataAttributes.orderedData[0].id
|
||||
}
|
||||
onChange={(dataAttributeId) =>
|
||||
dataAttributeId &&
|
||||
setDraftAttribute({ ...draftAttribute, dataAttributeId })
|
||||
}
|
||||
/>
|
||||
|
||||
<Input
|
||||
placeholder="Value"
|
||||
value={draftAttribute?.value ?? ""}
|
||||
onBlur={async () => {
|
||||
if (draftAttribute.value) {
|
||||
document.setDataAttribute(
|
||||
draftAttribute.dataAttributeId,
|
||||
draftAttribute.value
|
||||
);
|
||||
await document.save();
|
||||
}
|
||||
<React.Fragment key={dataAttribute.dataAttributeId}>
|
||||
<Dt type="tertiary" weight="bold" as="dt">
|
||||
{definition
|
||||
? DataAttributesHelper.getIcon(definition.dataType, definition.name, {
|
||||
size: 18,
|
||||
})
|
||||
: null}
|
||||
{definition?.name}
|
||||
</Dt>
|
||||
<Dd type="tertiary" as="dd">
|
||||
{displayedValue}
|
||||
<Tooltip content={t("Remove")} delay={500}>
|
||||
<RemoveButton
|
||||
onClick={() => {
|
||||
document.deleteDataAttribute(dataAttribute.dataAttributeId);
|
||||
void document.save();
|
||||
}}
|
||||
onChange={(event) =>
|
||||
setDraftAttribute({
|
||||
...draftAttribute,
|
||||
value: event.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Flex>
|
||||
)}
|
||||
</List>
|
||||
>
|
||||
<CloseIcon size={18} />
|
||||
</RemoveButton>
|
||||
</Tooltip>
|
||||
</Dd>
|
||||
</React.Fragment>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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} {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -249,7 +249,7 @@ export class IconLibrary {
|
||||
},
|
||||
browser: {
|
||||
component: BrowserIcon,
|
||||
keywords: "browser web website app",
|
||||
keywords: "browser web app",
|
||||
},
|
||||
collection: {
|
||||
component: (props) => <CollectionIcon expanded {...props} />,
|
||||
@@ -309,7 +309,7 @@ export class IconLibrary {
|
||||
},
|
||||
globe: {
|
||||
component: GlobeIcon,
|
||||
keywords: "world translate",
|
||||
keywords: "world website translate",
|
||||
},
|
||||
hashtag: {
|
||||
component: HashtagIcon,
|
||||
|
||||
Reference in new issue
Block a user