mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
Pad: Simplify serialization/deserialization
This commit is contained in:
parent
addc019810
commit
79e7697c1c
1 changed files with 10 additions and 26 deletions
|
@ -21,10 +21,6 @@ const hooks = require('../../static/js/pluginfw/hooks');
|
||||||
const {padutils: {warnDeprecated}} = require('../../static/js/pad_utils');
|
const {padutils: {warnDeprecated}} = require('../../static/js/pad_utils');
|
||||||
const promises = require('../utils/promises');
|
const promises = require('../utils/promises');
|
||||||
|
|
||||||
// serialization/deserialization attributes
|
|
||||||
const attributeBlackList = ['db', 'id'];
|
|
||||||
const jsonableList = ['pool'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copied from the Etherpad source code. It converts Windows line breaks to Unix
|
* Copied from the Etherpad source code. It converts Windows line breaks to Unix
|
||||||
* line breaks and convert Tabs to spaces
|
* line breaks and convert Tabs to spaces
|
||||||
|
@ -131,22 +127,16 @@ class Pad {
|
||||||
return newRev;
|
return newRev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
const o = {...this, pool: this.pool.toJsonable()};
|
||||||
|
delete o.db;
|
||||||
|
delete o.id;
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
// save all attributes to the database
|
// save all attributes to the database
|
||||||
async saveToDatabase() {
|
async saveToDatabase() {
|
||||||
const dbObject = {};
|
await this.db.set(`pad:${this.id}`, this);
|
||||||
|
|
||||||
for (const attr in this) {
|
|
||||||
if (typeof this[attr] === 'function') continue;
|
|
||||||
if (attributeBlackList.indexOf(attr) !== -1) continue;
|
|
||||||
|
|
||||||
dbObject[attr] = this[attr];
|
|
||||||
|
|
||||||
if (jsonableList.indexOf(attr) !== -1) {
|
|
||||||
dbObject[attr] = dbObject[attr].toJsonable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.db.set(`pad:${this.id}`, dbObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get time of last edit (changeset application)
|
// get time of last edit (changeset application)
|
||||||
|
@ -379,14 +369,8 @@ class Pad {
|
||||||
|
|
||||||
// if this pad exists, load it
|
// if this pad exists, load it
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
// copy all attr. To a transfrom via fromJsonable if necassary
|
Object.assign(this, value);
|
||||||
for (const attr in value) {
|
if ('pool' in value) this.pool = new AttributePool().fromJsonable(value.pool);
|
||||||
if (jsonableList.indexOf(attr) !== -1) {
|
|
||||||
this[attr] = this[attr].fromJsonable(value[attr]);
|
|
||||||
} else {
|
|
||||||
this[attr] = value[attr];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
const context = {pad: this, authorId, type: 'text', content: settings.defaultPadText};
|
const context = {pad: this, authorId, type: 'text', content: settings.defaultPadText};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue