Skip to content

Commit

Permalink
Release 2.4.0
Browse files Browse the repository at this point in the history
Fix compilation errors from dep upgrades
  • Loading branch information
butlerx committed Mar 28, 2022
1 parent 17b75c9 commit d356cb2
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wetty",
"version": "2.3.0",
"version": "2.4.0",
"description": "WeTTY = Web + TTY. Terminal access in browser over http/https",
"homepage": "https://github.com/butlerx/wetty",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/client/wetty/mobile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';

export function mobileKeyboard(): void {
const [screen] = document.getElementsByClassName('xterm-screen');
const [screen] = Array.from(document.getElementsByClassName('xterm-screen'));
if (_.isNull(screen)) return;
screen.setAttribute('contenteditable', 'true');
screen.setAttribute('spellcheck', 'false');
Expand Down
3 changes: 1 addition & 2 deletions src/client/wetty/socket.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import io from 'socket.io-client';

const userRegex = new RegExp('ssh/[^/]+$');
export const trim = (str: string): string => str.replace(/\/*$/, '');

const socketBase = trim(window.location.pathname).replace(userRegex, '');
const socketBase = trim(window.location.pathname).replace('/ssh/[^/]+$/', '');
export const socket = io(window.location.origin, {
path: `${trim(socketBase)}/socket.io`,
});
13 changes: 12 additions & 1 deletion src/client/wetty/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _ from 'lodash';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import { WebLinksAddon } from 'xterm-addon-web-links';
import type { Options } from './term/confiruragtion/shared/options';
import { configureTerm, shouldFitTerm } from './term/confiruragtion.js';
import { terminal as termElement } from '../shared/elements.js';

Expand All @@ -25,13 +26,23 @@ export class Term extends Terminal {
}
}

declare global {
interface Window {
wetty_term?: Term;
wetty_close_config?: () => void;
wetty_save_config?: (newConfig: Options) => void;
clipboardData: DataTransfer;
loadOptions: (conf: Options) => void;
}
}

export function terminal(socket: Socket): Term | undefined {
const term = new Term(socket) as Term;
if (_.isNull(termElement)) return undefined;
termElement.innerHTML = '';
term.open(termElement);
configureTerm(term);
window.onresize = term.resizeTerm;
(window as any).wetty_term = term;
window.wetty_term = term;
return term;
}
31 changes: 18 additions & 13 deletions src/client/wetty/term/confiruragtion.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Term } from '../term';
import type { Options } from './confiruragtion/shared/options';
import { copySelected, copyShortcut } from './confiruragtion/clipboard';
import { onInput, setOptions } from './confiruragtion/editor';
import { editor } from '../../shared/elements';
import { loadOptions } from './confiruragtion/load';

export function configureTerm(term: Term): void {
let options = loadOptions();
// Convert old options to new options
if (!('xterm' in options)) options = { xterm: options };
const options = loadOptions();
try {
setOptions(term, options);
} catch {
Expand All @@ -16,27 +15,33 @@ export function configureTerm(term: Term): void {

const toggle = document.querySelector('#options .toggler');
const optionsElem = document.getElementById('options');
if (editor == null || toggle == null || optionsElem == null)
if (editor == null || toggle == null || optionsElem == null) {
throw new Error("Couldn't initialize configuration menu");
}

function editorOnLoad() {
(editor.contentWindow as any).loadOptions(loadOptions());
(editor.contentWindow as any).wetty_close_config = () => {
optionsElem!.classList.toggle('opened');
editor?.contentWindow?.loadOptions(loadOptions());
editor.contentWindow!.wetty_close_config = () => {
optionsElem?.classList.toggle('opened');
};
(editor.contentWindow as any).wetty_save_config = (newConfig: any) => {
editor.contentWindow!.wetty_save_config = (newConfig: Options) => {
onInput(term, newConfig);
};
}
if (
(editor.contentDocument || editor.contentWindow!.document).readyState ===
'complete'
)
(
editor.contentDocument ||
(editor.contentWindow?.document ?? {
readyState: '',
})
).readyState === 'complete'
) {
editorOnLoad();
}
editor.addEventListener('load', editorOnLoad);

toggle.addEventListener('click', e => {
(editor.contentWindow as any).loadOptions(loadOptions());
editor?.contentWindow?.loadOptions(loadOptions());
optionsElem.classList.toggle('opened');
e.preventDefault();
});
Expand All @@ -53,5 +58,5 @@ export function configureTerm(term: Term): void {
}

export function shouldFitTerm(): boolean {
return (loadOptions() as any).wettyFitTerminal ?? true;
return loadOptions().wettyFitTerminal ?? true;
}
4 changes: 2 additions & 2 deletions src/client/wetty/term/confiruragtion/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
@returns boolean to indicate success or failure
*/
export function copySelected(text: string): boolean {
if ((window as any).clipboardData?.setData) {
(window as any).clipboardData.setData('Text', text);
if (window.clipboardData?.setData) {
window.clipboardData.setData('Text', text);
return true;
}
if (
Expand Down
12 changes: 6 additions & 6 deletions src/client/wetty/term/confiruragtion/editor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Term } from '../../term';
import { editor } from '../../../shared/elements';
import type { Options } from './shared/options';

export const onInput = (term: Term, updated: any) => {
export const onInput = (term: Term, updated: Options) => {
try {
const updatedConf = JSON.stringify(updated, null, 2);
if (localStorage.options === updatedConf) return;
Expand All @@ -16,16 +17,15 @@ export const onInput = (term: Term, updated: any) => {
editor.classList.remove('error');
localStorage.options = updatedConf;
} catch (e) {
console.error('Configuration Error');
console.error(e);
console.error('Configuration Error', e);
editor.classList.add('error');
}
};

export function setOptions(term: Term, options: any) {
Object.keys(options.xterm).forEach(key => {
export function setOptions(term: Term, { xterm }: Options) {
Object.keys(xterm).forEach(key => {
if (key === 'cols' || key === 'rows') return;
const value = options.xterm[key];
const value = xterm[key];
term.setOption(key, value);
});
}
19 changes: 16 additions & 3 deletions src/client/wetty/term/confiruragtion/load.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import _ from 'lodash';
import type { XTerm, Options } from './shared/options';

export function loadOptions(): Record<string, unknown> {
const defaultOptions = { xterm: { fontSize: 14 } };
export const defaultOptions: Options = {
xterm: { fontSize: 14 },
wettyVoid: 0,
wettyFitTerminal: true,
};

export function loadOptions(): Options {
try {
return _.isUndefined(localStorage.options)
let options = _.isUndefined(localStorage.options)
? defaultOptions
: JSON.parse(localStorage.options);
// Convert old options to new options
if (!('xterm' in options)) {
const xterm = options;
options = defaultOptions;
options.xterm = xterm as unknown as XTerm;
}
return options;
} catch {
return defaultOptions;
}
Expand Down
11 changes: 11 additions & 0 deletions src/client/wetty/term/confiruragtion/shared/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type XTerm = {
cols?: number;
rows?: number;
fontSize: number;
} & Record<string, unknown>;

export interface Options {
xterm: XTerm;
wettyFitTerminal: boolean;
wettyVoid: number;
}
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function start(
serverConf: Server = serverDefault,
command: string = defaultCommand,
forcessh: boolean = forceSSHDefault,
ssl?: SSL,
ssl: SSL | undefined = undefined,
): Promise<SocketIO.Server> {
const logger = getLogger();
if (ssh.key) {
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"include": [
"src/client"
]
"compilerOptions": {
"lib": ["DOM"]
},
"include": ["src/client"]
}

0 comments on commit d356cb2

Please sign in to comment.