mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-16 03:39:00 +08:00
fix wrong offset calculation
This commit is contained in:
parent
edd54f90b0
commit
c33d1bf38f
@ -115,26 +115,29 @@ export default class Search extends Extension {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rebaseNextResult(replace, index) {
|
rebaseNextResult(replace, index, lastOffset = 0) {
|
||||||
const nextIndex = index + 1
|
const nextIndex = index + 1
|
||||||
if (!this.results[nextIndex]) return
|
if (!this.results[nextIndex]) return null
|
||||||
|
|
||||||
const nextStep = this.results[nextIndex]
|
const { from: currentFrom, to: currentTo } = this.results[index]
|
||||||
const { from, to } = nextStep
|
const offset = (currentTo - currentFrom - replace.length) + lastOffset
|
||||||
const offset = (to - from - replace.length) * nextIndex
|
|
||||||
|
|
||||||
|
const { from, to } = this.results[nextIndex]
|
||||||
this.results[nextIndex] = {
|
this.results[nextIndex] = {
|
||||||
to: to - offset,
|
to: to - offset,
|
||||||
from: from - offset,
|
from: from - offset,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
replaceAll(replace) {
|
replaceAll(replace) {
|
||||||
return ({ tr }, dispatch) => {
|
return ({ tr }, dispatch) => {
|
||||||
|
let offset
|
||||||
this.results.forEach(({ from, to }, index) => {
|
this.results.forEach(({ from, to }, index) => {
|
||||||
tr.insertText(replace, from, to)
|
tr.insertText(replace, from, to)
|
||||||
|
|
||||||
this.rebaseNextResult(replace, index)
|
offset = this.rebaseNextResult(replace, index, offset)
|
||||||
})
|
})
|
||||||
|
|
||||||
dispatch(tr)
|
dispatch(tr)
|
||||||
|
Loading…
Reference in New Issue
Block a user