fix(plugin): left menu item refresh on blockId change
This commit is contained in:
@@ -7,12 +7,6 @@ export * from './commands/types';
|
|||||||
export { Editor as BlockEditor } from './editor';
|
export { Editor as BlockEditor } from './editor';
|
||||||
export * from './selection';
|
export * from './selection';
|
||||||
export { BlockDropPlacement, HookType, GroupDirection } from './types';
|
export { BlockDropPlacement, HookType, GroupDirection } from './types';
|
||||||
export type {
|
export type { Plugin, PluginCreator, PluginHooks, Virgo } from './types';
|
||||||
BlockDomInfo,
|
|
||||||
Plugin,
|
|
||||||
PluginCreator,
|
|
||||||
PluginHooks,
|
|
||||||
Virgo,
|
|
||||||
} from './types';
|
|
||||||
export { BaseView, getTextHtml, getTextProperties } from './views/base-view';
|
export { BaseView, getTextHtml, getTextProperties } from './views/base-view';
|
||||||
export type { ChildrenView, CreateView } from './views/base-view';
|
export type { ChildrenView, CreateView } from './views/base-view';
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { DragEvent } from 'react';
|
|
||||||
import { Observable, Subject } from 'rxjs';
|
import { Observable, Subject } from 'rxjs';
|
||||||
import { HooksRunner, HookType, BlockDomInfo, PluginHooks } from '../types';
|
import { HooksRunner, HookType, PluginHooks } from '../types';
|
||||||
|
|
||||||
export class Hooks implements HooksRunner, PluginHooks {
|
export class Hooks implements HooksRunner, PluginHooks {
|
||||||
private _subject: Record<string, Subject<unknown>> = {};
|
private _subject: Record<string, Subject<unknown>> = {};
|
||||||
|
|||||||
@@ -183,14 +183,6 @@ export enum HookType {
|
|||||||
ON_ROOTNODE_SCROLL = 'onRootNodeScroll',
|
ON_ROOTNODE_SCROLL = 'onRootNodeScroll',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BlockDomInfo {
|
|
||||||
blockId: string;
|
|
||||||
dom: HTMLElement;
|
|
||||||
type: BlockFlavorKeys;
|
|
||||||
rect: DOMRect;
|
|
||||||
properties: Record<string, unknown>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Editor's various callbacks, used in Editor
|
// Editor's various callbacks, used in Editor
|
||||||
export interface HooksRunner {
|
export interface HooksRunner {
|
||||||
init: () => void;
|
init: () => void;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { Virgo, PluginHooks } from '@toeverything/framework/virgo';
|
import { Virgo, PluginHooks } from '@toeverything/framework/virgo';
|
||||||
import { Cascader, CascaderItemProps } from '@toeverything/components/ui';
|
import { Cascader, CascaderItemProps } from '@toeverything/components/ui';
|
||||||
import { TurnIntoMenu } from './TurnIntoMenu';
|
import { TurnIntoMenu } from './TurnIntoMenu';
|
||||||
@@ -18,42 +18,43 @@ interface LeftMenuProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function LeftMenu(props: LeftMenuProps) {
|
export function LeftMenu(props: LeftMenuProps) {
|
||||||
const { editor, anchorEl, hooks, blockId } = props;
|
const { editor, anchorEl, hooks, blockId, onClose } = props;
|
||||||
const menu: CascaderItemProps[] = [
|
const menu: CascaderItemProps[] = useMemo(
|
||||||
{
|
() => [
|
||||||
title: 'Delete',
|
{
|
||||||
callback: () => {
|
title: 'Delete',
|
||||||
editor.commands.blockCommands.removeBlock(blockId);
|
callback: () => {
|
||||||
|
editor.commands.blockCommands.removeBlock(blockId);
|
||||||
|
},
|
||||||
|
shortcut: 'Del',
|
||||||
|
icon: <DeleteCashBinIcon />,
|
||||||
},
|
},
|
||||||
shortcut: 'Del',
|
{
|
||||||
icon: <DeleteCashBinIcon />,
|
title: 'Turn into',
|
||||||
},
|
subItems: [],
|
||||||
{
|
children: (
|
||||||
title: 'Turn into',
|
<TurnIntoMenu
|
||||||
subItems: [],
|
editor={editor}
|
||||||
children: (
|
hooks={hooks}
|
||||||
<TurnIntoMenu
|
blockId={blockId}
|
||||||
editor={editor}
|
onClose={() => {
|
||||||
hooks={hooks}
|
onClose();
|
||||||
blockId={blockId}
|
editor.selection.setSelectedNodesIds([]);
|
||||||
onClose={() => {
|
}}
|
||||||
props.onClose();
|
/>
|
||||||
editor.selection.setSelectedNodesIds([]);
|
),
|
||||||
}}
|
icon: <TurnIntoIcon />,
|
||||||
/>
|
|
||||||
),
|
|
||||||
icon: <TurnIntoIcon />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'Divide Here As A New Group',
|
|
||||||
icon: <UngroupIcon />,
|
|
||||||
callback: () => {
|
|
||||||
editor.commands.blockCommands.splitGroupFromBlock(blockId);
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
].filter(v => v);
|
title: 'Divide Here As A New Group',
|
||||||
|
icon: <UngroupIcon />,
|
||||||
const [menuList, setMenuList] = useState<CascaderItemProps[]>(menu);
|
callback: () => {
|
||||||
|
editor.commands.blockCommands.splitGroupFromBlock(blockId);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[editor, hooks, blockId, onClose]
|
||||||
|
);
|
||||||
|
|
||||||
// const filterItems = (
|
// const filterItems = (
|
||||||
// value: string,
|
// value: string,
|
||||||
@@ -90,7 +91,7 @@ export function LeftMenu(props: LeftMenuProps) {
|
|||||||
<>
|
<>
|
||||||
{props.children}
|
{props.children}
|
||||||
<Cascader
|
<Cascader
|
||||||
items={menuList}
|
items={menu}
|
||||||
anchorEl={anchorEl}
|
anchorEl={anchorEl}
|
||||||
placement="bottom-start"
|
placement="bottom-start"
|
||||||
open={Boolean(anchorEl)}
|
open={Boolean(anchorEl)}
|
||||||
|
|||||||
@@ -6,14 +6,15 @@ import {
|
|||||||
type DragEvent,
|
type DragEvent,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
type CSSProperties,
|
type CSSProperties,
|
||||||
|
useCallback,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Virgo,
|
Virgo,
|
||||||
BlockDomInfo,
|
|
||||||
PluginHooks,
|
PluginHooks,
|
||||||
BlockDropPlacement,
|
BlockDropPlacement,
|
||||||
LINE_GAP,
|
LINE_GAP,
|
||||||
|
AsyncBlock,
|
||||||
} from '@toeverything/framework/virgo';
|
} from '@toeverything/framework/virgo';
|
||||||
import { Button } from '@toeverything/components/common';
|
import { Button } from '@toeverything/components/common';
|
||||||
import { styled } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
@@ -25,6 +26,11 @@ import { MENU_WIDTH } from './menu-config';
|
|||||||
|
|
||||||
const MENU_BUTTON_OFFSET = 4;
|
const MENU_BUTTON_OFFSET = 4;
|
||||||
|
|
||||||
|
export interface BlockDomInfo {
|
||||||
|
block: AsyncBlock;
|
||||||
|
rect: DOMRect;
|
||||||
|
}
|
||||||
|
|
||||||
export type LineInfoSubject = Subject<
|
export type LineInfoSubject = Subject<
|
||||||
| {
|
| {
|
||||||
direction: BlockDropPlacement;
|
direction: BlockDropPlacement;
|
||||||
@@ -138,11 +144,11 @@ export const LeftMenuDraggable: FC<LeftMenuProps> = props => {
|
|||||||
if (block == null) return;
|
if (block == null) return;
|
||||||
setRootRect(editor.container.getBoundingClientRect());
|
setRootRect(editor.container.getBoundingClientRect());
|
||||||
const dragImage = await editor.blockHelper.getBlockDragImg(
|
const dragImage = await editor.blockHelper.getBlockDragImg(
|
||||||
block.blockId
|
block.block.id
|
||||||
);
|
);
|
||||||
if (dragImage) {
|
if (dragImage) {
|
||||||
event.dataTransfer.setDragImage(dragImage, -50, -10);
|
event.dataTransfer.setDragImage(dragImage, -50, -10);
|
||||||
editor.dragDropManager.setDragBlockInfo(event, block.blockId);
|
editor.dragDropManager.setDragBlockInfo(event, block.block.id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -154,16 +160,18 @@ export const LeftMenuDraggable: FC<LeftMenuProps> = props => {
|
|||||||
const onClick = (event: MouseEvent<Element>) => {
|
const onClick = (event: MouseEvent<Element>) => {
|
||||||
if (block == null) return;
|
if (block == null) return;
|
||||||
const currentTarget = event.currentTarget;
|
const currentTarget = event.currentTarget;
|
||||||
editor.selection.setSelectedNodesIds([block.blockId]);
|
editor.selection.setSelectedNodesIds([block.block.id]);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setAnchorEl(currentTarget);
|
setAnchorEl(currentTarget);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onClose = useCallback(() => setAnchorEl(undefined), [setAnchorEl]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const sub = blockInfo
|
const sub = blockInfo
|
||||||
.pipe(
|
.pipe(
|
||||||
distinctUntilChanged(
|
distinctUntilChanged(
|
||||||
(prev, curr) => prev?.blockId === curr?.blockId
|
(prev, curr) => prev?.block.id === curr?.block.id
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.subscribe(block => {
|
.subscribe(block => {
|
||||||
@@ -185,7 +193,7 @@ export const LeftMenuDraggable: FC<LeftMenuProps> = props => {
|
|||||||
setRootRect(editor.container.getBoundingClientRect());
|
setRootRect(editor.container.getBoundingClientRect());
|
||||||
setLine(prev => {
|
setLine(prev => {
|
||||||
if (
|
if (
|
||||||
prev?.blockInfo.blockId !== blockInfo.blockId ||
|
prev?.blockInfo.block.id !== blockInfo.block.id ||
|
||||||
prev?.direction !== direction
|
prev?.direction !== direction
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
@@ -224,8 +232,8 @@ export const LeftMenuDraggable: FC<LeftMenuProps> = props => {
|
|||||||
anchorEl={anchorEl}
|
anchorEl={anchorEl}
|
||||||
editor={props.editor}
|
editor={props.editor}
|
||||||
hooks={props.hooks}
|
hooks={props.hooks}
|
||||||
onClose={() => setAnchorEl(undefined)}
|
onClose={onClose}
|
||||||
blockId={block.blockId}
|
blockId={block.block.id}
|
||||||
>
|
>
|
||||||
<Draggable onClick={onClick}>
|
<Draggable onClick={onClick}>
|
||||||
<HandleChildIcon />
|
<HandleChildIcon />
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import {
|
import { HookType, BlockDropPlacement } from '@toeverything/framework/virgo';
|
||||||
BlockDomInfo,
|
|
||||||
HookType,
|
|
||||||
BlockDropPlacement,
|
|
||||||
} from '@toeverything/framework/virgo';
|
|
||||||
import { StrictMode } from 'react';
|
import { StrictMode } from 'react';
|
||||||
import { BasePlugin } from '../../base-plugin';
|
import { BasePlugin } from '../../base-plugin';
|
||||||
import { ignoreBlockTypes } from './menu-config';
|
import { ignoreBlockTypes } from './menu-config';
|
||||||
import { LineInfoSubject, LeftMenuDraggable } from './LeftMenuDraggable';
|
import {
|
||||||
|
LineInfoSubject,
|
||||||
|
LeftMenuDraggable,
|
||||||
|
BlockDomInfo,
|
||||||
|
} from './LeftMenuDraggable';
|
||||||
import { PluginRenderRoot } from '../../utils';
|
import { PluginRenderRoot } from '../../utils';
|
||||||
import { Subject, throttleTime } from 'rxjs';
|
import { Subject, throttleTime } from 'rxjs';
|
||||||
import { domToRect, last, Point } from '@toeverything/utils';
|
import { domToRect, last, Point } from '@toeverything/utils';
|
||||||
@@ -81,11 +81,8 @@ export class LeftMenuPlugin extends BasePlugin {
|
|||||||
this._lineInfo.next({
|
this._lineInfo.next({
|
||||||
direction,
|
direction,
|
||||||
blockInfo: {
|
blockInfo: {
|
||||||
blockId: block.id,
|
block,
|
||||||
dom: block.dom,
|
|
||||||
type: block.type,
|
|
||||||
rect: block.dom.getBoundingClientRect(),
|
rect: block.dom.getBoundingClientRect(),
|
||||||
properties: block.getProperties(),
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if (!isOuter) {
|
} else if (!isOuter) {
|
||||||
@@ -116,11 +113,8 @@ export class LeftMenuPlugin extends BasePlugin {
|
|||||||
this._lineInfo.next({
|
this._lineInfo.next({
|
||||||
direction,
|
direction,
|
||||||
blockInfo: {
|
blockInfo: {
|
||||||
blockId: block.id,
|
block,
|
||||||
dom: block.dom,
|
|
||||||
rect: block.dom.getBoundingClientRect(),
|
rect: block.dom.getBoundingClientRect(),
|
||||||
type: block.type,
|
|
||||||
properties: block.getProperties(),
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -169,11 +163,8 @@ export class LeftMenuPlugin extends BasePlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._blockInfo.next({
|
this._blockInfo.next({
|
||||||
blockId: node.id,
|
block: node,
|
||||||
dom: node.dom,
|
|
||||||
rect: node.dom.getBoundingClientRect(),
|
rect: node.dom.getBoundingClientRect(),
|
||||||
type: node.type,
|
|
||||||
properties: node.getProperties(),
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user