Skip to content

Commit

Permalink
Stop Slate throwing exceptions for things it could just ignore.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkelly committed Apr 27, 2020
1 parent 213b1fd commit 2f9ad08
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/slate-react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slate-react",
"description": "Tools for building completely customizable richtext editors with React.",
"version": "0.57.2",
"version": "0.57.3",
"license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git",
"main": "dist/index.js",
Expand Down
14 changes: 10 additions & 4 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ export const Editable = (props: EditableProps) => {

if (targetRange) {
const range = ReactEditor.toSlateRange(editor, targetRange)

if (!selection || !Range.equals(selection, range)) {
Transforms.select(editor, range)
if (range) {
if (!selection || !Range.equals(selection, range)) {
Transforms.select(editor, range)
}
}
}
}
Expand Down Expand Up @@ -390,7 +391,9 @@ export const Editable = (props: EditableProps) => {
hasEditableTarget(editor, domRange.endContainer)
) {
const range = ReactEditor.toSlateRange(editor, domRange)
Transforms.select(editor, range)
if (range) {
Transforms.select(editor, range)
}
} else {
Transforms.deselect(editor)
}
Expand Down Expand Up @@ -682,6 +685,9 @@ export const Editable = (props: EditableProps) => {
) {
event.preventDefault()
const range = ReactEditor.findEventRange(editor, event)
if (!range) {
return
}
const data = event.dataTransfer
Transforms.select(editor, range)
ReactEditor.insertData(editor, data)
Expand Down
28 changes: 18 additions & 10 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export const ReactEditor = {
* Get the target range from a DOM `event`.
*/

findEventRange(editor: ReactEditor, event: any): Range {
findEventRange(editor: ReactEditor, event: any): Range | null {
if ('nativeEvent' in event) {
event = event.nativeEvent
}
Expand Down Expand Up @@ -378,14 +378,15 @@ export const ReactEditor = {

// Resolve a Slate range from the DOM range.
const range = ReactEditor.toSlateRange(editor, domRange)

return range
},

/**
* Find a Slate point from a DOM selection's `domNode` and `domOffset`.
*/

toSlatePoint(editor: ReactEditor, domPoint: DOMPoint): Point {
toSlatePoint(editor: ReactEditor, domPoint: DOMPoint): Point | null {
const [nearestNode, nearestOffset] = normalizeDOMPoint(domPoint)
const parentNode = nearestNode.parentNode as DOMElement
let textNode: DOMElement | null = null
Expand Down Expand Up @@ -445,9 +446,10 @@ export const ReactEditor = {
}

if (!textNode) {
throw new Error(
`Cannot resolve a Slate point from DOM point: ${domPoint}`
)
return null
// throw new Error(
// `Cannot resolve a Slate point from DOM point: ${domPoint}`
// )
}

// COMPAT: If someone is clicking from one Slate editor into another,
Expand All @@ -465,7 +467,7 @@ export const ReactEditor = {
toSlateRange(
editor: ReactEditor,
domRange: DOMRange | DOMStaticRange | DOMSelection
): Range {
): Range | null {
const el =
domRange instanceof Selection
? domRange.anchorNode
Expand Down Expand Up @@ -498,16 +500,22 @@ export const ReactEditor = {
anchorOffset == null ||
focusOffset == null
) {
throw new Error(
`Cannot resolve a Slate range from DOM range: ${domRange}`
)
return null
// throw new Error(
// `Cannot resolve a Slate range from DOM range: ${domRange}`
// )
}

const anchor = ReactEditor.toSlatePoint(editor, [anchorNode, anchorOffset])
if (!anchor) {
return null
}
const focus = isCollapsed
? anchor
: ReactEditor.toSlatePoint(editor, [focusNode, focusOffset])

if (!focus) {
return null
}
return { anchor, focus }
},
}
2 changes: 1 addition & 1 deletion packages/slate/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slate",
"description": "A completely customizable framework for building rich text editors.",
"version": "0.57.1",
"version": "0.57.3",
"license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git",
"main": "dist/index.js",
Expand Down

0 comments on commit 2f9ad08

Please sign in to comment.