Skip to content

Commit

Permalink
revert to partial jerch#37 fix, remove workerPath from arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jerch committed Dec 10, 2022
1 parent 1f1d965 commit 3489a31
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ImageAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ImageAddon implements ITerminalAddon {
private _terminal: ITerminalExt | undefined;
private _handlers: Map<String, IResetHandler> = new Map();

constructor(workerPath: string, opts: Partial<IImageAddonOptions>) {
constructor(opts?: Partial<IImageAddonOptions>) {
this._opts = Object.assign({}, DEFAULT_OPTIONS, opts);
this._defaultOpts = Object.assign({}, DEFAULT_OPTIONS, opts);
}
Expand Down
18 changes: 18 additions & 0 deletions src/ImageStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ export class ImageStorage implements IDisposable {
this._fullyCleared = false;
}

/**
* Only advance text cursor.
* This is an edge case from empty sixels carrying only a height but no pixels.
* Partially fixes https://github.com/jerch/xterm-addon-image/issues/37.
*/
public advanceCursor(height: number): void {
if (this._opts.sixelScrolling) {
let cellSize = this._renderer.cellSize;
if (cellSize.width === -1 || cellSize.height === -1) {
cellSize = CELL_SIZE_DEFAULT;
}
const rows = Math.ceil(height / cellSize.height);
for (let i = 1; i < rows; ++i) {
this._terminal._core._inputHandler.lineFeed();
}
}
}

/**
* Method to add an image to the storage.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/SixelHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,21 @@ export class SixelHandler implements IDcsHandler, IResetHandler {
}

public unhook(success: boolean): boolean | Promise<boolean> {
if (this._aborted || !success || !this._dec) return true;
if (this._aborted || !success || !this._dec) {
return true;
}

const width = this._dec.width;
const height = this._dec.height;

// partial fix for https://github.com/jerch/xterm-addon-image/issues/37
if (!width || ! height) {
if (height) {
this._storage.advanceCursor(height);
}
return true;
}

const canvas = ImageRenderer.createCanvas(this._coreTerminal._core._coreBrowserService.window, width, height);
canvas.getContext('2d')?.putImageData(new ImageData(this._dec.data8, width, height), 0, 0);
if (this._dec.memoryUsage > MEM_PERMA_LIMIT) {
Expand Down
12 changes: 5 additions & 7 deletions test/ImageAddon.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const height = 600;

// eslint-disable-next-line
declare const ImageAddon: {
new(workerPath: string, options?: Partial<IImageAddonOptions>): any;
new(options?: Partial<IImageAddonOptions>): any;
};

interface ITestData {
Expand All @@ -38,8 +38,6 @@ interface IDimensions {
height: number;
}

const IMAGE_WORKER_PATH = '/workers/xterm-addon-image-worker.js';

// image: 640 x 80, 512 color
const TESTDATA: ITestData = (() => {
const pngImage = PNG.load(readFileSync('./addons/xterm-addon-image/fixture/palette.png'));
Expand Down Expand Up @@ -76,9 +74,9 @@ describe.only('ImageAddon', () => {
await page.goto(APP);
await openTerminal(page);
await page.evaluate(opts => {
(window as any).imageAddon = new ImageAddon(opts.workerPath, opts.opts);
(window as any).imageAddon = new ImageAddon(opts.opts);
(window as any).term.loadAddon((window as any).imageAddon);
}, { workerPath: IMAGE_WORKER_PATH, opts: { sixelPaletteLimit: 512 } });
}, {opts: { sixelPaletteLimit: 512 } });
});

it('test for private accessors', async () => {
Expand Down Expand Up @@ -133,9 +131,9 @@ describe.only('ImageAddon', () => {
showPlaceholder: false
};
await page.evaluate(opts => {
(window as any).imageAddonCustom = new ImageAddon(opts.workerPath, opts.opts);
(window as any).imageAddonCustom = new ImageAddon(opts.opts);
(window as any).term.loadAddon((window as any).imageAddonCustom);
}, { workerPath: IMAGE_WORKER_PATH, opts: customSettings });
}, {opts: customSettings });
assert.deepEqual(await page.evaluate(`window.imageAddonCustom._opts`), customSettings);
});
});
Expand Down
2 changes: 1 addition & 1 deletion typings/xterm-addon-image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ declare module 'xterm-addon-image' {
}

export class ImageAddon implements ITerminalAddon {
constructor(workerPath: string, options?: IImageAddonOptions);
constructor(options?: IImageAddonOptions);
public activate(terminal: Terminal): void;
public dispose(): void;

Expand Down

0 comments on commit 3489a31

Please sign in to comment.