* WIP: Slate editor
* WIP
* Focus at start / end working
* ah ha
* Super basic floating toolbar
* Nested list editing
* Pulling more logic into plugins
* inline code markdown
* Backspace at end of code block should remove mark
* Ensure there is always an empty line at editor end
* Add keyboard shortcuts for bold, italic, underline
* Add strikethrough shortcode and toolbar
* Toolbar to declarative
Fixed paragraph styling
Removed unused stuffs
* Super basic link editing
* Split Toolbar, now possible to edit and remove links
* Add new link to selection from toolbar working
* Ensure toolbar doesn't extend off screen
* Fix minor js issues, disable formatting of document title
* Boom, icons
* Remove codemirror, fix MD parsing issues
* CMD+S now saves inplace
* Add --- shortcut for horizontal rule
* Improved styling for link editor
* Add header anchors in readOnly
* More readable core text color
* Restored image file uploading 🎉
* Add support for inline md syntax, ** __ etc
* Centered
* Flooooow
* Checklist support
* Upgrade edit list plugin
* Finally. Allow keydown within rich textarea
* Update Markdown serializer
* Cleanup, remove async editor loading
* Editor > MarkdownEditor
Fixed unsaved changes warning triggered when all changes are saved
* MOAR typing
* Combine edit and view
* Fixed checkboxes still editable in readOnly
* wip
* Breadcrumb
Restored scroll
* Move document scene actions to menu
* Added: Support for code blocks, syntax highlighting
* Cleanup
* > styled component
* Prevent CMD+Enter from adding linebreak
* Show image uploading in layout activity indicator
* Upgrade editor deps
* Improve link toolbar. Only one scenario where it's not working now
198 lines
5.0 KiB
JavaScript
198 lines
5.0 KiB
JavaScript
// @flow
|
|
import { observable, action, computed, toJS } from 'mobx';
|
|
import { browserHistory } from 'react-router';
|
|
import get from 'lodash/get';
|
|
import invariant from 'invariant';
|
|
import { client } from 'utils/ApiClient';
|
|
import emojify from 'utils/emojify';
|
|
import type { Document, NavigationNode } from 'types';
|
|
|
|
type SaveProps = { redirect?: boolean };
|
|
|
|
const parseHeader = text => {
|
|
const firstLine = text.split(/\r?\n/)[0];
|
|
if (firstLine) {
|
|
const match = firstLine.match(/^#+ +(.*)$/);
|
|
|
|
if (match) {
|
|
return emojify(match[1]);
|
|
} else {
|
|
return '';
|
|
}
|
|
}
|
|
return '';
|
|
};
|
|
|
|
class DocumentStore {
|
|
@observable collapsedNodes: string[] = [];
|
|
@observable documentId = null;
|
|
@observable collectionId = null;
|
|
@observable document: Document;
|
|
@observable parentDocument: Document;
|
|
@observable hasPendingChanges = false;
|
|
@observable newDocument: ?boolean;
|
|
@observable newChildDocument: ?boolean;
|
|
|
|
@observable isEditing: boolean = false;
|
|
@observable isFetching: boolean = false;
|
|
@observable isSaving: boolean = false;
|
|
@observable isUploading: boolean = false;
|
|
|
|
/* Computed */
|
|
|
|
@computed get isCollection(): boolean {
|
|
return !!this.document && this.document.collection.type === 'atlas';
|
|
}
|
|
|
|
@computed get collectionTree(): ?Object {
|
|
if (
|
|
this.document &&
|
|
this.document.collection &&
|
|
this.document.collection.type === 'atlas'
|
|
) {
|
|
const tree = this.document.collection.navigationTree;
|
|
const collapseNodes = node => {
|
|
node.collapsed = this.collapsedNodes.includes(node.id);
|
|
node.children = node.children.map(childNode => {
|
|
return collapseNodes(childNode);
|
|
});
|
|
|
|
return node;
|
|
};
|
|
|
|
return collapseNodes(toJS(tree));
|
|
}
|
|
}
|
|
|
|
@computed get pathToDocument(): Array<NavigationNode> {
|
|
let path;
|
|
const traveler = (node, previousPath) => {
|
|
if (this.document && node.id === this.document.id) {
|
|
path = previousPath;
|
|
return;
|
|
} else {
|
|
node.children.forEach(childNode => {
|
|
const newPath = [...previousPath, node];
|
|
return traveler(childNode, newPath);
|
|
});
|
|
}
|
|
};
|
|
|
|
if (this.document && this.collectionTree) {
|
|
traveler(this.collectionTree, []);
|
|
invariant(path, 'Path is not available for collection, abort');
|
|
return path.splice(1);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/* Actions */
|
|
|
|
@action fetchDocument = async () => {
|
|
this.isFetching = true;
|
|
|
|
try {
|
|
const res = await client.get(
|
|
'/documents.info',
|
|
{
|
|
id: this.documentId,
|
|
},
|
|
{ cache: true }
|
|
);
|
|
invariant(res && res.data, 'Data should be available');
|
|
if (this.newChildDocument) {
|
|
this.parentDocument = res.data;
|
|
} else {
|
|
this.document = res.data;
|
|
}
|
|
} catch (e) {
|
|
console.error('Something went wrong');
|
|
}
|
|
this.isFetching = false;
|
|
};
|
|
|
|
@action saveDocument = async ({ redirect = true }: SaveProps) => {
|
|
if (this.isSaving) return;
|
|
|
|
this.isSaving = true;
|
|
|
|
try {
|
|
const res = await client.post(
|
|
'/documents.create',
|
|
{
|
|
parentDocument: get(this.parentDocument, 'id'),
|
|
collection: get(
|
|
this.parentDocument,
|
|
'collection.id',
|
|
this.collectionId
|
|
),
|
|
title: get(this.document, 'title', 'Untitled document'),
|
|
text: get(this.document, 'text'),
|
|
},
|
|
{ cache: true }
|
|
);
|
|
invariant(res && res.data, 'Data should be available');
|
|
const { url } = res.data;
|
|
|
|
this.hasPendingChanges = false;
|
|
if (redirect) browserHistory.push(url);
|
|
} catch (e) {
|
|
console.error('Something went wrong');
|
|
}
|
|
this.isSaving = false;
|
|
};
|
|
|
|
@action updateDocument = async ({ redirect = true }: SaveProps) => {
|
|
if (this.isSaving) return;
|
|
|
|
this.isSaving = true;
|
|
|
|
try {
|
|
const res = await client.post(
|
|
'/documents.update',
|
|
{
|
|
id: this.documentId,
|
|
title: get(this.document, 'title', 'Untitled document'),
|
|
text: get(this.document, 'text'),
|
|
},
|
|
{ cache: true }
|
|
);
|
|
invariant(res && res.data, 'Data should be available');
|
|
const { url } = res.data;
|
|
|
|
this.hasPendingChanges = false;
|
|
if (redirect) browserHistory.push(url);
|
|
} catch (e) {
|
|
console.error('Something went wrong');
|
|
}
|
|
this.isSaving = false;
|
|
};
|
|
|
|
@action deleteDocument = async () => {
|
|
this.isFetching = true;
|
|
|
|
try {
|
|
await client.post('/documents.delete', { id: this.documentId });
|
|
browserHistory.push(this.document.collection.id);
|
|
} catch (e) {
|
|
console.error('Something went wrong');
|
|
}
|
|
this.isFetching = false;
|
|
};
|
|
|
|
@action updateText = (text: string) => {
|
|
if (!this.document) return;
|
|
|
|
this.document.text = text;
|
|
this.document.title = parseHeader(text);
|
|
this.hasPendingChanges = true;
|
|
};
|
|
|
|
@action updateUploading = (uploading: boolean) => {
|
|
this.isUploading = uploading;
|
|
};
|
|
}
|
|
|
|
export default DocumentStore;
|