Skip to content

Commit

Permalink
Word Limit field setting
Browse files Browse the repository at this point in the history
Resolves #107
  • Loading branch information
brandonkelly committed Aug 11, 2023
1 parent 3426ecd commit 4674a93
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Added the “Word Limit” field setting. ([#107](https://github.com/craftcms/ckeditor/issues/107))
- The `ckeditor/convert` action will now find and convert `craft\fields\MissingField` instances that were meant to be Redactor fields.

This comment has been minimized.

Copy link
@zhuoxunyi

zhuoxunyi Aug 23, 2023

#``

- CKEditor fields with the “Insert table” button now include the `TableProperties` and `TableCellProperties` plugins. ([#103](https://github.com/craftcms/ckeditor/issues/103))
- Updated CKEditor 5 to 39.0.1.
Expand Down
46 changes: 45 additions & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public static function displayName(): string
*/
public bool $showWordCount = false;

/**
* @var int|null The total number of words allowed.
* @since 3.5.0
*/
public ?int $wordLimit = null;

/**
* @var string|array|null The volumes that should be available for image selection.
* @since 1.2.0
Expand Down Expand Up @@ -141,6 +147,37 @@ public function __construct($config = [])
parent::__construct($config);
}

/**
* @inheritdoc
*/
public function getElementValidationRules(): array
{
$rules = [];

if ($this->wordLimit) {
$rules[] = [
function(ElementInterface $element) {
$value = strip_tags((string)$element->getFieldValue($this->handle));
if (
// regex copied from the WordCount plugin, for consistency
preg_match_all('/(?:[\p{L}\p{N}]+\S?)+/', $value, $matches) &&
count($matches[0]) > $this->wordLimit
) {
$element->addError(
"field:$this->handle",
Craft::t('ckeditor', '{field} should contain at most {max, number} {max, plural, one{word} other{words}}.', [
'field' => Craft::t('site', $this->name),
'max' => $this->wordLimit,
]),
);
}
}
];
}

return $rules;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -302,6 +339,7 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st

$baseConfigJs = Json::encode($event->baseConfig);
$showWordCountJs = Json::encode($this->showWordCount);
$wordLimitJs = $this->wordLimit ?: 0;

$view->registerJs(<<<JS
(($) => {
Expand All @@ -324,7 +362,13 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
num: stats.characters,
}));
}
$('#' + $wordCountIdJs).html(Craft.escapeHtml(statText.join(', ')) || '&nbsp;');
const container = $('#' + $wordCountIdJs);
container.html(Craft.escapeHtml(statText.join(', ')) || '&nbsp;');
if ($wordLimitJs && stats.words > $wordLimitJs) {
container.addClass('error');
} else {
container.removeClass('error');
}
onUpdate(stats);
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/templates/_field-settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
on: field.showWordCount
}) }}

{{ forms.textField({
id: 'word-limit',
name: 'wordLimit',
label: 'Word Limit'|t('ckeditor'),
value: field.wordLimit,
type: 'number',
size: 5,
}) }}

{{ forms.lightswitchField({
id: 'enable-source-editing-for-non-admins',
name: 'enableSourceEditingForNonAdmins',
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en/ckeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
'The type of column this field should get in the database.' => 'The type of column this field should get in the database.',
'Toolbar' => 'Toolbar',
'View available settings' => 'View available settings',
'Word Limit' => 'Word Limit',
'You can save custom {name} configs as {ext} files in {path}.' => 'You can save custom {name} configs as {ext} files in {path}.',
'{attribute} isn’t valid JSON.' => '{attribute} isn’t valid JSON.',
'{field} should contain at most {max, number} {max, plural, one{word} other{words}}.' => '{field} should contain at most {max, number} {max, plural, one{word} other{words}}.',
];

0 comments on commit 4674a93

Please sign in to comment.