diff --git a/sideboard/src/sideboard/dnd/dnd.ts b/sideboard/src/sideboard/dnd/dnd.ts index 506090ce..cdfd4ab5 100644 --- a/sideboard/src/sideboard/dnd/dnd.ts +++ b/sideboard/src/sideboard/dnd/dnd.ts @@ -114,6 +114,10 @@ export type SideboardDragAndDropItem = { type: string; name: string; source: string; + /** the folder holding the dragged docref, and where in it: a doc can be + * linked from several folders, and a move takes only this one link */ + parentUrl: AutomergeUrl; + index: number; }; // Plain Map — not reactive. aria-checked is updated via DOM manipulation diff --git a/sideboard/src/sideboard/dnd/drop.test.ts b/sideboard/src/sideboard/dnd/drop.test.ts index eee9b5d5..24d52ca7 100644 --- a/sideboard/src/sideboard/dnd/drop.test.ts +++ b/sideboard/src/sideboard/dnd/drop.test.ts @@ -218,6 +218,80 @@ describe("executeDrop", () => { expect(names(root)).toEqual(["inner"]); }); + // A doc linked from two folders, and an empty folder to drag it into. + function linkedTwice() { + const shared = repo.create({ name: "s" }).url; + const link = { name: "s", type: "x", url: shared }; + const a = repo.create({ title: "a", docs: [link] }); + const b = repo.create({ title: "b", docs: [{ ...link }] }); + const c = repo.create({ title: "c", docs: [] }); + const root = repo.create({ + title: "root", + docs: [ + { name: "a", type: "folder", url: a.url }, + { name: "b", type: "folder", url: b.url }, + { name: "c", type: "folder", url: c.url }, + ], + }); + const inside = (f: DocHandle, index: number) => + rowSpot({ id: f.url, url: f.url, parentUrl: root.url, index }, "inside"); + return { shared, a, b, c, root, inside }; + } + const from = (url: AutomergeUrl, parentUrl: AutomergeUrl, index: number) => ({ + draggedIds: [`${parentUrl}/${index}`], + draggedUrls: [url], + draggedItems: [{ url, name: "s", type: "x", parentUrl, index }], + sourceToolId: "", + copyMode: false, + }); + + it("moves only the link that was dragged, not every link to that doc", async () => { + for (const [dragged, other] of [["b", "a"], ["a", "b"]] as const) { + const t = linkedTwice(); + await executeDrop( + from(t.shared, t[dragged].url, 0), + t.inside(t.c, 2), + repo, + t.root, + true + ); + expect(names(t.c)).toEqual(["s"]); + expect(names(t[dragged])).toEqual([]); + expect(names(t[other])).toEqual(["s"]); + } + }); + + it("finds the dragged link in its folder even if its index has moved", async () => { + const t = linkedTwice(); + t.b.change((doc) => void doc.docs.unshift({ name: "new", type: "x", url: repo.create({}).url })); + await executeDrop(from(t.shared, t.b.url, 0), t.inside(t.c, 2), repo, t.root, true); + expect(names(t.b)).toEqual(["new"]); + expect(names(t.a)).toEqual(["s"]); + expect(names(t.c)).toEqual(["s"]); + }); + + it("moving into a folder that already links to the doc leaves one link", async () => { + const t = linkedTwice(); + await executeDrop(from(t.shared, t.a.url, 0), t.inside(t.b, 1), repo, t.root, true); + expect(names(t.b)).toEqual(["s"]); + expect(names(t.a)).toEqual([]); + }); + + it("copying into a folder that already links to the doc says so", async () => { + const t = linkedTwice(); + await expect( + executeDrop( + { ...from(t.shared, t.a.url, 0), copyMode: true }, + t.inside(t.b, 1), + repo, + t.root, + true + ) + ).rejects.toThrow(DropError); + expect(names(t.a)).toEqual(["s"]); + expect(names(t.b)).toEqual(["s"]); + }); + it("from another view, adds a link at the spot", async () => { const root = folder("a", "b"); const other = repo.create({ name: "z" }).url; diff --git a/sideboard/src/sideboard/dnd/operations.ts b/sideboard/src/sideboard/dnd/operations.ts index ba3fe357..a62bc5e0 100644 --- a/sideboard/src/sideboard/dnd/operations.ts +++ b/sideboard/src/sideboard/dnd/operations.ts @@ -27,9 +27,16 @@ function extractPlainDocLink(docLink: DocLink): DocLink { export interface DropOperation { draggedIds: string[]; draggedUrls: AutomergeUrl[]; - // Full payload items, used to add links when the dragged docs - // aren't part of this sideboard's folder tree - draggedItems?: Array<{ url: AutomergeUrl; name?: string; type?: string }>; + // Full payload items: where each dragged docref sits (so a move takes + // that link and no other), and what to add when the dragged docs aren't + // part of this sideboard's folder tree + draggedItems?: Array<{ + url: AutomergeUrl; + name?: string; + type?: string; + parentUrl?: AutomergeUrl; + index?: number; + }>; sourceToolId: string; copyMode: boolean; } @@ -78,7 +85,8 @@ export async function executeDrop( const sources = await findSources( rootFolderHandle, - operation.draggedUrls, + operation.draggedItems ?? + operation.draggedUrls.map((url) => ({ url })), repo ); const target = await folderHandle(repo, rootFolderHandle, spot.folderUrl); @@ -166,14 +174,23 @@ export async function removeItemsByUrl( } } -// Every folder the walk loads is cached here, so folderHandle() and -// isDescendantFolder() can read it without loading again. +/** + * The docref each dragged item stands for: exactly one per item. A doc can be + * linked from several folders (and twice from one), so the url alone doesn't + * say which link was dragged; the row's parent and index do. If the tree has + * changed since the drag began and that index no longer holds the doc, take + * its link in the same folder, and failing that its first link anywhere. + * + * Every folder the walk loads is cached, so folderHandle() and + * isDescendantFolder() can read it without loading again. + */ async function findSources( rootFolderHandle: DocHandle, - draggedUrls: AutomergeUrl[], + dragged: Array<{ url: AutomergeUrl; parentUrl?: AutomergeUrl; index?: number }>, repo: Repo ): Promise { - const sources: ItemLocation[] = []; + const draggedUrls = new Set(dragged.map((item) => item.url)); + const found = new Map(); const visitedFolders = new Set(); async function traverse( @@ -190,8 +207,10 @@ async function findSources( for (let i = 0; i < docs.length; i++) { const doc = docs[i]; - if (draggedUrls.includes(doc.url)) { - sources.push({ url: doc.url, folderUrl, index: i, item: doc }); + if (draggedUrls.has(doc.url)) { + const list = found.get(doc.url) ?? []; + list.push({ url: doc.url, folderUrl, index: i, item: doc }); + found.set(doc.url, list); } if (doc.type !== "folder" || !doc.url) continue; if (doc.url === folderUrl || visitedFolders.has(doc.url)) continue; @@ -211,9 +230,38 @@ async function findSources( } await traverse(rootFolderHandle.url, rootFolderHandle.doc()?.docs, 0); + + const sources: ItemLocation[] = []; + const taken = new Set(); + for (const item of dragged) { + const links = (found.get(item.url) ?? []).filter( + (link) => !taken.has(link.folderUrl + "/" + link.index) + ); + const inParent = links.filter((link) => link.folderUrl === item.parentUrl); + const source = + inParent.find((link) => link.index === item.index) ?? + inParent[0] ?? + links[0]; + if (!source) continue; + taken.add(source.folderUrl + "/" + source.index); + sources.push(source); + } return sources; } +/** + * The links to put in `folder`, leaving out any doc it already links to (and + * any doc that appears twice among them): a folder holds one link per doc. + */ +function notAlreadyIn(folder: FolderDoc, links: DocLink[]): DocLink[] { + const present = new Set(folder.docs?.map((doc) => doc.url) ?? []); + return links.filter((link) => { + if (present.has(link.url)) return false; + present.add(link.url); + return true; + }); +} + async function folderHandle( repo: Repo, rootFolderHandle: DocHandle, @@ -341,10 +389,14 @@ async function performMove( ).length; const index = spot.index - removedBefore; + // Moving a doc into a folder that already links to it just takes away the + // link it was dragged from. + let added: DocLink[] = []; target.change((doc) => { - doc.docs.splice(index, 0, ...removedItems); + added = notAlreadyIn(doc, removedItems); + if (added.length) doc.docs.splice(index, 0, ...added); }); - return landedAt(spot.folderUrl, index, removedItems); + return landedAt(spot.folderUrl, index, added); } // Add links for docs dragged in from outside this sideboard's folder tree @@ -415,8 +467,17 @@ async function performCopy( if (item) itemsToCopy.push(extractPlainDocLink(item)); } + const fresh = notAlreadyIn(target.doc()!, itemsToCopy); + if (!fresh.length) { + const name = itemsToCopy[0]?.name; + throw new DropError( + itemsToCopy.length === 1 && name + ? `“${name}” is already in this folder.` + : "Those are already in this folder." + ); + } target.change((doc) => { - doc.docs.splice(spot.index, 0, ...itemsToCopy); + doc.docs.splice(spot.index, 0, ...fresh); }); - return landedAt(spot.folderUrl, spot.index, itemsToCopy); + return landedAt(spot.folderUrl, spot.index, fresh); } diff --git a/sideboard/src/sideboard/dnd/payload.ts b/sideboard/src/sideboard/dnd/payload.ts index 1cf7dc12..781cae47 100644 --- a/sideboard/src/sideboard/dnd/payload.ts +++ b/sideboard/src/sideboard/dnd/payload.ts @@ -8,6 +8,9 @@ export type DndPayloadItem = { url: AutomergeUrl; name?: string; type?: string; + /** where the dragged docref sits, when it came from a sideboard row */ + parentUrl?: AutomergeUrl; + index?: number; }; export type DndPayload = { diff --git a/sideboard/src/sideboard/document-list/item.tsx b/sideboard/src/sideboard/document-list/item.tsx index 6acdaccc..b423efda 100644 --- a/sideboard/src/sideboard/document-list/item.tsx +++ b/sideboard/src/sideboard/document-list/item.tsx @@ -85,6 +85,8 @@ export default function Item(props: { url: props.url, name: props.name, source: props.element.toolId!, + parentUrl: parentUrl(), + index: props.itemIndex ?? 0, }); const springLoad = createHoverTimer(SPRING_LOAD_MS, () => {