mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
Ordering is derived entirely from the collection's documentStructure, which drafts are not part of. Drafts therefore cannot be ordered, and unpublishing a document loses its position. Adds a nullable index column to documents recording a document's position amongst its siblings while it is absent from the structure, restored when the document is published. Sibling draft positions are shifted as documents around them are inserted or removed, emitting an update event so that the author's client stays in sync. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { clamp } from "es-toolkit";
|
|
import { useMemo } from "react";
|
|
import { sortNavigationNodes } from "@shared/utils/collections";
|
|
import type Collection from "~/models/Collection";
|
|
import type Document from "~/models/Document";
|
|
|
|
export default function useCollectionDocuments(
|
|
collection: Collection | undefined,
|
|
activeDocument: Document | undefined
|
|
) {
|
|
const insertDraftDocument = !!(
|
|
activeDocument &&
|
|
activeDocument.isActive &&
|
|
activeDocument.isDraft &&
|
|
activeDocument.collectionId === collection?.id &&
|
|
!activeDocument.parentDocumentId
|
|
);
|
|
|
|
// Only subscribe to asNavigationNode when we actually need to insert a draft
|
|
// into the sorted list. This avoids every CollectionLinkChildren observer
|
|
// re-rendering on every title keystroke.
|
|
const draftNavNode = insertDraftDocument
|
|
? activeDocument?.asNavigationNode
|
|
: undefined;
|
|
const draftIndex = insertDraftDocument ? activeDocument?.index : undefined;
|
|
|
|
return useMemo(() => {
|
|
if (!collection?.sortedDocuments) {
|
|
return undefined;
|
|
}
|
|
|
|
if (!draftNavNode) {
|
|
return collection.sortedDocuments;
|
|
}
|
|
|
|
const nodes = [...collection.sortedDocuments];
|
|
nodes.splice(clamp(draftIndex ?? 0, 0, nodes.length), 0, draftNavNode);
|
|
|
|
return sortNavigationNodes(nodes, collection.sort, false);
|
|
}, [draftNavNode, draftIndex, collection?.sortedDocuments, collection?.sort]);
|
|
}
|