1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 18:53:59 +02:00

fix typescript errors

This commit is contained in:
Ian Storm Taylor
2021-04-01 00:43:24 -04:00
parent 6712e6fbc9
commit d288842aee
5 changed files with 31 additions and 23 deletions

View File

@@ -252,15 +252,7 @@ export const Editable = (props: EditableProps) => {
// to the real event sadly. (2019/11/01) // to the real event sadly. (2019/11/01)
// https://github.com/facebook/react/issues/11211 // https://github.com/facebook/react/issues/11211
const onDOMBeforeInput = useCallback( const onDOMBeforeInput = useCallback(
( (event: InputEvent) => {
event: Event & {
data: string | null
dataTransfer: DataTransfer | null
getTargetRanges(): DOMStaticRange[]
inputType: string
isComposing: boolean
}
) => {
if ( if (
!readOnly && !readOnly &&
hasEditableTarget(editor, event.target) && hasEditableTarget(editor, event.target) &&
@@ -268,7 +260,7 @@ export const Editable = (props: EditableProps) => {
) { ) {
const { selection } = editor const { selection } = editor
const { inputType: type } = event const { inputType: type } = event
const data = event.dataTransfer || event.data || undefined const data = (event as any).dataTransfer || event.data || undefined
// These two types occur while a user is composing text and can't be // These two types occur while a user is composing text and can't be
// cancelled. Let them through and wait for the composition to end. // cancelled. Let them through and wait for the composition to end.
@@ -285,7 +277,7 @@ export const Editable = (props: EditableProps) => {
// to change the selection because it is the range that will be deleted, // to change the selection because it is the range that will be deleted,
// and those commands determine that for themselves. // and those commands determine that for themselves.
if (!type.startsWith('delete') || type.startsWith('deleteBy')) { if (!type.startsWith('delete') || type.startsWith('deleteBy')) {
const [targetRange] = event.getTargetRanges() const [targetRange] = (event as any).getTargetRanges()
if (targetRange) { if (targetRange) {
const range = ReactEditor.toSlateRange(editor, targetRange) const range = ReactEditor.toSlateRange(editor, targetRange)
@@ -304,7 +296,7 @@ export const Editable = (props: EditableProps) => {
type.startsWith('delete') type.startsWith('delete')
) { ) {
const direction = type.endsWith('Backward') ? 'backward' : 'forward' const direction = type.endsWith('Backward') ? 'backward' : 'forward'
Editor.deleteFragment(editor, direction) Editor.deleteFragment(editor, { direction })
return return
} }
@@ -949,7 +941,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'backward') Editor.deleteFragment(editor, { direction: 'backward' })
} else { } else {
Editor.deleteBackward(editor) Editor.deleteBackward(editor)
} }
@@ -961,7 +953,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'forward') Editor.deleteFragment(editor, { direction: 'forward' })
} else { } else {
Editor.deleteForward(editor) Editor.deleteForward(editor)
} }
@@ -973,7 +965,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'backward') Editor.deleteFragment(editor, { direction: 'backward' })
} else { } else {
Editor.deleteBackward(editor, { unit: 'line' }) Editor.deleteBackward(editor, { unit: 'line' })
} }
@@ -985,7 +977,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'forward') Editor.deleteFragment(editor, { direction: 'forward' })
} else { } else {
Editor.deleteForward(editor, { unit: 'line' }) Editor.deleteForward(editor, { unit: 'line' })
} }
@@ -997,7 +989,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'backward') Editor.deleteFragment(editor, { direction: 'backward' })
} else { } else {
Editor.deleteBackward(editor, { unit: 'word' }) Editor.deleteBackward(editor, { unit: 'word' })
} }
@@ -1009,7 +1001,7 @@ export const Editable = (props: EditableProps) => {
event.preventDefault() event.preventDefault()
if (selection && Range.isExpanded(selection)) { if (selection && Range.isExpanded(selection)) {
Editor.deleteFragment(editor, 'forward') Editor.deleteFragment(editor, { direction: 'forward' })
} else { } else {
Editor.deleteForward(editor, { unit: 'word' }) Editor.deleteForward(editor, { unit: 'word' })
} }
@@ -1140,7 +1132,10 @@ const isEventHandled = <
* Check if a DOM event is overrided by a handler. * Check if a DOM event is overrided by a handler.
*/ */
const isDOMEventHandled = (event: Event, handler?: (event: Event) => void) => { const isDOMEventHandled = <E extends Event>(
event: E,
handler?: (event: E) => void
) => {
if (!handler) { if (!handler) {
return false return false
} }

View File

@@ -40,8 +40,7 @@ const Element = (props: {
const readOnly = useReadOnly() const readOnly = useReadOnly()
const isInline = editor.isInline(element) const isInline = editor.isInline(element)
const key = ReactEditor.findKey(editor, element) const key = ReactEditor.findKey(editor, element)
let children: React.ReactNode = useChildren({
let children: JSX.Element | null = useChildren({
decorations, decorations,
node: element, node: element,
renderElement, renderElement,

View File

@@ -4,6 +4,7 @@ import { CAN_USE_DOM } from '../utils/environment'
/** /**
* Prevent warning on SSR by falling back to useEffect when DOM isn't available * Prevent warning on SSR by falling back to useEffect when DOM isn't available
*/ */
export const useIsomorphicLayoutEffect = CAN_USE_DOM export const useIsomorphicLayoutEffect = CAN_USE_DOM
? useLayoutEffect ? useLayoutEffect
: useEffect : useEffect

View File

@@ -22,6 +22,7 @@ import {
isDOMElement, isDOMElement,
isDOMSelection, isDOMSelection,
normalizeDOMPoint, normalizeDOMPoint,
hasShadowRoot,
} from '../utils/dom' } from '../utils/dom'
import { IS_CHROME } from '../utils/environment' import { IS_CHROME } from '../utils/environment'

View File

@@ -106,13 +106,19 @@ export interface EditorInterface {
unit?: 'character' | 'word' | 'line' | 'block' unit?: 'character' | 'word' | 'line' | 'block'
} }
) => void ) => void
deleteFragment: (editor: Editor) => void deleteFragment: (
editor: Editor,
options?: {
direction?: 'forward' | 'backward'
}
) => void
edges: (editor: Editor, at: Location) => [Point, Point] edges: (editor: Editor, at: Location) => [Point, Point]
end: (editor: Editor, at: Location) => Point end: (editor: Editor, at: Location) => Point
first: (editor: Editor, at: Location) => NodeEntry first: (editor: Editor, at: Location) => NodeEntry
fragment: (editor: Editor, at: Location) => Descendant[] fragment: (editor: Editor, at: Location) => Descendant[]
hasBlocks: (editor: Editor, element: Element) => boolean hasBlocks: (editor: Editor, element: Element) => boolean
hasInlines: (editor: Editor, element: Element) => boolean hasInlines: (editor: Editor, element: Element) => boolean
hasPath: (editor: Editor, path: Path) => boolean
hasTexts: (editor: Editor, element: Element) => boolean hasTexts: (editor: Editor, element: Element) => boolean
insertBreak: (editor: Editor) => void insertBreak: (editor: Editor) => void
insertFragment: (editor: Editor, fragment: Node[]) => void insertFragment: (editor: Editor, fragment: Node[]) => void
@@ -435,7 +441,13 @@ export const Editor: EditorInterface = {
* Delete the content in the current selection. * Delete the content in the current selection.
*/ */
deleteFragment(editor: Editor, direction?: 'forward' | 'backward'): void { deleteFragment(
editor: Editor,
options: {
direction?: 'forward' | 'backward'
} = {}
): void {
const { direction = 'forward' } = options
editor.deleteFragment(direction) editor.deleteFragment(direction)
}, },