feat: allow number for setTextSelection and insertContentAt
This commit is contained in:
@@ -13,16 +13,19 @@ declare module '@tiptap/core' {
|
|||||||
/**
|
/**
|
||||||
* Insert a node or string of HTML at a specific position.
|
* Insert a node or string of HTML at a specific position.
|
||||||
*/
|
*/
|
||||||
insertContentAt: (range: Range, value: Content) => Command,
|
insertContentAt: (position: number | Range, value: Content) => Command,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const insertContentAt: RawCommands['insertContentAt'] = (range, value) => ({ tr, dispatch, editor }) => {
|
export const insertContentAt: RawCommands['insertContentAt'] = (position, value) => ({ tr, dispatch, editor }) => {
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
const content = createNodeFromContent(value, editor.schema)
|
const content = createNodeFromContent(value, editor.schema)
|
||||||
|
const { from, to } = typeof position === 'number'
|
||||||
|
? { from: position, to: position }
|
||||||
|
: position
|
||||||
|
|
||||||
tr.replaceWith(range.from, range.to, content)
|
tr.replaceWith(from, to, content)
|
||||||
|
|
||||||
// set cursor at end of inserted content
|
// set cursor at end of inserted content
|
||||||
selectionToInsertionEnd(tr, tr.steps.length - 1, 1)
|
selectionToInsertionEnd(tr, tr.steps.length - 1, 1)
|
||||||
|
|||||||
@@ -8,17 +8,20 @@ declare module '@tiptap/core' {
|
|||||||
/**
|
/**
|
||||||
* Creates a TextSelection.
|
* Creates a TextSelection.
|
||||||
*/
|
*/
|
||||||
setTextSelection: (range: Range) => Command,
|
setTextSelection: (position: number | Range) => Command,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setTextSelection: RawCommands['setTextSelection'] = range => ({ tr, dispatch }) => {
|
export const setTextSelection: RawCommands['setTextSelection'] = position => ({ tr, dispatch }) => {
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
const { doc } = tr
|
const { doc } = tr
|
||||||
const from = minMax(range.from, 0, doc.content.size)
|
const { from, to } = typeof position === 'number'
|
||||||
const to = minMax(range.to, 0, doc.content.size)
|
? { from: position, to: position }
|
||||||
const selection = TextSelection.create(doc, from, to)
|
: position
|
||||||
|
const boundedFrom = minMax(from, 0, doc.content.size)
|
||||||
|
const boundedTo = minMax(to, 0, doc.content.size)
|
||||||
|
const selection = TextSelection.create(doc, boundedFrom, boundedTo)
|
||||||
|
|
||||||
tr.setSelection(selection)
|
tr.setSelection(selection)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user