2017-11-10 23:39:51 +00:00
|
|
|
import utils from '../services/utils';
|
|
|
|
|
2017-11-15 08:12:56 +00:00
|
|
|
const idShifter = offset => (state, getters) => {
|
|
|
|
const ids = Object.keys(getters.currentFileDiscussions);
|
|
|
|
const idx = ids.indexOf(state.currentDiscussionId) + offset + ids.length;
|
|
|
|
return ids[idx % ids.length];
|
|
|
|
};
|
|
|
|
|
2017-11-10 23:39:51 +00:00
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: {
|
|
|
|
currentDiscussionId: null,
|
|
|
|
newDiscussion: null,
|
2017-11-15 08:12:56 +00:00
|
|
|
newDiscussionId: null,
|
|
|
|
newCommentText: '',
|
|
|
|
newCommentSelection: { start: 0, end: 0 },
|
|
|
|
isCommenting: false,
|
|
|
|
stickyComment: null,
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
setCurrentDiscussionId: (state, value) => {
|
2017-11-15 08:12:56 +00:00
|
|
|
if (state.currentDiscussionId !== value) {
|
|
|
|
state.currentDiscussionId = value;
|
|
|
|
state.isCommenting = false;
|
|
|
|
}
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
setNewDiscussion: (state, value) => {
|
|
|
|
state.newDiscussion = value;
|
|
|
|
state.newDiscussionId = utils.uid();
|
|
|
|
state.currentDiscussionId = state.newDiscussionId;
|
2017-11-15 08:12:56 +00:00
|
|
|
state.isCommenting = true;
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
patchNewDiscussion: (state, value) => {
|
|
|
|
Object.assign(state.newDiscussion, value);
|
|
|
|
},
|
2017-11-15 08:12:56 +00:00
|
|
|
setNewCommentText: (state, value) => {
|
|
|
|
state.newCommentText = value || '';
|
|
|
|
},
|
|
|
|
setNewCommentSelection: (state, value) => {
|
|
|
|
state.newCommentSelection = value;
|
|
|
|
},
|
|
|
|
setIsCommenting: (state, value) => {
|
|
|
|
state.isCommenting = value;
|
|
|
|
if (!value) {
|
|
|
|
state.newDiscussionId = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setStickyComment: (state, value) => {
|
|
|
|
state.stickyComment = value;
|
|
|
|
},
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
newDiscussion: state =>
|
|
|
|
state.currentDiscussionId === state.newDiscussionId && state.newDiscussion,
|
2017-11-15 08:12:56 +00:00
|
|
|
currentFileDiscussionLastComments: (state, getters, rootState, rootGetters) => {
|
|
|
|
const discussions = rootGetters['content/current'].discussions;
|
|
|
|
const comments = rootGetters['content/current'].comments;
|
|
|
|
const discussionLastComments = {};
|
|
|
|
Object.keys(comments).forEach((commentId) => {
|
|
|
|
const comment = comments[commentId];
|
|
|
|
if (discussions[comment.discussionId]) {
|
|
|
|
const lastComment = discussionLastComments[comment.discussionId];
|
|
|
|
if (!lastComment || lastComment.created < comment.created) {
|
|
|
|
discussionLastComments[comment.discussionId] = comment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return discussionLastComments;
|
|
|
|
},
|
2017-11-10 23:39:51 +00:00
|
|
|
currentFileDiscussions: (state, getters, rootState, rootGetters) => {
|
2017-11-15 08:12:56 +00:00
|
|
|
const currentFileDiscussions = {};
|
2017-11-10 23:39:51 +00:00
|
|
|
const newDiscussion = getters.newDiscussion;
|
|
|
|
if (newDiscussion) {
|
2017-11-15 08:12:56 +00:00
|
|
|
currentFileDiscussions[state.newDiscussionId] = newDiscussion;
|
2017-11-10 23:39:51 +00:00
|
|
|
}
|
2017-11-15 08:12:56 +00:00
|
|
|
const discussions = rootGetters['content/current'].discussions;
|
|
|
|
const discussionLastComments = getters.currentFileDiscussionLastComments;
|
|
|
|
Object.keys(discussionLastComments)
|
|
|
|
.sort((id1, id2) =>
|
|
|
|
discussionLastComments[id2].created - discussionLastComments[id1].created)
|
|
|
|
.forEach((discussionId) => {
|
|
|
|
currentFileDiscussions[discussionId] = discussions[discussionId];
|
|
|
|
});
|
|
|
|
return currentFileDiscussions;
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
currentDiscussion: (state, getters) =>
|
|
|
|
getters.currentFileDiscussions[state.currentDiscussionId],
|
2017-11-15 08:12:56 +00:00
|
|
|
previousDiscussionId: idShifter(-1),
|
|
|
|
nextDiscussionId: idShifter(1),
|
|
|
|
currentDiscussionComments: (state, getters, rootState, rootGetters) => {
|
|
|
|
const comments = {};
|
|
|
|
if (getters.currentDiscussion) {
|
|
|
|
const contentComments = rootGetters['content/current'].comments;
|
|
|
|
Object.keys(contentComments)
|
|
|
|
.filter(commentId =>
|
|
|
|
contentComments[commentId].discussionId === state.currentDiscussionId)
|
|
|
|
.sort((id1, id2) =>
|
|
|
|
contentComments[id1].created - contentComments[id2].created)
|
|
|
|
.forEach((commentId) => {
|
|
|
|
comments[commentId] = contentComments[commentId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return comments;
|
|
|
|
},
|
|
|
|
currentDiscussionLastCommentId: (state, getters) =>
|
|
|
|
Object.keys(getters.currentDiscussionComments).pop(),
|
|
|
|
currentDiscussionLastComment: (state, getters) =>
|
|
|
|
getters.currentDiscussionComments[getters.currentDiscussionLastCommentId],
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
createNewDiscussion({ commit, rootGetters }, selection) {
|
|
|
|
if (selection) {
|
|
|
|
let text = rootGetters['content/current'].text.slice(selection.start, selection.end).trim();
|
2017-11-15 08:12:56 +00:00
|
|
|
const maxLength = 80;
|
|
|
|
if (text.length > maxLength) {
|
|
|
|
text = `${text.slice(0, maxLength - 1).trim()}…`;
|
2017-11-10 23:39:51 +00:00
|
|
|
}
|
|
|
|
commit('setNewDiscussion', { ...selection, text });
|
|
|
|
}
|
|
|
|
},
|
2017-11-15 08:12:56 +00:00
|
|
|
cleanCurrentFile(
|
|
|
|
{ getters, rootGetters, commit, dispatch },
|
|
|
|
{ filterComment, filterDiscussion } = {},
|
|
|
|
) {
|
|
|
|
const discussions = rootGetters['content/current'].discussions;
|
|
|
|
const comments = rootGetters['content/current'].comments;
|
|
|
|
const patch = {
|
|
|
|
discussions: {},
|
|
|
|
comments: {},
|
|
|
|
};
|
|
|
|
Object.keys(comments).forEach((commentId) => {
|
|
|
|
const comment = comments[commentId];
|
|
|
|
const discussion = discussions[comment.discussionId];
|
|
|
|
if (discussion && comment !== filterComment && discussion !== filterDiscussion) {
|
|
|
|
patch.discussions[comment.discussionId] = discussion;
|
|
|
|
patch.comments[commentId] = comment;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const nextDiscussionId = getters.nextDiscussionId;
|
|
|
|
dispatch('content/patchCurrent', patch, { root: true });
|
|
|
|
if (!getters.currentDiscussion) {
|
|
|
|
// Keep the gutter open
|
|
|
|
commit('setCurrentDiscussionId', nextDiscussionId);
|
|
|
|
}
|
|
|
|
},
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
};
|