Skip to content

Commit

Permalink
HTMLEditorField support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonom committed Mar 11, 2016
1 parent 6d480a1 commit 710659a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

If you see a field marked 'Description' you know roughly what type of content to put in there. But how do you know how much of it to write? A single sentence might do, but maybe a paragraph or more is required? A great content plan should recommend an ideal length for every type of content, so content authors and designers alike can make informed decisions.

This module extends the `TextField` and `TextareaField` classes in SilverStripe to allow you to set a recommended content length, and set soft upper and lower limits on character count.
This module extends the `TextField`, `TextareaField` and `HTMLEditorField` classes in SilverStripe to allow you to set a recommended content length, and set soft upper and lower limits on character count.

## Requirements

Expand All @@ -22,7 +22,7 @@ I promise it's worth your time to learn how to use Composer. If painless updatin

## How to use

With the module installed you can call call `setTargetLength()` on `TextField` and `TextareaField` form fields.
With the module installed you can call call `setTargetLength()` on `TextField`, `TextareaField` and `HTMLEditorField` form fields.

```php
// Ideal length: 100 characters
Expand All @@ -34,6 +34,10 @@ $field->setTargetLength(100);
// Minimum: 25
// Maximum: 150
$field->setTargetLength(100, 25, 150);

// Prefer to think in word count?
// 6 characters per word works okay for English
$field->setTargetLength(50*6);
```

## Want to see more like this?
Expand All @@ -44,7 +48,6 @@ I donate a lot of my time to open-source projects, so if you use this module in

## To Do

- [ ] Extension for HTMLEditorField?
- [ ] Translation
- [ ] Customise hint text through config

Expand Down
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ TextField:
TextareaField:
extensions:
- TextTargetLengthExtension
HTMLEditorField:
extensions:
- TextTargetLengthExtension
3 changes: 1 addition & 2 deletions code/TextTargetLengthExtension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Apply this extension to TextareaField and/or TextField
* Apply this extension to text editing form field such as TextareaField
* to allow setting of ideal, min and max lengths. These are
* soft limits only, to give CMS users an idea of target length.
*/
Expand All @@ -11,7 +11,6 @@ class TextTargetLengthExtension extends Extension
/**
* Set a target character length for text in a field.
*
*
* @param Int $idealCharCount the ideal number of characters for this text
* @param Int $minCharCount (default: null) the minimum number of characters for this text
* @param Int $maxCharCount (default: null) the maximum number of characters for this text
Expand Down
16 changes: 14 additions & 2 deletions javascript/text-target-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

updateCount: function() {
var field = $(this);
var countEl = field.next('p.target-length-count');
var countEl = field.siblings('p.target-length-count').first();
if (!countEl) return;
var charCount = field.val().length;
var charCount = this.getText().length;
if (field.data('previousCount') === charCount) return;
var ideal = field.data('targetIdealLength');
var min = field.data('targetMinLength');
Expand All @@ -29,6 +29,18 @@
countEl.html('Length target: <b>' + targetFulfilled + '%</b> <i>' + remark + '</i>');
field.data('previousCount', charCount);
},
getText: function() {
var field = $(this);
if (field.hasClass('htmleditor')) {
var editor = tinyMCE.getInstanceById(field.attr('ID'));
if (editor !== undefined) {
return $(editor.getContent()).text();
} else {
return $('<div />').html(field.val()).text();
}
}
return field.val();
},
onadd: function() {
// Insert extra markup
var field = $(this);
Expand Down

0 comments on commit 710659a

Please sign in to comment.