Skip to content

Commit aa702f6

Browse files
authored
Merge pull request #226 from WordPress/try/tinymce-single/contenteditable-false
Use contenteditable false for object blocks
2 parents 5fc5396 + bfeef37 commit aa702f6

File tree

5 files changed

+28
-80
lines changed

5 files changed

+28
-80
lines changed

tinymce-single/blocks.js

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
},
5050
getSelectedBlocks: function() {
5151
var editor = window.tinyMCE.activeEditor;
52+
53+
editor.selection.explicitRange = null;
54+
5255
var selection = window.getSelection();
5356
var startNode = editor.selection.getStart();
5457
var endNode = editor.selection.getEnd();

tinymce-single/blocks/core/image/register.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ window.wp.blocks.registerBlock( {
1313
],
1414
insert: function() {
1515
return (
16-
'<figure data-wp-block-type="core:image" class="alignright">' +
16+
'<figure data-wp-block-type="core:image" class="alignright" contenteditable="false">' +
1717
'<img src="https://cldup.com/HN3-c7ER9p.jpg" alt="">' +
18-
'<figcaption>I have no idea which mountain this is. It looks tall!</figcaption>' +
18+
'<figcaption contenteditable="true">I have no idea which mountain this is. It looks tall!</figcaption>' +
1919
'</figure>'
2020
);
2121
}

tinymce-single/blocks/my-awesome-plugin/youtube/structure.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
content: '';
88
display: block;
99
position: absolute;
10-
top: 0;
11-
right: 0;
12-
bottom: 0;
13-
left: 0;
1410
}
1511

1612
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"][data-mce-selected]:before {
1713
display: none;
1814
}
1915

16+
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"]:before,
2017
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"] iframe {
2118
width: 750px;
2219
height: 422px;
2320
}
2421

22+
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignleft:before,
23+
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignright:before,
2524
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignleft iframe,
2625
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignright iframe {
2726
width: 375px;
2827
height: 211px;
2928
}
3029

30+
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignfull:before,
3131
#editor figure[data-wp-block-type="my-awesome-plugin:youtube"].alignfull iframe {
3232
width: 950px;
3333
height: 534px;

tinymce-single/index.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ <h1>1.0 Is The Loneliest Number</h1>
2020

2121
<p>Many entrepreneurs idolize Steve Jobs. He’s such a perfectionist, they say. Nothing leaves the doors of 1 Infinite Loop in Cupertino without a polish and finish that makes geeks everywhere drool. No compromise!</p>
2222

23-
<figure data-wp-block-type="core:image" class="alignright">
23+
<figure data-wp-block-type="core:image" class="alignright" contenteditable="false">
2424
<img src="https://cldup.com/HN3-c7ER9p.jpg" alt="">
25-
<figcaption>I have no idea which mountain this is. It looks tall!</figcaption>
25+
<figcaption contenteditable="true">I have no idea which mountain this is. It looks tall!</figcaption>
2626
</figure>
2727

2828
<p>I like Apple for the opposite reason: they’re not afraid of getting a rudimentary 1.0 out into the world.</p>
@@ -39,10 +39,10 @@ <h2>NASA discovers system of seven Earth-sized planets</h2>
3939
<p>The discovery sets a new record for greatest number of habitable-zone planets found around a single star outside our solar system. All of these seven planets could have liquid water—key to life as we know it—under the right atmospheric conditions, but the chances are highest with the three in the habitable zone.</p>
4040
</blockquote>
4141

42-
<figure data-wp-block-type="core:gallery" data-wp-block-setting-column="3">
42+
<figure data-wp-block-type="core:gallery" data-wp-block-setting-column="3" contenteditable="false">
4343
<figure>
4444
<img src="https://www.nasa.gov/sites/default/files/styles/1x1_cardfeed/public/thumbnails/image/1_main_pia21423-png.png" alt="A star shines on a planet's surface.">
45-
<figcaption>TRAPPIST-1f</figcaption>
45+
<figcaption contenteditable="true">TRAPPIST-1f</figcaption>
4646
</figure>
4747
<figure>
4848
<img src="https://www.nasa.gov/sites/default/files/styles/1x1_cardfeed/public/thumbnails/image/exovolcano1920x1080.00033-1041.jpg" alt="">
@@ -54,9 +54,9 @@ <h2>NASA discovers system of seven Earth-sized planets</h2>
5454

5555
<p>Okay, now let’s take an interstellar road trip:</p>
5656

57-
<figure data-wp-block-type="my-awesome-plugin:youtube">
57+
<figure data-wp-block-type="my-awesome-plugin:youtube" contenteditable="false">
5858
<iframe width="560" height="315" src="https://www.youtube.com/embed/3-NTv0CdFCk" allowfullscreen></iframe>
59-
<figcaption>More planets! 😊</figcaption>
59+
<figcaption contenteditable="true">More planets! 😊</figcaption>
6060
</figure>
6161

6262
<p>And this is the end of another editor block prototype.</p>
@@ -67,7 +67,7 @@ <h2>NASA discovers system of seven Earth-sized planets</h2>
6767

6868
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
6969
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
70-
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.5.3/tinymce.min.js"></script>
70+
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.5.5/tinymce.min.js"></script>
7171

7272
<script src="blocks.js"></script>
7373
<script src="blocks/core/image/register.js"></script>

tinymce-single/tinymce/block.js

+12-67
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
var block = getSelectedBlock();
5151

5252
if ( ! editor.$( block ).find( 'figcaption' ).length ) {
53-
var figcaption = editor.$( '<figcaption><br></figcaption>' );
53+
var figcaption = editor.$( '<figcaption contenteditable="true"><br></figcaption>' );
5454

5555
editor.undoManager.transact( function() {
5656
editor.$( block ).append( figcaption );
@@ -97,6 +97,16 @@
9797
editor.on( 'preinit', function() {
9898
var DOM = tinymce.DOM;
9999

100+
editor.on( 'mousedown click', function( event ) {
101+
if ( event.target.getAttribute( 'contenteditable' ) === 'true' ) {
102+
if ( event.target !== editor.selection.getNode() ) {
103+
setTimeout( function() {
104+
editor.selection.placeCaretAt( event.clientX, event.clientY );
105+
}, 50 );
106+
}
107+
}
108+
}, true );
109+
100110
editor.addButton( 'block', {
101111
icon: 'gridicons-posts',
102112
tooltip: 'Add Block',
@@ -591,49 +601,16 @@
591601
}
592602

593603
var hidden = true;
594-
var keypress = false;
595604

596605
editor.on( 'keydown', function( event ) {
597-
keypress = true;
598-
599606
if ( ! isInputKeyEvent( event ) ) {
600607
return;
601608
}
602609

603-
// No typing directly on elements.
604-
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
605-
event.preventDefault();
606-
} else {
607-
hidden = true;
608-
}
609-
610+
hidden = true;
610611
insert = false;
611612
} );
612613

613-
editor.on( 'keyup', function( event ) {
614-
keypress = false;
615-
} );
616-
617-
editor.on( 'beforePastePreProcess beforeExecCommand', function( event ) {
618-
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
619-
event.preventDefault();
620-
}
621-
} );
622-
623-
editor.on( 'input', function( event ) {
624-
// Non key input (e.g. emoji).
625-
if ( keypress ) {
626-
return;
627-
}
628-
629-
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
630-
// Event not cancelable. :(
631-
// Let's see how this goes, it might be buggy.
632-
editor.undoManager.add();
633-
editor.undoManager.undo();
634-
}
635-
} );
636-
637614
editor.on( 'mousedown touchstart', function() {
638615
hidden = false;
639616
insert = false;
@@ -698,30 +675,6 @@
698675
addfigcaption();
699676
event.preventDefault();
700677
}
701-
702-
if ( keyCode === VK.BACKSPACE ) {
703-
var caretEl = editor.selection.getNode();
704-
705-
if ( caretEl.nodeName !== 'FIGCAPTION' ) {
706-
removeBlock();
707-
event.preventDefault();
708-
} else {
709-
var range = editor.selection.getRng();
710-
711-
if ( range.collapsed && range.startOffset === 0 ) {
712-
removefigcaption();
713-
event.preventDefault();
714-
}
715-
}
716-
}
717-
718-
if ( keyCode === VK.LEFT ) {
719-
var range = editor.selection.getRng();
720-
721-
if ( keyCode === VK.LEFT && range.startOffset === 0 ) {
722-
event.preventDefault();
723-
}
724-
}
725678
} else {
726679
if ( keyCode === VK.BACKSPACE ) {
727680
var selection = window.getSelection();
@@ -769,14 +722,6 @@
769722
metaCount = 0;
770723
} );
771724

772-
editor.on( 'dragstart', function( event ) {
773-
var block = getSelectedBlock();
774-
775-
if ( block.nodeName === 'FIGURE' ) {
776-
event.preventDefault();
777-
}
778-
} );
779-
780725
editor.on( 'nodeChange', function() {
781726
editor.$( editor.getBody() ).children().attr( 'data-mce-selected', null );
782727
editor.$( getSelectedBlock() ).attr( 'data-mce-selected', 'true' );

0 commit comments

Comments
 (0)