Skip to content

Commit

Permalink
Migrate Clipboard handler to TypeScript
Browse files Browse the repository at this point in the history
Also apply JavaScript rules in `.editorconfig` in TypeScript files as well.

Part of #335
  • Loading branch information
parisk committed Dec 6, 2016
1 parent 9f5790f commit 6ef6d06
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.js]
[*.{j,t}s]
max_line_length = 100

[*.css]
Expand Down
15 changes: 15 additions & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
* @license MIT
*/

export interface IBrowser {
isNode: boolean;
userAgent: string;
platform: string;
isFirefox: boolean;
isMSIE: boolean;
isMac: boolean;
isIpad: boolean;
isIphone: boolean;
isMSWindows: boolean;
}

export interface ITerminal {
element: HTMLElement;
rowContainer: HTMLElement;
textarea: HTMLTextAreaElement;
ydisp: number;
lines: string[];
rows: number;
browser: IBrowser;

/**
* Emit the 'data' event and populate the given data.
Expand All @@ -16,4 +30,5 @@ export interface ITerminal {
handler(data: string);
on(event: string, callback: () => void);
scrollDisp(disp: number, suppressScrollEvent: boolean);
cancel(ev: Event, force?: boolean);
}
6 changes: 3 additions & 3 deletions src/test/clipboard-test.js → src/handlers/Clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert = require('chai').assert;
var Terminal = require('../xterm');
var Clipboard = require('../handlers/Clipboard');
import { assert } from 'chai';
import * as Terminal from '../xterm';
import * as Clipboard from './Clipboard';


describe('evaluateCopiedTextProcessing', function () {
Expand Down
51 changes: 29 additions & 22 deletions src/handlers/Clipboard.js → src/handlers/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
* @license MIT
*/

import { ITerminal } from '../Interfaces';

interface IClipboardEvent extends ClipboardEvent {
clientX: number;
clientY: number;
}

/**
* Prepares text copied from terminal selection, to be saved in the clipboard by:
* 1. stripping all trailing white spaces
* 2. converting all non-breaking spaces to regular spaces
* @param {string} text The copied text that needs processing for storing in clipboard
* @returns {string}
*/
function prepareTextForClipboard(text) {
var space = String.fromCharCode(32),
export function prepareTextForClipboard(text: string) {
let space = String.fromCharCode(32),
nonBreakingSpace = String.fromCharCode(160),
allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'),
processedText = text.split('\n').map(function (line) {
// Strip all trailing white spaces and convert all non-breaking spaces
// to regular spaces.
var processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);
let processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);

return processedLine;
}).join('\n');
Expand All @@ -31,12 +38,13 @@ function prepareTextForClipboard(text) {
* Binds copy functionality to the given terminal.
* @param {ClipboardEvent} ev The original copy event to be handled
*/
function copyHandler(ev, term) {
var copiedText = window.getSelection().toString(),
export function copyHandler(ev: IClipboardEvent, term: ITerminal) {
let w = <any>window,
copiedText = w.getSelection().toString(),
text = prepareTextForClipboard(copiedText);

if (term.browser.isMSIE) {
window.clipboardData.setData('Text', text);
w.clipboardData.setData('Text', text);
} else {
ev.clipboardData.setData('text/plain', text);
}
Expand All @@ -49,23 +57,26 @@ function copyHandler(ev, term) {
* @param {ClipboardEvent} ev The original paste event to be handled
* @param {Terminal} term The terminal on which to apply the handled paste event
*/
function pasteHandler(ev, term) {
export function pasteHandler(ev: IClipboardEvent, term: ITerminal) {
ev.stopPropagation();

var dispatchPaste = function(text) {
let w = <any>window,
text: string;

let dispatchPaste = function(text) {
term.handler(text);
term.textarea.value = '';
return term.cancel(ev);
};

if (term.browser.isMSIE) {
if (window.clipboardData) {
var text = window.clipboardData.getData('Text');
if (w.clipboardData) {
text = w.clipboardData.getData('Text');
dispatchPaste(text);
}
} else {
if (ev.clipboardData) {
var text = ev.clipboardData.getData('text/plain');
text = ev.clipboardData.getData('text/plain');
dispatchPaste(text);
}
}
Expand All @@ -84,16 +95,16 @@ function pasteHandler(ev, term) {
* @param {ClipboardEvent} ev The original paste event to be handled
* @param {Terminal} term The terminal on which to apply the handled paste event
*/
function rightClickHandler(ev, term) {
var s = document.getSelection(),
export function rightClickHandler(ev: IClipboardEvent, term: ITerminal) {
let s = document.getSelection(),
selectedText = prepareTextForClipboard(s.toString()),
clickIsOnSelection = false;
clickIsOnSelection = false,
x = ev.clientX,
y = ev.clientY;

if (s.rangeCount) {
var r = s.getRangeAt(0),
let r = s.getRangeAt(0),
cr = r.getClientRects(),
x = ev.clientX,
y = ev.clientY,
i, rect;

for (i=0; i<cr.length; i++) {
Expand Down Expand Up @@ -123,7 +134,7 @@ function rightClickHandler(ev, term) {
term.textarea.style.height = '20px';
term.textarea.style.left = (x - 10) + 'px';
term.textarea.style.top = (y - 10) + 'px';
term.textarea.style.zIndex = 1000;
term.textarea.style.zIndex = '1000';
term.textarea.focus();

// Reset the terminal textarea's styling
Expand All @@ -137,7 +148,3 @@ function rightClickHandler(ev, term) {
}, 4);
}
}

export {
prepareTextForClipboard, copyHandler, pasteHandler, rightClickHandler
};

0 comments on commit 6ef6d06

Please sign in to comment.