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

260 lines
5.7 KiB
JavaScript
Raw Normal View History

// @flow
import { action, set, computed } from 'mobx';
import invariant from 'invariant';
2017-06-28 03:59:53 +00:00
import { client } from 'utils/ApiClient';
import parseTitle from 'shared/utils/parseTitle';
import unescape from 'shared/utils/unescape';
import type { NavigationNode } from 'types';
import BaseModel from 'models/BaseModel';
import Revision from 'models/Revision';
import User from 'models/User';
import Collection from 'models/Collection';
type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
2018-05-07 05:13:52 +00:00
export default class Document extends BaseModel {
isSaving: boolean;
ui: *;
store: *;
collaborators: User[];
collection: Collection;
2017-11-20 05:32:18 +00:00
collectionId: string;
lastViewedAt: ?string;
createdAt: string;
createdBy: User;
updatedAt: string;
updatedBy: User;
id: string;
team: string;
starred: boolean;
pinned: boolean;
text: string;
title: string;
emoji: string;
parentDocument: ?string;
publishedAt: ?string;
archivedAt: string;
deletedAt: ?string;
url: string;
urlId: string;
2018-05-17 06:07:33 +00:00
shareUrl: ?string;
revision: number;
constructor(data?: Object = {}, store: *) {
super(data, store);
this.updateTitle();
}
@action
updateTitle() {
set(this, parseTitle(this.text));
}
2017-11-10 22:14:30 +00:00
@computed
get modifiedSinceViewed(): boolean {
return !!this.lastViewedAt && this.lastViewedAt < this.updatedAt;
}
2017-11-10 22:14:30 +00:00
@computed
2018-07-01 16:16:38 +00:00
get pathToDocument(): NavigationNode[] {
let path;
const traveler = (nodes, previousPath) => {
nodes.forEach(childNode => {
2018-07-01 16:16:38 +00:00
const newPath = [...previousPath, childNode];
if (childNode.id === this.id) {
path = newPath;
return;
}
return traveler(childNode.children, newPath);
});
};
if (this.collection && this.collection.documents) {
traveler(this.collection.documents, []);
if (path) return path;
}
return [];
}
@computed
get isArchived(): boolean {
return !!this.archivedAt;
}
@computed
get isDeleted(): boolean {
return !!this.deletedAt;
}
2018-08-07 06:22:20 +00:00
@computed
get isDraft(): boolean {
return !this.publishedAt;
}
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
return this.text.replace(/^#/, '').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 {
return this.pathToDocument.length > 1
2017-09-24 20:51:33 +00:00
? this.pathToDocument[this.pathToDocument.length - 2].id
: null;
}
2018-05-17 06:07:33 +00:00
@action
share = async () => {
const res = await client.post('/shares.create', { documentId: this.id });
invariant(res && res.data, 'Share data should be available');
this.shareUrl = res.data.url;
return this.shareUrl;
2018-05-17 06:07:33 +00:00
};
@action
updateFromJson = data => {
set(this, data);
this.updateTitle();
};
archive = () => {
return this.store.archive(this);
};
restore = (revision: ?Revision) => {
return this.store.restore(this, revision);
};
@action
pin = async () => {
this.pinned = true;
try {
await this.store.pin(this);
} catch (err) {
this.pinned = false;
throw err;
}
};
@action
unpin = async () => {
this.pinned = false;
try {
await this.store.unpin(this);
} catch (err) {
this.pinned = true;
throw err;
}
};
2017-11-10 22:14:30 +00:00
@action
star = async () => {
this.starred = true;
try {
await this.store.star(this);
} catch (err) {
this.starred = false;
throw err;
}
};
2017-11-10 22:14:30 +00:00
@action
unstar = async () => {
this.starred = false;
try {
await this.store.unstar(this);
} catch (err) {
this.starred = true;
throw err;
}
};
2017-11-10 22:14:30 +00:00
@action
view = async () => {
await client.post('/views.create', { documentId: this.id });
};
2017-11-10 22:14:30 +00:00
@action
fetch = async () => {
const res = await client.post('/documents.info', { id: this.id });
invariant(res && res.data, 'Data should be available');
this.updateFromJson(res.data);
};
2017-11-10 22:14:30 +00:00
@action
2018-05-07 05:13:52 +00:00
save = async (options: SaveOptions) => {
if (this.isSaving) return this;
2018-07-04 20:00:53 +00:00
const isCreating = !this.id;
const wasDraft = !this.publishedAt;
this.isSaving = true;
this.updateTitle();
try {
2018-07-04 20:00:53 +00:00
if (isCreating) {
const data = {
parentDocument: undefined,
collection: this.collection.id,
title: this.title,
text: this.text,
2018-05-07 05:13:52 +00:00
...options,
};
if (this.parentDocument) {
data.parentDocument = this.parentDocument;
}
const document = await this.store.create(data);
return document;
2018-07-04 20:00:53 +00:00
} else {
const document = await this.store.update({
2018-07-04 20:00:53 +00:00
id: this.id,
title: this.title,
text: this.text,
lastRevision: this.revision,
...options,
});
return document;
}
} finally {
if (wasDraft && options.publish) {
this.store.rootStore.collections.fetch(this.collection.id, {
force: true,
});
}
this.isSaving = false;
}
2017-09-04 21:48:56 +00:00
};
move = (collectionId: string, parentDocumentId: ?string) => {
return this.store.move(this, collectionId, parentDocumentId);
};
duplicate = () => {
return this.store.duplicate(this);
};
download = async () => {
// Ensure the document is upto date with latest server contents
await this.fetch();
const blob = new Blob([unescape(this.text)], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
// Firefox support requires the anchor tag be in the DOM to trigger the dl
if (document.body) document.body.appendChild(a);
a.href = url;
a.download = `${this.title}.md`;
a.click();
};
}