Skip to content

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

:bug: Prevent calendar drag previews from overlapping existing entries https://github.com/siyuan-note/siyuan/issues/13740

Danielaad84eff42d6
diff --git a/app/src/protyle/render/av/calendar/preview.test.ts b/app/src/protyle/render/av/calendar/preview.test.ts
new file mode 100644
index 0000000000..825d9ef34c
--- /dev/null
+++ b/app/src/protyle/render/av/calendar/preview.test.ts
@@ -0,0 +1,19 @@
+import * as assert from "node:assert/strict";
+import {test} from "node:test";
+import {getCalendarPreviewTop} from "./preview";
+
+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}];
+ assert.equal(getCalendarPreviewTop(30, 25, 0, 100, items, 3), 63);
+ assert.equal(getCalendarPreviewTop(30, 25, 200, 300, items, 3), 30);
+ assert.equal(getCalendarPreviewTop(30, 40, 0, 100, items, 3), 128);
+});
+
+test("calendar preview clears all covered columns and variable-height content", () => {
+ const items = [{left: 100, right: 200, top: 65, bottom: 100},
+ {left: 0, right: 100, top: 30, bottom: 95}, {left: 100, right: 200, top: 103, bottom: 125}];
+ assert.equal(getCalendarPreviewTop(30, 28, 0, 200, items, 3), 128);
+ assert.equal(getCalendarPreviewTop(30, 28, 0, 100, items, 3), 98);
+ assert.equal(items[0].top, 65);
+});
diff --git a/app/src/protyle/render/av/calendar/preview.ts b/app/src/protyle/render/av/calendar/preview.ts
new file mode 100644
index 0000000000..d4ecf9b07c
--- /dev/null
+++ b/app/src/protyle/render/av/calendar/preview.ts
@@ -0,0 +1,64 @@
+interface IPreviewRect {
+ top: number;
+ bottom: number;
+ left: number;
+ right: number;
+}
+
+// 按实际尺寸避让目标日期列中的内容,换行条目和更多按钮也参与占位。
+export const getCalendarPreviewTop = (top: number, height: number, left: number, right: number,
+ obstacles: IPreviewRect[], gap: number) => {
+ const occupied = obstacles.filter(rect => rect.left < right && rect.right > left)
+ .sort((a, b) => a.top - b.top);
+ for (const rect of occupied) {
+ if (top + height + gap <= rect.top) {
+ break;
+ }
+ if (top < rect.bottom + gap) {
+ top = rect.bottom + gap;
+ }
+ }
+ return top;
+};
+
+export const createCalendarPreviewLayout = () => {
+ const heights = new Map<HTMLElement, {value: string; priority: string}>();
+ return {
+ place: (week: HTMLElement, layer: HTMLElement, card: HTMLElement) => {
+ const layerRect = layer.getBoundingClientRect();
+ const cardRect = card.getBoundingClientRect();
+ const header = week.querySelector<HTMLElement>(".av__calendar-days").getBoundingClientRect();
+ const grid = week.querySelector<HTMLElement>(".av__calendar-events");
+ 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);
+ card.style.top = `${top - layerRect.top}px`;
+ const rect = week.getBoundingClientRect();
+ const style = getComputedStyle(week);
+ const bottomPadding = parseFloat(style.paddingBottom) || 0;
+ const borders = (parseFloat(style.borderTopWidth) || 0) + (parseFloat(style.borderBottomWidth) || 0);
+ const required = top + cardRect.height + bottomPadding + (parseFloat(style.borderBottomWidth) || 0) - rect.top;
+ if (required > rect.height) {
+ if (!heights.has(week)) {
+ heights.set(week, {value: week.style.getPropertyValue("min-height"),
+ priority: week.style.getPropertyPriority("min-height")});
+ }
+ const extra = style.boxSizing === "border-box" ? 0 :
+ borders + bottomPadding + (parseFloat(style.paddingTop) || 0);
+ // 一轮拖动内只增加占位,避免切换目标日期时周高度反复变化。
+ week.style.minHeight = `${required - extra}px`;
+ }
+ },
+ destroy: () => {
+ heights.forEach(({value, priority}, week) => {
+ if (value) {
+ week.style.setProperty("min-height", value, priority);
+ } else {
+ week.style.removeProperty("min-height");
+ }
+ });
+ heights.clear();
+ },
+ };
+};
diff --git a/app/src/protyle/render/av/calendar/render.ts b/app/src/protyle/render/av/calendar/render.ts
index a72b652a48..9bdd21ec4c 100644
--- a/app/src/protyle/render/av/calendar/render.ts
+++ b/app/src/protyle/render/av/calendar/render.ts
@@ -20,6 +20,7 @@ import {addCalendarDays, calendarDay, calendarDayDistance, getCalendarInterval,
moveCalendarDate, packCalendarWeek, resizeCalendarDate} from "./date";
import {openCalendarJump} from "./jump";
import {getCalendarDropDay} from "./hitTest";
+import {createCalendarPreviewLayout} from "./preview";
import {addCalendarDateField, bindCalendarSettings, getCalendarSettingsHTML, isCalendarDateColumn} from "./settings";
import {getCalendarRequestRange, getCalendarState, setCalendarMode} from "./state";
import {bindCalendarUndated, getCalendarUndatedHTML} from "./undated";
@@ -103,6 +104,9 @@ const bindCalendarDrag = (root: HTMLElement, protyle: IProtyle, blockElement: HT
let destination = origin;
let dragging = false;
let cleaned = false;
+ let lastX = x;
+ let lastY = y;
+ const previewLayout = createCalendarPreviewLayout();
const sourceItems = Array.from(root.querySelectorAll<HTMLElement>("[data-calendar-item]"))
.filter(element => element.dataset.calendarItem === entry.row.id);
const clearPreview = () => {
@@ -137,8 +141,9 @@ const bindCalendarDrag = (root: HTMLElement, protyle: IProtyle, blockElement: HT
card.removeAttribute("tabindex");
card.style.left = `${segment.column * 100 / 7}%`;
card.style.width = `calc(${segment.span * 100 / 7}% - 4px)`;
- card.style.top = `${week.querySelector(".av__calendar-days").getBoundingClientRect().height}px`;
+ card.style.top = "0";
week.append(layer);
+ previewLayout.place(week, layer, card);
});
};
const clean = () => {
@@ -147,6 +152,7 @@ const bindCalendarDrag = (root: HTMLElement, protyle: IProtyle, blockElement: HT
}
cleaned = true;
clearPreview();
+ previewLayout.destroy();
root.classList.remove("av__calendar--dragging", "av__calendar--invalid");
sourceItems.forEach(element => element.classList.remove("av__calendar-item--dragging"));
if (dragging) {
@@ -170,11 +176,19 @@ const bindCalendarDrag = (root: HTMLElement, protyle: IProtyle, blockElement: HT
return;
}
begin();
+ if (clientX === lastX && clientY === lastY) {
+ return;
+ }
+ lastX = clientX;
+ lastY = clientY;
destination = getCalendarDropDay(root, clientX, clientY);
preview();
},
finish: (clientX: number, clientY: number) => {
- destination = getCalendarDropDay(root, clientX, clientY);
+ // 占位扩展后,同一落点沿用已展示的预览日期,避免松手时跳到相邻周。
+ if (clientX !== lastX || clientY !== lastY) {
+ destination = getCalendarDropDay(root, clientX, clientY);
+ }
if (dragging && root.isConnected) {
const date = candidate();
if (date && JSON.stringify(date) !== JSON.stringify(entry.date.value.date)) {