Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close math editor through ESC and new button #2697

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/serlo-editor/math/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { faCheckCircle, faCircle } from '@fortawesome/free-regular-svg-icons'
import { faQuestionCircle } from '@fortawesome/free-solid-svg-icons'
import { faQuestionCircle, faXmark } from '@fortawesome/free-solid-svg-icons'
import clsx from 'clsx'
import { useState, useCallback, createRef, useEffect } from 'react'
import { createPortal } from 'react-dom'
Expand Down Expand Up @@ -77,8 +77,8 @@ export interface MathEditorProps {
onEditorChange(visual: boolean): void
onInlineChange?(inline: boolean): void
onChange(state: string): void

onMoveOutRight: (options?: { closeThroughModal?: boolean }) => void
closeMathEditorOverlay: () => void
onMoveOutRight: () => void
onMoveOutLeft(): void
onDeleteOutRight?(): void
onDeleteOutLeft?(): void
Expand All @@ -98,7 +98,7 @@ export function MathEditor(props: MathEditorProps) {
(event) => {
event.preventDefault()
// close overlay
props.onMoveOutRight()
props.closeMathEditorOverlay()
CodingDive marked this conversation as resolved.
Show resolved Hide resolved
},
{
enableOnFormTags: true,
Expand Down Expand Up @@ -297,11 +297,12 @@ export function MathEditor(props: MathEditorProps) {
{hasError ? mathStrings.onlyLatex : mathStrings.latexEditorTitle}
</p>
<button
onClick={() => props.onMoveOutRight({ closeThroughModal: true })}
className="mr-0.5 mt-1 text-sm font-bold text-gray-600"
onClick={() => props.closeMathEditorOverlay()}
CodingDive marked this conversation as resolved.
Show resolved Hide resolved
className="mr-0.5 mt-1 text-sm font-bold text-gray-600 hover:bg-gray-200 hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-gray-700"
CodingDive marked this conversation as resolved.
Show resolved Hide resolved
aria-label="Close math formula editor"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up: i18n or borrow shorter close string from e.g. strings.share.close.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the English translation, the rest is done automagically through crowdin?

data-qa="plugin-math-close-formula-editor"
>
x
<FaIcon icon={faXmark} />
</button>
</div>
{!isVisualMode && (
Expand Down
1 change: 1 addition & 0 deletions src/serlo-editor/plugins/equations/editor/inline-math.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function InlineMath(props: InlineMathProps) {
onChange={onChange}
onMoveOutRight={onFocusNext}
onMoveOutLeft={onFocusPrevious}
closeMathEditorOverlay={onFocusNext}
/>
)
}
23 changes: 4 additions & 19 deletions src/serlo-editor/plugins/text/components/math-element.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useMemo, useState } from 'react'
import React, { useContext, useMemo } from 'react'
import { Editor, Node, Path, Range, Transforms } from 'slate'
import {
ReactEditor,
Expand Down Expand Up @@ -31,12 +31,7 @@ export function MathElement({
const editor = useSlate()
const selected = useSelected()
const preferences = useContext(PreferenceContext)
const visualModePreferences = !!preferences.getKey(visualEditorPreferenceKey)
const [isVisualMode, setIsVisualMode] = useState(visualModePreferences)

useEffect(() => {
setIsVisualMode(visualModePreferences)
}, [visualModePreferences])
const isVisualMode = !!preferences.getKey(visualEditorPreferenceKey)

const isInsideListElement = useMemo(() => {
return isElementWithinList(element, editor)
Expand Down Expand Up @@ -73,6 +68,7 @@ export function MathElement({
disableBlock={isInsideListElement}
onInlineChange={handleInlineChange}
onChange={(src) => updateElement({ src })}
closeMathEditorOverlay={transformOutOfElement}
onMoveOutRight={transformOutOfElement}
onMoveOutLeft={() => {
transformOutOfElement({ reverse: true })
Expand Down Expand Up @@ -185,11 +181,9 @@ export function MathElement({
function transformOutOfElement({
hejtful marked this conversation as resolved.
Show resolved Hide resolved
reverse = false,
shouldDelete = false,
closeThroughModal,
}: {
reverse?: boolean
shouldDelete?: boolean
closeThroughModal?: boolean
} = {}) {
const unit = 'character'

Expand All @@ -198,15 +192,6 @@ export function MathElement({
Transforms.delete(editor, { unit, reverse })
}

// When calling this function when the 'x' button of the modal is clicked,
// a small timeout is needed to reset the selection. Transforms.deselect()
// was not needed
if (closeThroughModal) {
setTimeout(() => {
ReactEditor.focus(editor)
})
} else {
ReactEditor.focus(editor)
}
ReactEditor.focus(editor)
}
}