2017-06-26 05:48:50 +00:00
|
|
|
// @flow
|
2019-01-09 06:49:20 +00:00
|
|
|
import { action, set, computed } from 'mobx';
|
2019-11-19 02:51:32 +00:00
|
|
|
import addDays from 'date-fns/add_days';
|
2017-06-26 05:48:50 +00:00
|
|
|
import invariant from 'invariant';
|
2017-06-28 03:59:53 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
2018-12-05 06:24:30 +00:00
|
|
|
import parseTitle from 'shared/utils/parseTitle';
|
|
|
|
import unescape from 'shared/utils/unescape';
|
|
|
|
import BaseModel from 'models/BaseModel';
|
|
|
|
import Revision from 'models/Revision';
|
|
|
|
import User from 'models/User';
|
2019-08-09 06:09:09 +00:00
|
|
|
import DocumentsStore from 'stores/DocumentsStore';
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2018-05-08 05:08:47 +00:00
|
|
|
type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
|
2018-05-07 05:13:52 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export default class Document extends BaseModel {
|
|
|
|
isSaving: boolean;
|
2019-08-09 06:09:09 +00:00
|
|
|
store: DocumentsStore;
|
2017-06-29 04:46:00 +00:00
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
collaborators: User[];
|
2017-11-20 05:32:18 +00:00
|
|
|
collectionId: string;
|
2017-07-16 16:24:45 +00:00
|
|
|
lastViewedAt: ?string;
|
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
|
|
|
id: string;
|
|
|
|
team: string;
|
2018-12-05 06:24:30 +00:00
|
|
|
pinned: boolean;
|
|
|
|
text: string;
|
|
|
|
title: string;
|
2017-07-29 23:15:04 +00:00
|
|
|
emoji: string;
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocumentId: ?string;
|
2018-02-28 06:41:12 +00:00
|
|
|
publishedAt: ?string;
|
2019-04-06 23:20:27 +00:00
|
|
|
archivedAt: string;
|
|
|
|
deletedAt: ?string;
|
2017-06-26 05:48:50 +00:00
|
|
|
url: string;
|
2018-12-05 06:24:30 +00:00
|
|
|
urlId: string;
|
2018-05-17 06:07:33 +00:00
|
|
|
shareUrl: ?string;
|
2017-11-25 21:04:54 +00:00
|
|
|
revision: number;
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
constructor(data?: Object = {}, store: DocumentsStore) {
|
2018-12-05 06:24:30 +00:00
|
|
|
super(data, store);
|
|
|
|
this.updateTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateTitle() {
|
2019-09-23 00:09:11 +00:00
|
|
|
const { title, emoji } = parseTitle(this.text);
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
set(this, { title, emoji });
|
|
|
|
}
|
2018-12-05 06:24:30 +00:00
|
|
|
}
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2019-12-19 05:00:36 +00:00
|
|
|
@computed
|
|
|
|
get isOnlyTitle(): boolean {
|
|
|
|
const { title } = parseTitle(this.text);
|
|
|
|
|
|
|
|
// find and extract title
|
|
|
|
const trimmedBody = this.text
|
|
|
|
.trim()
|
|
|
|
.replace(/^#/, '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
return unescape(trimmedBody) === title;
|
|
|
|
}
|
|
|
|
|
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
|
2019-04-18 02:11:23 +00:00
|
|
|
get isStarred(): boolean {
|
2019-08-09 06:09:09 +00:00
|
|
|
return !!this.store.starredIds.get(this.id);
|
2017-06-26 05:48:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2019-11-19 02:51:32 +00:00
|
|
|
@computed
|
|
|
|
get permanentlyDeletedAt(): ?string {
|
|
|
|
if (!this.deletedAt) {
|
2019-12-24 02:12:16 +00:00
|
|
|
return undefined;
|
2019-11-19 02:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addDays(new Date(this.deletedAt), 30).toString();
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:07:33 +00:00
|
|
|
@action
|
|
|
|
share = async () => {
|
2018-12-05 06:24:30 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-09-30 04:24:07 +00:00
|
|
|
@action
|
2018-12-05 06:24:30 +00:00
|
|
|
updateFromJson = data => {
|
|
|
|
set(this, data);
|
|
|
|
this.updateTitle();
|
|
|
|
};
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
archive = () => {
|
|
|
|
return this.store.archive(this);
|
|
|
|
};
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
restore = (revision: Revision) => {
|
2018-12-05 06:24:30 +00:00
|
|
|
return this.store.restore(this, revision);
|
2018-09-30 04:24:07 +00:00
|
|
|
};
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
@action
|
|
|
|
pin = async () => {
|
|
|
|
this.pinned = true;
|
|
|
|
try {
|
2019-10-06 01:42:03 +00:00
|
|
|
const res = await this.store.pin(this);
|
|
|
|
invariant(res && res.data, 'Data should be available');
|
|
|
|
this.updateFromJson(res.data);
|
2018-12-05 06:24:30 +00:00
|
|
|
} catch (err) {
|
2018-03-01 07:28:36 +00:00
|
|
|
this.pinned = false;
|
2018-12-05 06:24:30 +00:00
|
|
|
throw err;
|
2018-03-01 07:28:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
unpin = async () => {
|
|
|
|
this.pinned = false;
|
|
|
|
try {
|
2019-10-06 01:42:03 +00:00
|
|
|
const res = await this.store.unpin(this);
|
|
|
|
invariant(res && res.data, 'Data should be available');
|
|
|
|
this.updateFromJson(res.data);
|
2018-12-05 06:24:30 +00:00
|
|
|
} catch (err) {
|
2018-03-01 07:28:36 +00:00
|
|
|
this.pinned = true;
|
2018-12-05 06:24:30 +00:00
|
|
|
throw err;
|
2018-03-01 07:28:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2019-04-18 02:11:23 +00:00
|
|
|
star = () => {
|
|
|
|
return this.store.star(this);
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
unstar = async () => {
|
2019-04-18 02:11:23 +00:00
|
|
|
return this.store.unstar(this);
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
view = async () => {
|
2019-01-09 06:49:20 +00:00
|
|
|
await client.post('/views.create', { documentId: this.id });
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
fetch = async () => {
|
2018-12-05 06:24:30 +00:00
|
|
|
const res = await client.post('/documents.info', { id: this.id });
|
|
|
|
invariant(res && res.data, 'Data should be available');
|
|
|
|
this.updateFromJson(res.data);
|
2017-06-26 05:48:50 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2018-07-04 20:00:53 +00:00
|
|
|
const isCreating = !this.id;
|
2017-06-29 04:46:00 +00:00
|
|
|
this.isSaving = true;
|
2018-12-05 06:24:30 +00:00
|
|
|
this.updateTitle();
|
2017-06-29 04:46:00 +00:00
|
|
|
|
|
|
|
try {
|
2018-07-04 20:00:53 +00:00
|
|
|
if (isCreating) {
|
2019-06-27 03:24:20 +00:00
|
|
|
return await this.store.create({
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocumentId: this.parentDocumentId,
|
|
|
|
collectionId: this.collectionId,
|
2017-06-29 04:46:00 +00:00
|
|
|
title: this.title,
|
|
|
|
text: this.text,
|
2018-05-07 05:13:52 +00:00
|
|
|
...options,
|
2018-07-04 20:00:53 +00:00
|
|
|
});
|
2017-06-29 04:46:00 +00:00
|
|
|
}
|
2019-04-18 02:11:23 +00:00
|
|
|
|
2019-06-27 03:24:20 +00:00
|
|
|
return await this.store.update({
|
2019-04-18 02:11:23 +00:00
|
|
|
id: this.id,
|
|
|
|
title: this.title,
|
|
|
|
text: this.text,
|
|
|
|
lastRevision: this.revision,
|
|
|
|
...options,
|
|
|
|
});
|
2017-06-29 04:46:00 +00:00
|
|
|
} finally {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
2017-09-04 21:48:56 +00:00
|
|
|
};
|
2017-06-29 04:46:00 +00:00
|
|
|
|
2019-04-09 04:25:13 +00:00
|
|
|
move = (collectionId: string, parentDocumentId: ?string) => {
|
|
|
|
return this.store.move(this, collectionId, parentDocumentId);
|
2017-08-03 13:48:07 +00:00
|
|
|
};
|
|
|
|
|
2018-06-05 13:57:26 +00:00
|
|
|
duplicate = () => {
|
2018-07-21 19:55:40 +00:00
|
|
|
return this.store.duplicate(this);
|
2018-06-05 13:57:26 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 20:35:42 +00:00
|
|
|
download = async () => {
|
2018-12-05 06:24:30 +00:00
|
|
|
// Ensure the document is upto date with latest server contents
|
2018-06-23 20:35:42 +00:00
|
|
|
await this.fetch();
|
|
|
|
|
|
|
|
const blob = new Blob([unescape(this.text)], { type: 'text/markdown' });
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
const a = document.createElement('a');
|
2018-07-21 06:51:05 +00:00
|
|
|
|
|
|
|
// Firefox support requires the anchor tag be in the DOM to trigger the dl
|
|
|
|
if (document.body) document.body.appendChild(a);
|
2018-06-23 20:35:42 +00:00
|
|
|
a.href = url;
|
2017-09-09 23:55:02 +00:00
|
|
|
a.download = `${this.title}.md`;
|
|
|
|
a.click();
|
2018-06-23 20:35:42 +00:00
|
|
|
};
|
2017-06-26 05:48:50 +00:00
|
|
|
}
|