added replace and replaceAll commands

This commit is contained in:
Chrissi2812
2019-06-03 12:18:59 +02:00
parent 525619ad26
commit 2544e40f99
2 changed files with 45 additions and 1 deletions

View File

@@ -47,6 +47,8 @@ export default class Search extends Extension {
commands() {
return {
find: attrs => this.find(attrs),
replace: attrs => this.replace(attrs),
replaceAll: attrs => this.replaceAll(attrs),
clearSearch: () => this.clear(),
toggleSearch: () => this.toggleSearch(),
}
@@ -104,6 +106,40 @@ export default class Search extends Extension {
})
}
replace(replace) {
return (state, dispatch) => {
const { from, to } = this.results[0]
dispatch(state.tr.insertText(replace, from, to))
}
}
rebaseNextResult(replace, index) {
const nextIndex = index + 1
if (!this.results[nextIndex]) return
const nextStep = this.results[nextIndex]
const { from, to } = nextStep
const offset = (to - from - replace.length) * nextIndex
this.results[nextIndex] = {
to: to - offset,
from: from - offset,
}
}
replaceAll(replace) {
return ({ tr }, dispatch) => {
this.results.forEach(({ from, to }, index) => {
tr.insertText(replace, from, to)
this.rebaseNextResult(replace, index)
})
dispatch(tr)
}
}
find(searchTerm) {
return (state, dispatch) => {
this.searchTerm = (this.options.disableRegex) ? searchTerm.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') : searchTerm