2017-06-26 05:48:50 +00:00
|
|
|
// @flow
|
|
|
|
import { extendObservable, action, runInAction, computed } from 'mobx';
|
|
|
|
import invariant from 'invariant';
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
2017-06-26 05:48:50 +00:00
|
|
|
import stores from 'stores';
|
|
|
|
import ErrorsStore from 'stores/ErrorsStore';
|
2017-11-01 04:59:14 +00:00
|
|
|
import parseTitle from '../../shared/utils/parseTitle';
|
2017-06-26 05:48:50 +00:00
|
|
|
|
|
|
|
import type { User } from 'types';
|
2017-08-03 13:48:07 +00:00
|
|
|
import BaseModel from './BaseModel';
|
2017-06-26 05:48:50 +00:00
|
|
|
import Collection from './Collection';
|
|
|
|
|
2018-05-08 05:08:47 +00:00
|
|
|
type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
|
2018-05-07 05:13:52 +00:00
|
|
|
|
2017-08-03 13:48:07 +00:00
|
|
|
class Document extends BaseModel {
|
2017-07-05 06:02:06 +00:00
|
|
|
isSaving: boolean = false;
|
2017-06-29 04:46:00 +00:00
|
|
|
hasPendingChanges: boolean = false;
|
|
|
|
errors: ErrorsStore;
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
collaborators: User[];
|
2017-07-09 05:12:14 +00:00
|
|
|
collection: $Shape<Collection>;
|
2017-11-20 05:32:18 +00:00
|
|
|
collectionId: string;
|
2017-07-16 16:24:45 +00:00
|
|
|
firstViewedAt: ?string;
|
|
|
|
lastViewedAt: ?string;
|
|
|
|
modifiedSinceViewed: ?boolean;
|
2017-06-26 05:48:50 +00:00
|
|
|
createdAt: string;
|
|
|
|
createdBy: User;
|
2018-03-01 07:28:36 +00:00
|
|
|
updatedAt: string;
|
|
|
|
updatedBy: User;
|
2017-06-26 05:48:50 +00:00
|
|
|
html: string;
|
|
|
|
id: string;
|
|
|
|
team: string;
|
2017-07-29 23:15:04 +00:00
|
|
|
emoji: string;
|
2017-07-05 06:02:06 +00:00
|
|
|
private: boolean = false;
|
|
|
|
starred: boolean = false;
|
2018-03-01 07:28:36 +00:00
|
|
|
pinned: boolean = false;
|
2017-07-05 06:02:06 +00:00
|
|
|
text: string = '';
|
2017-07-19 06:29:20 +00:00
|
|
|
title: string = '';
|
2017-09-19 07:13:49 +00:00
|
|
|
parentDocument: ?string;
|
2018-02-28 06:41:12 +00:00
|
|
|
publishedAt: ?string;
|
2017-06-26 05:48:50 +00:00
|
|
|
url: string;
|
2017-06-28 03:36:09 +00:00
|
|
|
views: number;
|
2017-11-25 21:04:54 +00:00
|
|
|
revision: number;
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2017-07-16 18:47:48 +00:00
|
|
|
data: Object;
|
|
|
|
|
2017-06-26 05:48:50 +00:00
|
|
|
/* Computed */
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get modifiedSinceViewed(): boolean {
|
2017-07-16 16:24:45 +00:00
|
|
|
return !!this.lastViewedAt && this.lastViewedAt < this.updatedAt;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get pathToDocument(): Array<{ id: string, title: string }> {
|
2017-06-26 05:48:50 +00:00
|
|
|
let path;
|
|
|
|
const traveler = (nodes, previousPath) => {
|
|
|
|
nodes.forEach(childNode => {
|
2017-09-04 21:48:56 +00:00
|
|
|
const newPath = [
|
|
|
|
...previousPath,
|
|
|
|
{
|
|
|
|
id: childNode.id,
|
|
|
|
title: childNode.title,
|
|
|
|
},
|
|
|
|
];
|
2017-06-26 05:48:50 +00:00
|
|
|
if (childNode.id === this.id) {
|
|
|
|
path = newPath;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return traveler(childNode.children, newPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.collection.documents) {
|
|
|
|
traveler(this.collection.documents, []);
|
2018-02-28 06:41:12 +00:00
|
|
|
if (path) return path;
|
2017-06-26 05:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get isEmpty(): boolean {
|
2017-07-19 06:29:20 +00:00
|
|
|
// Check if the document title has been modified and user generated content exists
|
2017-07-24 02:11:48 +00:00
|
|
|
return this.text.replace(new RegExp(`^#$`), '').trim().length === 0;
|
2017-07-19 06:45:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get allowSave(): boolean {
|
2017-07-19 06:45:01 +00:00
|
|
|
return !this.isEmpty && !this.isSaving;
|
2017-07-19 06:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get parentDocumentId(): ?string {
|
2017-09-19 07:13:49 +00:00
|
|
|
return this.pathToDocument.length > 1
|
2017-09-24 20:51:33 +00:00
|
|
|
? this.pathToDocument[this.pathToDocument.length - 2].id
|
2017-09-19 07:13:49 +00:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2017-06-26 05:48:50 +00:00
|
|
|
/* Actions */
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
@action
|
|
|
|
pin = async () => {
|
|
|
|
this.pinned = true;
|
|
|
|
try {
|
|
|
|
await client.post('/documents.pin', { id: this.id });
|
|
|
|
} catch (e) {
|
|
|
|
this.pinned = false;
|
|
|
|
this.errors.add('Document failed to pin');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
unpin = async () => {
|
|
|
|
this.pinned = false;
|
|
|
|
try {
|
|
|
|
await client.post('/documents.unpin', { id: this.id });
|
|
|
|
} catch (e) {
|
|
|
|
this.pinned = true;
|
|
|
|
this.errors.add('Document failed to unpin');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
star = async () => {
|
2017-06-29 04:46:00 +00:00
|
|
|
this.starred = true;
|
|
|
|
try {
|
|
|
|
await client.post('/documents.star', { id: this.id });
|
|
|
|
} catch (e) {
|
|
|
|
this.starred = false;
|
|
|
|
this.errors.add('Document failed star');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
unstar = async () => {
|
2017-06-29 04:46:00 +00:00
|
|
|
this.starred = false;
|
|
|
|
try {
|
|
|
|
await client.post('/documents.unstar', { id: this.id });
|
|
|
|
} catch (e) {
|
|
|
|
this.starred = false;
|
|
|
|
this.errors.add('Document failed unstar');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
view = async () => {
|
2017-07-05 06:02:06 +00:00
|
|
|
this.views++;
|
2017-06-29 04:46:00 +00:00
|
|
|
try {
|
|
|
|
await client.post('/views.create', { id: this.id });
|
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Document failed to record view');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
fetch = async () => {
|
2017-06-26 05:48:50 +00:00
|
|
|
try {
|
2017-06-28 03:59:53 +00:00
|
|
|
const res = await client.post('/documents.info', { id: this.id });
|
2017-06-26 05:48:50 +00:00
|
|
|
invariant(res && res.data, 'Document API response should be available');
|
|
|
|
const { data } = res;
|
|
|
|
runInAction('Document#update', () => {
|
|
|
|
this.updateData(data);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Document failed loading');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-05-07 05:13:52 +00:00
|
|
|
save = async (options: SaveOptions) => {
|
2017-07-09 05:12:14 +00:00
|
|
|
if (this.isSaving) return this;
|
2018-05-08 05:53:13 +00:00
|
|
|
|
|
|
|
const wasDraft = !this.publishedAt;
|
2017-06-29 04:46:00 +00:00
|
|
|
this.isSaving = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let res;
|
|
|
|
if (this.id) {
|
|
|
|
res = await client.post('/documents.update', {
|
|
|
|
id: this.id,
|
|
|
|
title: this.title,
|
|
|
|
text: this.text,
|
2017-11-25 21:04:54 +00:00
|
|
|
lastRevision: this.revision,
|
2018-05-07 05:13:52 +00:00
|
|
|
...options,
|
2017-06-29 04:46:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-07-09 05:12:14 +00:00
|
|
|
const data = {
|
|
|
|
parentDocument: undefined,
|
2017-06-29 04:46:00 +00:00
|
|
|
collection: this.collection.id,
|
|
|
|
title: this.title,
|
|
|
|
text: this.text,
|
2018-05-07 05:13:52 +00:00
|
|
|
...options,
|
2017-07-09 05:12:14 +00:00
|
|
|
};
|
2017-09-19 07:13:49 +00:00
|
|
|
if (this.parentDocument) {
|
|
|
|
data.parentDocument = this.parentDocument;
|
|
|
|
}
|
2017-07-09 05:12:14 +00:00
|
|
|
res = await client.post('/documents.create', data);
|
2017-12-28 12:49:34 +00:00
|
|
|
if (res && res.data) this.emit('documents.create', res.data);
|
2017-06-29 04:46:00 +00:00
|
|
|
}
|
2017-08-29 15:37:17 +00:00
|
|
|
runInAction('Document#save', () => {
|
2017-12-28 12:49:34 +00:00
|
|
|
invariant(res && res.data, 'Data should be available');
|
2017-08-29 15:37:17 +00:00
|
|
|
this.updateData(res.data);
|
|
|
|
this.hasPendingChanges = false;
|
2017-07-05 06:02:06 +00:00
|
|
|
});
|
2017-09-13 02:54:22 +00:00
|
|
|
|
2018-05-08 05:08:47 +00:00
|
|
|
this.emit('documents.update', {
|
|
|
|
document: this,
|
|
|
|
collectionId: this.collection.id,
|
2017-09-13 02:54:22 +00:00
|
|
|
});
|
2018-05-08 05:53:13 +00:00
|
|
|
|
|
|
|
if (wasDraft && this.publishedAt) {
|
|
|
|
this.emit('documents.publish', {
|
|
|
|
id: this.id,
|
|
|
|
collectionId: this.collection.id,
|
|
|
|
});
|
|
|
|
}
|
2017-06-29 04:46:00 +00:00
|
|
|
} catch (e) {
|
2018-05-08 05:53:13 +00:00
|
|
|
this.errors.add('Document failed to save');
|
2017-06-29 04:46:00 +00:00
|
|
|
} finally {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
2017-07-05 06:02:06 +00:00
|
|
|
|
|
|
|
return this;
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
move = async (parentDocumentId: ?string) => {
|
2017-09-04 21:48:56 +00:00
|
|
|
try {
|
|
|
|
const res = await client.post('/documents.move', {
|
|
|
|
id: this.id,
|
|
|
|
parentDocument: parentDocumentId,
|
|
|
|
});
|
2017-09-13 06:30:18 +00:00
|
|
|
invariant(res && res.data, 'Data not available');
|
2017-09-04 21:48:56 +00:00
|
|
|
this.updateData(res.data);
|
|
|
|
this.emit('documents.move', {
|
|
|
|
id: this.id,
|
|
|
|
collectionId: this.collection.id,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Error while moving the document');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
2017-06-29 04:46:00 +00:00
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
delete = async () => {
|
2017-08-03 13:48:07 +00:00
|
|
|
try {
|
|
|
|
await client.post('/documents.delete', { id: this.id });
|
2017-08-29 06:50:45 +00:00
|
|
|
this.emit('documents.delete', {
|
2017-08-03 13:48:07 +00:00
|
|
|
id: this.id,
|
|
|
|
collectionId: this.collection.id,
|
|
|
|
});
|
2017-09-12 06:41:12 +00:00
|
|
|
return true;
|
2017-08-03 13:48:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Error while deleting the document');
|
|
|
|
}
|
2017-09-12 06:41:12 +00:00
|
|
|
return false;
|
2017-08-03 13:48:07 +00:00
|
|
|
};
|
|
|
|
|
2017-09-09 23:55:02 +00:00
|
|
|
download() {
|
|
|
|
const a = window.document.createElement('a');
|
|
|
|
a.textContent = 'download';
|
|
|
|
a.download = `${this.title}.md`;
|
2017-11-10 22:14:30 +00:00
|
|
|
a.href = `data:text/markdown;charset=UTF-8,${encodeURIComponent(
|
|
|
|
this.text
|
|
|
|
)}`;
|
2017-09-09 23:55:02 +00:00
|
|
|
a.click();
|
|
|
|
}
|
|
|
|
|
2017-07-10 03:02:10 +00:00
|
|
|
updateData(data: Object = {}, dirty: boolean = false) {
|
2017-07-29 23:15:04 +00:00
|
|
|
if (data.text) {
|
|
|
|
const { title, emoji } = parseTitle(data.text);
|
|
|
|
data.title = title;
|
|
|
|
data.emoji = emoji;
|
|
|
|
}
|
2017-07-16 18:47:48 +00:00
|
|
|
if (dirty) this.hasPendingChanges = true;
|
|
|
|
this.data = data;
|
2017-06-26 05:48:50 +00:00
|
|
|
extendObservable(this, data);
|
|
|
|
}
|
|
|
|
|
2017-07-09 17:27:29 +00:00
|
|
|
constructor(data?: Object = {}) {
|
2017-08-03 13:48:07 +00:00
|
|
|
super();
|
|
|
|
|
2017-07-09 17:27:29 +00:00
|
|
|
this.updateData(data);
|
2017-06-26 05:48:50 +00:00
|
|
|
this.errors = stores.errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Document;
|