-
-
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): template-doc settings for user-worksapce scope
- Loading branch information
Showing
12 changed files
with
239 additions
and
46 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
19 changes: 19 additions & 0 deletions
19
packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/template.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,19 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const menuItem = style({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: 8, | ||
}); | ||
export const menuItemIcon = style({ | ||
fontSize: 24, | ||
lineHeight: 0, | ||
}); | ||
export const menuItemText = style({ | ||
fontSize: 14, | ||
width: 0, | ||
flex: 1, | ||
textOverflow: 'ellipsis', | ||
whiteSpace: 'nowrap', | ||
overflow: 'hidden', | ||
}); |
110 changes: 110 additions & 0 deletions
110
packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/template.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,110 @@ | ||
import { Button, Menu, MenuItem } from '@affine/component'; | ||
import { type DocRecord, DocsService } from '@affine/core/modules/doc'; | ||
import { DocDisplayMetaService } from '@affine/core/modules/doc-display-meta'; | ||
import { FeatureFlagService } from '@affine/core/modules/feature-flag'; | ||
import { TemplateDocService } from '@affine/core/modules/template-doc'; | ||
import { useLiveData, useService, useServices } from '@toeverything/infra'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
import * as styles from './template.css'; | ||
|
||
export const TemplateDocSetting = () => { | ||
const { featureFlagService, templateDocService } = useServices({ | ||
FeatureFlagService, | ||
TemplateDocService, | ||
}); | ||
const setting = templateDocService.setting; | ||
|
||
const enabled = useLiveData(featureFlagService.flags.enable_template_doc.$); | ||
const loading = useLiveData(setting.loading$); | ||
const pageTemplateDocId = useLiveData(setting.pageTemplateDocId$); | ||
const journalTemplateDocId = useLiveData(setting.journalTemplateDocId$); | ||
|
||
const updatePageTemplate = useCallback( | ||
(id?: string) => { | ||
setting.updatePageTemplateDocId(id); | ||
}, | ||
[setting] | ||
); | ||
|
||
const updateJournalTemplate = useCallback( | ||
(id?: string) => { | ||
setting.updateJournalTemplateDocId(id); | ||
}, | ||
[setting] | ||
); | ||
|
||
if (!enabled) return null; | ||
if (loading) return null; | ||
|
||
return ( | ||
<div> | ||
Normal Page: | ||
<TemplateSelector | ||
current={pageTemplateDocId} | ||
onChange={updatePageTemplate} | ||
/> | ||
<br /> | ||
Journal: | ||
<TemplateSelector | ||
current={journalTemplateDocId} | ||
onChange={updateJournalTemplate} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
interface TemplateSelectorProps { | ||
current?: string; | ||
onChange?: (id?: string) => void; | ||
} | ||
const TemplateSelector = ({ current, onChange }: TemplateSelectorProps) => { | ||
const docsService = useService(DocsService); | ||
const doc = useLiveData(current ? docsService.list.doc$(current) : null); | ||
const isInTrash = useLiveData(doc?.trash$); | ||
|
||
return ( | ||
<Menu items={<List onChange={onChange} />}> | ||
<Button>{isInTrash ? 'Doc is removed' : (doc?.id ?? 'Unset')}</Button> | ||
</Menu> | ||
); | ||
}; | ||
|
||
const List = ({ onChange }: { onChange?: (id?: string) => void }) => { | ||
const list = useService(TemplateDocService).list; | ||
const [docs] = useState(list.getTemplateDocs()); | ||
|
||
const handleClick = useCallback( | ||
(id: string) => { | ||
onChange?.(id); | ||
}, | ||
[onChange] | ||
); | ||
|
||
return docs.map(doc => { | ||
return <DocItem key={doc.id} doc={doc} onClick={handleClick} />; | ||
}); | ||
}; | ||
|
||
interface DocItemProps { | ||
doc: DocRecord; | ||
onClick?: (id: string) => void; | ||
} | ||
const DocItem = ({ doc, onClick }: DocItemProps) => { | ||
const docDisplayService = useService(DocDisplayMetaService); | ||
const Icon = useLiveData(docDisplayService.icon$(doc.id)); | ||
const title = useLiveData(docDisplayService.title$(doc.id)); | ||
|
||
const handleClick = useCallback(() => { | ||
onClick?.(doc.id); | ||
}, [doc.id, onClick]); | ||
|
||
return ( | ||
<MenuItem onClick={handleClick}> | ||
<li className={styles.menuItem}> | ||
<Icon className={styles.menuItemIcon} /> | ||
<span className={styles.menuItemText}>{title}</span> | ||
</li> | ||
</MenuItem> | ||
); | ||
}; |
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
20 changes: 15 additions & 5 deletions
20
packages/frontend/core/src/modules/template-doc/entities/setting.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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
import { Entity } from '@toeverything/infra'; | ||
|
||
export type TemplateDocSettings = { | ||
templateId?: string; | ||
journalTemplateId?: string; | ||
}; | ||
import type { TemplateDocSettingStore } from '../store/setting'; | ||
|
||
export class TemplateDocSetting extends Entity { | ||
constructor() { | ||
constructor(private readonly store: TemplateDocSettingStore) { | ||
super(); | ||
} | ||
|
||
loading$ = this.store.watchIsLoading(); | ||
setting$ = this.store.watchSetting(); | ||
pageTemplateDocId$ = this.store.watchSettingKey('pageTemplateId'); | ||
journalTemplateDocId$ = this.store.watchSettingKey('journalTemplateId'); | ||
|
||
updatePageTemplateDocId(id?: string) { | ||
this.store.updateSetting('pageTemplateId', id); | ||
} | ||
|
||
updateJournalTemplateDocId(id?: string) { | ||
this.store.updateSetting('journalTemplateId', id); | ||
} | ||
} |
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.