content-editable
Semantic "contenteditable" component
Basically, this svelte component just creates a div
element and handles it's contenteditable behaviour:
- wraps texts between line breaks in paragraph elements (
<p>
) - handles text/html paste events:
- Removes all styles and attributes from the pasted html so it matches the expected styles
Installation
Run npm install --save content-editable
.
Usage
Basic example
<html>
<body>
<div class="container"></div>
<script src="path/to/content-editable.js"></script>
<script>
const component = new contentEditable.TextView({
target: document.querySelector('.container'),
data: {
html: '<p>The initial text</p>',
}
});
</script>
</body>
</html>
Usage inside a svelte component:
<TextView bind:html></TextView>
<script>
import { TextView } from 'contenteditable/src/TextView.html';
export default {
data() {
return {
html: '<p>The initial text</p>',
};
},
components: {
TextView
}
}
</script>
Subscribe to changes:
const component =
component.observe('html', html => console.log('HTML content changed to', html));
component.observe('text', text => console.log('Plain text content changed to', text));
To learn more about svelte observers, see the docs.