Stackedit/src/store/locationTemplate.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-04-27 14:37:05 +00:00
import moduleTemplate from './moduleTemplate';
import providerRegistry from '../services/providers/common/providerRegistry';
const addToGroup = (groups, item) => {
const list = groups[item.fileId];
if (!list) {
groups[item.fileId] = [item];
} else {
list.push(item);
}
2018-04-27 14:37:05 +00:00
};
export default (empty) => {
const module = moduleTemplate(empty);
module.getters = {
...module.getters,
2018-05-13 13:27:33 +00:00
groupedByFileId: (state, { items }) => {
2018-04-27 14:37:05 +00:00
const groups = {};
2018-05-13 13:27:33 +00:00
items.forEach(item => addToGroup(groups, item));
2018-04-27 14:37:05 +00:00
return groups;
},
groupedByFileIdAndHash: (state, { items }) => {
const fileIdGroups = {};
items.forEach((item) => {
let hashGroups = fileIdGroups[item.fileId];
if (!hashGroups) {
hashGroups = {};
fileIdGroups[item.fileId] = hashGroups;
}
const list = hashGroups[item.hash];
if (!list) {
hashGroups[item.hash] = [item];
} else {
list.push(item);
}
});
return fileIdGroups;
},
2018-05-13 13:27:33 +00:00
filteredGroupedByFileId: (state, { items }) => {
2018-04-27 14:37:05 +00:00
const groups = {};
items
.filter((item) => {
// Filter items that we can't use
const provider = providerRegistry.providersById[item.providerId];
return provider && provider.getToken(item);
})
.forEach(item => addToGroup(groups, item));
2018-04-27 14:37:05 +00:00
return groups;
},
2018-05-13 13:27:33 +00:00
current: (state, { filteredGroupedByFileId }, rootState, rootGetters) => {
const locations = filteredGroupedByFileId[rootGetters['file/current'].id] || [];
2018-04-27 14:37:05 +00:00
return locations.map((location) => {
const provider = providerRegistry.providersById[location.providerId];
2018-04-27 14:37:05 +00:00
return {
...location,
description: provider.getLocationDescription(location),
url: provider.getLocationUrl(location),
2018-04-27 14:37:05 +00:00
};
});
},
currentWithWorkspaceSyncLocation: (state, { current }, rootState, rootGetters) => {
const fileId = rootGetters['file/current'].id;
const fileSyncData = rootGetters['data/syncDataByItemId'][fileId];
const contentSyncData = rootGetters['data/syncDataByItemId'][`${fileId}/content`];
if (!fileSyncData || !contentSyncData) {
return current;
}
// Add the workspace sync location
const workspaceProvider = providerRegistry.providersById[
rootGetters['workspace/currentWorkspace'].providerId];
return [{
id: 'main',
providerId: workspaceProvider.id,
fileId,
description: workspaceProvider.getSyncDataDescription(fileSyncData, contentSyncData),
url: workspaceProvider.getSyncDataUrl(fileSyncData, contentSyncData),
}, ...current];
},
2018-04-27 14:37:05 +00:00
};
return module;
};