IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1KB

  1. import { h, Component } from 'preact';
  2. import WBIdTools from 'wb-id-tools';
  3. import urlForObject from 'url-for-object';
  4. function detectIds(value) {
  5. const matches = WBIdTools.detectIdentifiers(value);
  6. matches.sort((a, b) => (a.index - b.index));
  7. const res = [];
  8. let ofs = 0;
  9. for (let i = 0; i < matches.length; i++) {
  10. const { index } = matches[i];
  11. const id = matches[i][0];
  12. const typeName = WBIdTools.typeName(id);
  13. const url = (typeName === 'group' ? '/browse/' + id :
  14. typeName === 'collection' ? '/collection-browse/' + id :
  15. urlForObject({ uuid: id }));
  16. res.push(value.substring(ofs, index));
  17. res.push(h('a', { href: url }, id));
  18. ofs = index + id.length;
  19. }
  20. res.push(value.substring(ofs));
  21. return res;
  22. }
  23. class WBJsonViewer extends Component {
  24. render({ value, stringify }) {
  25. if (stringify)
  26. value = JSON.stringify(value, null, 2);
  27. return (
  28. <div class="wb-json-viewer">{ detectIds(value) }</div>
  29. );
  30. }
  31. }
  32. WBJsonViewer.defaultProps = {
  33. stringify: true
  34. };
  35. export default WBJsonViewer;