Make searchResult reactive

This commit is contained in:
Jos de Jong 2020-07-26 11:31:38 +02:00
parent ad3ac339cf
commit e23e4a82dd
1 changed files with 9 additions and 15 deletions

View File

@ -45,7 +45,6 @@
export let doc = {} export let doc = {}
let state = undefined let state = undefined
let searchResult = undefined
let selection = null let selection = null
let clipboard = null let clipboard = null
@ -56,6 +55,7 @@
let showSearch = false let showSearch = false
let searchText = '' let searchText = ''
$: searchResult = search(doc, searchText, searchResult)
const history = createHistory({ const history = createHistory({
onChange: (state) => { onChange: (state) => {
@ -99,7 +99,6 @@
doc = documentPatchResult.json doc = documentPatchResult.json
state = patchProps(statePatchResult.json, operations) state = patchProps(statePatchResult.json, operations)
searchResult = search(doc, searchText, searchResult)
if (newSelection) { if (newSelection) {
selection = newSelection selection = newSelection
} }
@ -224,7 +223,6 @@
if (item) { if (item) {
doc = immutableJSONPatch(doc, item.undo).json doc = immutableJSONPatch(doc, item.undo).json
state = item.prevState state = item.prevState
searchResult = search(doc, searchText, searchResult)
selection = item.prevSelection selection = item.prevSelection
console.log('undo', { item, doc, state, selection }) console.log('undo', { item, doc, state, selection })
@ -240,7 +238,6 @@
if (item) { if (item) {
doc = immutableJSONPatch(doc, item.redo).json doc = immutableJSONPatch(doc, item.redo).json
state = item.state state = item.state
searchResult = search(doc, searchText, searchResult)
selection = item.selection selection = item.selection
console.log('redo', { item, doc, state, selection }) console.log('redo', { item, doc, state, selection })
@ -250,34 +247,31 @@
} }
} }
function changeSearchText (text) { async function changeSearchText (text) {
searchText = text searchText = text
searchResult = search(doc, searchText, searchResult) await tick() // await for the search results to be updated
focusActiveSearchResult(searchResult && searchResult.activeItem) focusActiveSearchResult(searchResult && searchResult.activeItem)
} }
function nextSearchResult () { async function nextSearchResult () {
searchResult = searchNext(searchResult) searchResult = searchNext(searchResult)
focusActiveSearchResult(searchResult && searchResult.activeItem) focusActiveSearchResult(searchResult && searchResult.activeItem)
} }
function clearSearchResult () {
showSearch = false
searchText = ''
searchResult = search(doc, searchText, searchResult)
}
function previousSearchResult () { function previousSearchResult () {
searchResult = searchPrevious(searchResult) searchResult = searchPrevious(searchResult)
focusActiveSearchResult(searchResult && searchResult.activeItem) focusActiveSearchResult(searchResult && searchResult.activeItem)
} }
function clearSearchResult () {
showSearch = false
searchText = ''
}
async function focusActiveSearchResult (activeItem) { async function focusActiveSearchResult (activeItem) {
if (activeItem) { if (activeItem) {
state = expandPath(state, activeItem.path) state = expandPath(state, activeItem.path)
await tick() await tick()
scrollTo(activeItem.path.concat(activeItem.what)) scrollTo(activeItem.path.concat(activeItem.what))
} }
} }