diff --git a/src/lib/components/dictionary/test/page/tabs/calculation.svelte b/src/lib/components/dictionary/test/page/tabs/calculation.svelte index 9741cf0..cb3d6fa 100644 --- a/src/lib/components/dictionary/test/page/tabs/calculation.svelte +++ b/src/lib/components/dictionary/test/page/tabs/calculation.svelte @@ -11,42 +11,135 @@ let props = $props(); const operators = ['+', '-', '*', '/', '^', '(', ')']; - let expression = $state(''); - let cursorPosition = $state(0); + const logicalop = ['IF', 'THEN', 'ELSE', 'END', 'AND', 'OR', 'NOT', 'MIN', 'MAX']; + const comparisonop = ['=', '!=', '<', '>', '<=', '>=']; - function unselectAll(key) { + let tokens = $state([]); + let cursorIndex = $state(0); + let showLiteralPopover = $state(false); + let literalPopoverType = $state(('string')); +$inspect(tokens) + // let expression = $state(''); + // let cursorPosition = $state(0); + + function uid() { + return Math.random().toString(36).slice(2, 9); + } + + function insertToken(type, value) { + const token = { id: uid(), type, value }; + tokens = [...tokens.slice(0, cursorIndex), token, ...tokens.slice(cursorIndex)]; + cursorIndex += 1; + syncFormState(); + } + + function addValue(val) { + insertToken('test', val); + } + + function addOperator(op) { + const isLogical = logicalop.includes(op); + const isComparison = comparisonop.includes(op); + const type = isLogical ? 'keyword' : isComparison ? 'comparator' : 'operator'; + insertToken(type, op); + } + + function addLiteral(val) { + insertToken('literal', val); + } + + function addNewline() { + insertToken('newline', '\n'); + } + + function moveCursorLeft() { + if (cursorIndex > 0) cursorIndex -= 1; + } + + function moveCursorRight() { + if (cursorIndex < tokens.length) cursorIndex += 1; + } + + function deleteChar() { + if (cursorIndex > 0) { + tokens = [...tokens.slice(0, cursorIndex - 1), ...tokens.slice(cursorIndex)]; + cursorIndex -= 1; + syncFormState(); + } + } + + function clearExpression() { + tokens = []; + cursorIndex = 0; + syncFormState(); + } + + function syncFormState() { + const code = tokens + .map((t) => (t.type === 'newline' ? '\n' : t.value)) + .join(' ') + .replace(/ \n /g, '\n'); + props.calFormState.form.FormulaCode = code; + props.calFormState.validateField?.('FormulaCode', code, false); + } + + function getErrorStatus() { + const selected = props.calFormState.form.FormulaInput; + if (!Array.isArray(selected)) return []; + return selected.map((item) => ({ + value: item.value, + done: tokens.some((t) => t.type === 'test' && t.value === item.value) + })); + } + + function openLiteralPopover(type) { + literalPopoverType = type; + showLiteralPopover = true; + } + + function onLiteralConfirm(val) { + addLiteral(val); + showLiteralPopover = false; + } + + function unselectAll(key) { props.calFormState.form[key] = []; props.calFormState.validateField?.(key, [], false); } - function getErrorStatus(formulaCode = '') { - const selected = props.calFormState.form.FormulaInput; - if (!Array.isArray(selected)) return []; + // function unselectAll(key) { + // props.calFormState.form[key] = []; + // props.calFormState.validateField?.(key, [], false); + // } - return selected.map((item) => ({ - value: item.value, - done: new RegExp(`\\b${item.value}\\b`, 'i').test(formulaCode) - })); - } + // function getErrorStatus(formulaCode = '') { + // const selected = props.calFormState.form.FormulaInput; + // if (!Array.isArray(selected)) return []; - function addToExpression(text) { - const before = expression.slice(0, cursorPosition); - const after = expression.slice(cursorPosition); - expression = before + text + after; - cursorPosition += text.length; - } + // return selected.map((item) => ({ + // value: item.value, + // done: new RegExp(`\\b${item.value}\\b`, 'i').test(formulaCode) + // })); + // } - function addOperator(op) { - addToExpression(op); - props.calFormState.form.FormulaCode = expression; - props.calFormState.validateField?.('FormulaCode', expression, false); - } + // function addToExpression(text) { + // const before = expression.slice(0, cursorPosition); + // const after = expression.slice(cursorPosition); + // expression = before + text + after; + // cursorPosition += text.length; + // } - function addValue(val) { - addToExpression(val); - props.calFormState.form.FormulaCode = expression; - props.calFormState.validateField?.('FormulaCode', expression, false); - } + // function addOperator(op) { + // addToExpression(op); + // props.calFormState.form.FormulaCode = expression; + // props.calFormState.validateField?.('FormulaCode', expression, false); + // } + + // function addValue(val) { + // addToExpression(val); + // props.calFormState.form.FormulaCode = expression; + // props.calFormState.validateField?.('FormulaCode', expression, false); + // } // function handleInput(e) { // expression = e.target.value; @@ -59,63 +152,67 @@ // cursorPosition = e.target.selectionStart; // } - function handleContainerClick(e) { - const rect = e.currentTarget.getBoundingClientRect(); - const text = expression; - const charWidth = 8.5; - const padding = 12; - const clickX = e.clientX - rect.left - padding; - let newPos = Math.floor(clickX / charWidth); - newPos = Math.max(0, Math.min(newPos, text.length)); - cursorPosition = newPos; - } + // function handleContainerClick(e) { + // const rect = e.currentTarget.getBoundingClientRect(); + // const text = expression; + // const charWidth = 8.5; + // const padding = 12; + // const clickX = e.clientX - rect.left - padding; + // let newPos = Math.floor(clickX / charWidth); + // newPos = Math.max(0, Math.min(newPos, text.length)); + // cursorPosition = newPos; + // } - function moveCursorLeft() { - if (cursorPosition > 0) { - cursorPosition -= 1; - } - } + // function moveCursorLeft() { + // if (cursorPosition > 0) { + // cursorPosition -= 1; + // } + // } - function moveCursorRight() { - if (cursorPosition < expression.length) { - cursorPosition += 1; - } - } + // function moveCursorRight() { + // if (cursorPosition < expression.length) { + // cursorPosition += 1; + // } + // } - function deleteChar() { - if (cursorPosition > 0) { - const before = expression.slice(0, cursorPosition - 1); - const after = expression.slice(cursorPosition); - expression = before + after; - cursorPosition -= 1; - props.calFormState.form.FormulaCode = expression; - props.calFormState.validateField?.('FormulaCode', expression, false); - } - } + // function deleteChar() { + // if (cursorPosition > 0) { + // const before = expression.slice(0, cursorPosition - 1); + // const after = expression.slice(cursorPosition); + // expression = before + after; + // cursorPosition -= 1; + // props.calFormState.form.FormulaCode = expression; + // props.calFormState.validateField?.('FormulaCode', expression, false); + // } + // } - function clearExpression() { - expression = ''; - cursorPosition = 0; - props.calFormState.form.FormulaCode = expression; - props.calFormState.validateField?.('FormulaCode', expression, false); - } + // function clearExpression() { + // expression = ''; + // cursorPosition = 0; + // props.calFormState.form.FormulaCode = expression; + // props.calFormState.validateField?.('FormulaCode', expression, false); + // }
Enter Text Value
+ { + if (e.key === 'Enter' && stringLiteralInput.trim()) { + onAddLiteral(`"${stringLiteralInput.trim()}"`); + stringLiteralInput = ''; + } + }} + /> +Enter Number Value
+ { + if (e.key === 'Enter' && numberLiteralInput != null && !isNaN(numberLiteralInput)) { + onAddLiteral(String(numberLiteralInput)); + numberLiteralInput = null; + } + }} + /> +{expressionString}
+