国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.5KB

  1. CKEDITOR.plugins.add( 'textindent', {
  2. icons: 'textindent',
  3. init: function( editor ) {
  4. var indentation = editor.config.indentation;
  5. var indentationKey = editor.config.indentationKey;
  6. if(typeof(indentation) == 'undefined')
  7. indentation = '2em';
  8. if(typeof(indentationKey) == 'undefined')
  9. indentationKey = 'tab';
  10. editor.ui.addButton( 'textindent', {
  11. label: '缩进',
  12. command: 'ident-paragraph',
  13. toolbar: "textindent"
  14. });
  15. if( indentationKey !== false){
  16. editor.on('key', function(ev) {
  17. if(ev.data.domEvent.$.key.toLowerCase() === indentationKey.toLowerCase().trim() || ev.data.keyCode === indentationKey){
  18. editor.execCommand('ident-paragraph');
  19. ev.cancel();
  20. }
  21. });
  22. }
  23. editor.on( 'selectionChange', function()
  24. {
  25. var style_textindente = new CKEDITOR.style({
  26. element: 'p',
  27. styles: { 'text-indent': indentation },
  28. overrides: [{
  29. element: 'text-indent', attributes: { 'size': '0'}
  30. }]
  31. });
  32. if( style_textindente.checkActive(editor.elementPath(), editor) )
  33. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
  34. else
  35. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
  36. });
  37. editor.addCommand("ident-paragraph", {
  38. allowedContent: 'p{text-indent}',
  39. requiredContent: 'p',
  40. exec: function(evt) {
  41. var range = editor.getSelection().getRanges()[0];
  42. var walker = new CKEDITOR.dom.walker( range ),
  43. node;
  44. var state = editor.getCommand('ident-paragraph').state;
  45. while ( ( node = walker.next() ) ) {
  46. if ( node.type == CKEDITOR.NODE_ELEMENT ) {
  47. if(node.getName() === "p"){
  48. editor.fire('saveSnapshot');
  49. if( state == CKEDITOR.TRISTATE_ON){
  50. node.removeStyle("text-indent");
  51. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
  52. }
  53. else{
  54. node.setStyle( "text-indent", indentation );
  55. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
  56. }
  57. }
  58. }
  59. }
  60. if(node === null){
  61. node = editor.getSelection().getStartElement().getAscendant('p', true);
  62. editor.fire('saveSnapshot');
  63. if( state == CKEDITOR.TRISTATE_ON){
  64. node.removeStyle("text-indent");
  65. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
  66. }
  67. else{
  68. node.setStyle( "text-indent", indentation );
  69. editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
  70. }
  71. }
  72. }
  73. });
  74. }
  75. });