Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: allow editing note's created date from properties panel Signed-off-by: 01zulfi <[email protected]>

01zulfi2b1c9731775c
diff --git a/apps/web/__e2e__/models/note-properties.model.ts b/apps/web/__e2e__/models/note-properties.model.ts
index 21623126f..a3d1718bd 100644
--- a/apps/web/__e2e__/models/note-properties.model.ts
+++ b/apps/web/__e2e__/models/note-properties.model.ts
@@ -30,6 +30,7 @@ import {
iterateList
} from "./utils";
import { SessionHistoryItemModel } from "./session-history-item-model";
+import dayjs from "dayjs";
abstract class BaseProperties {
protected readonly page: Page;
@@ -222,6 +223,42 @@ export class NotePropertiesModel extends BaseProperties {
await this.close();
return history;
}
+
+ async getDateCreated() {
+ await this.open();
+ const dateCreated = await this.generalSection
+ .locator(getTestId("date-created"))
+ .textContent();
+ await this.close();
+ return dateCreated;
+ }
+
+ async editDateCreated(newDateCreated: number) {
+ await this.open();
+
+ const editIcon = this.page.locator(getTestId("edit-date-created"));
+ await editIcon.click();
+
+ const editDateCreatedPopup = this.page.locator(
+ getTestId("edit-date-created-popup")
+ );
+ await editDateCreatedPopup.waitFor({ state: "visible" });
+
+ const dateInput = editDateCreatedPopup.locator(
+ getTestId("date-created-input")
+ );
+ const timeInput = editDateCreatedPopup.locator(
+ getTestId("time-created-input")
+ );
+
+ const date = new Date(newDateCreated);
+ await dateInput.fill(dayjs(date).format("DD-MM-YYYY"));
+ await timeInput.fill(dayjs(date).format("hh:mm A"));
+
+ await editDateCreatedPopup.locator(getTestId("save-date-created")).click();
+
+ await this.close();
+ }
}
export class NoteContextMenuModel extends BaseProperties {
diff --git a/apps/web/__e2e__/notes.test.ts b/apps/web/__e2e__/notes.test.ts
index f33d79675..2d74b56da 100644
--- a/apps/web/__e2e__/notes.test.ts
+++ b/apps/web/__e2e__/notes.test.ts
@@ -20,7 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { test, expect } from "@playwright/test";
import { AppModel } from "./models/app.model";
import {
- getTestId,
groupByOptions,
NOTE,
orderByOptions,
@@ -447,3 +446,16 @@ test("archived note shouldn't appear in search results", async ({ page }) => {
const searchedNote = await notes.findNote(NOTE);
expect(searchedNote).toBeUndefined();
});
+
+test("edit note creation date in properties panel", async ({ page }) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote(NOTE);
+
+ const date = new Date("2020-01-15T10:30:00Z");
+ await note?.properties.editDateCreated(date.valueOf());
+
+ const dateCreated = await note?.properties.getDateCreated();
+ expect(dateCreated).toBe("15-01-2020 03:30 PM");
+});
diff --git a/apps/web/src/components/properties/index.tsx b/apps/web/src/components/properties/index.tsx
index 7e7954ab9..01ae5f5c5 100644
--- a/apps/web/src/components/properties/index.tsx
+++ b/apps/web/src/components/properties/index.tsx
@@ -45,6 +45,7 @@ import { useStore as useAppStore } from "../../stores/app-store";
import { useStore as useAttachmentStore } from "../../stores/attachment-store";
import { store as noteStore } from "../../stores/note-store";
import Toggle from "./toggle";
+import { NoteDateCreatedEditor } from "./note-date-created-editor";
import ScrollContainer from "../scroll-container";
import {
getFormattedDate,
@@ -212,13 +213,22 @@ function EditorProperties(props: EditorPropertiesProps) {
>
{item.label}
</Text>
- <Text
- className="selectable"
- variant="subBody"
- sx={{ fontSize: "body", flexShrink: 0 }}
- >
- {item.value(session.note[item.key])}
- </Text>
+ {item.key === "dateCreated" ? (
+ <NoteDateCreatedEditor
+ noteId={session.note.id}
+ dateCreated={session.note.dateCreated}
+ dateEdited={session.note.dateEdited}
+ displayValue={item.value(session.note[item.key])}
+ />
+ ) : (
+ <Text
+ className="selectable"
+ variant="subBody"
+ sx={{ fontSize: "body", flexShrink: 0 }}
+ >
+ {item.value(session.note[item.key])}
+ </Text>
+ )}
</Flex>
))}
{session.type === "deleted" ||
diff --git a/apps/web/src/components/properties/note-date-created-editor.tsx b/apps/web/src/components/properties/note-date-created-editor.tsx
new file mode 100644
index 000000000..1884c6fc0
--- /dev/null
+++ b/apps/web/src/components/properties/note-date-created-editor.tsx
@@ -0,0 +1,237 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { useState, useRef } from "react";
+import { Button, Flex, Text } from "@theme-ui/components";
+import { Calendar, Edit } from "../icons";
+import Field from "../field";
+import { DayPicker } from "../day-picker";
+import { PopupPresenter } from "@notesnook/ui";
+import { db } from "../../common/db";
+import { showToast } from "../../utils/toast";
+import { useStore as useThemeStore } from "../../stores/theme-store";
+import { store as noteStore } from "../../stores/note-store";
+import { getFormattedDate } from "@notesnook/common";
+import { getTimeFormat } from "@notesnook/core";
+import { setTimeOnly, setDateOnly } from "../../utils/date-time";
+import dayjs from "dayjs";
+import customParseFormat from "dayjs/plugin/customParseFormat";
+import { strings } from "@notesnook/intl";
+
+dayjs.extend(customParseFormat);
+
+type NoteDateCreatedEditorProps = {
+ noteId: string;
+ dateCreated: number;
+ dateEdited: number;
+ displayValue: string;
+};
+
+export function NoteDateCreatedEditor({
+ noteId,
+ dateCreated,
+ dateEdited,
+ displayValue
+}: NoteDateCreatedEditorProps) {
+ const [isEditing, setIsEditing] = useState(false);
+ const [showCalendar, setShowCalendar] = useState(false);
+ const [editedDate, setEditedDate] = useState<dayjs.Dayjs>(dayjs());
+ const dateTextRef = useRef<HTMLDivElement>(null);
+ const dateInputRef = useRef<HTMLInputElement>(null);
+ const theme = useThemeStore((store) => store.colorScheme);
+
+ return (
+ <>
+ <Flex sx={{ alignItems: "center", gap: 1 }}>
+ <Text
+ data-test-id="date-created"
+ ref={dateTextRef}
+ className="selectable"
+ variant="subBody"
+ sx={{ fontSize: "body", flexShrink: 0 }}
+ >
+ {displayValue}
+ </Text>
+ <Edit
+ size={14}
+ sx={{ cursor: "pointer", color: "icon" }}
+ onClick={() => {
+ setEditedDate(dayjs(dateCreated));
+ setIsEditing(true);
+ }}
+ data-test-id="edit-date-created"
+ />
+ </Flex>
+ {isEditing && (
+ <PopupPresenter
+ isOpen={isEditing}
+ onClose={() => {
+ setIsEditing(false);
+ setShowCalendar(false);
+ }}
+ position={{
+ isTargetAbsolute: true,
+ target: dateTextRef.current,
+ location: "below"
+ }}
+ >
+ <Flex
+ sx={{
+ bg: "background",
+ p: 3,
+ boxShadow: `0px 0px 25px 5px ${
+ theme === "dark" ? "#000000aa" : "#0000004e"
+ }`,
+ borderRadius: "dialog",
+ flexDirection: "column",
+ gap: 2,
+ width: 350
+ }}
+ data-test-id="edit-date-created-popup"
+ >
+ <Text variant="subtitle">{strings.editCreationDate()}</Text>
+ <Flex sx={{ gap: 2, flexDirection: "column" }}>
+ <Field
+ id="date-created"
+ label={strings.date()}
+ required
+ inputRef={dateInputRef}
+ data-test-id="date-created-input"
+ helpText={`${db.settings.getDateFormat()}`}
+ action={{
+ icon: Calendar,
+ onClick() {
+ setShowCalendar(!showCalendar);
+ }
+ }}
+ validate={(t) =>
+ dayjs(t, db.settings.getDateFormat(), true).isValid()
+ }
+ defaultValue={editedDate.format(db.settings.getDateFormat())}
+ onChange={(e) =>
+ setEditedDate((d) => setDateOnly(e.target.value, d))
+ }
+ />
+ <PopupPresenter
+ isOpen={showCalendar}
+ onClose={() => setShowCalendar(false)}
+ position={{
+ isTargetAbsolute: true,
+ target: dateInputRef.current,
+ location: "below"
+ }}
+ >
+ <DayPicker
+ sx={{
+ bg: "background",
+ p: 2,
+ boxShadow: `0px 0px 25px 5px ${
+ theme === "dark" ? "#000000aa" : "#0000004e"
+ }`,
+ borderRadius: "dialog",
+ width: 300
+ }}
+ selected={dayjs(editedDate).toDate()}
+ maxDate={new Date()}
+ onSelect={(day) => {
+ if (!day) return;
+ const date = getFormattedDate(day, "date");
+ setEditedDate((d) => setDateOnly(date, d));
+ if (dateInputRef.current) dateInputRef.current.value = date;
+ setShowCalendar(false);
+ }}
+ />
+ </PopupPresenter>
+ <Field
+ id="time-created"
+ label={strings.time()}
+ required
+ data-test-id="time-created-input"
+ helpText={`${
+ db.settings.getTimeFormat() === "12-hour"
+ ? "hh:mm AM/PM"
+ : "hh:mm"
+ }`}
+ validate={(t) => {
+ const format =
+ db.settings.getTimeFormat() === "12-hour"
+ ? "hh:mm a"
+ : "HH:mm";
+ return dayjs(t.toLowerCase(), format, true).isValid();
+ }}
+ defaultValue={editedDate.format(
+ getTimeFormat(db.settings.getTimeFormat())
+ )}
+ onChange={(e) =>
+ setEditedDate((d) => setTimeOnly(e.target.value, d))
+ }
+ />
+ </Flex>
+ <Flex sx={{ gap: 2, justifyContent: "flex-end" }}>
+ <Button
+ onClick={() => {
+ setIsEditing(false);
+ setShowCalendar(false);
+ }}
+ >
+ {strings.cancel()}
+ </Button>
+ <Button
+ variant="accentSecondary"
+ data-test-id="save-date-created"
+ onClick={async () => {
+ try {
+ if (editedDate.isAfter(dayjs())) {
+ showToast(
+ "error",
+ "Creation date cannot be in the future"
+ );
+ return;
+ }
+ if (dateEdited && editedDate.isAfter(dayjs(dateEdited))) {
+ showToast(
+ "error",
+ "Creation date cannot be after last edited date"
+ );
+ return;
+ }
+
+ await noteStore
+ .get()
+ .updateDateCreated(noteId, editedDate.valueOf());
+ setIsEditing(false);
+ setShowCalendar(false);
+ } catch (error) {
+ showToast(
+ "error",
+ "Failed to update creation date: " +
+ (error as Error).message
+ );
+ }
+ }}
+ >
+ {strings.save()}
+ </Button>
+ </Flex>
+ </Flex>
+ </PopupPresenter>
+ )}
+ </>
+ );
+}
diff --git a/apps/web/src/dialogs/add-reminder-dialog.tsx b/apps/web/src/dialogs/add-reminder-dialog.tsx
index 23c3c4e3c..5bc02fbde 100644
--- a/apps/web/src/dialogs/add-reminder-dialog.tsx
+++ b/apps/web/src/dialogs/add-reminder-dialog.tsx
@@ -38,6 +38,7 @@ import { Note, Reminder } from "@notesnook/core";
import { BaseDialogProps, DialogManager } from "../common/dialog-manager";
import { strings } from "@notesnook/intl";
import { checkFeature } from "../common";
+import { setTimeOnly, setDateOnly } from "../utils/date-time";
dayjs.extend(customParseFormat);
@@ -520,20 +521,10 @@ export const AddReminderDialog = DialogManager.register(
}
);
-function setTimeOnly(str: string, date: dayjs.Dayjs) {
- const value = dayjs(str, timeFormat(), true);
- return date.hour(value.hour()).minute(value.minute());
-}
-
function timeFormat() {
return getTimeFormat(db.settings.getTimeFormat());
}
-function setDateOnly(str: string, date: dayjs.Dayjs) {
- const value = dayjs(str, db.settings.getDateFormat(), true);
- return date.year(value.year()).month(value.month()).date(value.date());
-}
-
function getSelectedDaysText(
selectedDays: number[],
recurringMode: ValueOf<typeof RecurringModes>,
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 5173e0c01..0c126e780 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -405,6 +405,8 @@ class EditorStore extends BaseStore<EditorStore> {
event.item.favorite ?? session.note.favorite;
session.note.archived =
event.item.archived ?? session.note.archived;
+ session.note.dateCreated =
+ event.item.dateCreated ?? session.note.dateCreated;
session.note.dateEdited =
event.item.dateEdited ?? session.note.dateEdited;
});
diff --git a/apps/web/src/stores/note-store.ts b/apps/web/src/stores/note-store.ts
index fb57118aa..fe1fe0e32 100644
--- a/apps/web/src/stores/note-store.ts
+++ b/apps/web/src/stores/note-store.ts
@@ -121,6 +121,11 @@ class NoteStore extends BaseStore<NoteStore> {
await this.refresh();
};
+ updateDateCreated = async (id: string, dateCreated: number) => {
+ await db.notes.add({ id, dateCreated });
+ await this.refresh();
+ };
+
setColor = async (colorId: string, isChecked: boolean, ...ids: string[]) => {
await db.relations.to({ type: "note", ids }, "color").unlink();
if (!isChecked) {
diff --git a/apps/web/src/utils/date-time.ts b/apps/web/src/utils/date-time.ts
new file mode 100644
index 000000000..e85f1dba5
--- /dev/null
+++ b/apps/web/src/utils/date-time.ts
@@ -0,0 +1,32 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import dayjs from "dayjs";
+import { db } from "../common/db";
+import { getTimeFormat } from "@notesnook/core";
+
+export function setTimeOnly(str: string, date: dayjs.Dayjs) {
+ const value = dayjs(str, getTimeFormat(db.settings.getTimeFormat()), true);
+ return date.hour(value.hour()).minute(value.minute());
+}
+
+export function setDateOnly(str: string, date: dayjs.Dayjs) {
+ const value = dayjs(str, db.settings.getDateFormat(), true);
+ return date.year(value.year()).month(value.month()).date(value.date());
+}
diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts
index 7a5fdc51d..b492f470c 100644
--- a/packages/core/__tests__/notes.test.ts
+++ b/packages/core/__tests__/notes.test.ts
@@ -772,3 +772,11 @@ test("archived note shouldn't be in favorites", () =>
await db.notes.archive(true, id);
expect(await db.notes.favorites.count()).toBe(0);
}));
+
+test("edit note's created date", () =>
+ noteTest().then(async ({ db, id }) => {
+ const date = new Date("2020-01-01T10:00:00Z");
+ await db.notes.add({ id, dateCreated: date.valueOf() });
+ const note = await db.notes.note(id);
+ expect(note?.dateCreated).toBe(date.valueOf());
+ }));
diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts
index 0ff1090e9..89621cc65 100644
--- a/packages/core/src/collections/notes.ts
+++ b/packages/core/src/collections/notes.ts
@@ -178,6 +178,7 @@ export class Notes implements ICollection {
conflicted: item.conflicted,
readonly: item.readonly,
+ dateCreated: item.dateCreated,
dateEdited: item.dateEdited || dateEdited,
isGeneratedTitle: item.isGeneratedTitle
diff --git a/packages/intl/locale/en.po b/packages/intl/locale/en.po
index c235422d4..1919422eb 100644
--- a/packages/intl/locale/en.po
+++ b/packages/intl/locale/en.po
@@ -2386,6 +2386,10 @@ msgstr "Easy access"
msgid "Edit"
msgstr "Edit"
+#: src/strings.ts:2626
+msgid "Edit creation date"
+msgstr "Edit creation date"
+
#: src/strings.ts:527
msgid "Edit internal link"
msgstr "Edit internal link"
diff --git a/packages/intl/locale/pseudo-LOCALE.po b/packages/intl/locale/pseudo-LOCALE.po
index f1606a666..d9680b3df 100644
--- a/packages/intl/locale/pseudo-LOCALE.po
+++ b/packages/intl/locale/pseudo-LOCALE.po
@@ -2375,6 +2375,10 @@ msgstr ""
msgid "Edit"
msgstr ""
+#: src/strings.ts:2626
+msgid "Edit creation date"
+msgstr ""
+
#: src/strings.ts:527
msgid "Edit internal link"
msgstr ""
diff --git a/packages/intl/src/strings.ts b/packages/intl/src/strings.ts
index 860107803..320ada774 100644
--- a/packages/intl/src/strings.ts
+++ b/packages/intl/src/strings.ts
@@ -2622,5 +2622,6 @@ Use this if changes from other devices are not appearing on this device. This wi
t`Your free trial is on-going. Your subscription will start on ${trialExpiryDate}`,
weekFormat: () => t`Week format`,
weekFormatDesc: () =>
- t`Choose what day to display as the first day of the week`
+ t`Choose what day to display as the first day of the week`,
+ editCreationDate: () => t`Edit creation date`
};