繁体   English   中英

Ckeditor Wordcount 插件以针对特定字段

[英]Ckeditor Wordcount plugin to target specific fields

我正在使用 CKEditor - WYSIWYG HTML 编辑器 Drupal 7 模块和 wordcount 插件。 我已经在 ckeditor plugins 目录中添加了 wordcount 插件文件并使其正常工作,但是我的配置最大字数和字符数等对于所有字段都是相同的。 我想为特定字段指定不同的最大字数和字符数。 这是我的配置:

ckeditor.config.js

使用 CKEDITOR.replace 我做了以下事情:

  CKEDITOR.replace('edit-body-und-0-summary',
      {
          extraPlugins: 'wordcount',
          wordcount: {
              showCharCount: true,
              maxCharCount: 123
          }
      });

这有效,但在刷新页面后,它又回到了全局最大字数和字符数。 Textrea ID 是edit-body-und-0-summary但这个标签在页面的其他地方重复。

如何向 textarea 标签添加唯一 ID 并为不同字段(例如摘要和正文字段)设置不同的字数

我想通了,希望这可以帮助其他人。

我在我的 Adminimal Sub 主题中添加了以下内容以将一个类添加到摘要文本区域:

function adminimal_sub_form_alter(&$form, &$form_state, $form_id) { 
 if (isset($form['#node']) && $form['#node']->type == 'article') {
 // Add class to body textarea for various content types. This is used for the Ckeditor wordcount plugin to target summary textarea. 
   $form['body']['und']['0']['summary']['#attributes']['class']['0'] = 'article-summary';
 }
}

然后下载了 Ckeditor 4 Wordcount 插件并将其保存在插件目录(sites/all/libraries/ckeditor/plugins)中。

在 Drupal UI 中,我转到 configuration > CKEditor Filtered HTML (edit) 并在 Advanced options 下的 Custom Javascript configuration 中我添加了一个路径到我的 ckeditor.config.js (config.customConfig = '/sites/all/themes/global/ js/ckeditor.config.js';)。

这是我的 ckeditor.config.js:

CKEDITOR.editorConfig = function(config) {

 if (this.element.hasClass('article-summary')) {
 config.wordcount = {
    showCharCount: true,
    maxCharCount: 170
    }
}
// Make CKEditor's edit area as high as the textarea would be.
 if (this.element.$.rows > 0) {
    config.height = this.element.$.rows * 20 + 'px';
 }
}

在这里我可以设置 maxwordcount 并加载更多,但对于这个项目只需要 maxwordcount。 这样,如果 Drupal Ckeditor 模块更新,此文件将不会受到影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM