1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-13 00:10:37 +02:00

Desktop: Resolves #8080: Add support for plugin user data (#8312)

This commit is contained in:
Laurent Cozic
2023-06-13 18:06:16 +01:00
committed by GitHub
parent 6424832984
commit 92c68882e2
67 changed files with 13357 additions and 232 deletions

View File

@ -41,7 +41,7 @@ export default class Joplin {
public constructor(implementation: BasePlatformImplementation, plugin: Plugin, store: any) {
this.implementation_ = implementation;
this.data_ = new JoplinData();
this.data_ = new JoplinData(plugin);
this.plugins_ = new JoplinPlugins(plugin);
this.workspace_ = new JoplinWorkspace(store);
this.filters_ = new JoplinFilters();

View File

@ -1,7 +1,9 @@
import { ModelType } from '../../../BaseModel';
import BaseItem from '../../../models/BaseItem';
import Resource from '../../../models/Resource';
import { getItemUserData, setItemUserData, deleteItemUserData } from '../../../models/utils/userData';
import Api from '../../rest/Api';
import Plugin from '../Plugin';
import { Path } from './types';
/**
@ -44,6 +46,11 @@ export default class JoplinData {
private api_: any = new Api();
private pathSegmentRegex_: RegExp;
private plugin: Plugin;
public constructor(plugin: Plugin) {
this.plugin = plugin;
}
private serializeApiBody(body: any) {
if (typeof body !== 'string') { return JSON.stringify(body); }
@ -94,4 +101,33 @@ export default class JoplinData {
return Resource.fullPath(item);
}
/**
* Gets an item user data. User data are key/value pairs. The `key` can be any
* arbitrary string, while the `value` can be of any type supported by
* [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description)
*
* User data is synchronised across devices, and each value wil be merged based on their timestamp:
*
* - If value is modified by client 1, then modified by client 2, it will take the value from client 2
* - If value is modified by client 1, then deleted by client 2, the value will be deleted after merge
* - If value is deleted by client 1, then updated by client 2, the value will be restored and set to the value from client 2 after merge
*/
public async userDataGet<T>(itemType: ModelType, itemId: string, key: string) {
return getItemUserData<T>(itemType, itemId, this.plugin.id, key);
}
/**
* Sets a note user data. See {@link JoplinData.userDataGet} for more details.
*/
public async userDataSet<T>(itemType: ModelType, itemId: string, key: string, value: T) {
await setItemUserData<T>(itemType, itemId, this.plugin.id, key, value);
}
/**
* Deletes a note user data. See {@link JoplinData.userDataGet} for more details.
*/
public async userDataDelete(itemType: ModelType, itemId: string, key: string) {
await deleteItemUserData(itemType, itemId, this.plugin.id, key);
}
}