UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

109 lines (108 loc) 4.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRouteData = exports.getContributionRouteName = exports.getCommentContributionHtml = exports.getContributionHtml = exports.getContributionSnippet = exports.getContribution = exports.getContributionType = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_core_1 = require("@selfcommunity/react-core"); const types_1 = require("@selfcommunity/types"); const react_intl_1 = require("react-intl"); /** * From obj extract type of the contribution * @param obj */ function getContributionType(obj) { return types_1.SCContributionType.DISCUSSION in obj ? types_1.SCContributionType.DISCUSSION : types_1.SCContributionType.POST in obj ? types_1.SCContributionType.POST : types_1.SCContributionType.STATUS in obj ? types_1.SCContributionType.STATUS : types_1.SCContributionType.COMMENT in obj ? types_1.SCContributionType.COMMENT : null; } exports.getContributionType = getContributionType; /** * From obj extract the contribution * @param obj */ function getContribution(obj) { return types_1.SCContributionType.DISCUSSION in obj ? obj[types_1.SCContributionType.DISCUSSION] : types_1.SCContributionType.POST in obj ? obj[types_1.SCContributionType.POST] : types_1.SCContributionType.STATUS in obj ? obj[types_1.SCContributionType.STATUS] : types_1.SCContributionType.COMMENT in obj ? obj[types_1.SCContributionType.COMMENT] : null; } exports.getContribution = getContribution; /** * Get a snippet for a contribution * @param obj (Discussion, Post, Status, Comment) */ function getContributionSnippet(obj) { if (obj.type === types_1.SCContributionType.DISCUSSION) { return obj.summary ? (0, jsx_runtime_1.jsx)("span", { dangerouslySetInnerHTML: { __html: obj.summary } }) : obj.title; } else { return obj.summary ? ((0, jsx_runtime_1.jsx)("span", { dangerouslySetInnerHTML: { __html: obj.summary } })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: `ui.common.${obj.type.toLowerCase()}WithoutText`, defaultMessage: `ui.common.${obj.type.toLowerCase()}WithoutText` })); } } exports.getContributionSnippet = getContributionSnippet; /** * Get the contribution text * Hydrate text with mention, etc. * @param html Html of the contribution * @param handleUrl Func that handle urls */ function getContributionHtml(html, handleUrl) { return html.replace(/<mention.*? id="([0-9]+)"{1}.*?>@([a-z\d_-]+)<\/mention>/gi, (match, id, username) => { return `<a href='${handleUrl(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, { id, username })}'>@${username}</a>`; }); } exports.getContributionHtml = getContributionHtml; /** * Get the contribution text for comment object * Hydrate text with mention, etc. * @param html Html of the contribution * @param handleUrl Func that handle urls */ function getCommentContributionHtml(html, handleUrl) { return html.replace(/<mention.*? id="([0-9]+)"{1}.*?>@([a-z\d_-]+)<\/mention>/gi, (match, id, username) => { return `<a href='${handleUrl(react_core_1.SCRoutes.USER_PROFILE_ROUTE_NAME, { id, username })}'>@${username}</a>`; }); } exports.getCommentContributionHtml = getCommentContributionHtml; /** * Get route name for a contribution * @param obj */ function getContributionRouteName(obj) { if (!obj) return null; if (obj.type) { return react_core_1.SCRoutes[`${obj.type.toUpperCase()}_ROUTE_NAME`]; } return obj.type; } exports.getContributionRouteName = getContributionRouteName; /** * Get data for scRoutingContext.url() * @param obj (Discussion, Post, Status, Comment) */ function getRouteData(obj) { let data = {}; if (obj) { if (obj.type === types_1.SCContributionType.DISCUSSION || obj.type === types_1.SCContributionType.POST || obj.type === types_1.SCContributionType.STATUS) { data = Object.assign(Object.assign({}, obj), { contribution_type: obj.type, contribution_id: obj.id, contribution_slug: obj.slug }); } else if (obj.type === types_1.SCContributionType.COMMENT) { const contributionType = getContributionType(obj); const isContributionTypeObj = obj[contributionType] && typeof obj[contributionType] === 'object'; data = Object.assign(Object.assign({}, obj), { contribution_type: contributionType, contribution_id: isContributionTypeObj ? obj[contributionType].id : obj[contributionType], contribution_slug: isContributionTypeObj ? obj[contributionType].slug : contributionType }); } } return data; } exports.getRouteData = getRouteData;