Skip to content

Commit 16bc073

Browse files
committed
www(доработка имеющегося функционала): fix anchor routing
fix anchor routing
1 parent 6887433 commit 16bc073

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

www/components/driver-content-nav/DriverContentNav.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Menu} from "antd";
55
import {observer} from "mobx-react-lite";
66
import {useContentNav} from "../../contexts/content-nav-context";
77
import { useSharedStore } from "../../stores/shared-store";
8+
import { goToAnchor } from "../../utils/scroll.utls";
89

910

1011
interface IDriverContentNav {
@@ -30,8 +31,9 @@ export const DriverContentNav = observer<IDriverContentNav>( ( { className } ) =
3031

3132
// event.preventDefault();
3233

33-
// let link = event.target as HTMLLinkElement;
34-
// let elementId = link.href.replace( /(.*)?#/, `` );
34+
// let link = event.target as HTMLLinkElement;
35+
// let elementId = link.href.replace( /(.*)?#/, `` );
36+
// goToAnchor()
3537
// let element = document.getElementById( decodeURIComponent( elementId ) ) as HTMLElement;
3638
// let clientRect = element.getBoundingClientRect();
3739
//

www/hooks/content-autoscroll-hook.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { goToAnchor } from "../utils/scroll.utls";
55

66
export function useContentAutoscroll(){
77
const router = useRouter();
8-
8+
useLayoutEffect(()=>{
9+
let hash = location.hash.slice( 1 );
10+
if ( hash ) {
11+
goToAnchor( hash );
12+
}
13+
},[])
914
useEffect(() => {
1015
function scrollHandler() {
1116
// if (window.scrollX === 0) {
@@ -24,15 +29,17 @@ export function useContentAutoscroll(){
2429
}
2530

2631
function startHashChangeHandler() {
27-
setTimeout( () => {
28-
let hash = location.hash.slice( 1 );
2932

33+
34+
requestAnimationFrame(()=>{
35+
let hash = location.hash.slice( 1 );
3036
goToAnchor( hash );
31-
window.addEventListener( "scroll", scrollHandler );
32-
}, 100 );
37+
})
38+
// window.addEventListener( "scroll", scrollHandler );
3339
}
3440

35-
window.addEventListener("scroll", scrollHandler);
41+
42+
// window.addEventListener("scroll", scrollHandler);
3643

3744

3845
router.events.on(`hashChangeStart`, startHashChangeHandler);

www/hooks/use-doc-search.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ export const useDocSearch = () => {
55
const router = useRouter();
66

77

8-
useLayoutEffect(() => {
8+
useLayoutEffect( () => {
99
// @ts-ignore
10-
let ds = docsearch({
10+
let ds = docsearch( {
1111
apiKey: '0260af6591f616d3300bc3b89d75cfd1',
1212
indexName: 'nauchikus_typescript-definitive-guide',
1313
inputSelector: '#algolia-search',
1414
debug: false,
1515
/// TODO: [refactoring] add types
16-
handleSelected: function (input: any, event: any, suggestion: any, datasetNumber: any, context: any) {
17-
if (context.selectionMethod === 'click') {
18-
input.setVal('');
16+
handleSelected: function ( input: any, event: any, suggestion: any, datasetNumber: any, context: any ) {
17+
if ( context.selectionMethod === 'click' ) {
18+
input.setVal( '' );
1919

2020
let url = suggestion.url;
2121
let pattern = `/#gatsby-focus-wrapper`;
2222

23-
if (url.endsWith(pattern)) {
24-
url = url.substring(0, url.length - pattern.length);
23+
if ( url.endsWith( pattern ) ) {
24+
url = url.substring( 0, url.length - pattern.length );
2525
}
2626

2727
let origin = `https://typescript-definitive-guide.ru`;
2828

2929

30-
if (process.env.NODE_ENV === `development`) {
30+
if ( process.env.NODE_ENV === `development` ) {
3131
origin = `http://localhost:8000`;
3232

3333
url = url.replace(
@@ -37,24 +37,24 @@ export const useDocSearch = () => {
3737
}
3838

3939

40-
url = url.replace(origin, ``);
40+
url = url.replace( origin, `` );
4141

4242
router.push( url );
4343
}
4444
}
45-
});
45+
} );
4646

47-
let input = ds.autocomplete[0];
47+
let input = ds.autocomplete[ 0 ];
4848
let autocomplete = ds.autocomplete.autocomplete;
4949

5050
const input_blurHandler = () => {
51-
autocomplete.setVal('');
51+
autocomplete.setVal( '' );
5252
};
5353

54-
input.addEventListener(`blur`, input_blurHandler);
54+
input.addEventListener( `blur`, input_blurHandler );
5555

5656
return () => {
57-
input.removeEventListener(`blur`, input_blurHandler);
57+
input.removeEventListener( `blur`, input_blurHandler );
5858
};
59-
});
59+
}, [] );
6060
}

www/pages/book/chapters/[chapterId].tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {GetStaticPaths, GetStaticProps, NextPage} from 'next'
22
import Link from 'next/link';
33
import Head from "next/head";
44
import {ContentSlideLayer, DriverSlideLayer, SlideLayer} from "../../../components/layers/slide-layer/SlideLayer";
5-
import React, {useEffect, useMemo} from "react";
5+
import React, { useEffect, useLayoutEffect, useMemo } from "react";
66
import {createBox, generateContentSectionIncrementalId, generateIndex} from "../../../utils/string-utils";
77

88

@@ -92,12 +92,12 @@ const Chapters = observer<Chapters>(({pageDescription, urlResolver, sectionInfoA
9292
contentNavService.update(contentNavData);
9393
}, [contentNavData]);
9494

95+
96+
9597
const contentNavStore = useContentNavStore(contentNavService);
9698

9799
useContentAutoscroll();
98100

99-
// http://localhost:3000/book/chapters/Readonly,Partial,Required,Pick,Record
100-
101101

102102
const sections = sectionInfoAll.map(({key, elementId, markdown}) => {
103103
return (

www/services/Scroll.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export class Scroll {
4343
Scroll.getClientRectById(Scroll.getCurrentAnchor());
4444
static scrollToCurrentHash = () => {
4545
if (Scroll.isHash) {
46-
window.scrollTo({
47-
left: 0,
48-
top: Scroll.isAutoscrollComplete ? pageYOffset - Scroll.offset.offset : pageYOffset
49-
});
46+
// window.scrollTo({
47+
// left: 0,
48+
// top: Scroll.isAutoscrollComplete ? pageYOffset - Scroll.offset.offset : pageYOffset
49+
// });
5050
}
5151
}
5252
static scrollTo = () => {
@@ -55,9 +55,9 @@ export class Scroll {
5555
static scrollToAnchor = (anchor: string) => {
5656
let clientRect = Scroll.getClientRectById(anchor);
5757

58-
window.scrollTo({
59-
left: 0,
60-
top: clientRect.top + pageYOffset - Scroll.offset.offset
61-
});
58+
// window.scrollTo({
59+
// left: 0,
60+
// top: clientRect.top + pageYOffset - Scroll.offset.offset
61+
// });
6262
}
63-
}
63+
}

www/stores/use-content-active-section-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ export const useContentActiveSectionStore = (): ContentActiveSectionStore =>{
7272

7373

7474
return store;
75-
}
75+
}

0 commit comments

Comments
 (0)