Skip to content

Commit

Permalink
feat: add support for checking for attributes in extendMarkRange
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed May 17, 2021
1 parent 5b8808a commit ff7dd9b
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 14 deletions.
19 changes: 18 additions & 1 deletion docs/src/docPages/api/commands/extend-mark-range.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# extendMarkRange
The `extendMarkRange` command expands the current selection to encompass the current mark. If the current selection doesn’t have the specified mark, nothing changes.

<ContentMissing />
## Parameters
`typeOrName: string | MarkType`

Name or type of the mark.

`attributes?: Record<string, any>`

Optionally, you can specify attributes that the extented mark must contain.

## Usage
```js
// Expand selection to link marks
editor.commands.extendMarkRange('link')

// Expand selection to link marks with specific attributes
editor.commands.extendMarkRange('link', { href: 'https://google.com' })
```
1 change: 0 additions & 1 deletion docs/src/links.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
type: draft
- title: extendMarkRange
link: /api/commands/extend-mark-range
type: draft
- title: focus
link: /api/commands/focus
type: draft
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/commands/extendMarkRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ declare module '@tiptap/core' {
/**
* Extends the text selection to the current mark.
*/
extendMarkRange: (typeOrName: string | MarkType) => Command,
extendMarkRange: (typeOrName: string | MarkType, attributes?: Record<string, any>) => Command,
}
}
}

export const extendMarkRange: RawCommands['extendMarkRange'] = typeOrName => ({ tr, state, dispatch }) => {
export const extendMarkRange: RawCommands['extendMarkRange'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
const type = getMarkType(typeOrName, state.schema)
const { doc, selection } = tr
const { $from, empty } = selection
const { $from, from, to } = selection

if (empty && dispatch) {
const range = getMarkRange($from, type)
if (dispatch) {
const range = getMarkRange($from, type, attributes)

if (range) {
if (range && range.from <= from && range.to >= to) {
const newSelection = TextSelection.create(doc, range.from, range.to)

tr.setSelection(newSelection)
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/helpers/getDebugJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Node as ProseMirrorNode } from 'prosemirror-model'

/**
* Returns a node tree with node positions.
*/
export default function getDebugJSON(node: ProseMirrorNode) {
const debug = (startNode: ProseMirrorNode, startOffset = 0) => {
const nodes: any[] = []

startNode.forEach((n, offset) => {
const from = startOffset + offset
const to = from + n.nodeSize

nodes.push({
type: n.type.name,
attrs: { ...n.attrs },
from,
to,
marks: n.marks.map(mark => ({
type: mark.type.name,
attrs: { ...mark.attrs },
})),
content: debug(n, from + 1),
})
})

return nodes
}

return debug(node)
}
40 changes: 34 additions & 6 deletions packages/core/src/helpers/getMarkRange.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { MarkType, ResolvedPos } from 'prosemirror-model'
import { Mark as ProseMirrorMark, MarkType, ResolvedPos } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes'
import { Range } from '../types'

export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void {
function findMarkInSet(
marks: ProseMirrorMark[],
type: MarkType,
attributes: Record<string, any> = {},
): ProseMirrorMark | undefined {
return marks.find(item => {
return item.type === type && objectIncludes(item.attrs, attributes)
})
}

function isMarkInSet(
marks: ProseMirrorMark[],
type: MarkType,
attributes: Record<string, any> = {},
): boolean {
return !!findMarkInSet(marks, type, attributes)
}

export default function getMarkRange(
$pos: ResolvedPos,
type: MarkType,
attributes: Record<string, any> = {},
): Range | void {
if (!$pos || !type) {
return
}
Expand All @@ -12,9 +35,9 @@ export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range |
return
}

const link = start.node.marks.find(mark => mark.type === type)
const mark = findMarkInSet(start.node.marks, type, attributes)

if (!link) {
if (!mark) {
return
}

Expand All @@ -23,12 +46,17 @@ export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range |
let endIndex = startIndex + 1
let endPos = startPos + start.node.nodeSize

while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
findMarkInSet(start.node.marks, type, attributes)

while (startIndex > 0 && mark.isInSet($pos.parent.child(startIndex - 1).marks)) {
startIndex -= 1
startPos -= $pos.parent.child(startIndex).nodeSize
}

while (endIndex < $pos.parent.childCount && link.isInSet($pos.parent.child(endIndex).marks)) {
while (
endIndex < $pos.parent.childCount
&& isMarkInSet($pos.parent.child(endIndex).marks, type, attributes)
) {
endPos += $pos.parent.child(endIndex).nodeSize
endIndex += 1
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { default as generateHTML } from './helpers/generateHTML'
export { default as generateJSON } from './helpers/generateJSON'
export { default as getSchema } from './helpers/getSchema'
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
export { default as getDebugJSON } from './helpers/getDebugJSON'
export { default as getAttributes } from './helpers/getAttributes'
export { default as getMarkAttributes } from './helpers/getMarkAttributes'
export { default as getMarkRange } from './helpers/getMarkRange'
Expand Down
Loading

0 comments on commit ff7dd9b

Please sign in to comment.