refactor: switch to useEditor
This commit is contained in:
@@ -2,7 +2,7 @@ import type { BlockEditor } from './editor';
|
|||||||
import { styled, usePatchNodes } from '@toeverything/components/ui';
|
import { styled, usePatchNodes } from '@toeverything/components/ui';
|
||||||
import type { FC, PropsWithChildren } from 'react';
|
import type { FC, PropsWithChildren } from 'react';
|
||||||
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
||||||
import { RootContext } from './contexts';
|
import { EditorProvider } from './Contexts';
|
||||||
import { SelectionRect, SelectionRef } from './Selection';
|
import { SelectionRect, SelectionRef } from './Selection';
|
||||||
import {
|
import {
|
||||||
Protocol,
|
Protocol,
|
||||||
@@ -151,7 +151,7 @@ export const RenderRoot: FC<PropsWithChildren<RenderRootProps>> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RootContext.Provider value={{ editor, editorElement }}>
|
<EditorProvider value={{ editor, editorElement }}>
|
||||||
<Container
|
<Container
|
||||||
isWhiteboard={editor.isWhiteboard}
|
isWhiteboard={editor.isWhiteboard}
|
||||||
ref={ref => {
|
ref={ref => {
|
||||||
@@ -183,7 +183,7 @@ export const RenderRoot: FC<PropsWithChildren<RenderRootProps>> = ({
|
|||||||
{editor.isWhiteboard ? null : <ScrollBlank editor={editor} />}
|
{editor.isWhiteboard ? null : <ScrollBlank editor={editor} />}
|
||||||
{patchedNodes}
|
{patchedNodes}
|
||||||
</Container>
|
</Container>
|
||||||
</RootContext.Provider>
|
</EditorProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { createContext, useContext } from 'react';
|
import { createContext, useContext } from 'react';
|
||||||
import type { BlockEditor, AsyncBlock } from './editor';
|
import type { BlockEditor, AsyncBlock } from './editor';
|
||||||
import type { Column } from '@toeverything/datasource/db-service';
|
|
||||||
import { genErrorObj } from '@toeverything/utils';
|
import { genErrorObj } from '@toeverything/utils';
|
||||||
|
|
||||||
export const RootContext = createContext<{
|
const RootContext = createContext<{
|
||||||
editor: BlockEditor;
|
editor: BlockEditor;
|
||||||
// TODO: Temporary fix, dependencies in the new architecture are bottom-up, editors do not need to be passed down from the top
|
// TODO: Temporary fix, dependencies in the new architecture are bottom-up, editors do not need to be passed down from the top
|
||||||
editorElement: () => JSX.Element;
|
editorElement: () => JSX.Element;
|
||||||
@@ -14,6 +13,8 @@ export const RootContext = createContext<{
|
|||||||
) as any
|
) as any
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const EditorProvider = RootContext.Provider;
|
||||||
|
|
||||||
export const useEditor = () => {
|
export const useEditor = () => {
|
||||||
return useContext(RootContext);
|
return useContext(RootContext);
|
||||||
};
|
};
|
||||||
@@ -22,16 +23,3 @@ export const useEditor = () => {
|
|||||||
* @deprecated
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
export const BlockContext = createContext<AsyncBlock>(null as any);
|
export const BlockContext = createContext<AsyncBlock>(null as any);
|
||||||
|
|
||||||
/**
|
|
||||||
* Context of column information
|
|
||||||
*
|
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
export const ColumnsContext = createContext<{
|
|
||||||
fromId: string;
|
|
||||||
columns: Column[];
|
|
||||||
}>({
|
|
||||||
fromId: '',
|
|
||||||
columns: [],
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
} from './editor';
|
} from './editor';
|
||||||
import { noop, Point } from '@toeverything/utils';
|
import { noop, Point } from '@toeverything/utils';
|
||||||
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||||
import { RootContext } from './contexts';
|
import { useEditor } from './Contexts';
|
||||||
|
|
||||||
function useRequestReRender() {
|
function useRequestReRender() {
|
||||||
const [, setUpdateCounter] = useState(0);
|
const [, setUpdateCounter] = useState(0);
|
||||||
@@ -56,7 +56,7 @@ function useRequestReRender() {
|
|||||||
export const useBlock = (blockId: string) => {
|
export const useBlock = (blockId: string) => {
|
||||||
const [block, setBlock] = useState<AsyncBlock>();
|
const [block, setBlock] = useState<AsyncBlock>();
|
||||||
const requestReRender = useRequestReRender();
|
const requestReRender = useRequestReRender();
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!blockId) {
|
if (!blockId) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -95,7 +95,7 @@ export const useOnSelect = (
|
|||||||
blockId: string,
|
blockId: string,
|
||||||
cb: (isSelect: boolean) => void
|
cb: (isSelect: boolean) => void
|
||||||
) => {
|
) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.selectionManager.observe(blockId, SelectEventTypes.onSelect, cb);
|
editor.selectionManager.observe(blockId, SelectEventTypes.onSelect, cb);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -117,7 +117,7 @@ export const useOnSelectActive = (
|
|||||||
blockId: string,
|
blockId: string,
|
||||||
cb: (position: Point | undefined) => void
|
cb: (position: Point | undefined) => void
|
||||||
) => {
|
) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.selectionManager.observe(blockId, SelectEventTypes.active, cb);
|
editor.selectionManager.observe(blockId, SelectEventTypes.active, cb);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -139,7 +139,7 @@ export const useOnSelectSetSelection = <T extends keyof SelectionSettingsMap>(
|
|||||||
blockId: string,
|
blockId: string,
|
||||||
cb: (args: SelectionSettingsMap[T]) => void
|
cb: (args: SelectionSettingsMap[T]) => void
|
||||||
) => {
|
) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.selectionManager.observe(
|
editor.selectionManager.observe(
|
||||||
blockId,
|
blockId,
|
||||||
@@ -162,7 +162,7 @@ export const useOnSelectSetSelection = <T extends keyof SelectionSettingsMap>(
|
|||||||
* @export
|
* @export
|
||||||
*/
|
*/
|
||||||
export const useOnSelectChange = (cb: (info: SelectionInfo) => void) => {
|
export const useOnSelectChange = (cb: (info: SelectionInfo) => void) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.selectionManager.onSelectionChange(cb);
|
editor.selectionManager.onSelectionChange(cb);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -177,7 +177,7 @@ export const useOnSelectChange = (cb: (info: SelectionInfo) => void) => {
|
|||||||
* @export
|
* @export
|
||||||
*/
|
*/
|
||||||
export const useOnSelectEnd = (cb: (info: SelectionInfo) => void) => {
|
export const useOnSelectEnd = (cb: (info: SelectionInfo) => void) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.selectionManager.onSelectEnd(cb);
|
editor.selectionManager.onSelectEnd(cb);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -195,7 +195,7 @@ export const useOnSelectStartWith = (
|
|||||||
blockId: string,
|
blockId: string,
|
||||||
cb: (args: MouseEvent) => void
|
cb: (args: MouseEvent) => void
|
||||||
) => {
|
) => {
|
||||||
const { editor } = useContext(RootContext);
|
const { editor } = useEditor();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor.mouseManager.onSelectStartWith(blockId, cb);
|
editor.mouseManager.onSelectStartWith(blockId, cb);
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
export { ColumnsContext, RootContext } from './contexts';
|
|
||||||
export { RenderRoot, MIN_PAGE_WIDTH } from './RenderRoot';
|
export { RenderRoot, MIN_PAGE_WIDTH } from './RenderRoot';
|
||||||
export * from './render-block';
|
export * from './render-block';
|
||||||
export * from './hooks';
|
export * from './hooks';
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Protocol } from '@toeverything/datasource/db-service';
|
import { Protocol } from '@toeverything/datasource/db-service';
|
||||||
import { useCallback, useContext, useEffect, useState } from 'react';
|
import { useCallback, useContext, useEffect, useState } from 'react';
|
||||||
import { useEditor } from '../contexts';
|
import { useEditor } from '../Contexts';
|
||||||
import { AsyncBlock } from '../editor';
|
import { AsyncBlock } from '../editor';
|
||||||
import { useRecastView } from '../recast-block';
|
import { useRecastView } from '../recast-block';
|
||||||
import { useRecastBlock } from '../recast-block/Context';
|
import { useRecastBlock } from '../recast-block/Context';
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { styled, Theme } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
import { FC, useContext, useLayoutEffect, useMemo, useRef } from 'react';
|
import { FC, useLayoutEffect, useMemo, useRef } from 'react';
|
||||||
|
|
||||||
// import { RenderChildren } from './RenderChildren';
|
// import { RenderChildren } from './RenderChildren';
|
||||||
import { RootContext } from '../contexts';
|
import { useEditor } from '../Contexts';
|
||||||
import { useBlock } from '../hooks';
|
import { useBlock } from '../hooks';
|
||||||
|
|
||||||
interface RenderBlockProps {
|
interface RenderBlockProps {
|
||||||
@@ -14,7 +14,7 @@ export const RenderBlock: FC<RenderBlockProps> = ({
|
|||||||
blockId,
|
blockId,
|
||||||
hasContainer = true,
|
hasContainer = true,
|
||||||
}) => {
|
}) => {
|
||||||
const { editor, editorElement } = useContext(RootContext);
|
const { editor, editorElement } = useEditor();
|
||||||
const { block } = useBlock(blockId);
|
const { block } = useBlock(blockId);
|
||||||
const blockRef = useRef<HTMLDivElement>(null);
|
const blockRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user