copy.js 2.3 KB

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