2021-03-20 14:21:27 +00:00
|
|
|
const editTextarea = document.getElementsByClassName('edit-form__textarea')[0]
|
2021-03-20 13:55:00 +00:00
|
|
|
|
2021-03-20 17:48:23 +00:00
|
|
|
function placeCursor(position, el = editTextarea) {
|
|
|
|
el.selectionEnd = position
|
|
|
|
el.selectionStart = el.selectionEnd
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedText(el = editTextarea) {
|
|
|
|
const [start, end] = [el.selectionStart, el.selectionEnd]
|
|
|
|
const text = el.value
|
|
|
|
return text.substring(start, end)
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:29:45 +00:00
|
|
|
function textInserter(text, cursorPosition = null, el = editTextarea) {
|
|
|
|
return function() {
|
|
|
|
const [start, end] = [el.selectionStart, el.selectionEnd]
|
|
|
|
el.setRangeText(text, start, end, 'select')
|
|
|
|
el.focus()
|
|
|
|
if (cursorPosition == null) {
|
|
|
|
placeCursor(end + text.length)
|
|
|
|
} else {
|
|
|
|
placeCursor(end + cursorPosition)
|
|
|
|
}
|
2021-03-20 17:48:23 +00:00
|
|
|
}
|
2021-03-20 13:55:00 +00:00
|
|
|
}
|
2021-03-20 17:48:23 +00:00
|
|
|
|
2021-04-05 17:38:44 +00:00
|
|
|
function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTextarea) {
|
|
|
|
return function() {
|
|
|
|
const [start, end] = [el.selectionStart, el.selectionEnd]
|
|
|
|
if (postfix == null) {
|
|
|
|
postfix = prefix
|
|
|
|
}
|
2021-04-26 16:29:41 +00:00
|
|
|
let text = getSelectedText(el)
|
|
|
|
let result = prefix + text + postfix
|
2021-04-05 17:38:44 +00:00
|
|
|
el.setRangeText(result, start, end, 'select')
|
|
|
|
el.focus()
|
|
|
|
placeCursor(end + cursorPosition)
|
|
|
|
}
|
2021-03-20 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 09:04:57 +00:00
|
|
|
const wrapBold = selectionWrapper(2, '**'),
|
|
|
|
wrapItalic = selectionWrapper(2, '//'),
|
|
|
|
wrapMonospace = selectionWrapper(1, '`'),
|
|
|
|
wrapHighlighted = selectionWrapper(2, '!!'),
|
|
|
|
wrapLifted = selectionWrapper(1, '^'),
|
|
|
|
wrapLowered = selectionWrapper(2, ',,'),
|
|
|
|
wrapStrikethrough = selectionWrapper(2, '~~'),
|
2021-04-05 18:29:45 +00:00
|
|
|
wrapLink = selectionWrapper(2, '[[', ']]')
|
|
|
|
|
2021-05-01 06:57:14 +00:00
|
|
|
const insertHorizontalBar = textInserter('\n----\n'),
|
|
|
|
insertImgBlock = textInserter('\nimg {\n \n}\n', 10),
|
|
|
|
insertTableBlock = textInserter('\ntable {\n \n}\n', 12),
|
|
|
|
insertRocket = textInserter('\n=> '),
|
|
|
|
insertXcl = textInserter('\n<= '),
|
|
|
|
insertHeading2 = textInserter('\n## '),
|
|
|
|
insertHeading3 = textInserter('\n### '),
|
|
|
|
insertCodeblock = textInserter('\n```\n\n```\n', 5),
|
|
|
|
insertBulletedList = textInserter('\n* '),
|
|
|
|
insertNumberedList = textInserter('\n*. ')
|
2021-03-20 17:48:23 +00:00
|
|
|
|
2021-04-05 18:29:45 +00:00
|
|
|
function insertDate() {
|
|
|
|
let date = new Date().toISOString().split('T')[0]
|
|
|
|
textInserter(date)()
|
2021-03-20 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 18:47:21 +00:00
|
|
|
function insertTimeUTC() {
|
|
|
|
let time = new Date().toISOString().substring(11, 19) + " UTC"
|
|
|
|
textInserter(time)()
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:29:45 +00:00
|
|
|
function insertUserlink() {
|
|
|
|
const userlink = document.querySelector('.header-links__entry_user a')
|
|
|
|
const userHypha = userlink.getAttribute('href').substring(7) // no /hypha/
|
|
|
|
textInserter('[[' + userHypha + ']]')()
|
2021-03-20 17:48:23 +00:00
|
|
|
}
|