Skip to content

Commit

Permalink
feat: insert link plugin tinymce
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvente committed Jan 18, 2024
1 parent 4653322 commit 53ce3a6
Show file tree
Hide file tree
Showing 28 changed files with 1,384 additions and 20 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"fast-xml-parser": "^4.0.10",
"frontend-components-tinymce-advanced-plugins": "^1.0.2",
"lodash-es": "^4.17.21",
"lodash.clonedeep": "^4.5.0",
"lodash.flatten": "^4.4.0",
"moment": "^2.29.4",
"moment-shortformat": "^2.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/editors/data/constants/tinyMCE.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const buttons = StrictDict({
undo: 'undo',
underline: 'underline',
a11ycheck: 'a11ycheck',
insertLink: 'insertlink',
});

export const plugins = listKeyStore([
Expand Down
41 changes: 41 additions & 0 deletions src/editors/sharedComponents/InsertLinkModal/BlockLink/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import PropTypes from 'prop-types';
import { Button } from '@edx/paragon';
import { LinkOff } from '@edx/paragon/icons';
import { formatBlockPath } from '../utils';

import './index.scss';

const BlockLink = ({ path, onCloseLink }) => {
const { title, subTitle } = formatBlockPath(path);
return (
<div className="link-container d-flex row p-4 rounded border border-gray-400 mx-4 mt-3">
<div className="col-10">
<p className="text-left">{subTitle}</p>
<p className="h2 w-20 title">{title}</p>
</div>
<div className="col-2">
<Button
variant="tertiary"
className="d-flex justify-content-center align-items-center"
data-testid="close-link-button"
size="lg"
iconBefore={LinkOff}
onClick={onCloseLink}
>
&nbsp;
</Button>
</div>
</div>
);
};

BlockLink.defaultProps = {
onCloseLink: () => {},

Check warning on line 33 in src/editors/sharedComponents/InsertLinkModal/BlockLink/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlockLink/index.jsx#L33

Added line #L33 was not covered by tests
};

BlockLink.propTypes = {
path: PropTypes.string.isRequired,
onCloseLink: PropTypes.func,
};

export default BlockLink;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.link-container {
.title {
overflow-wrap: break-word;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { formatBlockPath } from '../utils';
import BlockLink from './index';

Enzyme.configure({ adapter: new Adapter() });

describe('BlockLink Component', () => {
const defaultProps = {
path: 'Some Path',
onCloseLink: jest.fn(),
};

test('renders with default props', () => {
const wrapper = shallow(<BlockLink {...defaultProps} />);
expect(wrapper.text()).toContain('Some Path');
});

test('renders correctly with custom path', () => {
const customProps = {
...defaultProps,
path: 'Custom Path',
};
const wrapper = shallow(<BlockLink {...customProps} />);
expect(wrapper.text()).toContain('Custom Path');
});

test('calls onCloseLink when the button is clicked', () => {
const wrapper = shallow(<BlockLink {...defaultProps} />);
wrapper.find({ 'data-testid': 'close-link-button' }).simulate('click');
expect(defaultProps.onCloseLink).toHaveBeenCalledTimes(1);
});

test('renders with valid title and subtitle', () => {
const customProps = {
path: 'Root Section / Child 1',
onCloseLink: jest.fn(),
};
const wrapper = shallow(<BlockLink {...customProps} />);
const { title, subTitle } = formatBlockPath(customProps.path);

expect(wrapper.text()).toContain(title);
expect(wrapper.text()).toContain(subTitle);
});
});
150 changes: 150 additions & 0 deletions src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { useState } from 'react';
import PropTypes from 'prop-types';
import { Button, TransitionReplace, ActionRow } from '@edx/paragon';
import { ArrowForwardIos, ArrowBack } from '@edx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';

import {
blockTypes,
getSectionsList,
getChildrenFromList,
} from '../utils';

import messages from './messages';
import './index.scss';

const BlocksList = ({ blocks, onBlockSelected }) => {
const intl = useIntl();
const messagesTest = {

Check warning on line 18 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L17-L18

Added lines #L17 - L18 were not covered by tests
[blockTypes.section]: intl.formatMessage(
messages.blocksListSubsectionTitle,
),
[blockTypes.subsection]: intl.formatMessage(messages.blocksListUnitTitle),
[blockTypes.unit]: intl.formatMessage(messages.blocksListUnitTitle),
};

const [blockState, setBlockState] = useState({

Check warning on line 26 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L26

Added line #L26 was not covered by tests
blockSelected: {},
type: blockTypes.subsection,
hasNavigated: false,
blocksNavigated: [],
});

const sections = getSectionsList(blocks);
const subSections = getChildrenFromList(

Check warning on line 34 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L33-L34

Added lines #L33 - L34 were not covered by tests
blockState.blockSelected,
blocks,
);
const listItems = blockState.hasNavigated ? subSections : sections;

const isBlockSelectedUnit = blockState.type === blockTypes.unit;

Check warning on line 40 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L40

Added line #L40 was not covered by tests
const blockNameButtonClass = isBlockSelectedUnit ? 'col-12' : 'col-11';

const handleSelectBlock = (block, navigate = false) => {
if (navigate) {
setBlockState({

Check warning on line 45 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L45

Added line #L45 was not covered by tests
...blockState,
blocksNavigated: [...blockState.blocksNavigated, block.id],
blockSelected: block,
type: block.type,
hasNavigated: true,
});
} else {
onBlockSelected(block);

Check warning on line 53 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L52-L53

Added lines #L52 - L53 were not covered by tests
}
};

const handleGoBack = () => {
const newValue = blockState.blocksNavigated.filter(
(id) => id !== blockState.blockSelected.id,

Check warning on line 59 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L57-L59

Added lines #L57 - L59 were not covered by tests
);
if (newValue.length) {
const lastBlockIndex = newValue.length - 1;
const blockId = newValue[lastBlockIndex];
const newBlock = blocks[blockId];
setBlockState({

Check warning on line 65 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L62-L65

Added lines #L62 - L65 were not covered by tests
...blockState,
type: newBlock.type,
hasNavigated: true,
blockSelected: newBlock,
blocksNavigated: newValue,
});
} else {
setBlockState({

Check warning on line 73 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L72-L73

Added lines #L72 - L73 were not covered by tests
...blockState,
type: blockState.section,
hasNavigated: false,
blockSelected: {},
});
}
};

return (

Check warning on line 82 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L82

Added line #L82 was not covered by tests
<>
{blockState.hasNavigated && (
<ActionRow className="w-100 d-flex justify-content-space-between p-3">

Check warning on line 85 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L85

Added line #L85 was not covered by tests
<Button
variant="tertiary"
className="col-1"
onClick={handleGoBack}
iconBefore={ArrowBack}
>
&nbsp;
</Button>

<p className="col-11 text-center">{messagesTest[blockState.type]}</p>
</ActionRow>
)}
<div className="block-list-container">
{listItems.map((block) => (
<TransitionReplace key={`transition_${block.id}`}>

Check warning on line 100 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L100

Added line #L100 was not covered by tests
<ActionRow
key={block.id}
className="w-100 d-flex justify-content-space-between p-3"
>
<Button
variant="tertiary"
className={`${blockNameButtonClass} py-4`}
onClick={() => handleSelectBlock(block)}

Check warning on line 108 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L108

Added line #L108 was not covered by tests
>
<span className="w-100 text-left">{block.displayName}</span>
</Button>
{!isBlockSelectedUnit && (
<Button

Check warning on line 113 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L113

Added line #L113 was not covered by tests
variant="tertiary"
className="col-1 py-4"
onClick={() => handleSelectBlock(block, true)}

Check warning on line 116 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L116

Added line #L116 was not covered by tests
iconAfter={ArrowForwardIos}
>
&nbsp;
</Button>
)}
</ActionRow>
</TransitionReplace>
))}
</div>
</>
);
};

BlocksList.defaultProps = {
onBlockSelected: () => {},

Check warning on line 131 in src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/BlocksList/index.jsx#L131

Added line #L131 was not covered by tests
};

const blockShape = PropTypes.shape({
id: PropTypes.string.isRequired,
blockId: PropTypes.string.isRequired,
lmsWebUrl: PropTypes.string.isRequired,
legacyWebUrl: PropTypes.string.isRequired,
studentViewUrl: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
displayName: PropTypes.string.isRequired,
children: PropTypes.arrayOf(PropTypes.string),
});

BlocksList.propTypes = {
blocks: PropTypes.objectOf(blockShape).isRequired,
onBlockSelected: PropTypes.func,
};

export default BlocksList;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.block-list-container {
height: 200px;
overflow-y: auto;
overflow-x: none;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import Enzyme, { shallow } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';

import BlocksList from '.';

Enzyme.configure({ adapter: new Adapter() });

const mockBlocks = {
block1: { id: 'block1', path: 'Block 1', type: 'section' },
block2: { id: 'block2', path: 'Block 2', type: 'subsection' },
};

describe('BlocksList Component', () => {
// eslint-disable-next-line react/prop-types
const IntlProviderWrapper = ({ children }) => (
<IntlProvider locale="en" messages={{}}>
{children}
</IntlProvider>
);

test('renders without crashing', () => {
const wrapper = shallow(
<IntlProviderWrapper>
<BlocksList blocks={mockBlocks} onBlockSelected={() => {}} />
</IntlProviderWrapper>,
);
expect(wrapper.exists()).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineMessages } from '@edx/frontend-platform/i18n';

const messages = defineMessages({
blocksListSubsectionTitle: {
id: 'blocks.list.subsection.title',
defaultMessage: 'Subsections',
description: 'Title for the subsections blocks',
},
blocksListUnitTitle: {
id: 'blocks.list.unit.title',
defaultMessage: 'Units',
description: 'Title for the units blocks',
},
});

export default messages;
47 changes: 47 additions & 0 deletions src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Button } from '@edx/paragon';
import PropTypes from 'prop-types';

import { formatBlockPath } from '../utils';

const FilterBlock = ({ block, onBlockFilterClick }) => {
const { title, subTitle } = formatBlockPath(block.path);

Check warning on line 7 in src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx#L7

Added line #L7 was not covered by tests

const handleBlockClick = () => {
onBlockFilterClick(block);

Check warning on line 10 in src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx#L9-L10

Added lines #L9 - L10 were not covered by tests
};

return (

Check warning on line 13 in src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx#L13

Added line #L13 was not covered by tests
<Button
key={`filtered_block_${block.id}`}
variant="tertiary"
className="d-flex flex-column w-100 align-items-start p-3"
onClick={handleBlockClick}
data-testid="filter-block-item"
>
<span className="h5 text-left">{subTitle}</span>
<span className="h3">{title}</span>
</Button>
);
};

FilterBlock.defaultProps = {
onBlockFilterClick: () => {},

Check warning on line 28 in src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/FilterBlock/index.jsx#L28

Added line #L28 was not covered by tests
};

const blockShape = PropTypes.shape({
id: PropTypes.string.isRequired,
blockId: PropTypes.string.isRequired,
lmsWebUrl: PropTypes.string.isRequired,
legacyWebUrl: PropTypes.string.isRequired,
studentViewUrl: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
displayName: PropTypes.string.isRequired,
children: PropTypes.arrayOf(PropTypes.string),
});

FilterBlock.propTypes = {
block: PropTypes.objectOf(blockShape).isRequired,
onBlockFilterClick: PropTypes.func,
};

export default FilterBlock;
Loading

0 comments on commit 53ce3a6

Please sign in to comment.