2017-06-26 05:48:50 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import addDays from "date-fns/add_days";
|
2020-09-21 05:32:28 +00:00
|
|
|
import differenceInDays from "date-fns/difference_in_days";
|
2020-06-20 20:59:15 +00:00
|
|
|
import invariant from "invariant";
|
2020-09-01 03:03:05 +00:00
|
|
|
import { action, computed, observable, set } from "mobx";
|
2020-06-20 20:59:15 +00:00
|
|
|
import parseTitle from "shared/utils/parseTitle";
|
|
|
|
import unescape from "shared/utils/unescape";
|
2020-08-09 05:53:59 +00:00
|
|
|
import DocumentsStore from "stores/DocumentsStore";
|
2020-06-20 20:59:15 +00:00
|
|
|
import BaseModel from "models/BaseModel";
|
|
|
|
import User from "models/User";
|
2020-09-21 05:32:28 +00:00
|
|
|
import View from "./View";
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2020-06-03 06:16:15 +00:00
|
|
|
type SaveOptions = {
|
|
|
|
publish?: boolean,
|
|
|
|
done?: boolean,
|
|
|
|
autosave?: boolean,
|
|
|
|
lastRevision?: number,
|
|
|
|
};
|
2018-05-07 05:13:52 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export default class Document extends BaseModel {
|
2020-04-22 04:43:01 +00:00
|
|
|
@observable isSaving: boolean = false;
|
|
|
|
@observable embedsDisabled: boolean = false;
|
2020-08-08 22:18:37 +00:00
|
|
|
@observable injectTemplate: boolean = false;
|
2020-09-21 06:37:09 +00:00
|
|
|
@observable lastViewedAt: ?string;
|
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-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;
|
2020-08-08 22:18:37 +00:00
|
|
|
template: boolean;
|
|
|
|
templateId: ?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;
|
2017-11-25 21:04:54 +00:00
|
|
|
revision: number;
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
constructor(fields: Object, store: DocumentsStore) {
|
|
|
|
super(fields, store);
|
|
|
|
|
2020-09-21 05:32:28 +00:00
|
|
|
if (this.isNewDocument && this.isFromTemplate) {
|
2020-08-08 22:18:37 +00:00
|
|
|
this.title = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:07:34 +00:00
|
|
|
get emoji() {
|
|
|
|
const { emoji } = parseTitle(this.title);
|
|
|
|
return emoji;
|
2018-12-05 06:24:30 +00:00
|
|
|
}
|
2017-06-26 05:48:50 +00:00
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@computed
|
|
|
|
get noun(): string {
|
|
|
|
return this.template ? "template" : "document";
|
|
|
|
}
|
|
|
|
|
2019-12-19 05:00:36 +00:00
|
|
|
@computed
|
|
|
|
get isOnlyTitle(): boolean {
|
2020-04-05 22:07:34 +00:00
|
|
|
return !this.text.trim();
|
2019-12-19 05:00:36 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:32:28 +00:00
|
|
|
@computed
|
|
|
|
get isNew(): boolean {
|
|
|
|
return (
|
|
|
|
!this.lastViewedAt &&
|
|
|
|
differenceInDays(new Date(), new Date(this.createdAt)) < 14
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@computed
|
|
|
|
get isTemplate(): boolean {
|
|
|
|
return !!this.template;
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:22:20 +00:00
|
|
|
@computed
|
|
|
|
get isDraft(): boolean {
|
|
|
|
return !this.publishedAt;
|
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@computed
|
|
|
|
get titleWithDefault(): string {
|
|
|
|
return this.title || "Untitled";
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@computed
|
2020-09-21 05:32:28 +00:00
|
|
|
get isNewDocument(): boolean {
|
2020-08-08 22:18:37 +00:00
|
|
|
return this.createdAt === this.updatedAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
get isFromTemplate(): boolean {
|
|
|
|
return !!this.templateId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
get placeholder(): ?string {
|
|
|
|
return this.isTemplate ? "Start your template…" : "Start with a title…";
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:07:33 +00:00
|
|
|
@action
|
|
|
|
share = async () => {
|
2020-07-29 02:14:32 +00:00
|
|
|
return this.store.rootStore.shares.create({ documentId: this.id });
|
2018-05-17 06:07:33 +00:00
|
|
|
};
|
|
|
|
|
2018-09-30 04:24:07 +00:00
|
|
|
@action
|
2021-01-30 05:36:09 +00:00
|
|
|
updateFromJson = (data: Object) => {
|
2018-12-05 06:24:30 +00:00
|
|
|
set(this, data);
|
|
|
|
};
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
archive = () => {
|
|
|
|
return this.store.archive(this);
|
|
|
|
};
|
|
|
|
|
2021-01-30 05:36:09 +00:00
|
|
|
restore = (options: { revisionId?: string, collectionId?: string }) => {
|
2020-09-07 18:51:09 +00:00
|
|
|
return this.store.restore(this, options);
|
2018-09-30 04:24:07 +00:00
|
|
|
};
|
|
|
|
|
2020-09-01 03:03:05 +00:00
|
|
|
unpublish = () => {
|
|
|
|
return this.store.unpublish(this);
|
|
|
|
};
|
|
|
|
|
2020-04-22 04:43:01 +00:00
|
|
|
@action
|
|
|
|
enableEmbeds = () => {
|
|
|
|
this.embedsDisabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
disableEmbeds = () => {
|
|
|
|
this.embedsDisabled = true;
|
|
|
|
};
|
|
|
|
|
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);
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Data should be available");
|
2019-10-06 01:42:03 +00:00
|
|
|
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);
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Data should be available");
|
2019-10-06 01:42:03 +00:00
|
|
|
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
|
2020-01-11 21:54:49 +00:00
|
|
|
view = () => {
|
2020-12-01 07:17:39 +00:00
|
|
|
// we don't record views for documents in the trash
|
2020-12-03 04:50:54 +00:00
|
|
|
if (this.isDeleted || !this.publishedAt) {
|
2020-12-01 07:17:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:54:49 +00:00
|
|
|
return this.store.rootStore.views.create({ documentId: this.id });
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2020-09-21 05:32:28 +00:00
|
|
|
@action
|
|
|
|
updateLastViewed = (view: View) => {
|
|
|
|
this.lastViewedAt = view.lastViewedAt;
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2020-08-08 22:18:37 +00:00
|
|
|
templatize = async () => {
|
|
|
|
return this.store.templatize(this.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateFromTemplate = async (template: Document) => {
|
|
|
|
this.templateId = template.id;
|
|
|
|
this.title = template.title;
|
|
|
|
this.text = template.text;
|
|
|
|
this.injectTemplate = true;
|
2017-06-26 05:48:50 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2021-01-30 05:36:09 +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;
|
|
|
|
|
|
|
|
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,
|
2021-01-30 05:36:09 +00:00
|
|
|
publish: options.publish,
|
|
|
|
done: options.done,
|
|
|
|
autosave: options.autosave,
|
2018-07-04 20:00:53 +00:00
|
|
|
});
|
2017-06-29 04:46:00 +00:00
|
|
|
}
|
2019-04-18 02:11:23 +00:00
|
|
|
|
2020-06-03 06:17:54 +00:00
|
|
|
if (options.lastRevision) {
|
|
|
|
return await this.store.update({
|
|
|
|
id: this.id,
|
|
|
|
title: this.title,
|
|
|
|
text: this.text,
|
2020-08-08 22:18:37 +00:00
|
|
|
templateId: this.templateId,
|
2020-06-03 06:17:54 +00:00
|
|
|
lastRevision: options.lastRevision,
|
2021-01-30 05:36:09 +00:00
|
|
|
publish: options.publish,
|
|
|
|
done: options.done,
|
|
|
|
autosave: options.autosave,
|
2020-06-03 06:17:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
throw new Error("Attempting to update without a lastRevision");
|
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) => {
|
2020-12-16 03:07:29 +00:00
|
|
|
return this.store.move(this.id, 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
|
|
|
};
|
|
|
|
|
2020-07-17 04:26:23 +00:00
|
|
|
getSummary = (paragraphs: number = 4) => {
|
2020-08-09 01:53:11 +00:00
|
|
|
const result = this.text.trim().split("\n").slice(0, paragraphs).join("\n");
|
2020-07-17 04:26:23 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
2020-04-05 22:07:34 +00:00
|
|
|
const body = unescape(this.text);
|
|
|
|
const blob = new Blob([`# ${this.title}\n\n${body}`], {
|
2020-06-20 20:59:15 +00:00
|
|
|
type: "text/markdown",
|
2020-04-05 22:07:34 +00:00
|
|
|
});
|
2018-06-23 20:35:42 +00:00
|
|
|
const url = URL.createObjectURL(blob);
|
2020-06-20 20:59:15 +00:00
|
|
|
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;
|
2020-08-08 22:18:37 +00:00
|
|
|
a.download = `${this.titleWithDefault}.md`;
|
2017-09-09 23:55:02 +00:00
|
|
|
a.click();
|
2018-06-23 20:35:42 +00:00
|
|
|
};
|
2017-06-26 05:48:50 +00:00
|
|
|
}
|