Skip to content

Commit

Permalink
Set Aztec t delete the Enter for paragraph block
Browse files Browse the repository at this point in the history
  • Loading branch information
hypest committed Apr 30, 2019
1 parent ca2a93f commit 897d272
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
4 changes: 3 additions & 1 deletion react-native-aztec/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
wordpressUtilsVersion = '1.22'
espressoVersion = '3.0.1'

aztecVersion = 'list-block-fix-try2'
aztecVersion = 'list-block-fix-try3'
}

repositories {
Expand All @@ -27,6 +27,7 @@ buildscript {
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'

// import the `readReactNativeVersion()` function
Expand Down Expand Up @@ -62,6 +63,7 @@ android {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
java.srcDirs += "src/${dir}/kotlin"
res.srcDirs "src/${dir}/res"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.wordpress.aztec.plugins.wpcomments.HiddenGutenbergPlugin;
import org.wordpress.aztec.plugins.wpcomments.WordPressCommentsPlugin;
import org.wordpress.aztec.plugins.wpcomments.toolbar.MoreToolbarButton;
import org.wordpress.aztec.watchers.EnterPressedUnderway;

import java.util.Map;
import java.util.ArrayList;
Expand Down Expand Up @@ -415,6 +414,11 @@ public void setOnPasteHandling(final ReactAztecText view, boolean onPasteHandlin
view.shouldHandleOnPaste = onPasteHandling;
}

@ReactProp(name = "deleteEnter", defaultBoolean = false)
public void setShouldDeleteEnter(final ReactAztecText view, boolean shouldDeleteEnter) {
view.shouldDeleteEnter = shouldDeleteEnter;
}

@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.<String, Integer>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class ReactAztecText extends AztecText {
boolean shouldHandleOnSelectionChange = false;
boolean shouldHandleActiveFormatsChange = false;

boolean shouldDeleteEnter = false;

// This optional variable holds the outer HTML tag that will be added to the text when the user start typing in it
// This is required to keep placeholder text working, and start typing with styled text.
// Ref: https://github.com/wordpress-mobile/gutenberg-mobile/issues/707
Expand Down Expand Up @@ -328,6 +330,11 @@ public void addTextChangedListener(TextWatcher watcher) {
if (mListeners == null) {
mListeners = new ArrayList<>();
super.addTextChangedListener(getTextWatcherDelegator());

// Keep the enter pressed watcher at the beginning of the watchers list.
// We want to intercept Enter.key as soon as possible, and before other listeners start modifying the text.
// Also note that this Watchers, when the AztecKeyListener is set, keep hold a copy of the content in the editor.
mListeners.add(new EnterPressedWatcher(this, () -> shouldDeleteEnter));
}

mListeners.add(watcher);
Expand Down Expand Up @@ -458,6 +465,10 @@ public void setActiveFormats(Iterable<String> newFormats) {
updateToolbarButtons(newStylesList);
}

protected boolean isEnterPressedUnderway() {
return EnterPressedWatcher.Companion.isEnterPressedUnderway(getText());
}

/**
* This class will redirect *TextChanged calls to the listeners only in the case where the text
* is changed by the user, and not explicitly set by JS.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.wordpress.mobile.ReactNativeAztec

import android.text.Editable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextWatcher
import org.wordpress.aztec.AztecText
import org.wordpress.aztec.Constants
import java.lang.ref.WeakReference

// Class to be used as a span to temporarily denote that Enter was detected
class EnterPressedUnderway

interface EnterDeleter {
fun shouldDeleteEnter(): Boolean
}

class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter) : TextWatcher {

private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)
private lateinit var textBefore : SpannableStringBuilder
private var start: Int = -1
private var selStart: Int = 0
private var selEnd: Int = 0
var done = false

override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
val aztecText = aztecTextRef.get()
if (aztecText?.getAztecKeyListener() != null && !aztecText.isTextChangedListenerDisabled()) {
// we need to make a copy to preserve the contents as they were before the change
textBefore = SpannableStringBuilder(text)
this.start = start
this.selStart = aztecText.selectionStart
this.selEnd = aztecText.selectionEnd
}
}

override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
val aztecText = aztecTextRef.get()
val aztecKeyListener = aztecText?.getAztecKeyListener()
if (aztecText != null && !aztecText.isTextChangedListenerDisabled() && aztecKeyListener != null) {
val newTextCopy = SpannableStringBuilder(text)
// if new text length is longer than original text by 1
if (textBefore?.length == newTextCopy.length - 1) {
// now check that the inserted character is actually a NEWLINE
if (newTextCopy[this.start] == Constants.NEWLINE) {
done = false
aztecText.editableText.setSpan(EnterPressedUnderway(), 0, 0, Spanned.SPAN_USER)
aztecKeyListener.onEnterKey(newTextCopy.replace(this.start, this.start+1, ""), true, selStart, selEnd)
}
}
}
}

override fun afterTextChanged(text: Editable) {
aztecTextRef.get()?.editableText?.getSpans(0, 0, EnterPressedUnderway::class.java)?.forEach {
if (!done) {
done = true
if (enterDeleter.shouldDeleteEnter())
text.replace(start, start + 1, "")
}
aztecTextRef.get()?.editableText?.removeSpan(it)
}
}

companion object {
fun isEnterPressedUnderway(spanned: Spanned?): Boolean {
return spanned?.getSpans(0, 0, EnterPressedUnderway::class.java)?.isNotEmpty() ?: false
}
}
}
2 changes: 2 additions & 0 deletions react-native-aztec/src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AztecView extends React.Component {
activeFormats: PropTypes.array,
isSelected: PropTypes.bool,
disableGutenbergMode: PropTypes.bool,
deleteEnter: PropTypes.bool,
text: PropTypes.object,
placeholder: PropTypes.string,
placeholderTextColor: ColorPropType,
Expand Down Expand Up @@ -158,6 +159,7 @@ class AztecView extends React.Component {
onHTMLContentWithCursor = { this._onHTMLContentWithCursor }
onSelectionChange = { this._onSelectionChange }
onEnter = { this.props.onEnter && this._onEnter }
deleteEnter = { this.props.deleteEnter }
// IMPORTANT: the onFocus events are thrown away as these are handled by onPress() in the upper level.
// It's necessary to do this otherwise onFocus may be set by `{...otherProps}` and thus the onPress + onFocus
// combination generate an infinite loop as described in https://github.com/wordpress-mobile/gutenberg-mobile/issues/302
Expand Down

0 comments on commit 897d272

Please sign in to comment.