WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/rich-cheetahs-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'slate': minor
---

Add `Editor.isEditorNode`, `Element.isElementNode`, and `Text.isTextNode` as alternative type guards for when we already know the object is a node.
Use these new functions instead of `isEditor`, `isElement`, and `isText` whenever possible, the classic functions are only necessary for typechecking an entirely unknown object.
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Should we also change the docs to refer to the new type guards by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely think so, user code should only need the old functions for serializing/deserializing etc, basically call match functions etc are better off with the new ones.

Just didn't want to cement them in too deep before getting feedback on the naming. Thankfully old code still functions, just inefficiently (equally as inefficiently as before, to be precise)

===
21 changes: 11 additions & 10 deletions packages/slate-dom/src/plugin/dom-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export const DOMEditor: DOMEditorInterface = {
// If the drop target is inside a void node, move it into either the
// next or previous node, depending on which side the `x` and `y`
// coordinates are closest to.
if (Element.isElement(node) && Editor.isVoid(editor, node)) {
if (Element.isElementNode(node) && Editor.isVoid(editor, node)) {
const rect = target.getBoundingClientRect()
const isPrev = editor.isInline(node)
? x - rect.left < rect.left + rect.width - x
Expand Down Expand Up @@ -396,7 +396,7 @@ export const DOMEditor: DOMEditorInterface = {
const parent = NODE_TO_PARENT.get(child)

if (parent == null) {
if (Editor.isEditor(child)) {
if (child === editor) {
return path
} else {
break
Expand Down Expand Up @@ -548,21 +548,22 @@ export const DOMEditor: DOMEditorInterface = {

isTargetInsideNonReadonlyVoid: (editor, target) => {
if (IS_READ_ONLY.get(editor)) return false
if (!DOMEditor.hasTarget(editor, target)) return false

const slateNode =
DOMEditor.hasTarget(editor, target) &&
DOMEditor.toSlateNode(editor, target)
return Element.isElement(slateNode) && Editor.isVoid(editor, slateNode)
const slateNode = DOMEditor.toSlateNode(editor, target)
return Element.isElementNode(slateNode) && Editor.isVoid(editor, slateNode)
},

setFragmentData: (editor, data, originEvent) =>
editor.setFragmentData(data, originEvent),

toDOMNode: (editor, node) => {
const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor)
const domNode = Editor.isEditor(node)
? EDITOR_TO_ELEMENT.get(editor)
: KEY_TO_ELEMENT?.get(DOMEditor.findKey(editor, node))
const domNode =
node === editor
? EDITOR_TO_ELEMENT.get(editor)
: EDITOR_TO_KEY_TO_ELEMENT.get(editor)?.get(
DOMEditor.findKey(editor, node)
)

if (!domNode) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-dom/src/plugin/with-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const withDOM = <T extends BaseEditor>(

if (e.selection && Range.isCollapsed(e.selection)) {
const parentBlockEntry = Editor.above(e, {
match: n => Element.isElement(n) && Editor.isBlock(e, n),
match: n => Element.isElementNode(n) && Editor.isBlock(e, n),
at: e.selection,
})

Expand Down
8 changes: 4 additions & 4 deletions packages/slate-dom/src/utils/diff-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function verifyDiffState(editor: Editor, textDiff: TextDiff): boolean {
}

const node = Node.get(editor, path)
if (!Text.isText(node)) {
if (!Text.isTextNode(node)) {
return false
}

Expand All @@ -49,7 +49,7 @@ export function verifyDiffState(editor: Editor, textDiff: TextDiff): boolean {
}

const nextNode = Node.get(editor, nextPath)
return Text.isText(nextNode) && nextNode.text.startsWith(diff.text)
return Text.isTextNode(nextNode) && nextNode.text.startsWith(diff.text)
}

export function applyStringDiff(text: string, ...diffs: StringDiff[]) {
Expand Down Expand Up @@ -170,12 +170,12 @@ export function normalizePoint(editor: Editor, point: Point): Point | null {
}

let leaf = Node.get(editor, path)
if (!Text.isText(leaf)) {
if (!Text.isTextNode(leaf)) {
return null
}

const parentBlock = Editor.above(editor, {
match: n => Element.isElement(n) && Editor.isBlock(editor, n),
match: n => Element.isElementNode(n) && Editor.isBlock(editor, n),
at: path,
})

Expand Down
16 changes: 9 additions & 7 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,8 @@ export const Editable = forwardRef(
) {
const block = Editor.above(editor, {
at: anchor.path,
match: n => Element.isElement(n) && Editor.isBlock(editor, n),
match: n =>
Element.isElementNode(n) && Editor.isBlock(editor, n),
})

if (block && Node.string(block[0]).includes('\t')) {
Expand Down Expand Up @@ -1186,7 +1187,7 @@ export const Editable = forwardRef(
relatedTarget
)

if (Element.isElement(node) && !editor.isVoid(node)) {
if (Element.isElementNode(node) && !editor.isVoid(node)) {
return
}
}
Expand Down Expand Up @@ -1234,13 +1235,14 @@ export const Editable = forwardRef(
let blockPath = path
if (
!(
Element.isElement(node) &&
Element.isElementNode(node) &&
Editor.isBlock(editor, node)
)
) {
const block = Editor.above(editor, {
match: n =>
Element.isElement(n) && Editor.isBlock(editor, n),
Element.isElementNode(n) &&
Editor.isBlock(editor, n),
at: path,
})

Expand Down Expand Up @@ -1436,7 +1438,7 @@ export const Editable = forwardRef(
const node = ReactEditor.toSlateNode(editor, event.target)

if (
Element.isElement(node) &&
Element.isElementNode(node) &&
Editor.isVoid(editor, node)
) {
event.preventDefault()
Expand All @@ -1455,7 +1457,7 @@ export const Editable = forwardRef(
const node = ReactEditor.toSlateNode(editor, event.target)
const path = ReactEditor.findPath(editor, node)
const voidMatch =
(Element.isElement(node) &&
(Element.isElementNode(node) &&
Editor.isVoid(editor, node)) ||
Editor.void(editor, { at: path, voids: true })

Expand Down Expand Up @@ -1836,7 +1838,7 @@ export const Editable = forwardRef(
)

if (
Element.isElement(currentNode) &&
Element.isElementNode(currentNode) &&
Editor.isVoid(editor, currentNode) &&
(Editor.isInline(editor, currentNode) ||
Editor.isBlock(editor, currentNode))
Expand Down
7 changes: 4 additions & 3 deletions packages/slate-react/src/hooks/use-children.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ const useChildren = (props: {
const editor = useSlateStatic()
IS_NODE_MAP_DIRTY.set(editor as ReactEditor, false)

const isEditor = Editor.isEditor(node)
const isBlock = !isEditor && Element.isElement(node) && !editor.isInline(node)
const isBlock = Element.isElementNode(node) && !editor.isInline(node)
const isLeafBlock = isBlock && Editor.hasInlines(editor, node)
const chunkSize = isLeafBlock ? null : editor.getChunkSize(node)
const chunking = !!chunkSize
Expand Down Expand Up @@ -119,7 +118,9 @@ const useChildren = (props: {

if (!chunking) {
return node.children.map((n, i) =>
Text.isText(n) ? renderTextComponent(n, i) : renderElementComponent(n, i)
Text.isTextNode(n)
? renderTextComponent(n, i)
: renderElementComponent(n, i)
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/slate-react/src/hooks/use-decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useDecorations = (
return decorate([node, path])
}

const equalityFn = Text.isText(node)
const equalityFn = Text.isTextNode(node)
? isTextDecorationsEqual
: isElementDecorationsEqual

Expand Down
2 changes: 1 addition & 1 deletion packages/slate-react/test/chunking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const getChildrenAndTreeForShape = (
}

const withChunking = (editor: ReactEditor) => {
editor.getChunkSize = node => (Editor.isEditor(node) ? 3 : null)
editor.getChunkSize = node => (node === editor ? 3 : null)
return editor
}

Expand Down
2 changes: 1 addition & 1 deletion packages/slate/src/core/get-dirty-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getDirtyPaths: WithEditorFirstArg<Editor['getDirtyPaths']> = (
case 'insert_node': {
const { node, path } = op
const levels = Path.levels(path)
const descendants = Text.isText(node)
const descendants = Text.isTextNode(node)
? []
: Array.from(Node.nodes(node), ([, p]) => path.concat(p))

Expand Down
32 changes: 17 additions & 15 deletions packages/slate/src/core/normalize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
const [node, path] = entry

// There are no core normalizations for text nodes.
if (Text.isText(node)) {
if (Text.isTextNode(node)) {
return
}

// Ensure that block and inline nodes have at least one text child.
if (Element.isElement(node) && node.children.length === 0) {
if (Element.isElementNode(node) && node.children.length === 0) {
const child = { text: '' }
Transforms.insertNodes(editor, child, {
at: path.concat(0),
Expand All @@ -28,26 +28,28 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
}

// Determine whether the node should have block or inline children.
const shouldHaveInlines = Editor.isEditor(node)
? false
: Element.isElement(node) &&
(editor.isInline(node) ||
node.children.length === 0 ||
Text.isText(node.children[0]) ||
editor.isInline(node.children[0]))
const shouldHaveInlines =
node === editor
? false
: Element.isElementNode(node) &&
(editor.isInline(node) ||
node.children.length === 0 ||
Text.isTextNode(node.children[0]) ||
editor.isInline(node.children[0]))

// Since we'll be applying operations while iterating, keep track of an
// index that accounts for any added/removed nodes.
let n = 0

for (let i = 0; i < node.children.length; i++, n++) {
const currentNode = Node.get(editor, path)
if (Text.isText(currentNode)) continue
if (Text.isTextNode(currentNode)) continue
const child = currentNode.children[n] as Descendant
const prev = currentNode.children[n - 1] as Descendant
const isLast = i === node.children.length - 1
const isInlineOrText =
Text.isText(child) || (Element.isElement(child) && editor.isInline(child))
Text.isTextNode(child) ||
(Element.isElementNode(child) && editor.isInline(child))

// Only allow block nodes in the top-level children and parent blocks
// that only contain block nodes. Similarly, only allow inline nodes in
Expand All @@ -67,10 +69,10 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
Transforms.unwrapNodes(editor, { at: path.concat(n), voids: true })
}
n--
} else if (Element.isElement(child)) {
} else if (Element.isElementNode(child)) {
// Ensure that inline nodes are surrounded by text nodes.
if (editor.isInline(child)) {
if (prev == null || !Text.isText(prev)) {
if (prev == null || !Text.isTextNode(prev)) {
const newChild = { text: '' }
Transforms.insertNodes(editor, newChild, {
at: path.concat(n),
Expand All @@ -95,13 +97,13 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
// To prevent slate from breaking, we can add the `children` field,
// and now that it is valid, we can to many more operations easily,
// such as extend normalizers to fix erronous structure.
if (!Text.isText(child) && !('children' in child)) {
if (!Text.isTextNode(child) && !('children' in child)) {
const elementChild = child as Element
elementChild.children = []
}

// Merge adjacent text nodes that are empty or match.
if (prev != null && Text.isText(prev)) {
if (prev != null && Text.isTextNode(prev)) {
if (Text.equals(child, prev, { loose: true })) {
Transforms.mergeNodes(editor, { at: path.concat(n), voids: true })
n--
Expand Down
2 changes: 1 addition & 1 deletion packages/slate/src/editor/add-mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const addMark: EditorInterface['addMark'] = (editor, key, value) => {

if (selection) {
const match = (node: Node, path: Path) => {
if (!Text.isText(node)) {
if (!Text.isTextNode(node)) {
return false // marks can only be applied to text
}
const [parentNode, parentPath] = Editor.parent(editor, path)
Expand Down
2 changes: 1 addition & 1 deletion packages/slate/src/editor/element-read-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export const elementReadOnly: EditorInterface['elementReadOnly'] = (
) => {
return Editor.above(editor, {
...options,
match: n => Element.isElement(n) && Editor.isElementReadOnly(editor, n),
match: n => Element.isElementNode(n) && Editor.isElementReadOnly(editor, n),
})
}
2 changes: 1 addition & 1 deletion packages/slate/src/editor/get-void.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Element } from '../interfaces/element'
export const getVoid: EditorInterface['void'] = (editor, options = {}) => {
return Editor.above(editor, {
...options,
match: n => Element.isElement(n) && Editor.isVoid(editor, n),
match: n => Element.isElementNode(n) && Editor.isVoid(editor, n),
})
}
2 changes: 1 addition & 1 deletion packages/slate/src/editor/has-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Element } from '../interfaces/element'

export const hasBlocks: EditorInterface['hasBlocks'] = (editor, element) => {
return element.children.some(
n => Element.isElement(n) && Editor.isBlock(editor, n)
n => Element.isElementNode(n) && Editor.isBlock(editor, n)
)
}
2 changes: 1 addition & 1 deletion packages/slate/src/editor/has-inlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Text } from '../interfaces/text'

export const hasInlines: EditorInterface['hasInlines'] = (editor, element) => {
return element.children.some(
n => Text.isText(n) || Editor.isInline(editor, n)
n => Text.isTextNode(n) || Editor.isInline(editor, n)
)
}
2 changes: 1 addition & 1 deletion packages/slate/src/editor/has-texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { EditorInterface } from '../interfaces/editor'
import { Text } from '../interfaces/text'

export const hasTexts: EditorInterface['hasTexts'] = (editor, element) => {
return element.children.every(n => Text.isText(n))
return element.children.every(n => Text.isTextNode(n))
}
7 changes: 7 additions & 0 deletions packages/slate/src/editor/is-editor-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Editor, EditorInterface, Node } from '../interfaces'

export const isEditorNode: EditorInterface['isEditorNode'] = (
node: Node
): node is Editor => {
return typeof (node as Editor).apply === 'function'
}
Loading
Loading