Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix an issue

Adorian Doranda37e8d36932
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 23d79581fe..4f5b907daa 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -27,6 +27,15 @@ import { useScrollFade } from "../../react/scroll_fade";
*/
const FOOTER_QUIET_MS = 5000;
+/**
+ * How long the card just made is still the one the column opens out for.
+ *
+ * Long enough to cover a write that never answers, as `FOOTER_QUIET_MS` is. `created` names the
+ * card until another is made, and the card itself is not marked in any way, so without this a card
+ * dragged out of the column and back opens out again on the drop.
+ */
+const CREATED_GROW_MS = 5000;
+
/** How long an open takes. Matches `--board-expand-duration` in the board's own rules. */
const EXPAND_MS = 200;
import NoteLink from "../../react/NoteLink";
@@ -93,7 +102,8 @@ export default function Column({
onFocusCard: (noteId: string) => void
} & DragContext) {
const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);
- const [ created, setCreated ] = useState<{ noteId?: string, takesFocus: boolean }>();
+ const [ created, setCreated ] =
+ useState<{ noteId?: string, takesFocus: boolean, at: number }>();
// `isNew` stays true until another column is added, so the reveal is recorded here rather than
// replayed on every redraw of the board.
const [ isRevealed, setIsRevealed ] = useState(false);
@@ -109,14 +119,15 @@ export default function Column({
// An inserted card is left focused, since that is where the work is; a card from the footer is
// not, or it would take focus from the editor being typed in.
const cardInserted = useCallback(
- (noteId: string | undefined) => setCreated({ noteId, takesFocus: true }), []);
+ (noteId: string | undefined) =>
+ setCreated({ noteId, takesFocus: true, at: Date.now() }), []);
const addingFromFooter = useCallback(() => {
// Long enough for a write that never answers; `cardAdded` shortens it when one does.
footerQuietUntil.current = Date.now() + FOOTER_QUIET_MS;
}, []);
const cardAdded = useCallback((noteId: string | undefined) => {
footerQuietUntil.current = Date.now() + FLIP_SETTLE_MS;
- setCreated({ noteId, takesFocus: false });
+ setCreated({ noteId, takesFocus: false, at: Date.now() });
}, []);
const { setColumnNameToEdit, setColumnLimitToEdit, setActiveColumn } =
useContext(BoardActionsContext);
@@ -132,7 +143,8 @@ export default function Column({
// `footerQuietUntil`: the scroll to the end and the fade already show it.
useFlip(contentRef, {
selector: ".board-note",
- grow: (card) => card.getAttribute("data-note-id") === created?.noteId
+ grow: (card) => !!created && card.getAttribute("data-note-id") === created.noteId
+ && Date.now() - created.at < CREATED_GROW_MS
&& Date.now() > footerQuietUntil.current
});
const { handleDragOver, handleDragLeave, handleDrop } = useDragging({
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index a2fdbe2a9a..bf2d6159fe 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -100,7 +100,11 @@ function Harness({ note, noteIds, initialConfig }: { note: ReturnType<typeof bui
/** Files a fresh note under the board, the way an import or another client's write reaches it. */
function addCard(board: ReturnType<typeof buildNote>, status: string) {
- const card = buildNote({ title: "Added", "#status": status });
+ return fileCard(board, buildNote({ title: "Added", "#status": status }));
+}
+
+/** Puts a note the test already holds under the board, for one made before it is filed. */
+function fileCard(board: ReturnType<typeof buildNote>, card: ReturnType<typeof buildNote>) {
const branchId = `${board.noteId}_${card.noteId}`;
froca.branches[branchId] = new FBranch(froca, {
@@ -111,6 +115,7 @@ function addCard(board: ReturnType<typeof buildNote>, status: string) {
parentNoteId: board.noteId
});
board.addChild(card.noteId, branchId, false);
+ return card;
}
/** Opens the title editor the way the keyboard does, the menu entry needing a rendered menu. */
@@ -1434,6 +1439,46 @@ describe("Board column rename", () => {
expect(others.some(other => other.classList.contains("appearing"))).toBe(false);
});
+ /**
+ * The column names the card it just made until it makes another, so what opens out is bounded
+ * in time as well: a card dragged out of the column and back is not the one just made, however
+ * long it has been the last.
+ */
+ it("opens out the card it just made, and not that card once it is old", async () => {
+ for (const [ passed, height ] of [ [ 0, "0px" ], [ 6000, "" ] ] as const) {
+ const { note, container } = await setup();
+ withBoxes();
+
+ try {
+ const card = buildNote({ title: "Made", "#status": "Doing" });
+ vi.spyOn(BoardApi.prototype, "insertRowAtPosition")
+ .mockResolvedValue({ noteId: card.noteId } as never);
+
+ // Made here, and drawn only by the refresh that follows.
+ const standIn = container.querySelectorAll<HTMLElement>(".board-column")[1]
+ .querySelector<HTMLElement>(".board-note");
+ await act(async () => {
+ standIn?.dispatchEvent(new KeyboardEvent("keydown",
+ { key: "Enter", bubbles: true }));
+ await flush();
+ });
+
+ vi.setSystemTime(Date.now() + passed);
+ fileCard(note, card);
+ await redraw(note, container);
+
+ const arrived = [ ...container.querySelectorAll<HTMLElement>(".board-note") ]
+ .find(element => element.getAttribute("data-note-id") === card.noteId);
+ if (!arrived) throw new Error("expected the card just made to be drawn");
+ expect(arrived.style.height).toBe(height);
+ } finally {
+ vi.useRealTimers();
+ dropBoxes();
+ render(null, container);
+ }
+ }
+ });
+
/**
* A card that arrives from somewhere else, a move between columns above all, mounts as a new
* element in the column that draws it. Only the card the column itself made opens out of
@@ -1441,33 +1486,11 @@ describe("Board column rename", () => {
*/
it("opens out only the card the column made, not one that merely arrived", async () => {
const { note, container } = await setup();
-
- // happy-dom lays nothing out, and a child with no offset parent is one `useFlip` skips.
- for (const [ property, read ] of Object.entries({
- offsetParent(this: HTMLElement) { return this.parentElement; },
- offsetHeight() { return 34; },
- offsetTop() { return 0; }
- })) {
- Object.defineProperty(HTMLElement.prototype, property,
- { configurable: true, get: read as () => unknown });
- }
+ withBoxes();
try {
addCard(note, "Doing");
- await act(async () => {
- render(
- <ParentComponent.Provider value={new Component()}>
- <Harness
- note={note}
- noteIds={[ ...note.getChildNoteIds() ]}
- initialConfig={DEFAULT_CONFIG}
- />
- </ParentComponent.Provider>,
- container
- );
- await flush();
- });
- await act(async () => { await flush(); });
+ await redraw(note, container);
const arrived = [ ...container.querySelectorAll<HTMLElement>(".board-note") ]
.find(card => card.textContent?.includes("Added"));
@@ -1475,12 +1498,46 @@ describe("Board column rename", () => {
expect(arrived.style.height).toBe("");
expect(arrived.style.overflow).toBe("");
} finally {
- for (const property of [ "offsetParent", "offsetHeight", "offsetTop" ]) {
- delete (HTMLElement.prototype as unknown as Record<string, unknown>)[property];
- }
+ dropBoxes();
}
});
+ /** happy-dom lays nothing out, and a child with no offset parent is one `useFlip` skips. */
+ function withBoxes() {
+ for (const [ property, read ] of Object.entries({
+ offsetParent(this: HTMLElement) { return this.parentElement; },
+ offsetHeight() { return 34; },
+ offsetTop() { return 0; }
+ })) {
+ Object.defineProperty(HTMLElement.prototype, property,
+ { configurable: true, get: read as () => unknown });
+ }
+ }
+
+ function dropBoxes() {
+ for (const property of [ "offsetParent", "offsetHeight", "offsetTop" ]) {
+ delete (HTMLElement.prototype as unknown as Record<string, unknown>)[property];
+ }
+ }
+
+ /** Draws the board again, which is how a card filed under it reaches the column. */
+ async function redraw(note: ReturnType<typeof buildNote>, container: HTMLElement) {
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={new Component()}>
+ <Harness
+ note={note}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ initialConfig={DEFAULT_CONFIG}
+ />
+ </ParentComponent.Provider>,
+ container
+ );
+ await flush();
+ });
+ await act(async () => { await flush(); });
+ }
+
it("reveals a card inserted next to another one, without leaving its place", async () => {
const { note, container } = await setup();