Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting multiple block by text selection #172

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 8 additions & 59 deletions tinymce-single/tinymce/block.css
Original file line number Diff line number Diff line change
@@ -1,69 +1,12 @@
#editor img[data-mce-selected] {
#editor img[data-mce-selected],
#editor hr[data-mce-selected] {
outline: none;
}

#editor img::selection {
background-color: transparent;
}

#editor hr[data-mce-selected] {
outline: none;
}

#editor.has-block-ui *[data-mce-selected="block"],
#editor.has-block-ui hr[data-mce-selected] {
outline: 2px solid #e0e5e9;
outline-offset: 11px;
/*background-color: rgba( 135, 166, 188, 0.3 );
box-shadow: 0px 0px 0px 11px rgba( 135, 166, 188, 0.3 );
position: relative;*/
}

/*#editor *[data-mce-selected="block"]:before {
content: '';
border-top: 2px solid #87a6bc;
border-left: 2px solid #87a6bc;
border-right: 2px solid #87a6bc;
display: block;
position: absolute;
top: -14px;
left: -12px;
right: -12px;
bottom: -14px;
}

#editor *[data-mce-selected="block"] ~ [data-mce-selected="block"]:before {
border-top: none;
border-bottom: 2px solid #87a6bc;
}*/

#editor *[data-mce-selected="move"] {
color: #87a6bc;
outline: 1px dashed #87a6bc;
outline-offset: 11px;
}

#editor.is-moving-block > *:before {
content: '\21E2';
display: block;
position: absolute;
font-size: 24px;
/*background-color: #0087be;
padding: 0 3px;
border-radius: 3px;
color: #fff;*/
color: #87a6bc;
font-weight: normal;
margin-top: -24px;
left: 40px;
}

#editor.is-moving-block > .alignright:before,
#editor.is-moving-block > [data-mce-selected="move"]:before,
#editor.is-moving-block > [data-mce-selected="move"] + *:before {
content: '';
}

.mce-container,
.mce-container *,
.mce-container button,
Expand Down Expand Up @@ -287,3 +230,9 @@ div.mce-inline-toolbar-grp.block-toolbar {
div.mce-inline-toolbar-grp.block-toolbar > div.mce-stack-layout {
padding: 0;
}

.block-outline {
pointer-events: none;
outline: 2px solid #e0e5e9;
outline-offset: 11px;
}
54 changes: 48 additions & 6 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
} );

function moveBlockUp() {
$blocks = editor.$( '*[data-mce-selected="block"]' ).add( element );
$blocks = getBlockSelection();
rect = element.getBoundingClientRect();
$prev = $blocks.first().prev();

Expand All @@ -176,7 +176,7 @@
}

function moveBlockDown() {
$blocks = editor.$( '*[data-mce-selected="block"]' ).add( element );
$blocks = getBlockSelection();
rect = element.getBoundingClientRect();
$next = $blocks.last().next();

Expand Down Expand Up @@ -221,6 +221,11 @@
editor.buttons.strikethrough.icon = 'gridicons-strikethrough';
editor.buttons.link.icon = 'gridicons-link';

var blockOutline = document.createElement( 'div' );

blockOutline.className = 'block-outline';
document.body.appendChild( blockOutline );

var toolbarInline = editor.wp._createToolbar( [ 'bold', 'italic', 'strikethrough', 'link' ] );
var toolbarCaret = editor.wp._createToolbar( [ 'add' ] );
blockToolbar = editor.wp._createToolbar( [ 'up', 'down' ] );
Expand Down Expand Up @@ -519,6 +524,28 @@
insert = false;
} );

function getBlockSelection( selection ) {
selection = selection || window.getSelection();

if ( selection.anchorNode.compareDocumentPosition( selection.focusNode ) & Node.DOCUMENT_POSITION_FOLLOWING ) {
var startNode = selection.anchorNode;
var endNode = selection.focusNode;
} else {
var startNode = selection.focusNode;
var endNode = selection.anchorNode;
}

var $start = editor.$( editor.dom.getParent( startNode, function( element ) {
return element.parentNode === editor.getBody();
} ) );

var $end = editor.$( editor.dom.getParent( endNode, function( element ) {
return element.parentNode === editor.getBody();
} ) );

return $start.add( $start.nextUntil( $end ) ).add( $end );
}

editor.on( 'selectionChange nodeChange', function( event ) {
var selection = window.getSelection();
var isCollapsed = selection.isCollapsed;
Expand Down Expand Up @@ -550,7 +577,25 @@
toolbarCaret.hide();

if ( isBlockUIVisible ) {
showBlockUI();
var selectedBlocks = getBlockSelection();

if ( selectedBlocks.length === 1 ) {
showBlockUI();
} else {
hideBlockUI();
blockToolbar.reposition();
}

var startRect = selectedBlocks.first()[0].getBoundingClientRect();
var endRect = selectedBlocks.last()[0].getBoundingClientRect();

DOM.setStyles( blockOutline, {
position: 'absolute',
left: Math.min( startRect.left, endRect.left ) + 'px',
top: startRect.top + window.pageYOffset + 'px',
height: endRect.bottom - startRect.top + 'px',
width: Math.max( startRect.width, endRect.width ) + 'px'
} );
} else {
hideBlockUI();
}
Expand All @@ -564,9 +609,6 @@
} );

editor.on( 'nodeChange', function( event ) {
editor.$( '*[data-mce-selected="block"]' ).attr( 'data-mce-selected', null );
editor.$( element ).attr( 'data-mce-selected', 'block' );

insert = false;
} );

Expand Down