This commit is contained in:
Tom Moor committed 2024-07-27 16:21:11 -04:00
1 parent ab7395f5ae
commit c3ffdb714e
9 files changed
+122 -41

No files matched your search

@@ -9,7 +9,6 @@ import {
type DataAttributeOptions,
} from "@shared/models/types";
import { DataAttributeValidation } from "@shared/validations";
import DataAttribute from "~/models/DataAttribute";
import Button from "~/components/Button";
import Flex from "~/components/Flex";
import Input from "~/components/Input";
@@ -70,7 +69,7 @@ export const DataAttributeForm = observer(function DataAttributeForm_({
field.onChange(value);
if (value === DataAttributeDataType.List) {
setValue("options", { options: [""] });
setValue("options", { values: [""] });
}
}}
ariaLabel={t("Format")}
@@ -84,14 +83,14 @@ export const DataAttributeForm = observer(function DataAttributeForm_({
/>
{values.dataType === DataAttributeDataType.List && (
<Flex column gap={8}>
{values.options?.options?.map((option, index) => (
{values.options?.values?.map((option, index) => (
<div key={index}>
<Input
value={option}
onChange={(event) => {
const newOptions = [...(values.options?.options ?? [])];
newOptions[index] = event.target.value;
setValue("options", { options: newOptions });
const newValues = [...(values.options?.values ?? [])];
newValues[index] = event.target.value;
setValue("options", { values: newValues });
}}
type="text"
autoComplete="off"
@@ -106,12 +105,12 @@ export const DataAttributeForm = observer(function DataAttributeForm_({
render={({ field }) => (
<NudeButton
disabled={
(values.options?.options?.length ?? 0) >=
(values.options?.values?.length ?? 0) >=
DataAttributeValidation.maxOptions
}
onClick={() => {
field.onChange({
options: [...(field.value?.options ?? []), ""],
values: [...(field.value?.values ?? []), ""],
});
}}
>
+11 -11
View File
@@ -20,8 +20,8 @@ import React, { ComponentProps } from "react";
import { useTranslation } from "react-i18next";
import { integrationSettingsPath } from "@shared/utils/routeHelpers";
import ZapierIcon from "~/components/Icons/ZapierIcon";
import { Hook, PluginManager } from "~/utils/PluginManager";
import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import { Hook, PluginManager } from "~/utils/PluginManager";
import isCloudHosted from "~/utils/isCloudHosted";
import lazy from "~/utils/lazyWithRetry";
import { settingsPath } from "~/utils/routeHelpers";
@@ -122,16 +122,6 @@ const useSettingsConfig = () => {
group: t("Workspace"),
icon: BeakerIcon,
},
{
name: t("Attributes"),
path: settingsPath("attributes"),
component: DataAttributes,
enabled:
can.createDataAttribute &&
FeatureFlags.isEnabled(Feature.dataAttributes),
group: t("Workspace"),
icon: DatabaseIcon,
},
{
name: t("Members"),
path: settingsPath("members"),
@@ -156,6 +146,16 @@ const useSettingsConfig = () => {
group: t("Workspace"),
icon: ShapesIcon,
},
{
name: t("Data Attributes"),
path: settingsPath("attributes"),
component: DataAttributes,
enabled:
can.createDataAttribute &&
FeatureFlags.isEnabled(Feature.dataAttributes),
group: t("Workspace"),
icon: DatabaseIcon,
},
{
name: t("Shared Links"),
path: settingsPath("shares"),
+27 -1
View File
@@ -6,6 +6,7 @@ import { action, autorun, computed, observable, set } from "mobx";
import { Node, Schema } from "prosemirror-model";
import ExtensionManager from "@shared/editor/lib/ExtensionManager";
import { richExtensions, withComments } from "@shared/editor/nodes";
import { DataAttributeDataType } from "@shared/models/types";
import type {
JSONObject,
NavigationNode,
@@ -498,7 +499,32 @@ export default class Document extends ParanoidModel {
};
@action
setDataAttribute = (dataAttributeId: string, value: string | number) => {
setDataAttribute = (
dataAttributeId: string,
input: string | number | boolean
) => {
const definition = this.store.rootStore.dataAttributes.get(dataAttributeId);
if (!definition) {
throw new Error(`Data attribute ${dataAttributeId} not found`);
}
let value: string | number | boolean = input;
switch (definition.dataType) {
case DataAttributeDataType.Number:
value = Number(input);
break;
case DataAttributeDataType.Boolean:
value = input === true || input === "true";
break;
case DataAttributeDataType.List:
if (!definition.options?.values.includes(input as string)) {
throw new Error(
`Invalid value for data attribute ${dataAttributeId}`
);
}
break;
}
this.dataAttributes = (this.dataAttributes ?? [])
.filter((attr) => attr.dataAttributeId !== dataAttributeId)
.concat({ dataAttributeId, value, updatedAt: new Date().toISOString() });
@@ -9,6 +9,7 @@ import Flex from "@shared/components/Flex";
import { TeamPreference } from "@shared/types";
import Document from "~/models/Document";
import Revision from "~/models/Revision";
import Button from "~/components/Button";
import DocumentMeta from "~/components/DocumentMeta";
import Fade from "~/components/Fade";
import Input from "~/components/Input";
@@ -55,7 +56,15 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
const addProperty = (
<InlineLink
to={documentPath(document)}
onClick={() => setDraftAttribute((state) => (state ? null : {}))}
onClick={() =>
setDraftAttribute((state) =>
state
? null
: {
dataAttributeId: dataAttributes.orderedData[0].id,
}
)
}
>
<PlusIcon size={18} /> Property
</InlineLink>
@@ -100,7 +109,8 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
</Link>
</Wrapper>
) : null}
{hasDataAttributes && document.dataAttributes.length === 0 ? (
{hasDataAttributes &&
(!document.dataAttributes || document.dataAttributes.length === 0) ? (
<>&nbsp;•&nbsp;{addProperty}</>
) : null}
</Meta>
@@ -112,11 +122,20 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
<Text type="tertiary" weight="bold">
{definition?.name}:
</Text>{" "}
<Text type="tertiary">{dataAttribute.value}</Text>
<Text type="tertiary">{String(dataAttribute.value)}</Text>
<Button
onClick={() => {
document.deleteDataAttribute(dataAttribute.dataAttributeId);
void document.save();
}}
neutral
>
x
</Button>
</div>
);
})}
{document.dataAttributes.length > 1 && addProperty}
{document.dataAttributes?.length > 0 && addProperty}
{draftAttribute && (
<Flex gap={8} auto>
<InputSelect
@@ -130,6 +149,7 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
dataAttributes.orderedData[0].id
}
onChange={(dataAttributeId) =>
dataAttributeId &&
setDraftAttribute({ ...draftAttribute, dataAttributeId })
}
/>
+14 -6
View File
@@ -1,11 +1,12 @@
import { observer } from "mobx-react";
import { DatabaseIcon } from "outline-icons";
import { DatabaseIcon, MoreIcon } from "outline-icons";
import * as React from "react";
import { useTranslation, Trans } from "react-i18next";
import DataAttribute from "~/models/DataAttribute";
import { Action } from "~/components/Actions";
import Button from "~/components/Button";
import Heading from "~/components/Heading";
import Item from "~/components/List/Item";
import PaginatedList from "~/components/PaginatedList";
import Scene from "~/components/Scene";
import Text from "~/components/Text";
@@ -14,6 +15,7 @@ import useActionContext from "~/hooks/useActionContext";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import { DataAttributesHelper } from "~/utils/DataAttributesHelper";
function DataAttributes() {
const team = useCurrentTeam();
@@ -24,7 +26,7 @@ function DataAttributes() {
return (
<Scene
title={t("Attributes")}
title={t("Data Attributes")}
icon={<DatabaseIcon />}
actions={
<>
@@ -41,19 +43,25 @@ function DataAttributes() {
</>
}
>
<Heading>{t("Attributes")}</Heading>
<Heading>{t("Data Attributes")}</Heading>
<Text as="p" type="secondary">
<Trans>
Attributes allow you to define data to be stored with your documents.
They can be used to store metadata, tags, or any other structured
data.
They can be used to store custom properties, metadata, or any other
structured information that is common across documents.
</Trans>
</Text>
<PaginatedList
fetch={dataAttributes.fetchAll}
items={dataAttributes.orderedData}
renderItem={(dataAttribute: DataAttribute) => (
<span>{JSON.stringify(dataAttribute)}</span>
<Item
key={dataAttribute.id}
title={dataAttribute.name}
subtitle={dataAttribute.description || dataAttribute.dataType}
image={DataAttributesHelper.getIcon(dataAttribute)}
actions={<MoreIcon />}
/>
)}
/>
</Scene>
+26
View File
@@ -0,0 +1,26 @@
import {
CaseSensitiveIcon,
DoneIcon,
HashtagIcon,
TableOfContentsIcon,
} from "outline-icons";
import * as React from "react";
import { DataAttributeDataType } from "@shared/models/types";
import DataAttribute from "~/models/DataAttribute";
export class DataAttributesHelper {
public static getIcon(dataAttribute: DataAttribute) {
switch (dataAttribute.dataType) {
case DataAttributeDataType.Boolean:
return <DoneIcon />;
case DataAttributeDataType.Number:
return <HashtagIcon />;
case DataAttributeDataType.String:
return <CaseSensitiveIcon />;
case DataAttributeDataType.List:
return <TableOfContentsIcon />;
default:
return null;
}
}
}
+5 -8
View File
@@ -75,14 +75,11 @@ export default async function documentUpdater({
if (dataAttributes !== undefined) {
// TODO: Validate schema
document.dataAttributes = uniqBy(
[
...dataAttributes.map(({ dataAttributeId, value }) => ({
dataAttributeId,
value,
updatedAt: new Date(),
})),
...(document.dataAttributes ?? []),
],
dataAttributes.map(({ dataAttributeId, value, updatedAt }) => ({
dataAttributeId,
value,
updatedAt: updatedAt ?? new Date(),
})),
"dataAttributeId"
);
}
+1 -1
View File
@@ -48,7 +48,7 @@ export const DataAttributesCreateSchema = BaseSchema.extend({
/** Options for the data attribute (if list type) */
options: z
.object({
options: z.array(z.string()),
values: z.array(z.string()),
})
.optional(),
+7 -2
View File
@@ -255,9 +255,14 @@ export const DocumentsUpdateSchema = BaseSchema.extend({
/** Whether the editing session is complete */
done: z.boolean().optional(),
// TODO
/** Data attributes to be updated, attributes not included will be removed */
dataAttributes: z
.array(z.object({ dataAttributeId: z.string(), value: z.string() }))
.array(
z.object({
dataAttributeId: z.string(),
value: z.string().or(z.boolean()).or(z.number()),
})
)
.optional(),
}),
}).refine((req) => !(req.body.append && !req.body.text), {