Skip to content

Commit

Permalink
Improve formulaBox behaviour on line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
albertprz committed Mar 12, 2024
1 parent 7e04b18 commit bb89b8a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Components/Table/Handler.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import App.Components.Table.HandlerHelpers (cellArrowMove, cellMove, copyCells,
import App.Components.Table.Models (Action(..), AppState, EventTransition(..))
import App.Components.Table.Selection (MultiSelection(..), SelectionState(..))
import App.Evaluator.Formula (mkLocalContext)
import App.Utils.Dom (KeyCode(..), actOnElementById, ctrlKey, displayFunctionType, emptyFormulaBox, emptyFormulaSignature, focusById, focusCell, focusCellElem, isModifierKeyCode, performSyntaxHighlight, prevent, shiftKey, toEvent, toMouseEvent, updateFormulaBox, withPrevent)
import App.Utils.Dom (KeyCode(..), actOnElementById, ctrlKey, displayFunctionType, emptyFormulaBox, emptyFormulaSignature, focusById, focusCell, focusCellElem, introduceFormulaNewLine, isModifierKeyCode, performSyntaxHighlight, prevent, shiftKey, toEvent, toMouseEvent, updateFormulaBox, withPrevent)
import App.Utils.HashMap (lookup2) as HashMap
import App.Utils.Selection (getSelection)
import App.Utils.Selection as Selection
Expand Down Expand Up @@ -71,6 +71,9 @@ handleAction (WriteCell cell value) = do
handleAction (FormulaKeyDown Enter ev)
| ctrlKey ev = withPrevent ev insertFormula

handleAction (FormulaKeyDown Enter ev) =
withPrevent ev (introduceFormulaNewLine *> performSyntaxHighlight)

handleAction (FormulaKeyDown Tab ev) =
withPrevent ev $ focusCell =<< gets _.selectedCell

Expand Down
24 changes: 20 additions & 4 deletions src/Utils/Dom.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import App.Evaluator.Common (LocalFormulaCtx, lookupBuiltinFn, lookupModuleFn, l
import App.Parser.Common (qVar, qVarOp)
import App.SyntaxTree.Common (QVar(..), Var(..), preludeModule)
import App.SyntaxTree.FnDef (FnId, FnSig)
import App.Utils.Common (refEquals, spyShow)
import App.Utils.Common (refEquals)
import App.Utils.Selection (getCaretPosition, getSelection, innerText, setCaretPosition)
import App.Utils.Selection as Selection
import App.Utils.String (last, startsWith) as String
import Bookhound.Parser (runParser)
import Data.Array (filterA)
import Data.Int as Int
import Data.String (Pattern(..), stripSuffix)
import Data.String (Pattern(..))
import Data.String (null, splitAt) as String
import Data.String.CodeUnits (indexOf', lastIndexOf')
import Data.String.CodeUnits (length, slice, takeRight) as String
import Halogen.HTML (HTML, span, text)
Expand Down Expand Up @@ -54,7 +55,6 @@ performSyntaxHighlight = liftEffect do
selection <- getSelection =<< window
caretPosition <- getCaretPosition selection (toNode formulaBox)
formulaText <- getFormulaBoxContents
let x = spyShow "formula" formulaText
updateFormulaBox formulaText
traverse_ (setCaretPosition selection $ toNode formulaBox) caretPosition

Expand Down Expand Up @@ -92,6 +92,22 @@ displayFunctionType ctx = liftEffect do
<$> html
setInnerHTML (toElement formulaSignature) htmlString

introduceFormulaNewLine :: forall m. MonadEffect m => m Unit
introduceFormulaNewLine = liftEffect do
formulaBox <- justSelectElementById formulaBoxId
formulaText <- getFormulaBoxContents
selection <- getSelection =<< window
caretPosition <- getCaretPosition selection (toNode formulaBox)
let
{ before: formulaBegin, after: formulaEnd } =
String.splitAt (fromMaybe zero caretPosition) formulaText
newFormulaText
| String.null formulaEnd = formulaBegin <> "\n\n"
| otherwise = formulaBegin <> "\n" <> formulaEnd
updateFormulaBox newFormulaText
traverse_ (setCaretPosition selection $ toNode formulaBox)
((_ + 1) <$> caretPosition)

syntaxAtomsToElements :: forall a b. Array SyntaxAtom -> Array (HTML a b)
syntaxAtomsToElements = map toElement <<< condenseSyntaxAtoms
where
Expand Down Expand Up @@ -146,8 +162,8 @@ setFormulaBox formulaText = do
formulaBox <- justSelectElementById formulaBoxId
liftEffect $ setInnerHTML (toElement formulaBox) htmlString
where
html = unwrap <$> formulaElements formulaText
htmlString = fold $ StringRenderer.render (const mempty) <$> html
html = unwrap <$> formulaElements formulaText

emptyFormulaBox :: forall m. MonadEffect m => m Unit
emptyFormulaBox = liftEffect
Expand Down

0 comments on commit bb89b8a

Please sign in to comment.