Mark internals as private
This commit is contained in:
parent
7f3e272839
commit
ade1b705d2
@ -25,82 +25,84 @@
|
|||||||
|
|
||||||
class AnkiConnect {
|
class AnkiConnect {
|
||||||
constructor(server) {
|
constructor(server) {
|
||||||
this.server = server;
|
this._server = server;
|
||||||
this.localVersion = 2;
|
this._localVersion = 2;
|
||||||
this.remoteVersion = 0;
|
this._remoteVersion = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addNote(note) {
|
async addNote(note) {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('addNote', {note});
|
return await this._ankiInvoke('addNote', {note});
|
||||||
}
|
}
|
||||||
|
|
||||||
async canAddNotes(notes) {
|
async canAddNotes(notes) {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('canAddNotes', {notes});
|
return await this._ankiInvoke('canAddNotes', {notes});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDeckNames() {
|
async getDeckNames() {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('deckNames');
|
return await this._ankiInvoke('deckNames');
|
||||||
}
|
}
|
||||||
|
|
||||||
async getModelNames() {
|
async getModelNames() {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('modelNames');
|
return await this._ankiInvoke('modelNames');
|
||||||
}
|
}
|
||||||
|
|
||||||
async getModelFieldNames(modelName) {
|
async getModelFieldNames(modelName) {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('modelFieldNames', {modelName});
|
return await this._ankiInvoke('modelFieldNames', {modelName});
|
||||||
}
|
}
|
||||||
|
|
||||||
async guiBrowse(query) {
|
async guiBrowse(query) {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('guiBrowse', {query});
|
return await this._ankiInvoke('guiBrowse', {query});
|
||||||
}
|
}
|
||||||
|
|
||||||
async storeMediaFile(filename, dataBase64) {
|
async storeMediaFile(filename, dataBase64) {
|
||||||
await this.checkVersion();
|
await this._checkVersion();
|
||||||
return await this.ankiInvoke('storeMediaFile', {filename, data: dataBase64});
|
return await this._ankiInvoke('storeMediaFile', {filename, data: dataBase64});
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkVersion() {
|
async findNoteIds(notes) {
|
||||||
if (this.remoteVersion < this.localVersion) {
|
await this._checkVersion();
|
||||||
this.remoteVersion = await this.ankiInvoke('version');
|
const actions = notes.map((note) => ({
|
||||||
if (this.remoteVersion < this.localVersion) {
|
action: 'findNotes',
|
||||||
|
params: {
|
||||||
|
query: `deck:"${this._escapeQuery(note.deckName)}" ${this._fieldsToQuery(note.fields)}`
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
return await this._ankiInvoke('multi', {actions});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
|
||||||
|
async _checkVersion() {
|
||||||
|
if (this._remoteVersion < this._localVersion) {
|
||||||
|
this._remoteVersion = await this._ankiInvoke('version');
|
||||||
|
if (this._remoteVersion < this._localVersion) {
|
||||||
throw new Error('Extension and plugin versions incompatible');
|
throw new Error('Extension and plugin versions incompatible');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async findNoteIds(notes) {
|
_ankiInvoke(action, params) {
|
||||||
await this.checkVersion();
|
return requestJson(this._server, 'POST', {action, params, version: this._localVersion});
|
||||||
const actions = notes.map((note) => ({
|
|
||||||
action: 'findNotes',
|
|
||||||
params: {
|
|
||||||
query: `deck:"${AnkiConnect.escapeQuery(note.deckName)}" ${AnkiConnect.fieldsToQuery(note.fields)}`
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
return await this.ankiInvoke('multi', {actions});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ankiInvoke(action, params) {
|
_escapeQuery(text) {
|
||||||
return requestJson(this.server, 'POST', {action, params, version: this.localVersion});
|
|
||||||
}
|
|
||||||
|
|
||||||
static escapeQuery(text) {
|
|
||||||
return text.replace(/"/g, '');
|
return text.replace(/"/g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
static fieldsToQuery(fields) {
|
_fieldsToQuery(fields) {
|
||||||
const fieldNames = Object.keys(fields);
|
const fieldNames = Object.keys(fields);
|
||||||
if (fieldNames.length === 0) {
|
if (fieldNames.length === 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = fieldNames[0];
|
const key = fieldNames[0];
|
||||||
return `${key.toLowerCase()}:"${AnkiConnect.escapeQuery(fields[key])}"`;
|
return `${key.toLowerCase()}:"${this._escapeQuery(fields[key])}"`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user