fix: adaptation of editor changes and restoration of scrollManager functionality

This commit is contained in:
qishaoxuan
2022-07-27 15:53:20 +08:00
parent c4b1c10ffc
commit 500a68df7a
4 changed files with 42 additions and 19 deletions
@@ -1,5 +1,5 @@
/* eslint-disable filename-rules/match */ /* eslint-disable filename-rules/match */
import { useEffect, useRef } from 'react'; import { useEffect, useRef, type UIEvent } from 'react';
import { useParams } from 'react-router'; import { useParams } from 'react-router';
import { import {
MuiBox as Box, MuiBox as Box,
@@ -37,7 +37,8 @@ export function Page(props: PageProps) {
const { user } = useUserAndSpaces(); const { user } = useUserAndSpaces();
const templatesPortalFlag = useFlag('BooleanTemplatesPortal', false); const templatesPortalFlag = useFlag('BooleanTemplatesPortal', false);
const dailyNotesFlag = useFlag('BooleanDailyNotes', false); const dailyNotesFlag = useFlag('BooleanDailyNotes', false);
const editor = useRef<BlockEditor>(); const editorRef = useRef<BlockEditor>();
const scrollContainerRef = useRef();
useEffect(() => { useEffect(() => {
if (!user?.id || !page_id) return; if (!user?.id || !page_id) return;
@@ -60,6 +61,13 @@ export function Page(props: PageProps) {
updateRecentPages(); updateRecentPages();
}, [user, props.workspace, page_id]); }, [user, props.workspace, page_id]);
const onScroll = (event: UIEvent) => {
editorRef.current.scrollManager.scrollContainer =
scrollContainerRef.current;
editorRef.current.getHooks().onRootNodeScroll(event);
editorRef.current.scrollManager.emitScrollEvent(event);
};
return ( return (
<LigoApp> <LigoApp>
<LigoLeftContainer style={{ width: fixedDisplay ? '300px' : 0 }}> <LigoLeftContainer style={{ width: fixedDisplay ? '300px' : 0 }}>
@@ -104,12 +112,12 @@ export function Page(props: PageProps) {
</WorkspaceSidebarContent> </WorkspaceSidebarContent>
</WorkspaceSidebar> </WorkspaceSidebar>
</LigoLeftContainer> </LigoLeftContainer>
<LigoRightContainer> <LigoRightContainer ref={scrollContainerRef} onScroll={onScroll}>
{page_id ? ( {page_id ? (
<AffineEditor <AffineEditor
workspace={props.workspace} workspace={props.workspace}
rootBlockId={page_id} rootBlockId={page_id}
ref={editor} ref={editorRef}
/> />
) : ( ) : (
<Box <Box
@@ -133,4 +133,8 @@ export class Hooks implements HooksRunner, PluginHooks {
public beforeCut(e: ClipboardEvent): void { public beforeCut(e: ClipboardEvent): void {
this._runHook(HookType.BEFORE_CUT, e); this._runHook(HookType.BEFORE_CUT, e);
} }
public onRootNodeScroll(e: React.UIEvent): void {
this._runHook(HookType.ON_ROOTNODE_SCROLL, e);
}
} }
@@ -1,3 +1,4 @@
import { type UIEvent } from 'react';
import EventEmitter from 'eventemitter3'; import EventEmitter from 'eventemitter3';
import { domToRect, Rect } from '@toeverything/utils'; import { domToRect, Rect } from '@toeverything/utils';
@@ -9,7 +10,8 @@ type VerticalTypes = 'up' | 'down' | null;
type HorizontalTypes = 'left' | 'right' | null; type HorizontalTypes = 'left' | 'right' | null;
export class ScrollManager { export class ScrollManager {
private _editor: BlockEditor; private _editor: Block_editor;
private _scrollContainer: HTMLElement;
private _animationFrame: null | number = null; private _animationFrame: null | number = null;
private _eventName = 'scrolling'; private _eventName = 'scrolling';
private _currentMoveDirection: [HorizontalTypes, VerticalTypes] = [ private _currentMoveDirection: [HorizontalTypes, VerticalTypes] = [
@@ -41,7 +43,10 @@ export class ScrollManager {
} }
public get scrollContainer() { public get scrollContainer() {
return this._editor.ui_container; return this._scrollContainer;
}
public set scrollContainer(dom: HTMLElement) {
this._scrollContainer = dom;
} }
public get verticalScrollTriggerDistance() { public get verticalScrollTriggerDistance() {
@@ -53,22 +58,26 @@ export class ScrollManager {
} }
public get scrollTop() { public get scrollTop() {
return this._editor.ui_container.scrollTop; return this._scrollContainer.scrollTop;
} }
public set scrollTop(top: number) { public set scrollTop(top: number) {
this._editor.ui_container.scrollTop = top; this._scrollContainer.scrollTop = top;
} }
public get scrollLeft() { public get scrollLeft() {
return this._editor.ui_container.scrollLeft; return this._scrollContainer.scrollLeft;
} }
public set scrollLeft(left: number) { public set scrollLeft(left: number) {
this._editor.ui_container.scrollLeft = left; this._scrollContainer.scrollLeft = left;
} }
public get scrollMoveOffset() { public get scrollMoveOffset() {
return this._scrollMoveOffset; return this._scrollMoveOffset;
} }
public get scrollingEvent() {
return this._scrollingEvent; public emitScrollEvent(event: UIEvent) {
this._scrollingEvent.emit(this._eventName, {
direction: this._currentMoveDirection,
event,
});
} }
public scrollTo({ public scrollTo({
@@ -84,13 +93,13 @@ export class ScrollManager {
left = left !== undefined ? left : this.scrollContainer.scrollLeft; left = left !== undefined ? left : this.scrollContainer.scrollLeft;
if (behavior === 'smooth') { if (behavior === 'smooth') {
this._editor.ui_container.scrollBy({ this._scrollContainer.scrollBy({
top, top,
left, left,
behavior, behavior,
}); });
} else { } else {
this._editor.ui_container.scrollTo(left, top); this._scrollContainer.scrollTo(left, top);
} }
} }
@@ -110,7 +119,7 @@ export class ScrollManager {
if (!block.dom) { if (!block.dom) {
return console.warn(`Block is not exist.`); return console.warn(`Block is not exist.`);
} }
const containerRect = domToRect(this._editor.ui_container); const containerRect = domToRect(this._scrollContainer);
const blockRect = domToRect(block.dom); const blockRect = domToRect(block.dom);
const blockRelativeTopToEditor = const blockRelativeTopToEditor =
@@ -153,7 +162,7 @@ export class ScrollManager {
} }
private _getKeepInViewParams(blockRect: Rect) { private _getKeepInViewParams(blockRect: Rect) {
const { top, bottom } = domToRect(this._editor.ui_container); const { top, bottom } = domToRect(this._scrollContainer);
if (blockRect.top <= top + blockRect.height * 3) { if (blockRect.top <= top + blockRect.height * 3) {
return -1; return -1;
} }
@@ -218,9 +227,9 @@ export class ScrollManager {
behavior: 'auto', behavior: 'auto',
}); });
this._updateScrollInfo(left, top); this._updateScrollInfo(left, top);
this._scrollingEvent.emit(this._eventName, { // this._scrollingEvent.emit(this._eventName, {
direction: this._currentMoveDirection, // direction: this._currentMoveDirection,
}); // });
this._autoScroll(); this._autoScroll();
}); });
} }
@@ -175,6 +175,7 @@ export enum HookType {
AFTER_ON_NODE_DRAG_OVER = 'afterOnNodeDragOver', AFTER_ON_NODE_DRAG_OVER = 'afterOnNodeDragOver',
BEFORE_COPY = 'beforeCopy', BEFORE_COPY = 'beforeCopy',
BEFORE_CUT = 'beforeCut', BEFORE_CUT = 'beforeCut',
ON_ROOTNODE_SCROLL = 'onRootNodeScroll',
} }
export interface BlockDomInfo { export interface BlockDomInfo {
@@ -222,6 +223,7 @@ export interface HooksRunner {
) => void; ) => void;
beforeCopy: (e: ClipboardEvent) => void; beforeCopy: (e: ClipboardEvent) => void;
beforeCut: (e: ClipboardEvent) => void; beforeCut: (e: ClipboardEvent) => void;
onRootNodeScroll: (e: React.UIEvent) => void;
} }
export type PayloadType<T extends Array<any>> = T extends [] export type PayloadType<T extends Array<any>> = T extends []