mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
drag'n'drop reorder documents in the client store
This commit is contained in:
1 parent
660e9682c5
commit
3864443eae
4 files changed
+85
-3
No files matched your search
@@ -56,7 +56,7 @@ class CollectionLink extends React.Component<Props> {
|
||||
}
|
||||
|
||||
documents.move(
|
||||
document,
|
||||
documents.get(document.id),
|
||||
collection.id,
|
||||
undefined,
|
||||
result.destination.index
|
||||
|
||||
@@ -88,7 +88,12 @@ class DocumentLink extends React.Component<Props> {
|
||||
return;
|
||||
}
|
||||
|
||||
documents.move(document, collection.id, node.id, result.destination.index);
|
||||
documents.move(
|
||||
documents.get(document.id),
|
||||
collection.id,
|
||||
node.id,
|
||||
result.destination.index
|
||||
);
|
||||
};
|
||||
|
||||
render() {
|
||||
|
||||
@@ -100,6 +100,58 @@ export default class Collection extends BaseModel {
|
||||
return [];
|
||||
}
|
||||
|
||||
@action
|
||||
addDocumentToStructure(
|
||||
document: NavigationNode,
|
||||
parentDocumentId: ?string,
|
||||
index: ?number
|
||||
) {
|
||||
if (!parentDocumentId) {
|
||||
this.documents.splice(
|
||||
index !== undefined ? index : this.documents.length,
|
||||
0,
|
||||
document
|
||||
);
|
||||
} else {
|
||||
const recursivelyAddDocument = nodes => {
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
if (nodes[i].id === parentDocumentId) {
|
||||
nodes[i].children.splice(
|
||||
index !== undefined ? index : nodes[i].children.length,
|
||||
0,
|
||||
document
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
const isAdded = recursivelyAddDocument(nodes[i].children);
|
||||
if (isAdded) return true;
|
||||
}
|
||||
};
|
||||
|
||||
recursivelyAddDocument(this.documents);
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
removeDocumentInStructure(documentId: string) {
|
||||
const recursivelyRemoveDocument = nodes => {
|
||||
const index = nodes.findIndex(item => item.id === documentId);
|
||||
if (index !== -1) {
|
||||
nodes.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
const isFound = recursivelyRemoveDocument(nodes[i].children);
|
||||
if (isFound) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return recursivelyRemoveDocument(this.documents);
|
||||
}
|
||||
|
||||
toJS = () => {
|
||||
return pick(this, ['id', 'name', 'color', 'description', 'private']);
|
||||
};
|
||||
|
||||
@@ -18,7 +18,12 @@ import BaseStore from 'stores/BaseStore';
|
||||
import RootStore from 'stores/RootStore';
|
||||
import Document from 'models/Document';
|
||||
import Revision from 'models/Revision';
|
||||
import type { FetchOptions, PaginationParams, SearchResult } from 'types';
|
||||
import type {
|
||||
FetchOptions,
|
||||
PaginationParams,
|
||||
SearchResult,
|
||||
NavigationNode,
|
||||
} from 'types';
|
||||
|
||||
export default class DocumentsStore extends BaseStore<Document> {
|
||||
@observable recentlyViewedIds: string[] = [];
|
||||
@@ -361,6 +366,25 @@ export default class DocumentsStore extends BaseStore<Document> {
|
||||
parentDocumentId: ?string,
|
||||
index: ?number
|
||||
) => {
|
||||
const oldCollection = this.rootStore.collections.get(document.collectionId);
|
||||
// Retrive all children documents
|
||||
const childDocuments = oldCollection.getDocumentChildren(document.id);
|
||||
// Remove document from old collection
|
||||
oldCollection.removeDocumentInStructure(document.id);
|
||||
|
||||
// Recreate navigation node object
|
||||
const navigationNode: NavigationNode = {
|
||||
id: document.id,
|
||||
title: document.title,
|
||||
url: document.url,
|
||||
children: childDocuments,
|
||||
};
|
||||
|
||||
const collection = this.rootStore.collections.get(collectionId);
|
||||
// Move document to new location on the client side
|
||||
collection.addDocumentToStructure(navigationNode, parentDocumentId, index);
|
||||
|
||||
// Send data to server
|
||||
const res = await client.post('/documents.move', {
|
||||
id: document.id,
|
||||
collectionId,
|
||||
@@ -369,6 +393,7 @@ export default class DocumentsStore extends BaseStore<Document> {
|
||||
});
|
||||
invariant(res && res.data, 'Data not available');
|
||||
|
||||
// Apply data from the server
|
||||
res.data.documents.forEach(this.add);
|
||||
res.data.collections.forEach(this.rootStore.collections.add);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user