1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import promptAction from '@ohos.promptAction'; 17import Logger from '../utils/Logger'; 18import pasteboard from '@ohos.pasteboard'; 19import Constants from '../utils/Constants'; 20 21@Component 22export struct PasteControl { 23 @Link isSelectionMenuHidden: boolean; 24 @Link textInputValue: string; 25 26 @Builder 27 CopyButton() { 28 Text($r('app.string.copy')) 29 .fontSize($r('app.integer.layout_size_16')) 30 .fontColor(Color.Black) 31 .onClick(() => { 32 this.isSelectionMenuHidden = false; 33 this.copyMessage(); 34 }) 35 } 36 37 promptAction(message: string | Resource) { 38 try { 39 promptAction.showToast({ 40 message: message, 41 duration: 3000, 42 }); 43 } catch (error) { 44 Logger.error(`showToast args error code is ${error.code}, message is ${error.message}`); 45 } 46 } 47 48 copyMessage() { 49 let systemPasteboard = pasteboard.getSystemPasteboard(); 50 let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, this.textInputValue); 51 systemPasteboard = pasteboard.getSystemPasteboard(); 52 systemPasteboard.setData(pasteData).then(async () => { 53 this.promptAction($r('app.string.copy_succeed')); 54 Logger.info('Succeeded in setting PasteData.'); 55 }); 56 } 57 58 pastToMessage() { 59 let systemPasteboard = pasteboard.getSystemPasteboard(); 60 systemPasteboard.getData().then((pasteData) => { 61 let primaryText = pasteData.getPrimaryText(); 62 let currentInputValue: string = this.textInputValue; 63 if (!primaryText) { 64 this.promptAction('Empty'); 65 } 66 this.textInputValue = currentInputValue + primaryText; 67 this.promptAction($r('app.string.paste_succeed')); 68 Logger.error('Succeed to get PasteData. primaryText' + primaryText); 69 }).catch((error: string) => { 70 this.promptAction(error); 71 Logger.info('promise, getCurrentLocation: error=' + JSON.stringify(error)); 72 }); 73 } 74 75 build() { 76 Row() { 77 this.CopyButton() 78 PasteButton() 79 .fontSize($r('app.integer.layout_size_16')) 80 .fontColor(Color.Black) 81 .backgroundColor(Color.White) 82 .padding(0) 83 .onClick(() => { 84 this.isSelectionMenuHidden = false; 85 this.pastToMessage(); 86 }) 87 } 88 .width($r('app.integer.layout_size_120')) 89 .justifyContent(FlexAlign.SpaceAround) 90 .padding({ 91 left: $r('app.integer.layout_size_20'), 92 right: $r('app.integer.layout_size_20'), 93 top: $r('app.integer.layout_size_10'), 94 bottom: $r('app.integer.layout_size_10') 95 }) 96 .borderRadius($r('app.integer.layout_size_20')) 97 .backgroundColor(Color.White) 98 .border({ 99 width: Constants.BORDERWIDTH, 100 color: $r('app.color.border_color') 101 }) 102 .position({ x: $r('app.integer.layout_size_60'), y: -50 }) 103 } 104}