clientjs/packages/dashboard-components/src/utils.js
changeset 0 5f4fcbc80b37
child 4 df751568fda6
equal deleted inserted replaced
-1:000000000000 0:5f4fcbc80b37
       
     1 
       
     2 export const categoryColors = {
       
     3   topic: 'black',
       
     4   approval: '#00FF7F',
       
     5   disapproval: '#ff6666',
       
     6   question: '#3399ff',
       
     7   reference: '#FFFF66',
       
     8   important: '#abd9e9',
       
     9   keyword: '#c2a5cf',
       
    10   comment: '#abdda4',
       
    11   trouble: '#fdae61',
       
    12   call: '#007533',
       
    13   highlight: '#ffff3c',
       
    14 };
       
    15 
       
    16 export const categoryStyles = (cat) => {
       
    17   const color = categoryColors[cat];
       
    18   if (color) {
       
    19     return { backgroundColor: color, fontWeight: 'normal' };
       
    20   }
       
    21   return {};
       
    22 };
       
    23 
       
    24 export function getAnnotationStyle(annotation, metacategories) {
       
    25   let style = 'highlight';
       
    26   annotation.tags.some((tag) => {
       
    27     const tagStr = tag.startsWith('cat:') ? tag.substring(4) : tag;
       
    28     const tagStrLc = tagStr.toLowerCase();
       
    29     let metacategory = false;
       
    30     if (tag.startsWith('cat:')) {
       
    31       metacategory = metacategories.find(mc => mc.tag === tagStrLc);
       
    32     }
       
    33     if (metacategory) {
       
    34       style = metacategory.id;
       
    35       return true;
       
    36     }
       
    37     return false;
       
    38   });
       
    39   return categoryStyles(style);
       
    40 }
       
    41 
       
    42 export function isDefinitionInTags(tags) {
       
    43   return (
       
    44     tags.includes('définition')
       
    45     || tags.includes('definition')
       
    46     || tags.includes('act:définition')
       
    47     || tags.includes('act:definition')
       
    48     || tags.includes('act:def')
       
    49   );
       
    50 }
       
    51 
       
    52 // taken from https://github.com/disqus/disqus-react/blob/master/src/utils.js
       
    53 export function insertScript(src, id, parentElement, attrs = {}) {
       
    54   const script = window.document.createElement('script');
       
    55   script.async = true;
       
    56   script.src = src;
       
    57   script.id = id;
       
    58   Object.keys(attrs).forEach((k) => {
       
    59     script.setAttribute(k, attrs[k]);
       
    60   });
       
    61   parentElement.appendChild(script);
       
    62 
       
    63   return script;
       
    64 }
       
    65 
       
    66 // take from https://github.com/disqus/disqus-react/blob/master/src/utils.js
       
    67 export function removeScript(id, parentElement) {
       
    68   const script = window.document.getElementById(id);
       
    69   if (script) {
       
    70     parentElement.removeChild(script);
       
    71   }
       
    72 }
       
    73 
       
    74 export function getTerms(annotations) {
       
    75   return annotations.reduce((res, annotation) => {
       
    76     const tags = annotation.tags.map(t => t.toLowerCase());
       
    77 
       
    78     if (!tags.includes('cat:mot-clef')) {
       
    79       return res;
       
    80     }
       
    81 
       
    82     let term;
       
    83 
       
    84     (annotation.target[0].selector || []).some((selector) => {
       
    85       if (selector.type === 'TextQuoteSelector' && selector.exact) {
       
    86         term = selector.exact.trim().toLowerCase();
       
    87         return true;
       
    88       }
       
    89       return false;
       
    90     });
       
    91 
       
    92     if (term) {
       
    93       res[term] = (res[term] || []);
       
    94       res[term].push(annotation);
       
    95     }
       
    96     return res;
       
    97   }, {});
       
    98 }
       
    99 
       
   100 export function toBase64(msg) {
       
   101   return Buffer.from(msg).toString('base64');
       
   102 }