diff --git a/.editorconfig b/.editorconfig index f2aa1a38..ae59e935 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,7 +6,7 @@ indent_size = 2 insert_final_newline = true trim_trailing_whitespace = true -[*.js] +[*.{j,t}s] max_line_length = 100 [*.css] diff --git a/src/Interfaces.ts b/src/Interfaces.ts index db1b622a..ff7bbef2 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -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. @@ -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); } diff --git a/src/test/clipboard-test.js b/src/handlers/Clipboard.test.ts similarity index 81% rename from src/test/clipboard-test.js rename to src/handlers/Clipboard.test.ts index 12eeb517..3302cb6c 100644 --- a/src/test/clipboard-test.js +++ b/src/handlers/Clipboard.test.ts @@ -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 () { diff --git a/src/handlers/Clipboard.js b/src/handlers/Clipboard.ts similarity index 74% rename from src/handlers/Clipboard.js rename to src/handlers/Clipboard.ts index 70c625e4..825023f0 100644 --- a/src/handlers/Clipboard.js +++ b/src/handlers/Clipboard.ts @@ -5,6 +5,15 @@ * @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; +} + /** * Prepares text copied from terminal selection, to be saved in the clipboard by: * 1. stripping all trailing white spaces @@ -12,14 +21,14 @@ * @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'); @@ -31,12 +40,15 @@ 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) { + // We cast `window` to `any` type, because TypeScript has not declared the `clipboardData` + // property that we use below for Internet Explorer. + let w = 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); } @@ -49,23 +61,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 = 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); } } @@ -84,16 +99,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