});
$('div[contenteditable]').on('keyup', function(event) {
var sel = document.getSelection();
- if (sel.anchorNode instanceof Element && sel.anchorOffset == 0 && sel.isCollapsed) {
+ if ((sel.anchorNode instanceof Element && (
+ (sel.anchorOffset == 0 && sel.isCollapsed) || // first line
+ (sel.anchorNode.tagName === 'PRE'))) ||
+ (sel.anchorNode.parentNode.tagName === 'PRE')) {
// start of line
show_block_style_popup();
} else {
if (action == 'code') {
action = 'pre';
class_name = 'screen';
+ if ($(this).hasClass('on')) { // toggle off
+ action = 'p';
+ class_name = null;
+ }
}
document.execCommand('formatBlock', false, action);
var sel = window.getSelection();
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.css('position', 'absolute');
var sel = window.getSelection();
- var pos = $(sel.anchorNode).offset();
+ var anchor = sel.anchorNode;
+ if (anchor instanceof Text) {
+ anchor = anchor.parentNode;
+ }
+ if (anchor.tagName === "PRE") {
+ block_style_popup.find('[data-action=code]').addClass('on');
+ } else {
+ block_style_popup.find('[data-action=code]').removeClass('on');
+ }
+ var pos = $(anchor).offset();
block_style_popup.css('top', pos.top - 33);
block_style_popup.css('left', pos.left);
block_style_popup.show();