Text Editor Svelte 元件

Text Editor Svelte 元件代表 Text Editor 元件。

Text Editor 元件

包含下列元件

Text Editor 屬性

屬性類型預設值說明
<TextEditor> 屬性
value字串

Text editor 初始 html 內容值。

placeholder字串編輯器 placeholder 內容,在編輯器為空時顯示。預設未指定
resizable布林值false讓編輯器可調整大小(其高度會符合其內容)
mode字串工具列

Text editor 按鈕模式。可以是

  • toolbar - 會在 text editor 容器元素中加入包含編輯器按鈕的工具列
  • popover - 會在編輯器中選取文字上方顯示包含編輯器按鈕的 popover 泡泡
  • keyboard-toolbar - 當編輯器獲得焦點時,包含編輯器按鈕的工具列會出現在虛擬鍵盤上方。僅支援 iOS、Android cordova 應用程式和 Android Chrome。不支援時會改為 popover 模式。
buttons陣列

包含編輯器按鈕的陣列,或包含編輯器按鈕的陣列陣列(群組)。預設所有按鈕都已啟用,其預設值為

[
  ['bold', 'italic', 'underline', 'strikeThrough'],
  ['orderedList', 'unorderedList'],
  ['link', 'image'],
  ['paragraph', 'h1', 'h2', 'h3'],
  ['alignLeft', 'alignCenter', 'alignRight', 'alignJustify'],
  ['subscript', 'superscript'],
  ['indent', 'outdent'],
]
dividers布林值true在按鈕群組之間加入視覺分隔線
imageUrlText字串插入圖片 URL在圖片 URL 要求上顯示的提示文字
linkUrlText字串插入連結 URL在連結 URL 要求上顯示的提示文字
clearFormattingOnPaste布林值true啟用時,會清除從剪貼簿貼上的任何格式
customButtons物件

包含自訂按鈕的物件。物件屬性金鑰是應在 buttons 中用於啟用按鈕的按鈕 ID。

例如,要指定會加入 <hr> 的自訂按鈕,我們可以使用下列宣告

<TextEditor
  customButtons={{
    // property key is the button id
    hr: {
      // button html content
      content: '&lt;hr&gt;',
      // button click handler
      onClick(event, buttonEl) {
        document.execCommand('insertHorizontalRule', false);
      }
    }
  }}
  buttons={['bold', 'italic', 'hr']}
/>
`

Text Editor 事件

事件引數說明
<TextEditor> 事件
textEditorChange(value)當編輯器值已變更時,會觸發事件
textEditorInput會在編輯器的內容「input」事件上觸發事件
textEditorFocus會在編輯器的內容焦點上觸發事件
textEditorBlur會在編輯器的內容模糊上觸發事件
textEditorButtonClick(buttonId)會在編輯器按鈕按一下時觸發事件
textEditorKeyboardOpen當編輯器鍵盤工具列出現時,將觸發事件
textEditorKeyboardClose當編輯器鍵盤工具列消失時,將觸發事件
textEditorPopoverOpen當編輯器彈出視窗開啟時,將觸發事件
textEditorPopoverClose當編輯器彈出視窗關閉時,將觸發事件
textEditorBeforeDestroy在 Text Editor 實例被銷毀之前,將觸發事件

存取 Text Editor 實例

如果您需要使用 Text Editor API,您可以透過呼叫元件的 .instance() 方法來存取其初始化的實例。例如

<TextEditor bind:this={component}>...</TextEditor>

<script>
  let component;

  // to get instance in some method
  component.instance()
</script>


範例

text-editor.svelte
<script>
  import {
    Page,
    Navbar,
    BlockTitle,
    BlockHeader,
    Block,
    TextEditor,
    List,
    ListInput,
  } from 'framework7-svelte';

  const customButtons = {
    hr: {
      content: '<hr>',
      // eslint-disable-next-line
      onClick(editor, buttonEl) {
        document.execCommand('insertHorizontalRule', false);
      },
    },
  };

  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>
      <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>
      <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>
      <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>
      <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>`;

  let listEditorValue = '';
</script>

<Page>
  <Navbar title="Text Editor" />

  <Block>
    <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>
    <p>
      It comes with the basic set of formatting features. But its functionality can be easily
      extended and customized to fit any requirements.
    </p>
  </Block>

  <BlockTitle>Default Setup</BlockTitle>
  <TextEditor />

  <BlockTitle>With Placeholder</BlockTitle>
  <TextEditor placeholder="Enter text..." />

  <BlockTitle>With Default Value</BlockTitle>
  <TextEditor
    placeholder="Enter text..."
    value={customValue}
    onTextEditorChange={(value) => (customValue = value)}
  />

  <BlockTitle>Specific Buttons</BlockTitle>
  <BlockHeader>It is possible to customize which buttons (commands) to show.</BlockHeader>
  <TextEditor
    placeholder="Enter text..."
    buttons={[
      ['bold', 'italic', 'underline', 'strikeThrough'],
      ['orderedList', 'unorderedList'],
    ]}
  />

  <BlockTitle>Custom Button</BlockTitle>
  <BlockHeader>
    It is possible to create custom editor buttons. Here is the custom "hr" button that adds
    horizontal rule:
  </BlockHeader>
  <TextEditor
    placeholder="Enter text..."
    {customButtons}
    buttons={[['bold', 'italic', 'underline', 'strikeThrough'], 'hr']}
  />

  <BlockTitle>Resizable</BlockTitle>
  <BlockHeader>Editor will be resized based on its content.</BlockHeader>
  <TextEditor
    placeholder="Enter text..."
    resizable
    buttons={['bold', 'italic', 'underline', 'strikeThrough']}
  />

  <BlockTitle>Popover Mode</BlockTitle>
  <BlockHeader>
    In this mode, there is no toolbar with buttons, but they appear as popover when you select any
    text in editor.
  </BlockHeader>
  <TextEditor
    placeholder="Enter text..."
    mode="popover"
    buttons={['bold', 'italic', 'underline', 'strikeThrough']}
    style="--f7-text-editor-height: 150px"
  />

  <BlockTitle>Keyboard Toolbar Mode</BlockTitle>
  <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>
  <TextEditor
    placeholder="Enter text..."
    mode="keyboard-toolbar"
    style="--f7-text-editor-height: 150px"
  />

  <BlockTitle>As List Input</BlockTitle>
  <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>
  <List strongIos dividersIos outlineIos>
    <ListInput type="text" label="Name" placeholder="Your name" />
    <ListInput
      type="texteditor"
      label="About"
      placeholder="About"
      resizable
      textEditorParams={{
        mode: 'popover',
        buttons: ['bold', 'italic', 'underline', 'strikeThrough'],
      }}
      value={listEditorValue}
      onTextEditorChange={(value) => (listEditorValue = value)}
    />
  </List>
</Page>