fix(whiteboard): error when drag away last element in Chrome

This commit is contained in:
austaras 2022-08-12 18:23:00 +08:00 committed by Austaras
parent c67a033f8a
commit 308d2c4445
9 changed files with 85 additions and 85 deletions

View File

@ -124,10 +124,16 @@ export class EditorUtil extends TDShapeUtil<T, E> {
); );
const activateIfEditing = useCallback(() => { const activateIfEditing = useCallback(() => {
const shapes =
state.document.pages[state.appState.currentPageId].shapes;
// https://bugs.chromium.org/p/chromium/issues/detail?id=1352417
if (shapes[shape.id] != null) {
return;
}
if (editingText && editingId !== shape.id) { if (editingText && editingId !== shape.id) {
app.setEditingText(shape.id); app.setEditingText(shape.id);
} }
}, [app, shape.id, editingText, editingId]); }, [app, state, shape.id, editingText, editingId]);
return ( return (
<HTMLContainer ref={ref} {...events}> <HTMLContainer ref={ref} {...events}>

View File

@ -1,6 +1,6 @@
import type { BlockEditor } from './editor'; 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 { PropsWithChildren } from 'react';
import React, { useEffect, useRef, useState, useCallback } from 'react'; import React, { useEffect, useRef, useState, useCallback } from 'react';
import { EditorProvider } from './Contexts'; import { EditorProvider } from './Contexts';
import { SelectionRect, SelectionRef } from './Selection'; import { SelectionRect, SelectionRef } from './Selection';
@ -152,6 +152,7 @@ export const RenderRoot = ({
}; };
const onDrop = (event: React.DragEvent<Element>) => { const onDrop = (event: React.DragEvent<Element>) => {
event.preventDefault();
editor.dragDropManager.handlerEditorDrop(event); editor.dragDropManager.handlerEditorDrop(event);
editor.getHooks().onRootNodeDrop(event); editor.getHooks().onRootNodeDrop(event);
}; };

View File

@ -32,11 +32,11 @@ export class BlockCommands {
) { ) {
const block = await this._editor.getBlockById(blockId); const block = await this._editor.getBlockById(blockId);
if (block) { if (block) {
const next_block = await this._editor.createBlock(type); const nextBlock = await this._editor.createBlock(type);
if (next_block) { if (nextBlock) {
await block.after(next_block); await block.after(nextBlock);
this._editor.selectionManager.activeNodeByNodeId(next_block.id); this._editor.selectionManager.activeNodeByNodeId(nextBlock.id);
return next_block; return nextBlock;
} }
} }
return undefined; return undefined;

View File

@ -101,73 +101,70 @@ export class DragDropManager {
if (this._blockDragDirection !== BlockDropPlacement.none) { if (this._blockDragDirection !== BlockDropPlacement.none) {
const blockId = event.dataTransfer.getData(this._blockIdKey); const blockId = event.dataTransfer.getData(this._blockIdKey);
if (!(await this._canBeDrop(event))) return; if (!(await this._canBeDrop(event))) return;
if (this._blockDragDirection === BlockDropPlacement.bottom) { switch (this._blockDragDirection) {
this._editor.commands.blockCommands.moveBlockAfter( case BlockDropPlacement.bottom: {
blockId, this._editor.commands.blockCommands.moveBlockAfter(
this._blockDragTargetId
);
}
if (
[BlockDropPlacement.left, BlockDropPlacement.right].includes(
this._blockDragDirection
)
) {
const dropType =
this._blockDragDirection === BlockDropPlacement.left
? GridDropType.left
: GridDropType.right;
// if target is a grid item create grid item
if (targetBlock.type !== Protocol.Block.Type.gridItem) {
await this._editor.commands.blockCommands.createLayoutBlock(
blockId, blockId,
this._blockDragTargetId, this._blockDragTargetId
dropType
);
} else {
await this._editor.commands.blockCommands.moveInNewGridItem(
blockId,
this._blockDragTargetId,
dropType
); );
break;
} }
} case BlockDropPlacement.left:
if ( case BlockDropPlacement.right: {
[ const dropType =
BlockDropPlacement.outerLeft, this._blockDragDirection === BlockDropPlacement.left
BlockDropPlacement.outerRight,
].includes(this._blockDragDirection)
) {
if (targetBlock.type !== Protocol.Block.Type.grid) {
await this._editor.commands.blockCommands.createLayoutBlock(
blockId,
this._blockDragTargetId,
this._blockDragDirection ===
BlockDropPlacement.outerLeft
? GridDropType.left ? GridDropType.left
: GridDropType.right : GridDropType.right;
); // if target is a grid item create grid item
} if (targetBlock.type !== Protocol.Block.Type.gridItem) {
if (targetBlock.type === Protocol.Block.Type.grid) { await this._editor.commands.blockCommands.createLayoutBlock(
const gridItems = await targetBlock.children(); blockId,
if ( this._blockDragTargetId,
BlockDropPlacement.outerRight === dropType
this._blockDragDirection );
) { } else {
await this._editor.commands.blockCommands.moveInNewGridItem( await this._editor.commands.blockCommands.moveInNewGridItem(
blockId, blockId,
gridItems[gridItems.length - 1].id this._blockDragTargetId,
dropType
); );
} }
if ( break;
BlockDropPlacement.outerLeft === }
this._blockDragDirection case BlockDropPlacement.outerLeft:
) { case BlockDropPlacement.outerRight: {
await this._editor.commands.blockCommands.moveInNewGridItem( if (targetBlock.type !== Protocol.Block.Type.grid) {
await this._editor.commands.blockCommands.createLayoutBlock(
blockId, blockId,
gridItems[0].id, this._blockDragTargetId,
GridDropType.right this._blockDragDirection ===
BlockDropPlacement.outerLeft
? GridDropType.left
: GridDropType.right
); );
} }
if (targetBlock.type === Protocol.Block.Type.grid) {
const gridItems = await targetBlock.children();
if (
BlockDropPlacement.outerRight ===
this._blockDragDirection
) {
await this._editor.commands.blockCommands.moveInNewGridItem(
blockId,
gridItems[gridItems.length - 1].id
);
}
if (
BlockDropPlacement.outerLeft ===
this._blockDragDirection
) {
await this._editor.commands.blockCommands.moveInNewGridItem(
blockId,
gridItems[0].id,
GridDropType.right
);
}
}
} }
} }
} }
@ -424,7 +421,6 @@ export class DragDropManager {
} }
public handlerEditorDrop(event: React.DragEvent<Element>) { public handlerEditorDrop(event: React.DragEvent<Element>) {
event.preventDefault();
// IMP: can not use Decorators now may use decorators is right // IMP: can not use Decorators now may use decorators is right
if (this.isEnabled()) { if (this.isEnabled()) {
if (this.isDragBlock(event)) { if (this.isDragBlock(event)) {

View File

@ -207,6 +207,7 @@ export class ScrollManager {
} }
private _getKeepInViewParams(blockRect: Rect) { private _getKeepInViewParams(blockRect: Rect) {
if (this.scrollContainer == null) return 0;
const { top, bottom } = domToRect(this._scrollContainer); const { top, bottom } = domToRect(this._scrollContainer);
if (blockRect.top <= top + blockRect.height * 3) { if (blockRect.top <= top + blockRect.height * 3) {
return -1; return -1;

View File

@ -1,4 +1,4 @@
import { noop, Point } from '@toeverything/utils'; import { Point } from '@toeverything/utils';
import { useCallback, useEffect, useRef, useState } from 'react'; import { useCallback, useEffect, useRef, useState } from 'react';
import { useEditor } from './Contexts'; import { useEditor } from './Contexts';
import { import {
@ -62,7 +62,7 @@ export const useBlock = (blockId: string) => {
return undefined; return undefined;
} }
let valid = true; let valid = true;
let offUpdate = noop; let offUpdate: () => void | undefined = undefined;
editor.getBlockById(blockId).then(node => { editor.getBlockById(blockId).then(node => {
if (!valid) { if (!valid) {
return; return;
@ -79,7 +79,7 @@ export const useBlock = (blockId: string) => {
return () => { return () => {
valid = false; valid = false;
offUpdate(); offUpdate?.();
}; };
}, [blockId, editor, requestReRender]); }, [blockId, editor, requestReRender]);

View File

@ -1,7 +1,6 @@
import { styled } from '@toeverything/components/ui'; import { styled } from '@toeverything/components/ui';
import { FC, useLayoutEffect, useMemo, useRef } from 'react'; import { useCallback, useMemo } from 'react';
// import { RenderChildren } from './RenderChildren';
import { useEditor } from '../Contexts'; import { useEditor } from '../Contexts';
import { useBlock } from '../hooks'; import { useBlock } from '../hooks';
@ -10,19 +9,21 @@ interface RenderBlockProps {
hasContainer?: boolean; hasContainer?: boolean;
} }
export const RenderBlock = ({ export function RenderBlock({
blockId, blockId,
hasContainer = true, hasContainer = true,
}: RenderBlockProps) => { }: RenderBlockProps) {
const { editor, editorElement } = useEditor(); const { editor, editorElement } = useEditor();
const { block } = useBlock(blockId); const { block } = useBlock(blockId);
const blockRef = useRef<HTMLDivElement>(null);
useLayoutEffect(() => { const setRef = useCallback(
if (block && blockRef.current) { (dom: HTMLElement) => {
block.dom = blockRef.current; if (block != null && dom != null) {
} block.dom = dom;
}); }
},
[block]
);
const blockView = useMemo(() => { const blockView = useMemo(() => {
if (block?.type) { if (block?.type) {
@ -54,17 +55,13 @@ export const RenderBlock = ({
) : null; ) : null;
return hasContainer ? ( return hasContainer ? (
<BlockContainer <BlockContainer block-id={blockId} ref={setRef} data-block-id={blockId}>
block-id={blockId}
ref={blockRef}
data-block-id={blockId}
>
{view} {view}
</BlockContainer> </BlockContainer>
) : ( ) : (
<> {view}</> <> {view}</>
); );
}; }
const BlockContainer = styled('div')(({ theme }) => ({ const BlockContainer = styled('div')(({ theme }) => ({
fontSize: theme.typography.body1.fontSize, fontSize: theme.typography.body1.fontSize,

View File

@ -93,7 +93,7 @@ export class LeftMenuPlugin extends BasePlugin {
} }
}; };
private _onDrop = () => { private _onDrop = (e: React.DragEvent<Element>) => {
this._lineInfo.next(undefined); this._lineInfo.next(undefined);
}; };
private _handleDragOverBlockNode = async ( private _handleDragOverBlockNode = async (

View File

@ -12,7 +12,6 @@ export {
isFunction, isFunction,
isString, isString,
isPlainObject, isPlainObject,
noop,
lowerFirst, lowerFirst,
last, last,
first, first,