在Chrome和Firefox上,CKeditor中的粘贴选项似乎无法正常工作。

5
使用在线 ckeditor http://sdk.ckeditor.com/samples/classic.html 时,我发现过去的选项(粘贴、纯文本粘贴和从 Word 粘贴)无法从剪贴板复制。它会显示错误信息“Your browser does not allow you to paste plain text this way. Press Ctrl+Shift+V to paste.” 但是在 IE 中可以正常工作(它会提示允许访问),而在 Chrome 或 Firefox 中则无法正常工作。这是一个 bug 还是需要从浏览器或 ckEditor 进行某些配置?因为我记得几个月前我使用了相同的行为,它会弹出窗口让你将内容粘贴到编辑器中。
谢谢, Vijai
4个回答

8

只需将以下代码添加到config.js中:

CKEDITOR.on("instanceReady", function(event) {
    event.editor.on("beforeCommandExec", function(event) {
        // Show the paste dialog for the paste buttons and right-click paste
        if (event.data.name == "paste") {
            event.editor._.forcePasteDialog = true;
        }
        // Don't show the paste dialog for Ctrl+Shift+V
        if (event.data.name == "pastetext" && event.data.commandData.from == "keystrokeHandler") {
            event.cancel();
        }
    })
});

这个解决方案是由Pixilation在GitHub上提供的:https://github.com/ckeditor/ckeditor4/issues/469#issuecomment-524185244 - pbarney

2

Chrome不允许这样做,因为这是一个安全漏洞。有人可能会窃取您复制的数据,因此Chrome和大多数其他浏览器都不允许您这样做。按下Ctrl + Shift + V即可粘贴。


谢谢您的回复。如果是这样的话,CKeditor应该删除这三个选项,对吧?因为它们似乎存在,但只会向用户抛出错误。 - Oceanvijai
它确实可以在IE上正常工作。但他们可以使它只在IE上出现。 - Jujhar Singh
1
还有一件事,我们项目中使用的当前CKeditor似乎不会限制我们在同一浏览器上执行此操作。因此,我假设这个限制是在当前版本的ckeditor中,我能否在ckeditor发布说明或其他地方找到它。我正在使用CKEditor 4.6。 - Oceanvijai
@Oceanvijai 嗯,无论 CKEditor 的版本如何,它都不应该在 Chrome 上工作,但您可以尝试通过 http://ckeditor.com/contact 与他们联系。 - Jujhar Singh
有时我在Chrome或Edge中从Outlook粘贴到CKEditor 4时会出现此对话框。我仍然不明白是什么触发了它,因为在同一台机器上有时C&P有效,有时无效。有时即使使用CTRL + V也会出现对话框! 如果真的是浏览器问题,我不明白为什么我可以在Word Online中使用“粘贴”按钮随时粘贴而没有任何问题。那里没有安全漏洞?所以“浏览器不允许您这样做”的答案听起来并不令人信服。 - davidthegrey
显示剩余4条评论

1
根据官方ckeditor团队的说法:目前还没有解决此问题的方案。 参考此链接: https://github.com/ckeditor/ckeditor-dev/issues/469

You may see the comment from one of the "ckeditor developer" (mlewand https://github.com/mlewand).

我认为目前最好的解决方案就是使用以下方法将它们删除:

removeButtons : "Paste,PasteText,PasteFromWord"

我建议所有遇到这个问题的人都留言,这样他们就会为此问题做些什么。或者尝试使用较低版本。

1
使用 CKEditor 上传图像插件

演示

我们也遇到了同样的问题。添加了插件和图像上传和下载 API 操作。
然后从编辑器中删除过去的按钮。
config.removeButtons = 'Paste,PasteText,PasteFromWord';

将以下代码添加到CKEditor的config.js文件中。
config.extraPlugins = 'uploadimage';
config.uploadUrl = '/uploader/upload.php';
config.filebrowserUploadUrl = '/uploader/upload.php';

接着,使用CTRL + V将图像从Word文档粘贴到此处。

我正在使用MVC5。因此配置如下:

config.extraPlugins = 'uploadimage';
config.uploadUrl = '/CkEditorUploadSupport/UploadImage';
config.filebrowserUploadUrl = '/CkEditorUploadSupport/UploadImage';

MVC控制器代码;(项目控制器文件夹下的控制器名称为“CkEditorUploadSupport”)

     public JsonResult UploadImage()
    {
        if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
        {
            var file = System.Web.HttpContext.Current.Request.Files["upload"];
            
            var targetLocation = @"D:\CKTestFolder";

            if (!Directory.Exists(targetLocation))
            {
                Directory.CreateDirectory(targetLocation);
            }
            var pattern = new Regex(@"[:!@#$%^&*()}{|\"":?><\[\]\\;'/,~]");

            var modifiedFileName = pattern.Replace(file.FileName, "");
            modifiedFileName = modifiedFileName.Replace("\"", " ");
            modifiedFileName = modifiedFileName.Replace("4â€Â", " ");

            // Some browsers send file names with full path.
            // We are only interested in the file name.
            var physicalPath = Path.Combine(targetLocation, modifiedFileName);
            var fileName = Path.GetFileName(physicalPath);
            var newName = fileName;


            while (System.IO.File.Exists(physicalPath))
            {
                var newFileName = Path.GetFileNameWithoutExtension(fileName)
                                  + "_" + RandomString(3) +
                                  Path.GetExtension(fileName);
                physicalPath = Path.Combine(targetLocation, newFileName);
                newName = newFileName;
            }

            file.SaveAs(physicalPath);

            var response = new
            {
                uploaded = 1,
                fileName = newName,
                url = "/CkEditorUploadSupport/OpenImage?imageName=" + newName
            };

            return Json(response);
        }

        var response2 = new
        {
            uploaded = 0,
            message = "Upload Error.."
        };

        return Json(response2);
    }

    public ActionResult OpenImage(string imageName)
    {
        var targetLocation = @"D:\CKTestFolder";

        var physicalPath = Path.Combine(targetLocation, imageName);

        if (!System.IO.File.Exists(physicalPath))
        {
            var response2 = new
            {
                uploaded = 0,
                message = "File Not Found"
            };

            return Json(response2);
        }

        string mimeType = MimeMapping.GetMimeMapping(imageName);
        return base.File(physicalPath, mimeType);
    }

    private static Random random = new Random();

    public static string RandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return new string(Enumerable.Repeat(chars, length)
            .Select(s => s[random.Next(s.Length)]).ToArray());
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接