-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lists): start refactoring lists code
- Loading branch information
Showing
6 changed files
with
92 additions
and
40 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/extension-list-item/src/helpers/getCurrentListItemPos.ts
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,30 @@ | ||
import { getNodeType } from '@tiptap/core' | ||
import { EditorState } from '@tiptap/pm/state' | ||
import { ResolvedPos } from 'prosemirror-model' | ||
|
||
export const getCurrentListItemPos = (typeOrName: string, state: EditorState): ResolvedPos | undefined => { | ||
const { $from } = state.selection | ||
const nodeType = getNodeType(typeOrName, state.schema) | ||
|
||
let currentNode = null | ||
let currentDepth = $from.depth | ||
let currentPos = $from.pos | ||
let targetDepth: number | null = null | ||
|
||
while (currentDepth > 0 && targetDepth === null) { | ||
currentNode = $from.node(currentDepth) | ||
|
||
if (currentNode.type === nodeType) { | ||
targetDepth = currentDepth | ||
} else { | ||
currentDepth -= 1 | ||
currentPos -= 1 | ||
} | ||
} | ||
|
||
if (targetDepth === null) { | ||
return | ||
} | ||
|
||
return state.doc.resolve(currentPos) | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/extension-list-item/src/helpers/hasListItemAfter.ts
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,17 @@ | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
export const hasListItemAfter = (typeOrName: string, state: EditorState): boolean => { | ||
const { $anchor } = state.selection | ||
|
||
const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2) | ||
|
||
if ($targetPos.index() === $targetPos.parent.childCount - 1) { | ||
return false | ||
} | ||
|
||
if ($targetPos.nodeAfter?.type.name !== typeOrName) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/extension-list-item/src/helpers/hasListItemBefore.ts
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,17 @@ | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
export const hasListItemBefore = (typeOrName: string, state: EditorState): boolean => { | ||
const { $anchor } = state.selection | ||
|
||
const $targetPos = state.doc.resolve($anchor.pos - 2) | ||
|
||
if ($targetPos.index() === 0) { | ||
return false | ||
} | ||
|
||
if ($targetPos.nodeBefore?.type.name !== typeOrName) { | ||
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
21 changes: 21 additions & 0 deletions
21
packages/extension-list-item/src/helpers/listItemhasSublist.ts
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,21 @@ | ||
import { getNodeType } from '@tiptap/core' | ||
import { Node } from '@tiptap/pm/model' | ||
import { EditorState } from '@tiptap/pm/state' | ||
|
||
export const listItemHasSubList = (typeOrName: string, state: EditorState, node?: Node) => { | ||
if (!node) { | ||
return false | ||
} | ||
|
||
const nodeType = getNodeType(typeOrName, state.schema) | ||
|
||
let hasSubList = false | ||
|
||
node.descendants(child => { | ||
if (child.type === nodeType) { | ||
hasSubList = true | ||
} | ||
}) | ||
|
||
return hasSubList | ||
} |
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