refactor(page): rename pageId

This commit is contained in:
tzhangchi 2022-09-08 19:20:37 +08:00
parent 9442c023e5
commit 281c3f6c44
17 changed files with 58 additions and 61 deletions

View File

@ -14,7 +14,7 @@ type EdgelessProps = {
}; };
export const Edgeless = (props: EdgelessProps) => { export const Edgeless = (props: EdgelessProps) => {
const { page_id } = useParams(); const { pageId } = useParams();
const { user } = useUserAndSpaces(); const { user } = useUserAndSpaces();
useEffect(() => { useEffect(() => {
@ -31,7 +31,5 @@ export const Edgeless = (props: EdgelessProps) => {
update_recent_pages(); update_recent_pages();
}, [user, props.workspace]); }, [user, props.workspace]);
return ( return <MemoAffineBoard workspace={props.workspace} rootBlockId={pageId} />;
<MemoAffineBoard workspace={props.workspace} rootBlockId={page_id} />
);
}; };

View File

@ -15,11 +15,11 @@ export function WorkspaceContainer() {
<Route path="/" element={<WorkspaceRootContainer />}> <Route path="/" element={<WorkspaceRootContainer />}>
<Route path="/pages" element={<Pages />} /> <Route path="/pages" element={<Pages />} />
<Route <Route
path="/:page_id/edgeless" path="/:pageId/edgeless"
element={<Edgeless workspace={workspaceId} />} element={<Edgeless workspace={workspaceId} />}
/> />
<Route <Route
path="/:page_id" path="/:pageId"
element={<Page workspace={workspaceId} />} element={<Page workspace={workspaceId} />}
/> />
<Route path="/" element={<WorkspaceHome />} /> <Route path="/" element={<WorkspaceHome />} />

View File

@ -34,7 +34,7 @@ export function Page(props: PageProps) {
const [activeTab, setActiveTab] = useState( const [activeTab, setActiveTab] = useState(
TabMap.get(TAB_TITLE.PAGES).value TabMap.get(TAB_TITLE.PAGES).value
); );
const { page_id } = useParams(); const { pageId } = useParams();
const { showSpaceSidebar, fixedDisplay, setSpaceSidebarVisible } = const { showSpaceSidebar, fixedDisplay, setSpaceSidebarVisible } =
useShowSpaceSidebar(); useShowSpaceSidebar();
const dailyNotesFlag = useFlag('BooleanDailyNotes', false); const dailyNotesFlag = useFlag('BooleanDailyNotes', false);
@ -79,7 +79,7 @@ export function Page(props: PageProps) {
</div> </div>
<div> <div>
<CollapsiblePageTree title="PAGES"> <CollapsiblePageTree title="PAGES">
{page_id ? <PageTree /> : null} {pageId ? <PageTree /> : null}
</CollapsiblePageTree> </CollapsiblePageTree>
</div> </div>
</div> </div>
@ -91,7 +91,7 @@ export function Page(props: PageProps) {
</WorkspaceSidebarContent> </WorkspaceSidebarContent>
</WorkspaceSidebar> </WorkspaceSidebar>
</LigoLeftContainer> </LigoLeftContainer>
<EditorContainer workspace={props.workspace} pageId={page_id} /> <EditorContainer workspace={props.workspace} pageId={pageId} />
</LigoApp> </LigoApp>
); );
} }

View File

@ -38,7 +38,7 @@ export type CollapsiblePageTreeProps = {
export function CollapsiblePageTree(props: CollapsiblePageTreeProps) { export function CollapsiblePageTree(props: CollapsiblePageTreeProps) {
const { className, style, children, title, initialOpen = true } = props; const { className, style, children, title, initialOpen = true } = props;
const navigate = useNavigate(); const navigate = useNavigate();
const { workspaceId, page_id } = useParams(); const { workspaceId, pageId } = useParams();
const { handleAddPage } = usePageTree(); const { handleAddPage } = usePageTree();
const { addPageToday } = useCalendarHeatmap(); const { addPageToday } = useCalendarHeatmap();
@ -46,7 +46,7 @@ export function CollapsiblePageTree(props: CollapsiblePageTreeProps) {
const [open, setOpen] = useState(initialOpen); const [open, setOpen] = useState(initialOpen);
const create_page = useCallback(async () => { const create_page = useCallback(async () => {
if (page_id) { if (pageId) {
const newPage = await services.api.editorBlock.create({ const newPage = await services.api.editorBlock.create({
workspace: workspaceId, workspace: workspaceId,
type: 'page' as const, type: 'page' as const,
@ -57,7 +57,7 @@ export function CollapsiblePageTree(props: CollapsiblePageTreeProps) {
navigate(`/${workspaceId}/${newPage.id}`); navigate(`/${workspaceId}/${newPage.id}`);
} }
}, [addPageToday, handleAddPage, navigate, page_id, workspaceId]); }, [addPageToday, handleAddPage, navigate, pageId, workspaceId]);
const [newPageBtnVisible, setNewPageBtnVisible] = useState<boolean>(false); const [newPageBtnVisible, setNewPageBtnVisible] = useState<boolean>(false);

View File

@ -116,7 +116,7 @@ const renderTOCContent = tocDataSource => {
}; };
export const TOC = () => { export const TOC = () => {
const { page_id } = useParams(); const { pageId } = useParams();
const [tocDataSource, setTocDataSource] = useState<TOCType[]>([]); const [tocDataSource, setTocDataSource] = useState<TOCType[]>([]);
const [activeBlockId, setActiveBlockId] = useState(''); const [activeBlockId, setActiveBlockId] = useState('');
@ -124,7 +124,7 @@ export const TOC = () => {
const listenerMapRef = useRef<ListenerMap>(new Map()); const listenerMapRef = useRef<ListenerMap>(new Map());
const { currentEditors } = useCurrentEditors(); const { currentEditors } = useCurrentEditors();
const editor = currentEditors[page_id] as Virgo; const editor = currentEditors[pageId] as Virgo;
const updateTocDataSource = useCallback(async () => { const updateTocDataSource = useCallback(async () => {
if (!editor) { if (!editor) {
@ -134,7 +134,7 @@ export const TOC = () => {
const listenerMap = listenerMapRef.current; const listenerMap = listenerMapRef.current;
/* page listener: trigger update-notice when add new group */ /* page listener: trigger update-notice when add new group */
const pageAsyncBlock = (await editor.getBlockByIds([page_id]))?.[0]; const pageAsyncBlock = (await editor.getBlockByIds([pageId]))?.[0];
if (!listenerMap.has(pageAsyncBlock.id)) { if (!listenerMap.has(pageAsyncBlock.id)) {
listenerMap.set( listenerMap.set(
pageAsyncBlock.id, pageAsyncBlock.id,
@ -144,7 +144,7 @@ export const TOC = () => {
/* block listener: trigger update-notice when change block content */ /* block listener: trigger update-notice when change block content */
const { children = [] } = const { children = [] } =
(await editor.queryByPageId(page_id))?.[0] || {}; (await editor.queryByPageId(pageId))?.[0] || {};
const asyncBlocks = (await editor.getBlockByIds(children)) || []; const asyncBlocks = (await editor.getBlockByIds(children)) || [];
const { tocContents } = await getContentByAsyncBlocks( const { tocContents } = await getContentByAsyncBlocks(
asyncBlocks, asyncBlocks,
@ -155,7 +155,7 @@ export const TOC = () => {
/* toc: flat content */ /* toc: flat content */
const tocDataSource = getPageTOC(asyncBlocks, tocContents); const tocDataSource = getPageTOC(asyncBlocks, tocContents);
setTocDataSource(tocDataSource); setTocDataSource(tocDataSource);
}, [editor, page_id]); }, [editor, pageId]);
/* init toc and add page/block update-listener & unmount-listener */ /* init toc and add page/block update-listener & unmount-listener */
useEffect(() => { useEffect(() => {

View File

@ -6,16 +6,16 @@ import { CreateView } from '@toeverything/framework/virgo';
type RefLinkView = CreateView; type RefLinkView = CreateView;
export const RefLinkView = ({ block, editor }: RefLinkView) => { export const RefLinkView = ({ block, editor }: RefLinkView) => {
const page_id = useMemo(() => block.getProperty('reference'), [block]); const pageId = useMemo(() => block.getProperty('reference'), [block]);
const [block_content, set_block] = const [block_content, set_block] =
useState<Awaited<ReturnType<typeof editor.search>>[number]>(); useState<Awaited<ReturnType<typeof editor.search>>[number]>();
useEffect(() => { useEffect(() => {
editor editor
.search({ tag: `id:${page_id}` }) .search({ tag: `id:${pageId}` })
.then(block => set_block(block[0])); .then(block => set_block(block[0]));
}, [editor, page_id]); }, [editor, pageId]);
return <InlineRefLink block={block_content} pageId={page_id} />; return <InlineRefLink block={block_content} pageId={pageId} />;
}; };

View File

@ -8,7 +8,7 @@ export const useAddComment = ({
selectionInfo, selectionInfo,
setShow, setShow,
}: WithEditorSelectionType) => { }: WithEditorSelectionType) => {
const { workspaceId, page_id: pageId } = useParams(); const { workspaceId, pageId } = useParams();
const [currentComment, setCurrentComment] = useState(''); const [currentComment, setCurrentComment] = useState('');
const createComment = useCallback(async (): Promise<{ const createComment = useCallback(async (): Promise<{

View File

@ -44,7 +44,7 @@ export const DoubleLinkMenu = ({
hooks, hooks,
style, style,
}: DoubleLinkMenuProps) => { }: DoubleLinkMenuProps) => {
const { page_id: curPageId } = useParams(); const { pageId: curPageId } = useParams();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
const dialogRef = useRef<HTMLDivElement>(); const dialogRef = useRef<HTMLDivElement>();

View File

@ -42,7 +42,7 @@ const normalizeUrl = (url: string) => {
}; };
export const LinkMenu = ({ editor, hooks }: LinkMenuProps) => { export const LinkMenu = ({ editor, hooks }: LinkMenuProps) => {
const { page_id: curPageId } = useParams(); const { pageId: curPageId } = useParams();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
const dialogRef = useRef<HTMLDivElement>(); const dialogRef = useRef<HTMLDivElement>();

View File

@ -9,7 +9,7 @@ import { useParams } from 'react-router-dom';
import type { CommentInfo } from './type'; import type { CommentInfo } from './type';
export const useComments = () => { export const useComments = () => {
const { workspaceId, page_id: pageId } = useParams(); const { workspaceId, pageId } = useParams();
const [comments, setComments] = useState<CommentInfo[]>([]); const [comments, setComments] = useState<CommentInfo[]>([]);
const [observeIds, setObserveIds] = useState<string[]>([]); const [observeIds, setObserveIds] = useState<string[]>([]);
@ -71,7 +71,7 @@ export const useComments = () => {
}; };
export const useActiveComment = () => { export const useActiveComment = () => {
const { workspaceId, page_id: pageId } = useParams(); const { workspaceId, pageId } = useParams();
const { currentEditors } = useCurrentEditors(); const { currentEditors } = useCurrentEditors();
const editor = useMemo(() => { const editor = useMemo(() => {
return currentEditors[pageId] as Virgo; return currentEditors[pageId] as Virgo;

View File

@ -47,8 +47,8 @@ export async function fetchActivitiesHeatmap(
// const pages = await db.getByType('page'); // const pages = await db.getByType('page');
const pages_with_ids = (await Promise.all( const pages_with_ids = (await Promise.all(
flattenedItems.map(async (page_item: any) => { flattenedItems.map(async (page_item: any) => {
const page_id = page_item.id; const pageId = page_item.id;
return [page_id, await db.get(page_id as 'page')]; return [pageId, await db.get(pageId as 'page')];
}) })
)) as [string, BlockImplInstance][]; )) as [string, BlockImplInstance][];
const pages = new Map(pages_with_ids); const pages = new Map(pages_with_ids);

View File

@ -57,7 +57,9 @@ export const TreeItem = forwardRef<HTMLDivElement, TreeItemProps>(
}, },
ref ref
) => { ) => {
const { workspaceId, page_id } = useParams(); const params = useParams();
const workspaceId = params['workspaceId'];
const curPageId = params['pageId'];
const BooleanPageTreeItemMoreActions = useFlag( const BooleanPageTreeItemMoreActions = useFlag(
'BooleanPageTreeItemMoreActions', 'BooleanPageTreeItemMoreActions',
true true
@ -71,7 +73,7 @@ export const TreeItem = forwardRef<HTMLDivElement, TreeItemProps>(
disableSelection={disableSelection} disableSelection={disableSelection}
disableInteraction={disableInteraction} disableInteraction={disableInteraction}
spacing={`${indentationWidth * depth + 12}px`} spacing={`${indentationWidth * depth + 12}px`}
active={pageId === page_id} active={pageId === curPageId}
{...props} {...props}
> >
{childCount !== 0 ? ( {childCount !== 0 ? (

View File

@ -19,7 +19,7 @@ import {
const page_tree_atom = atom<TreeItems | undefined>([]); const page_tree_atom = atom<TreeItems | undefined>([]);
export const usePageTree = ({ indentationWidth = 16 }: DndTreeProps = {}) => { export const usePageTree = ({ indentationWidth = 16 }: DndTreeProps = {}) => {
const { workspaceId, page_id } = useParams(); const { workspaceId, pageId } = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const [items] = useAtom(page_tree_atom); const [items] = useAtom(page_tree_atom);
const [activeId, setActiveId] = useState<string | undefined>(undefined); const [activeId, setActiveId] = useState<string | undefined>(undefined);
@ -132,7 +132,7 @@ export const usePageTree = ({ indentationWidth = 16 }: DndTreeProps = {}) => {
await services.api.userConfig.removePage(workspaceId, id); await services.api.userConfig.removePage(workspaceId, id);
//remove page from jwst //remove page from jwst
await services.api.pageTree.removePage(workspaceId, id); await services.api.pageTree.removePage(workspaceId, id);
if (id === page_id) { if (id === pageId) {
navigate(`/${workspaceId}`); navigate(`/${workspaceId}`);
} }
}, },
@ -140,8 +140,8 @@ export const usePageTree = ({ indentationWidth = 16 }: DndTreeProps = {}) => {
); );
const handleAddPage = useCallback( const handleAddPage = useCallback(
async (page_id?: string) => { async (pageId?: string) => {
await savePageTreeData([{ id: page_id, children: [] }, ...items]); await savePageTreeData([{ id: pageId, children: [] }, ...items]);
}, },
[items, savePageTreeData] [items, savePageTreeData]
); );
@ -177,7 +177,7 @@ export const usePageTree = ({ indentationWidth = 16 }: DndTreeProps = {}) => {
export const useDndTreeAutoUpdate = () => { export const useDndTreeAutoUpdate = () => {
const [, set_items] = useAtom(page_tree_atom); const [, set_items] = useAtom(page_tree_atom);
const { workspaceId, page_id } = useParams(); const { workspaceId, pageId } = useParams();
const fetch_page_tree_data = useCallback(async () => { const fetch_page_tree_data = useCallback(async () => {
const pages = await services.api.pageTree.getPageTree<TreeItem>( const pages = await services.api.pageTree.getPageTree<TreeItem>(
@ -191,11 +191,11 @@ export const useDndTreeAutoUpdate = () => {
}, [fetch_page_tree_data]); }, [fetch_page_tree_data]);
useEffect(() => { useEffect(() => {
if (!page_id) return () => {}; if (!pageId) return () => {};
let unobserve: () => void; let unobserve: () => void;
const auto_update_page_tree = async () => { const auto_update_page_tree = async () => {
unobserve = await services.api.pageTree.observe( unobserve = await services.api.pageTree.observe(
{ workspace: workspaceId, page: page_id }, { workspace: workspaceId, page: pageId },
() => { () => {
fetch_page_tree_data(); fetch_page_tree_data();
} }
@ -206,5 +206,5 @@ export const useDndTreeAutoUpdate = () => {
return () => { return () => {
unobserve?.(); unobserve?.();
}; };
}, [fetch_page_tree_data, page_id, workspaceId]); }, [fetch_page_tree_data, pageId, workspaceId]);
}; };

View File

@ -266,36 +266,33 @@ export class EditorBlock extends ServiceBaseClass {
} }
async copyPage( async copyPage(
workspaceId: string, workspaceId: string,
source_page_id: string, sourcePageId: string,
new_page_id: string newPageId: string
): Promise<boolean> { ): Promise<boolean> {
const db = await this.database.getDatabase(workspaceId); const db = await this.database.getDatabase(workspaceId);
const source_page = await this.getBlock( const sourcePage = await this.getBlock(
workspaceId, workspaceId,
source_page_id as 'block' sourcePageId as 'block'
); );
const new_page = await this.getBlock( const newPage = await this.getBlock(workspaceId, newPageId as 'block');
workspaceId, if (!sourcePage) {
new_page_id as 'block'
);
if (!source_page) {
return false; return false;
} }
const source_page_children = source_page.children; const sourcePageChildren = sourcePage.children;
const decorations = source_page.getDecorations(); const decorations = sourcePage.getDecorations();
Object.entries(decorations).forEach(([key, value]) => { Object.entries(decorations).forEach(([key, value]) => {
new_page?.setDecoration(key, source_page.getDecoration(key)); newPage?.setDecoration(key, sourcePage.getDecoration(key));
}); });
//@ts-ignore //@ts-ignore
this.decorate_page_title(new_page, 'copy from '); this.decorate_page_title(newPage, 'copy from ');
for (let i = 0; i < source_page_children.length; i++) { for (let i = 0; i < sourcePageChildren.length; i++) {
const source_page_child = await db.get( const sourcePageChild = await db.get(
source_page_children[i] as 'block' sourcePageChildren[i] as 'block'
); );
new_page?.insertChildren(source_page_child); newPage?.insertChildren(sourcePageChild);
} }
return true; return true;
} }

View File

@ -350,7 +350,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
const [file] = (await fromEvent(handles)) as File[]; const [file] = (await fromEvent(handles)) as File[];
const binary = await file?.arrayBuffer(); const binary = await file?.arrayBuffer();
console.log(this._provider.providers); console.log(this._provider.providers);
let { indexeddb } = ( const { indexeddb } = (
this._provider.providers as any[] this._provider.providers as any[]
).find(p => p.indexeddb); ).find(p => p.indexeddb);
await indexeddb?.idb?.clearData(); await indexeddb?.idb?.clearData();
@ -391,9 +391,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
}, },
parse: () => this._doc.toJSON(), parse: () => this._doc.toJSON(),
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
parse_page: (page_id: string) => { parse_page: (pageId: string) => {
const blocks = this._blocks.toJSON(); const blocks = this._blocks.toJSON();
return resolve_block(blocks, page_id); return resolve_block(blocks, pageId);
}, },
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
parse_pages: (resolve = false) => { parse_pages: (resolve = false) => {

View File

@ -9,7 +9,7 @@ const _currentEditors = atom<EditorsMap>({} as EditorsMap);
/** hook for using editors outside page */ /** hook for using editors outside page */
export const useCurrentEditors = () => { export const useCurrentEditors = () => {
const { workspaceId, page_id: pageId } = useParams(); const { workspaceId, pageId } = useParams();
const { pathname } = useLocation(); const { pathname } = useLocation();
const [currentEditors, setCurrentEditors] = useAtom(_currentEditors); const [currentEditors, setCurrentEditors] = useAtom(_currentEditors);

View File

@ -6,8 +6,8 @@ export function getUserDisplayName(user?: UserInfo) {
} }
/** /**
* Get page_id from URL * Get pageId from URL
* @returns page_id * @returns pageId
*/ */
export function getPageId() { export function getPageId() {
const path = window.location.pathname.match(/\/(\w+)\/(\w+)/); const path = window.location.pathname.match(/\/(\w+)\/(\w+)/);