Explore: Add history to query fields

- queries are saved to localstorage history array
- one history per datasource type (plugin ID)
- 100 items kept with timestamps
- history suggestions can be pulled up with Ctrl-SPACE
This commit is contained in:
David Kaltschmidt
2018-08-06 14:45:03 +02:00
parent dc60828407
commit eaff7b0f68
6 changed files with 142 additions and 12 deletions
+12
View File
@@ -32,6 +32,18 @@ describe('store', () => {
expect(store.getBool('key5', false)).toBe(true);
});
it('gets an object', () => {
expect(store.getObject('object1')).toBeUndefined();
expect(store.getObject('object1', [])).toEqual([]);
store.setObject('object1', [1]);
expect(store.getObject('object1')).toEqual([1]);
});
it('sets an object', () => {
expect(store.setObject('object2', { a: 1 })).toBe(true);
expect(store.getObject('object2')).toEqual({ a: 1 });
});
it('key should be deleted', () => {
store.set('key6', '123');
store.delete('key6');
+32
View File
@@ -14,6 +14,38 @@ export class Store {
return window.localStorage[key] === 'true';
}
getObject(key: string, def?: any) {
let ret = def;
if (this.exists(key)) {
const json = window.localStorage[key];
try {
ret = JSON.parse(json);
} catch (error) {
console.error(`Error parsing store object: ${key}. Returning default: ${def}. [${error}]`);
}
}
return ret;
}
// Returns true when successfully stored
setObject(key: string, value: any): boolean {
let json;
try {
json = JSON.stringify(value);
} catch (error) {
console.error(`Could not stringify object: ${key}. [${error}]`);
return false;
}
try {
this.set(key, json);
} catch (error) {
// Likely hitting storage quota
console.error(`Could not save item in localStorage: ${key}. [${error}]`);
return false;
}
return true;
}
exists(key) {
return window.localStorage[key] !== void 0;
}