• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 */
15import fileIo from '@ohos.fileio'
16import mediaLibrary from '@ohos.multimedia.mediaLibrary'
17import router from '@ohos.router'
18import Logger from '../model/Logger'
19import MediaUtils from '../model/MediaUtils'
20import TitleBar from '../view/TitleBar'
21
22@Entry
23@Component
24struct DocumentPage {
25  private tag: string = 'DocumentPage'
26  private mediaUtils: MediaUtils = MediaUtils.getInstance(getContext(this))
27  @State fileContent: string = ''
28
29  async saveFile() {
30    Logger.info(this.tag, 'saveFile')
31    let fd = await this.mediaUtils.createFile(mediaLibrary.MediaType.FILE)
32    Logger.info(this.tag, `fd = ${fd}`)
33    await fileIo.write(fd, this.fileContent)
34    await fileIo.close(fd)
35    router.back()
36  }
37
38  build() {
39    Column() {
40      TitleBar()
41      TextArea({ placeholder: 'input file content', text: this.fileContent })
42        .placeholderColor(Color.Gray)
43        .fontSize(22)
44        .margin({ left: 15, right: 15, top: 15 })
45        .textAlign(TextAlign.Start)
46        .height('30%')
47        .onChange((value: string) => {
48          this.fileContent = value
49        })
50      Button() {
51        Text($r('app.string.save'))
52          .fontColor(Color.Black)
53          .fontSize(17)
54      }
55      .backgroundColor('#0D9FFB')
56      .height(50)
57      .width(200)
58      .margin({ top: 10 })
59      .onClick(() => {
60        Logger.info(this.tag, 'saveFile onClick')
61        this.saveFile()
62      })
63    }
64    .height('100%')
65    .width('100%')
66  }
67}