From 52d7e0f87e10a2a0db52d3e1b6a736fc001e5b1d Mon Sep 17 00:00:00 2001 From: DanInSpace Date: Sat, 20 Mar 2021 22:48:23 +0500 Subject: [PATCH] More actions on toolbar, correct cursor position --- assets/assets.qtpl.go | 75 +++++++++++++++++- assets/toolbar.js | 71 ++++++++++++++++- views/mutators.qtpl | 25 ++++-- views/mutators.qtpl.go | 176 +++++++++++++++++++++++++++-------------- 4 files changed, 275 insertions(+), 72 deletions(-) diff --git a/assets/assets.qtpl.go b/assets/assets.qtpl.go index 6ff02ce..5fddd1a 100644 --- a/assets/assets.qtpl.go +++ b/assets/assets.qtpl.go @@ -422,15 +422,84 @@ func StreamToolbarJS(qw422016 *qt422016.Writer) { //line assets/assets.qtpl:14 qw422016.N().S(`const editTextarea = document.getElementsByClassName('edit-form__textarea')[0] -function insertTextAtCursor(text, el = editTextarea) { - const [start, end] = [el.selectionStart, el.selectionEnd]; - el.setRangeText(text, start, end, 'select'); +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) +} + +function insertTextAtCursor(text, cursorPosition = null, el = editTextarea) { + 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) + } +} + +function wrapSelection(prefix, postfix = null, el = editTextarea) { + const [start, end] = [el.selectionStart, el.selectionEnd] + if (postfix == null) { + postfix = prefix + } + text = getSelectedText(el) + result = prefix + text + postfix + el.setRangeText(result, start, end, 'select') + el.focus() + placeCursor(end + (prefix + postfix).length) } function insertDate() { let date = new Date().toISOString().split('T')[0] insertTextAtCursor(date) } + +function wrapBold() { + wrapSelection('**') +} + +function wrapItalic() { + wrapSelection('//') +} + +function wrapMonospace() { + wrapSelection('`) +//line assets/assets.qtpl:14 + qw422016.N().S("`") +//line assets/assets.qtpl:14 + qw422016.N().S(`') +} + +function wrapHighlighted() { + wrapSelection('!!') +} + +function wrapLifted() { + wrapSelection('^') +} + +function wrapLowered() { + wrapSelection(',,') +} + +function wrapStroked() { + wrapSelection('~~') +} + +function insertHorizontalBar() { + insertTextAtCursor('----\n') +} + +function insertImgBlock() { + insertTextAtCursor('img {\n\t\n}\n', 7) +} `) //line assets/assets.qtpl:14 qw422016.N().S(` diff --git a/assets/toolbar.js b/assets/toolbar.js index a528e73..7ea07aa 100644 --- a/assets/toolbar.js +++ b/assets/toolbar.js @@ -1,11 +1,76 @@ const editTextarea = document.getElementsByClassName('edit-form__textarea')[0] -function insertTextAtCursor(text, el = editTextarea) { - const [start, end] = [el.selectionStart, el.selectionEnd]; - el.setRangeText(text, start, end, 'select'); +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) +} + +function insertTextAtCursor(text, cursorPosition = null, el = editTextarea) { + 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) + } +} + +function wrapSelection(prefix, postfix = null, el = editTextarea) { + const [start, end] = [el.selectionStart, el.selectionEnd] + if (postfix == null) { + postfix = prefix + } + text = getSelectedText(el) + result = prefix + text + postfix + el.setRangeText(result, start, end, 'select') + el.focus() + placeCursor(end + (prefix + postfix).length) } function insertDate() { let date = new Date().toISOString().split('T')[0] insertTextAtCursor(date) } + +function wrapBold() { + wrapSelection('**') +} + +function wrapItalic() { + wrapSelection('//') +} + +function wrapMonospace() { + wrapSelection('`') +} + +function wrapHighlighted() { + wrapSelection('!!') +} + +function wrapLifted() { + wrapSelection('^') +} + +function wrapLowered() { + wrapSelection(',,') +} + +function wrapStroked() { + wrapSelection('~~') +} + +function insertHorizontalBar() { + insertTextAtCursor('----\n') +} + +function insertImgBlock() { + insertTextAtCursor('img {\n\t\n}\n', 7) +} diff --git a/views/mutators.qtpl b/views/mutators.qtpl index dfcc049..a32bd23 100644 --- a/views/mutators.qtpl +++ b/views/mutators.qtpl @@ -1,15 +1,29 @@ {% import "net/http" %} {% import "github.com/bouncepaw/mycorrhiza/util" %} +{% func Toolbar() %} + + +{% endfunc %} + {% func EditHTML(rq *http.Request, hyphaName, textAreaFill, warning string) %} {%s= NavHTML(rq, hyphaName, "edit") %}

Edit {%s util.BeautifulName(hyphaName) %}

{%s= warning %} -
- -
@@ -19,8 +33,8 @@ Cancel
+{%s= Toolbar() %}
- {% endfunc %} {% func PreviewHTML(rq *http.Request, hyphaName, textAreaFill, warning string, renderedPage string) %} @@ -31,7 +45,7 @@ {%s= warning %}
- +
@@ -40,5 +54,6 @@

Note that the hypha is not saved yet. You can preview the changes ↓

{%s= renderedPage %}
+{%s= Toolbar() %} {% endfunc %} diff --git a/views/mutators.qtpl.go b/views/mutators.qtpl.go index 6f577a6..62f6356 100644 --- a/views/mutators.qtpl.go +++ b/views/mutators.qtpl.go @@ -24,157 +24,211 @@ var ( ) //line views/mutators.qtpl:4 -func StreamEditHTML(qw422016 *qt422016.Writer, rq *http.Request, hyphaName, textAreaFill, warning string) { +func StreamToolbar(qw422016 *qt422016.Writer) { //line views/mutators.qtpl:4 qw422016.N().S(` + + `) -//line views/mutators.qtpl:5 +//line views/mutators.qtpl:19 +} + +//line views/mutators.qtpl:19 +func WriteToolbar(qq422016 qtio422016.Writer) { +//line views/mutators.qtpl:19 + qw422016 := qt422016.AcquireWriter(qq422016) +//line views/mutators.qtpl:19 + StreamToolbar(qw422016) +//line views/mutators.qtpl:19 + qt422016.ReleaseWriter(qw422016) +//line views/mutators.qtpl:19 +} + +//line views/mutators.qtpl:19 +func Toolbar() string { +//line views/mutators.qtpl:19 + qb422016 := qt422016.AcquireByteBuffer() +//line views/mutators.qtpl:19 + WriteToolbar(qb422016) +//line views/mutators.qtpl:19 + qs422016 := string(qb422016.B) +//line views/mutators.qtpl:19 + qt422016.ReleaseByteBuffer(qb422016) +//line views/mutators.qtpl:19 + return qs422016 +//line views/mutators.qtpl:19 +} + +//line views/mutators.qtpl:21 +func StreamEditHTML(qw422016 *qt422016.Writer, rq *http.Request, hyphaName, textAreaFill, warning string) { +//line views/mutators.qtpl:21 + qw422016.N().S(` +`) +//line views/mutators.qtpl:22 qw422016.N().S(NavHTML(rq, hyphaName, "edit")) -//line views/mutators.qtpl:5 +//line views/mutators.qtpl:22 qw422016.N().S(`

Edit `) -//line views/mutators.qtpl:8 +//line views/mutators.qtpl:25 qw422016.E().S(util.BeautifulName(hyphaName)) -//line views/mutators.qtpl:8 +//line views/mutators.qtpl:25 qw422016.N().S(`

`) -//line views/mutators.qtpl:9 +//line views/mutators.qtpl:26 qw422016.N().S(warning) -//line views/mutators.qtpl:9 +//line views/mutators.qtpl:26 qw422016.N().S(` -
- -

Cancel
-
- `) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:36 + qw422016.N().S(Toolbar()) +//line views/mutators.qtpl:36 + qw422016.N().S(` + +`) +//line views/mutators.qtpl:38 } -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 func WriteEditHTML(qq422016 qtio422016.Writer, rq *http.Request, hyphaName, textAreaFill, warning string) { -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 StreamEditHTML(qw422016, rq, hyphaName, textAreaFill, warning) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 qt422016.ReleaseWriter(qw422016) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 } -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 func EditHTML(rq *http.Request, hyphaName, textAreaFill, warning string) string { -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 qb422016 := qt422016.AcquireByteBuffer() -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 WriteEditHTML(qb422016, rq, hyphaName, textAreaFill, warning) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 qs422016 := string(qb422016.B) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 qt422016.ReleaseByteBuffer(qb422016) -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 return qs422016 -//line views/mutators.qtpl:24 +//line views/mutators.qtpl:38 } -//line views/mutators.qtpl:26 +//line views/mutators.qtpl:40 func StreamPreviewHTML(qw422016 *qt422016.Writer, rq *http.Request, hyphaName, textAreaFill, warning string, renderedPage string) { -//line views/mutators.qtpl:26 +//line views/mutators.qtpl:40 qw422016.N().S(` `) -//line views/mutators.qtpl:27 +//line views/mutators.qtpl:41 qw422016.N().S(NavHTML(rq, hyphaName, "edit")) -//line views/mutators.qtpl:27 +//line views/mutators.qtpl:41 qw422016.N().S(`

Edit `) -//line views/mutators.qtpl:30 +//line views/mutators.qtpl:44 qw422016.E().S(util.BeautifulName(hyphaName)) -//line views/mutators.qtpl:30 +//line views/mutators.qtpl:44 qw422016.N().S(` (preview)

`) -//line views/mutators.qtpl:31 +//line views/mutators.qtpl:45 qw422016.N().S(warning) -//line views/mutators.qtpl:31 +//line views/mutators.qtpl:45 qw422016.N().S(`
-
Cancel

Note that the hypha is not saved yet. You can preview the changes ↓

`) -//line views/mutators.qtpl:41 +//line views/mutators.qtpl:55 qw422016.N().S(renderedPage) -//line views/mutators.qtpl:41 +//line views/mutators.qtpl:55 qw422016.N().S(`
+`) +//line views/mutators.qtpl:57 + qw422016.N().S(Toolbar()) +//line views/mutators.qtpl:57 + qw422016.N().S(`
`) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 } -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 func WritePreviewHTML(qq422016 qtio422016.Writer, rq *http.Request, hyphaName, textAreaFill, warning string, renderedPage string) { -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 StreamPreviewHTML(qw422016, rq, hyphaName, textAreaFill, warning, renderedPage) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 qt422016.ReleaseWriter(qw422016) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 } -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 func PreviewHTML(rq *http.Request, hyphaName, textAreaFill, warning string, renderedPage string) string { -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 qb422016 := qt422016.AcquireByteBuffer() -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 WritePreviewHTML(qb422016, rq, hyphaName, textAreaFill, warning, renderedPage) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 qs422016 := string(qb422016.B) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 qt422016.ReleaseByteBuffer(qb422016) -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 return qs422016 -//line views/mutators.qtpl:44 +//line views/mutators.qtpl:59 }