Skip to content

Commit

Permalink
PR Fixes
Browse files Browse the repository at this point in the history
Fixes editor behavior when formula overflows the input.
  • Loading branch information
eireland committed Dec 24, 2024
1 parent 46e3cb5 commit 713642d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
19 changes: 14 additions & 5 deletions v3/src/components/common/edit-formula-modal.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
@use "../vars";
$edit-formula-modal-min-height: 180;
$edit-formula-modal-min-height-px: #{$edit-formula-modal-min-height}px;
$edit-formula-modal-min-width: 400;
$edit-formula-modal-min-width-px: #{$edit-formula-modal-min-width}px;

// https://mattferderer.com/use-sass-variables-in-typescript-and-javascript
:export {
editFormulaModalMinHeight: $edit-formula-modal-min-height;
editFormulaModalMinWidth: $edit-formula-modal-min-width;
}

.codap-modal-content {
min-height: 180px;
min-width: 400px;
min-height: $edit-formula-modal-min-height-px;
min-width: $edit-formula-modal-min-width-px;

.formula-modal-body {
cursor: default;
Expand Down Expand Up @@ -32,9 +42,8 @@
width: 100%;

.formula-editor-input {
height: 100%;
width: 100%;
flex-grow: 1;
width: calc(100% - 50px);

.cm-editor {
height: 100%;
width: 100%;
Expand Down
9 changes: 9 additions & 0 deletions v3/src/components/common/edit-formula-modal.scss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// https://mattferderer.com/use-sass-variables-in-typescript-and-javascript
export interface IEditFormulaModalShared {
editFormulaModalMinHeight: number
editFormulaModalMinWidth: number
}

export const styles: IEditFormulaModalShared

export default styles
37 changes: 20 additions & 17 deletions v3/src/components/common/edit-formula-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Box, Button, Flex, FormControl, FormLabel, Input, ModalBody, ModalCloseButton, ModalFooter, ModalHeader, Tooltip
Box, Button, Flex, FormControl, FormLabel, Input, ModalBody, ModalFooter, Tooltip
} from "@chakra-ui/react"
import React, { useCallback, useEffect, useState } from "react"
import { observer } from "mobx-react-lite"
Expand All @@ -13,7 +13,7 @@ import { InsertFunctionMenu } from "./formula-insert-function-menu"
import { InsertValuesMenu } from "./formula-insert-values-menu"
import ResizeHandle from "../../assets/icons/icon-corner-resize-handle.svg"

import "./edit-formula-modal.scss"
import styles from './edit-formula-modal.scss'

interface IProps {
applyFormula: (formula: string) => void
Expand All @@ -29,14 +29,19 @@ interface IProps {
export const EditFormulaModal = observer(function EditFormulaModal({
applyFormula, formulaPrompt, isOpen, onClose, titleInput, titleLabel, titlePlaceholder, value
}: IProps) {
const minWidth = 400
const minHeight = 180
const minWidth = +styles.editFormulaModalMinWidth
const minHeight = +styles.editFormulaModalMinHeight
const headerHeight = 36
const footerHeight = 56
const insertButtonsHeight = 35

const modalContentRef = React.useRef<HTMLDivElement>(null)
const [showValuesMenu, setShowValuesMenu] = useState(false)
const [showFunctionMenu, setShowFunctionMenu] = useState(false)
const formulaEditorState = useFormulaEditorState(value ?? "")
const { formula, setFormula } = formulaEditorState
const [dimension, setDimension] = useState({ width: minWidth, height: minHeight })

const [dimensions, setDimensions] = useState({ width: minWidth, height: minHeight })
const editorHeight = dimensions.height - headerHeight - footerHeight - insertButtonsHeight

useEffect(() => {
setFormula(value || "")
Expand All @@ -52,7 +57,7 @@ export const EditFormulaModal = observer(function EditFormulaModal({
setShowFunctionMenu(false)
setFormula(value || "")
onClose?.()
setDimension({ width: minWidth, height: minHeight })
setDimensions({ width: minWidth, height: minHeight })
}

const handleModalWhitespaceClick = () => {
Expand Down Expand Up @@ -97,9 +102,9 @@ export const EditFormulaModal = observer(function EditFormulaModal({
e.currentTarget.setPointerCapture(e.pointerId)

Check warning on line 102 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L102

Added line #L102 was not covered by tests
}

const modalRect = document.querySelector(".codap-modal-content")?.getBoundingClientRect()
const startWidth = modalRect?.width ?? dimension.width
const startHeight = modalRect?.height ?? dimension.height
const modalRect = modalContentRef.current?.getBoundingClientRect()
const startWidth = modalRect?.width ?? dimensions.width
const startHeight = modalRect?.height ?? dimensions.height
const startPosition = {x: e.pageX, y: e.pageY}

Check warning on line 108 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L108

Added line #L108 was not covered by tests

let resizingWidth = startWidth, resizingHeight = startHeight

Check warning on line 110 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L110

Added line #L110 was not covered by tests
Expand All @@ -109,25 +114,23 @@ export const EditFormulaModal = observer(function EditFormulaModal({
const yDelta = pointerMoveEvent.pageY - startPosition.y
resizingWidth = Math.max(startWidth + xDelta, minWidth)
resizingHeight = Math.max(startHeight + yDelta, minHeight)
setDimension({
width: resizingWidth, height: resizingHeight,
})
setDimensions({width: Math.round(resizingWidth), height: Math.round(resizingHeight)})

Check warning on line 117 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L112-L117

Added lines #L112 - L117 were not covered by tests
}
const onPointerUp = () => {
document.body.removeEventListener("pointermove", onPointerMove, { capture: true })
document.body.removeEventListener("pointerup", onPointerUp, { capture: true })

Check warning on line 121 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L119-L121

Added lines #L119 - L121 were not covered by tests
}
document.body.addEventListener("pointermove", onPointerMove, { capture: true })
document.body.addEventListener("pointerup", onPointerUp, { capture: true })

Check warning on line 124 in v3/src/components/common/edit-formula-modal.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/edit-formula-modal.tsx#L123-L124

Added lines #L123 - L124 were not covered by tests
}, [dimension.height, dimension.width])
}, [dimensions.height, dimensions.width, minHeight, minWidth])

return (
<FormulaEditorContext.Provider value={formulaEditorState}>
<CodapModal
isOpen={isOpen}
onClose={closeModal}
modalWidth={`${dimension.width}px`}
modalHeight={`${dimension.height}px`}
modalWidth={`${dimensions.width}px`}
modalHeight={`${dimensions.height}px`}
onClick={handleModalWhitespaceClick}
>
<ModalBody className="formula-modal-body" onKeyDown={handleKeyDown}>
Expand All @@ -146,7 +149,7 @@ export const EditFormulaModal = observer(function EditFormulaModal({
</FormLabel>
<FormLabel className="formula-editor-container">
{formulaPrompt ?? t("DG.AttrFormView.formulaPrompt")}
<FormulaEditor />
<FormulaEditor editorHeight={editorHeight}/>
</FormLabel>
</FormControl>
<Flex className="formula-insert-buttons-container" flexDirection="row" justifyContent="flex-start">
Expand Down
8 changes: 6 additions & 2 deletions v3/src/components/common/formula-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { formulaLanguageWithHighlighting } from "../../models/formula/lezer/form
import { getGlobalValueManager, getSharedModelManager } from "../../models/tiles/tile-environment"
import { FormulaEditorApi, useFormulaEditorContext } from "./formula-editor-context"

import styles from './edit-formula-modal.scss'

interface ICompletionOptions {
attributes: boolean
boundaries: boolean
Expand All @@ -35,6 +37,7 @@ const kAllOptions: ICompletionOptions = {
interface IProps {
// options default to true if not specified
options?: Partial<ICompletionOptions>
editorHeight?: number
}

/*
Expand Down Expand Up @@ -255,7 +258,7 @@ function cmExtensionsSetup() {
return extensions.filter(Boolean)
}

export function FormulaEditor({ options: _options }: IProps) {
export function FormulaEditor({ options: _options, editorHeight = +styles.editFormulaModalMinHeight }: IProps) {
const dataSet = useDataSetContext()
const jsonOptions = JSON.stringify(_options ?? {})
const options = useMemo(() => JSON.parse(jsonOptions), [jsonOptions])
Expand Down Expand Up @@ -283,6 +286,7 @@ export function FormulaEditor({ options: _options }: IProps) {
// .input-element indicates to CodapModal not to drag the modal from within the element
const classes = "formula-editor-input input-element"
return <CodeMirror ref={cmRef} className={classes} data-testid="formula-editor-input" height="70px"
basicSetup={false} extensions={extensions} onCreateEditor={handleCreateEditor}
basicSetup={false} extensions={extensions} style={{height: editorHeight}}
onCreateEditor={handleCreateEditor}
value={formula} onChange={handleFormulaChange} />
}

0 comments on commit 713642d

Please sign in to comment.