2017-11-10 23:39:51 +00:00
|
|
|
import utils from '../services/utils';
|
2017-11-17 20:33:49 +00:00
|
|
|
import googleHelper from '../services/providers/helpers/googleHelper';
|
|
|
|
import syncSvc from '../services/syncSvc';
|
2017-11-10 23:39:51 +00:00
|
|
|
|
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,
|
2017-11-17 00:38:55 +00:00
|
|
|
isCommenting: false,
|
2017-11-15 08:12:56 +00:00
|
|
|
newCommentText: '',
|
|
|
|
newCommentSelection: { start: 0, end: 0 },
|
2017-11-17 00:38:55 +00:00
|
|
|
newCommentFocus: false,
|
2017-11-15 08:12:56 +00:00
|
|
|
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-17 00:38:55 +00:00
|
|
|
state.newCommentFocus = true;
|
2017-11-10 23:39:51 +00:00
|
|
|
},
|
|
|
|
patchNewDiscussion: (state, value) => {
|
|
|
|
Object.assign(state.newDiscussion, value);
|
|
|
|
},
|
2017-11-17 00:38:55 +00:00
|
|
|
setIsCommenting: (state, value) => {
|
|
|
|
state.isCommenting = value;
|
|
|
|
if (!value) {
|
|
|
|
state.newDiscussionId = null;
|
|
|
|
} else {
|
|
|
|
state.newCommentFocus = true;
|
|
|
|
}
|
|
|
|
},
|
2017-11-15 08:12:56 +00:00
|
|
|
setNewCommentText: (state, value) => {
|
|
|
|
state.newCommentText = value || '';
|
|
|
|
},
|
|
|
|
setNewCommentSelection: (state, value) => {
|
|
|
|
state.newCommentSelection = value;
|
|
|
|
},
|
2017-11-17 00:38:55 +00:00
|
|
|
setNewCommentFocus: (state, value) => {
|
|
|
|
state.newCommentFocus = value;
|
2017-11-15 08:12:56 +00:00
|
|
|
},
|
|
|
|
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 = {};
|
2017-12-10 23:49:20 +00:00
|
|
|
Object.entries(comments).forEach(([, comment]) => {
|
2017-11-15 08:12:56 +00:00
|
|
|
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;
|
2017-12-10 23:49:20 +00:00
|
|
|
Object.entries(discussionLastComments)
|
|
|
|
.sort(([, lastComment1], [, lastComment2]) =>
|
|
|
|
lastComment1.created - lastComment2.created)
|
|
|
|
.forEach(([discussionId]) => {
|
2017-11-15 08:12:56 +00:00
|
|
|
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;
|
2017-12-10 23:49:20 +00:00
|
|
|
Object.entries(contentComments)
|
|
|
|
.filter(([, comment]) =>
|
|
|
|
comment.discussionId === state.currentDiscussionId)
|
|
|
|
.sort(([, comment1], [, comment2]) =>
|
|
|
|
comment1.created - comment2.created)
|
|
|
|
.forEach(([commentId, comment]) => {
|
|
|
|
comments[commentId] = comment;
|
2017-11-15 08:12:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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: {
|
2017-11-21 08:19:22 +00:00
|
|
|
cancelNewComment({ commit, getters }) {
|
|
|
|
commit('setIsCommenting', false);
|
|
|
|
if (!getters.currentDiscussion) {
|
|
|
|
commit('setCurrentDiscussionId', getters.nextDiscussionId);
|
|
|
|
}
|
|
|
|
},
|
2017-11-17 20:33:49 +00:00
|
|
|
createNewDiscussion({ commit, dispatch, rootGetters }, selection) {
|
2017-12-17 15:08:52 +00:00
|
|
|
const loginToken = rootGetters['workspace/loginToken'];
|
2017-11-17 20:33:49 +00:00
|
|
|
if (!loginToken) {
|
2017-12-10 23:49:20 +00:00
|
|
|
dispatch('modal/signInForComment', {
|
|
|
|
onResolve: () => googleHelper.signin()
|
|
|
|
.then(() => syncSvc.requestSync())
|
|
|
|
.then(() => dispatch('createNewDiscussion', selection)),
|
|
|
|
}, { root: true })
|
2017-11-17 20:33:49 +00:00
|
|
|
.catch(() => { }); // Cancel
|
|
|
|
} else if (selection) {
|
2017-11-10 23:39:51 +00:00
|
|
|
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: {},
|
|
|
|
};
|
2017-12-10 23:49:20 +00:00
|
|
|
Object.entries(comments).forEach(([commentId, comment]) => {
|
2017-11-15 08:12:56 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
};
|