* WIP * feat: Add collection.memberships endpoint * feat: Add ability to filter collection.memberships with query * WIP * Merge stashed work * feat: Add ability to filter memberships by permission * continued refactoring * paginated list component * Collection member management * fix: Incorrect policy data sent down after collection.update * Reduce duplication, add empty state * cleanup * fix: Modal close should be a real button * fix: Allow opening edit from modal * fix: remove unused methods * test: fix * Passing test suite * Refactor * fix: Flow UI errors * test: Add collections.update tests * lint * test: moar tests * fix: Missing scopes, more missing tests * fix: Handle collection privacy change over socket * fix: More membership scopes * fix: view endpoint permissions * fix: respond to privacy change on socket event * policy driven menus * fix: share endpoint policies * chore: Use policies to drive documents UI * alignment * fix: Header height * fix: Correct behavior when collection becomes private * fix: Header height for read-only collection * send id's over socket instead of serialized objects * fix: Remote policy change * fix: reduce collection fetching * More websocket efficiencies * fix: Document collection pinning * fix: Restored ability to edit drafts fix: Removed ability to star drafts * fix: Require write permissions to pin doc to collection * fix: Header title overlaying document actions at small screen sizes * fix: Jank on load caused by previous commit * fix: Double collection fetch post-publish * fix: Hide publish button if draft is in no longer accessible collection * fix: Always allow deleting drafts fix: Improved handling of deleted documents * feat: Show collections in drafts view feat: Show more obvious 'draft' badge on documents * fix: incorrect policies after publish to private collection * fix: Duplicating a draft publishes it
203 lines
4.5 KiB
JavaScript
203 lines
4.5 KiB
JavaScript
// @flow
|
|
import { action, set, computed } from 'mobx';
|
|
import invariant from 'invariant';
|
|
import { client } from 'utils/ApiClient';
|
|
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';
|
|
import DocumentsStore from 'stores/DocumentsStore';
|
|
|
|
type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
|
|
|
|
export default class Document extends BaseModel {
|
|
isSaving: boolean;
|
|
store: DocumentsStore;
|
|
|
|
collaborators: User[];
|
|
collectionId: string;
|
|
lastViewedAt: ?string;
|
|
createdAt: string;
|
|
createdBy: User;
|
|
updatedAt: string;
|
|
updatedBy: User;
|
|
id: string;
|
|
team: string;
|
|
pinned: boolean;
|
|
text: string;
|
|
title: string;
|
|
emoji: string;
|
|
parentDocumentId: ?string;
|
|
publishedAt: ?string;
|
|
archivedAt: string;
|
|
deletedAt: ?string;
|
|
url: string;
|
|
urlId: string;
|
|
shareUrl: ?string;
|
|
revision: number;
|
|
|
|
constructor(data?: Object = {}, store: DocumentsStore) {
|
|
super(data, store);
|
|
this.updateTitle();
|
|
}
|
|
|
|
@action
|
|
updateTitle() {
|
|
const { title, emoji } = parseTitle(this.text);
|
|
|
|
if (title) {
|
|
set(this, { title, emoji });
|
|
}
|
|
}
|
|
|
|
@computed
|
|
get modifiedSinceViewed(): boolean {
|
|
return !!this.lastViewedAt && this.lastViewedAt < this.updatedAt;
|
|
}
|
|
|
|
@computed
|
|
get isStarred(): boolean {
|
|
return !!this.store.starredIds.get(this.id);
|
|
}
|
|
|
|
@computed
|
|
get isArchived(): boolean {
|
|
return !!this.archivedAt;
|
|
}
|
|
|
|
@computed
|
|
get isDeleted(): boolean {
|
|
return !!this.deletedAt;
|
|
}
|
|
|
|
@computed
|
|
get isDraft(): boolean {
|
|
return !this.publishedAt;
|
|
}
|
|
|
|
@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;
|
|
};
|
|
|
|
@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 {
|
|
const res = await this.store.pin(this);
|
|
invariant(res && res.data, 'Data should be available');
|
|
this.updateFromJson(res.data);
|
|
} catch (err) {
|
|
this.pinned = false;
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
@action
|
|
unpin = async () => {
|
|
this.pinned = false;
|
|
try {
|
|
const res = await this.store.unpin(this);
|
|
invariant(res && res.data, 'Data should be available');
|
|
this.updateFromJson(res.data);
|
|
} catch (err) {
|
|
this.pinned = true;
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
@action
|
|
star = () => {
|
|
return this.store.star(this);
|
|
};
|
|
|
|
@action
|
|
unstar = async () => {
|
|
return this.store.unstar(this);
|
|
};
|
|
|
|
@action
|
|
view = async () => {
|
|
await client.post('/views.create', { documentId: this.id });
|
|
};
|
|
|
|
@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);
|
|
};
|
|
|
|
@action
|
|
save = async (options: SaveOptions) => {
|
|
if (this.isSaving) return this;
|
|
|
|
const isCreating = !this.id;
|
|
this.isSaving = true;
|
|
this.updateTitle();
|
|
|
|
try {
|
|
if (isCreating) {
|
|
return await this.store.create({
|
|
parentDocumentId: this.parentDocumentId,
|
|
collectionId: this.collectionId,
|
|
title: this.title,
|
|
text: this.text,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
return await this.store.update({
|
|
id: this.id,
|
|
title: this.title,
|
|
text: this.text,
|
|
lastRevision: this.revision,
|
|
...options,
|
|
});
|
|
} finally {
|
|
this.isSaving = false;
|
|
}
|
|
};
|
|
|
|
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();
|
|
};
|
|
}
|