Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Restore calendar drag preview to its original position https://github.com/siyuan-note/siyuan/issues/19887

Daniel6af688423dfa
diff --git a/app/src/protyle/render/av/calendar/preview.test.ts b/app/src/protyle/render/av/calendar/preview.test.ts
index 825d9ef34c..0a658a4120 100644
--- a/app/src/protyle/render/av/calendar/preview.test.ts
+++ b/app/src/protyle/render/av/calendar/preview.test.ts
@@ -2,6 +2,15 @@ import * as assert from "node:assert/strict";
import {test} from "node:test";
import {getCalendarPreviewTop} from "./preview";
+test("calendar preview returns to the original row when its date range is unchanged", () => {
+ const items = [{left: 0, right: 100, top: 30, bottom: 60},
+ {left: 0, right: 100, top: 63, bottom: 103}, {left: 0, right: 100, top: 106, bottom: 136}];
+ assert.equal(getCalendarPreviewTop(30, 40, 0, 100, items, 3, items[1]), 63);
+ assert.equal(getCalendarPreviewTop(30, 40, 0, 100, items, 3), 139);
+ const continuation = {left: 0, right: 300, top: 90, bottom: 130};
+ assert.equal(getCalendarPreviewTop(30, 40, 0, 300, [continuation], 3, continuation), 90);
+});
+
test("calendar preview uses free space without covering existing entries", () => {
const items = [{left: 0, right: 100, top: 30, bottom: 60},
{left: 100, right: 200, top: 30, bottom: 60}, {left: 0, right: 100, top: 95, bottom: 125}];
diff --git a/app/src/protyle/render/av/calendar/preview.ts b/app/src/protyle/render/av/calendar/preview.ts
index d4ecf9b07c..6dee4fe3ea 100644
--- a/app/src/protyle/render/av/calendar/preview.ts
+++ b/app/src/protyle/render/av/calendar/preview.ts
@@ -7,7 +7,11 @@ interface IPreviewRect {
// 按实际尺寸避让目标日期列中的内容,换行条目和更多按钮也参与占位。
export const getCalendarPreviewTop = (top: number, height: number, left: number, right: number,
- obstacles: IPreviewRect[], gap: number) => {
+ obstacles: IPreviewRect[], gap: number, source?: IPreviewRect) => {
+ // 日期范围未变化时复用原条目位置,保留同一天内的显示顺序。
+ if (source) {
+ return source.top;
+ }
const occupied = obstacles.filter(rect => rect.left < right && rect.right > left)
.sort((a, b) => a.top - b.top);
for (const rect of occupied) {
@@ -24,7 +28,7 @@ export const getCalendarPreviewTop = (top: number, height: number, left: number,
export const createCalendarPreviewLayout = () => {
const heights = new Map<HTMLElement, {value: string; priority: string}>();
return {
- place: (week: HTMLElement, layer: HTMLElement, card: HTMLElement) => {
+ place: (week: HTMLElement, layer: HTMLElement, card: HTMLElement, source?: HTMLElement) => {
const layerRect = layer.getBoundingClientRect();
const cardRect = card.getBoundingClientRect();
const header = week.querySelector<HTMLElement>(".av__calendar-days").getBoundingClientRect();
@@ -32,7 +36,8 @@ export const createCalendarPreviewLayout = () => {
const gap = parseFloat(getComputedStyle(grid).rowGap) || 0;
const obstacles = Array.from(week.querySelectorAll<HTMLElement>("[data-calendar-item], [data-calendar-expand]"))
.map(element => element.getBoundingClientRect());
- const top = getCalendarPreviewTop(header.bottom, cardRect.height, cardRect.left, cardRect.right, obstacles, gap);
+ const top = getCalendarPreviewTop(header.bottom, cardRect.height, cardRect.left, cardRect.right,
+ obstacles, gap, source?.getBoundingClientRect());
card.style.top = `${top - layerRect.top}px`;
const rect = week.getBoundingClientRect();
const style = getComputedStyle(week);
diff --git a/app/src/protyle/render/av/calendar/render.ts b/app/src/protyle/render/av/calendar/render.ts
index 1d0b0f62c6..273e57f18f 100644
--- a/app/src/protyle/render/av/calendar/render.ts
+++ b/app/src/protyle/render/av/calendar/render.ts
@@ -149,7 +149,9 @@ const bindCalendarDrag = (root: HTMLElement, protyle: IProtyle, blockElement: HT
card.style.width = `calc(${segment.span * 100 / 7}% - 4px)`;
card.style.top = "0";
week.append(layer);
- previewLayout.place(week, layer, card);
+ const source = next.start === entry.start && next.end === entry.end ?
+ sourceItems.find(element => week.contains(element)) : undefined;
+ previewLayout.place(week, layer, card, source);
});
};
const clean = () => {