Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tex.init({
// ES6
import tex from 'tex'
// or
import { exec, init, destroy, getContent } from 'tex'
import { exec, init, destroy, getContent, setContent } from 'tex'
```

### Parameters
Expand All @@ -88,7 +88,10 @@ import { exec, init, destroy, getContent } from 'tex'
```javascript
tex.getContent(document.getElementById("editor"));
```

### Set Content
```javascript
tex.setContent(document.getElementById("editor"),"<p>Text in <strong>HTML</strong> format")
```
### Exec
```javascript
// Execute a document command.
Expand Down
15 changes: 14 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div id="editor1">Hello</div>
<textarea id="editor2">Hello world!</textarea>
<div id="editor3">Hello world!</div>

<div id="editor4"></div>
<script>
const tex = window.tex;
tex.init({
Expand Down Expand Up @@ -40,6 +40,19 @@
console.log(content);
}
});
tex.init({
element: document.getElementById("editor4"),
buttons: ['bold', 'italic', 'underline', 'strikethrough', 'textColor', 'heading1', 'heading2', 'paragraph', 'removeFormat', 'quote', 'olist', 'ulist', 'code', 'line', 'link', 'image', 'html'],
defaultParagraphSeparator: 'p',
styleWithCSS: false,
theme: 'dark',
onChange: (content) => {
console.log(content);
}
});
tex.setContent(document.getElementById("editor4"),
"<h1>Hello there!</h1><p>This text was added using the <code>setContent</code> method.</p>" +
"<p>It allows you to set the content of the editor programmatically.</p>");

// tex.destroy(document.getElementById("editor"));
</script>
Expand Down
12 changes: 11 additions & 1 deletion src/tex.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ var getContent = el => {
const content = element.querySelector('.tex-content')
return content.innerHTML
}
var setContent = (el, content) => {
const element = document.querySelector(`[tex-id="${el.id}"]`)
const contentElement = element.querySelector('.tex-content')
contentElement.innerHTML = content
element.querySelector('.htmlContent').value = content
el.value = content
}



var init = settings => {
var theme = settings.theme || 'light'
Expand Down Expand Up @@ -383,12 +392,13 @@ htmlContent.oninput = ({ target: { firstChild } }) => {
return settings.element
}

var tex = { exec: exec, init: init, destroy: destroy, getContent: getContent }
var tex = { exec: exec, init: init, destroy: destroy, getContent: getContent, setContent: setContent }

exports.exec = exec
exports.init = init
exports.destroy = destroy
exports.getContent = getContent
exports.setContent = setContent
exports['default'] = tex

Object.defineProperty(exports, '__esModule', { value: true })
Expand Down