jcubic / jquery.terminal

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands
https://terminal.jcubic.pl
MIT License
3.11k stars 569 forks source link

Add this context to mousewheel and touchscroll events #965

Open jcubic opened 2 weeks ago

jcubic commented 2 weeks ago

I have an idea for a new feature for jQuery Terminal

The task is to add this context (that in source code is in self variable) to mousewheel and touchscroll and update TypeScript types for the callbacks.

This code should work:

function scroll() {
  const { length } = this.get_output().split('\n');
  const rows = term.rows();
  if (rows > length) {
    this.removeClass('shake');
    this.addClass('shake');
    this.stopTime('shake');
    this.oneTime(200, 'shake', () => {
      this.removeClass('shake');
    });
  }
}

export function initTerminal() {
  const $ = globalThis.$;
  const $term = $('.term');
  $term.empty();

  const term = $term.terminal({}, {
    mousewheel: scroll,
    touchscroll: scroll
  });

  return $term;
};

Code taken from new website (after refactoring to new API).