Link.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 React, { useContext, useCallback, useRef, useState } from 'react'
  21. import { EditorContext } from '../context'
  22. import { ElementTypes } from '../Element'
  23. import { Icon, Popover, Form, Input, Button } from 'antd'
  24. import { FormItemProps, FormComponentProps } from 'antd/lib/form'
  25. import { WrappedFormUtils } from 'antd/lib/form/Form'
  26. const formItemStyle: Partial<FormItemProps> = {
  27. labelCol: { span: 8 },
  28. wrapperCol: { span: 16 }
  29. }
  30. const tailStyle: Partial<FormItemProps> = {
  31. wrapperCol: { offset: 8, span: 16 }
  32. }
  33. const Link: React.FC = () => {
  34. const form = useRef<WrappedFormUtils>()
  35. const { insertElement } = useContext(EditorContext)
  36. const [visible, setVisible] = useState(false)
  37. const openPopover = useCallback(() => {
  38. setVisible(true)
  39. }, [])
  40. const insertLink = useCallback(
  41. ({ href, text }) => {
  42. insertElement(ElementTypes.Link, href, [{ text }])
  43. setVisible(false)
  44. form.current.resetFields()
  45. },
  46. [insertElement]
  47. )
  48. return (
  49. <Popover
  50. visible={visible}
  51. content={<LinkFormWrapper ref={form} onSave={insertLink} />}
  52. >
  53. <Icon
  54. type="link"
  55. className="richtext-toolbar-item"
  56. onClick={openPopover}
  57. />
  58. </Popover>
  59. )
  60. }
  61. export default Link
  62. interface ILinkFormProps extends FormComponentProps {
  63. onSave: (values: { text: string; href: string }) => void
  64. }
  65. const LinkForm: React.FC<ILinkFormProps> = (props, ref) => {
  66. const { form, onSave } = props
  67. const { getFieldDecorator } = form
  68. const saveLink = useCallback(() => {
  69. form.validateFieldsAndScroll((err, values) => {
  70. if (err) {
  71. return
  72. }
  73. onSave(values)
  74. })
  75. }, [])
  76. React.useImperativeHandle(ref, () => ({ form }))
  77. return (
  78. <Form>
  79. <Form.Item {...formItemStyle} label="链接名称">
  80. {getFieldDecorator('text', {
  81. rules: [{ required: true, message: '链接名称不能为空' }]
  82. })(<Input size="small" autoFocus />)}
  83. </Form.Item>
  84. <Form.Item {...formItemStyle} label="链接地址">
  85. {getFieldDecorator('href', {
  86. rules: [
  87. { required: true, message: '链接地址不能为空' },
  88. {
  89. type: 'url',
  90. message: '链接地址不正确'
  91. }
  92. ]
  93. })(<Input size="small" />)}
  94. </Form.Item>
  95. <Form.Item {...tailStyle}>
  96. <Button type="primary" size="small" onClick={saveLink}>
  97. 插入
  98. </Button>
  99. </Form.Item>
  100. </Form>
  101. )
  102. }
  103. const LinkFormWrapper = Form.create<ILinkFormProps>()(
  104. React.forwardRef<WrappedFormUtils>(LinkForm)
  105. )