plugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @license Licensed under the terms of any of the following licenses at your choice:
  3. *
  4. * - GNU General Public License Version 2 or later (the "GPL")
  5. * http://www.gnu.org/licenses/gpl.html
  6. * (See Appendix A)
  7. *
  8. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  9. * http://www.gnu.org/licenses/lgpl.html
  10. * (See Appendix B)
  11. *
  12. * - Mozilla Public License Version 1.1 or later (the "MPL")
  13. * http://www.mozilla.org/MPL/MPL-1.1.html
  14. * (See Appendix C)
  15. *
  16. */
  17. /**
  18. * @fileOverview Paste as code plugin.
  19. */
  20. (function() {
  21. // The pastecode command definition.
  22. var pasteCodeCmd = {
  23. // Snapshots are done manually by editable.insertXXX methods.
  24. canUndo: false,
  25. async: true,
  26. exec: function( editor ) {
  27. editor.getClipboardData({ title: editor.lang.pastecode.title }, function( data ) {
  28. // Do not use editor#paste, because it would start from beforePaste event.
  29. //data && editor.fire( 'paste', { type: 'html', dataValue: data.dataValue } );
  30. editor.insertHtml( data.dataValue );
  31. editor.fire( 'afterCommandExec', {
  32. name: 'pastecode',
  33. command: pasteCodeCmd,
  34. returnValue: !!data
  35. });
  36. });
  37. }
  38. };
  39. // Register the plugin.
  40. CKEDITOR.plugins.add( 'pastecode', {
  41. requires: 'clipboard',
  42. lang: 'fr,en', // %REMOVE_LINE_CORE%
  43. icons: 'pastecode,pastecode-rtl', // %REMOVE_LINE_CORE%
  44. init: function( editor ) {
  45. var commandName = 'pastecode';
  46. editor.addCommand( commandName, new CKEDITOR.dialogCommand( 'pastecode', {
  47. //allowedContent : required,
  48. //requiredContent : required
  49. } ) );
  50. editor.ui.addButton && editor.ui.addButton( 'PasteCode', {
  51. label: editor.lang.pastecode.button,
  52. command: commandName,
  53. toolbar: 'clipboard,40'
  54. });
  55. CKEDITOR.dialog.add( 'pastecode', function( editor )
  56. {
  57. return {
  58. title : editor.lang.pastecode.title,
  59. minWidth : 350,
  60. minHeight : 200,
  61. contents : [
  62. {
  63. id : 'general',
  64. label : editor.lang.pastecode.code,
  65. elements : [
  66. {
  67. type : 'textarea',
  68. id : 'contents',
  69. label : editor.lang.pastecode.code,
  70. //cols: 140,
  71. rows: 10,
  72. validate : CKEDITOR.dialog.validate.notEmpty( editor.lang.pastecode.notEmpty ),
  73. required : true,
  74. commit : function()
  75. {
  76. //element.setHtml( this.getValue() );
  77. editor.insertHtml( this.getValue() );
  78. }
  79. }
  80. ]
  81. }
  82. ],
  83. onOk : function()
  84. {
  85. /*
  86. if ( this.insertMode )
  87. editor.insertElement( this.pre );
  88. this.commitContent( this.pre );
  89. */
  90. this.commitContent();
  91. }
  92. };
  93. } );
  94. editor.on( 'paste', function( evt ) {
  95. // Do NOT overwrite if HTML format is explicitly requested.
  96. // This allows pastefromword dominates over pastecode.
  97. /*
  98. if ( evt.data.type != 'html' )
  99. evt.data.type = 'text';
  100. */
  101. });
  102. editor.on( 'pasteState', function( evt ) {
  103. editor.getCommand( commandName ).setState( evt.data );
  104. });
  105. }
  106. });
  107. })();