tiptap/docs/src/docPages/installation/svelte.md

56 lines
1.2 KiB
Markdown
Raw Normal View History

# Svelte
2021-01-30 05:05:47 +08:00
## toc
## Introduction
The following guide describes how to integrate tiptap with your Svelte project.
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'
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-03-08 20:20:26 +08:00
onDestroy(() => {
editor.destroy()
})
</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
</button>
2021-03-08 20:20:26 +08:00
<button on:click={() => editor.chain().focus().toggleItalic().run()} class:active={editor.isActive('italic')}>
Italic
</button>
2021-03-08 20:20:26 +08:00
<button on:click={() => editor.chain().focus().toggleStrike().run()} class:active={editor.isActive('strike')}>
Strike
</button>
{/if}
<div bind:this={element} />
2021-01-30 05:05:47 +08:00
```