Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { renderMath } from '@pie-lib/math-rendering';
import InlineDropdown from '../respArea/InlineDropdown';

jest.mock('@pie-lib/math-rendering', () => ({
renderMath: jest.fn(),
}));

jest.mock('@tiptap/react', () => ({
NodeViewWrapper: ({ children, ...props }) => (
<div data-testid="node-view-wrapper" {...props}>
Expand Down Expand Up @@ -104,6 +109,32 @@ describe('InlineDropdown', () => {
expect(valueDiv).toBeInTheDocument();
});

it('renders math inside the value control on mount', () => {
const { container } = render(<InlineDropdown {...defaultProps} />);
const valueDiv = container.querySelector('div[style*="border"]');

expect(renderMath).toHaveBeenCalledWith(valueDiv);
});

it('re-renders math when the value changes', () => {
const updatedNode = {
...mockNode,
attrs: {
...mockNode.attrs,
value: '<span>Updated math</span>',
},
};

const { container, rerender } = render(<InlineDropdown {...defaultProps} />);

renderMath.mockClear();

rerender(<InlineDropdown {...defaultProps} node={updatedNode} />);

const valueDiv = container.querySelector('div[style*="border"]');
expect(renderMath).toHaveBeenCalledWith(valueDiv);
});

it('uses 2px horizontal margin on the value control and no horizontal margin on the wrapper', () => {
const { container, getByTestId } = render(<InlineDropdown {...defaultProps} />);
const valueDiv = container.querySelector('div[style*="border"]');
Expand Down Expand Up @@ -476,7 +507,7 @@ describe('InlineDropdown', () => {
await waitFor(() => {
expect(queryByTestId('inline-dropdown-toolbar')).toBeInTheDocument();
});
});
});

it('renders delete control on portaled custom toolbar when container el is set', async () => {
const { findByLabelText } = render(<InlineDropdown {...defaultProps} selected />);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { renderMath } from '@pie-lib/math-rendering';
import PropTypes from 'prop-types';
import { NodeViewWrapper } from '@tiptap/react';
import { NodeSelection } from 'prosemirror-state';
import { Chevron } from '../icons/RespArea';
import ReactDOM from 'react-dom';
import CustomToolbarWrapper from '../../extensions/custom-toolbar-wrapper';
import { setToolbarOpened } from '../../utils/toolbar';

Expand All @@ -16,6 +17,7 @@ const InlineDropdown = (props) => {
const toolbarRef = useRef(null);
const toolbarEditor = useRef(null);
const pendingCloseRequest = useRef(false);
const elementRef = useRef(null);

const isHeld = () =>
editor._holdInlineDropdownToolbarIndex != null &&
Expand Down Expand Up @@ -93,13 +95,16 @@ const InlineDropdown = (props) => {
}
}, [editor, node, selected]);



const isScrollbarClicked = (event) =>
event.clientX > document.documentElement.clientWidth ||
event.clientY > document.documentElement.clientHeight ||
event.target === document.documentElement;


useEffect(() => {
if (elementRef.current && typeof renderMath === 'function') {
renderMath(elementRef.current);
}
}, [value]);

useEffect(() => {
// Calculate position relative to selection
Expand All @@ -113,10 +118,10 @@ const InlineDropdown = (props) => {
});

const handleClickOutside = (event) => {

if( isScrollbarClicked(event) ) {
if (isScrollbarClicked(event)) {
return;
}

const insideSomeEditor = event.target.closest('[data-toolbar-for]');

if (
Expand Down Expand Up @@ -151,6 +156,7 @@ const InlineDropdown = (props) => {
}}
>
<div
ref={elementRef}
style={{
display: 'inline-flex',
minWidth: '178px',
Expand Down
Loading