-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84a8c1b
commit e6b643c
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export default class Clipboard { | ||
constructor(target) { | ||
this._target = target; | ||
|
||
this._eventHandlers = { | ||
'copy': this._handleCopy.bind(this), | ||
'paste': this._handlePaste.bind(this) | ||
}; | ||
|
||
// ===== EVENT HANDLERS ===== | ||
|
||
this.onpaste = () => {}; | ||
} | ||
|
||
// ===== PRIVATE METHODS ===== | ||
|
||
_handleCopy(e) { | ||
if (navigator.clipboard.writeText) { | ||
navigator.clipboard.writeText(e.clipboardData.getData('text/plain')).catch(() => {/* Do nothing */}); | ||
} | ||
} | ||
|
||
_handlePaste(e) { | ||
if (navigator.clipboard.readText) { | ||
navigator.clipboard.readText().then(this.onpaste).catch(() => {/* Do nothing */}); | ||
} else if (e.clipboardData) { | ||
this.onpaste(e.clipboardData.getData('text/plain')); | ||
} | ||
} | ||
|
||
// ===== PUBLIC METHODS ===== | ||
|
||
grab() { | ||
if (!Clipboard.isSupported) return; | ||
this._target.addEventListener('copy', this._eventHandlers.copy); | ||
this._target.addEventListener('paste', this._eventHandlers.paste); | ||
} | ||
|
||
ungrab() { | ||
if (!Clipboard.isSupported) return; | ||
this._target.removeEventListener('copy', this._eventHandlers.copy); | ||
this._target.removeEventListener('paste', this._eventHandlers.paste); | ||
} | ||
} | ||
|
||
Clipboard.isSupported = (navigator && navigator.clipboard) ? true : false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const expect = chai.expect; | ||
|
||
import Clipboard from '../core/clipboard.js'; | ||
|
||
describe('Automatic Clipboard Sync', function () { | ||
"use strict"; | ||
|
||
if (Clipboard.isSupported) { | ||
beforeEach(function () { | ||
if (navigator.clipboard.writeText) { | ||
sinon.spy(navigator.clipboard, 'writeText'); | ||
} | ||
if (navigator.clipboard.readText) { | ||
sinon.spy(navigator.clipboard, 'readText'); | ||
} | ||
}); | ||
|
||
afterEach(function () { | ||
if (navigator.clipboard.writeText) { | ||
navigator.clipboard.writeText.restore(); | ||
} | ||
if (navigator.clipboard.readText) { | ||
navigator.clipboard.readText.restore(); | ||
} | ||
}); | ||
} | ||
|
||
it('incoming clipboard data from the server is copied to the local clipboard', function () { | ||
const text = 'Random string for testing'; | ||
const clipboard = new Clipboard(); | ||
if (Clipboard.isSupported) { | ||
const clipboardData = new DataTransfer(); | ||
clipboardData.setData("text/plain", text); | ||
clipboard._handleCopy(new ClipboardEvent('paste', { clipboardData })); | ||
if (navigator.clipboard.writeText) { | ||
expect(navigator.clipboard.writeText).to.have.been.calledWith(text); | ||
} | ||
} | ||
}); | ||
|
||
it('should copy local pasted data to the server clipboard', function () { | ||
const text = 'Another random string for testing'; | ||
const clipboard = new Clipboard(); | ||
clipboard.onpaste = pasterText => expect(pasterText).to.equal(text); | ||
if (Clipboard.isSupported) { | ||
const clipboardData = new DataTransfer(); | ||
clipboardData.setData("text/plain", text); | ||
clipboard._handlePaste(new ClipboardEvent('paste', { clipboardData })); | ||
if (navigator.clipboard.readText) { | ||
expect(navigator.clipboard.readText).to.have.been.called; | ||
} | ||
} | ||
}); | ||
}); |