mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
Allow modifying constructor only options
This commit is contained in:
@@ -86,7 +86,7 @@ export abstract class CoreTerminal extends Disposable implements ICoreTerminal {
|
||||
public get cols(): number { return this._bufferService.cols; }
|
||||
public get rows(): number { return this._bufferService.rows; }
|
||||
public get buffers(): IBufferSet { return this._bufferService.buffers; }
|
||||
public get options(): ITerminalOptions { return this.optionsService.options; }
|
||||
public get options(): ITerminalOptions { return this.optionsService.publicOptions; }
|
||||
|
||||
constructor(
|
||||
options: Partial<ITerminalOptions>
|
||||
|
||||
@@ -121,11 +121,13 @@ export class MockLogService implements ILogService {
|
||||
export class MockOptionsService implements IOptionsService {
|
||||
public serviceBrand: any;
|
||||
public options: ITerminalOptions = clone(DEFAULT_OPTIONS);
|
||||
public publicOptions: ITerminalOptions = clone(DEFAULT_OPTIONS);
|
||||
public onOptionChange: IEvent<string> = new EventEmitter<string>().event;
|
||||
constructor(testOptions?: Partial<ITerminalOptions>) {
|
||||
if (testOptions) {
|
||||
for (const key of Object.keys(testOptions)) {
|
||||
this.options[key] = testOptions[key];
|
||||
this.publicOptions[key] = testOptions[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ export class OptionsService implements IOptionsService {
|
||||
public get onOptionChange(): IEvent<string> { return this._onOptionChange.event; }
|
||||
|
||||
constructor(options: Partial<ITerminalOptions>) {
|
||||
this.options = { ... DEFAULT_OPTIONS };
|
||||
this.options = { ...DEFAULT_OPTIONS };
|
||||
for (const key in options) {
|
||||
if (key in this.options) {
|
||||
try {
|
||||
@@ -88,6 +88,7 @@ export class OptionsService implements IOptionsService {
|
||||
for (const propName in this.options) {
|
||||
const privatePropName = `_${propName}`;
|
||||
this._options[privatePropName] = this.options[propName];
|
||||
|
||||
Object.defineProperty(this.options, propName, {
|
||||
get: () => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
@@ -99,6 +100,35 @@ export class OptionsService implements IOptionsService {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error('No option with key "' + propName + '"');
|
||||
}
|
||||
|
||||
value = this._sanitizeAndValidateOption(propName, value);
|
||||
// Don't fire an option change event if they didn't change
|
||||
if (this._options[privatePropName] !== value) {
|
||||
this._options[privatePropName] = value;
|
||||
this._onOptionChange.fire(propName);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public get publicOptions(): ITerminalOptions {
|
||||
const publicOptions = { ... this.options };
|
||||
for (const propName in CONSTRUCTOR_ONLY_OPTIONS) {
|
||||
const privatePropName = `_${propName}`;
|
||||
Object.defineProperty(publicOptions, propName, {
|
||||
get: () => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error(`No option with key "${propName}"`);
|
||||
}
|
||||
return this._options[privatePropName];
|
||||
},
|
||||
set: (value: any) => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error('No option with key "' + propName + '"');
|
||||
}
|
||||
// Throw an error if any constructor only option is modified
|
||||
// from terminal.options
|
||||
if (CONSTRUCTOR_ONLY_OPTIONS.includes(propName)) {
|
||||
throw new Error(`Option "${propName}" can only be set in the constructor`);
|
||||
}
|
||||
@@ -112,6 +142,7 @@ export class OptionsService implements IOptionsService {
|
||||
}
|
||||
});
|
||||
}
|
||||
return publicOptions;
|
||||
}
|
||||
|
||||
public setOption(key: string, value: any): void {
|
||||
@@ -138,7 +169,7 @@ export class OptionsService implements IOptionsService {
|
||||
break;
|
||||
case 'cursorWidth':
|
||||
value = Math.floor(value);
|
||||
// Fall through for bounds check
|
||||
// Fall through for bounds check
|
||||
case 'lineHeight':
|
||||
case 'tabStopWidth':
|
||||
if (value < 1) {
|
||||
|
||||
@@ -181,6 +181,7 @@ export interface IOptionsService {
|
||||
serviceBrand: undefined;
|
||||
|
||||
readonly options: ITerminalOptions;
|
||||
readonly publicOptions: ITerminalOptions;
|
||||
|
||||
readonly onOptionChange: IEvent<string>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user