feat: Drag document into starred section to star
This commit is contained in:
@@ -13,14 +13,14 @@ import DropCursor from "./DropCursor";
|
|||||||
import Header from "./Header";
|
import Header from "./Header";
|
||||||
import PlaceholderCollections from "./PlaceholderCollections";
|
import PlaceholderCollections from "./PlaceholderCollections";
|
||||||
import Relative from "./Relative";
|
import Relative from "./Relative";
|
||||||
import SidebarLink from "./SidebarLink";
|
import SidebarLink, { DragObject } from "./SidebarLink";
|
||||||
import StarredContext from "./StarredContext";
|
import StarredContext from "./StarredContext";
|
||||||
import StarredLink from "./StarredLink";
|
import StarredLink from "./StarredLink";
|
||||||
|
|
||||||
const STARRED_PAGINATION_LIMIT = 10;
|
const STARRED_PAGINATION_LIMIT = 10;
|
||||||
|
|
||||||
function Starred() {
|
function Starred() {
|
||||||
const { stars } = useStores();
|
const { documents, stars } = useStores();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const { loading, next, end, error, page } = usePaginatedRequest<Star>(
|
const { loading, next, end, error, page } = usePaginatedRequest<Star>(
|
||||||
@@ -30,7 +30,7 @@ function Starred() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Drop to reorder document
|
// Drop to reorder star
|
||||||
const [{ isOverReorder, isDraggingAnyStar }, dropToReorder] = useDrop({
|
const [{ isOverReorder, isDraggingAnyStar }, dropToReorder] = useDrop({
|
||||||
accept: "star",
|
accept: "star",
|
||||||
drop: async (item: { star: Star }) => {
|
drop: async (item: { star: Star }) => {
|
||||||
@@ -44,9 +44,25 @@ function Starred() {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
// Drop to star document
|
||||||
toast.error(t("Could not load starred documents"));
|
const [{ documentIsOverReorder, isDraggingAnyDocument }, dropToStar] =
|
||||||
}
|
useDrop({
|
||||||
|
accept: "document",
|
||||||
|
drop: async (item: DragObject) => {
|
||||||
|
const document = documents.get(item.id);
|
||||||
|
await document?.star(fractionalIndex(null, stars.orderedData[0].index));
|
||||||
|
},
|
||||||
|
collect: (monitor) => ({
|
||||||
|
documentIsOverReorder: !!monitor.isOver(),
|
||||||
|
isDraggingAnyDocument: monitor.getItemType() === "document",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (error) {
|
||||||
|
toast.error(t("Could not load starred documents"));
|
||||||
|
}
|
||||||
|
}, [t, error]);
|
||||||
|
|
||||||
if (!stars.orderedData.length) {
|
if (!stars.orderedData.length) {
|
||||||
return null;
|
return null;
|
||||||
@@ -64,6 +80,13 @@ function Starred() {
|
|||||||
position="top"
|
position="top"
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{isDraggingAnyDocument && (
|
||||||
|
<DropCursor
|
||||||
|
isActiveDrop={documentIsOverReorder}
|
||||||
|
innerRef={dropToStar}
|
||||||
|
position="top"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{stars.orderedData
|
{stars.orderedData
|
||||||
.slice(0, page * STARRED_PAGINATION_LIMIT)
|
.slice(0, page * STARRED_PAGINATION_LIMIT)
|
||||||
.map((star) => (
|
.map((star) => (
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import DocumentLink from "./DocumentLink";
|
|||||||
import DropCursor from "./DropCursor";
|
import DropCursor from "./DropCursor";
|
||||||
import Folder from "./Folder";
|
import Folder from "./Folder";
|
||||||
import Relative from "./Relative";
|
import Relative from "./Relative";
|
||||||
import SidebarLink from "./SidebarLink";
|
import SidebarLink, { DragObject } from "./SidebarLink";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
star: Star;
|
star: Star;
|
||||||
@@ -135,8 +135,39 @@ function StarredLink({ star }: Props) {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Drop to star document
|
||||||
|
const [{ documentIsOverReorder, isDraggingAnyDocument }, dropToStar] =
|
||||||
|
useDrop({
|
||||||
|
accept: "document",
|
||||||
|
drop: async (item: DragObject) => {
|
||||||
|
const next = star?.next();
|
||||||
|
const document = documents.get(item.id);
|
||||||
|
await document?.star(
|
||||||
|
fractionalIndex(star?.index || null, next?.index || null)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
collect: (monitor) => ({
|
||||||
|
documentIsOverReorder: !!monitor.isOver(),
|
||||||
|
isDraggingAnyDocument: monitor.getItemType() === "document",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
const displayChildDocuments = expanded && !isDragging;
|
const displayChildDocuments = expanded && !isDragging;
|
||||||
|
|
||||||
|
const cursor = (
|
||||||
|
<>
|
||||||
|
{isDraggingAny && (
|
||||||
|
<DropCursor isActiveDrop={isOverReorder} innerRef={dropToReorder} />
|
||||||
|
)}
|
||||||
|
{isDraggingAnyDocument && (
|
||||||
|
<DropCursor
|
||||||
|
isActiveDrop={documentIsOverReorder}
|
||||||
|
innerRef={dropToStar}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
|
||||||
if (documentId) {
|
if (documentId) {
|
||||||
const document = documents.get(documentId);
|
const document = documents.get(documentId);
|
||||||
if (!document) {
|
if (!document) {
|
||||||
@@ -200,9 +231,7 @@ function StarredLink({ star }: Props) {
|
|||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Folder>
|
</Folder>
|
||||||
{isDraggingAny && (
|
{cursor}
|
||||||
<DropCursor isActiveDrop={isOverReorder} innerRef={dropToReorder} />
|
|
||||||
)}
|
|
||||||
</Relative>
|
</Relative>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -225,9 +254,7 @@ function StarredLink({ star }: Props) {
|
|||||||
collection={collection}
|
collection={collection}
|
||||||
expanded={displayChildDocuments}
|
expanded={displayChildDocuments}
|
||||||
/>
|
/>
|
||||||
{isDraggingAny && (
|
{cursor}
|
||||||
<DropCursor isActiveDrop={isOverReorder} innerRef={dropToReorder} />
|
|
||||||
)}
|
|
||||||
</Relative>
|
</Relative>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -343,7 +343,7 @@ export default class Document extends ParanoidModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@action
|
@action
|
||||||
star = () => this.store.star(this);
|
star = (index?: string) => this.store.star(this, index);
|
||||||
|
|
||||||
@action
|
@action
|
||||||
unstar = () => this.store.unstar(this);
|
unstar = () => this.store.unstar(this);
|
||||||
|
|||||||
@@ -728,9 +728,10 @@ export default class DocumentsStore extends Store<Document> {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
star = (document: Document) =>
|
star = (document: Document, index?: string) =>
|
||||||
this.rootStore.stars.create({
|
this.rootStore.stars.create({
|
||||||
documentId: document.id,
|
documentId: document.id,
|
||||||
|
index,
|
||||||
});
|
});
|
||||||
|
|
||||||
unstar = (document: Document) => {
|
unstar = (document: Document) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user