From: Frédéric Péters Date: Thu, 4 Jun 2020 16:13:25 +0000 (+0200) Subject: add block toolbar to create a code section X-Git-Tag: v2022~31 X-Git-Url: https://git.0d.be/?p=chloro.git;a=commitdiff_plain;h=1a7b7afa88e0b0b3b966727981a740e25905c2d3 add block toolbar to create a code section --- diff --git a/chloro/phyll/static/css/style.scss b/chloro/phyll/static/css/style.scss index e3fb0af..9c912ba 100644 --- a/chloro/phyll/static/css/style.scss +++ b/chloro/phyll/static/css/style.scss @@ -183,7 +183,7 @@ footer { } } -#style-popup { +.style-popup { background: white; input { display: none; @@ -200,7 +200,6 @@ footer { button { padding: 0; height: 2em; - width: 2em; text-align: center; background: #eee; border: 1px outset #ccc; @@ -213,4 +212,7 @@ footer { text-decoration: underline; } } + &.short button { + width: 2em; + } } diff --git a/chloro/phyll/static/js/chloro.js b/chloro/phyll/static/js/chloro.js index c997bc7..4030c7c 100644 --- a/chloro/phyll/static/js/chloro.js +++ b/chloro/phyll/static/js/chloro.js @@ -4,6 +4,7 @@ $(function() { var sel = document.getSelection(); var anchorNode = sel.anchorNode; var prev_p = sel.anchorNode.previousSibling; + if (! prev_p) return; if (prev_p.tagName != 'P') { prev_p = $(prev_p).parents('p')[0]; } @@ -17,6 +18,16 @@ $(function() { } return true; }); + $('div[contenteditable]').on('keyup', function(event) { + var sel = document.getSelection(); + if (sel.anchorNode instanceof Element && sel.anchorOffset == 0 && sel.isCollapsed) { + // start of line + show_block_style_popup(); + } else { + hide_block_style_popup(); + } + return true; + }); $('#save').on('click', function() { var text = $('div[contenteditable]')[0].innerHTML; @@ -29,6 +40,48 @@ $(function() { return false; }); + var block_style_popup = null; + function block_style() { + var action = $(this).data('action'); + var class_name = null; + if (action == 'code') { + action = 'pre'; + class_name = 'screen'; + } + document.execCommand('formatBlock', false, action); + var sel = window.getSelection(); + if (class_name) { + $(sel.anchorNode).addClass(class_name); + } + var range = document.createRange(); + range.setStart(sel.anchorNode, 0); + sel.removeAllRanges(); + sel.addRange(range); + hide_block_style_popup(); + } + function show_block_style_popup() { + if (block_style_popup === null) { + block_style_popup = $( + '
' + + '' + + '
'); + block_style_popup.hide(); + block_style_popup.insertAfter($('.actions')); + block_style_popup.find('button').on('click', block_style); + } + block_style_popup.css('position', 'absolute'); + var sel = window.getSelection(); + var pos = $(sel.anchorNode).offset(); + block_style_popup.css('top', pos.top - 33); + block_style_popup.css('left', pos.left); + block_style_popup.show(); + } + function hide_block_style_popup() { + if (block_style_popup) { + $(block_style_popup).hide(); + } + } + var style_popup = null; function update_style() { var action = $(this).data('action'); @@ -81,7 +134,7 @@ $(function() { function show_style_popup(sel) { if (style_popup === null) { - style_popup = $('
' + + style_popup = $('
' + '' + '' + '' +