From 9524727391f95fe1bc63101dc7a0f17c1bc083bb Mon Sep 17 00:00:00 2001 From: Ivan <8611739+IRBorisov@users.noreply.github.com> Date: Wed, 14 May 2025 10:47:15 +0300 Subject: [PATCH] B: Fix boolean envelopement --- .../dlg-create-block/tab-block-children.tsx | 2 -- .../components/rs-input/text-editing.ts | 36 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/rsconcept/frontend/src/features/oss/dialogs/dlg-create-block/tab-block-children.tsx b/rsconcept/frontend/src/features/oss/dialogs/dlg-create-block/tab-block-children.tsx index def07853..4f7d6146 100644 --- a/rsconcept/frontend/src/features/oss/dialogs/dlg-create-block/tab-block-children.tsx +++ b/rsconcept/frontend/src/features/oss/dialogs/dlg-create-block/tab-block-children.tsx @@ -17,8 +17,6 @@ export function TabBlockChildren() { const children_operations = useWatch({ control, name: 'children_operations' }); const exclude = parent ? [-parent, ...manager.oss.hierarchy.expandAllInputs([-parent]).filter(id => id < 0)] : []; - console.log(exclude); - const value = [...children_blocks.map(id => -id), ...children_operations]; function handleChangeSelected(newValue: number[]) { diff --git a/rsconcept/frontend/src/features/rsform/components/rs-input/text-editing.ts b/rsconcept/frontend/src/features/rsform/components/rs-input/text-editing.ts index bbc089d0..26d6ede1 100644 --- a/rsconcept/frontend/src/features/rsform/components/rs-input/text-editing.ts +++ b/rsconcept/frontend/src/features/rsform/components/rs-input/text-editing.ts @@ -1,6 +1,6 @@ // Formatted text editing helpers -import { type ReactCodeMirrorRef } from '@uiw/react-codemirror'; +import { type ReactCodeMirrorRef, type SelectionRange } from '@uiw/react-codemirror'; import { CodeMirrorWrapper } from '@/utils/codemirror'; @@ -145,8 +145,7 @@ export class RSTextWrapper extends CodeMirrorWrapper { return true; } case TokenID.BOOLEAN: { - const selStart = selection.from; - if (hasSelection && this.ref.view.state.sliceDoc(selStart, selStart + 1) === 'ℬ') { + if (hasSelection && this.startsWithBoolean(selection)) { this.envelopeWith('ℬ', ''); } else { this.envelopeWith('ℬ(', ')'); @@ -285,4 +284,35 @@ export class RSTextWrapper extends CodeMirrorWrapper { } return false; } + + private startsWithBoolean(range: SelectionRange): boolean { + const text = this.ref.view.state.sliceDoc(range.from, range.to); + if (!text.startsWith('ℬ') || !text.endsWith(')')) { + return false; + } + const openParenIndex = text.indexOf('(', 1); + if (openParenIndex === -1) { + return false; + } + for (const char of text.slice(0, openParenIndex)) { + if (char !== 'ℬ') { + return false; + } + } + const bracketsContent = text.slice(openParenIndex + 1, text.length - 1); + return this.isValidBracketSequence(bracketsContent); + } + + private isValidBracketSequence(text: string): boolean { + let depth = 0; + for (const char of text) { + if (char === '(') { + depth++; + } else if (char === ')') { + depth--; + if (depth < 0) return false; + } + } + return depth === 0; + } }