Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat(view): support save current view

lawvs98688592ab25
diff --git a/libs/components/editor-core/src/recast-block/types/constant.ts b/libs/components/editor-core/src/recast-block/types/constant.ts
index 386b21098e..a18f21035d 100644
--- a/libs/components/editor-core/src/recast-block/types/constant.ts
+++ b/libs/components/editor-core/src/recast-block/types/constant.ts
@@ -7,3 +7,4 @@ export const TABLE_VALUES_KEY = 'recastValues' as const;
*/
export const KANBAN_PROPERTIES_KEY = 'kanbanProps' as const;
export const META_VIEWS_KEY = 'recastViews' as const;
+export const META_CURRENT_VIEW_ID_KEY = 'recastCurrentViewId' as const;
diff --git a/libs/components/editor-core/src/recast-block/types/recast-property.ts b/libs/components/editor-core/src/recast-block/types/recast-property.ts
index ec4cc0fc5c..172c605b9e 100644
--- a/libs/components/editor-core/src/recast-block/types/recast-property.ts
+++ b/libs/components/editor-core/src/recast-block/types/recast-property.ts
@@ -1,10 +1,11 @@
-import { CSSProperties, ReactNode } from 'react';
+import { CSSProperties } from 'react';
import {
KANBAN_PROPERTIES_KEY,
+ META_CURRENT_VIEW_ID_KEY,
META_PROPERTIES_KEY,
META_VIEWS_KEY,
} from './constant';
-import { RecastScene, RecastView } from './view';
+import { RecastScene, RecastView, RecastViewId } from './view';
// ---------------------------------------------------
// Property
@@ -117,5 +118,6 @@ export type RecastDataProperties = Partial<{
scene: RecastScene;
[META_PROPERTIES_KEY]: RecastMetaProperty[];
[META_VIEWS_KEY]: RecastView[];
+ [META_CURRENT_VIEW_ID_KEY]: RecastViewId;
[KANBAN_PROPERTIES_KEY]: RecastKanbanProperty;
}>;
diff --git a/libs/components/editor-core/src/recast-block/view.ts b/libs/components/editor-core/src/recast-block/view.ts
index b07a81eb2c..27a4ce79be 100644
--- a/libs/components/editor-core/src/recast-block/view.ts
+++ b/libs/components/editor-core/src/recast-block/view.ts
@@ -1,8 +1,11 @@
import { nanoid } from 'nanoid';
-import { useCallback, useState } from 'react';
+import { useCallback } from 'react';
import { useRecastBlock } from './Context';
import {
+ KanbanView,
+ META_CURRENT_VIEW_ID_KEY,
META_VIEWS_KEY,
+ RecastPropertyId,
RecastScene,
RecastView,
RecastViewId,
@@ -27,13 +30,32 @@ const DEFAULT_VIEWS: RecastView[] = [
},
];
-export const useRecastView = () => {
+/**
+ * Get the current view of the group
+ */
+export const useCurrentView = () => {
const recastBlock = useRecastBlock();
const recastViews =
recastBlock.getProperty(META_VIEWS_KEY) ?? DEFAULT_VIEWS;
- // TODO save cur view
- const [currentView, changeView] = useState(recastViews[0]);
+ const currentViewId = recastBlock.getProperty(META_CURRENT_VIEW_ID_KEY);
+ const currentView =
+ recastViews.find(v => v.id === currentViewId) ?? recastViews[0];
+
+ const setCurrentView = useCallback(
+ async (newView: RecastView) => {
+ await recastBlock.setProperty(META_CURRENT_VIEW_ID_KEY, newView.id);
+ },
+ [recastBlock]
+ );
+ return [currentView, setCurrentView] as const;
+};
+
+export const useRecastView = () => {
+ const recastBlock = useRecastBlock();
+ const recastViews =
+ recastBlock.getProperty(META_VIEWS_KEY) ?? DEFAULT_VIEWS;
+ const [currentView, setCurrentView] = useCurrentView();
const getView = useCallback(
(id: RecastViewId) => {
@@ -118,7 +140,7 @@ export const useRecastView = () => {
return {
currentView,
recastViews,
- changeView,
+ setCurrentView,
addView,
updateView,
renameView,