Skip to content

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

:bug: Preserve calendar view mode after restart https://github.com/siyuan-note/siyuan/issues/19767

Danielb4689eb71e12
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
index 59e34540bf..75953af6b3 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
@@ -7,7 +7,7 @@
"id": "20240208172514-9dsv6na",
"title": "Database",
"type": "doc",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -45,7 +45,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20251224101749-zybcbjk",
- "updated": "20260920084445"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -687,7 +687,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20260920225154-tz8a00o",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -701,12 +701,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260920225154-vz01ydx",
- "updated": "20260920225154"
+ "updated": "20260923103343"
},
"Children": [
{
"Type": "NodeText",
- "Data": "Displays dated entries by month or week. The week view has seven date columns; entries spanning multiple days continue across those days. Your browsing date and month/week choice are independent in each editor."
+ "Data": "Displays dated entries by month or week. The week view has seven date columns; entries spanning multiple days continue across those days. Each editor keeps its current mode and browsing date independently. The month/week choice is saved for each calendar view in this workspace and restored when opening it in a new editor or restarting SiYuan. A new editor starts browsing from today."
}
]
},
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
index b62c43ac12..49426642ca 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
@@ -7,7 +7,7 @@
"id": "20240208113259-nykkvaq",
"title": "数据库",
"type": "doc",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -45,7 +45,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20240208115136-9g2b46l",
- "updated": "20260920084445"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -687,7 +687,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20260920225154-xtg5hn3",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -701,12 +701,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260920225154-5w7mqrf",
- "updated": "20260920225154"
+ "updated": "20260923103343"
},
"Children": [
{
"Type": "NodeText",
- "Data": "按月或周显示有日期的条目。周视图包含七个日期列,跨日条目会连续显示在对应日期上。各编辑器独立记录当前浏览日期和月、周模式。"
+ "Data": "按月或周显示有日期的条目。周视图包含七个日期列,跨日条目会连续显示在对应日期上。各编辑器的当前模式和浏览日期互不影响。每个日历视图的月、周模式会保存在当前工作空间中,在新编辑器中打开或重启思源后恢复。新编辑器从今天开始浏览。"
}
]
},
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
index 46b3ec8cfc..c2772a922e 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
@@ -7,7 +7,7 @@
"id": "20240208171522-y7dxcno",
"title": "資料庫",
"type": "doc",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -45,7 +45,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20251224101729-yhz5zrt",
- "updated": "20260920084445"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -687,7 +687,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20260920225154-xknvq29",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -701,12 +701,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260920225154-z9ga0hb",
- "updated": "20260920225154"
+ "updated": "20260923103343"
},
"Children": [
{
"Type": "NodeText",
- "Data": "按月或週顯示有日期的項目。週視圖包含七個日期欄,跨日項目會連續顯示在對應日期上。各編輯器獨立記錄目前瀏覽日期和月、週模式。"
+ "Data": "按月或週顯示有日期的項目。週視圖包含七個日期欄,跨日項目會連續顯示在對應日期上。各編輯器的目前模式與瀏覽日期互不影響。每個日曆視圖的月、週模式會儲存在目前工作空間,在新編輯器中開啟或重新啟動思源後恢復。新編輯器從今天開始瀏覽。"
}
]
},
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
index 40d48245f6..abf1e9d8e8 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
@@ -8,7 +8,7 @@
"id": "20240530101000-3eaevtp",
"title": "データベース",
"type": "doc",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -46,7 +46,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20251224101829-lva3op2",
- "updated": "20260920084445"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -688,7 +688,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20260920225154-vntgfc2",
- "updated": "20260923095432"
+ "updated": "20260923103343"
},
"Children": [
{
@@ -702,12 +702,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260920225154-m98kczu",
- "updated": "20260920225154"
+ "updated": "20260923103343"
},
"Children": [
{
"Type": "NodeText",
- "Data": "日付のあるエントリを月または週ごとに表示します。週表示には7つの日付列があり、複数日にわたるエントリは各日に連続して表示されます。表示中の日付と月・週の切り替えは、エディターごとに保持されます。"
+ "Data": "日付のあるエントリを月または週ごとに表示します。週表示には7つの日付列があり、複数日にわたるエントリは各日に連続して表示されます。各エディターの現在の表示モードと閲覧日付は互いに影響しません。月・週の表示モードはカレンダービューごとにこのワークスペースへ保存され、新しいエディターで開いたときや SiYuan の再起動後に復元されます。新しいエディターでは今日から閲覧を始めます。"
}
]
},
diff --git a/app/src/constants.ts b/app/src/constants.ts
index 8ea5266ad1..910815e88e 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -200,6 +200,7 @@ export abstract class Constants {
public static readonly LOCAL_MOVE_PATH = "local-move-path";
public static readonly LOCAL_RECENT_DOCS = "local-recent-docs";
public static readonly LOCAL_CLOSED_TABS = "local-closed-tabs";
+ public static readonly LOCAL_AV_CALENDAR_MODES = "local-av-calendar-modes";
// dialog
public static readonly DIALOG_CONFIRM = "dialog-confirm";
diff --git a/app/src/protyle/render/av/calendar/render.ts b/app/src/protyle/render/av/calendar/render.ts
index 92af2bd640..07c94ca0d6 100644
--- a/app/src/protyle/render/av/calendar/render.ts
+++ b/app/src/protyle/render/av/calendar/render.ts
@@ -1,4 +1,5 @@
import {Constants} from "../../../../constants";
+import {setStorageVal} from "../../../util/compatibility";
import * as dayjs from "dayjs";
import {escapeAttr, escapeHtml} from "../../../../util/escape";
import {transaction} from "../../../wysiwyg/transaction";
@@ -18,7 +19,7 @@ import {addCalendarDays, calendarDay, calendarDayDistance, getCalendarInterval,
moveCalendarDate, packCalendarWeek, resizeCalendarDate} from "./date";
import {openCalendarJump} from "./jump";
import {addCalendarDateField, bindCalendarSettings, getCalendarSettingsHTML, isCalendarDateColumn} from "./settings";
-import {getCalendarRequestRange, getCalendarState} from "./state";
+import {getCalendarRequestRange, getCalendarState, setCalendarMode} from "./state";
const iconButton = (action: string, icon: string, label: string) => `<button type="button" class="block__icon block__icon--show" data-calendar-action="${action}" aria-label="${escapeAttr(label)}"><svg><use xlink:href="#${icon}"></use></svg></button>`;
@@ -403,7 +404,10 @@ export const renderCalendar = async (blockElement: HTMLElement, protyle: IProtyl
}});
});
root.querySelector<HTMLSelectElement>("[data-calendar-mode]").addEventListener("change", event => {
- state.mode = (event.target as HTMLSelectElement).value as "month" | "week";
+ const modes = setCalendarMode(blockElement, data.viewID, (event.target as HTMLSelectElement).value as "month" | "week");
+ if (modes) {
+ setStorageVal(Constants.LOCAL_AV_CALENDAR_MODES, modes);
+ }
state.expandedWeeks.clear();
refresh();
});
diff --git a/app/src/protyle/render/av/calendar/state.test.ts b/app/src/protyle/render/av/calendar/state.test.ts
index f297df441c..07a4e30789 100644
--- a/app/src/protyle/render/av/calendar/state.test.ts
+++ b/app/src/protyle/render/av/calendar/state.test.ts
@@ -7,16 +7,19 @@ import * as ts from "typescript";
import * as dates from "./date";
const stateExports = {};
+const storage: Record<string, Record<string, string>> = {"local-av-calendar-modes": {}};
runInNewContext(ts.transpileModule(readFileSync(join(__dirname, "state.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},
}).outputText, {
exports: stateExports,
- require: (id: string) => id === "./date" ? dates : {Constants: {CUSTOM_SY_AV_VIEW: "custom-sy-av-view"}},
+ require: (id: string) => id === "./date" ? dates :
+ {Constants: {CUSTOM_SY_AV_VIEW: "custom-sy-av-view", LOCAL_AV_CALENDAR_MODES: "local-av-calendar-modes"}},
+ window: {siyuan: {storage}},
});
-const {getCalendarCreationDate, getCalendarState} = stateExports as typeof import("./state");
+const {getCalendarCreationDate, getCalendarRequestRange, getCalendarState, setCalendarMode} = stateExports as typeof import("./state");
-const block = (viewID: string, type = "calendar") => ({getAttribute: (key: string) =>
- key === "data-av-type" ? type : viewID}) as unknown as Element;
+const block = (viewID: string, type = "calendar", avID = "database") => ({getAttribute: (key: string) =>
+ key === "data-av-type" ? type : key === "data-av-id" ? avID : viewID}) as unknown as Element;
test("calendar navigation is independent between editors and database views", () => {
const first = block("view-a");
@@ -31,6 +34,26 @@ test("calendar navigation is independent between editors and database views", ()
assert.equal(getCalendarState(first, "view-b").mode, "month");
});
+test("calendar mode survives reopening without restoring the browsing date or changing other views", () => {
+ const first = block("persisted-view");
+ const state = getCalendarState(first);
+ state.anchor = new Date(2020, 0, 1).getTime();
+ assert.equal(setCalendarMode(first, "persisted-view", "week")["database:persisted-view"], "week");
+ assert.equal(storage["local-av-calendar-modes"]["database:persisted-view"], "week");
+
+ const reopened = block("persisted-view");
+ assert.equal(getCalendarState(reopened).mode, "week");
+ assert.equal(getCalendarState(reopened).anchor, dates.calendarDay(Date.now()));
+ const range = getCalendarRequestRange(reopened);
+ assert.equal(dates.calendarDayDistance(range.start, range.end), 7);
+ assert.equal(getCalendarState(block("other-view")).mode, "month");
+ assert.equal(getCalendarState(block("persisted-view", "calendar", "other-database")).mode, "month");
+
+ setCalendarMode(reopened, "persisted-view", "month");
+ assert.equal(storage["local-av-calendar-modes"]["database:persisted-view"], undefined);
+ assert.equal(getCalendarState(block("persisted-view")).mode, "month");
+});
+
test("only a calendar with an ordinary date source supplies the date of a new entry", () => {
const calendar = block("view-a");
const state = getCalendarState(calendar);
diff --git a/app/src/protyle/render/av/calendar/state.ts b/app/src/protyle/render/av/calendar/state.ts
index 94caf4d081..4f72c7f6d6 100644
--- a/app/src/protyle/render/av/calendar/state.ts
+++ b/app/src/protyle/render/av/calendar/state.ts
@@ -12,6 +12,16 @@ interface ICalendarState {
const states = new WeakMap<Element, Map<string, ICalendarState>>();
+const getModeKey = (blockElement: Element, viewID: string) => {
+ const avID = blockElement.getAttribute("data-av-id");
+ return avID && viewID ? `${avID}:${viewID}` : "";
+};
+
+const getSavedMode = (blockElement: Element, viewID: string): ICalendarState["mode"] => {
+ const key = getModeKey(blockElement, viewID);
+ return key && window.siyuan.storage?.[Constants.LOCAL_AV_CALENDAR_MODES]?.[key] === "week" ? "week" : "month";
+};
+
export const getCalendarState = (blockElement: Element, viewID = blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW) || "") => {
let views = states.get(blockElement);
if (!views) {
@@ -20,12 +30,29 @@ export const getCalendarState = (blockElement: Element, viewID = blockElement.ge
}
let state = views.get(viewID);
if (!state) {
- state = {anchor: calendarDay(Date.now()), mode: "month", weekStart: 1, expandedWeeks: new Set()};
+ state = {anchor: calendarDay(Date.now()), mode: getSavedMode(blockElement, viewID), weekStart: 1, expandedWeeks: new Set()};
views.set(viewID, state);
}
return state;
};
+export const setCalendarMode = (blockElement: Element, viewID: string, mode: ICalendarState["mode"]) => {
+ getCalendarState(blockElement, viewID).mode = mode;
+ const key = getModeKey(blockElement, viewID);
+ if (!key || !window.siyuan.storage) {
+ return;
+ }
+ const stored = window.siyuan.storage[Constants.LOCAL_AV_CALENDAR_MODES];
+ const modes = stored && typeof stored === "object" && !Array.isArray(stored) ? {...stored} : {};
+ if (mode === "week") {
+ modes[key] = mode;
+ } else {
+ delete modes[key];
+ }
+ window.siyuan.storage[Constants.LOCAL_AV_CALENDAR_MODES] = modes;
+ return modes;
+};
+
export const getCalendarRequestRange = (blockElement: Element, viewID?: string) => {
const state = getCalendarState(blockElement, viewID);
return getCalendarRange(state.anchor, state.mode, state.weekStart);
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index c910c9782f..4be41afe14 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -806,6 +806,7 @@ export const getLocalStorage = (cb: () => void) => {
defaultStorage[Constants.LOCAL_ZOOM] = 1;
defaultStorage[Constants.LOCAL_MOVE_PATH] = {keys: [], k: ""};
defaultStorage[Constants.LOCAL_RECENT_DOCS] = {type: "viewedAt"}; // TRecentDocsSort
+ defaultStorage[Constants.LOCAL_AV_CALENDAR_MODES] = {};
[Constants.LOCAL_EXPORTIMG, Constants.LOCAL_EXPORTPATH, Constants.LOCAL_SEARCHKEYS, Constants.LOCAL_PDFTHEME, Constants.LOCAL_BAZAAR,
Constants.LOCAL_EXPORTWORD, Constants.LOCAL_EXPORTPDF, Constants.LOCAL_DOCINFO, Constants.LOCAL_MOBILE_TABS,
@@ -816,7 +817,7 @@ export const getLocalStorage = (cb: () => void) => {
Constants.LOCAL_DIALOGPOSITION, Constants.LOCAL_SEARCHUNREF, Constants.LOCAL_HISTORY,
Constants.LOCAL_OUTLINE, Constants.LOCAL_FILEPOSITION, Constants.LOCAL_FILESPATHS, Constants.LOCAL_IMAGES,
Constants.LOCAL_PLUGIN_DOCKS, Constants.LOCAL_EMOJIS, Constants.LOCAL_MOVE_PATH, Constants.LOCAL_RECENT_DOCS,
- Constants.LOCAL_CLOSED_TABS].forEach((key) => {
+ Constants.LOCAL_CLOSED_TABS, Constants.LOCAL_AV_CALENDAR_MODES].forEach((key) => {
const value = response.data[key];
if (typeof value === "string") {
try {