From 1f980050caa7c6e20c498e7a4181581f6198820c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 30 May 2024 18:46:03 -0400 Subject: [PATCH] fix: Incorrect empty check for collection description results in large empty space below title --- app/models/Collection.ts | 9 ++++++++- shared/utils/ProsemirrorHelper.ts | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 55668b6cc..4544c43a6 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -6,6 +6,7 @@ import { NavigationNode, type ProsemirrorData, } from "@shared/types"; +import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { sortNavigationNodes } from "@shared/utils/collections"; import type CollectionsStore from "~/stores/CollectionsStore"; import Document from "~/models/Document"; @@ -134,8 +135,14 @@ export default class Collection extends ParanoidModel { return !this.permission; } + /** + * Check whether this collection has a description. + * + * @returns boolean + */ + @computed get hasDescription(): boolean { - return !!this.data; + return this.data ? !ProsemirrorHelper.isEmptyData(this.data) : false; } @computed diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index a28177ee7..39e5e66b2 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -52,6 +52,30 @@ export class ProsemirrorHelper { }; } + /** + * Returns true if the data looks like an empty document. + * + * @param data The ProsemirrorData to check. + * @returns True if the document is empty. + */ + static isEmptyData(data: ProsemirrorData): boolean { + if (data.type !== "doc") { + return false; + } + + if (data.content.length === 1) { + const node = data.content[0]; + return ( + node.type === "paragraph" && + (node.content === null || + node.content === undefined || + node.content.length === 0) + ); + } + + return data.content.length === 0; + } + /** * Returns the node as plain text. *