Skip to content

Commit

Permalink
refactor(core): set edgeless theme on doc created (toeverything#8670)
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Nov 4, 2024
1 parent a4f27ef commit 12e3cf1
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 126 deletions.
3 changes: 0 additions & 3 deletions packages/common/infra/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export * from './modules/feature-flag';
export * from './modules/global-context';
export * from './modules/lifecycle';
export * from './modules/storage';
export * from './modules/theme';
export * from './modules/workspace';
export * from './orm';
export * from './storage';
Expand All @@ -27,7 +26,6 @@ import {
configureGlobalStorageModule,
configureTestingGlobalStorage,
} from './modules/storage';
import { configureAppThemeModule } from './modules/theme';
import {
configureTestingWorkspaceProvider,
configureWorkspaceModule,
Expand All @@ -41,7 +39,6 @@ export function configureInfraModules(framework: Framework) {
configureGlobalContextModule(framework);
configureLifecycleModule(framework);
configureFeatureFlagModule(framework);
configureAppThemeModule(framework);
}

export function configureTestingInfraModules(framework: Framework) {
Expand Down
4 changes: 4 additions & 0 deletions packages/common/infra/src/modules/doc/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createEvent } from '../../../framework';
import type { DocRecord } from '../entities/record';

export const DocCreated = createEvent<DocRecord>('DocCreated');
1 change: 1 addition & 0 deletions packages/common/infra/src/modules/doc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { Doc } from './entities/doc';
export { DocRecord } from './entities/record';
export { DocRecordList } from './entities/record-list';
export { DocCreated } from './events';
export { DocScope } from './scopes/doc';
export { DocService } from './services/doc';
export { DocsService } from './services/docs';
Expand Down
2 changes: 2 additions & 0 deletions packages/common/infra/src/modules/doc/services/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ObjectPool } from '../../../utils';
import type { Doc } from '../entities/doc';
import { DocPropertyList } from '../entities/property-list';
import { DocRecordList } from '../entities/record-list';
import { DocCreated } from '../events';
import { DocScope } from '../scopes/doc';
import type { DocsStore } from '../stores/docs';
import { DocService } from './doc';
Expand Down Expand Up @@ -74,6 +75,7 @@ export class DocsService extends Service {
if (options.primaryMode) {
docRecord.setPrimaryMode(options.primaryMode);
}
this.eventBus.emit(DocCreated, docRecord);
return docRecord;
}

Expand Down
9 changes: 0 additions & 9 deletions packages/common/infra/src/modules/theme/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/frontend/apps/android/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AffineContext } from '@affine/component/context';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { AffineContext } from '@affine/core/components/context';
import { Telemetry } from '@affine/core/components/telemetry';
import { configureMobileModules } from '@affine/core/mobile/modules';
import { router } from '@affine/core/mobile/router';
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/electron/renderer/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AffineContext } from '@affine/component/context';
import { GlobalLoading } from '@affine/component/global-loading';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { AffineContext } from '@affine/core/components/context';
import { WindowsAppControls } from '@affine/core/components/pure/header/windows-app-controls';
import { Telemetry } from '@affine/core/components/telemetry';
import { router } from '@affine/core/desktop/router';
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/apps/electron/renderer/shell/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ThemeProvider } from '@affine/component/theme-provider';
import { ShellAppFallback } from '@affine/core/components/affine/app-container';
import { useAppSettingHelper } from '@affine/core/components/hooks/affine/use-app-setting-helper';
import { ThemeProvider } from '@affine/core/components/theme-provider';
import { configureAppSidebarModule } from '@affine/core/modules/app-sidebar';
import {
AppTabsHeader,
Expand All @@ -9,9 +9,9 @@ import {
import { configureDesktopApiModule } from '@affine/core/modules/desktop-api';
import { configureI18nModule, I18nProvider } from '@affine/core/modules/i18n';
import { configureElectronStateStorageImpls } from '@affine/core/modules/storage';
import { configureAppThemeModule } from '@affine/core/modules/theme';
import { SplitViewFallback } from '@affine/core/modules/workbench/view/split-view/split-view';
import {
configureAppThemeModule,
configureGlobalStorageModule,
Framework,
FrameworkRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/ios/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AffineContext } from '@affine/component/context';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { AffineContext } from '@affine/core/components/context';
import { Telemetry } from '@affine/core/components/telemetry';
import { configureMobileModules } from '@affine/core/mobile/modules';
import { router } from '@affine/core/mobile/router';
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/mobile/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AffineContext } from '@affine/component/context';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { AffineContext } from '@affine/core/components/context';
import { Telemetry } from '@affine/core/components/telemetry';
import { configureMobileModules } from '@affine/core/mobile/modules';
import { router } from '@affine/core/mobile/router';
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/web/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AffineContext } from '@affine/component/context';
import { GlobalLoading } from '@affine/component/global-loading';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { AffineContext } from '@affine/core/components/context';
import { Telemetry } from '@affine/core/components/telemetry';
import { router } from '@affine/core/desktop/router';
import { configureCommonModules } from '@affine/core/modules';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AIPageRootBlockSpec,
} from '@affine/core/blocksuite/presets/ai';
import { EditorSettingService } from '@affine/core/modules/editor-setting';
import { AppThemeService } from '@affine/core/modules/theme';
import { mixpanel } from '@affine/track';
import {
ConfigExtension,
Expand Down Expand Up @@ -32,7 +33,6 @@ import {
} from '@blocksuite/affine-shared/utils';
import type { Container } from '@blocksuite/global/di';
import {
AppThemeService,
DocService,
DocsService,
type FrameworkProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ import type { WeekDatePickerHandle } from '@affine/component';
import { WeekDatePicker } from '@affine/component';
import { useJournalRouteHelper } from '@affine/core/components/hooks/use-journal';
import { JournalService } from '@affine/core/modules/journal';
import type { Doc, DocCollection } from '@blocksuite/affine/store';
import type { Doc } from '@blocksuite/affine/store';
import { useLiveData, useService } from '@toeverything/infra';
import dayjs from 'dayjs';
import { useEffect, useRef, useState } from 'react';

export interface JournalWeekDatePickerProps {
docCollection: DocCollection;
page: Doc;
}

const weekStyle = { maxWidth: 800, width: '100%' };
export const JournalWeekDatePicker = ({
docCollection,
page,
}: JournalWeekDatePickerProps) => {
export const JournalWeekDatePicker = ({ page }: JournalWeekDatePickerProps) => {
const handleRef = useRef<WeekDatePickerHandle>(null);
const journalService = useService(JournalService);
const journalDateStr = useLiveData(journalService.journalDate$(page.id));
const journalDate = journalDateStr ? dayjs(journalDateStr) : null;
const { openJournal } = useJournalRouteHelper(docCollection);
const { openJournal } = useJournalRouteHelper();
const [date, setDate] = useState(
(journalDate ?? dayjs()).format('YYYY-MM-DD')
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { Button } from '@affine/component';
import { useJournalRouteHelper } from '@affine/core/components/hooks/use-journal';
import { useI18n } from '@affine/i18n';
import type { DocCollection } from '@blocksuite/affine/store';
import { useCallback } from 'react';

export interface JournalTodayButtonProps {
docCollection: DocCollection;
}

export const JournalTodayButton = ({
docCollection,
}: JournalTodayButtonProps) => {
export const JournalTodayButton = () => {
const t = useI18n();
const journalHelper = useJournalRouteHelper(docCollection);
const journalHelper = useJournalRouteHelper();

const onToday = useCallback(() => {
journalHelper.openToday();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import { WorkbenchService } from '@affine/core/modules/workbench';
import { type DocMode } from '@blocksuite/affine/blocks';
import type { DocCollection } from '@blocksuite/affine/store';
import { type DocProps, DocsService, useServices } from '@toeverything/infra';
import { useTheme } from 'next-themes';
import { useCallback, useMemo } from 'react';

import { getValueByDefaultTheme } from '../../hooks/use-journal';

export const usePageHelper = (docCollection: DocCollection) => {
const {
docsService,
Expand All @@ -25,7 +22,6 @@ export const usePageHelper = (docCollection: DocCollection) => {
const workbench = workbenchService.workbench;
const docRecordList = docsService.list;
const appSidebar = appSidebarService.sidebar;
const { resolvedTheme } = useTheme();

const createPageAndOpen = useCallback(
(mode?: DocMode, open?: boolean | 'new-tab') => {
Expand All @@ -35,14 +31,6 @@ export const usePageHelper = (docCollection: DocCollection) => {
};
const page = docsService.createDoc({ docProps });

const value = getValueByDefaultTheme(
editorSettingService.editorSetting.settings$.value.edgelessDefaultTheme,
resolvedTheme || 'light'
);
docRecordList
.doc$(page.id)
.value?.setProperty('edgelessColorTheme', value);

if (mode) {
docRecordList.doc$(page.id).value?.setPrimaryMode(mode);
}
Expand All @@ -58,7 +46,6 @@ export const usePageHelper = (docCollection: DocCollection) => {
docRecordList,
docsService,
editorSettingService.editorSetting,
resolvedTheme,
workbench,
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ConfirmModalProvider } from '@affine/component';
import { ProviderComposer } from '@affine/component/provider-composer';
import { ThemeProvider } from '@affine/component/theme-provider';
import { ThemeProvider } from '@affine/core/components/theme-provider';
import type { createStore } from 'jotai';
import { Provider } from 'jotai';
import type { PropsWithChildren } from 'react';
import { useMemo } from 'react';

import { ConfirmModalProvider } from '../../ui/modal';

export type AffineContextProps = PropsWithChildren<{
store?: ReturnType<typeof createStore>;
}>;
Expand Down
60 changes: 13 additions & 47 deletions packages/frontend/core/src/components/hooks/use-journal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { EditorSettingService } from '@affine/core/modules/editor-setting';
import type { EdgelessDefaultTheme } from '@affine/core/modules/editor-setting/schema';
import { JournalService } from '@affine/core/modules/journal';
import { i18nTime } from '@affine/i18n';
import { track } from '@affine/track';
import { type DocCollection, Text } from '@blocksuite/affine/store';
import { Text } from '@blocksuite/affine/store';
import {
type DocProps,
DocsService,
Expand All @@ -12,29 +11,9 @@ import {
useServices,
} from '@toeverything/infra';
import dayjs from 'dayjs';
import { useTheme } from 'next-themes';
import { useCallback, useMemo } from 'react';

import { WorkbenchService } from '../../modules/workbench';
import { useDocCollectionHelper } from './use-block-suite-workspace-helper';

export const getValueByDefaultTheme = (
defaultTheme: EdgelessDefaultTheme,
currentAppTheme: string
) => {
switch (defaultTheme) {
case 'dark':
return 'dark';
case 'light':
return 'light';
case 'specified':
return currentAppTheme === 'dark' ? 'dark' : 'light';
case 'auto':
return 'system';
default:
return 'system';
}
};

type MaybeDate = Date | string | number;
export const JOURNAL_DATE_FORMAT = 'YYYY-MM-DD';
Expand All @@ -53,9 +32,7 @@ function toDayjs(j?: string | false) {
/**
* @deprecated use `JournalService` directly
*/
export const useJournalHelper = (docCollection: DocCollection) => {
const bsWorkspaceHelper = useDocCollectionHelper(docCollection);
const { resolvedTheme } = useTheme();
export const useJournalHelper = () => {
const { docsService, editorSettingService, journalService } = useServices({
DocsService,
EditorSettingService,
Expand All @@ -69,17 +46,11 @@ export const useJournalHelper = (docCollection: DocCollection) => {
(maybeDate: MaybeDate) => {
const day = dayjs(maybeDate);
const title = day.format(JOURNAL_DATE_FORMAT);
const page = bsWorkspaceHelper.createDoc();
const value = getValueByDefaultTheme(
editorSettingService.editorSetting.settings$.value.edgelessDefaultTheme,
resolvedTheme || 'light'
);
docsService.list
.doc$(page.id)
.value?.setProperty('edgelessColorTheme', value);
docsService.list.setPrimaryMode(page.id, 'page');
const docRecord = docsService.createDoc();
const { doc, release } = docsService.open(docRecord.id);
docsService.list.setPrimaryMode(docRecord.id, 'page');
// set created date to match the journal date
page.collection.setDocMeta(page.id, {
docRecord.setMeta({
createDate: dayjs()
.set('year', day.year())
.set('month', day.month())
Expand All @@ -91,17 +62,12 @@ export const useJournalHelper = (docCollection: DocCollection) => {
page: { title: new Text(title) },
note: editorSettingService.editorSetting.get('affine:note'),
};
initDocFromProps(page, docProps);
journalService.setJournalDate(page.id, title);
return page;
initDocFromProps(doc.blockSuiteDoc, docProps);
release();
journalService.setJournalDate(docRecord.id, title);
return docRecord;
},
[
bsWorkspaceHelper,
editorSettingService.editorSetting,
resolvedTheme,
docsService.list,
journalService,
]
[docsService, editorSettingService.editorSetting, journalService]
);

/**
Expand Down Expand Up @@ -138,8 +104,8 @@ export const useJournalHelper = (docCollection: DocCollection) => {
};

// split useJournalRouteHelper since it requires a <Route /> context, which may not work in lit
export const useJournalRouteHelper = (docCollection: DocCollection) => {
const { getJournalByDate } = useJournalHelper(docCollection);
export const useJournalRouteHelper = () => {
const { getJournalByDate } = useJournalHelper();
const workbench = useService(WorkbenchService).workbench;
/**
* open journal by date, create one if not exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ export const RootAppSidebar = (): ReactElement => {
{t['com.affine.workspaceSubPath.all']()}
</span>
</MenuLinkItem>
<AppSidebarJournalButton
docCollection={currentWorkspace.docCollection}
/>
<AppSidebarJournalButton />
<MenuItem
data-testid="slider-bar-workspace-setting-button"
icon={<SettingsIcon />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ import { DocDisplayMetaService } from '@affine/core/modules/doc-display-meta';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { isNewTabTrigger } from '@affine/core/utils';
import { useI18n } from '@affine/i18n';
import type { DocCollection } from '@blocksuite/affine/store';
import { TodayIcon } from '@blocksuite/icons/rc';
import { useLiveData, useService } from '@toeverything/infra';
import { type MouseEvent } from 'react';

interface AppSidebarJournalButtonProps {
docCollection: DocCollection;
}

export const AppSidebarJournalButton = ({
docCollection,
}: AppSidebarJournalButtonProps) => {
export const AppSidebarJournalButton = () => {
const t = useI18n();
const docDisplayMetaService = useService(DocDisplayMetaService);
const workbench = useService(WorkbenchService).workbench;
const location = useLiveData(workbench.location$);
const { openToday } = useJournalRouteHelper(docCollection);
const { openToday } = useJournalRouteHelper();
const maybeDocId = location.pathname.split('/')[1];
const { isJournal } = useJournalInfoHelper(maybeDocId);

Expand Down
Loading

0 comments on commit 12e3cf1

Please sign in to comment.