parent
ddc636a684
commit
cd56975094
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* @Description: quill delta -> html
|
||||
* @Author: tangjiang
|
||||
* @Github:
|
||||
* @Date: 2019-12-24 08:51:25
|
||||
* @LastEditors : tangjiang
|
||||
* @LastEditTime : 2019-12-24 10:45:44
|
||||
*/
|
||||
export const formatDelta = (deltas) => {
|
||||
|
||||
let formatted = [];
|
||||
|
||||
deltas.forEach(element => {
|
||||
let text = null;
|
||||
// 没有图片时
|
||||
if (!element['insert']['image']) {
|
||||
text = element['insert']; // 获取插入的内容
|
||||
// 元素有属性时
|
||||
if (element['attributes']) {
|
||||
// 获取所有的key值
|
||||
const keys = Object.keys(element['attributes']);
|
||||
keys.forEach(key => {
|
||||
text = operate(text, key, element['attributes'][key]);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const image = element['insert']['image'];
|
||||
const {url, alt} = image;
|
||||
if (url && (url.startsWith('http') || url.startsWith('https'))) {
|
||||
text = `
|
||||
<img
|
||||
src="${url}"
|
||||
style="{display: 'inline-block'}"
|
||||
width="60px"
|
||||
height="30px"
|
||||
alt="${alt}"
|
||||
/>
|
||||
`;
|
||||
// text = "<img src="+url+" width='60px' height='30px' onclick='' alt="+alt+"/>";
|
||||
}
|
||||
}
|
||||
|
||||
formatted.push(text);
|
||||
});
|
||||
|
||||
return formatted.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {*} text 文本内容
|
||||
* @param {*} key 属性key
|
||||
* @param {*} value 属性key对应的值
|
||||
*/
|
||||
export const operate = (text, key, value) => {
|
||||
let operatedText = null;
|
||||
|
||||
switch (key) {
|
||||
case 'bold':
|
||||
operatedText = `<strong>${text}</strong>`;
|
||||
break;
|
||||
case 'italic':
|
||||
operatedText = `<i>${text}</i>`;
|
||||
break;
|
||||
case 'strike':
|
||||
operatedText = `<s>${text}</s>`;
|
||||
break;
|
||||
case 'underline':
|
||||
operatedText = `<u>${text}</u>`;
|
||||
break;
|
||||
case 'link':
|
||||
operatedText = `<a href="${value}" target="blank">${text}</a>`;
|
||||
break;
|
||||
default:
|
||||
operatedText = text;
|
||||
}
|
||||
|
||||
return operatedText;
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Author: tangjiang
|
||||
* @Github:
|
||||
* @Date: 2019-12-23 10:53:25
|
||||
* @LastEditors : tangjiang
|
||||
* @LastEditTime : 2019-12-24 16:17:00
|
||||
*/
|
||||
import types from "./actionTypes";
|
||||
|
||||
import {
|
||||
fetchAddComment,
|
||||
fetchCommentLists,
|
||||
fetchAddChildComment,
|
||||
fetchDeleteComment,
|
||||
fetchLikeComment,
|
||||
fetchShowOrHideComment
|
||||
} from '../../services/commentService';
|
||||
|
||||
// 添加评论
|
||||
export const addComment = (identifier, comments) => {
|
||||
return (dispatch) => {
|
||||
fetchAddComment(identifier, comments).then(res => {
|
||||
if (res.status === 200) {
|
||||
// 重新加载评论列表
|
||||
dispatch(getCommentLists(identifier));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 获取评论列表
|
||||
export const getCommentLists = (identifier) => {
|
||||
return (dispatch) => {
|
||||
fetchCommentLists(identifier).then(res => {
|
||||
console.log('获取评论列表: ====>>>>', res);
|
||||
if (res.status === 200) {
|
||||
const {data} = res;
|
||||
dispatch({
|
||||
type: types.GET_COMMENT_LISTS,
|
||||
payload: data
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 子回复
|
||||
export const replayChildComment = (identifier, comment) => {
|
||||
return (dispatch) => {
|
||||
fetchAddChildComment(identifier, comment).then(res => {
|
||||
// console.log('添加子评论成功: ====>>>>', res);
|
||||
if (res.status === 200) {
|
||||
// 重新加载评论列表
|
||||
dispatch(getCommentLists(identifier));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 删除评论
|
||||
export const deleteComment = (identifier, delId) => {
|
||||
return (dispatch) => {
|
||||
fetchDeleteComment(identifier, delId).then(res => {
|
||||
if (res.status === 200) {
|
||||
// 重新加载评论列表
|
||||
dispatch(getCommentLists(identifier));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 点赞
|
||||
export const likeComment = (identifier, id, params, cb) => {
|
||||
return (dispatch) => {
|
||||
fetchLikeComment(id, params).then(res => {
|
||||
if (res.status === 200) {
|
||||
// 重新加载评论列表
|
||||
const {container_type} = params;
|
||||
// if (container_type === 'Discuss') {
|
||||
// dispatch(getCommentLists(identifier))
|
||||
// } else if {
|
||||
// }
|
||||
const {praise_count} = res.data;
|
||||
switch (container_type) {
|
||||
case 'Discuss':
|
||||
dispatch(getCommentLists(identifier))
|
||||
break;
|
||||
case 'Hack':
|
||||
dispatch({
|
||||
type: types.ADD_OJ_LIKE_COUNT,
|
||||
payload: praise_count
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 显示或隐藏评论
|
||||
export const showOrHideComment = (identifier, id, params) => {
|
||||
return (dispatch) => {
|
||||
fetchShowOrHideComment(identifier, id, params).then(res => {
|
||||
if (res.status === 200) {
|
||||
// 重新加载评论列表
|
||||
dispatch(getCommentLists(identifier));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
import types from "../actions/actionTypes";
|
||||
|
||||
/*
|
||||
* @Description: 评论reducer
|
||||
* @Author: tangjiang
|
||||
* @Github:
|
||||
* @Date: 2019-12-23 10:35:31
|
||||
* @LastEditors : tangjiang
|
||||
* @LastEditTime : 2019-12-23 14:51:42
|
||||
*/
|
||||
const initialState = {
|
||||
comments: {
|
||||
content: '' // 评论内容
|
||||
},
|
||||
commentLists: {}, // 评论列表
|
||||
pages: {
|
||||
limit: 20,
|
||||
page: 1
|
||||
}
|
||||
};
|
||||
|
||||
const commentReducer = (state = initialState, action) => {
|
||||
|
||||
const { payload, type } = action;
|
||||
switch (type) {
|
||||
case types.ADD_COMMENTS:
|
||||
return {
|
||||
...state
|
||||
}
|
||||
case types.GET_COMMENT_LISTS:
|
||||
return {
|
||||
...state,
|
||||
commentLists: Object.assign({}, payload)
|
||||
}
|
||||
default:
|
||||
return {
|
||||
...state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default commentReducer;
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @Description: 评论 service
|
||||
* @Author: tangjiang
|
||||
* @Github:
|
||||
* @Date: 2019-12-23 10:43:27
|
||||
* @LastEditors : tangjiang
|
||||
* @LastEditTime : 2019-12-24 17:10:49
|
||||
*/
|
||||
import axios from 'axios';
|
||||
|
||||
// 添加评论
|
||||
export async function fetchAddComment (identifier, params) {
|
||||
const url = `/problems/${identifier}/comments.json`;
|
||||
return axios.post(url, params);
|
||||
}
|
||||
|
||||
// 获取评论列表
|
||||
export async function fetchCommentLists (identifier) {
|
||||
const url = `/problems/${identifier}/comments.json`;
|
||||
return axios.get(url);
|
||||
}
|
||||
|
||||
// 添加子评论
|
||||
export async function fetchAddChildComment (identifier, params) {
|
||||
const url = `/problems/${identifier}/comments/reply.json`;
|
||||
return axios.post(url, params);
|
||||
}
|
||||
|
||||
// 删除评论
|
||||
export async function fetchDeleteComment (identifier, id) {
|
||||
const url = `/problems/${identifier}/comments/${id}.json`;
|
||||
return axios.delete(url);
|
||||
}
|
||||
|
||||
// 点赞
|
||||
export async function fetchLikeComment (id, params) {
|
||||
const url = `/discusses/${id}/plus.json`;
|
||||
return axios.post(url, params);
|
||||
}
|
||||
|
||||
// 显示或隐藏
|
||||
export async function fetchShowOrHideComment (identifier, id, params) {
|
||||
const url = `/problems/${identifier}/comments/${id}/hidden.json`;
|
||||
return axios.post(url, params);
|
||||
}
|
Loading…
Reference in new issue