copy.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. UE.plugin.register("copy", function() {
  2. var me = this;
  3. function initZeroClipboard() {
  4. ZeroClipboard.config({
  5. debug: false,
  6. swfPath:
  7. me.options.UEDITOR_HOME_URL +
  8. "third-party/zeroclipboard/ZeroClipboard.swf"
  9. });
  10. var client = (me.zeroclipboard = new ZeroClipboard());
  11. // 复制内容
  12. client.on("copy", function(e) {
  13. var client = e.client,
  14. rng = me.selection.getRange(),
  15. div = document.createElement("div");
  16. div.appendChild(rng.cloneContents());
  17. client.setText(div.innerText || div.textContent);
  18. client.setHtml(div.innerHTML);
  19. rng.select();
  20. });
  21. // hover事件传递到target
  22. client.on("mouseover mouseout", function(e) {
  23. var target = e.target;
  24. if (target) {
  25. if (e.type == "mouseover") {
  26. domUtils.addClass(target, "edui-state-hover");
  27. } else if (e.type == "mouseout") {
  28. domUtils.removeClasses(target, "edui-state-hover");
  29. }
  30. }
  31. });
  32. // flash加载不成功
  33. client.on("wrongflash noflash", function() {
  34. ZeroClipboard.destroy();
  35. });
  36. // 触发事件
  37. me.fireEvent("zeroclipboardready", client);
  38. }
  39. return {
  40. bindEvents: {
  41. ready: function() {
  42. if (!browser.ie) {
  43. if (window.ZeroClipboard) {
  44. initZeroClipboard();
  45. } else {
  46. utils.loadFile(
  47. document,
  48. {
  49. src:
  50. me.options.UEDITOR_HOME_URL +
  51. "third-party/zeroclipboard/ZeroClipboard.js",
  52. tag: "script",
  53. type: "text/javascript",
  54. defer: "defer"
  55. },
  56. function() {
  57. initZeroClipboard();
  58. }
  59. );
  60. }
  61. }
  62. }
  63. },
  64. commands: {
  65. copy: {
  66. execCommand: function(cmd) {
  67. if (!me.document.execCommand("copy")) {
  68. alert(me.getLang("copymsg"));
  69. }
  70. }
  71. }
  72. }
  73. };
  74. });