Codemirror是否提供剪切,复制和粘贴API? [英] Does codemirror provide Cut, Copy and Paste API?

查看:372
本文介绍了Codemirror是否提供剪切,复制和粘贴API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://codemirror.net/doc/manual.html 中,我只能找到 getRange()
undo(),redo()等,但我找不到cut(),copy()和粘贴API,
和其他内容,当我尝试运行 editor.execCommand( cut)时,出现错误。
您能帮我吗?谢谢!

From http://codemirror.net/doc/manual.html, I only find getRange(), undo(), redo() etc, and I can't find cut(), copy() and paste API, and more when I try to run editor.execCommand("cut"), I get the error. Could you help me? Thanks!

推荐答案

使用 clipboard.js ,您可以定义 text()函数来获取

Using clipboard.js, you can define the text() function to grab the value of the CodeMirror's inner document.

存储对(< textarea> )编辑器选择器的引用,以方便使用

Store a reference to the (<textarea>) editor's selector for convenience.

var editorSelector = '#editor' // or '#editor + .CodeMirror';

实例化一个新的 ClipBoard 对象您的按钮。

Instantiate a new ClipBoard object with reference to your button.

new Clipboard('.clip-btn-native', {
    text: function(trigger) {
        return getCodeMirrorNative(editorSelector).getDoc().getValue();
    }
});

通过本机JavaScript检索 CodeMirror 实例。

Retrieve a CodeMirror Instance via native JavaScript.

function getCodeMirrorNative(target) {
    var _target = target;
    if (typeof _target === 'string') {
        _target = document.querySelector(_target);
    }
    if (_target === null || !_target.tagName === undefined) {
        throw new Error('Element does not reference a CodeMirror instance.');
    }

    if (_target.className.indexOf('CodeMirror') > -1) {
        return _target.CodeMirror;
    }

    if (_target.tagName === 'TEXTAREA') {
        return _target.nextSibling.CodeMirror;
    }

    return null;
};








请查看完整内容;在 JSFiddle 上进行了深入演示。


Demo

Please see complete; in-depth demo over at JSFiddle.

这篇关于Codemirror是否提供剪切,复制和粘贴API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆