Skip to content

Commit

Permalink
open tab if footnote in it
Browse files Browse the repository at this point in the history
  • Loading branch information
dobri1408 committed Jan 23, 2024
1 parent c4162c9 commit 191c39a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/Blocks/Footnote/FootnotesBlockView.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {
openAccordionIfContainsFootnoteReference,
openAccordionOrTabIfContainsFootnoteReference,
getAllBlocksAndSlateFields,
makeFootnoteListOfUniqueItems,
makeFootnote,
Expand Down Expand Up @@ -107,7 +107,7 @@ const FootnotesBlockView = (props) => {
href={`#ref-${parentUid || uid}`}
aria-label="Back to content"
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#ref-${parentUid || uid}`,
)
}
Expand All @@ -122,7 +122,7 @@ const FootnotesBlockView = (props) => {
href={`#ref-${ref}`}
aria-label="Back to content"
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#ref-${ref}`,
)
}
Expand All @@ -139,7 +139,7 @@ const FootnotesBlockView = (props) => {
href={`#ref-${parentUid || uid}`}
aria-label="Back to content"
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#ref-${parentUid || uid}`,
)
}
Expand Down
10 changes: 5 additions & 5 deletions src/editor/render.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEditorContext } from '@plone/volto-slate/hooks';
import { getAllBlocksAndSlateFields } from '@eeacms/volto-slate-footnote/editor/utils';
import {
makeFootnoteListOfUniqueItems,
openAccordionIfContainsFootnoteReference,
openAccordionOrTabIfContainsFootnoteReference,
} from './utils';
import { isEmpty } from 'lodash';
import { useSelector } from 'react-redux';
Expand Down Expand Up @@ -112,7 +112,7 @@ export const FootnoteElement = (props) => {
as={UniversalLink}
href={`#footnote-${citationRefId}`}
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#footnote-${citationRefId}`,
)
}
Expand All @@ -134,7 +134,7 @@ export const FootnoteElement = (props) => {
as={UniversalLink}
href={`#footnote-${item.zoteroId || item.uid}`}
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#footnote-${item.zoteroId || item.uid}`,
)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export const FootnoteElement = (props) => {
as={UniversalLink}
href={`#footnote-${citationRefId}`}
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#footnote-${citationRefId}`,
)
}
Expand All @@ -198,7 +198,7 @@ export const FootnoteElement = (props) => {
as={UniversalLink}
href={`#footnote-${item.zoteroId || item.uid}`}
onClick={() =>
openAccordionIfContainsFootnoteReference(
openAccordionOrTabIfContainsFootnoteReference(
`#footnote-${item.zoteroId || item.uid}`,
)
}
Expand Down
21 changes: 20 additions & 1 deletion src/editor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,34 @@ const retriveValuesOfSlateFromNestedPath = (path, value) => {
* Will open accordion if contains footnote reference
* @param {string} footnoteId
*/
export const openAccordionIfContainsFootnoteReference = (footnoteId) => {
export const openAccordionOrTabIfContainsFootnoteReference = (footnoteId) => {
if (typeof window !== 'undefined') {
const footnote = document.querySelector(footnoteId);

if (footnote !== null && footnote.closest('.accordion') !== null) {
const comp = footnote.closest('.accordion').querySelector('.title');
if (!comp.className.includes('active')) {
comp.click();
}
}

if (footnote !== null && footnote.closest('.tab-name') !== null) {
const idOfTabBlock = footnote.closest('.tabs-block').id;

const idOfTabPanel = footnote.closest('.tab-name').id;
const allTabs = document
.getElementById(idOfTabBlock)
.getElementsByClassName('menu-item-text');

(Array.from(allTabs) || []).forEach((tab) => {
if (
tab.textContent === idOfTabPanel &&
!tab.parentElement.classList.contains('active')
) {
tab.parentElement.click();
}
});
}
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions src/editor/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
makeFootnote,
openAccordionIfContainsFootnoteReference,
openAccordionOrTabIfContainsFootnoteReference,
getAllBlocksAndSlateFields,
} from './utils';
import { getAllBlocks } from '@plone/volto-slate/utils';
Expand All @@ -22,7 +22,7 @@ describe('makeFootnote', () => {
});
});

describe('openAccordionIfContainsFootnoteReference', () => {
describe('openAccordionOrTabIfContainsFootnoteReference', () => {
it('should open accordion if it contains footnote reference', () => {
document.body.innerHTML = `
<div class="accordion">
Expand All @@ -34,7 +34,7 @@ describe('openAccordionIfContainsFootnoteReference', () => {
const title = document.querySelector('.title');
title.click = jest.fn();

openAccordionIfContainsFootnoteReference('#footnote');
openAccordionOrTabIfContainsFootnoteReference('#footnote');

expect(title.click).toHaveBeenCalled();
});
Expand All @@ -49,7 +49,7 @@ describe('openAccordionIfContainsFootnoteReference', () => {
const title = document.querySelector('.title');
title.click = jest.fn();

openAccordionIfContainsFootnoteReference('#footnote');
openAccordionOrTabIfContainsFootnoteReference('#footnote');

expect(title.click).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit 191c39a

Please sign in to comment.