Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat * feat * feat * feat * Create witty-carrots-heal.md * feat * Update witty-carrots-heal.md * feat
- Loading branch information
Showing
16 changed files
with
500 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@udecode/plate-ui-dnd": minor | ||
--- | ||
|
||
dnd plugin - new options: | ||
- `enableScroller`: this adds a scroll area at the top and bottom of the window so the document scrolls when the mouse drags over. If you have another scroll container, you can either keep it disabled or override the props so the scroll areas are correctly positioned. | ||
- `scrollerProps` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import { dndStore } from '../../dndStore'; | ||
import { Scroller, ScrollerProps } from './Scroller'; | ||
|
||
export const DndScroller = (props: Partial<ScrollerProps>) => { | ||
const isDragging = dndStore.use.isDragging(); | ||
|
||
return <Scroller enabled={isDragging} {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import React, { | ||
CSSProperties, | ||
HTMLAttributes, | ||
RefObject, | ||
useEffect, | ||
useRef, | ||
} from 'react'; | ||
import { throttle } from 'lodash'; | ||
import raf from 'raf'; | ||
|
||
const getCoords = (e: any) => { | ||
if (e.type === 'touchmove') { | ||
return { x: e.changedTouches[0].clientX, y: e.changedTouches[0].clientY }; | ||
} | ||
|
||
return { x: e.clientX, y: e.clientY }; | ||
}; | ||
|
||
export interface ScrollAreaProps { | ||
placement: 'top' | 'bottom'; | ||
enabled?: boolean; | ||
height?: number; | ||
zIndex?: number; | ||
minStrength?: number; | ||
strengthMultiplier?: number; | ||
containerRef?: RefObject<any>; | ||
scrollAreaProps?: HTMLAttributes<HTMLDivElement>; | ||
} | ||
|
||
export const ScrollArea = ({ | ||
placement, | ||
enabled = true, | ||
height = 100, | ||
zIndex = 10000, | ||
minStrength = 0.15, | ||
strengthMultiplier = 25, | ||
containerRef, | ||
scrollAreaProps, | ||
}: ScrollAreaProps) => { | ||
const ref = useRef<HTMLDivElement>(); | ||
|
||
const scaleYRef = useRef(0); | ||
const frameRef = useRef<number | null>(null); | ||
|
||
const direction = placement === 'top' ? -1 : 1; | ||
|
||
// Drag a fixed, invisible box of custom height at the top, and bottom | ||
// of the window. Make sure to show it only when dragging something. | ||
const style: CSSProperties = { | ||
position: 'fixed', | ||
height, | ||
width: '100%', | ||
opacity: 0, | ||
zIndex, | ||
...scrollAreaProps?.style, | ||
}; | ||
|
||
if (placement === 'top') { | ||
style.top = 0; | ||
} else if (placement === 'bottom') { | ||
style.bottom = 0; | ||
} | ||
|
||
const stopScrolling = () => { | ||
scaleYRef.current = 0; | ||
|
||
if (frameRef.current) { | ||
raf.cancel(frameRef.current); | ||
frameRef.current = null; | ||
} | ||
}; | ||
|
||
const startScrolling = () => { | ||
const tick = () => { | ||
const scaleY = scaleYRef.current; | ||
|
||
// stop scrolling if there's nothing to do | ||
if (strengthMultiplier === 0 || scaleY === 0) { | ||
stopScrolling(); | ||
return; | ||
} | ||
|
||
const container = containerRef?.current ?? window; | ||
container.scrollBy(0, scaleY * strengthMultiplier * direction); | ||
|
||
frameRef.current = raf(tick); | ||
|
||
// there's a bug in safari where it seems like we can't get | ||
// mousemove events from a container that also emits a scroll | ||
// event that same frame. So we should double the strengthMultiplier and only adjust | ||
// the scroll position at 30fps | ||
}; | ||
|
||
tick(); | ||
}; | ||
|
||
// Update scaleY every 100ms or so | ||
// and start scrolling if necessary | ||
const updateScrolling = throttle( | ||
(e) => { | ||
const container = ref.current; | ||
if (!container) return; | ||
|
||
const { top: y, height: h } = container.getBoundingClientRect(); | ||
const coords = getCoords(e); | ||
|
||
const strength = Math.max(Math.max(coords.y - y, 0) / h, minStrength); | ||
|
||
// calculate strength | ||
scaleYRef.current = direction === -1 ? 1 - strength : strength; | ||
|
||
// start scrolling if we need to | ||
if (!frameRef.current && scaleYRef.current) { | ||
startScrolling(); | ||
} | ||
}, | ||
100, | ||
{ trailing: false } | ||
); | ||
|
||
const handleEvent = (e: any) => { | ||
updateScrolling(e); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!enabled) { | ||
stopScrolling(); | ||
} | ||
}, [enabled]); | ||
|
||
if (!enabled) return null; | ||
|
||
// Hide the element if not enabled, so it doesn't interfere with clicking things under it. | ||
return ( | ||
<div | ||
ref={ref as any} | ||
style={style} | ||
onDragOver={handleEvent} | ||
onDragLeave={stopScrolling} | ||
onDragEnd={stopScrolling} | ||
// touchmove events don't seem to work across siblings, so we unfortunately | ||
// would have to attach the listeners to the body | ||
onTouchMove={handleEvent} | ||
{...scrollAreaProps} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import { ScrollArea, ScrollAreaProps } from './ScrollArea'; | ||
|
||
export type ScrollerProps = Omit<ScrollAreaProps, 'placement'>; | ||
/** | ||
* Set up an edge scroller at the top of the page for scrolling up. | ||
* One at the bottom for scrolling down. | ||
*/ | ||
export const Scroller = (props: ScrollerProps) => { | ||
return ( | ||
<> | ||
<ScrollArea placement="top" {...props} /> | ||
<ScrollArea placement="bottom" {...props} /> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './DndScroller'; | ||
export * from './ScrollArea'; | ||
export * from './Scroller'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { createPluginFactory } from '@udecode/plate-core'; | ||
import { DndScroller, ScrollerProps } from './components/index'; | ||
import { dndStore } from './dndStore'; | ||
|
||
export interface DndPlugin { | ||
enableScroller?: boolean; | ||
scrollerProps?: Partial<ScrollerProps>; | ||
} | ||
|
||
export const KEY_DND = 'dnd'; | ||
|
||
export const createDndPlugin = createPluginFactory<DndPlugin>({ | ||
key: KEY_DND, | ||
handlers: { | ||
onDragStart: () => () => dndStore.set.isDragging(true), | ||
onDragEnd: () => () => dndStore.set.isDragging(false), | ||
onDrop: (editor) => () => editor.isDragging as boolean, | ||
}, | ||
then: (editor, { options }) => ({ | ||
renderAfterEditable: options.enableScroller | ||
? () => <DndScroller {...options?.scrollerProps} /> | ||
: undefined, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createStore } from '@udecode/plate-core'; | ||
|
||
export const dndStore = createStore('dnd')({ | ||
isDragging: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7e0e9c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
plate-examples – ./
plate-examples-git-main-udecode.vercel.app
plate-examples-udecode.vercel.app
plate-examples.vercel.app
7e0e9c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
plate – ./
plate.udecode.io
plate-udecode.vercel.app
www.plate.udecode.io
plate-git-main-udecode.vercel.app