File size: 1,294 Bytes
7aec436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<script>
  import {onMount, onDestroy, tick} from 'svelte';
  import {_} from '../locales/index';

  export let message;
  export let values;
  let el;

  const build = () => {
    while (el.firstChild) {
      el.removeChild(el.firstChild);
    }
    const parts = message.split(/{(\w+)}/g);
    for (let i = 0; i < parts.length; i++) {
      const part = parts[i];
      const isText = i % 2 === 0;
      if (isText) {
        el.appendChild(document.createTextNode(part));
      } else {
        const value = values && values[part];
        if (value) {
          const node = document.createElement('a');
          node.href = value.href;
          node.textContent = value.text;
          if (value.newTab) {
            node.target = '_blank';
            node.rel = 'noopener noreferrer';
          }
          el.appendChild(node);
        } else {
          console.warn('Missing placeholder', part);
          el.appendChild(document.createTextNode(`???{${part}}`));
        }
      }
    }
  };

  const unsubscribe = _.subscribe(() => {
    if (el) {
      // message props don't get updated until after tick
      tick().then(build);
    }
  });

  onMount(build);
  onDestroy(unsubscribe);
</script>

<span bind:this={el}></span>