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 */
15
16import { BaseElement, element } from '../../../base-ui/BaseElement';
17import { info } from '../../../log/Log';
18import { SpStatisticsHttpUtil } from '../../../statistics/util/SpStatisticsHttpUtil';
19import { PluginConvertUtils } from './utils/PluginConvertUtils';
20import { SpTraceCommandHtml } from './SpTraceCommand.html';
21
22@element('trace-command')
23export class SpTraceCommand extends BaseElement {
24  private codeHl: HTMLTextAreaElement | undefined | null;
25  private copyEl: HTMLElement | undefined | null;
26
27  get hdcCommon(): string {
28    return `${this.codeHl!.textContent}`;
29  }
30
31  set hdcCommon(value: string) {
32    info('hdc Common is:', value);
33    this.codeHl!.textContent = value;
34  }
35
36  //当 custom element首次被插入文档DOM时,被调用。
37  public connectedCallback(): void {
38    this.codeHl!.textContent = '';
39    this.copyEl?.addEventListener('click', this.codeCopyEvent);
40    this.codeHl?.addEventListener('selectionchange', this.textSelectEvent);
41  }
42
43  public disconnectedCallback(): void {
44    this.copyEl?.removeEventListener('click', this.codeCopyEvent);
45  }
46
47  codeCopyEvent = (): void => {
48    this.codeHl?.select();
49    document.execCommand('copy');
50    let allPlugin: Array<string> = [];
51    PluginConvertUtils.pluginConfig.forEach((plugin) => {
52      //@ts-ignore
53      allPlugin.push(plugin.pluginName);
54    });
55    SpStatisticsHttpUtil.addOrdinaryVisitAction({
56      action: 'config_page',
57      event: 'offline_record',
58      eventData: {
59        plugin: allPlugin,
60      },
61    });
62  };
63
64  textSelectEvent = (): void => {
65    this.copyEl!.style.backgroundColor = '#FFFFFF';
66  };
67
68  initElements(): void {
69    this.codeHl = this.shadowRoot?.querySelector('#code-text') as HTMLTextAreaElement;
70    this.copyEl = this.shadowRoot?.querySelector('#copy-image') as HTMLElement;
71  }
72
73  initHtml(): string {
74    return SpTraceCommandHtml;
75  }
76}
77