feat(editor): focus block when click on space

This commit is contained in:
austaras 2022-08-24 18:25:57 +08:00 committed by Austaras
parent 77e3513d6d
commit 3e0d076899
15 changed files with 64 additions and 43 deletions

View File

@ -188,7 +188,7 @@ export const BulletView = ({ block, editor }: CreateView) => {
return ( return (
<BlockContainer editor={editor} block={block} selected={isSelect}> <BlockContainer editor={editor} block={block} selected={isSelect}>
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<List> <List>
<BulletLeft> <BulletLeft>
<BulletIcon numberType={properties.numberType} /> <BulletIcon numberType={properties.numberType} />

View File

@ -170,7 +170,7 @@ export const CodeView = ({ block, editor }: CreateCodeView) => {
editor.selectionManager.activePreviousNode(block.id, 'start'); editor.selectionManager.activePreviousNode(block.id, 'start');
}; };
return ( return (
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<CodeBlock <CodeBlock
onKeyDown={e => { onKeyDown={e => {
e.stopPropagation(); e.stopPropagation();
@ -198,7 +198,8 @@ export const CodeView = ({ block, editor }: CreateCodeView) => {
</div> </div>
<div> <div>
<div className="copy-block" onClick={copyCode}> <div className="copy-block" onClick={copyCode}>
<DuplicateIcon></DuplicateIcon>Copy <DuplicateIcon />
Copy
</div> </div>
</div> </div>
</div> </div>

View File

@ -33,7 +33,7 @@ export const EmbedLinkView = (props: EmbedLinkView) => {
}; };
return ( return (
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<LinkContainer> <LinkContainer>
{embedLinkUrl ? ( {embedLinkUrl ? (
<SourceView <SourceView

View File

@ -1,13 +1,12 @@
import { useState } from 'react';
import { CreateView } from '@toeverything/framework/virgo';
import { import {
useOnSelect,
BlockPendantProvider, BlockPendantProvider,
useOnSelect,
} from '@toeverything/components/editor-core'; } from '@toeverything/components/editor-core';
import { Upload } from '../../components/upload/upload'; import { CreateView } from '@toeverything/framework/virgo';
import { useState } from 'react';
import { SourceView } from '../../components/source-view'; import { SourceView } from '../../components/source-view';
import { styled } from '@toeverything/components/ui';
import { LinkContainer } from '../../components/style-container'; import { LinkContainer } from '../../components/style-container';
import { Upload } from '../../components/upload/upload';
const MESSAGES = { const MESSAGES = {
ADD_FIGMA_LINK: 'Add figma link', ADD_FIGMA_LINK: 'Add figma link',
@ -30,7 +29,7 @@ export const FigmaView = ({ block, editor }: FigmaView) => {
setIsSelect(isSelect); setIsSelect(isSelect);
}); });
return ( return (
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<LinkContainer> <LinkContainer>
{figmaUrl ? ( {figmaUrl ? (
<SourceView <SourceView

View File

@ -165,12 +165,10 @@ export const ImageView = ({ block, editor }: ImageView) => {
}; };
return ( return (
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<ImageBlock> <ImageBlock>
<div style={{ position: 'relative' }} ref={resizeBox}> <div style={{ position: 'relative' }} ref={resizeBox}>
{!isSelect ? ( {!isSelect ? <ImageShade onClick={handleClick} /> : null}
<ImageShade onClick={handleClick}></ImageShade>
) : null}
{imgUrl ? ( {imgUrl ? (
<div <div
onMouseDown={e => { onMouseDown={e => {

View File

@ -183,7 +183,7 @@ export const NumberedView = ({ block, editor }: CreateView) => {
return ( return (
<BlockContainer editor={editor} block={block} selected={isSelect}> <BlockContainer editor={editor} block={block} selected={isSelect}>
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<List> <List>
<div className={'checkBoxContainer'}> <div className={'checkBoxContainer'}>
{getNumber(properties.numberType, number)}. {getNumber(properties.numberType, number)}.

View File

@ -1,14 +1,14 @@
import { useRef, useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useRef, useState } from 'react';
import { useParams } from 'react-router'; import { useParams } from 'react-router';
import { BackLink, TextProps } from '@toeverything/components/common'; import { BackLink, TextProps } from '@toeverything/components/common';
import { import {
RenderBlockChildren,
BlockPendantProvider, BlockPendantProvider,
RenderBlockChildren,
} from '@toeverything/components/editor-core'; } from '@toeverything/components/editor-core';
import { styled } from '@toeverything/components/ui';
import { ContentColumnValue } from '@toeverything/datasource/db-service'; import { ContentColumnValue } from '@toeverything/datasource/db-service';
import { CreateView } from '@toeverything/framework/virgo'; import { CreateView } from '@toeverything/framework/virgo';
import { Theme, styled } from '@toeverything/components/ui';
import { import {
TextManage, TextManage,
@ -81,7 +81,7 @@ export const PageView = ({ block, editor }: CreateView) => {
return ( return (
<PageTitleBlock> <PageTitleBlock>
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<TextManage <TextManage
alwaysShowPlaceholder alwaysShowPlaceholder
ref={textRef} ref={textRef}

View File

@ -242,7 +242,7 @@ export const TextView = ({
selected={isSelect} selected={isSelect}
className={containerClassName} className={containerClassName}
> >
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<TextBlock <TextBlock
block={block} block={block}
type={block.type} type={block.type}

View File

@ -132,7 +132,7 @@ export const TodoView = ({ block, editor }: CreateView) => {
return ( return (
<BlockContainer editor={editor} block={block} selected={isSelect}> <BlockContainer editor={editor} block={block} selected={isSelect}>
<BlockPendantProvider block={block}> <BlockPendantProvider editor={editor} block={block}>
<TodoBlock> <TodoBlock>
<div className={'checkBoxContainer'}> <div className={'checkBoxContainer'}>
<CheckBox <CheckBox

View File

@ -1,7 +1,12 @@
import { styled } from '@toeverything/components/ui'; import { styled } from '@toeverything/components/ui';
import type { PropsWithChildren } from 'react'; import { containerFlavor } from '@toeverything/datasource/db-service';
import { useRef } from 'react'; import {
import type { AsyncBlock } from '../editor'; useCallback,
useRef,
type MouseEvent,
type PropsWithChildren,
} from 'react';
import type { AsyncBlock, BlockEditor } from '../editor';
import { getRecastItemValue, useRecastBlockMeta } from '../recast-block'; import { getRecastItemValue, useRecastBlockMeta } from '../recast-block';
import { PendantPopover } from './pendant-popover'; import { PendantPopover } from './pendant-popover';
import { PendantRender } from './pendant-render'; import { PendantRender } from './pendant-render';
@ -9,10 +14,12 @@ import { PendantRender } from './pendant-render';
* @deprecated * @deprecated
*/ */
interface BlockTagProps { interface BlockTagProps {
editor: BlockEditor;
block: AsyncBlock; block: AsyncBlock;
} }
export const BlockPendantProvider = ({ export const BlockPendantProvider = ({
editor,
block, block,
children, children,
}: PropsWithChildren<BlockTagProps>) => { }: PropsWithChildren<BlockTagProps>) => {
@ -23,8 +30,23 @@ export const BlockPendantProvider = ({
const showTriggerLine = const showTriggerLine =
properties.filter(property => getValue(property.id)).length === 0; properties.filter(property => getValue(property.id)).length === 0;
const onClick = useCallback(
(e: MouseEvent<HTMLDivElement>) => {
if (containerFlavor.includes(block.type)) {
return;
}
if (e.target === e.currentTarget) {
const rect = e.currentTarget.getBoundingClientRect();
const middle = (rect.left + rect.right) / 2;
const position = e.clientX < middle ? 'start' : 'end';
editor.selectionManager.activeNodeByNodeId(block.id, position);
}
},
[editor, block]
);
return ( return (
<Container> <Container onClick={onClick}>
{children} {children}
{showTriggerLine ? ( {showTriggerLine ? (

View File

@ -196,11 +196,11 @@ export class ScrollManager {
private _getKeepInViewParams(blockRect: Rect) { private _getKeepInViewParams(blockRect: Rect) {
if (this.scrollContainer == null) return 0; 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) {
return -1; return -1;
} }
if (blockRect.bottom >= bottom - blockRect.height * 3) { if (blockRect.bottom >= bottom - blockRect.height) {
return 1; return 1;
} }
return 0; return 0;

View File

@ -1,3 +1,4 @@
import { containerFlavor } from '@toeverything/datasource/db-service';
import { BlockDropPlacement, HookType } from '@toeverything/framework/virgo'; import { BlockDropPlacement, HookType } from '@toeverything/framework/virgo';
import { domToRect, last, Point } from '@toeverything/utils'; import { domToRect, last, Point } from '@toeverything/utils';
import { StrictMode } from 'react'; import { StrictMode } from 'react';
@ -9,7 +10,6 @@ import {
LeftMenuDraggable, LeftMenuDraggable,
LineInfoSubject, LineInfoSubject,
} from './LeftMenuDraggable'; } from './LeftMenuDraggable';
import { ignoreBlockTypes } from './menu-config';
const DRAG_THROTTLE_DELAY = 60; const DRAG_THROTTLE_DELAY = 60;
export class LeftMenuPlugin extends BasePlugin { export class LeftMenuPlugin extends BasePlugin {
private _mousedown?: boolean; private _mousedown?: boolean;
@ -104,7 +104,7 @@ export class LeftMenuPlugin extends BasePlugin {
const block = await this.editor.getBlockByPoint( const block = await this.editor.getBlockByPoint(
new Point(event.clientX, event.clientY) new Point(event.clientX, event.clientY)
); );
if (block == null || ignoreBlockTypes.includes(block.type)) return; if (block == null || containerFlavor.includes(block.type)) return;
const { direction, block: targetBlock } = const { direction, block: targetBlock } =
await this.editor.dragDropManager.checkBlockDragTypes( await this.editor.dragDropManager.checkBlockDragTypes(
event, event,
@ -143,7 +143,7 @@ export class LeftMenuPlugin extends BasePlugin {
const node = await this.editor.getBlockByPoint( const node = await this.editor.getBlockByPoint(
new Point(event.clientX, event.clientY) new Point(event.clientX, event.clientY)
); );
if (node == null || ignoreBlockTypes.includes(node.type)) { if (node == null || containerFlavor.includes(node.type)) {
return; return;
} }
if (node.dom) { if (node.dom) {

View File

@ -1,14 +1,14 @@
import { BlockFlavorKeys, Protocol } from '@toeverything/datasource/db-service';
import ShortTextIcon from '@mui/icons-material/ShortText';
import TitleIcon from '@mui/icons-material/Title';
import FormatListBulletedIcon from '@mui/icons-material/FormatListBulleted'; import FormatListBulletedIcon from '@mui/icons-material/FormatListBulleted';
import HorizontalRuleIcon from '@mui/icons-material/HorizontalRule'; import HorizontalRuleIcon from '@mui/icons-material/HorizontalRule';
import ListIcon from '@mui/icons-material/List';
import NotificationsNoneIcon from '@mui/icons-material/NotificationsNone'; import NotificationsNoneIcon from '@mui/icons-material/NotificationsNone';
import ShortTextIcon from '@mui/icons-material/ShortText';
import TitleIcon from '@mui/icons-material/Title';
import { import {
CodeBlockInlineIcon, CodeBlockInlineIcon,
PagesIcon, PagesIcon,
} from '@toeverything/components/common'; } from '@toeverything/components/common';
import ListIcon from '@mui/icons-material/List'; import { Protocol } from '@toeverything/datasource/db-service';
export const MENU_WIDTH = 14; export const MENU_WIDTH = 14;
export const pageConvertIconSize = 24; export const pageConvertIconSize = 24;
type MenuItem = { type MenuItem = {
@ -93,12 +93,3 @@ const textTypeBlocks: MenuItem[] = [
export const addMenuList = [...textTypeBlocks].filter(v => v); export const addMenuList = [...textTypeBlocks].filter(v => v);
export const textConvertMenuList = textTypeBlocks; export const textConvertMenuList = textTypeBlocks;
export const ignoreBlockTypes: BlockFlavorKeys[] = [
Protocol.Block.Type.workspace,
Protocol.Block.Type.page,
Protocol.Block.Type.group,
Protocol.Block.Type.title,
Protocol.Block.Type.grid,
Protocol.Block.Type.gridItem,
];

View File

@ -42,6 +42,7 @@ export {
type TemplateMeta, type TemplateMeta,
} from './services/editor-block/templates'; } from './services/editor-block/templates';
export type { Template } from './services/editor-block/templates/types'; export type { Template } from './services/editor-block/templates/types';
export { containerFlavor } from './services/editor-block/types';
export { DEFAULT_COLUMN_KEYS } from './services/editor-block/utils/column/default-config'; export { DEFAULT_COLUMN_KEYS } from './services/editor-block/utils/column/default-config';
const api = new Proxy<DbServicesMap>({} as DbServicesMap, { const api = new Proxy<DbServicesMap>({} as DbServicesMap, {

View File

@ -4,6 +4,15 @@ import { Column, DefaultColumnsValue } from './utils/column';
export type BlockFlavors = typeof Protocol.Block.Type; export type BlockFlavors = typeof Protocol.Block.Type;
export type BlockFlavorKeys = keyof typeof Protocol.Block.Type; export type BlockFlavorKeys = keyof typeof Protocol.Block.Type;
export const containerFlavor: BlockFlavorKeys[] = [
Protocol.Block.Type.workspace,
Protocol.Block.Type.page,
Protocol.Block.Type.group,
Protocol.Block.Type.title,
Protocol.Block.Type.grid,
Protocol.Block.Type.gridItem,
];
export interface CreateEditorBlock { export interface CreateEditorBlock {
workspace: string; workspace: string;
type: keyof BlockFlavors; type: keyof BlockFlavors;