Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:anchor): url fragment change on click - nzReplace #8464

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c11a42
fix(module:tabs): wrong cursor
ParsaArvanehPA Feb 1, 2024
aac33c4
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 13, 2024
f7525f5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
8a974b9
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
e5276bb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 20, 2024
0f989fe
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 27, 2024
ec35a90
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 28, 2024
6ec6fe1
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 2, 2024
5d1e384
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
d81339e
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
a494c4f
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Mar 24, 2024
9f9acf9
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Mar 24, 2024
2284767
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 6, 2024
0ac094d
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 23, 2024
59d40a5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA May 12, 2024
35b916c
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Jun 4, 2024
82afabb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 14, 2024
11b7fc8
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 17, 2024
8705669
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 4, 2024
0e963ac
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 12, 2024
5df9b87
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 12, 2024
672ab2d
Merge branch 'feature/anchor-navigation-without-location-change' of h…
ParsaArvanehPA Aug 12, 2024
500dcf9
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 12, 2024
7b9723c
feat(module:anchor): url fragment change on click - nzReplace
ParsaArvanehPA Aug 13, 2024
04b1832
Update anchor-link.component.ts
Laffery Nov 23, 2024
b451362
Update anchor.component.ts
Laffery Nov 23, 2024
4c2a709
Update anchor.spec.ts
Laffery Nov 23, 2024
211dc34
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Nov 23, 2024
fe67bf4
Update anchor.spec.ts
Laffery Nov 23, 2024
5de5899
Update anchor.spec.ts
Laffery Nov 23, 2024
0a75117
Update replace.ts
Laffery Nov 23, 2024
c4d289d
Merge branch 'master' into feature/anchor-navigation-without-location…
Laffery Nov 26, 2024
fbe2b98
Update anchor.component.ts
Laffery Nov 26, 2024
f9fb540
Update anchor-link.component.ts
Laffery Nov 26, 2024
561d0d9
Update anchor.spec.ts
Laffery Nov 26, 2024
49ecf10
Update anchor.spec.ts
Laffery Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/anchor/anchor-link.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { NzAnchorComponent } from './anchor.component';
export class NzAnchorLinkComponent implements OnInit, OnDestroy {
@Input() nzHref = '#';
@Input() nzTarget?: string;
@Input() nzReplace: boolean = false;

titleStr: string | null = '';
titleTpl?: TemplateRef<NzSafeAny>;
Expand Down Expand Up @@ -99,11 +100,11 @@ export class NzAnchorLinkComponent implements OnInit, OnDestroy {
this.renderer.removeClass(this.elementRef.nativeElement, 'ant-anchor-link-active');
}

goToClick(e: Event): void {
async goToClick(e: Event): Promise<void> {
e.preventDefault();
e.stopPropagation();
if (this.platform.isBrowser) {
this.anchorComp.handleScrollTo(this);
await this.anchorComp.handleScrollTo(this);
}
}

Expand Down
19 changes: 17 additions & 2 deletions components/anchor/anchor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { fromEvent, Subject } from 'rxjs';
import { takeUntil, throttleTime } from 'rxjs/operators';

Expand Down Expand Up @@ -112,6 +113,7 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
@Input() nzContainer?: string | HTMLElement;
@Input() nzCurrentAnchor?: string;
@Input() nzDirection: NzDirectionVHType = 'vertical';
@Input() nzReplace: boolean = false;

@Output() readonly nzClick = new EventEmitter<string>();
@Output() readonly nzChange = new EventEmitter<string>();
Expand All @@ -135,7 +137,9 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
private cdr: ChangeDetectorRef,
private platform: Platform,
private zone: NgZone,
private renderer: Renderer2
private renderer: Renderer2,
private router: Router,
private activatedRoute: ActivatedRoute
) {}

registerLink(link: NzAnchorLinkComponent): void {
Expand Down Expand Up @@ -255,7 +259,7 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
}
}

handleScrollTo(linkComp: NzAnchorLinkComponent): void {
async handleScrollTo(linkComp: NzAnchorLinkComponent): Promise<void> {
const el = this.doc.querySelector(linkComp.nzHref);
if (!el) {
return;
Expand All @@ -273,6 +277,7 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
}
});
this.nzClick.emit(linkComp.nzHref);
await this.updateUrlFragment(linkComp.nzHref, linkComp.nzReplace);
}

ngOnChanges(changes: SimpleChanges): void {
Expand All @@ -291,4 +296,14 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
this.setActive();
}
}

async updateUrlFragment(nzHref: string, replace?: boolean): Promise<void> {
let urlWithoutFragment = nzHref.split('#')[1] ?? nzHref;

await this.router.navigate([], {
relativeTo: this.activatedRoute,
fragment: urlWithoutFragment,
replaceUrl: replace ? replace : this.nzReplace
});
}
}
40 changes: 39 additions & 1 deletion components/anchor/anchor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
/* eslint-disable */
// eslint-disable
import { fakeAsync, tick, TestBed, ComponentFixture } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Component, DebugElement, ViewChild } from '@angular/core';
import { By } from '@angular/platform-browser';
import { NzScrollService } from 'ng-zorro-antd/core/services';
import { NzAnchorModule } from './anchor.module';
import { NzAnchorComponent } from './anchor.component';
import { NzDirectionVHType } from 'ng-zorro-antd/core/types';
import { ActivatedRoute, Router } from '@angular/router';
import { of } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';

const throttleTime = 51;

const activatedRouteStub = {
paramMap: {
subscribe() {
return of();
}
}
};

describe('anchor', () => {
let fixture: ComponentFixture<TestComponent>;
let dl: DebugElement;
let context: TestComponent;
let page: PageObject;
let srv: NzScrollService;
let router: Router;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NzAnchorModule],
providers: [{ provide: ActivatedRoute, useValue: activatedRouteStub }, RouterTestingModule],
declarations: [TestComponent]
});
fixture = TestBed.createComponent(TestComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
fixture.detectChanges();
page = new PageObject();
router = TestBed.get(Router);
spyOn(context, '_scroll');
spyOn(context, '_change');
srv = TestBed.inject(NzScrollService);
Expand Down Expand Up @@ -121,6 +135,28 @@ describe('anchor', () => {
done();
}, throttleTime);
});

it('should call updateUrlFragment method when an anchor link is clicked', () => {
spyOn(context.comp, 'updateUrlFragment');
const linkList = dl.queryAll(By.css('.ant-anchor-link-title'));
(linkList[0].nativeElement as HTMLLinkElement).click();
fixture.detectChanges();
expect(context.comp.updateUrlFragment).toHaveBeenCalled();
});

it('should update fragment part of url when anchor link is clicked', fakeAsync(() => {
fixture['ngZone']!.run(() => {
router.initialNavigation();
router.navigateByUrl('/');

const linkList = dl.queryAll(By.css('.ant-anchor-link-title'));
(linkList[1].nativeElement as HTMLLinkElement).click();
fixture.detectChanges();
});
tick();

expect(router.url).toBe(`/#basic`);
}));
});

describe('property', () => {
Expand Down Expand Up @@ -302,6 +338,7 @@ describe('anchor', () => {
[nzContainer]="nzContainer"
[nzCurrentAnchor]="nzCurrentAnchor"
[nzDirection]="nzDirection"
[nzReplace]="replace"
(nzClick)="_click($event)"
(nzScroll)="_scroll($event)"
(nzChange)="_change($event)"
Expand Down Expand Up @@ -362,6 +399,7 @@ export class TestComponent {
nzContainer: any = null;
nzCurrentAnchor?: string;
nzDirection: NzDirectionVHType = 'vertical';
replace = false;
_click() {}
_change() {}
_scroll() {}
Expand Down
14 changes: 14 additions & 0 deletions components/anchor/demo/replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 8
title:
zh-CN: 替换历史中的 href
en-US: Replace href in history
---

## zh-CN

替换浏览器历史记录中的路径,后退按钮将返回到上一页而不是上一个锚点。

## en-US

Replace path in browser history, so back button returns to previous page instead of previous anchor item.
13 changes: 13 additions & 0 deletions components/anchor/demo/replace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from '@angular/core';

@Component({
selector: 'nz-demo-anchor-replace',
template: `
<nz-anchor>
<nz-link [nzReplace]="true" nzHref="#components-anchor-demo-basic" nzTitle="Basic demo"></nz-link>
<nz-link [nzReplace]="false" nzHref="#components-anchor-demo-static" nzTitle="Static demo"></nz-link>
<nz-link [nzReplace]="false" nzHref="#api" nzTitle="API"> </nz-link>
</nz-anchor>
`
})
export class NzDemoAnchorReplaceComponent {}
20 changes: 11 additions & 9 deletions components/anchor/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ import { NzAnchorModule } from 'ng-zorro-antd/anchor';

| Property | Description | Type | Default | Global Config |
| -------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------- | ------------ | ------------- |
| `[nzAffix]` | Fixed mode of Anchor | `boolean` | `true` |
| `[nzAffix]` | Fixed mode of Anchor | `boolean` | `true` | |
| `[nzBounds]` | Bounding distance of anchor area, unit: px | `number` | `5` | ✅ |
| `[nzOffsetTop]` | Pixels to offset from top when calculating position of scroll | `number` | `0` | ✅ |
| `[nzShowInkInFixed]` | Whether show ink-balls in Fixed mode | `boolean` | `false` | ✅ |
| `[nzTargetOffset]` | Anchor scroll offset, default as `offsetTop`, [example](#components-anchor-demo-targetOffset) | `number` | - | |
| `[nzContainer]` | Scrolling container | `string \| HTMLElement` | `window` |
| `[nzContainer]` | Scrolling container | `string \| HTMLElement` | `window` | |
| `[nzCurrentAnchor]` | Customize the anchor highlight | `string` | - | |
| `[nzDirection]` | Set Anchor direction | `'vertical' \| 'horizontal'` | `'vertical'` | |
| `(nzClick)` | Click of Anchor item | `EventEmitter<string>` | - |
| `[nzReplace]` | Replace items' href in browser history instead of pushing it | `boolean` | `false` | |
| `(nzClick)` | Click of Anchor item | `EventEmitter<string>` | - | |
| `(nzChange)` | Listening for anchor link change | `EventEmitter<string>` | - | |
| `(nzScroll)` | The scroll function that is triggered when scrolling to an anchor. | `EventEmitter<NzAnchorLinkComponent>` | - |
| `(nzScroll)` | The scroll function that is triggered when scrolling to an anchor. | `EventEmitter<NzAnchorLinkComponent>` | - | |

### nz-link:standalone

| Property | Description | Type |
| ------------ | ----------------------------------------- | ----------------------------- |
| `[nzHref]` | target of hyperlink | `string` |
| `[nzTarget]` | Specifies where to display the linked URL | `string` |
| `[nzTitle]` | content of hyperlink | `string \| TemplateRef<void>` |
| Property | Description | Type | Default |
| ------------- | ---------------------------------------------------------- | ----------------------------- | ------- |
| `[nzHref]` | target of hyperlink | `string` | - |
| `[nzTarget]` | Specifies where to display the linked URL | `string` | - |
| `[nzTitle]` | content of hyperlink | `string \| TemplateRef<void>` | - |
| `[nzReplace]` | Replace item href in browser history instead of pushing it | `boolean` | `false` |
12 changes: 7 additions & 5 deletions components/anchor/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ import { NzAnchorModule } from 'ng-zorro-antd/anchor';
| `[nzContainer]` | 指定滚动的容器 | `string \| HTMLElement` | `window` |
| `[nzCurrentAnchor]` | 自定义高亮的锚点 | string | - | |
| `[nzDirection]` | 设置导航方向 | `'vertical' \| 'horizontal'` | `'vertical'` | |
| `[nzReplace]` | 替换浏览器历史记录中项目的 href 而不是推送它 | `boolean` | `false` | |
| `(nzClick)` | 点击项触发 | `EventEmitter<string>` | - |
| `(nzChange)` | 监听锚点链接改变 | `EventEmitter<string>` | - | |
| `(nzScroll)` | 滚动至某锚点时触发 | `EventEmitter<NzAnchorLinkComponent>` | - |

### nz-link:standalone

| 成员 | 说明 | 类型 |
| ------------ | -------------------------------- | ----------------------------- |
| `[nzHref]` | 锚点链接 | `string` |
| `[nzTarget]` | 该属性指定在何处显示链接的资源。 | `string` |
| `[nzTitle]` | 文字内容 | `string \| TemplateRef<void>` |
| 成员 | 说明 | 类型 | Default |
| ------------- | -------------------------------------------- | ----------------------------- | ------- |
| `[nzHref]` | 锚点链接 | `string` | - |
| `[nzTarget]` | 该属性指定在何处显示链接的资源。 | `string` | - |
| `[nzTitle]` | 文字内容 | `string \| TemplateRef<void>` | - |
| `[nzReplace]` | 替换浏览器历史记录中的项目 href 而不是推送它 | `boolean` | `false` |
6 changes: 0 additions & 6 deletions scripts/site/template/demo-component.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export class {{componentName}} {
expanded = false;
@ViewChildren(NzCodeBoxComponent) codeBoxes!: QueryList<NzCodeBoxComponent>;

goLink(link: string): void {
if (window) {
window.location.hash = link;
}
}

expandAllCode(): void {
this.expanded = !this.expanded;
this.codeBoxes.forEach(code => {
Expand Down
8 changes: 1 addition & 7 deletions scripts/site/template/doc-component.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@ import { Component } from '@angular/core';
templateUrl : './{{component}}-{{language}}.html',
preserveWhitespaces: false
})
export class NzDoc{{componentName}}Component {
goLink(link: string) {
if (window) {
window.location.hash = link;
}
}
}
export class NzDoc{{componentName}}Component { }
2 changes: 1 addition & 1 deletion scripts/site/utils/generate-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function generateToc(language, name, demoMap) {
const links = linkArray.map(link => link.content).join('');
return `
<nz-affix class="toc-affix" [nzOffsetTop]="16">
<nz-anchor [nzAffix]="false" nzShowInkInFixed (nzClick)="goLink($event)">
<nz-anchor [nzAffix]="false" nzShowInkInFixed>
${links}
</nz-anchor>
</nz-affix>`;
Expand Down
2 changes: 1 addition & 1 deletion scripts/site/utils/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function generateToc(meta, raw) {
}
return `
<nz-affix class="toc-affix" [nzOffsetTop]="16">
<nz-anchor [nzAffix]="false" nzShowInkInFixed (nzClick)="goLink($event)">
<nz-anchor [nzAffix]="false" nzShowInkInFixed>
${links}
</nz-anchor>
</nz-affix>`;
Expand Down