-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANET-4801 Only search DOM on frontend, useSelect in editor
* Since we only need to watch for changes in the editor, we don't need to make the code that queries the DOM update the block when the DOM changes. Instead we can use `useSelect` to get the blocks and filter the headers (and potentially other blocks that can output a heading). * Decouple fetching the headers from creating a hierarchical representation. * Just fetch the headings in render of SubmenuFrontend. This component doesn't get re-rendered anyway, and also the headings won't change (unless other blocks still need to be rendered, we probably should check that). * Split functions into dedicated files. * Make backtop into a component. This can be put anywhere on the page since it is a fixed display element. Since there can only be one SubMenu block, and there is no other way to add a backtop, we just add it there. Though maybe we should decouple this and make this into a setting on the page. * Use plain old links instead of implementing our own behavior. Mainly did this to remove (a lot of) complexity from the code while doing other changes. We should probably evaluate if we still want to do a transition, though this can probably still make use of anchors.
- Loading branch information
Showing
12 changed files
with
206 additions
and
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import useScrollPosition from '@react-hook/window-scroll'; | ||
|
||
export const BackTop = ({}) => { | ||
const cookies = document.getElementById('set-cookie'); | ||
|
||
const cookiesVisible = cookies && cookies.style.display !== 'none'; | ||
|
||
const scrollPosition = useScrollPosition(60); | ||
|
||
return scrollPosition < 400 ? '' : <a | ||
className={ 'back-top' } | ||
style={ { | ||
display: 'block', | ||
bottom: cookiesVisible ? '120px' : '50px', | ||
} } | ||
onClick={ () => window.scrollTo(0, 0) } | ||
/>; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import { Fragment, useEffect } from '@wordpress/element'; | ||
import { getSubmenuStyle, addSubmenuActions } from './submenuFunctions'; | ||
import { getSubmenuStyle } from './getSubmenuStyle'; | ||
import { SubmenuItems } from './SubmenuItems'; | ||
import { useSubmenuItemsLoad } from './useSubmenuItemsLoad'; | ||
import { makeHierarchical } from './makeHierarchical'; | ||
import { getHeadingsFromDom } from './getHeadingsFromDom'; | ||
import { BackTop } from './BackTop'; | ||
|
||
export const SubmenuFrontend = ({ title, className, levels, submenu_style }) => { | ||
|
||
const { menuItems } = useSubmenuItemsLoad(levels, false); | ||
|
||
useEffect(() => addSubmenuActions(menuItems), [menuItems]); | ||
|
||
const headings = getHeadingsFromDom(levels); | ||
const menuItems = makeHierarchical(headings); | ||
const style = getSubmenuStyle(className, submenu_style); | ||
|
||
return ( | ||
<section className={`block submenu-block submenu-${style}`}> | ||
<h2>{title}</h2> | ||
<SubmenuItems menuItems={menuItems} /> | ||
<section className={ `block submenu-block submenu-${ style }` }> | ||
<h2>{ title }</h2> | ||
<SubmenuItems menuItems={ menuItems }/> | ||
<BackTop/> | ||
</section> | ||
); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const generateAnchor = text => text.toLowerCase().trim().replace(/ /g, '-') | ||
|
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,26 @@ | ||
import { generateAnchor } from './generateAnchor'; | ||
|
||
const getHeadingLevel = heading => Number(heading.tagName.replace('H', '')); | ||
|
||
export const getHeadingsFromDom = (selectedLevels) => { | ||
const container = document.querySelector('.page-template'); | ||
// Get all heading tags that we need to query | ||
const headingsSelector = selectedLevels.map(level => `:not(.submenu-block) h${level.heading}`); | ||
|
||
return [...container.querySelectorAll(headingsSelector)].map(heading=> { | ||
const levelConfig = selectedLevels.find((selected) => selected.heading === getHeadingLevel(heading)) | ||
|
||
if (!heading.id) { | ||
heading.id = generateAnchor(heading.textContent); | ||
} | ||
|
||
return ({ | ||
content: heading.textContent, | ||
level: levelConfig.heading, | ||
style: levelConfig.style, | ||
shouldLink: levelConfig.link, | ||
anchor: heading.id, | ||
}); | ||
}); | ||
} | ||
|
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,13 @@ | ||
// Map for old attribute 'submenu_style' | ||
const SUBMENU_STYLES = { | ||
1: 'long', | ||
2: 'short', | ||
3: 'sidebar' | ||
}; | ||
|
||
export const getSubmenuStyle = (className, submenu_style) => { | ||
if (className && className.includes('is-style-')) { | ||
return className.split('is-style-')[1]; | ||
} | ||
return submenu_style ? SUBMENU_STYLES[submenu_style] : 'long'; | ||
}; |
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,33 @@ | ||
export const makeHierarchical = headings => { | ||
let previousMenuItem; | ||
|
||
return headings.reduce((menuItems, heading) => { | ||
const { level, shouldLink, anchor, content, style } = heading; | ||
|
||
// const parent = deeperThanPrevious ? previousHeading.children : menuItems; | ||
let possibleParent = previousMenuItem || menuItems; | ||
|
||
while (possibleParent.level && possibleParent.level >= level) { | ||
possibleParent = possibleParent.parent; | ||
} | ||
|
||
const parent = possibleParent; | ||
|
||
const container = parent === menuItems ? menuItems : parent.children; | ||
|
||
const menuItem = { | ||
text: content, | ||
style: style, | ||
children: [], | ||
parent: parent, | ||
level, | ||
shouldLink, | ||
anchor, | ||
}; | ||
container.push(menuItem); | ||
|
||
previousMenuItem = menuItem; | ||
|
||
return menuItems; | ||
}, []); | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.