|
|
@@ -1,13 +1,40 @@
|
|
|
-import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
|
|
+import { isServer } from '@growi/core/dist/utils';
|
|
|
+import { atom, useAtom } from 'jotai';
|
|
|
import { useCallback } from 'react';
|
|
|
import { useIsEditable, usePageNotFound } from '~/states/page';
|
|
|
+import { EditorMode, EditorModeHash, type UseEditorModeReturn } from './types';
|
|
|
+import { determineEditorModeByHash } from './utils';
|
|
|
|
|
|
-import {
|
|
|
- editingMarkdownAtom,
|
|
|
- editorModeAtom,
|
|
|
- selectedGrantAtom,
|
|
|
-} from './atoms';
|
|
|
-import { EditorMode, type UseEditorModeReturn } from './types';
|
|
|
+// Base atom for editor mode
|
|
|
+const editorModeBaseAtom = atom<EditorMode | null>(null);
|
|
|
+
|
|
|
+// Derived atom with initialization logic
|
|
|
+export const editorModeAtom = atom(
|
|
|
+ (get) => {
|
|
|
+ const baseMode = get(editorModeBaseAtom);
|
|
|
+
|
|
|
+ // If already initialized, return the current mode
|
|
|
+ if (baseMode !== null) {
|
|
|
+ return baseMode;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Initialize from hash on first access
|
|
|
+ return determineEditorModeByHash();
|
|
|
+ },
|
|
|
+ (_get, set, newMode: EditorMode) => {
|
|
|
+ // Update URL hash when mode changes (client-side only)
|
|
|
+ if (!isServer()) {
|
|
|
+ const { pathname, search } = window.location;
|
|
|
+ const hash =
|
|
|
+ newMode === EditorMode.Editor
|
|
|
+ ? EditorModeHash.Edit
|
|
|
+ : EditorModeHash.View;
|
|
|
+ window.history.replaceState(null, '', `${pathname}${search}${hash}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ set(editorModeBaseAtom, newMode);
|
|
|
+ },
|
|
|
+);
|
|
|
|
|
|
export const useEditorMode = (): UseEditorModeReturn => {
|
|
|
const isEditable = useIsEditable();
|
|
|
@@ -48,11 +75,10 @@ export const useEditorMode = (): UseEditorModeReturn => {
|
|
|
};
|
|
|
};
|
|
|
|
|
|
-export const useEditingMarkdown = () => useAtomValue(editingMarkdownAtom);
|
|
|
-export const useSetEditingMarkdown = () => useSetAtom(editingMarkdownAtom);
|
|
|
-
|
|
|
/**
|
|
|
- * Hook for managing selected grant in page editor
|
|
|
- * Used for temporary grant selection before applying to the page
|
|
|
+ * Internal atoms for derived atom usage (special naming convention)
|
|
|
+ * These atoms are exposed only for creating derived atoms in other modules
|
|
|
*/
|
|
|
-export const useSelectedGrant = () => useAtom(selectedGrantAtom);
|
|
|
+export const _atomsForDerivedAbilities = {
|
|
|
+ editorModeAtom,
|
|
|
+} as const;
|