From 7744e0965303edef8b739906b2e1febfaf87f54a Mon Sep 17 00:00:00 2001 From: Yamaguchi Date: Sat, 28 Oct 2023 21:59:05 +0900 Subject: [PATCH] Use Helper function to copy to clipboard --- frontend/src/app/address/address.page.ts | 23 +++------------- frontend/src/app/app.helper.ts | 1 - .../app/block-rawdata/block-rawdata.page.html | 8 ------ .../block-rawdata/block-rawdata.page.spec.ts | 5 ++-- .../app/block-rawdata/block-rawdata.page.ts | 26 ++++--------------- .../transaction-rawdata.page.html | 8 ------ .../transaction-rawdata.page.ts | 26 ++++--------------- 7 files changed, 17 insertions(+), 80 deletions(-) diff --git a/frontend/src/app/address/address.page.ts b/frontend/src/app/address/address.page.ts index 836fad91..daaf2fab 100644 --- a/frontend/src/app/address/address.page.ts +++ b/frontend/src/app/address/address.page.ts @@ -1,9 +1,10 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; -import { NavController } from '@ionic/angular'; +import { NavController, ToastController } from '@ionic/angular'; import { BackendService } from '../backend.service'; import { AppConst } from '../app.const'; +import Helper from '../app.helper'; @Component({ selector: 'app-address', @@ -34,6 +35,7 @@ export class AddressPage implements OnInit { constructor( private activatedRoute: ActivatedRoute, private navCtrl: NavController, + private toastController: ToastController, private backendService: BackendService ) {} @@ -56,24 +58,7 @@ export class AddressPage implements OnInit { } copyAddress() { - const textArea = document.createElement('textarea'); - textArea.value = this.address; - - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - try { - document.execCommand('copy'); - this.copied = true; - setTimeout(() => { - this.copied = false; - }, 800); - } catch (err) { - console.error('Fallback: Oops, unable to copy', err); - } - - document.body.removeChild(textArea); + Helper.copy(this.toastController, this.address); } onNextPage() { diff --git a/frontend/src/app/app.helper.ts b/frontend/src/app/app.helper.ts index 11774e73..4c367356 100644 --- a/frontend/src/app/app.helper.ts +++ b/frontend/src/app/app.helper.ts @@ -2,7 +2,6 @@ import { Component, Injectable } from '@angular/core'; import { ToastController } from '@ionic/angular'; export default class Helper { - @Injectable() static async copy(toastController: ToastController, text: string) { navigator.clipboard.writeText(text); const toast = await toastController.create({ diff --git a/frontend/src/app/block-rawdata/block-rawdata.page.html b/frontend/src/app/block-rawdata/block-rawdata.page.html index e7e2dc32..8dffd8a8 100644 --- a/frontend/src/app/block-rawdata/block-rawdata.page.html +++ b/frontend/src/app/block-rawdata/block-rawdata.page.html @@ -12,16 +12,8 @@ Copy Raw Data - Copied !

{{ blockRawData }}

-
- - - Copy Raw Data
- Copied ! -
diff --git a/frontend/src/app/block-rawdata/block-rawdata.page.spec.ts b/frontend/src/app/block-rawdata/block-rawdata.page.spec.ts index babd116f..dbefe240 100644 --- a/frontend/src/app/block-rawdata/block-rawdata.page.spec.ts +++ b/frontend/src/app/block-rawdata/block-rawdata.page.spec.ts @@ -1,5 +1,5 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; -import { IonicModule } from '@ionic/angular'; +import { IonicModule, NavParams } from '@ionic/angular'; import { BlockRawdataPage } from './block-rawdata.page'; import { HttpClientTestingModule } from '@angular/common/http/testing'; @@ -11,7 +11,8 @@ describe('BlockRawdataPage', () => { beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [BlockRawdataPage], - imports: [IonicModule.forRoot(), HttpClientTestingModule] + imports: [IonicModule.forRoot(), HttpClientTestingModule], + providers: [NavParams] }).compileComponents(); fixture = TestBed.createComponent(BlockRawdataPage); diff --git a/frontend/src/app/block-rawdata/block-rawdata.page.ts b/frontend/src/app/block-rawdata/block-rawdata.page.ts index 8f574eef..c43a03ba 100644 --- a/frontend/src/app/block-rawdata/block-rawdata.page.ts +++ b/frontend/src/app/block-rawdata/block-rawdata.page.ts @@ -1,24 +1,25 @@ import { Component, OnInit, Input } from '@angular/core'; -import { NavParams, ModalController } from '@ionic/angular'; +import { NavParams, ModalController, ToastController } from '@ionic/angular'; import { HttpClient } from '@angular/common/http'; import { BackendService } from '../backend.service'; +import Helper from '../app.helper'; @Component({ selector: 'app-block-rawdata', templateUrl: './block-rawdata.page.html', styleUrls: ['./block-rawdata.page.scss'], - providers: [BackendService, NavParams] + providers: [BackendService] }) export class BlockRawdataPage implements OnInit { @Input() blockHash: string; blockRawData = ''; - copied = false; constructor( private navParams: NavParams, private httpClient: HttpClient, private modalCtrl: ModalController, + private toastController: ToastController, private backendService: BackendService ) {} @@ -40,24 +41,7 @@ export class BlockRawdataPage implements OnInit { } copyBlockRawData() { - const textArea = document.createElement('textarea'); - textArea.value = this.blockRawData; - - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - try { - document.execCommand('copy'); - this.copied = true; - setTimeout(() => { - this.copied = false; - }, 800); - } catch (err) { - console.error('Fallback: Oops, unable to copy', err); - } - - document.body.removeChild(textArea); + Helper.copy(this.toastController, this.blockRawData); } dismiss() { diff --git a/frontend/src/app/transaction-rawdata/transaction-rawdata.page.html b/frontend/src/app/transaction-rawdata/transaction-rawdata.page.html index fc4915da..aaefda9c 100644 --- a/frontend/src/app/transaction-rawdata/transaction-rawdata.page.html +++ b/frontend/src/app/transaction-rawdata/transaction-rawdata.page.html @@ -12,16 +12,8 @@ Copy Raw Data - Copied !

{{ txRawData }}

-
- - - Copy Raw Data
- Copied ! -
diff --git a/frontend/src/app/transaction-rawdata/transaction-rawdata.page.ts b/frontend/src/app/transaction-rawdata/transaction-rawdata.page.ts index eaab893d..07ca51a6 100644 --- a/frontend/src/app/transaction-rawdata/transaction-rawdata.page.ts +++ b/frontend/src/app/transaction-rawdata/transaction-rawdata.page.ts @@ -1,8 +1,9 @@ import { Component, OnInit, Input } from '@angular/core'; -import { NavParams, ModalController } from '@ionic/angular'; +import { NavParams, ModalController, ToastController } from '@ionic/angular'; import { HttpClient } from '@angular/common/http'; import { BackendService } from '../backend.service'; +import Helper from '../app.helper'; @Component({ selector: 'app-transaction-rawdata', @@ -13,12 +14,12 @@ import { BackendService } from '../backend.service'; export class TransactionRawdataPage implements OnInit { @Input() txid: string; txRawData = ''; - copied = false; constructor( private navParams: NavParams, private httpClient: HttpClient, private modalCtrl: ModalController, + private toastController: ToastController, private backendService: BackendService ) {} @@ -39,25 +40,8 @@ export class TransactionRawdataPage implements OnInit { ); } - copyTxRawData() { - const textArea = document.createElement('textarea'); - textArea.value = this.txRawData; - - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - try { - document.execCommand('copy'); - this.copied = true; - setTimeout(() => { - this.copied = false; - }, 800); - } catch (err) { - console.error('Fallback: Oops, unable to copy', err); - } - - document.body.removeChild(textArea); + async copyTxRawData() { + Helper.copy(this.toastController, this.txRawData); } dismiss() {