-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
53 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,29 @@ | ||
import { Node, NodeType } from '@tiptap/pm/model' | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
/** | ||
* Finds the first node of a given type or name in the current selection. | ||
* @param state The editor state. | ||
* @param typeOrName The node type or name. | ||
* @param pos The position to start searching from. | ||
* @param maxDepth The maximum depth to search. | ||
* @returns The node and the depth as an array. | ||
*/ | ||
export const getNodeAtPosition = (state: EditorState, typeOrName: string | NodeType, pos: number, maxDepth = 20) => { | ||
const $pos = state.doc.resolve(pos) | ||
|
||
let currentDepth = maxDepth | ||
let node: Node | null = null | ||
|
||
while (currentDepth > 0 && node === null) { | ||
const currentNode = $pos.node(currentDepth) | ||
|
||
if (currentNode?.type.name === typeOrName) { | ||
node = currentNode | ||
} else { | ||
currentDepth -= 1 | ||
} | ||
} | ||
|
||
return [node, currentDepth] as [Node | null, number] | ||
} |
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,11 @@ | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
export const istAtEndOfNode = (state: EditorState) => { | ||
const { $from, $to } = state.selection | ||
|
||
if ($to.parentOffset < $to.parent.nodeSize - 2 || $from.pos !== $to.pos) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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,11 @@ | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
export const isAtStartOfNode = (state: EditorState) => { | ||
const { $from, $to } = state.selection | ||
|
||
if ($from.parentOffset > 0 || $from.pos !== $to.pos) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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