diff --git a/app/components/DataAttribute/DataAttributeForm.tsx b/app/components/DataAttribute/DataAttributeForm.tsx
index a42ffda63e..a273293292 100644
--- a/app/components/DataAttribute/DataAttributeForm.tsx
+++ b/app/components/DataAttribute/DataAttributeForm.tsx
@@ -88,6 +88,11 @@ export const DataAttributeForm = observer(function DataAttributeForm_({
{
+ const newOptions = [...(values.options?.options ?? [])];
+ newOptions[index] = event.target.value;
+ setValue("options", { options: newOptions });
+ }}
type="text"
autoComplete="off"
autoFocus
diff --git a/app/models/Document.ts b/app/models/Document.ts
index c8245bdbef..ac217131b3 100644
--- a/app/models/Document.ts
+++ b/app/models/Document.ts
@@ -62,10 +62,12 @@ export default class Document extends ParanoidModel {
@observable
lastViewedAt: string | undefined;
+ @Field
@observable
dataAttributes: {
dataAttributeId: string;
- value: string;
+ value: string | number;
+ updatedAt: string;
}[];
store: DocumentsStore;
@@ -490,6 +492,22 @@ export default class Document extends ParanoidModel {
});
};
+ @action
+ setDataAttribute = (dataAttributeId: string, value: string | number) => {
+ this.dataAttributes = (this.dataAttributes ?? [])
+ .filter((attr) => attr.dataAttributeId !== dataAttributeId)
+ .concat({ dataAttributeId, value, updatedAt: new Date().toISOString() });
+ return this;
+ };
+
+ @action
+ deleteDataAttribute = (dataAttributeId: string) => {
+ this.dataAttributes = this.dataAttributes?.filter(
+ (attr) => attr.dataAttributeId !== dataAttributeId
+ );
+ return this;
+ };
+
@action
updateLastViewed = (view: View) => {
this.lastViewedAt = view.lastViewedAt;
diff --git a/app/scenes/Document/components/DataLoader.tsx b/app/scenes/Document/components/DataLoader.tsx
index 28bfc21718..7a9abf0de9 100644
--- a/app/scenes/Document/components/DataLoader.tsx
+++ b/app/scenes/Document/components/DataLoader.tsx
@@ -13,6 +13,7 @@ import useCurrentTeam from "~/hooks/useCurrentTeam";
import useCurrentUser from "~/hooks/useCurrentUser";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
+import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import Logger from "~/utils/Logger";
import {
NotFoundError,
@@ -53,8 +54,16 @@ type Props = RouteComponentProps
& {
};
function DataLoader({ match, children }: Props) {
- const { ui, views, shares, comments, documents, revisions, subscriptions } =
- useStores();
+ const {
+ ui,
+ views,
+ shares,
+ comments,
+ documents,
+ revisions,
+ subscriptions,
+ dataAttributes,
+ } = useStores();
const team = useCurrentTeam();
const user = useCurrentUser();
const [error, setError] = React.useState(null);
@@ -82,6 +91,15 @@ function DataLoader({ match, children }: Props) {
const can = usePolicy(document);
const location = useLocation();
+ React.useEffect(() => {
+ if (
+ !dataAttributes.isLoaded &&
+ FeatureFlags.isEnabled(Feature.dataAttributes)
+ ) {
+ void dataAttributes.fetchAll();
+ }
+ }, [dataAttributes]);
+
React.useEffect(() => {
async function fetchDocument() {
try {
diff --git a/app/scenes/Document/components/DocumentMeta.tsx b/app/scenes/Document/components/DocumentMeta.tsx
index 3a47259bbd..ee0f906cbb 100644
--- a/app/scenes/Document/components/DocumentMeta.tsx
+++ b/app/scenes/Document/components/DocumentMeta.tsx
@@ -1,18 +1,23 @@
import { LocationDescriptor } from "history";
import { observer, useObserver } from "mobx-react";
-import { CommentIcon } from "outline-icons";
+import { CommentIcon, PlusIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { Link, useRouteMatch } from "react-router-dom";
import styled from "styled-components";
+import Flex from "@shared/components/Flex";
import { TeamPreference } from "@shared/types";
import Document from "~/models/Document";
import Revision from "~/models/Revision";
import DocumentMeta from "~/components/DocumentMeta";
import Fade from "~/components/Fade";
+import Input from "~/components/Input";
+import InputSelect from "~/components/InputSelect";
+import Text from "~/components/Text";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
+import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import { documentPath, documentInsightsPath } from "~/utils/routeHelpers";
type Props = {
@@ -24,7 +29,7 @@ type Props = {
};
function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
- const { views, comments, ui } = useStores();
+ const { views, comments, dataAttributes, ui } = useStores();
const { t } = useTranslation();
const match = useRouteMatch();
const team = useCurrentTeam();
@@ -33,53 +38,136 @@ 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 [draftAttribute, setDraftAttribute] = React.useState<{
+ dataAttributeId: string;
+ value: string;
+ } | null>(null);
const Wrapper = viewsLoadedOnMount.current ? React.Fragment : Fade;
const insightsPath = documentInsightsPath(document);
const commentsCount = comments.unresolvedCommentsInDocumentCount(document.id);
+ const hasDataAttributes =
+ FeatureFlags.isEnabled(Feature.dataAttributes) &&
+ dataAttributes.orderedData.length > 0;
+
+ const addProperty = (
+ setDraftAttribute((state) => (state ? null : {}))}
+ >
+ Property
+
+ );
+
return (
-
- {team.getPreference(TeamPreference.Commenting) && can.comment && (
- <>
- •
- ui.toggleComments(document.id)}
- >
-
- {commentsCount
- ? t("{{ count }} comment", { count: commentsCount })
- : t("Comment")}
-
- >
- )}
- {totalViewers &&
- can.listViews &&
- !document.isDraft &&
- !document.isTemplate ? (
-
- •
-
- {t("Viewed by")}{" "}
- {onlyYou
- ? t("only you")
- : `${totalViewers} ${
- totalViewers === 1 ? t("person") : t("people")
- }`}
-
-
- ) : null}
-
+ <>
+
+ {team.getPreference(TeamPreference.Commenting) && can.comment && (
+ <>
+ •
+ ui.toggleComments(document.id)}
+ >
+
+ {commentsCount
+ ? t("{{ count }} comment", { count: commentsCount })
+ : t("Comment")}
+
+ >
+ )}
+ {totalViewers &&
+ can.listViews &&
+ !document.isDraft &&
+ !document.isTemplate ? (
+
+ •
+
+ {t("Viewed by")}{" "}
+ {onlyYou
+ ? t("only you")
+ : `${totalViewers} ${
+ totalViewers === 1 ? t("person") : t("people")
+ }`}
+
+
+ ) : null}
+ {hasDataAttributes && document.dataAttributes.length === 0 ? (
+ <> • {addProperty}>
+ ) : null}
+
+
+ {document.dataAttributes?.map((dataAttribute) => {
+ const definition = dataAttributes.get(dataAttribute.dataAttributeId);
+ return (
+
+
+ {definition?.name}:
+ {" "}
+ {dataAttribute.value}
+
+ );
+ })}
+ {document.dataAttributes.length > 1 && addProperty}
+ {draftAttribute && (
+
+ ({
+ label: attribute.name,
+ value: attribute.id,
+ }))}
+ value={
+ draftAttribute?.dataAttributeId ??
+ dataAttributes.orderedData[0].id
+ }
+ onChange={(dataAttributeId) =>
+ setDraftAttribute({ ...draftAttribute, dataAttributeId })
+ }
+ />
+
+ {
+ if (draftAttribute.value) {
+ document.setDataAttribute(
+ draftAttribute.dataAttributeId,
+ draftAttribute.value
+ );
+ await document.save();
+ }
+ }}
+ onChange={(event) =>
+ setDraftAttribute({
+ ...draftAttribute,
+ value: event.target.value,
+ })
+ }
+ />
+
+ )}
+
+ >
);
}
-const CommentLink = styled(Link)`
+const Properties = styled.div`
+ margin-top: -1.9em;
+ margin-bottom: 2em;
+ font-size: 14px;
+ position: relative;
+`;
+
+const InlineLink = styled(Link)`
display: inline-flex;
align-items: center;
`;
diff --git a/app/scenes/Document/components/Insights.tsx b/app/scenes/Document/components/Insights.tsx
index 3ab5a1b8e3..156178030e 100644
--- a/app/scenes/Document/components/Insights.tsx
+++ b/app/scenes/Document/components/Insights.tsx
@@ -20,12 +20,11 @@ import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import useTextSelection from "~/hooks/useTextSelection";
import InsightsMenu from "~/menus/InsightsMenu";
-import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import { documentPath } from "~/utils/routeHelpers";
import Sidebar from "./SidebarLayout";
function Insights() {
- const { views, dataAttributes, documents } = useStores();
+ const { views, documents } = useStores();
const { t } = useTranslation();
const match = useRouteMatch<{ documentSlug: string }>();
const history = useHistory();
@@ -45,9 +44,6 @@ function Insights() {
useKeyDown("Escape", onCloseInsights);
- // Temporarily here, insights will be removed / renamed.
- const hasDataAttributes = FeatureFlags.isEnabled(Feature.dataAttributes);
-
return (
{document ? (
@@ -58,27 +54,6 @@ function Insights() {
justify="space-between"
>
- {hasDataAttributes && (
-
- {t("Data attributes")}
-
- {dataAttributes.orderedData.map((dataAttribute) => (
-
- {dataAttribute.name}:{" "}
- {
- document.dataAttributes?.find(
- (da) => da.dataAttributeId === dataAttribute.id
- )?.value
- }
-
- ))}
-
- )}
{document.sourceMetadata && (
<>
diff --git a/app/scenes/Settings/DataAttributes.tsx b/app/scenes/Settings/DataAttributes.tsx
index b803abb22b..c3a9a61a7e 100644
--- a/app/scenes/Settings/DataAttributes.tsx
+++ b/app/scenes/Settings/DataAttributes.tsx
@@ -50,7 +50,7 @@ function DataAttributes() {
(
{JSON.stringify(dataAttribute)}
diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts
index f20bfe82ea..c03af893b7 100644
--- a/server/commands/documentUpdater.ts
+++ b/server/commands/documentUpdater.ts
@@ -1,5 +1,6 @@
+import uniqBy from "lodash/uniqBy";
import { Transaction } from "sequelize";
-import { Event, Document, User, DocumentDataAttribute } from "@server/models";
+import { Event, Document, User } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
type Props = {
@@ -7,8 +8,11 @@ type Props = {
user: User;
/** The existing document */
document: Document;
- /** Data attributes to apply to the document */
- attributes?: Pick[];
+ /** TODO: Data attributes to apply to the document */
+ dataAttributes?: {
+ dataAttributeId: string;
+ value: string;
+ }[];
/** The new title */
title?: string;
/** The document icon */
@@ -49,7 +53,7 @@ type Props = {
export default async function documentUpdater({
user,
document,
- attributes,
+ dataAttributes,
title,
icon,
color,
@@ -68,8 +72,19 @@ export default async function documentUpdater({
const previousTitle = document.title;
const cId = collectionId || document.collectionId;
- if (attributes !== undefined) {
- await updateDataAttributes(user, document, attributes, { transaction });
+ if (dataAttributes !== undefined) {
+ // TODO: Validate schema
+ document.dataAttributes = uniqBy(
+ [
+ ...dataAttributes.map(({ dataAttributeId, value }) => ({
+ dataAttributeId,
+ value,
+ updatedAt: new Date(),
+ })),
+ ...(document.dataAttributes ?? []),
+ ],
+ "dataAttributeId"
+ );
}
if (title !== undefined) {
@@ -157,24 +172,3 @@ export default async function documentUpdater({
transaction,
});
}
-
-async function updateDataAttributes(
- user: User,
- document: Document,
- attributes: Pick[],
- { transaction }: { transaction: Transaction }
-) {
- await DocumentDataAttribute.destroy({
- where: { documentId: document.id },
- transaction,
- });
-
- await DocumentDataAttribute.bulkCreate(
- attributes.map((attribute) => ({
- ...attribute,
- userId: user.id,
- documentId: document.id,
- })),
- { transaction }
- );
-}
diff --git a/server/migrations/20240611010054-create-data-attributes.js b/server/migrations/20240611010054-create-data-attributes.js
index d491e70fd6..6bea9d8776 100644
--- a/server/migrations/20240611010054-create-data-attributes.js
+++ b/server/migrations/20240611010054-create-data-attributes.js
@@ -74,70 +74,18 @@ module.exports = {
{ transaction }
);
- await queryInterface.createTable(
- "document_data_attributes",
- {
- id: {
- type: Sequelize.UUID,
- allowNull: false,
- primaryKey: true,
- },
- userId: {
- type: Sequelize.UUID,
- allowNull: false,
- onDelete: "cascade",
- references: {
- model: "users",
- }
- },
- documentId: {
- type: Sequelize.UUID,
- allowNull: false,
- onDelete: "cascade",
- references: {
- model: "documents",
- }
- },
- dataAttributeId: {
- type: Sequelize.UUID,
- allowNull: false,
- onDelete: "cascade",
- references: {
- model: "data_attributes",
- }
- },
- value: {
- type: Sequelize.STRING,
- allowNull: false,
- },
- createdAt: {
- type: Sequelize.DATE,
- allowNull: false,
- },
- updatedAt: {
- type: Sequelize.DATE,
- allowNull: false,
- },
- }, {
- transaction
- });
-
- await queryInterface.addIndex(
- "document_data_attributes",
- ["documentId"],
- { transaction }
- );
- await queryInterface.addIndex(
- "document_data_attributes",
- ["dataAttributeId"],
- { transaction }
- );
+ await queryInterface.addColumn("documents", "dataAttributes", {
+ type: Sequelize.JSONB,
+ allowNull: true,
+ });
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
- await queryInterface.dropTable("document_data_attributes", { transaction });
await queryInterface.dropTable("data_attributes", { transaction });
+ await queryInterface.removeColumn("documents", "dataAttributes", {
+ transaction,
+ });
});
}
};
diff --git a/server/models/DataAttribute.ts b/server/models/DataAttribute.ts
index a994798066..f403cb6b46 100644
--- a/server/models/DataAttribute.ts
+++ b/server/models/DataAttribute.ts
@@ -1,4 +1,8 @@
-import { InferAttributes, InferCreationAttributes } from "sequelize";
+import {
+ InferAttributes,
+ InferCreationAttributes,
+ type SaveOptions,
+} from "sequelize";
import {
Column,
Table,
@@ -10,6 +14,7 @@ import {
Default,
BeforeCreate,
} from "sequelize-typescript";
+import { z } from "zod";
import {
DataAttributeDataType,
type DataAttributeOptions,
@@ -52,7 +57,7 @@ class DataAttribute extends ParanoidModel<
@Column(DataType.ENUM(...Object.values(DataAttributeDataType)))
dataType: DataAttributeDataType;
- /** Options for `list` data type. */
+ /** Additional options for some datatypes. */
@AllowNull
@Column(DataType.JSONB)
options: DataAttributeOptions;
@@ -68,11 +73,15 @@ class DataAttribute extends ParanoidModel<
// hooks
@BeforeCreate
- static async checkMaximumAttributes(model: DataAttribute) {
+ static async checkMaximumAttributes(
+ model: DataAttribute,
+ { transaction }: SaveOptions
+ ) {
const count = await this.count({
where: {
teamId: model.teamId,
},
+ transaction,
});
if (count >= DataAttributeValidation.max) {
@@ -80,6 +89,26 @@ class DataAttribute extends ParanoidModel<
}
}
+ /**
+ * The Zod type for validating this data attribute.
+ */
+ get zodType() {
+ switch (this.dataType) {
+ case DataAttributeDataType.String:
+ return z.string();
+ case DataAttributeDataType.Number:
+ return z.number();
+ case DataAttributeDataType.Boolean:
+ return z.boolean();
+ case DataAttributeDataType.List:
+ return z
+ .string()
+ .refine((value) => this.options.values.includes(value));
+ default:
+ throw new Error(`Unknown data type: ${this.dataType}`);
+ }
+ }
+
// associations
@BelongsTo(() => User, "createdById")
diff --git a/server/models/Document.ts b/server/models/Document.ts
index 948d1969c0..61cfede2c7 100644
--- a/server/models/Document.ts
+++ b/server/models/Document.ts
@@ -41,6 +41,7 @@ import {
BelongsToMany,
} from "sequelize-typescript";
import isUUID from "validator/lib/isUUID";
+import { ZodError } from "zod";
import type {
NavigationNode,
ProsemirrorData,
@@ -54,7 +55,7 @@ import { ValidationError } from "@server/errors";
import { generateUrlId } from "@server/utils/url";
import Backlink from "./Backlink";
import Collection from "./Collection";
-import DocumentDataAttribute from "./DocumentDataAttribute";
+import DataAttribute from "./DataAttribute";
import FileOperation from "./FileOperation";
import Revision from "./Revision";
import Star from "./Star";
@@ -88,10 +89,6 @@ type AdditionalFindOptions = {
as: "updatedBy",
paranoid: false,
},
- {
- model: DocumentDataAttribute,
- as: "dataAttributes",
- },
],
where: {
publishedAt: {
@@ -284,6 +281,10 @@ class Document extends ParanoidModel<
@Column
color: string | null;
+ // TODO
+ @Column(DataType.JSONB)
+ dataAttributes: Record[];
+
/**
* The content of the document as Markdown.
*
@@ -370,6 +371,52 @@ class Document extends ParanoidModel<
// hooks
+ @BeforeSave
+ static async validateDataAttributes(
+ model: Document,
+ { transaction }: SaveOptions
+ ) {
+ if (model.changed("dataAttributes")) {
+ const dataAttributeIds = model.dataAttributes.map(
+ (d) => d.dataAttributeId
+ );
+ const definitions = await DataAttribute.findAll({
+ where: {
+ id: dataAttributeIds,
+ },
+ transaction,
+ });
+
+ for (const attr of model.dataAttributes) {
+ const definition = definitions.find(
+ (d) => d.id === attr.dataAttributeId
+ );
+ if (!definition) {
+ throw ValidationError(
+ `Data attribute ${attr.dataAttributeId} not found`
+ );
+ }
+
+ try {
+ definition.zodType.parse(attr.value);
+ } catch (err) {
+ if (err instanceof ZodError) {
+ const { path, message } = err.issues[0];
+ const errMessage =
+ path.length > 0
+ ? `${path[path.length - 1]}: ${message}`
+ : message;
+ throw ValidationError(
+ `Data attribute ${attr.dataAttributeId} has invalid value: ${errMessage}`
+ );
+ }
+
+ throw err;
+ }
+ }
+ }
+ }
+
@BeforeSave
static async updateCollectionStructure(
model: Document,
@@ -559,9 +606,6 @@ class Document extends ParanoidModel<
@Column(DataType.UUID)
collectionId?: string | null;
- @HasMany(() => DocumentDataAttribute)
- dataAttributes: DocumentDataAttribute[];
-
@HasMany(() => UserMembership)
memberships: UserMembership[];
diff --git a/server/models/DocumentDataAttribute.ts b/server/models/DocumentDataAttribute.ts
deleted file mode 100644
index 5f9e54e278..0000000000
--- a/server/models/DocumentDataAttribute.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { InferAttributes, InferCreationAttributes } from "sequelize";
-import {
- BelongsTo,
- ForeignKey,
- Column,
- Table,
- DataType,
-} from "sequelize-typescript";
-import DataAttribute from "./DataAttribute";
-import Document from "./Document";
-import User from "./User";
-import IdModel from "./base/IdModel";
-import Fix from "./decorators/Fix";
-
-@Table({
- tableName: "document_data_attributes",
- modelName: "document_data_attribute",
-})
-@Fix
-class DocumentDataAttribute extends IdModel<
- InferAttributes,
- Partial>
-> {
- @Column
- value: string;
-
- // associations
-
- @BelongsTo(() => DataAttribute, "dataAttributeId")
- dataAttribute: DataAttribute;
-
- @ForeignKey(() => DataAttribute)
- @Column(DataType.UUID)
- dataAttributeId: string;
-
- @BelongsTo(() => Document, "documentId")
- document: Document;
-
- @ForeignKey(() => Document)
- @Column(DataType.UUID)
- documentId: string;
-
- @BelongsTo(() => User, "userId")
- user: User;
-
- @ForeignKey(() => User)
- @Column(DataType.UUID)
- userId: string;
-}
-
-export default DocumentDataAttribute;
diff --git a/server/presenters/document.ts b/server/presenters/document.ts
index 2f3ae325c6..6f50fa7651 100644
--- a/server/presenters/document.ts
+++ b/server/presenters/document.ts
@@ -3,7 +3,6 @@ import { Document } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { TextHelper } from "@server/models/helpers/TextHelper";
import { APIContext } from "@server/types";
-import presentDocumentDataAttribute from "./documentDataAttribute";
import presentUser from "./user";
type Options = {
@@ -77,8 +76,7 @@ async function presentDocument(
if (!options.isPublic) {
const source = await document.$get("import");
- data.dataAttributes =
- document.dataAttributes?.map(presentDocumentDataAttribute) ?? [];
+ data.dataAttributes = document.dataAttributes;
data.isCollectionDeleted = await document.isCollectionDeleted();
data.collectionId = document.collectionId;
data.parentDocumentId = document.parentDocumentId;
diff --git a/server/presenters/documentDataAttribute.ts b/server/presenters/documentDataAttribute.ts
deleted file mode 100644
index 4924cc31f3..0000000000
--- a/server/presenters/documentDataAttribute.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type DocumentDataAttribute from "@server/models/DocumentDataAttribute";
-
-export default function presentDocumentDataAttribute(
- dataAttribute: DocumentDataAttribute
-) {
- return {
- id: dataAttribute.id,
- value: dataAttribute.value,
- dataAttributeId: dataAttribute.dataAttributeId,
- documentId: dataAttribute.documentId,
- createdAt: dataAttribute.createdAt,
- updatedAt: dataAttribute.updatedAt,
- };
-}
diff --git a/server/routes/api/documents/schema.ts b/server/routes/api/documents/schema.ts
index 0524e3d152..c4deec10e6 100644
--- a/server/routes/api/documents/schema.ts
+++ b/server/routes/api/documents/schema.ts
@@ -249,6 +249,11 @@ export const DocumentsUpdateSchema = BaseSchema.extend({
/** Whether the editing session is complete */
done: z.boolean().optional(),
+
+ // TODO
+ dataAttributes: z
+ .array(z.object({ dataAttributeId: z.string(), value: z.string() }))
+ .optional(),
}),
}).refine((req) => !(req.body.append && !req.body.text), {
message: "text is required while appending",
diff --git a/shared/models/types.ts b/shared/models/types.ts
index f6d40b6950..12dfae15bb 100644
--- a/shared/models/types.ts
+++ b/shared/models/types.ts
@@ -1,10 +1,10 @@
export enum DataAttributeDataType {
String = "string",
- Integer = "integer",
+ Number = "number",
Boolean = "boolean",
List = "list",
}
export type DataAttributeOptions = {
- options: string[];
+ values: string[];
};