mermaid/src/editor.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

function decodeHTMLEntities (str) {
2017-04-11 22:14:25 +08:00
if (str && typeof str === 'string') {
// strip script/html tags
2017-04-11 22:14:25 +08:00
var element = document.querySelector('.editor')
2017-04-11 22:14:25 +08:00
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '')
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '')
element.innerHTML = str
str = element.textContent
element.textContent = ''
}
2017-04-11 22:14:25 +08:00
return str
}
2017-04-11 22:14:25 +08:00
var mermaidEditor = {
$: document.querySelector,
textField: '',
submit: '',
graph: '',
init: function () {
document.querySelector('.button').addEventListener('click', function () {
mermaidEditor.update()
})
},
update: function () {
var txt = document.querySelector('.editor').value
txt = txt.replace(/>/g, '&gt;')
txt = txt.replace(/</g, '&lt;')
txt = decodeHTMLEntities(txt).trim()
document.querySelector('.mermaid').innerHTML = txt
global.mermaid.init()
document.querySelector('.editor').value = txt
}
}
2017-04-11 22:14:25 +08:00
document.addEventListener('DOMContentLoaded', function () {
mermaidEditor.init()
}, false)
2017-04-11 22:14:25 +08:00
exports = mermaidEditor