Some checks failed
Verify / Install node dependencies (push) Failing after 2s
Verify / Build packages (push) Has been skipped
Verify / Check linting & formatting (push) Has been skipped
Verify / Run unit tests (push) Has been skipped
Verify / Run e2e tests (shard 1/4) (push) Has been skipped
Verify / Run e2e tests (shard 2/4) (push) Has been skipped
Verify / Run e2e tests (shard 3/4) (push) Has been skipped
Verify / Run e2e tests (shard 4/4) (push) Has been skipped
Publish / Resolve publish config (push) Failing after 2s
Publish / Send notification to Slack (push) Has been skipped
Publish / Send failure notification to Slack (push) Failing after 8s
Verify / Merge e2e reports (push) Has been skipped
Publish / Build (push) Has been skipped
Publish / Bump Versions OR Release to Registry (push) Has been skipped
48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import type { Node as PMNode } from '@tiptap/pm/model'
|
|
import type { Mapping } from '@tiptap/pm/transform'
|
|
|
|
import type { FindAndReplaceMeta, FindAndReplacePluginState } from '../plugin/plugin-state.js'
|
|
import { searchTextblocks } from '../search/search.js'
|
|
|
|
import { getChangedTextblocks } from './getChangedTextblocks.js'
|
|
import { mapResult } from './mapResult.js'
|
|
import { mapResults } from './mapResults.js'
|
|
import { mergeResults } from './mergeResults.js'
|
|
import { refreshDecorations } from './refreshDecorations.js'
|
|
import { resolveCurrentIndex } from './resolveCurrentIndex.js'
|
|
import { resultInTextblocks } from './resultInTextblocks.js'
|
|
|
|
export function refreshDocumentState(
|
|
previousState: FindAndReplacePluginState,
|
|
state: FindAndReplacePluginState,
|
|
doc: PMNode,
|
|
meta: FindAndReplaceMeta | undefined,
|
|
mapping: Mapping,
|
|
): FindAndReplacePluginState {
|
|
const textblocks = getChangedTextblocks(doc, mapping)
|
|
const mappedResults = mapResults(previousState.results, mapping)
|
|
const unaffectedResults = mappedResults.filter(result => !resultInTextblocks(result, textblocks))
|
|
const changedResults = searchTextblocks(textblocks, state.searchTerm, state)
|
|
const results = mergeResults(unaffectedResults, changedResults)
|
|
const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping)
|
|
const previousCurrent =
|
|
previousState.currentIndex === null
|
|
? null
|
|
: mapResult(previousState.results[previousState.currentIndex], mapping)
|
|
const decorations = previousState.decorations.map(mapping, doc)
|
|
|
|
return {
|
|
...state,
|
|
results,
|
|
currentIndex,
|
|
decorations: refreshDecorations(
|
|
decorations,
|
|
doc,
|
|
results,
|
|
currentIndex,
|
|
textblocks,
|
|
previousCurrent,
|
|
),
|
|
}
|
|
}
|