Skip to content

Commit

Permalink
fix: disable tooltip with no selected text and external url edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvente committed Mar 15, 2024
1 parent 095aa67 commit 7055c2b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/editors/sharedComponents/InsertLinkModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Button,
Tabs,
Tab,
Form,
} from '@openedx/paragon';
import { actions, selectors } from '../../data/redux/insertlink';
import BaseModal from '../BaseModal';
Expand All @@ -33,7 +32,6 @@ const InsertLinkModal = ({
const [blocksList, setBlocksList] = useState(null);
const [, setInvalidUrlInput] = useState(false);
const [inputUrlValue, setInputUrlValue] = useState('');
const [errorUrlNotSelected, setErrorUrlNotSelected] = useState(false);
const dispatch = useDispatch();
const { selectedBlocks } = useSelector(selectors.insertlinkState);

Expand Down Expand Up @@ -112,33 +110,43 @@ const InsertLinkModal = ({
const editor = editorRef.current;
if (editor) {
const selectedHTML = editor.selection.getContent({ format: 'html' });
const regex = /data-block-id="([^"]+)"/;
const match = selectedHTML.match(regex);
const regexDataBlockId = /data-block-id="([^"]+)"/;
const regexHref = /href="([^"]+)"/;
const matchDataBlockId = selectedHTML.match(regexDataBlockId);
const matchHreUrl = selectedHTML.match(regexHref);

// Extracting the value from the match
const dataBlockId = match ? match[1] : null;
const dataBlockId = matchDataBlockId ? matchDataBlockId[1] : null;
const hrefUrl = matchHreUrl ? matchHreUrl[1] : null;
const blockSelectedUrl = selectedBlocks?.[dataBlockId]?.lmsWebUrl;
const hasExternalUrl = hrefUrl !== blockSelectedUrl;

if (selectedHTML && !dataBlockId) {
const selectedNode = editor.selection.getNode();
const parentNode = editor.dom.getParent(selectedNode, 'a');

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

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/index.jsx#L125-L126

Added lines #L125 - L126 were not covered by tests
if (parentNode) {
const dataBlockIdParent = parentNode.getAttribute('data-block-id');
const url = parentNode.getAttribute('href');
const blockIsValid = dataBlockIdParent in selectedBlocks;
if (dataBlockIdParent && blockIsValid) {
const hasValidUrl = url === blockSelectedUrl;

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

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/index.jsx#L128-L131

Added lines #L128 - L131 were not covered by tests
if (dataBlockIdParent && blockIsValid && hasValidUrl) {
setBlocksSelected(selectedBlocks[dataBlockIdParent]);
} else {
setBlocksSelected(null);

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

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/index.jsx#L133-L135

Added lines #L133 - L135 were not covered by tests
}
}
}

if (dataBlockId) {
if (dataBlockId && hasExternalUrl) {
setBlocksSelected(null);
}

if (dataBlockId && !hasExternalUrl) {
const blockIsValid = dataBlockId in selectedBlocks;

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

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/index.jsx#L145

Added line #L145 was not covered by tests
if (dataBlockId && blockIsValid) {
setBlocksSelected(selectedBlocks[dataBlockId]);

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

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/InsertLinkModal/index.jsx#L147

Added line #L147 was not covered by tests
}
}

if (!selectedHTML) {
setErrorUrlNotSelected(true);
}
}
}, []);

Expand All @@ -148,7 +156,7 @@ const InsertLinkModal = ({
close={onClose}
title={intl.formatMessage(messages.insertLinkModalTitle)}
confirmAction={(
<Button variant="primary" onClick={handleSave} disabled={errorUrlNotSelected}>
<Button variant="primary" onClick={handleSave}>
{intl.formatMessage(messages.insertLinkModalButtonSave)}
</Button>
)}
Expand All @@ -167,24 +175,17 @@ const InsertLinkModal = ({
title={intl.formatMessage(messages.insertLinkModalCoursePagesTabTitle)}
className="col-12 w-100 tabs-container"
>
{errorUrlNotSelected && (
<Form.Control.Feedback type="invalid" className="mt-4">
{intl.formatMessage(messages.insertLinkModalUrlNotSelectedErrorMessage)}
</Form.Control.Feedback>
)}

<SearchBlocks
blocks={blocksList || {}}
onSearchFilter={handleSearchedBlocks}
searchInputValue={searchField}
onBlockSelected={handleSelectedBlock}
disabledBlocks={errorUrlNotSelected}
/>
{!blocksSearched && (
<BlocksList
blocks={blocksList || {}}
onBlockSelected={handleSelectedBlock}
disableBlocks={errorUrlNotSelected}
/>
)}
</Tab>
Expand Down
5 changes: 5 additions & 0 deletions src/editors/sharedComponents/TinyMceWidget/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ export const setupCustomBehavior = ({
icon: 'new-tab',
tooltip: translations?.insertLinkTooltipTitle ?? '',
onAction: openInsertLinkModal,
onSetup(api) {
editor.on('SelectionChange', () => {
api.setDisabled(editor.selection.getContent().length === 0);

Check warning on line 167 in src/editors/sharedComponents/TinyMceWidget/hooks.js

View check run for this annotation

Codecov / codecov/patch

src/editors/sharedComponents/TinyMceWidget/hooks.js#L165-L167

Added lines #L165 - L167 were not covered by tests
});
},
});

// overriding the code plugin's icon with 'HTML' text
Expand Down

0 comments on commit 7055c2b

Please sign in to comment.