CopyMultilineValue.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (C) 2018 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled from 'styled-components'
  17. import CopyButton from '@src/components/ui/CopyButton'
  18. import DomUtils from '@src/utils/DomUtils'
  19. import notificationStore from '@src/stores/NotificationStore'
  20. const CopyButtonStyled = styled(CopyButton)`
  21. background-position-y: 4px;
  22. margin-left: 4px;
  23. `
  24. const Wrapper = styled.div<any>`
  25. cursor: pointer;
  26. word-break: break-word;
  27. &:hover ${CopyButtonStyled} {
  28. opacity: 1;
  29. }
  30. `
  31. type Props = {
  32. value: string | null | undefined,
  33. onCopy?: (value: string) => void,
  34. useDangerousHtml?: boolean,
  35. }
  36. @observer
  37. class CopyMultineValue extends React.Component<Props> {
  38. handleCopy() {
  39. let { value } = this.props
  40. if (!value) {
  41. return
  42. }
  43. if (this.props.useDangerousHtml) {
  44. value = value.replace(/<br\s*\/>/g, '\n').replace(/<.*?>/g, '')
  45. }
  46. const succesful = DomUtils.copyTextToClipboard(value)
  47. if (this.props.onCopy) this.props.onCopy(value)
  48. if (succesful) {
  49. notificationStore.alert('The message has been copied to clipboard.')
  50. }
  51. }
  52. render() {
  53. let text: React.ReactNode = this.props.value
  54. if (this.props.useDangerousHtml) {
  55. text = <span dangerouslySetInnerHTML={{ __html: text as string }} />
  56. }
  57. return (
  58. <Wrapper
  59. onClick={() => { this.handleCopy() }}
  60. >
  61. {text}
  62. <CopyButtonStyled />
  63. </Wrapper>
  64. )
  65. }
  66. }
  67. export default CopyMultineValue