mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) move client code to core
Summary: This moves all client code to core, and makes minimal fix-ups to get grist and grist-core to compile correctly. The client works in core, but I'm leaving clean-up around the build and bundles to follow-up. Test Plan: existing tests pass; server-dev bundle looks sane Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2627
This commit is contained in:
50
app/client/ui/transientInput.ts
Normal file
50
app/client/ui/transientInput.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* This is a temporary <input> element. The intended usage is to create is when needed (e.g. when
|
||||
* some "rename" option is chosen), and provide methods to save and to close.
|
||||
*
|
||||
* It calls save() on Enter and on blur, which should return a Promise. On successful save, and on
|
||||
* Escape, it calls close(), which should destroy the <input>.
|
||||
*/
|
||||
|
||||
import {reportError} from 'app/client/models/AppModel';
|
||||
import {dom, DomArg} from 'grainjs';
|
||||
|
||||
export interface ITransientInputOptions {
|
||||
initialValue: string;
|
||||
save(value: string): Promise<void>|any;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
export function transientInput({initialValue, save, close}: ITransientInputOptions,
|
||||
...args: Array<DomArg<HTMLInputElement>>) {
|
||||
let lastSave: string = initialValue;
|
||||
|
||||
async function onSave(explicitSave: boolean) {
|
||||
try {
|
||||
if (explicitSave || input.value !== lastSave) {
|
||||
lastSave = input.value;
|
||||
await save(input.value);
|
||||
}
|
||||
close();
|
||||
} catch (err) {
|
||||
reportError(err);
|
||||
delayedFocus();
|
||||
}
|
||||
}
|
||||
|
||||
function delayedFocus() {
|
||||
setTimeout(() => { input.focus(); input.select(); }, 10);
|
||||
}
|
||||
|
||||
const input = dom('input', {type: 'text', placeholder: 'Enter name'},
|
||||
dom.prop('value', initialValue),
|
||||
dom.on('blur', () => onSave(false)),
|
||||
dom.onKeyDown({
|
||||
Enter: () => onSave(true),
|
||||
Escape: () => close(),
|
||||
}),
|
||||
...args,
|
||||
);
|
||||
delayedFocus();
|
||||
return input;
|
||||
}
|
||||
Reference in New Issue
Block a user