-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(entity): swapper and finder rework
- Loading branch information
Showing
4 changed files
with
145 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,129 @@ | ||
import { ContextStatePageContent } from '@/types/context' | ||
import { Store } from 'vuex' | ||
import { ContextState } from '@/types/context' | ||
import { useScroll } from '@/use/scroll' | ||
import { useStore } from 'vuex' | ||
import { computed, reactive, nextTick } from 'vue' | ||
|
||
export const useEntity = () => { | ||
const entry = (input: string, target: string): boolean => { | ||
return ( | ||
input.startsWith('/' + target) || | ||
input.startsWith('/' + target.toUpperCase()) | ||
) | ||
const store = useStore() | ||
const pages = computed(() => store.state.project.pages) | ||
const fstate = reactive({ | ||
entry: '' as string, | ||
actuallyLetterCounter: 0 as number, | ||
actuallyLetterRaw: '' as string, | ||
listOfLettersExists: [] as Array<Record<string, any>>, | ||
maxLetterCounter: 0 as number, | ||
}) | ||
|
||
const utils = () => { | ||
const entry = (input: string, target: string): boolean => { | ||
return ( | ||
input.startsWith('/' + target) || | ||
input.startsWith('/' + target.toUpperCase()) | ||
) | ||
} | ||
|
||
return { entry } | ||
} | ||
|
||
const switcherText = ( | ||
store: Store<any>, | ||
{ entry, output, equal }: Record<any, any> | ||
) => { | ||
const arr = store.state.context.entity | ||
|
||
// TODO: Deletar em caso de output vazio | ||
if (!entry || !output) return | ||
|
||
arr.forEach((e: ContextStatePageContent) => { | ||
const text = e.raw.split(' ') | ||
|
||
text.forEach((t: string) => { | ||
if (equal && t === entry) { | ||
store.commit('context/switchEntityRaw', { | ||
entity: e, | ||
match: t, | ||
raw: output, | ||
}) | ||
} else if (!equal && t.includes(entry)) { | ||
store.commit('context/switchEntityRaw', { | ||
entity: e, | ||
match: entry, | ||
raw: output, | ||
}) | ||
} | ||
const swapper = () => { | ||
const switcherText = ({ entry, output, equal }: Record<any, any>) => { | ||
const arr = store.state.context.entity | ||
|
||
// TODO: Deletar em caso de output vazio | ||
if (!entry || !output) return | ||
|
||
arr.forEach((e: ContextStatePageContent) => { | ||
const text = e.raw.split(' ') | ||
|
||
text.forEach((t: string) => { | ||
if (equal && t === entry) { | ||
store.commit('context/switchEntityRaw', { | ||
entity: e, | ||
match: t, | ||
raw: output, | ||
}) | ||
} else if (!equal && t.includes(entry)) { | ||
store.commit('context/switchEntityRaw', { | ||
entity: e, | ||
match: entry, | ||
raw: output, | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
return { switcherText } | ||
} | ||
|
||
const finder = () => { | ||
const onFinder = () => { | ||
fstate.listOfLettersExists = [] | ||
fstate.actuallyLetterCounter = 0 | ||
fstate.actuallyLetterRaw = '' | ||
fstate.maxLetterCounter = 0 | ||
|
||
pages.value.forEach((context: ContextState) => { | ||
context.entity.forEach((entity: ContextStatePageContent) => { | ||
if (!fstate.entry) return | ||
|
||
if (entity.raw.includes(fstate.entry)) { | ||
fstate.listOfLettersExists.push({ entity, page: context }) | ||
fstate.maxLetterCounter++ | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
if (fstate.maxLetterCounter === 0) return | ||
|
||
onSearchGo(fstate.listOfLettersExists[0]) | ||
} | ||
|
||
const onSearchGo = (object: Record<string, any>) => { | ||
fstate.actuallyLetterRaw = object.letter | ||
fstate.actuallyLetterCounter = | ||
fstate.listOfLettersExists.indexOf(object) + 1 | ||
|
||
const pageIndex = store.state.project.pages.indexOf(object.page) | ||
const entityIndex = store.state.project.pages[pageIndex].entity.indexOf( | ||
object.entity | ||
) | ||
|
||
onGo(`entity-${entityIndex}`, object.page) | ||
} | ||
|
||
const onGo = async (go: string | symbol, page: ContextState) => { | ||
if (store.state.context.id !== page.id) store.commit('context/load', page) | ||
await nextTick | ||
useScroll().to(`#${String(go)}`, 'center') | ||
} | ||
|
||
const onUp = () => { | ||
if (fstate.actuallyLetterCounter >= fstate.maxLetterCounter) { | ||
onSearchGo(fstate.listOfLettersExists[0]) | ||
} else { | ||
const object = fstate.listOfLettersExists[fstate.actuallyLetterCounter] | ||
|
||
onSearchGo(object) | ||
} | ||
} | ||
|
||
const onDown = () => { | ||
if (fstate.actuallyLetterCounter <= 1) { | ||
onSearchGo( | ||
fstate.listOfLettersExists[fstate.listOfLettersExists.length - 1] | ||
) | ||
} else { | ||
const object = | ||
fstate.listOfLettersExists[fstate.actuallyLetterCounter - 2] | ||
|
||
onSearchGo(object) | ||
} | ||
} | ||
|
||
return { onFinder, onSearchGo, onGo, onUp, onDown } | ||
} | ||
|
||
return { entry, switcherText } | ||
return { utils, swapper, finder, fstate } | ||
} |