From f3081d6401b63da41b03376f9f952bf1cca8303d Mon Sep 17 00:00:00 2001 From: Kimsible <1877318+kimsible@users.noreply.github.com> Date: Tue, 17 Nov 2020 14:04:44 +0100 Subject: [PATCH] small refactor + clipboard copy on anchor click in instance page (#3318) * Refacto scrollTo top and anchors * Add on click clipboard copy and notifier to anchors about/instance page * Fix margin top anchor middle title about page * Use viewportscroller for scrolltoanchor * Add to Link copied notification on instance page Co-authored-by: kimsible --- .../about-instance.component.html | 100 +++++++++++++++--- .../about-instance.component.scss | 4 +- .../about-instance.component.ts | 10 +- client/src/app/app.component.ts | 2 +- client/src/app/helpers/utils.ts | 8 +- client/src/app/menu/menu.component.html | 13 +-- client/src/app/menu/menu.component.ts | 14 ++- .../misc/top-menu-dropdown.component.ts | 7 +- 8 files changed, 117 insertions(+), 41 deletions(-) diff --git a/client/src/app/+about/about-instance/about-instance.component.html b/client/src/app/+about/about-instance/about-instance.component.html index d759c4343..045bb3fbc 100644 --- a/client/src/app/+about/about-instance/about-instance.component.html +++ b/client/src/app/+about/about-instance/about-instance.component.html @@ -23,7 +23,10 @@

ADMINISTRATORS & SUSTAINABILITY @@ -32,7 +35,12 @@
-
+

Who we are

@@ -41,7 +49,12 @@
- +

Why we created this instance

@@ -50,7 +63,12 @@
- +

INFORMATION

@@ -75,7 +104,12 @@
-
+

Description

@@ -85,8 +119,11 @@
+ class="anchor-link" + routerLink="/about/instance" + fragment="moderation" + #anchorLink + (click)="onClickCopyLink(anchorLink)">

MODERATION

@@ -94,7 +131,12 @@
-
+

Moderation information

@@ -103,7 +145,12 @@
- +

Code of conduct

@@ -112,7 +159,12 @@
- +

Terms

@@ -120,7 +172,13 @@
- +

OTHER INFORMATION

@@ -128,7 +186,12 @@
-
+

Hardware information

@@ -143,7 +206,12 @@
- +

STATISTICS

diff --git a/client/src/app/+about/about-instance/about-instance.component.scss b/client/src/app/+about/about-instance/about-instance.component.scss index 2e77ade9b..fe3ce22b0 100644 --- a/client/src/app/+about/about-instance/about-instance.component.scss +++ b/client/src/app/+about/about-instance/about-instance.component.scss @@ -43,12 +43,12 @@ .middle-title { @include in-content-small-title; - margin-top: 45px; + margin-top: 0; margin-bottom: 25px; } .block { - margin-bottom: 30px; + margin-bottom: 75px; font-size: 15px; } diff --git a/client/src/app/+about/about-instance/about-instance.component.ts b/client/src/app/+about/about-instance/about-instance.component.ts index bd587fa26..e74b5daeb 100644 --- a/client/src/app/+about/about-instance/about-instance.component.ts +++ b/client/src/app/+about/about-instance/about-instance.component.ts @@ -2,7 +2,8 @@ import { ViewportScroller } from '@angular/common' import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute } from '@angular/router' import { ContactAdminModalComponent } from '@app/+about/about-instance/contact-admin-modal.component' -import { ServerService } from '@app/core' +import { Notifier } from '@app/core' +import { copyToClipboard } from '../../../assets/player/utils' import { InstanceService } from '@app/shared/shared-instance' import { ServerConfig } from '@shared/models' import { ResolverData } from './about-instance.resolver' @@ -42,6 +43,7 @@ export class AboutInstanceComponent implements OnInit, AfterViewChecked { constructor ( private viewportScroller: ViewportScroller, private route: ActivatedRoute, + private notifier: Notifier, private instanceService: InstanceService ) {} @@ -87,4 +89,10 @@ export class AboutInstanceComponent implements OnInit, AfterViewChecked { openContactModal () { return this.contactAdminModal.show() } + + onClickCopyLink (anchor: HTMLAnchorElement) { + const link = anchor.href + copyToClipboard(link) + this.notifier.success(link, $localize `Link copied`) + } } diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index edec3216e..da51040ad 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -121,7 +121,7 @@ export class AppComponent implements OnInit, AfterViewInit { // scrollToAnchor first to preserve anchor position when using history navigation if (e.anchor) { setTimeout(() => { - document.getElementById(e.anchor).scrollIntoView({ behavior: 'smooth', inline: 'nearest' }) + this.viewportScroller.scrollToAnchor(e.anchor) }) return diff --git a/client/src/app/helpers/utils.ts b/client/src/app/helpers/utils.ts index d9007dd77..a22507f46 100644 --- a/client/src/app/helpers/utils.ts +++ b/client/src/app/helpers/utils.ts @@ -144,8 +144,12 @@ function sortBy (obj: any[], key1: string, key2?: string) { }) } -function scrollToTop () { - window.scroll(0, 0) +function scrollToTop (behavior: 'auto' | 'smooth' = 'auto') { + window.scrollTo({ + left: 0, + top: 0, + behavior + }) } function isInViewport (el: HTMLElement) { diff --git a/client/src/app/menu/menu.component.html b/client/src/app/menu/menu.component.html index a5985a301..88e3d60db 100644 --- a/client/src/app/menu/menu.component.html +++ b/client/src/app/menu/menu.component.html @@ -3,7 +3,7 @@
- +
- + My account - + My library diff --git a/client/src/app/menu/menu.component.ts b/client/src/app/menu/menu.component.ts index 48ed91973..97a3b6d24 100644 --- a/client/src/app/menu/menu.component.ts +++ b/client/src/app/menu/menu.component.ts @@ -1,8 +1,10 @@ +import { ViewportScroller } from '@angular/common' import { HotkeysService } from 'angular2-hotkeys' import * as debug from 'debug' import { switchMap } from 'rxjs/operators' import { Component, OnInit, ViewChild } from '@angular/core' import { Router } from '@angular/router' +import { scrollToTop } from '@app/helpers' import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core' import { LanguageChooserComponent } from '@app/menu/language-chooser.component' import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' @@ -39,6 +41,7 @@ export class MenuComponent implements OnInit { } constructor ( + private viewportScroller: ViewportScroller, private authService: AuthService, private userService: UserService, private serverService: ServerService, @@ -199,23 +202,18 @@ export class MenuComponent implements OnInit { return this.languages.find(lang => lang.id === localeId).label } - onSameUrlRestoreScrollPosition (link: HTMLAnchorElement) { + onActiveLinkScrollToAnchor (link: HTMLAnchorElement) { const linkURL = link.getAttribute('href') const linkHash = link.getAttribute('fragment') // On same url without fragment restore top scroll position if (!linkHash && this.router.url.includes(linkURL)) { - window.scrollTo({ - left: 0, - top: 0, - behavior: 'smooth' - }) + scrollToTop('smooth') } // On same url with fragment restore anchor scroll position if (linkHash && this.router.url === linkURL) { - const anchor = document.getElementById(link.getAttribute('fragment')) - anchor.scrollIntoView({ behavior: 'smooth', inline: 'nearest' }) + this.viewportScroller.scrollToAnchor(linkHash) } if (this.screenService.isInSmallView()) { diff --git a/client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts b/client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts index ba5568595..2cafb6c55 100644 --- a/client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts +++ b/client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts @@ -3,6 +3,7 @@ import { filter } from 'rxjs/operators' import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core' import { NavigationEnd, Router } from '@angular/router' import { MenuService, ScreenService } from '@app/core' +import { scrollToTop } from '@app/helpers' import { GlobalIconName } from '@app/shared/shared-icons' import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap' @@ -96,11 +97,7 @@ export class TopMenuDropdownComponent implements OnInit, OnDestroy { onActiveLinkScrollToTop (link: HTMLAnchorElement) { if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) { - window.scrollTo({ - left: 0, - top: 0, - behavior: 'smooth' - }) + scrollToTop('smooth') } }