2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* NewBaseEditor is equivalent to BaseEditor for outside code, but is in typescript, and
|
|
|
|
* so is friendlier and clearer to derive TypeScript classes from.
|
|
|
|
*/
|
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {CellValue} from "app/common/DocActions";
|
|
|
|
import {Disposable, IDisposableOwner, Observable} from 'grainjs';
|
|
|
|
|
2021-02-04 03:17:17 +00:00
|
|
|
export interface IEditorCommandGroup {
|
|
|
|
fieldEditCancel: () => void;
|
|
|
|
fieldEditSaveHere: () => void;
|
|
|
|
[cmd: string]: () => void;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export interface Options {
|
|
|
|
gristDoc: GristDoc;
|
|
|
|
field: ViewFieldRec;
|
|
|
|
cellValue: CellValue;
|
|
|
|
formulaError?: Observable<CellValue>;
|
|
|
|
editValue?: string;
|
|
|
|
cursorPos: number;
|
2021-02-04 03:17:17 +00:00
|
|
|
commands: IEditorCommandGroup;
|
2021-05-17 14:05:49 +00:00
|
|
|
state? : any;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Required parameters:
|
|
|
|
* @param {RowModel} options.field: ViewSectionField (i.e. column) being edited.
|
|
|
|
* @param {String} options.cellValue: The value in the underlying cell being edited.
|
|
|
|
* @param {String} options.editValue: String to be edited, or undefined to use cellValue.
|
|
|
|
* @param {Number} options.cursorPos: The initial position where to place the cursor.
|
|
|
|
* @param {Object} options.commands: Object mapping command names to functions, to enable as part
|
|
|
|
* of the command group that should be activated while the editor exists.
|
|
|
|
*/
|
|
|
|
export abstract class NewBaseEditor extends Disposable {
|
|
|
|
/**
|
|
|
|
* Override the create() method to allow the parameters of create() expected by old-style
|
|
|
|
* Editors and provided by FieldBuilder. TODO: remove this method once all editors have been
|
|
|
|
* updated to new-style Disposables.
|
|
|
|
*/
|
2021-03-17 03:45:44 +00:00
|
|
|
public static create<Opt extends Options>(owner: IDisposableOwner|null, options: Opt): NewBaseEditor;
|
2020-10-02 15:10:00 +00:00
|
|
|
public static create(options: Options): NewBaseEditor;
|
|
|
|
public static create(ownerOrOptions: any, options?: any): NewBaseEditor {
|
|
|
|
return options ?
|
|
|
|
Disposable.create.call(this as any, ownerOrOptions, options) :
|
|
|
|
Disposable.create.call(this as any, null, ownerOrOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the typed-in value should change the cell without opening the editor, and if so,
|
|
|
|
* returns the value to save. E.g. on typing " ", CheckBoxEditor toggles value without opening.
|
|
|
|
*/
|
|
|
|
public static skipEditor(typedVal: string|undefined, origVal: CellValue): CellValue|undefined {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-05-17 14:05:49 +00:00
|
|
|
/**
|
|
|
|
* Current state of the editor. Optional, not all editors will report theirs current state.
|
|
|
|
*/
|
|
|
|
public editorState? : Observable<any>;
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
constructor(protected options: Options) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after the editor is instantiated to attach its DOM to the page.
|
2021-02-04 03:17:17 +00:00
|
|
|
* - cellElem: The element representing the cell that this editor should match
|
2020-10-02 15:10:00 +00:00
|
|
|
* in size and position. Used by derived classes, e.g. to construct an EditorPlacement object.
|
|
|
|
*/
|
2021-02-04 03:17:17 +00:00
|
|
|
public abstract attach(cellElem: Element): void;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
/**
|
|
|
|
* Returns DOM container with the editor, typically present and attached after attach() has been
|
|
|
|
* called.
|
|
|
|
*/
|
|
|
|
public getDom(): HTMLElement|null { return null; }
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Called to get the value to save back to the cell.
|
|
|
|
*/
|
|
|
|
public abstract getCellValue(): CellValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used if an editor needs preform any actions before a save
|
|
|
|
*/
|
|
|
|
public prepForSave(): void | Promise<void> {
|
|
|
|
// No-op by default.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to get the text in the editor, used when switching between editing data and formula.
|
|
|
|
*/
|
|
|
|
public abstract getTextValue(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to get the position of the cursor in the editor. Used when switching between editing
|
|
|
|
* data and formula.
|
|
|
|
*/
|
|
|
|
public abstract getCursorPos(): number;
|
|
|
|
}
|