Text Editor - 图1

Text Editor React Component

Text Editor React component represents Text Editor component.

Text Editor Components

There are following components included:

  • TextEditor / F7TextEditor

Text Editor Properties

PropTypeDefaultDescription
<TextEditor> properties
valuestring

Text editor initial html content value.

placeholderstringEditor placeholder content displayed when it is empty. By default it is not specified
resizablebooleanfalseMakes editor resizable (when its height will fit to its content)
modestringtoolbar

Text editor buttons mode. Can be:

  • toolbar - it will add toolbar with editor buttons in text editor container element
  • popover - it will show popover bubble with editor buttons on top of the selected text in editor
  • keyboard-toolbar - toolbar with editor buttons will appear on top of virtual keyboard when editor is in the focus. It is supported only in iOS, Android cordova apps and in Android Chrome. When not supported it will fallback to popover mode.
buttonsarray

Array with editor buttons, or array of arrays (groups) with editor buttons. By default all buttons enabled and its default value is:

  1. [
  2. [‘bold’, italic’, underline’, strikeThrough’],
  3. [‘orderedList’, unorderedList’],
  4. [‘link’, image’],
  5. [‘paragraph’, h1’, h2’, h3’],
  6. [‘alignLeft’, alignCenter’, alignRight’, alignJustify’],
  7. [‘subscript’, superscript’],
  8. [‘indent’, outdent’],
  9. ]
dividersbooleantrueAdds visual divider between buttons group
imageUrlTextstringInsert image URLPrompt text that appears on image url request
linkUrlTextstringInsert link URLPrompt text that appears on link url request
clearFormattingOnPastebooleantrueWhen enabled it will clear any formatting on paste from clipboard
customButtonsobject

Object with custom buttons. Object property key is the button id that should be used in buttons to enable it.

For example to specify custom button that will add <hr> we can use following declaration:

  1. <TextEditor
  2. customButtons={{
  3. // property key is the button id
  4. hr: {
  5. // button html content
  6. content: ‘&lt;hr&gt;’,
  7. // button click handler
  8. onClick(event, buttonEl) {
  9. document.execCommand(‘insertHorizontalRule’, false);
  10. }
  11. }
  12. }}
  13. {/ now we use custom button idhrto add it to buttons /}
  14. buttons={[‘bold’,italic’, ‘hr’]}
  15. />
`

Text Editor Events

EventArgumentsDescription
<TextEditor> events
textEditorChange(value)Event will be triggered when editor value has been changed
textEditorInputEvent will be triggered on editor’s content “input” event
textEditorFocusEvent will be triggered on editor’s content focus
textEditorBlurEvent will be triggered on editor’s content blur
textEditorButtonClick(buttonId)Event will be triggered on editor button click
textEditorKeyboardOpenEvent will be triggered when editor keyboard toolbar appears
textEditorKeyboardCloseEvent will be triggered when editor keyboard toolbar disappears
textEditorPopoverOpenEvent will be triggered on editor popover open
textEditorPopoverCloseEvent will be triggered on editor popover close
textEditorBeforeDestroyEvent will be triggered right before Text Editor instance will be destroyed

Access To Text Editor Instance

If you need to use Text Editor API you can access its initialized instance by accessing .f7TextEditor component’s property.

Examples

  1. export default class extends React.Component {
  2. constructor() {
  3. super();
  4. this.state = {
  5. customButtons: {
  6. hr: {
  7. content: '&lt;hr&gt;',
  8. onClick(editor, buttonEl) {
  9. document.execCommand('insertHorizontalRule', false);
  10. },
  11. },
  12. },
  13. customValue: '<p>Lorem, ipsum dolor sit amet consectetur adipisicing...</p>',
  14. listEditorValue: '',
  15. };
  16. }
  17. render() {
  18. return (
  19. <Page>
  20. <Navbar title="Text Editor"></Navbar>
  21. <BlockTitle>Default Setup</BlockTitle>
  22. <TextEditor />
  23. <BlockTitle>With Placeholder</BlockTitle>
  24. <TextEditor
  25. placeholder="Enter text..."
  26. />
  27. <BlockTitle>With Default Value</BlockTitle>
  28. <TextEditor
  29. placeholder="Enter text..."
  30. value={this.state.customValue}
  31. onTextEditorChange={(value) => this.setState({ customValue: value })}
  32. />
  33. <BlockTitle>Specific Buttons</BlockTitle>
  34. <BlockHeader>It is possible to customize which buttons (commands) to show.</BlockHeader>
  35. <TextEditor
  36. placeholder="Enter text..."
  37. buttons={[
  38. ['bold', 'italic', 'underline', 'strikeThrough'],
  39. ['orderedList', 'unorderedList']
  40. ]}
  41. />
  42. <BlockTitle>Custom Button</BlockTitle>
  43. <BlockHeader>It is possible to create custom editor buttons. Here is the custom "hr" button that adds horizontal rule:</BlockHeader>
  44. <TextEditor
  45. placeholder="Enter text..."
  46. customButtons={this.state.customButtons}
  47. buttons={[
  48. ['bold', 'italic', 'underline', 'strikeThrough'],
  49. 'hr'
  50. ]}
  51. />
  52. <BlockTitle>Resizable</BlockTitle>
  53. <BlockHeader>Editor will be resized based on its content.</BlockHeader>
  54. <TextEditor
  55. placeholder="Enter text..."
  56. resizable
  57. buttons={['bold', 'italic', 'underline', 'strikeThrough']}
  58. />
  59. <BlockTitle>Popover Mode</BlockTitle>
  60. <BlockHeader>In this mode, there is no toolbar with buttons, but they appear as popover when you select any text in editor.</BlockHeader>
  61. <TextEditor
  62. placeholder="Enter text..."
  63. mode="popover"
  64. buttons={['bold', 'italic', 'underline', 'strikeThrough']}
  65. style={{'--f7-text-editor-height': '150px'}}
  66. />
  67. <BlockTitle>Keyboard Toolbar Mode</BlockTitle>
  68. <BlockHeader>In this mode, toolbar with buttons will appear on top of virtual keyboard when editor is in the focus. It is supported only in iOS, Android cordova apps and in Android Chrome. When not supported it will fallback to "popover" mode.</BlockHeader>
  69. <TextEditor
  70. placeholder="Enter text..."
  71. mode="keyboard-toolbar"
  72. style={{'--f7-text-editor-height': '150px'}}
  73. />
  74. <BlockTitle>As List Input</BlockTitle>
  75. <BlockHeader>Text editor can be used in list with other inputs. In this example it is enabled with "keyboard-toolbar"/"popover" type for "About" field.</BlockHeader>
  76. <List>
  77. <ListInput
  78. type="text"
  79. label="Name"
  80. placeholder="Your name"
  81. />
  82. <ListInput
  83. type="texteditor"
  84. label="About"
  85. placeholder="About"
  86. resizable
  87. textEditorParams={{
  88. mode: 'popover',
  89. buttons: ['bold', 'italic', 'underline', 'strikeThrough']
  90. }}
  91. value={this.state.listEditorValue}
  92. onTextEditorChange={(value) => this.setState({listEditorValue: value})}
  93. />
  94. </List>
  95. </Page>
  96. );
  97. }
  98. }

← Tabs

Toggle →