-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): new "is journal" page property (#8525)
- Loading branch information
Showing
19 changed files
with
494 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 14 additions & 7 deletions
21
packages/frontend/core/src/components/blocksuite/block-suite-editor/journal-doc-title.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
import { useJournalInfoHelper } from '@affine/core/components/hooks/use-journal'; | ||
import { useI18n } from '@affine/i18n'; | ||
import { JournalService } from '@affine/core/modules/journal'; | ||
import { i18nTime, useI18n } from '@affine/i18n'; | ||
import type { Doc } from '@blocksuite/affine/store'; | ||
import { useLiveData, useService } from '@toeverything/infra'; | ||
import dayjs from 'dayjs'; | ||
|
||
import * as styles from './styles.css'; | ||
|
||
export const BlocksuiteEditorJournalDocTitle = ({ page }: { page: Doc }) => { | ||
const { localizedJournalDate, isTodayJournal, journalDate } = | ||
useJournalInfoHelper(page.id); | ||
const journalService = useService(JournalService); | ||
const journalDateStr = useLiveData(journalService.journalDate$(page.id)); | ||
const journalDate = journalDateStr ? dayjs(journalDateStr) : null; | ||
const isTodayJournal = useLiveData(journalService.journalToday$(page.id)); | ||
const localizedJournalDate = i18nTime(journalDateStr, { | ||
absolute: { accuracy: 'day' }, | ||
}); | ||
const t = useI18n(); | ||
|
||
// TODO(catsjuice): i18n | ||
const day = journalDate?.format('dddd') ?? null; | ||
|
||
return ( | ||
<span className="doc-title-container"> | ||
<span>{localizedJournalDate}</span> | ||
<div className="doc-title-container" data-testid="journal-title"> | ||
<span data-testid="date">{localizedJournalDate}</span> | ||
{isTodayJournal ? ( | ||
<span className={styles.titleTodayTag}>{t['com.affine.today']()}</span> | ||
) : ( | ||
<span className={styles.titleDayTag}>{day}</span> | ||
)} | ||
</span> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/frontend/core/src/components/doc-properties/types/journal.css.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { cssVar } from '@toeverything/theme'; | ||
import { cssVarV2 } from '@toeverything/theme/v2'; | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const property = style({ | ||
padding: 4, | ||
}); | ||
|
||
export const root = style({ | ||
height: '100%', | ||
display: 'flex', | ||
gap: 2, | ||
alignItems: 'center', | ||
}); | ||
|
||
export const checkbox = style({ | ||
fontSize: 24, | ||
color: cssVarV2('icon/primary'), | ||
}); | ||
|
||
export const date = style({ | ||
fontSize: cssVar('fontSm'), | ||
color: cssVarV2('text/primary'), | ||
lineHeight: '22px', | ||
padding: '0 4px', | ||
borderRadius: 4, | ||
':hover': { | ||
background: cssVarV2('layer/background/hoverOverlay'), | ||
}, | ||
}); | ||
|
||
export const duplicateTag = style({ | ||
padding: '0 8px', | ||
border: `1px solid ${cssVarV2('database/border')}`, | ||
background: cssVarV2('layer/background/error'), | ||
color: cssVarV2('toast/iconState/error'), | ||
borderRadius: 4, | ||
}); |
134 changes: 134 additions & 0 deletions
134
packages/frontend/core/src/components/doc-properties/types/journal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { Checkbox, DatePicker, Menu, PropertyValue } from '@affine/component'; | ||
import { JournalService } from '@affine/core/modules/journal'; | ||
import { WorkbenchService } from '@affine/core/modules/workbench'; | ||
import { ViewService } from '@affine/core/modules/workbench/services/view'; | ||
import { i18nTime, useI18n } from '@affine/i18n'; | ||
import { | ||
DocService, | ||
useLiveData, | ||
useService, | ||
useServiceOptional, | ||
} from '@toeverything/infra'; | ||
import dayjs from 'dayjs'; | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
|
||
import * as styles from './journal.css'; | ||
|
||
export const JournalValue = () => { | ||
const t = useI18n(); | ||
|
||
const journalService = useService(JournalService); | ||
const doc = useService(DocService).doc; | ||
const journalDate = useLiveData(journalService.journalDate$(doc.id)); | ||
const checked = !!journalDate; | ||
|
||
const [selectedDate, setSelectedDate] = useState( | ||
dayjs().format('YYYY-MM-DD') | ||
); | ||
const [showDatePicker, setShowDatePicker] = useState(false); | ||
const displayDate = useMemo( | ||
() => | ||
i18nTime(selectedDate, { | ||
absolute: { accuracy: 'day' }, | ||
}), | ||
[selectedDate] | ||
); | ||
const docs = useLiveData( | ||
useMemo( | ||
() => journalService.journalsByDate$(selectedDate), | ||
[journalService, selectedDate] | ||
) | ||
); | ||
const conflict = docs.length > 1; | ||
|
||
useEffect(() => { | ||
if (journalDate) setSelectedDate(journalDate); | ||
}, [journalDate]); | ||
|
||
const handleDateSelect = useCallback( | ||
(day: string) => { | ||
const date = dayjs(day).format('YYYY-MM-DD'); | ||
setSelectedDate(date); | ||
journalService.setJournalDate(doc.id, date); | ||
}, | ||
[journalService, doc.id] | ||
); | ||
|
||
const handleCheck = useCallback( | ||
(_: unknown, v: boolean) => { | ||
if (!v) { | ||
journalService.removeJournalDate(doc.id); | ||
} else { | ||
handleDateSelect(selectedDate); | ||
} | ||
}, | ||
[handleDateSelect, journalService, doc.id, selectedDate] | ||
); | ||
|
||
const workbench = useService(WorkbenchService).workbench; | ||
const activeView = useLiveData(workbench.activeView$); | ||
const view = useServiceOptional(ViewService)?.view ?? activeView; | ||
|
||
const handleOpenDuplicate = useCallback( | ||
(e: React.MouseEvent) => { | ||
e.stopPropagation(); | ||
workbench.openSidebar(); | ||
view.activeSidebarTab('journal'); | ||
}, | ||
[view, workbench] | ||
); | ||
|
||
const toggle = useCallback(() => { | ||
handleCheck(null, !checked); | ||
}, [checked, handleCheck]); | ||
|
||
return ( | ||
<PropertyValue className={styles.property} onClick={toggle}> | ||
<div className={styles.root}> | ||
<Checkbox | ||
className={styles.checkbox} | ||
checked={checked} | ||
onChange={handleCheck} | ||
/> | ||
{checked ? ( | ||
<Menu | ||
contentOptions={{ | ||
onClick: e => e.stopPropagation(), | ||
sideOffset: 10, | ||
alignOffset: -30, | ||
style: { padding: 20 }, | ||
}} | ||
rootOptions={{ | ||
modal: true, | ||
open: showDatePicker, | ||
onOpenChange: setShowDatePicker, | ||
}} | ||
items={ | ||
<DatePicker | ||
weekDays={t['com.affine.calendar-date-picker.week-days']()} | ||
monthNames={t['com.affine.calendar-date-picker.month-names']()} | ||
todayLabel={t['com.affine.calendar-date-picker.today']()} | ||
value={selectedDate} | ||
onChange={handleDateSelect} | ||
/> | ||
} | ||
> | ||
<div data-testid="date-selector" className={styles.date}> | ||
{displayDate} | ||
</div> | ||
</Menu> | ||
) : null} | ||
|
||
{checked && conflict ? ( | ||
<div | ||
data-testid="conflict-tag" | ||
className={styles.duplicateTag} | ||
onClick={handleOpenDuplicate} | ||
> | ||
{t['com.affine.page-properties.property.journal-duplicated']()} | ||
</div> | ||
) : null} | ||
</div> | ||
</PropertyValue> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.