From fa8db951e92dff8820354d28852631caca05d2bf Mon Sep 17 00:00:00 2001 From: Ruslan Date: Sun, 31 Dec 2023 23:50:25 +0300 Subject: [PATCH] Remove linked list part 12 --- lib/features/useValueParser.tsx | 72 ++---------------------- lib/utils/functions/getTokensByUI.tsx | 12 ++++ lib/utils/functions/getTokensByValue.tsx | 48 ++++++++++++++++ 3 files changed, 66 insertions(+), 66 deletions(-) create mode 100644 lib/utils/functions/getTokensByUI.tsx create mode 100644 lib/utils/functions/getTokensByValue.tsx diff --git a/lib/features/useValueParser.tsx b/lib/features/useValueParser.tsx index 09866f9..36e122c 100644 --- a/lib/features/useValueParser.tsx +++ b/lib/features/useValueParser.tsx @@ -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() @@ -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 - }) ?? [] -} +} \ No newline at end of file diff --git a/lib/utils/functions/getTokensByUI.tsx b/lib/utils/functions/getTokensByUI.tsx new file mode 100644 index 0000000..e5f86d1 --- /dev/null +++ b/lib/utils/functions/getTokensByUI.tsx @@ -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) +} \ No newline at end of file diff --git a/lib/utils/functions/getTokensByValue.tsx b/lib/utils/functions/getTokensByValue.tsx new file mode 100644 index 0000000..0e89a0b --- /dev/null +++ b/lib/utils/functions/getTokensByValue.tsx @@ -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 + }) ?? [] +} \ No newline at end of file