Text Editor - 图1

Text Editor Svelte Component

Text Editor Svelte component represents Text Editor component.

Text Editor Components

There are following components included:

  • TextEditor

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. buttons={[‘bold’,italic’, ‘hr’]}
  14. />
`

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 calling .instance() component’s method. For example:

  1. <TextEditor bind:this={component}>...</TextEditor>
  2. <script>
  3. let component;
  4. // to get instance in some method
  5. component.instance()
  6. </script>

Examples

  1. <Page>
  2. <Navbar title="Text Editor"></Navbar>
  3. <Block>
  4. <p>Framework7 comes with a touch-friendly Rich Text Editor component. It is based on modern "contenteditable" API so it should work everywhere as is.</p>
  5. <p>It comes with the basic set of formatting features. But its functionality can be easily extended and customized to fit any requirements.</p>
  6. </Block>
  7. <BlockTitle>Default Setup</BlockTitle>
  8. <TextEditor />
  9. <BlockTitle>With Placeholder</BlockTitle>
  10. <TextEditor
  11. placeholder="Enter text..."
  12. />
  13. <BlockTitle>With Default Value</BlockTitle>
  14. <TextEditor
  15. placeholder="Enter text..."
  16. value={customValue}
  17. onTextEditorChange={(value) => customValue = value}
  18. />
  19. <BlockTitle>Specific Buttons</BlockTitle>
  20. <BlockHeader>It is possible to customize which buttons (commands) to show.</BlockHeader>
  21. <TextEditor
  22. placeholder="Enter text..."
  23. buttons={[
  24. ['bold', 'italic', 'underline', 'strikeThrough'],
  25. ['orderedList', 'unorderedList']
  26. ]}
  27. />
  28. <BlockTitle>Custom Button</BlockTitle>
  29. <BlockHeader>It is possible to create custom editor buttons. Here is the custom "hr" button that adds horizontal rule:</BlockHeader>
  30. <TextEditor
  31. placeholder="Enter text..."
  32. customButtons={customButtons}
  33. buttons={[
  34. ['bold', 'italic', 'underline', 'strikeThrough'],
  35. 'hr'
  36. ]}
  37. />
  38. <BlockTitle>Resizable</BlockTitle>
  39. <BlockHeader>Editor will be resized based on its content.</BlockHeader>
  40. <TextEditor
  41. placeholder="Enter text..."
  42. resizable
  43. buttons={['bold', 'italic', 'underline', 'strikeThrough']}
  44. />
  45. <BlockTitle>Popover Mode</BlockTitle>
  46. <BlockHeader>In this mode, there is no toolbar with buttons, but they appear as popover when you select any text in editor.</BlockHeader>
  47. <TextEditor
  48. placeholder="Enter text..."
  49. mode="popover"
  50. buttons={['bold', 'italic', 'underline', 'strikeThrough']}
  51. style="--f7-text-editor-height: 150px"
  52. />
  53. <BlockTitle>Keyboard Toolbar Mode</BlockTitle>
  54. <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>
  55. <TextEditor
  56. placeholder="Enter text..."
  57. mode="keyboard-toolbar"
  58. style="--f7-text-editor-height: 150px"
  59. />
  60. <BlockTitle>As List Input</BlockTitle>
  61. <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>
  62. <List>
  63. <ListInput
  64. type="text"
  65. label="Name"
  66. placeholder="Your name"
  67. />
  68. <ListInput
  69. type="texteditor"
  70. label="About"
  71. placeholder="About"
  72. resizable
  73. textEditorParams={{
  74. mode: 'popover',
  75. buttons: ['bold', 'italic', 'underline', 'strikeThrough']
  76. }}
  77. value={listEditorValue}
  78. onTextEditorChange={(value) => listEditorValue = value}
  79. />
  80. </List>
  81. </Page>
  82. <script>
  83. import { Page, Navbar, BlockTitle, BlockHeader, Block, TextEditor, List, ListInput } from 'framework7-svelte';
  84. let customButtons = {
  85. hr: {
  86. content: '<hr>',
  87. onClick(editor, buttonEl) {
  88. document.execCommand('insertHorizontalRule', false);
  89. },
  90. },
  91. };
  92. let customValue = `<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur sunt, sapiente quis eligendi consectetur hic asperiores assumenda quidem dolore quasi iusto tenetur commodi qui ullam sint sed alias! Consequatur, dolor!</p>
  93. <p>Provident reiciendis exercitationem reprehenderit amet repellat laborum, sequi id quam quis quo quos facere veniam ad libero dolorum animi. Nobis, illum culpa explicabo dolorem vitae ut dolor at reprehenderit magnam?</p>
  94. <p>Qui, animi. Dolores dicta, nobis aut expedita enim eum assumenda modi, blanditiis voluptatibus excepturi non pariatur. Facilis fugit facere sequi molestias nemo in, suscipit inventore consequuntur, repellat perferendis, voluptas odit.</p>
  95. <p>Tempora voluptates, doloribus architecto eligendi numquam facilis perspiciatis autem quam voluptas maxime ratione harum laudantium cum deleniti. In, alias deserunt voluptatibus eligendi libero nobis est unde et perspiciatis cumque voluptatum.</p>
  96. <p>Quam error doloribus qui laboriosam eligendi. Aspernatur quam pariatur perspiciatis reprehenderit atque dicta culpa, aut rem? Assumenda, quibusdam? Reprehenderit necessitatibus facere nemo iure maiores porro voluptates accusamus quibusdam. Nesciunt, assumenda?</p>`;
  97. let listEditorValue = '';
  98. </script>

← Tabs

Toggle →