fix: fix paste not working bug

This commit is contained in:
xianshenglu 2023-11-12 00:01:04 +08:00
parent f0027bfe50
commit ed6ac63747
3 changed files with 27 additions and 7 deletions

View File

@ -19,27 +19,35 @@ export default class Clipboard {
navigator.clipboard.writeText(e.clipboardData.getData('text/plain')).catch(() => {/* Do nothing */});
}
}
/**
* @param {ClipboardEvent} e
*/
_handlePaste(e) {
if (navigator.clipboard.readText) {
navigator.clipboard.readText().then(this.onpaste).catch(() => {/* Do nothing */});
} else if (e.clipboardData) {
if(!this._isVncEvent()){
return;
}
if(e.clipboardData){
this.onpaste(e.clipboardData.getData('text/plain'));
}
}
_isVncEvent(){
const isTargetFocused = document.activeElement === this._target;
return isTargetFocused;
}
// ===== PUBLIC METHODS =====
grab() {
if (!Clipboard.isSupported) return;
this._target.addEventListener('copy', this._eventHandlers.copy);
this._target.addEventListener('paste', this._eventHandlers.paste);
// _target can not listen the paste event.
document.body.addEventListener('paste', this._eventHandlers.paste);
}
ungrab() {
if (!Clipboard.isSupported) return;
this._target.removeEventListener('copy', this._eventHandlers.copy);
this._target.removeEventListener('paste', this._eventHandlers.paste);
document.body.removeEventListener('paste', this._eventHandlers.paste);
}
}

View File

@ -85,6 +85,10 @@ export default class Keyboard {
_handleKeyDown(e) {
const code = this._getKeyCode(e);
const isCtrlVEvent = code === "KeyV" && e.ctrlKey;
if(isCtrlVEvent){
return;
}
let keysym = KeyboardUtil.getKeysym(e);
let numlock = e.getModifierState('NumLock');
let capslock = e.getModifierState('CapsLock');

View File

@ -261,7 +261,7 @@ export default class RFB extends EventTargetMixin {
}
this._clipboard = new Clipboard(this._canvas);
this._clipboard.onpaste = this.clipboardPasteFrom.bind(this);
this._clipboard.onpaste = this._handlePasteEvent.bind(this);
this._keyboard = new Keyboard(this._canvas);
this._keyboard.onkeyevent = this._handleKeyEvent.bind(this);
@ -500,6 +500,14 @@ export default class RFB extends EventTargetMixin {
this._canvas.blur();
}
_handlePasteEvent(text){
this.clipboardPasteFrom(text);
this.sendKey(KeyTable.XK_Control_L, "ControlLeft", true)
this.sendKey(KeyTable.XK_V, "KeyV", true)
this.sendKey(KeyTable.XK_V, "KeyV", false)
this.sendKey(KeyTable.XK_Control_L, "ControlLeft", false)
}
clipboardPasteFrom(text) {
if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; }