link.js 903 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // jshint ignore:start
  2. import React from 'react';
  3. import Action from './action';
  4. import isUrl from 'misago/utils/is-url';
  5. export default function(props) {
  6. return (
  7. <Action
  8. execAction={insertLink}
  9. title={gettext("Insert link")}
  10. {...props}
  11. >
  12. <span className="material-icon">
  13. insert_link
  14. </span>
  15. </Action>
  16. );
  17. }
  18. export function insertLink(selection, replace) {
  19. let url = '';
  20. let label = '';
  21. if (selection.length) {
  22. if (isUrl(selection)) {
  23. url = selection;
  24. } else {
  25. label = selection;
  26. }
  27. }
  28. url = $.trim(prompt(gettext("Enter link address") + ':', url) || '');
  29. if (url.length === 0) return false;
  30. label = $.trim(prompt(gettext("Enter link label (optional)") + ':', label));
  31. if (url.length) {
  32. if (label.length > 0) {
  33. replace('[' + label + '](' + url + ')');
  34. } else {
  35. replace(url);
  36. }
  37. }
  38. }