2016-11-25 1 views
0

내 프로젝트 중 하나에서 CKEditor 4.5.7을 사용하고 있습니다. 아래의 스크린 샷과 같이 Format 콤보를 사용자 정의 했으므로 오른쪽 클릭을하면이 콤보 안에 모든 항목이 나타납니다.CKEditor 컨텍스트 (중첩 된) 메뉴 안에 콤보 항목 형식 추가

CKEditorFormatCombo

그리고 아래는 형식 콤보 코드입니다 :

config.format_tags = 'p;h3;h4;pre;ImageInline;ImageCentered;ImageCenteredWithDropShadow;FigureHeading;Equation;EquationDefinition;TableWithoutBorder'; 
config.format_ImageInline = { name: 'Image inline', element: 'img', attributes: { 'class': 'noborder' } }; 
config.format_ImageCentered = { name: 'Image centered', element: 'img', attributes: { 'class': 'noborderblock' } }; 
config.format_ImageCenteredWithDropShadow = { name: 'Image centered drop shadow', element: 'img', attributes: { 'class': 'border' } }; 
config.format_FigureHeading = { name: 'Figure/Table heading', element: 'p'/*['p', 'td']*/, attributes: { 'class': 'footing' } }; 
config.format_Equation = { name: 'Equation', element: 'table', attributes: { 'class': 'equation' } }; 
config.format_EquationDefinition = { name: 'Equation definition', element: 'table', attributes: { 'class': 'where' } }; 
config.format_TableWithoutBorder = { name: 'Table without border', element: 'table', attributes: { 'class': 'nobordertable' } }; 

의 스크린 샷 아래와 같이 나는 그들이 상황에 맞는 메뉴에 표시 얻을 수있었습니다 : CKEditor Context menu

하지만를 그들 각각에 대해 command 이름이 무엇인지 확실하지 않습니다. 즉

이미 CKEditor의 전체 소스 코드를 다운로드 ckeditor를 통해 사라 \ 플러그인 \ 형식 \ plugin.js하지만 command로 지정하기 위해 무엇을 알아낼 수 없습니다했다

command: 'format_ImageCentered' /*I need help here*/ command: 'format_ImageCenteredWithDropShadow' /*I need help here*/

.

내가 참조로 URL 아래 사용했다 :

var ck_article = CKEDITOR.replace("content", { customConfig: '<config js file path>', bodyClass: '<css class>' }); 
ck_article.on("instanceReady", function (evt) { 
    var editor = evt.editor;   
    /*Code for checking if editor has context menu or not removed for brevity*/ 
    //... 
    //... 
    editor.addMenuGroup('ck_group'); 

    editor.addMenuItem('bold', { 
     label: 'Bold', 
     command: 'bold', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('iconselector', { 
     label: '...', 
     command: 'iconselector', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('numberedlist', { 
     label: 'Numbered List', 
     command: 'numberedlist', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('bulletedlist', { 
     label: 'Bulleted List', 
     command: 'bulletedlist', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('link', { 
     label: 'Link', 
     command: 'link', 
     group: 'ck_group' 
    }); 

    editor.addMenuItems({ 
     formatting: { 
      label: 'Formatting', 
      group: 'ck_group', 
      getItems: function() { 
       var selection = editor.getSelection(); 
       //This is to nest items inside context menu of CKEditor 
       return { 
        format_ImageCentered: CKEDITOR.TRISTATE_ON, 
        format_ImageCenteredWithDropShadow: CKEDITOR.TRISTATE_ON 
       } 
      } 
     }, 

     format_ImageCentered: { 
      label: "Image centered", 
      group: 'ck_group', 
      command: 'format_ImageCentered' /*I need help here*/ 
     }, 

     format_ImageCenteredWithDropShadow: { 
      label: "Image centered drop shadow", 
      group: 'ck_group', 
      command: 'format_ImageCenteredWithDropShadow' /*I need help here*/ 
     } 
    }); 

    editor.contextMenu.addListener(function (element, selection, elementPath) { 

     var contentMenuItems = { 
      link: CKEDITOR.TRISTATE_ON, 
      bold: CKEDITOR.TRISTATE_ON, 
      numberedlist: CKEDITOR.TRISTATE_ON, 
      bulletedlist: CKEDITOR.TRISTATE_ON, 
      iconselector: CKEDITOR.TRISTATE_ON, 
      formatting: CKEDITOR.TRISTATE_ON 
     }; 

     if (element.getAscendant('a', true)) { 
      //If we are already inside 'a' tag then remove link from Context menu else we will end up with two "Link" menus 
      delete contentMenuItems.link 
     } 

     if ($.trim(selection.getSelectedText()) === '') { 
      //If no text is selected then remove bold from context menu 
      delete contentMenuItems.bold; 
      //contentMenuItems.bold = CKEDITOR.TRISTATE_DISABLED; //This doesn't work as the menu item is disabled but hover effect is still there 

      //Similarly remove link if nothing is selected as it will insert hyperlink text as words inside CKEditor 
      delete contentMenuItems.link; 
      //contentMenuItems.link = CKEDITOR.TRISTATE_DISABLED; //This doesn't work as the menu item is disabled but hover effect is still there 
     } 

     return contentMenuItems; 
    }); 
}); 

참고 :

답변

0

형식 조합에 나열된 모든 옵션마다 플러그인이 만들어졌습니다. 이보다 더 좋은 방법이 있다면 알려주세요. 다른 사람이 비슷한 문제를 발견하면 코드를 전달하고 있습니다. 플러그인이 모두 동일한 코드를 가지고있는 유일한 차이는 pluginName에 대한 값이 정확한 인 것이다 나는 단지 하나 개의 플러그인 코드를 붙여하고 Plugin Folder

: 스크린 샷 아래

플러그인 폴더 CKEditor 폴더 안에 보이는 방법을 보여줍니다 폴더 이름과 동일 : 다음 CKEditor 연장

//All the files inside folder stating with context_<name> have exact same code except pluginName variable. 
//I need to this to support format inside right click 
(function() { 
    "use strict"; 
    var pluginName = 'contextmenu_tablewithoutborder'; //This name will be used to add to 'config.extraPlugins' string 
    var commandName = pluginName; 
    // Register the plugin within the editor. 
    CKEDITOR.plugins.add(pluginName, { 
     // Register the icons. They must match command names. 
     icons: pluginName, 
     // The plugin initialization logic goes inside this method. 
     init: function (editor) { 
      // Define an editor command. 
      editor.addCommand(commandName, { //Command name must match with name provided in editor.ui.addButton 
       // Define the function that will be fired when the command is executed. 
       exec: function (editor) { 
        if (typeof editor.applyFormatStyles === 'function') 
         editor.applyFormatStyles(pluginName.split('_')[1]); 
        else 
         throw new Error('applyFormatStyles is not defined as function (' + pluginName + ')'); 
       } 
      }); 
     } 
    }); 
})(); 

아래에 첨가 방법 :

CKEDITOR.editor.prototype.getFormatStyles = function() { 
    var styles = {} 
    var editor = this; 
    var config = editor.config, 
     lang = editor.lang.format; 

    // Gets the list of tags from the settings. 
    var tags = config.format_tags.split(';'); 

    for (var i = 0; i < tags.length; i++) { 
     var tag = tags[i]; 
     var style = new CKEDITOR.style(config['format_' + tag]); 
     if (!editor.filter.customConfig || editor.filter.check(style)) { 
      styles[tag] = style; 
      styles[tag]._.enterMode = editor.config.enterMode;    
     } 
    } 

    return styles; 
} 

CKEDITOR.editor.prototype.applyFormatStyles = function (styleName) { 
    var editor = this; 
    var styles = editor.getFormatStyles(); 
    editor.focus(); 
    editor.fire('saveSnapshot'); 

    var style = styles[styleName], 
     elementPath = editor.elementPath(); 

    editor[style.checkActive(elementPath, editor) ? 'removeStyle' : 'applyStyle'](style); 

    // Save the undo snapshot after all changes are affected. (#4899) 
    setTimeout(function() { 
     editor.fire('saveSnapshot'); 
    }, 0); 
} 

나는 다음 내 설정 파일을 수정 및 추가 플러그인 등 모든 플러그인을 추가 : 다음

CKEDITOR.editorConfig = function (config) { 
    var extraPlugins = []; 
    //Remove other code for brevity 
    //... 
    //... 
    config.format_tags = 'p;h3;h4;pre;imageinline;imagecentered;imagecenteredwithdropshadow;figureheading;equation;equationdefinition;tablewithoutborder'; 
    config.format_imageinline = { name: 'Image inline', element: 'img', attributes: { 'class': 'noborder' } }; 
    config.format_imagecentered = { name: 'Image centered', element: 'img', attributes: { 'class': 'noborderblock' } }; 
    config.format_imagecenteredwithdropshadow = { name: 'Image centered drop shadow', element: 'img', attributes: { 'class': 'border' } }; 
    config.format_figureheading = { name: 'Figure/Table heading', element: 'p'/*['p', 'td']*/, attributes: { 'class': 'footing' } }; 
    config.format_equation = { name: 'Equation', element: 'table', attributes: { 'class': 'equation' } }; 
    config.format_equationdefinition = { name: 'Equation definition', element: 'table', attributes: { 'class': 'where' } }; 
    config.format_tablewithoutborder = { name: 'Table without border', element: 'table', attributes: { 'class': 'nobordertable' } }; 

    var contextmenu_plugins = config.format_tags.split(";"); 
    for (var i = 0; i < contextmenu_plugins.length; i++) { 
     var pluginName = contextmenu_plugins[i]; 
     extraPlugins.push("contextmenu_{0}".format(pluginName)) 
    } 

    config.extraPlugins = extraPlugins.join(','); 
} 

그리고를 마지막으로 나는 상황에 맞는 메뉴를 편집기 I 범위를 만들 때. 나는이 논리를 plugin.js 파일에 추가 할 수 있었지만 plugin.js 코드가 라인이나 2의 차이점을 제외하고는 정확히 동일하기를 원했기 때문에 거기에 추가하는 것을 신경 쓰지 않았다.

var ck_article = CKEDITOR.replace("content", { customConfig: '<config js file path>', bodyClass: '<css class>' }); 

ck_article.on("instanceReady", function (evt) { 
    var editor = evt.editor; 
    editor.addMenuGroup('ck_group'); 

    editor.addMenuItem('bold', { 
     label: 'Bold', 
     command: 'bold', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('iconselector', { 
     label: '...', 
     command: 'iconselector', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('numberedlist', { 
     label: 'Numbered List', 
     command: 'numberedlist', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('bulletedlist', { 
     label: 'Bulleted List', 
     command: 'bulletedlist', 
     group: 'ck_group' 
    }); 

    editor.addMenuItem('link', { 
     label: 'Link', 
     command: 'link', 
     group: 'ck_group' 
    }); 

    editor.addMenuItems({ 
     formatting: { 
      label: 'Formatting', 
      group: 'ck_group', 
      getItems: function() { 
       var selection = editor.getSelection(); 
       //This is to nest items inside context menu of CKEditor 
       var tags = editor.config.format_tags.split(";"); 
       var menu_items = { 
       }; 
       for (var i = 0; i < tags.length; i++) { 
        menu_items[tags[i]] = CKEDITOR.TRISTATE_ON; 
       } 
       //menu_items - will have object something like below 
       //{p: 1, h3: 1, h4: 1......} 
       return menu_items; 
      } 
     }, 

     p: { 
      label: "Normal", 
      group: 'ck_group', 
      command: 'contextmenu_p' 
     }, 

     h3: { 
      label: "Heading", 
      group: 'ck_group', 
      command: 'contextmenu_h3' 
     }, 

     h4: { 
      label: "Sub Heading", 
      group: 'ck_group', 
      command: 'contextmenu_h4' 
     }, 

     pre: { 
      label: "Formatted", 
      group: 'ck_group', 
      command: 'contextmenu_pre' 
     }, 

     imageinline: { 
      label: "Image inline", 
      group: 'ck_group', 
      command: 'contextmenu_imageinline' 
     }, 

     imagecentered: { 
      label: "Image centered", 
      group: 'ck_group', 
      command: 'contextmenu_imagecentered' 
     }, 

     imagecenteredwithdropshadow: { 
      label: "Image centered drop shadow", 
      group: 'ck_group', 
      command: 'contextmenu_imagecenteredwithdropshadow' 
     }, 

     figureheading: { 
      label: "Figure/Table heading", 
      group: 'ck_group', 
      command: 'contextmenu_figureheading' 
     }, 

     equation: { 
      label: "Equation", 
      group: 'ck_group', 
      command: 'contextmenu_equation' 
     }, 

     equationdefinition: { 
      label: "Equation definition", 
      group: 'ck_group', 
      command: 'contextmenu_equationdefinition' 
     }, 

     tablewithoutborder: { 
      label: "Table without border", 
      group: 'ck_group', 
      command: 'contextmenu_tablewithoutborder' 
     } 
    }); 

    editor.contextMenu.addListener(function (element, selection, elementPath) { 

     var contentMenuItems = { 
      link: CKEDITOR.TRISTATE_ON, 
      bold: CKEDITOR.TRISTATE_ON, 
      numberedlist: CKEDITOR.TRISTATE_ON, 
      bulletedlist: CKEDITOR.TRISTATE_ON, 
      iconselector: CKEDITOR.TRISTATE_ON, 
      formatting: CKEDITOR.TRISTATE_ON 
     }; 

     if (element.getAscendant('a', true)) { 
      //If we are already inside 'a' tag then remove link from Context menu else we will end up with two "Link" menus 
      delete contentMenuItems.link 
     } 

     if ($.trim(selection.getSelectedText()) === '') { 
      //If no text is selected then remove bold from context menu 
      delete contentMenuItems.bold; 

      //Similarly remove link if nothing is selected as it will insert hyperlink text as words inside CKEditor 
      delete contentMenuItems.link; 
     } 

     return contentMenuItems; 
    }); 
}); 

코드가 너무 많지만 다른 간단한 방법으로는 찾을 수 없습니다.

이 모든 보이는 방법이다 완료되면 : 이 CKEditor Nested Context Menu

빠른 정리 해보 :

  • 만 모든 plugin.js 파일에 변경 형식 콤보에 나타나는 각 항목에 대한
  • 만든 플러그인입니다 pluginName (폴더 이름 임)
  • 확장 CKEditor 및 applyFormatStyles plugin.js 파일에서 호출하는 방법을 추가하십시오.
  • 설정 파일에서 16,
  • 모든 이들과 같은 별도의 플러그인을 추가
  • 그리고 CKEditor 인스턴스의 생성에 상황에 맞는 메뉴를 확장