Skip to content

Commit

Permalink
Remove linked list part 12
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowely committed Dec 31, 2023
1 parent 5b3667b commit fa8db95
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
72 changes: 6 additions & 66 deletions lib/features/useValueParser.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {useEffect} from 'react'
import {Option} from '../types'
import {isAnnotated} from '../utils/checkers/isAnnotated'
import {Parser} from '../utils/classes/Parser/Parser'
import {Store} from '../utils/classes/Store'
import {findGap} from '../utils/functions/findGap'
import {getClosestIndexes} from '../utils/functions/getClosestIndexes'
import {useStore} from '../utils/hooks/useStore'
import {getTokensByUI} from '../utils/functions/getTokensByUI'
import {getTokensByValue} from '../utils/functions/getTokensByValue'

export const useValueParser = () => {
const store = useStore()
Expand All @@ -15,64 +11,8 @@ export const useValueParser = () => {
}), true)

useEffect(() => {
/*if (store.focus.target)
updateStateFromUI(store, options)
else {*/
updateStateFromValue(store, value, options)
//}
store.tokens = store.focus.target
? getTokensByUI(store)
: getTokensByValue(store)
}, [value, options])
}


function updateStateFromUI(store: Store, options?: Option[]) {
const {focus} = store
const tokens = Parser.split(focus.content, options)

if (tokens.length === 1) return

store.tokens = store.tokens.toSpliced(focus.index, 1, ...tokens)
}


function updateStateFromValue(store: Store, value: string, options?: Option[]) {
const ranges = getRangeMap(store)
const gap = findGap(store.previousValue, value)
store.previousValue = value

switch (true) {
//Mark removing happen
case gap.left && (ranges.includes(gap.left) && gap.right && Math.abs(gap.left - gap.right) > 1):
const updatedIndex1 = ranges.indexOf(gap.left)
const tokens1 = parseUnionedLabels(store, updatedIndex1 - 1, updatedIndex1)
store.tokens = store.tokens.toSpliced(updatedIndex1, 1, ...tokens1)
break
//Changing in label
case gap.left !== undefined:
const [updatedIndex] = getClosestIndexes(ranges, gap.left)
const tokens2 = parseUnionedLabels(store, updatedIndex)
if (tokens2.length === 1) return
store.tokens = store.tokens.toSpliced(updatedIndex, 1, ...tokens2)
break
default:
//Parse all string
store.tokens = Parser.split(value, options)
}
}

function parseUnionedLabels(store: Store, ...indexes: number[]) {
let span = ''
for (const index of indexes) {
span += store.tokens[index].label
}

return Parser.split(span, store.props.options)
}

function getRangeMap(store: Store): number[] {
let position = 0
return store.tokens.map(token => {
const length = isAnnotated(token) ? token.annotation.length : token.label.length
position += length
return position - length
}) ?? []
}
}
12 changes: 12 additions & 0 deletions lib/utils/functions/getTokensByUI.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Store} from '../classes/Store'
import {Option} from '../../types'
import {Parser} from '../classes/Parser/Parser'

export function getTokensByUI(store: Store) {
const {focus, props: {options}} = store
const tokens = Parser.split(focus.content, options)

if (tokens.length === 1) return store.tokens

return store.tokens.toSpliced(focus.index, 1, ...tokens)
}
48 changes: 48 additions & 0 deletions lib/utils/functions/getTokensByValue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {Store} from '../classes/Store'
import {Option} from '../../types'
import {findGap} from './findGap'
import {getClosestIndexes} from './getClosestIndexes'
import {Parser} from '../classes/Parser/Parser'
import {isAnnotated} from '../checkers/isAnnotated'

export function getTokensByValue(store: Store) {
const {props: {value, options}} = store
const ranges = getRangeMap(store)
const gap = findGap(store.previousValue, value)
store.previousValue = value

switch (true) {
//Mark removing happen
case gap.left && (ranges.includes(gap.left) && gap.right && Math.abs(gap.left - gap.right) > 1):
const updatedIndex1 = ranges.indexOf(gap.left)
const tokens1 = parseUnionLabels(store, updatedIndex1 - 1, updatedIndex1)
return store.tokens.toSpliced(updatedIndex1, 1, ...tokens1)
//Changing in label
case gap.left !== undefined:
const [updatedIndex] = getClosestIndexes(ranges, gap.left)
const tokens2 = parseUnionLabels(store, updatedIndex)
if (tokens2.length === 1) return store.tokens
return store.tokens.toSpliced(updatedIndex, 1, ...tokens2)
default:
//Parse all string
return Parser.split(value, options)
}
}

function parseUnionLabels(store: Store, ...indexes: number[]) {
let span = ''
for (const index of indexes) {
span += store.tokens[index].label
}

return Parser.split(span, store.props.options)
}

function getRangeMap(store: Store): number[] {
let position = 0
return store.tokens.map(token => {
const length = isAnnotated(token) ? token.annotation.length : token.label.length
position += length
return position - length
}) ?? []
}

0 comments on commit fa8db95

Please sign in to comment.