withLinks.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { Transforms, Editor, Range, Element } from 'slate'
  21. import { ReactEditor } from 'slate-react'
  22. import { isUrl } from 'utils/util'
  23. import { ElementTypes } from '../constants'
  24. const unwrapLink = (editor: Editor) => {
  25. Transforms.unwrapNodes(editor, { match: (n) => n.type === ElementTypes.Link })
  26. }
  27. const wrapLink = (editor: ReactEditor, url: string) => {
  28. const [existedLink] = Editor.nodes(editor, {
  29. match: (n) => n.type === ElementTypes.Link
  30. })
  31. if (existedLink) {
  32. unwrapLink(editor)
  33. }
  34. const { selection } = editor
  35. const isCollapsed = selection && Range.isCollapsed(selection)
  36. const link: Element = {
  37. type: ElementTypes.Link,
  38. url,
  39. children: isCollapsed ? [{ text: url }] : []
  40. }
  41. if (isCollapsed) {
  42. Transforms.insertNodes(editor, link)
  43. } else {
  44. Transforms.wrapNodes(editor, link, { split: true })
  45. Transforms.collapse(editor, { edge: 'end' })
  46. }
  47. }
  48. const withLinks = (editor: ReactEditor) => {
  49. const { insertData, insertText, isInline } = editor
  50. editor.isInline = (element) => {
  51. return element.type === ElementTypes.Link ? true : isInline(element)
  52. }
  53. editor.insertText = (text) => {
  54. if (text && isUrl(text)) {
  55. wrapLink(editor, text)
  56. } else {
  57. insertText(text)
  58. }
  59. }
  60. editor.insertData = (data) => {
  61. const text = data.getData('text/plain')
  62. if (text && isUrl(text)) {
  63. wrapLink(editor, text)
  64. } else {
  65. insertData(data)
  66. }
  67. }
  68. return editor
  69. }
  70. export default withLinks