Last active
January 13, 2023 13:49
-
-
Save Crenshinibon/ae72045e1613a80495f751a34106b4ce to your computer and use it in GitHub Desktop.
Integrating Trix into SvelteKit project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const prerender = false | |
export const ssr = false | |
export const actions = { | |
default: async ( {request}) => { | |
const data = await request.formData(); | |
const content = data.get("content") | |
return { | |
content | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { enhance } from '$app/forms'; | |
import 'trix'; | |
import 'trix/dist/trix.css'; | |
export let form; | |
export let initialContent = ""; | |
$: editorContent = form ? form.content : initialContent; | |
let trixEditor; | |
$: { | |
if(trixEditor) { | |
trixEditor.editor.setSelectedRange([0, Number.MAX_SAFE_INTEGER]) | |
trixEditor.editor.insertHTML(editorContent) | |
} | |
} | |
</script> | |
<form name="trix-form" method="POST" use:enhance> | |
<input id="trix-content" type="hidden" name="content" value={editorContent} > | |
<trix-editor | |
bind:this={trixEditor} | |
class="trix-content" | |
input="trix-content" | |
/> | |
<button type="submit">Save</button> | |
</form> | |
<div class="trix-content"> | |
{@html editorContent} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment