MOBILE-4616 chore: Move copyClipboard to CoreText

main
Pau Ferrer Ocaña 2024-07-16 12:49:28 +02:00
parent caa100979a
commit 7068db3f62
8 changed files with 44 additions and 27 deletions

View File

@ -45,6 +45,7 @@ import { ActivatedRoute } from '@angular/router';
import { CoreConstants } from '@/core/constants';
import { CoreDom } from '@singletons/dom';
import { CoreKeyboard } from '@singletons/keyboard';
import { CoreText } from '@singletons/text';
/**
* Page that displays a message discussion page.
@ -924,7 +925,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
*/
copyMessage(message: AddonMessagesConversationMessageFormatted): void {
const text = 'smallmessage' in message ? message.smallmessage || message.text || '' : message.text || '';
CoreUtils.copyToClipboard(CoreTextUtils.decodeHTMLEntities(text));
CoreText.copyToClipboard(CoreTextUtils.decodeHTMLEntities(text));
}
/**

View File

@ -16,7 +16,7 @@ import { ContextLevel } from '@/core/constants';
import { Component, EventEmitter, HostBinding, Input, OnInit, Output } from '@angular/core';
import { CoreAnimations } from '@components/animations';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CoreText } from '@singletons/text';
import { CoreTextUtils } from '@services/utils/text';
import { CoreUserWithAvatar } from '@components/user-avatar/user-avatar';
@ -102,7 +102,7 @@ export class CoreMessageComponent implements OnInit {
* Copy message to clipboard.
*/
copyMessage(): void {
CoreUtils.copyToClipboard(CoreTextUtils.decodeHTMLEntities(this.text));
CoreText.copyToClipboard(CoreTextUtils.decodeHTMLEntities(this.text));
}
}

View File

@ -25,7 +25,7 @@ import { CoreNavigator } from '@services/navigator';
import { CorePlatform } from '@services/platform';
import { CoreSites } from '@services/sites';
import { CoreDomUtils, ToastDuration } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { CoreText } from '@singletons/text';
/**
* Page that displays the developer options.
@ -165,7 +165,7 @@ export class CoreSettingsDevPage implements OnInit {
* Copies site info.
*/
copyInfo(): void {
CoreUtils.copyToClipboard(JSON.stringify({ disabledFeatures: this.disabledFeatures, sitePlugins: this.sitePlugins }));
CoreText.copyToClipboard(JSON.stringify({ disabledFeatures: this.disabledFeatures, sitePlugins: this.sitePlugins }));
}
/**

View File

@ -29,6 +29,7 @@ import { CorePlatform } from '@services/platform';
import { CoreNetwork } from '@services/network';
import { CoreLoginHelper } from '@features/login/services/login-helper';
import { CoreSitesFactory } from '@services/sites-factory';
import { CoreText } from '@singletons/text';
/**
* Device Info to be shown and copied to clipboard.
@ -226,7 +227,7 @@ export class CoreSettingsDeviceInfoPage implements OnDestroy {
* Copies device info into the clipboard.
*/
copyInfo(): void {
CoreUtils.copyToClipboard(JSON.stringify(this.deviceInfo));
CoreText.copyToClipboard(JSON.stringify(this.deviceInfo));
}
/**
@ -238,7 +239,7 @@ export class CoreSettingsDeviceInfoPage implements OnDestroy {
const el = <Element>e.target;
const text = el?.closest('ion-item')?.textContent?.trim();
text && CoreUtils.copyToClipboard(text);
text && CoreText.copyToClipboard(text);
}
/**

View File

@ -13,7 +13,7 @@
// limitations under the License.
import { Component, OnInit } from '@angular/core';
import { CoreUtils } from '@services/utils/utils';
import { CoreText } from '@singletons/text';
import { CoreErrorLogs, CoreSettingsErrorLog } from '@singletons/error-logs';
/**
@ -39,9 +39,9 @@ export class CoreSettingsErrorLogPage implements OnInit {
*/
async copyError(error?: CoreSettingsErrorLog): Promise<void> {
if (error) {
await CoreUtils.copyToClipboard(JSON.stringify(error));
await CoreText.copyToClipboard(JSON.stringify(error));
} else {
await CoreUtils.copyToClipboard(JSON.stringify({ errors: this.errorLogs }));
await CoreText.copyToClipboard(JSON.stringify({ errors: this.errorLogs }));
}
}

View File

@ -17,7 +17,7 @@ import { CoreSharedModule } from '@/core/shared.module';
import { Component, Input } from '@angular/core';
import { CoreFileEntry } from '@services/file-helper';
import { CoreUtils } from '@services/utils/utils';
import { CoreText } from '@singletons/text';
import { ModalController } from '@singletons';
/**
@ -56,7 +56,7 @@ export class CoreViewerTextComponent {
* Copy the text to clipboard.
*/
copyText(): void {
CoreUtils.copyToClipboard(this.content || '');
CoreText.copyToClipboard(this.content || '');
}
}

View File

@ -23,7 +23,7 @@ import { CoreWS } from '@services/ws';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreMimetypeUtils } from '@services/utils/mimetype';
import { CoreTextUtils } from '@services/utils/text';
import { makeSingleton, Clipboard, InAppBrowser, FileOpener, WebIntent, Translate, NgZone } from '@singletons';
import { makeSingleton, InAppBrowser, FileOpener, WebIntent, Translate, NgZone } from '@singletons';
import { CoreLogger } from '@singletons/logger';
import { CoreCanceledError } from '@classes/errors/cancelederror';
import { CoreFileEntry } from '@services/file-helper';
@ -40,6 +40,7 @@ import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { CoreUrlUtils } from './url';
import { QRScanner } from '@features/native/plugins';
import { CoreArray } from '@singletons/array';
import { CoreText } from '@singletons/text';
export type TreeNode<T> = T & { children: TreeNode<T>[] };
@ -366,22 +367,12 @@ export class CoreUtilsProvider {
* Copies a text to clipboard and shows a toast message.
*
* @param text Text to be copied
* @returns Promise resolved when text is copied.
* @returns Promise resolved when the text is copied.
*
* @deprecated since 4.5 Use CoreText.copyToClipboard instead.
*/
async copyToClipboard(text: string): Promise<void> {
try {
await Clipboard.copy(text);
} catch {
// Use HTML Copy command.
const virtualInput = document.createElement('textarea');
virtualInput.innerHTML = text;
virtualInput.select();
virtualInput.setSelectionRange(0, 99999);
document.execCommand('copy'); // eslint-disable-line deprecation/deprecation
}
// Show toast using ionicLoading.
CoreDomUtils.showToast('core.copiedtoclipboard', true);
return CoreText.copyToClipboard(text);
}
/**

View File

@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Clipboard } from '@singletons';
import { CoreDomUtils } from '@services/utils/dom';
/**
* Singleton with helper functions for text manipulation.
*/
@ -68,4 +71,25 @@ export class CoreText {
return text.substring(1);
}
/**
* Copies a text to clipboard and shows a toast message.
*
* @param text Text to be copied
*/
static async copyToClipboard(text: string): Promise<void> {
try {
await Clipboard.copy(text);
} catch {
// Use HTML Copy command.
const virtualInput = document.createElement('textarea');
virtualInput.innerHTML = text;
virtualInput.select();
virtualInput.setSelectionRange(0, 99999);
document.execCommand('copy'); // eslint-disable-line deprecation/deprecation
}
// Show toast using ionicLoading.
CoreDomUtils.showToast('core.copiedtoclipboard', true);
}
}