-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(front) add buttons in the toolbar of ashley for latex
At the moment we can use mathematical latex syntax by pressing $ to start Inline Tex or CMD + M for Tex Block. For users, it would be much easier, if they have access directly in the toolbar to these commands. We, now, add two buttons to suggest these new features.
- Loading branch information
Showing
19 changed files
with
406 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/frontend/js/components/AshleyEditor/LatexButton/index.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { EditorState, ContentState } from 'draft-js'; | ||
import { IntlProvider } from 'react-intl'; | ||
|
||
import { | ||
insertInlineTeX, | ||
insertTeXBlock, | ||
} from 'draft-js-latex-plugin/lib/utils/insertTeX'; | ||
import { TypeLatexStyle } from '../../../types/Enums'; | ||
import { LatexButton } from '.'; | ||
|
||
const editor = EditorState.createWithContent( | ||
ContentState.createFromText('Hello World!'), | ||
); | ||
|
||
const props = { | ||
editorState: editor, | ||
type: TypeLatexStyle.INLINE, | ||
getEditorState: jest.fn(), | ||
setEditorState: jest.fn(), | ||
theme: { | ||
button: '', | ||
buttonWrapper: '', | ||
active: 'active', | ||
}, | ||
}; | ||
jest.mock('draft-js-latex-plugin/lib/utils/insertTeX', () => ({ | ||
insertInlineTeX: jest.fn(), | ||
insertTeXBlock: jest.fn(), | ||
})); | ||
|
||
describe('LatexButton', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('should handle adding inline latex', () => { | ||
const { getByTestId } = render(<LatexButton {...props} />); | ||
const button = getByTestId('addLatexinline'); | ||
fireEvent.click(button); | ||
expect(props.setEditorState).toHaveBeenCalled(); | ||
expect(props.getEditorState).toHaveBeenCalled(); | ||
expect(insertInlineTeX).toHaveBeenCalled(); | ||
expect(insertTeXBlock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows the tooltip text over inline latex button is be present', () => { | ||
const { getByRole, getByTestId } = render( | ||
<IntlProvider locale="en"> | ||
<LatexButton {...props} /> | ||
</IntlProvider>, | ||
); | ||
const button = getByTestId('addLatexinline'); | ||
fireEvent.mouseEnter(button); | ||
getByRole('tooltip', { name: 'Add a Latex inline mathematical formula' }); | ||
}); | ||
|
||
it('should handle adding block latex', () => { | ||
props.type = TypeLatexStyle.BLOCK; | ||
const { getByTestId } = render(<LatexButton {...props} />); | ||
const button = getByTestId('addLatexblock'); | ||
fireEvent.click(button); | ||
expect(props.setEditorState).toHaveBeenCalled(); | ||
expect(props.getEditorState).toHaveBeenCalled(); | ||
expect(insertInlineTeX).not.toHaveBeenCalled(); | ||
expect(insertTeXBlock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows the tooltip text over latex block button is be present', () => { | ||
props.type = TypeLatexStyle.BLOCK; | ||
const { getByRole, getByTestId } = render( | ||
<IntlProvider locale="en"> | ||
<LatexButton {...props} /> | ||
</IntlProvider>, | ||
); | ||
const button = getByTestId('addLatexblock'); | ||
fireEvent.mouseEnter(button); | ||
getByRole('tooltip', { | ||
name: 'Add a Latex mathematical formula in a new block', | ||
}); | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
src/frontend/js/components/AshleyEditor/LatexButton/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { useMemo, useEffect, useRef, useState } from 'react'; | ||
import { DraftJsButtonTheme } from '@draft-js-plugins/buttons'; | ||
import { EditorState } from 'draft-js'; | ||
import { | ||
insertInlineTeX, | ||
insertTeXBlock, | ||
} from 'draft-js-latex-plugin/lib/utils/insertTeX'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import Tooltip from 'rc-tooltip'; | ||
import { messagesEditor } from '../messages'; | ||
import { DraftHandleValue, TypeLatexStyle } from '../../../types/Enums'; | ||
|
||
interface LatexButtonProps { | ||
type: string; | ||
theme: DraftJsButtonTheme; | ||
editorState: EditorState; | ||
getEditorState: () => EditorState; | ||
setEditorState: (editorState: EditorState) => void; | ||
} | ||
|
||
export const LatexButton = (props: LatexButtonProps) => { | ||
const node = useRef<HTMLDivElement>(null); | ||
const { buttonWrapper = '', button, active } = props.theme; | ||
const [isActive, setIsActive] = useState(false); | ||
|
||
const isInlineTeXSelected = () => { | ||
const selection = props.editorState.getSelection(); | ||
const contentState = props.editorState.getCurrentContent(); | ||
const blockKey = selection.getStartKey(); | ||
const block = contentState.getBlockForKey(blockKey); | ||
const offset = selection.getIsBackward() | ||
? selection.getStartOffset() | ||
: selection.getEndOffset(); | ||
const entityKey = block?.getEntityAt(offset); | ||
if (!entityKey) { | ||
return false; | ||
} | ||
return contentState.getEntity(entityKey).getType() === 'INLINETEX'; | ||
}; | ||
|
||
const isLatexBlock = () => { | ||
const selection = props.editorState.getSelection(); | ||
const block = props.editorState | ||
.getCurrentContent() | ||
.getBlockForKey(selection.getStartKey()); | ||
return block.getData().get('type') === 'TEXBLOCK'; | ||
}; | ||
|
||
useEffect(() => { | ||
if (props.type === TypeLatexStyle.BLOCK) { | ||
setIsActive(isLatexBlock()); | ||
} else if (props.type === TypeLatexStyle.INLINE) { | ||
setIsActive(isInlineTeXSelected()); | ||
} | ||
}, [props.editorState]); | ||
|
||
const imageClassName = useMemo( | ||
() => `latex-${props.type} ${button} ${isActive ? active : ''}`, | ||
[props.type, isActive], | ||
); | ||
|
||
const handleAddLatex = ( | ||
e: React.MouseEvent<HTMLButtonElement>, | ||
type: string, | ||
) => { | ||
e.preventDefault(); | ||
let newEditorState; | ||
const editorState = props.getEditorState(); | ||
if (type === TypeLatexStyle.BLOCK) { | ||
newEditorState = insertTeXBlock(editorState); | ||
} else { | ||
newEditorState = insertInlineTeX(editorState); | ||
} | ||
props.setEditorState(newEditorState); | ||
return DraftHandleValue.HANDLED; | ||
}; | ||
return ( | ||
<div className={buttonWrapper} ref={node}> | ||
<Tooltip | ||
overlay={ | ||
<FormattedMessage | ||
{...(props.type === TypeLatexStyle.INLINE | ||
? messagesEditor.toolTipAddLatexInline | ||
: messagesEditor.toolTipAddLatexBlock)} | ||
/> | ||
} | ||
placement="bottom" | ||
> | ||
<button | ||
data-testid={`addLatex${props.type}`} | ||
className={`${imageClassName}`} | ||
onClick={(e) => handleAddLatex(e, props.type)} | ||
type="button" | ||
/> | ||
</Tooltip> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.