使用W3C Clipboard API将文本从extjs htmleditor组件复制到剪贴板

时间:2016-06-21 16:44:56

标签: javascript extjs clipboard extjs5

我需要将文本从extjs htmleditor组件复制到剪贴板,然后将其粘贴到文本文档(word或oppenoffice)中。

使用W3C剪贴板API可以实现这一点吗?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="laufnavigation.awp.hska.de.androidwearapp">

<uses-feature android:name="android.hardware.type.watch" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"

    android:theme="@android:style/Theme.DeviceDefault">
    <uses-library
        android:name="com.google.android.wearable"
        android:required="false" />

    <activity android:name=".MainWearActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyWearableListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:scheme="wear" android:host="*" />
        </intent-filter>
    </service>

</application>
</manifest>

https://fiddle.sencha.com/#fiddle/1cdi

1 个答案:

答案 0 :(得分:2)

我认为您无法使用ClipboardEvent界面,因为代表事件提供与修改剪贴板相关的信息,即剪切,复制和粘贴事件,因为您要复制点击按钮,没有这样的事件。

手动选择和复制htmleditor组件中的文本非常棘手,因为其DOM表示为<iframe>

我认为解决方案就像:

{
    xtype:'button',
    text:'Copy',
    handler:function(){
        var one = Ext.ComponentQuery.query('#OneItemId')[0];
        var two = Ext.ComponentQuery.query('#TwoItemId')[0];

        var editorFrame = one.inputEl.dom,
            editorFrameDocument = editorFrame.contentDocument || editorFrame.contentWindow.document;

        if(editorFrameDocument) {
            var documentSelection, selectionRange;

            // Select text in htmleditor iframe body
            // IE
            if (editorFrameDocument.body.createTextRange) {
                selectionRange = editorFrameDocument.body.createTextRange();
                selectionRange.moveToElementText(editorFrameDocument.body);
                selectionRange.select();
            } else if (window.getSelection) {
                documentSelection = editorFrameDocument.getSelection()        

                selectionRange = editorFrameDocument.createRange()
                selectionRange.selectNodeContents(editorFrameDocument.body);

                documentSelection.removeAllRanges();
                documentSelection.addRange(selectionRange);
            }

            // Copy selected text
            editorFrameDocument.execCommand('copy');
        }
    }

}

Working fiddle

只需在编辑器中添加一些文字,点击&#34;复制&#34;并粘贴到任何你想要的地方。