From ab03daf527547f7a40e4fdc298f50f9ab0534a54 Mon Sep 17 00:00:00 2001 From: Darks Date: Wed, 4 Dec 2019 22:47:55 +0100 Subject: [PATCH] Retrait du gitignore des dossiers scripts --- .gitignore | 1 - app/static/scripts/editor.js | 113 +++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 app/static/scripts/editor.js diff --git a/.gitignore b/.gitignore index 5c948f1..b5993a7 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,6 @@ venv/ # pipenv Pipfile Pipfile.lock -scripts/ ## Deployment files diff --git a/app/static/scripts/editor.js b/app/static/scripts/editor.js new file mode 100644 index 0000000..56a3e83 --- /dev/null +++ b/app/static/scripts/editor.js @@ -0,0 +1,113 @@ +/* Add callbacks on text formatting buttons */ + +function edit(e, type) { + function inline(type, str, repeat, insert) { + // Characters used to format inline blocs + // repeat: if true, add one more char to the longest suite found + // insert: insert between char and str (before and after) + var chars = { + 'bold': '*', + 'italic': '/', + 'underline': '_', + 'strikethrough': '~', + 'inline-code': '`', + 'h1': '===', + 'h2': '---', + 'h3': '...', + } + + if (repeat) { + // Detect longest suite of similar chars + var n = 1; var tmp = 1; + for(var i = 0; i < str.length; i++) { + if(str[i] == chars[type]) tmp++; + else tmp = 1; + n = (tmp > n) ? tmp : n; + } + return chars[type].repeat(n) + insert + str + insert + chars[type].repeat(n); + } + + return chars[type] + insert + str + insert + chars[type]; + } + + function list(type, str) { + switch(type) { + case 'list-bulleted': + return '* ' + str.replaceAll('\n', '\n* '); + break; + case 'list-numbered': + return '1. ' + str; + break; + } + } + + var ta = e.parentNode.parentNode.querySelector('textarea'); + var start = ta.selectionStart; + var end = ta.selectionEnd; + + switch(type) { + case 'bold': + case 'italic': + case 'underline': + case 'strikethrough': + case 'inline-code': + ta.value = ta.value.substring(0, start) + + inline(type, ta.value.substring(start, end), true, '') + + ta.value.substring(end); + break; + case 'h1': + case 'h2': + case 'h3': + ta.value = ta.value.substring(0, start) + + inline(type, ta.value.substring(start, end), false, ' ') + + ta.value.substring(end); + break; + case 'list-bulleted': + case 'list-numbered': + ta.value = ta.value.substring(0, start) + + list(type, ta.value.substring(start, end)) + + ta.value.substring(end); + break; + } +} + +function pre(type, str, multiline) { + +} + + +function bold(e) { + var ta = e.parentNode.parentNode.querySelector('textarea'); + var indexStart = ta.selectionStart; + var indexEnd = ta.selectionEnd; + var txt = ta.value.substring(indexStart, indexEnd); + ta.value += '\n' + inline('bold', txt); +} + + +// Tab insert some spaces +// Ctrl+Enter send the form +ta = document.querySelector(".editor textarea"); +ta.addEventListener('keydown', function(e) { + var keyCode = e.keyCode || e.which; + if (keyCode == 9) { + e.preventDefault(); + + var start = e.target.selectionStart; + var end = e.target.selectionEnd; + // set textarea value to: text before caret + tab + text after caret + e.target.value = e.target.value.substring(0, start) + "\t" + e.target.value.substring(end); + e.target.selectionEnd = start + 1; + } + if (e.ctrlKey && keyCode == 13) { + var e = e.target; + while(! (e instanceof HTMLFormElement)) { + e = e.parentNode; + } + try { + e.submit(); + } catch(exception) { + e.submit.click(); + } + } +});