anki-apkg-generator
Version:
generate anki's apkg file by code
111 lines (89 loc) • 3.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class Note {
constructor(model) {
_defineProperty(this, "id", void 0);
_defineProperty(this, "model", void 0);
_defineProperty(this, "fieldsValue", void 0);
_defineProperty(this, "tags", void 0);
_defineProperty(this, "name", '');
this.id = Date.now();
this.fieldsValue = [];
this.tags = [];
this.model = model;
}
setName(name) {
this.name = name;
return this;
}
setId(id) {
this.id = id;
return this;
}
setFieldsValue() {
var fieldsValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.fieldsValue = fieldsValue;
return this;
}
setTags() {
var tags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
this.tags = tags;
return this;
} // private _fixDeprecatedBuiltinModelsAndWarn () {
// if (
// this.model.kind === ModelKinds.Close &&
// this.fieldsValue.length === 1
// ) {
// return [...this.fieldsValue, '']
// }
// return this.fieldsValue
// }
_checkNumberModelFieldsMatchesNumFields() {
//
if (this.model.fields.length !== this.fieldsValue.length) {
throw new Error("\n length of fields in Model does not match length of fieldsValue in Note: ".concat(this.model.name, " has ").concat(this.model.fields.length, " fields, but note has ").concat(this.fieldsValue.length, " fieldsValue\n "));
}
if (this.model.fields.length === 0) {
throw new Error('fields can not be empty');
}
} // checkInvalidHtmlTagsInFields () {
// if (this.fieldsValue.some(value => {
// }))
// }
writeToDatabase(db, deckId) {
// this.fieldsValue = this._fixDeprecatedBuiltinModelsAndWarn()
this._checkNumberModelFieldsMatchesNumFields(); // this.checkInvalidHtmlTagsInFields()
var timestamp = Date.now();
var noteGuid = db.generateGuid(deckId, this.name);
var id = db.getNoteId(noteGuid, this.id);
db.update('insert or replace into notes values(:id,:guid,:mid,:mod,:usn,:tags,:flds,:sfld,:csum,:flags,:data)', {
':id': id,
// integer primary key,
':guid': noteGuid,
// text not null,
':mid': this.model.id,
// integer not null,
':mod': db.getId('notes', 'mod', timestamp),
// integer not null,
':usn': -1,
// integer not null,
':tags': " ".concat(this.tags.join(' '), " "),
// text not null,
':flds': this.fieldsValue.join('\u001F'),
// text not null, fields
':sfld': this.fieldsValue[0],
// integer not null, sortField
':csum': 0,
//integer not null,
':flags': 0,
// integer not null,
':data': '' // text not null,
});
this.model.card.writeToDatabase(db, deckId, id, timestamp);
}
}
exports.default = Note;