Skip to content

Commit 4c9324c

Browse files
committed
Squashed 'libs/editor/' changes from f1f6d10..a35d654
a35d654 0.8 version bump 7c242ad Merge pull request #330 from wordpress-mobile/issue/299-exit-blockquote c0a8f79 Merge branch 'develop' into issue/299-exit-blockquote dd267de Merge pull request #329 from wordpress-mobile/issue/302-fix-dom-errors dc084a2 Don't process the second Enter when exiting a blockquote, to avoid adding an extra new line 8a2f91e Allow double Enter press to exit a blockquote 400a5f7 Sanitize all calls to window.getSelection() that are followed by getRangeAt(), which can cause a DOM error 607c010 Fix typo in node traversal method f2d468f If getFocusedField can't select the current contenteditable div, force focus on the content field d2e8a78 Avoid DOM error console logs by adding some null checks cf01d93 Merge commit 'c6efe0a9190244d40e64300efc9cca56ae5acd5c' into develop ab52e25 Merge branch 'develop' into issue/120editor-initial-focus 6301484 Show the software keyboard once the DOM has loaded 359edf6 Focus on the title field when opening posts git-subtree-dir: libs/editor git-subtree-split: a35d654234ed2691a62289d9f554333446f1fa0d
1 parent c6efe0a commit 4c9324c

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

WordPressEditor/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ android {
2323
buildToolsVersion "23.0.2"
2424

2525
defaultConfig {
26-
versionCode 6
27-
versionName "0.6"
26+
versionCode 8
27+
versionName "0.8"
2828
minSdkVersion 14
2929
targetSdkVersion 23
3030
}

WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,14 @@ public void run() {
968968
button.setChecked(false);
969969
}
970970

971+
boolean editorHasFocus = false;
972+
971973
// Add any media files that were placed in a queue due to the DOM not having loaded yet
972974
if (mWaitingMediaFiles.size() > 0) {
973975
// Image insertion will only work if the content field is in focus
974976
// (for a new post, no field is in focus until user action)
975977
mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').focus();");
978+
editorHasFocus = true;
976979

977980
for (Map.Entry<String, MediaFile> entry : mWaitingMediaFiles.entrySet()) {
978981
appendMediaFile(entry.getValue(), entry.getKey(), null);
@@ -985,6 +988,7 @@ public void run() {
985988
// Gallery insertion will only work if the content field is in focus
986989
// (for a new post, no field is in focus until user action)
987990
mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').focus();");
991+
editorHasFocus = true;
988992

989993
for (MediaGallery mediaGallery : mWaitingGalleries) {
990994
appendGallery(mediaGallery);
@@ -993,6 +997,14 @@ public void run() {
993997
mWaitingGalleries.clear();
994998
}
995999

1000+
if (!editorHasFocus) {
1001+
mWebView.execJavaScriptFromString("ZSSEditor.focusFirstEditableField();");
1002+
}
1003+
1004+
// Show the keyboard
1005+
((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
1006+
.showSoftInput(mWebView, InputMethodManager.SHOW_IMPLICIT);
1007+
9961008
ProfilingUtils.split("EditorFragment.onDomLoaded completed");
9971009
ProfilingUtils.dump();
9981010
ProfilingUtils.stop();

libs/editor-common/assets/ZSSRichTextEditor.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,22 @@ ZSSEditor.getField = function(fieldId) {
193193

194194
ZSSEditor.getFocusedField = function() {
195195
var currentField = $(this.closerParentNodeWithName('div'));
196-
var currentFieldId = currentField.attr('id');
196+
var currentFieldId;
197197

198-
while (currentField
199-
&& (!currentFieldId || this.editableFields[currentFieldId] == null)) {
200-
currentField = this.closerParentNodeStartingAtNode('div', currentField);
198+
if (currentField) {
201199
currentFieldId = currentField.attr('id');
200+
}
202201

202+
while (currentField && (!currentFieldId || this.editableFields[currentFieldId] == null)) {
203+
currentField = this.closerParentNodeStartingAtNode('div', currentField);
204+
if (currentField) {
205+
currentFieldId = currentField.attr('id');
206+
}
207+
}
208+
209+
if (!currentFieldId) {
210+
ZSSEditor.resetSelectionOnField('zss_field_content');
211+
currentFieldId = 'zss_field_content';
203212
}
204213

205214
return this.editableFields[currentFieldId];
@@ -357,6 +366,9 @@ ZSSEditor.stylesCallback = function(stylesArray) {
357366

358367
ZSSEditor.backupRange = function(){
359368
var selection = window.getSelection();
369+
if (selection.rangeCount < 1) {
370+
return;
371+
}
360372
var range = selection.getRangeAt(0);
361373

362374
ZSSEditor.currentSelection =
@@ -411,7 +423,6 @@ ZSSEditor.getCaretArguments = function() {
411423
};
412424

413425
ZSSEditor.getJoinedFocusedFieldIdAndCaretArguments = function() {
414-
415426
var joinedArguments = ZSSEditor.getJoinedCaretArguments();
416427
var idArgument = "id=" + ZSSEditor.getFocusedField().getNodeId();
417428

@@ -430,6 +441,9 @@ ZSSEditor.getJoinedCaretArguments = function() {
430441

431442
ZSSEditor.getCaretYPosition = function() {
432443
var selection = window.getSelection();
444+
if (selection.rangeCount == 0) {
445+
return 0;
446+
}
433447
var range = selection.getRangeAt(0);
434448
var span = document.createElement("span");
435449
// Ensure span has dimensions and position by
@@ -543,7 +557,7 @@ ZSSEditor.setStrikeThrough = function() {
543557
var mustHandleWebKitIssue = (isDisablingStrikeThrough
544558
&& ZSSEditor.isCommandEnabled(commandName));
545559

546-
if (mustHandleWebKitIssue) {
560+
if (mustHandleWebKitIssue && window.getSelection().rangeCount > 0) {
547561
var troublesomeNodeNames = ['del'];
548562

549563
var selection = window.getSelection();
@@ -2730,6 +2744,9 @@ ZSSEditor.closerParentNode = function() {
27302744

27312745
var parentNode = null;
27322746
var selection = window.getSelection();
2747+
if (selection.rangeCount < 1) {
2748+
return null;
2749+
}
27332750
var range = selection.getRangeAt(0).cloneRange();
27342751

27352752
var currentNode = range.commonAncestorContainer;
@@ -2752,15 +2769,15 @@ ZSSEditor.closerParentNodeStartingAtNode = function(nodeName, startingNode) {
27522769
nodeName = nodeName.toLowerCase();
27532770

27542771
var parentNode = null;
2755-
var currentNode = startingNode,parentElement;
2772+
var currentNode = startingNode.parentElement;
27562773

27572774
while (currentNode) {
27582775

27592776
if (currentNode.nodeName == document.body.nodeName) {
27602777
break;
27612778
}
27622779

2763-
if (currentNode.nodeName.toLowerCase() == nodeName
2780+
if (currentNode.nodeName && currentNode.nodeName.toLowerCase() == nodeName
27642781
&& currentNode.nodeType == document.ELEMENT_NODE) {
27652782
parentNode = currentNode;
27662783

@@ -2779,6 +2796,9 @@ ZSSEditor.closerParentNodeWithName = function(nodeName) {
27792796

27802797
var parentNode = null;
27812798
var selection = window.getSelection();
2799+
if (selection.rangeCount < 1) {
2800+
return null;
2801+
}
27822802
var range = selection.getRangeAt(0).cloneRange();
27832803

27842804
var referenceNode = range.commonAncestorContainer;
@@ -2820,6 +2840,9 @@ ZSSEditor.parentTags = function() {
28202840

28212841
var parentTags = [];
28222842
var selection = window.getSelection();
2843+
if (selection.rangeCount < 1) {
2844+
return null;
2845+
}
28232846
var range = selection.getRangeAt(0);
28242847

28252848
var currentNode = range.commonAncestorContainer;
@@ -2937,21 +2960,29 @@ ZSSField.prototype.handleKeyDownEvent = function(e) {
29372960
} else if (this.isMultiline()) {
29382961
this.wrapCaretInParagraphIfNecessary();
29392962

2940-
// If enter was pressed to end a UL or OL, let's double check and handle it accordingly if so
29412963
if (wasEnterPressed) {
2942-
sel = window.getSelection();
2943-
node = $(sel.anchorNode);
2944-
children = $(sel.anchorNode.childNodes);
2964+
var sel = window.getSelection();
2965+
if (sel.rangeCount < 1) {
2966+
return null;
2967+
}
2968+
var node = $(sel.anchorNode);
2969+
var children = $(sel.anchorNode.childNodes);
2970+
var parentNode = rangy.getSelection().anchorNode.parentNode;
29452971

2972+
// If enter was pressed to end a UL or OL, let's double check and handle it accordingly if so
29462973
if (sel.isCollapsed && node.is(NodeName.LI) && (!children.length ||
29472974
(children.length == 1 && children.first().is(NodeName.BR)))) {
29482975
e.preventDefault();
2949-
var parentNode = rangy.getSelection().anchorNode.parentNode;
29502976
if (parentNode && parentNode.nodeName === NodeName.OL) {
29512977
ZSSEditor.setOrderedList();
29522978
} else if (parentNode && parentNode.nodeName === NodeName.UL) {
29532979
ZSSEditor.setUnorderedList();
29542980
}
2981+
// Exit blockquote when the user presses Enter inside a blockquote on a new line
2982+
// (main use case is to allow double Enter to exit blockquote)
2983+
} else if (sel.isCollapsed && sel.baseOffset == 0 && parentNode && parentNode.nodeName == 'BLOCKQUOTE') {
2984+
e.preventDefault();
2985+
ZSSEditor.setBlockquote();
29552986
}
29562987
}
29572988
}
@@ -3211,7 +3242,7 @@ ZSSField.prototype.wrapCaretInParagraphIfNecessary = function()
32113242
if (parentNodeShouldBeParagraph) {
32123243
var selection = window.getSelection();
32133244

3214-
if (selection) {
3245+
if (selection && selection.rangeCount > 0) {
32153246
var range = selection.getRangeAt(0);
32163247

32173248
if (range.startContainer == range.endContainer) {

0 commit comments

Comments
 (0)