All files / app app.component.ts

31.74% Statements 40/126
23.65% Branches 22/93
10% Functions 3/30
34% Lines 34/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                          1x 1x 1x   1x   1x             1x 1x                             3x     1x   1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x             1x             1x             1x                   1x                               1x       1x 1x                         1x                                                                                                                                                
import {
  AfterViewInit,
  ChangeDetectionStrategy,
  Component,
  HostBinding,
  HostListener,
  isDevMode,
  ViewContainerRef
} from '@angular/core';
import { NavigationStart, Router, RouterOutlet } from '@angular/router';
import { runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { filter } from 'rxjs';
import { LoginPopupComponent } from './component/login-popup/login-popup.component';
import { SubscriptionBarComponent } from './component/subscription-bar/subscription-bar.component';
import { UserClipboardComponent } from './component/user-clipboard/user-clipboard.component';
import { userClipboardPlugin } from './mods/clipboard';
import { pdfPlugin, pdfUrl } from './mods/media/pdf';
import { pipPlugin } from './mods/system/pip';
import { archivePlugin, archiveUrl } from './mods/tools/archive';
import { AdminService } from './service/admin.service';
import { OriginService } from './service/api/origin.service';
import { ProxyService } from './service/api/proxy.service';
import { ScrapeService } from './service/api/scrape.service';
import { ConfigService } from './service/config.service';
import { Store } from './store/store';
import { createPip } from './util/embed';
import { memo } from './util/memo';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [
    MobxAngularModule,
    LoginPopupComponent,
    SubscriptionBarComponent,
    UserClipboardComponent,
    RouterOutlet,
  ],
})
export class AppComponent implements AfterViewInit {
 
  @HostBinding('class.electron')
  electron = this.config.electron;
 
  debug = !isDevMode() && this.store.account.debug;
  website = 'https://github.com/cjmalloy/jasper-ui';
 
  pdfPlugin = this.admin.getPlugin('plugin/pdf') as typeof pdfPlugin || undefined;
  archivePlugin = this.admin.getPlugin('plugin/archive') as typeof archivePlugin || undefined;
  pipPlugin = this.admin.getPlugin('plugin/pip') as typeof pipPlugin || undefined;
  userClipboardPlugin = this.admin.getPlugin('plugin/user/clipboard') as typeof userClipboardPlugin || undefined;
 
  constructor(
    public config: ConfigService,
    public store: Store,
    private admin: AdminService,
    private proxy: ProxyService,
    private origins: OriginService,
    private scrape: ScrapeService,
    private router: Router,
    private vc: ViewContainerRef,
  ) {
    document.body.style.height = '';
    Eif (!this.store.account.debug && this.config.version) this.website = 'https://github.com/cjmalloy/jasper-ui/releases/tag/' + this.config.version;
    window.addEventListener('keyup', event => {
      const hotkey = !this.hotkeyActive(event) || this.hotkey(event.key);
      if (this.store.hotkey && hotkey) {
        runInAction(() => this.store.hotkey = false);
        document.body.classList.remove('hotkey');
      }
    }, { capture: true });
    window.addEventListener('keydown', event => {
      const hotkey = this.hotkeyActive(event) || this.hotkey(event.key);
      if (this.store.hotkey !== hotkey) {
        runInAction(() => this.store.hotkey = hotkey);
        document.body.classList.toggle('hotkey', hotkey);
      }
    }, { capture: true });
    window.addEventListener('pointerenter', event => {
      const hotkey = this.hotkeyActive(event);
      if (this.store.hotkey !== hotkey) {
        runInAction(() => this.store.hotkey = hotkey);
        document.body.classList.toggle('hotkey', hotkey);
      }
    }, { capture: true });
    window.addEventListener('pointerout', event => {
      const hotkey = this.hotkeyActive(event);
      if (this.store.hotkey !== hotkey) {
        runInAction(() => this.store.hotkey = hotkey);
        document.body.classList.toggle('hotkey', hotkey);
      }
    }, { capture: true });
  }
 
  ngAfterViewInit() {
    this.store.eventBus.events.subscribe(({ event, ref, repost }) => {
      if (event === 'pdf' && this.pdfPlugin) {
        let pdf = pdfUrl(this.pdfPlugin, ref, repost);
        if (!pdf) return;
        if (pdf.url.startsWith('cache:') || this.pdfPlugin.config?.proxy) pdf.url = this.proxy.getFetch(pdf.url, pdf.origin, pdf.title + (pdf.title.toLowerCase().endsWith('.pdf') ? '' : '.pdf'));
        open(pdf.url, '_blank');
      }
      if (event === 'archive' && this.archivePlugin) {
        let url = archiveUrl(this.archivePlugin, ref, repost);
        if (!url) return;
        open(url, '_blank');
      }
      if (event === 'pip' && this.pipPlugin) {
        createPip(this.vc, ref!, this.pipPlugin.config?.windowConfig);
      }
    });
    window.visualViewport?.addEventListener('resize', event => {
      const vv = event?.target as VisualViewport;
      runInAction(() => this.store.viewportHeight = vv.height);
    });
    let currentNavigationId = 0;
    this.router.events.pipe(
      filter(event => event instanceof NavigationStart)
    ).subscribe((event: NavigationStart) => {
      const isLinkClick = event.navigationTrigger === 'imperative';
      const isForwardButton = event.navigationTrigger === 'popstate' &&
        event.restoredState &&
        event.restoredState.navigationId > currentNavigationId;
      runInAction(() => this.store.view.back = !isLinkClick && !isForwardButton);
      currentNavigationId = event.restoredState?.navigationId ?? event.id;
    });
  }
 
  @memo
  get macos() {
    return /Macintosh/i.test(navigator.userAgent);
  }
 
  hotkey(key: string) {
    return this.macos ? key === 'Meta' : key === 'Control';
  }
 
  hotkeyActive(event: KeyboardEvent | PointerEvent) {
    return this.macos ? event.metaKey : event.ctrlKey;
  }
 
  @HostListener('window:blur')
  removeHotkey() {
    if (this.store.hotkey) {
      runInAction(() => this.store.hotkey = false);
      document.body.classList.remove('hotkey');
    }
  }
 
  @HostListener('window:offline')
  offline() {
    if (!this.store.offline) {
      runInAction(() => this.store.offline = true);
    }
  }
 
  @HostListener('window:online')
  online() {
    if (this.store.offline) {
      runInAction(() => this.store.offline = false);
    }
  }
 
  @HostListener('window:paste', ['$event'])
  paste(event: ClipboardEvent) {
    const items = event.clipboardData?.items;
    if (!items) return;
    for (let i = 0; i < items.length; i++) {
      const d = items[i];
      if (d?.kind === 'file') {
        this.upload(event, items);
        this.removeHotkey();
        return;
      }
    }
  }
 
  dragOver(event: DragEvent) {
    event.preventDefault();
  }
 
  upload(event: Event, items?: DataTransferItemList) {
    if (!items) return;
    if ((event.target as HTMLElement)?.tagName === 'INPUT') return;
    if ((event.target as HTMLElement)?.tagName === 'TEXTAREA') return;
    event.preventDefault();
    const files = [] as any;
    for (let i = 0; i < items.length; i++) {
      const d = items[i];
      if (d?.kind === 'file') {
        files.push(d.getAsFile());
      }
    }
    if (!files.length) return;
    this.store.submit.addFiles(files);
    if (!this.store.submit.upload) {
      this.router.navigate(['/submit/upload'], { queryParams: { tag: this.store.view.queryTags }});
    }
  }
 
}