Merge pull request #393 from sourcelair/migrate-clipboard-to-typescript
Migrate Clipboard handler to TypeScript
This commit is contained in:
+1
-1
@@ -6,7 +6,7 @@ indent_size = 2
|
|||||||
insert_final_newline = true
|
insert_final_newline = true
|
||||||
trim_trailing_whitespace = true
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
[*.js]
|
[*.{j,t}s]
|
||||||
max_line_length = 100
|
max_line_length = 100
|
||||||
|
|
||||||
[*.css]
|
[*.css]
|
||||||
|
|||||||
@@ -2,12 +2,26 @@
|
|||||||
* @license MIT
|
* @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 {
|
export interface ITerminal {
|
||||||
element: HTMLElement;
|
element: HTMLElement;
|
||||||
rowContainer: HTMLElement;
|
rowContainer: HTMLElement;
|
||||||
|
textarea: HTMLTextAreaElement;
|
||||||
ydisp: number;
|
ydisp: number;
|
||||||
lines: string[];
|
lines: string[];
|
||||||
rows: number;
|
rows: number;
|
||||||
|
browser: IBrowser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emit the 'data' event and populate the given data.
|
* Emit the 'data' event and populate the given data.
|
||||||
@@ -16,4 +30,5 @@ export interface ITerminal {
|
|||||||
handler(data: string);
|
handler(data: string);
|
||||||
on(event: string, callback: () => void);
|
on(event: string, callback: () => void);
|
||||||
scrollDisp(disp: number, suppressScrollEvent: boolean);
|
scrollDisp(disp: number, suppressScrollEvent: boolean);
|
||||||
|
cancel(ev: Event, force?: boolean);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
var assert = require('chai').assert;
|
import { assert } from 'chai';
|
||||||
var Terminal = require('../xterm');
|
import * as Terminal from '../xterm';
|
||||||
var Clipboard = require('../handlers/Clipboard');
|
import * as Clipboard from './Clipboard';
|
||||||
|
|
||||||
|
|
||||||
describe('evaluateCopiedTextProcessing', function () {
|
describe('evaluateCopiedTextProcessing', function () {
|
||||||
@@ -5,6 +5,24 @@
|
|||||||
* @license MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { ITerminal } from '../Interfaces';
|
||||||
|
|
||||||
|
// We are extending the ClipboardEvent interface, since TypeScript has not declared the
|
||||||
|
// clientX and clientY properties that we use.
|
||||||
|
interface IClipboardEvent extends ClipboardEvent {
|
||||||
|
clientX: number;
|
||||||
|
clientY: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IWindow extends Window {
|
||||||
|
clipboardData?: {
|
||||||
|
getData(format: string): string;
|
||||||
|
setData(format: string, data: string);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
declare var window: IWindow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepares text copied from terminal selection, to be saved in the clipboard by:
|
* Prepares text copied from terminal selection, to be saved in the clipboard by:
|
||||||
* 1. stripping all trailing white spaces
|
* 1. stripping all trailing white spaces
|
||||||
@@ -12,14 +30,14 @@
|
|||||||
* @param {string} text The copied text that needs processing for storing in clipboard
|
* @param {string} text The copied text that needs processing for storing in clipboard
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function prepareTextForClipboard(text) {
|
export function prepareTextForClipboard(text: string): string {
|
||||||
var space = String.fromCharCode(32),
|
let space = String.fromCharCode(32),
|
||||||
nonBreakingSpace = String.fromCharCode(160),
|
nonBreakingSpace = String.fromCharCode(160),
|
||||||
allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'),
|
allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'),
|
||||||
processedText = text.split('\n').map(function (line) {
|
processedText = text.split('\n').map(function (line) {
|
||||||
// Strip all trailing white spaces and convert all non-breaking spaces
|
// Strip all trailing white spaces and convert all non-breaking spaces
|
||||||
// to regular spaces.
|
// to regular spaces.
|
||||||
var processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);
|
let processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);
|
||||||
|
|
||||||
return processedLine;
|
return processedLine;
|
||||||
}).join('\n');
|
}).join('\n');
|
||||||
@@ -31,8 +49,10 @@ function prepareTextForClipboard(text) {
|
|||||||
* Binds copy functionality to the given terminal.
|
* Binds copy functionality to the given terminal.
|
||||||
* @param {ClipboardEvent} ev The original copy event to be handled
|
* @param {ClipboardEvent} ev The original copy event to be handled
|
||||||
*/
|
*/
|
||||||
function copyHandler(ev, term) {
|
export function copyHandler(ev: IClipboardEvent, term: ITerminal) {
|
||||||
var copiedText = window.getSelection().toString(),
|
// We cast `window` to `any` type, because TypeScript has not declared the `clipboardData`
|
||||||
|
// property that we use below for Internet Explorer.
|
||||||
|
let copiedText = window.getSelection().toString(),
|
||||||
text = prepareTextForClipboard(copiedText);
|
text = prepareTextForClipboard(copiedText);
|
||||||
|
|
||||||
if (term.browser.isMSIE) {
|
if (term.browser.isMSIE) {
|
||||||
@@ -49,10 +69,12 @@ function copyHandler(ev, term) {
|
|||||||
* @param {ClipboardEvent} ev The original paste event to be handled
|
* @param {ClipboardEvent} ev The original paste event to be handled
|
||||||
* @param {Terminal} term The terminal on which to apply the handled paste event
|
* @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();
|
ev.stopPropagation();
|
||||||
|
|
||||||
var dispatchPaste = function(text) {
|
let text: string;
|
||||||
|
|
||||||
|
let dispatchPaste = function(text) {
|
||||||
term.handler(text);
|
term.handler(text);
|
||||||
term.textarea.value = '';
|
term.textarea.value = '';
|
||||||
return term.cancel(ev);
|
return term.cancel(ev);
|
||||||
@@ -60,12 +82,12 @@ function pasteHandler(ev, term) {
|
|||||||
|
|
||||||
if (term.browser.isMSIE) {
|
if (term.browser.isMSIE) {
|
||||||
if (window.clipboardData) {
|
if (window.clipboardData) {
|
||||||
var text = window.clipboardData.getData('Text');
|
text = window.clipboardData.getData('Text');
|
||||||
dispatchPaste(text);
|
dispatchPaste(text);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (ev.clipboardData) {
|
if (ev.clipboardData) {
|
||||||
var text = ev.clipboardData.getData('text/plain');
|
text = ev.clipboardData.getData('text/plain');
|
||||||
dispatchPaste(text);
|
dispatchPaste(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,16 +106,16 @@ function pasteHandler(ev, term) {
|
|||||||
* @param {ClipboardEvent} ev The original paste event to be handled
|
* @param {ClipboardEvent} ev The original paste event to be handled
|
||||||
* @param {Terminal} term The terminal on which to apply the handled paste event
|
* @param {Terminal} term The terminal on which to apply the handled paste event
|
||||||
*/
|
*/
|
||||||
function rightClickHandler(ev, term) {
|
export function rightClickHandler(ev: IClipboardEvent, term: ITerminal) {
|
||||||
var s = document.getSelection(),
|
let s = document.getSelection(),
|
||||||
selectedText = prepareTextForClipboard(s.toString()),
|
selectedText = prepareTextForClipboard(s.toString()),
|
||||||
clickIsOnSelection = false;
|
clickIsOnSelection = false,
|
||||||
|
x = ev.clientX,
|
||||||
|
y = ev.clientY;
|
||||||
|
|
||||||
if (s.rangeCount) {
|
if (s.rangeCount) {
|
||||||
var r = s.getRangeAt(0),
|
let r = s.getRangeAt(0),
|
||||||
cr = r.getClientRects(),
|
cr = r.getClientRects(),
|
||||||
x = ev.clientX,
|
|
||||||
y = ev.clientY,
|
|
||||||
i, rect;
|
i, rect;
|
||||||
|
|
||||||
for (i=0; i<cr.length; i++) {
|
for (i=0; i<cr.length; i++) {
|
||||||
@@ -123,7 +145,7 @@ function rightClickHandler(ev, term) {
|
|||||||
term.textarea.style.height = '20px';
|
term.textarea.style.height = '20px';
|
||||||
term.textarea.style.left = (x - 10) + 'px';
|
term.textarea.style.left = (x - 10) + 'px';
|
||||||
term.textarea.style.top = (y - 10) + 'px';
|
term.textarea.style.top = (y - 10) + 'px';
|
||||||
term.textarea.style.zIndex = 1000;
|
term.textarea.style.zIndex = '1000';
|
||||||
term.textarea.focus();
|
term.textarea.focus();
|
||||||
|
|
||||||
// Reset the terminal textarea's styling
|
// Reset the terminal textarea's styling
|
||||||
@@ -137,7 +159,3 @@ function rightClickHandler(ev, term) {
|
|||||||
}, 4);
|
}, 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
|
||||||
prepareTextForClipboard, copyHandler, pasteHandler, rightClickHandler
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user