2021-01-25 18:05:19 +08:00
|
|
|
# Svelte
|
|
|
|
|
2021-01-30 05:05:47 +08:00
|
|
|
## toc
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
The following guide describes how to integrate tiptap with your Svelte project.
|
|
|
|
|
2021-01-25 18:05:19 +08:00
|
|
|
TODO
|
|
|
|
|
|
|
|
Svelte REPL: https://svelte.dev/repl/c839da77db2444e5b23a752266613639?version=3.31.2
|
|
|
|
|
|
|
|
App.svelte
|
|
|
|
```html
|
|
|
|
<script>
|
|
|
|
import Editor from './Editor.svelte';
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<Editor />
|
|
|
|
```
|
|
|
|
|
|
|
|
Editor.svelte
|
|
|
|
```html
|
2021-01-30 05:05:47 +08:00
|
|
|
<script type="module">
|
2021-03-08 20:20:26 +08:00
|
|
|
import { onMount, onDestroy } from 'svelte'
|
|
|
|
import { Editor } from '@tiptap/core'
|
2021-01-25 18:05:19 +08:00
|
|
|
import { defaultExtensions } from '@tiptap/starter-kit'
|
|
|
|
let element
|
|
|
|
let editor
|
|
|
|
onMount(() => {
|
|
|
|
editor = new Editor({
|
|
|
|
element: element,
|
|
|
|
extensions: defaultExtensions(),
|
2021-03-08 20:20:26 +08:00
|
|
|
onTransaction: () => {
|
|
|
|
// force re-render so `editor.isActive` works as expected
|
|
|
|
editor = editor
|
|
|
|
},
|
2021-01-25 18:05:19 +08:00
|
|
|
})
|
|
|
|
})
|
2021-03-08 20:20:26 +08:00
|
|
|
onDestroy(() => {
|
|
|
|
editor.destroy()
|
|
|
|
})
|
2021-01-25 18:05:19 +08:00
|
|
|
</script>
|
|
|
|
{#if editor}
|
2021-03-08 20:20:26 +08:00
|
|
|
<button on:click={() => editor.chain().focus().toggleBold().run()} class:active={editor.isActive('bold')}>
|
|
|
|
Bold
|
2021-01-25 18:05:19 +08:00
|
|
|
</button>
|
2021-03-08 20:20:26 +08:00
|
|
|
<button on:click={() => editor.chain().focus().toggleItalic().run()} class:active={editor.isActive('italic')}>
|
|
|
|
Italic
|
2021-01-25 18:05:19 +08:00
|
|
|
</button>
|
2021-03-08 20:20:26 +08:00
|
|
|
<button on:click={() => editor.chain().focus().toggleStrike().run()} class:active={editor.isActive('strike')}>
|
|
|
|
Strike
|
2021-01-25 18:05:19 +08:00
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
<div bind:this={element} />
|
2021-01-30 05:05:47 +08:00
|
|
|
```
|