difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function () {
    if (!(cc && cc.EditBox)) {
        return;
    }
    
    var KeyboardReturnType = cc.EditBox.KeyboardReturnType;
    var _p = cc.EditBox._EditBoxImpl.prototype;
    var _currentEditBoxImpl = null;
 
    function getKeyboardReturnType (type) {
        switch (type) {
            case KeyboardReturnType.DEFAULT:
            case KeyboardReturnType.DONE:
                return 'done';
            case KeyboardReturnType.SEND:
                return 'send';
            case KeyboardReturnType.SEARCH:
                return 'search';
            case KeyboardReturnType.GO:
                return 'go';
            case KeyboardReturnType.NEXT:
                return 'next';
        }
        return 'done';
    }
 
    function updateLabelsVisibility(editBox) {
        var placeholderLabel = editBox._placeholderLabel;
        var textLabel = editBox._textLabel;
        var displayText = editBox._impl._text;
  
        placeholderLabel.node.active = displayText === '';
        textLabel.node.active = displayText !== '';
    }
 
    cc.EditBox.prototype.editBoxEditingDidBegan = function () {
        cc.Component.EventHandler.emitEvents(this.editingDidBegan, this);
        this.node.emit('editing-did-began', this);
    };
 
    cc.EditBox.prototype.editBoxEditingDidEnded = function () {
        cc.Component.EventHandler.emitEvents(this.editingDidEnded, this);
        this.node.emit('editing-did-ended', this);
    };
 
    cc.EditBox.prototype._updateStayOnTop = function () {
        // wx not support
    };
 
    _p.setFocus = function () {
        this._beginEditing();
    };
 
    _p.isFocused = function () {
        return this._editing;
    };
 
    _p.setInputMode = function (inputMode) {
        this._inputMode = inputMode;
    };
 
    _p._beginEditing = function () {
        this.createInput();
    };
 
    _p._endEditing = function () {
        this._delegate && this._delegate.editBoxEditingDidEnded();
        this._editing = false;
    };
 
    _p.createInput = function () {
        // Unregister keyboard event listener in old editBoxImpl if keyboard haven't hidden.
        if (_currentEditBoxImpl !== this) {
            if (_currentEditBoxImpl) {
                _currentEditBoxImpl._endEditing();
                wx.offKeyboardConfirm(_currentEditBoxImpl.onKeyboardConfirmCallback);
                wx.offKeyboardInput(_currentEditBoxImpl.onKeyboardInputCallback);
                wx.offKeyboardComplete(_currentEditBoxImpl.onKeyboardCompleteCallback);
            }
            _currentEditBoxImpl = this;
        }
 
        var multiline = this._inputMode === cc.EditBox.InputMode.ANY;
        var editBoxImpl = this;
        this._editing = true;
 
        function onKeyboardConfirmCallback (res) {
            editBoxImpl._text = res.value;
            editBoxImpl._delegate && editBoxImpl._delegate.editBoxEditingReturn && editBoxImpl._delegate.editBoxEditingReturn();
            wx.hideKeyboard({
                success: function (res) {
                    
                },
                fail: function (res) {
                    cc.warn(res.errMsg);
                }
            });
        }
 
        function onKeyboardInputCallback (res) {        
            if (res.value.length > editBoxImpl._maxLength) {
                res.value = res.value.slice(0, editBoxImpl._maxLength);
            }
            if (editBoxImpl._delegate && editBoxImpl._delegate.editBoxTextChanged) {
                if (editBoxImpl._text !== res.value) {
                    editBoxImpl._text = res.value;
                    editBoxImpl._delegate.editBoxTextChanged(editBoxImpl._text);
                    updateLabelsVisibility(editBoxImpl._delegate);
                }
            }
        }
 
        function onKeyboardCompleteCallback () {
            editBoxImpl._endEditing();
            wx.offKeyboardConfirm(onKeyboardConfirmCallback);
            wx.offKeyboardInput(onKeyboardInputCallback);
            wx.offKeyboardComplete(onKeyboardCompleteCallback);
            _currentEditBoxImpl = null;
        }
        
        wx.showKeyboard({
            defaultValue: editBoxImpl._text,
            maxLength: editBoxImpl._maxLength,
            multiple: multiline,
            confirmHold: false,  // hide keyboard mannually by wx.onKeyboardConfirm
            confirmType: getKeyboardReturnType(editBoxImpl._returnType),
            success: function (res) {
                editBoxImpl._delegate && editBoxImpl._delegate.editBoxEditingDidBegan && editBoxImpl._delegate.editBoxEditingDidBegan();
            },
            fail: function (res) {
                cc.warn(res.errMsg);
                editBoxImpl._endEditing();
            }
        });
        wx.onKeyboardConfirm(onKeyboardConfirmCallback);
        wx.onKeyboardInput(onKeyboardInputCallback);
        wx.onKeyboardComplete(onKeyboardCompleteCallback);
    };
})();