refactor(view): migrate to new view api

This commit is contained in:
lawvs
2022-07-26 17:24:05 +08:00
parent 7584986aa9
commit f8485d895b
6 changed files with 28 additions and 27 deletions

View File

@@ -17,7 +17,6 @@ const SceneMap: Record<RecastScene, ComponentType<CreateView>> = {
page: ScenePage, page: ScenePage,
table: SceneTable, table: SceneTable,
kanban: SceneKanban, kanban: SceneKanban,
whiteboard: ScenePage,
} as const; } as const;
const GroupBox = styled('div')(({ theme }) => { const GroupBox = styled('div')(({ theme }) => {

View File

@@ -1,3 +1,5 @@
import { RecastScene } from '@toeverything/components/editor-core';
export const PANEL_CONFIG = { export const PANEL_CONFIG = {
FILTER: 'filter', FILTER: 'filter',
SORTER: 'sorter', SORTER: 'sorter',
@@ -5,9 +7,11 @@ export const PANEL_CONFIG = {
GROUP_BY: 'group_by', GROUP_BY: 'group_by',
} as const; } as const;
export const SCENE_CONFIG = { /**
* See {@link RecastScene}
*/
export const SCENE_CONFIG: Record<Uppercase<RecastScene>, string> = {
PAGE: 'page', PAGE: 'page',
KANBAN: 'kanban', KANBAN: 'kanban',
TABLE: 'table', TABLE: 'table',
REFLINK: 'reflink',
} as const; } as const;

View File

@@ -1,18 +1,16 @@
import { FC, useState, useEffect, useRef } from 'react';
import { CreateView } from '@toeverything/framework/virgo';
import { Upload } from '../../components/upload/upload';
import { services } from '@toeverything/datasource/db-service';
import { styled } from '@toeverything/components/ui';
import DeleteSweepOutlinedIcon from '@mui/icons-material/DeleteSweepOutlined';
import DownloadIcon from '@mui/icons-material/Download';
import { Image as SourceView } from '../../components/ImageView';
import { import {
useCurrentView,
useOnSelect, useOnSelect,
useRecastBlockScene,
WrapperWithPendantAndDragDrop, WrapperWithPendantAndDragDrop,
} from '@toeverything/components/editor-core'; } from '@toeverything/components/editor-core';
import './styles.css'; import { styled } from '@toeverything/components/ui';
import { services } from '@toeverything/datasource/db-service';
import { CreateView } from '@toeverything/framework/virgo';
import { FC, useEffect, useRef, useState } from 'react';
import { Image as SourceView } from '../../components/ImageView';
import { Upload } from '../../components/upload/upload';
import { SCENE_CONFIG } from '../group/config'; import { SCENE_CONFIG } from '../group/config';
import './styles.css';
const MESSAGES = { const MESSAGES = {
ADD_AN_IMAGE: 'Add an image', ADD_AN_IMAGE: 'Add an image',
}; };
@@ -62,14 +60,14 @@ export const ImageView: FC<ImageView> = ({ block, editor }) => {
const [imgWidth, setImgWidth] = useState<number>(0); const [imgWidth, setImgWidth] = useState<number>(0);
const [ratio, set_ratio] = useState<number>(0); const [ratio, set_ratio] = useState<number>(0);
const resize_box = useRef(null); const resize_box = useRef(null);
const { scene } = useRecastBlockScene(); const [currentView] = useCurrentView();
const [isSelect, setIsSelect] = useState<boolean>(); const [isSelect, setIsSelect] = useState<boolean>();
useOnSelect(block.id, (isSelect: boolean) => { useOnSelect(block.id, (isSelect: boolean) => {
setIsSelect(isSelect); setIsSelect(isSelect);
}); });
const img_load = (url: string) => { const img_load = (url: string) => {
let boxWidth = resize_box.current.offsetWidth; const boxWidth = resize_box.current.offsetWidth;
const imageStyle = block.getProperty('image_style'); const imageStyle = block.getProperty('image_style');
if (imageStyle?.width) { if (imageStyle?.width) {
@@ -171,7 +169,7 @@ export const ImageView: FC<ImageView> = ({ block, editor }) => {
e.stopPropagation(); e.stopPropagation();
}} }}
> >
{scene === SCENE_CONFIG.PAGE ? ( {currentView.type === SCENE_CONFIG.PAGE ? (
<SourceView <SourceView
block={block} block={block}
viewStyle={{ viewStyle={{

View File

@@ -1,10 +1,11 @@
import { import {
CreateView,
RenderBlock, RenderBlock,
useCurrentView,
useOnSelect, useOnSelect,
useRecastBlockScene,
WrapperWithPendantAndDragDrop, WrapperWithPendantAndDragDrop,
} from '@toeverything/components/editor-core'; } from '@toeverything/components/editor-core';
import { CreateView } from '@toeverything/framework/virgo'; import { styled } from '@toeverything/components/ui';
import type { import type {
ComponentPropsWithoutRef, ComponentPropsWithoutRef,
ComponentPropsWithRef, ComponentPropsWithRef,
@@ -13,10 +14,8 @@ import type {
} from 'react'; } from 'react';
import { forwardRef, useState } from 'react'; import { forwardRef, useState } from 'react';
import style9 from 'style9'; import style9 from 'style9';
import { BlockContainer } from '../components/BlockContainer';
import { styled } from '@toeverything/components/ui';
import { SCENE_CONFIG } from '../blocks/group/config'; import { SCENE_CONFIG } from '../blocks/group/config';
import { BlockContainer } from '../components/BlockContainer';
type WithChildrenConfig = { type WithChildrenConfig = {
indent: CSSProperties['marginLeft']; indent: CSSProperties['marginLeft'];
@@ -68,8 +67,8 @@ const ChildrenView = ({
handleCollapse, handleCollapse,
indent, indent,
}: ChildrenViewProp) => { }: ChildrenViewProp) => {
const { scene } = useRecastBlockScene(); const [currentView] = useCurrentView();
const isKanbanScene = scene === SCENE_CONFIG.KANBAN; const isKanbanScene = currentView.type === SCENE_CONFIG.KANBAN;
return ( return (
<div <div
@@ -130,9 +129,9 @@ export const withTreeViewChildren = (
const childrenIds = block.childrenIds; const childrenIds = block.childrenIds;
const showChildren = !collapsed && childrenIds.length > 0; const showChildren = !collapsed && childrenIds.length > 0;
const [isSelect, setIsSelect] = useState<boolean>(); const [isSelect, setIsSelect] = useState<boolean>(false);
useOnSelect(block.id, (is_select: boolean) => { useOnSelect(block.id, (isSelect: boolean) => {
setIsSelect(is_select); setIsSelect(isSelect);
}); });
const handleCollapse = () => { const handleCollapse = () => {
block.setProperty('collapsed', { value: true }); block.setProperty('collapsed', { value: true });

View File

@@ -7,6 +7,7 @@ import { RecastScene } from './types';
* *
* 获取/设置多维区块场景 * 获取/设置多维区块场景
* @public * @public
* @deprecated Use the `useRecastView` or `useCurrentView` API
*/ */
export const useRecastBlockScene = () => { export const useRecastBlockScene = () => {
const groupBlock = useRecastBlock(); const groupBlock = useRecastBlock();

View File

@@ -7,7 +7,7 @@ export enum RecastScene {
Page = 'page', Page = 'page',
Kanban = 'kanban', Kanban = 'kanban',
Table = 'table', Table = 'table',
Whiteboard = 'whiteboard', // Whiteboard = 'whiteboard',
} }
export type RecastViewId = string & { export type RecastViewId = string & {