|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | +/*----------------------------------------------------------------------------- |
| 4 | +| Copyright (c) 2019, PhosphorJS Contributors |
| 5 | +| |
| 6 | +| Distributed under the terms of the BSD 3-Clause License. |
| 7 | +| |
| 8 | +| The full license is in the file LICENSE, distributed with this software. |
| 9 | +|----------------------------------------------------------------------------*/ |
| 10 | + |
| 11 | +import { CaptureWidget } from '@lumino/keyboard-capture'; |
| 12 | +import { Message } from '@lumino/messaging'; |
| 13 | +import { ISignal, Signal } from '@lumino/signaling'; |
| 14 | +import { Panel, Widget } from '@lumino/widgets'; |
| 15 | + |
| 16 | +import '../style/index.css'; |
| 17 | + |
| 18 | +export class OutputWidget extends Widget { |
| 19 | + /** |
| 20 | + * |
| 21 | + */ |
| 22 | + constructor(options?: Widget.IOptions) { |
| 23 | + super(options); |
| 24 | + this._output = document.createElement('div'); |
| 25 | + this._exportButton = document.createElement('button'); |
| 26 | + this._exportButton.innerText = 'Show'; |
| 27 | + this._copyButton = document.createElement('button'); |
| 28 | + this._copyButton.innerText = 'Copy'; |
| 29 | + this._clearButton = document.createElement('button'); |
| 30 | + this._clearButton.innerText = 'Clear'; |
| 31 | + this.node.appendChild(this._exportButton); |
| 32 | + this.node.appendChild(this._copyButton); |
| 33 | + this.node.appendChild(this._clearButton); |
| 34 | + this.node.appendChild(this._output); |
| 35 | + this.addClass('lm-keyboardCaptureOutputArea'); |
| 36 | + } |
| 37 | + |
| 38 | + set value(content: string) { |
| 39 | + this._output.innerHTML = content; |
| 40 | + } |
| 41 | + |
| 42 | + get action(): ISignal<this, 'display' | 'clipboard' | 'clear'> { |
| 43 | + return this._action; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Handle the DOM events for the widget. |
| 48 | + * |
| 49 | + * @param event - The DOM event sent to the element. |
| 50 | + */ |
| 51 | + handleEvent(event: Event): void { |
| 52 | + switch (event.type) { |
| 53 | + case 'click': |
| 54 | + if (event.target === this._exportButton) { |
| 55 | + event.stopPropagation(); |
| 56 | + this._action.emit('display'); |
| 57 | + } else if (event.target === this._copyButton) { |
| 58 | + event.stopPropagation(); |
| 59 | + this._action.emit('clipboard'); |
| 60 | + } else if (event.target === this._clearButton) { |
| 61 | + event.stopPropagation(); |
| 62 | + this._action.emit('clear'); |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A message handler invoked on a `'before-attach'` message. |
| 70 | + */ |
| 71 | + protected onBeforeAttach(msg: Message): void { |
| 72 | + this._exportButton.addEventListener('click', this); |
| 73 | + this._copyButton.addEventListener('click', this); |
| 74 | + this._clearButton.addEventListener('click', this); |
| 75 | + super.onBeforeAttach(msg); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * A message handler invoked on an `'after-detach'` message. |
| 80 | + */ |
| 81 | + protected onAfterDetach(msg: Message): void { |
| 82 | + super.onAfterDetach(msg); |
| 83 | + this._exportButton.removeEventListener('click', this); |
| 84 | + this._copyButton.removeEventListener('click', this); |
| 85 | + this._clearButton.removeEventListener('click', this); |
| 86 | + } |
| 87 | + |
| 88 | + private _output: HTMLElement; |
| 89 | + private _exportButton: HTMLButtonElement; |
| 90 | + private _copyButton: HTMLButtonElement; |
| 91 | + private _clearButton: HTMLButtonElement; |
| 92 | + private _action = new Signal<this, 'display' | 'clipboard' | 'clear'>(this); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Initialize the applicaiton. |
| 97 | + */ |
| 98 | +async function init(): Promise<void> { |
| 99 | + // Add the text editors to a dock panel. |
| 100 | + let capture = new CaptureWidget(); |
| 101 | + let output = new OutputWidget(); |
| 102 | + |
| 103 | + capture.node.textContent = |
| 104 | + 'Focus me and hit each key on your keyboard without any modifiers'; |
| 105 | + |
| 106 | + // Add the dock panel to the document. |
| 107 | + let box = new Panel(); |
| 108 | + box.id = 'main'; |
| 109 | + box.addWidget(capture); |
| 110 | + box.addWidget(output); |
| 111 | + |
| 112 | + capture.dataAdded.connect((sender, entry) => { |
| 113 | + output.value = `Added ${entry.type}: ${ |
| 114 | + entry.code ? `${entry.code} →` : '' |
| 115 | + } <kbd>${entry.key}</kbd>`; |
| 116 | + }); |
| 117 | + output.action.connect((sender, action) => { |
| 118 | + if (action === 'clipboard') { |
| 119 | + navigator.clipboard.writeText(capture.formatMap()); |
| 120 | + } else if (action === 'clear') { |
| 121 | + capture.clear(); |
| 122 | + output.value = ' '; |
| 123 | + } else { |
| 124 | + output.value = `<pre>${capture.formatMap()}</pre>`; |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + window.onresize = () => { |
| 129 | + box.update(); |
| 130 | + }; |
| 131 | + Widget.attach(box, document.body); |
| 132 | +} |
| 133 | + |
| 134 | +window.onload = init; |
0 commit comments