This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/models/Document.js

259 lines
6.3 KiB
JavaScript
Raw Normal View History

// @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';
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
2017-11-01 04:59:14 +00:00
import parseTitle from '../../shared/utils/parseTitle';
import type { User } from 'types';
import BaseModel from './BaseModel';
import Collection from './Collection';
2017-07-19 06:29:20 +00:00
const DEFAULT_TITLE = 'Untitled document';
class Document extends BaseModel {
isSaving: boolean = false;
hasPendingChanges: boolean = false;
errors: ErrorsStore;
collaborators: Array<User>;
collection: $Shape<Collection>;
firstViewedAt: ?string;
lastViewedAt: ?string;
modifiedSinceViewed: ?boolean;
createdAt: string;
createdBy: User;
html: string;
id: string;
team: string;
emoji: string;
private: boolean = false;
starred: boolean = false;
text: string = '';
2017-07-19 06:29:20 +00:00
title: string = '';
parentDocument: ?string;
updatedAt: string;
updatedBy: User;
url: string;
2017-06-28 03:36:09 +00:00
views: number;
2017-07-16 18:47:48 +00:00
data: Object;
/* Computed */
@computed get modifiedSinceViewed(): boolean {
return !!this.lastViewedAt && this.lastViewedAt < this.updatedAt;
}
2017-09-04 21:48:56 +00:00
@computed get pathToDocument(): Array<{ id: string, title: string }> {
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,
},
];
if (childNode.id === this.id) {
path = newPath;
return;
} else {
return traveler(childNode.children, newPath);
}
});
};
if (this.collection.documents) {
traveler(this.collection.documents, []);
invariant(path, 'Path is not available for collection, abort');
return path;
}
return [];
}
2017-07-19 06:45:01 +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
}
@computed get allowSave(): boolean {
return !this.isEmpty && !this.isSaving;
2017-07-19 06:29:20 +00:00
}
@computed get allowDelete(): boolean {
const collection = this.collection;
return (
collection &&
collection.type === 'atlas' &&
collection.documents &&
collection.documents.length > 1
);
}
@computed get parentDocumentId(): ?string {
return this.pathToDocument.length > 1
2017-09-24 20:51:33 +00:00
? this.pathToDocument[this.pathToDocument.length - 2].id
: null;
}
/* Actions */
@action star = async () => {
this.starred = true;
try {
await client.post('/documents.star', { id: this.id });
} catch (e) {
this.starred = false;
this.errors.add('Document failed star');
}
};
@action unstar = async () => {
this.starred = false;
try {
await client.post('/documents.unstar', { id: this.id });
} catch (e) {
this.starred = false;
this.errors.add('Document failed unstar');
}
};
@action view = async () => {
this.views++;
try {
await client.post('/views.create', { id: this.id });
} catch (e) {
this.errors.add('Document failed to record view');
}
};
@action fetch = async () => {
try {
2017-06-28 03:59:53 +00:00
const res = await client.post('/documents.info', { id: this.id });
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');
}
};
@action save = async () => {
if (this.isSaving) return this;
this.isSaving = true;
try {
let res;
if (this.id) {
res = await client.post('/documents.update', {
id: this.id,
title: this.title,
text: this.text,
});
} else {
2017-07-19 06:29:20 +00:00
if (!this.title) {
this.title = DEFAULT_TITLE;
this.text = this.text.replace(
2017-07-24 02:11:48 +00:00
new RegExp(`^# `),
2017-07-19 06:29:20 +00:00
`# ${DEFAULT_TITLE}`
);
}
const data = {
parentDocument: undefined,
collection: this.collection.id,
title: this.title,
text: this.text,
};
if (this.parentDocument) {
data.parentDocument = this.parentDocument;
}
res = await client.post('/documents.create', data);
}
runInAction('Document#save', () => {
invariant(res && res.data, 'Data should be available');
this.updateData(res.data);
this.hasPendingChanges = false;
});
this.emit('collections.update', {
id: this.collection.id,
collection: this.collection,
});
} catch (e) {
this.errors.add('Document failed saving');
} finally {
this.isSaving = false;
}
return this;
};
2017-09-04 21:48:56 +00:00
@action move = async (parentDocumentId: ?string) => {
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;
};
@action delete = async () => {
try {
await client.post('/documents.delete', { id: this.id });
2017-08-29 06:50:45 +00:00
this.emit('documents.delete', {
id: this.id,
collectionId: this.collection.id,
});
2017-09-12 06:41:12 +00:00
return true;
} catch (e) {
this.errors.add('Error while deleting the document');
}
2017-09-12 06:41:12 +00:00
return false;
};
download() {
const a = window.document.createElement('a');
a.textContent = 'download';
a.download = `${this.title}.md`;
a.href = `data:text/markdown;charset=UTF-8,${encodeURIComponent(this.text)}`;
a.click();
}
2017-07-10 03:02:10 +00:00
updateData(data: Object = {}, dirty: boolean = false) {
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;
extendObservable(this, data);
}
2017-07-09 17:27:29 +00:00
constructor(data?: Object = {}) {
super();
2017-07-09 17:27:29 +00:00
this.updateData(data);
this.errors = stores.errors;
}
}
export default Document;