mirror of
				https://github.com/CaramelFur/Picsur.git
				synced 2025-11-03 18:55:48 +01:00 
			
		
		
		
	Fix clipboard on webkit
This commit is contained in:
		@@ -1,11 +1,11 @@
 | 
				
			|||||||
import { Clipboard } from '@angular/cdk/clipboard';
 | 
					 | 
				
			||||||
import { Component, EventEmitter, Input, Output } from '@angular/core';
 | 
					import { Component, EventEmitter, Input, Output } from '@angular/core';
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  MatFormFieldAppearance,
 | 
					  MatFormFieldAppearance,
 | 
				
			||||||
  SubscriptSizing,
 | 
					  SubscriptSizing,
 | 
				
			||||||
} from '@angular/material/form-field';
 | 
					} from '@angular/material/form-field';
 | 
				
			||||||
import { Fail, FT } from 'picsur-shared/dist/types';
 | 
					import { FT, Fail } from 'picsur-shared/dist/types';
 | 
				
			||||||
import { Logger } from 'src/app/services/logger/logger.service';
 | 
					import { Logger } from 'src/app/services/logger/logger.service';
 | 
				
			||||||
 | 
					import { ClipboardService } from 'src/app/util/clipboard.service';
 | 
				
			||||||
import { ErrorService } from 'src/app/util/error-manager/error.service';
 | 
					import { ErrorService } from 'src/app/util/error-manager/error.service';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Component({
 | 
					@Component({
 | 
				
			||||||
@@ -31,12 +31,12 @@ export class CopyFieldComponent {
 | 
				
			|||||||
  @Output('hide') onHide = new EventEmitter<boolean>();
 | 
					  @Output('hide') onHide = new EventEmitter<boolean>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(
 | 
					  constructor(
 | 
				
			||||||
    private readonly clipboard: Clipboard,
 | 
					    private readonly clipboard: ClipboardService,
 | 
				
			||||||
    private readonly errorService: ErrorService,
 | 
					    private readonly errorService: ErrorService,
 | 
				
			||||||
  ) {}
 | 
					  ) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public copy() {
 | 
					  public async copy() {
 | 
				
			||||||
    if (this.clipboard.copy(this.value)) {
 | 
					    if (await this.clipboard.copy(this.value)) {
 | 
				
			||||||
      this.errorService.info(`Copied ${this.label}!`);
 | 
					      this.errorService.info(`Copied ${this.label}!`);
 | 
				
			||||||
      this.onCopy.emit(this.value);
 | 
					      this.onCopy.emit(this.value);
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,3 @@
 | 
				
			|||||||
import { ClipboardModule } from '@angular/cdk/clipboard';
 | 
					 | 
				
			||||||
import { CommonModule } from '@angular/common';
 | 
					import { CommonModule } from '@angular/common';
 | 
				
			||||||
import { NgModule } from '@angular/core';
 | 
					import { NgModule } from '@angular/core';
 | 
				
			||||||
import { MatButtonModule } from '@angular/material/button';
 | 
					import { MatButtonModule } from '@angular/material/button';
 | 
				
			||||||
@@ -15,7 +14,6 @@ import { CopyFieldComponent } from './copy-field.component';
 | 
				
			|||||||
    MatInputModule,
 | 
					    MatInputModule,
 | 
				
			||||||
    MatIconModule,
 | 
					    MatIconModule,
 | 
				
			||||||
    MatButtonModule,
 | 
					    MatButtonModule,
 | 
				
			||||||
    ClipboardModule,
 | 
					 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  exports: [CopyFieldComponent],
 | 
					  exports: [CopyFieldComponent],
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,14 @@
 | 
				
			|||||||
import { Clipboard } from '@angular/cdk/clipboard';
 | 
					 | 
				
			||||||
import { Component, OnInit, ViewChild } from '@angular/core';
 | 
					import { Component, OnInit, ViewChild } from '@angular/core';
 | 
				
			||||||
import { MatPaginator, PageEvent } from '@angular/material/paginator';
 | 
					import { MatPaginator, PageEvent } from '@angular/material/paginator';
 | 
				
			||||||
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
 | 
					import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
 | 
				
			||||||
import { EApiKey } from 'picsur-shared/dist/entities/apikey.entity';
 | 
					import { EApiKey } from 'picsur-shared/dist/entities/apikey.entity';
 | 
				
			||||||
import { Fail, FT, HasFailed } from 'picsur-shared/dist/types';
 | 
					import { FT, Fail, HasFailed } from 'picsur-shared/dist/types';
 | 
				
			||||||
import { BehaviorSubject, Subject } from 'rxjs';
 | 
					import { BehaviorSubject, Subject } from 'rxjs';
 | 
				
			||||||
import { ApiKeysService } from 'src/app/services/api/apikeys.service';
 | 
					import { ApiKeysService } from 'src/app/services/api/apikeys.service';
 | 
				
			||||||
import { UserService } from 'src/app/services/api/user.service';
 | 
					import { UserService } from 'src/app/services/api/user.service';
 | 
				
			||||||
import { Logger } from 'src/app/services/logger/logger.service';
 | 
					import { Logger } from 'src/app/services/logger/logger.service';
 | 
				
			||||||
import { BootstrapService } from 'src/app/util/bootstrap.service';
 | 
					import { BootstrapService } from 'src/app/util/bootstrap.service';
 | 
				
			||||||
 | 
					import { ClipboardService } from 'src/app/util/clipboard.service';
 | 
				
			||||||
import { DialogService } from 'src/app/util/dialog-manager/dialog.service';
 | 
					import { DialogService } from 'src/app/util/dialog-manager/dialog.service';
 | 
				
			||||||
import { ErrorService } from 'src/app/util/error-manager/error.service';
 | 
					import { ErrorService } from 'src/app/util/error-manager/error.service';
 | 
				
			||||||
import { Throttle } from 'src/app/util/throttle';
 | 
					import { Throttle } from 'src/app/util/throttle';
 | 
				
			||||||
@@ -38,7 +38,7 @@ export class SettingsApiKeysComponent implements OnInit {
 | 
				
			|||||||
  constructor(
 | 
					  constructor(
 | 
				
			||||||
    private readonly apikeysService: ApiKeysService,
 | 
					    private readonly apikeysService: ApiKeysService,
 | 
				
			||||||
    private readonly userService: UserService,
 | 
					    private readonly userService: UserService,
 | 
				
			||||||
    private readonly clipboard: Clipboard,
 | 
					    private readonly clipboard: ClipboardService,
 | 
				
			||||||
    private readonly errorService: ErrorService,
 | 
					    private readonly errorService: ErrorService,
 | 
				
			||||||
    private readonly dialogService: DialogService,
 | 
					    private readonly dialogService: DialogService,
 | 
				
			||||||
    // Public because used in template
 | 
					    // Public because used in template
 | 
				
			||||||
@@ -64,7 +64,7 @@ export class SettingsApiKeysComponent implements OnInit {
 | 
				
			|||||||
      this.paginator.firstPage();
 | 
					      this.paginator.firstPage();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const clipboardResult = this.clipboard.copy(result.key);
 | 
					    const clipboardResult = await this.clipboard.copy(result.key);
 | 
				
			||||||
    if (!clipboardResult) {
 | 
					    if (!clipboardResult) {
 | 
				
			||||||
      return this.errorService.showFailure(
 | 
					      return this.errorService.showFailure(
 | 
				
			||||||
        Fail(FT.Internal, 'Failed to copy api key to clipboard'),
 | 
					        Fail(FT.Internal, 'Failed to copy api key to clipboard'),
 | 
				
			||||||
@@ -75,8 +75,8 @@ export class SettingsApiKeysComponent implements OnInit {
 | 
				
			|||||||
    this.errorService.success('Api key created and copied to clipboard');
 | 
					    this.errorService.success('Api key created and copied to clipboard');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public copyKey(apikey: string) {
 | 
					  public async copyKey(apikey: string) {
 | 
				
			||||||
    const result = this.clipboard.copy(apikey);
 | 
					    const result = await this.clipboard.copy(apikey);
 | 
				
			||||||
    if (!result) {
 | 
					    if (!result) {
 | 
				
			||||||
      return this.errorService.showFailure(
 | 
					      return this.errorService.showFailure(
 | 
				
			||||||
        Fail(FT.Internal, 'Failed to copy api key to clipboard'),
 | 
					        Fail(FT.Internal, 'Failed to copy api key to clipboard'),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,3 @@
 | 
				
			|||||||
import { ClipboardModule } from '@angular/cdk/clipboard';
 | 
					 | 
				
			||||||
import { CommonModule } from '@angular/common';
 | 
					import { CommonModule } from '@angular/common';
 | 
				
			||||||
import { NgModule } from '@angular/core';
 | 
					import { NgModule } from '@angular/core';
 | 
				
			||||||
import { ReactiveFormsModule } from '@angular/forms';
 | 
					import { ReactiveFormsModule } from '@angular/forms';
 | 
				
			||||||
@@ -29,7 +28,6 @@ import { SettingsApiKeysRoutingModule } from './settings-apikeys.routing.module'
 | 
				
			|||||||
    MatPaginatorModule,
 | 
					    MatPaginatorModule,
 | 
				
			||||||
    MatInputModule,
 | 
					    MatInputModule,
 | 
				
			||||||
    MomentModule,
 | 
					    MomentModule,
 | 
				
			||||||
    ClipboardModule,
 | 
					 | 
				
			||||||
    ReactiveFormsModule,
 | 
					    ReactiveFormsModule,
 | 
				
			||||||
    FabModule,
 | 
					    FabModule,
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								frontend/src/app/util/clipboard.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								frontend/src/app/util/clipboard.service.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					import { Injectable } from '@angular/core';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Injectable({
 | 
				
			||||||
 | 
					  providedIn: 'any',
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					export class ClipboardService {
 | 
				
			||||||
 | 
					  async copy(text: string): Promise<boolean> {
 | 
				
			||||||
 | 
					    if (!navigator.clipboard) return false;
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      const result = await navigator.clipboard.writeText(text);
 | 
				
			||||||
 | 
					      return result === undefined;
 | 
				
			||||||
 | 
					    } catch (err) {
 | 
				
			||||||
 | 
					      return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user