This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
68 lines (58 loc) · 2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict'
const marked = require('./marked-config')
// https://stephanwagner.me/auto-resizing-textarea-with-vanilla-javascript
function addAutoResize(element) {
element.style.boxSizing = 'border-box';
var offset = element.offsetHeight - element.clientHeight;
element.addEventListener('input', function (event) {
event.target.style.height = 'auto';
event.target.style.height = event.target.scrollHeight + offset + 'px';
});
element.removeAttribute('data-autoresize');
}
function addButton(element, label, callback) {
const button = document.createElement('button')
button.innerText = label
button.addEventListener('click', callback)
const li = document.createElement('li')
li.appendChild(button)
element.appendChild(li)
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
window.addEventListener('load', async function () {
// Keep searching for editors forever
while (true) {
const editor = document.querySelector('.ember-view > .editor:not(.zendesk-markdown-loaded)')
if (editor !== null) {
editor.classList.add('zendesk-markdown-loaded')
run(editor)
} else {
await sleep(100)
}
}
})
function run(editor) {
const toolbar = editor.querySelector('.zendesk-editor--custom-tools')
const richText = editor.querySelector('.zendesk-editor--rich-text-comment')
// Self-documentation
if (toolbar !== null) {
addButton(toolbar, 'Markdown?', function () {
alert('This is a browser extension. https://github.com/dxw/zendesk-markdown')
})
}
// Add the textarea
const textarea = document.createElement('textarea')
textarea.style.backgroundColor = '#ddd'
addAutoResize(textarea)
editor.parentElement.insertBefore(textarea, editor)
// Disable editing the rich text field
richText.contentEditable = false
// Hook the textarea up to the rich text field
textarea.addEventListener('input', function () {
const input = textarea.value
const output = marked(input)
richText.innerHTML = output
})
}