1const { XTools } = require('../engine/XTools');
2
3/*
4 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17class CanvasInput {
18  static FOCUS = false;
19  static SAFE_AREA = [];
20  static SetSafeArea(x, y, w, h) {
21    CanvasInput.SAFE_AREA = [x, y, w, h];
22  }
23  static Reset(x, y, w, h, value, cb, cb2) {
24    CanvasInput.SAFE_AREA = [x, y, w, h];
25    let ci = document.getElementById('canvas_textarea');
26    ci.style.left = x + 'px';
27    ci.style.top = y + 'px';
28    ci.style.width = w + 'px';
29    ci.style.height = h + 'px';
30    ci.style.display = 'block';
31    ci.value = value;
32    ci.onchange = (e) => {
33      if (cb) {
34        cb(e.target.value);
35      }
36    };
37    ci.oninput = (e) => {
38      if (cb2) {
39        cb2(e.target.value);
40      }
41    };
42    ci.focus();
43
44    ci.addEventListener('keydown', (k) => {
45      if (k.key === 'Enter') {
46        if (k.shiftKey) {
47        } else {
48          CanvasInput.Hide();
49        }
50      }
51    });
52    CanvasInput.FOCUS = true;
53  }
54  static Hide(x, y) {
55    if (x && y) {
56      if (XTools.InRect(x, y, ...CanvasInput.SAFE_AREA)) {
57        return;
58      }
59    }
60    let ci = document.getElementById('canvas_textarea');
61    ci.style.display = 'none';
62    CanvasInput.FOCUS = false;
63  }
64}
65
66module.exports = {
67  CanvasInput,
68};
69