From 74bce0d79f4f07fd7e4bd488c4a8d8212d2effe9 Mon Sep 17 00:00:00 2001 From: Wisnu Priyambodo Date: Thu, 24 Sep 2020 08:05:21 +0700 Subject: [PATCH] Insert clipboard text on paste event --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- richeditor/build.gradle | 20 ++++++------- richeditor/src/main/assets/rich_editor.js | 5 ++++ .../richeditor/ClipboardInterface.java | 29 +++++++++++++++++++ .../jp/wasabeef/richeditor/RichEditor.java | 1 + 6 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 richeditor/src/main/java/jp/wasabeef/richeditor/ClipboardInterface.java diff --git a/build.gradle b/build.gradle index f93da44..aa1b7fc 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:3.4.0' + classpath 'com.android.tools.build:gradle:4.0.1' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aac5b46..000f5df 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip diff --git a/richeditor/build.gradle b/richeditor/build.gradle index 0e049a8..1de5736 100644 --- a/richeditor/build.gradle +++ b/richeditor/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' -group 'com.github.kishannareshpal' +group 'com.github.quipper' android { - compileSdkVersion 29 - buildToolsVersion "28.0.3" + compileSdkVersion 29 + buildToolsVersion "28.0.3" - defaultConfig { - minSdkVersion 17 - targetSdkVersion 29 - versionCode 1 - versionName "1.3.2" - } -} \ No newline at end of file + defaultConfig { + minSdkVersion 17 + targetSdkVersion 29 + versionCode 1 + versionName "1.3.4" + } +} diff --git a/richeditor/src/main/assets/rich_editor.js b/richeditor/src/main/assets/rich_editor.js index a70fc6e..420b752 100644 --- a/richeditor/src/main/assets/rich_editor.js +++ b/richeditor/src/main/assets/rich_editor.js @@ -365,6 +365,11 @@ RE.editor.addEventListener("keyup", function(e) { } }); RE.editor.addEventListener("click", RE.enabledEditingItems); +RE.editor.addEventListener("paste", function(evt) { + evt.preventDefault(); + var plain = Clipboard.getText(); + RE.insertHTML(plain); +}); /* Helper Function */ diff --git a/richeditor/src/main/java/jp/wasabeef/richeditor/ClipboardInterface.java b/richeditor/src/main/java/jp/wasabeef/richeditor/ClipboardInterface.java new file mode 100644 index 0000000..853138a --- /dev/null +++ b/richeditor/src/main/java/jp/wasabeef/richeditor/ClipboardInterface.java @@ -0,0 +1,29 @@ +package jp.wasabeef.richeditor; + +import android.content.ClipData; +import android.content.ClipDescription; +import android.content.ClipboardManager; +import android.content.Context; + +public class ClipboardInterface { + private Context context; + + public ClipboardInterface(Context context) { + this.context = context; + } + + @android.webkit.JavascriptInterface + public String getText() { + String plainText = ""; + ClipboardManager cm = (ClipboardManager) context + .getSystemService(Context.CLIPBOARD_SERVICE); + if (cm != null) { + ClipData cd = cm.getPrimaryClip(); + if (cd != null && cd.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) && cd.getItemCount() > 0) { + ClipData.Item item = cd.getItemAt(0); + plainText = item.getText().toString(); + } + } + return plainText; + } +} diff --git a/richeditor/src/main/java/jp/wasabeef/richeditor/RichEditor.java b/richeditor/src/main/java/jp/wasabeef/richeditor/RichEditor.java index fd1cd6a..c86c850 100644 --- a/richeditor/src/main/java/jp/wasabeef/richeditor/RichEditor.java +++ b/richeditor/src/main/java/jp/wasabeef/richeditor/RichEditor.java @@ -201,6 +201,7 @@ public RichEditor(Context context, AttributeSet attrs, int defStyleAttr) { setWebChromeClient(new WebChromeClient()); setWebViewClient(createWebviewClient()); addJavascriptInterface(new JavascriptInterface(context), "Android"); + addJavascriptInterface(new ClipboardInterface(context), "Clipboard"); loadUrl(SETUP_HTML); applyAttributes(context, attrs); }