├── lang ├── _manifest_exclude ├── en.yml ├── hr.yml ├── de.yml └── ru.yml ├── _config.php ├── screenshots └── character-count.gif ├── _config └── config.yml ├── .editorconfig ├── client ├── css │ └── text-target-length.css └── javascript │ └── text-target-length.js ├── composer.json ├── LICENSE ├── src └── TextTargetLengthExtension.php └── README.md /lang/_manifest_exclude: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /_config.php: -------------------------------------------------------------------------------- 1 | {value}% {remark}" 4 | LengthTooShort: "Keep going!" 5 | LengthTooLong: "Too long" 6 | LengthAdequate: "Okay" 7 | LengthIdeal: "Great!" 8 | -------------------------------------------------------------------------------- /lang/hr.yml: -------------------------------------------------------------------------------- 1 | hr: 2 | TextTargetLength: 3 | LengthTarget: "Ciljana dužina: {value}% {remark}" 4 | LengthTooShort: "Nastavite pisati!" 5 | LengthTooLong: "Predugo" 6 | LengthAdequate: "Dobro" 7 | LengthIdeal: "Odlično!" 8 | -------------------------------------------------------------------------------- /lang/de.yml: -------------------------------------------------------------------------------- 1 | de: 2 | TextTargetLength: 3 | LengthTarget: "Textlänge: {value}% {remark}" 4 | LengthTooShort: "der optimalen Länge" 5 | LengthTooLong: "zu lange" 6 | LengthAdequate: "nahezu optimal" 7 | LengthIdeal: "optimal" 8 | -------------------------------------------------------------------------------- /lang/ru.yml: -------------------------------------------------------------------------------- 1 | ru: 2 | TextTargetLength: 3 | LengthTarget: "Необходимая длинна: {value}% {remark}" 4 | LengthTooShort: "Слишком короткий текст!" 5 | LengthTooLong: "Слишком длинный текст!" 6 | LengthAdequate: "Хорошо!" 7 | LengthIdeal: "Идеально!" 8 | -------------------------------------------------------------------------------- /_config/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | Name: text-target-length 3 | --- 4 | SilverStripe\Forms\TextField: 5 | extensions: 6 | - JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension 7 | SilverStripe\Forms\TextareaField: 8 | extensions: 9 | - JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension 10 | SilverStripe\Forms\HTMLEditor\HTMLEditorField: 11 | extensions: 12 | - JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # For more information about the properties used in this file, 2 | # please see the EditorConfig documentation: 3 | # http://editorconfig.org 4 | 5 | # Default for all files 6 | [*] 7 | charset = utf-8 8 | end_of_line = lf 9 | indent_size = 4 10 | indent_style = tab 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | # PSR-2 for PHP 15 | [*.php] 16 | indent_style = space 17 | 18 | # The indent size used in the package.json file cannot be changed: 19 | # https://github.com/npm/npm/pull/3180#issuecomment-16336516 20 | [{*.yml,package.json}] 21 | indent_size = 2 22 | indent_style = space 23 | -------------------------------------------------------------------------------- /client/css/text-target-length.css: -------------------------------------------------------------------------------- 1 | .target-length p.target-length-count { 2 | color: #8796a3; 3 | margin: 0 auto; 4 | padding: .25em 0; 5 | } 6 | .target-length p.target-length-count i, 7 | .target-length p.target-length-count b { 8 | color: #00a651; 9 | } 10 | .target-length p.target-length-count b { 11 | font-weight: bold; 12 | } 13 | .target-length p.target-length-count.over i, 14 | .target-length p.target-length-count.over b { 15 | color: #e3080a; 16 | } 17 | .target-length p.target-length-count.under i, 18 | .target-length p.target-length-count.under b { 19 | color: #e39d08; 20 | } 21 | .input-group p.target-length-count { 22 | width: 100%; 23 | } 24 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jonom/silverstripe-text-target-length", 3 | "description": "Set character length recommendations on SilverStripe text form fields", 4 | "type": "silverstripe-vendormodule", 5 | "keywords": ["silverstripe", "textfield", "textareafield", "target"], 6 | "license": "BSD-3-Clause", 7 | "authors": [{ 8 | "name": "Jonathon Menz", 9 | "homepage": "http://jonathonmenz.com" 10 | }], 11 | "require": { 12 | "silverstripe/framework": "^4.0 || ^5.0 || ^6.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "JonoM\\SilverStripeTextTargetLength\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "expose": [ 21 | "client" 22 | ] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Jonathon Menz 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of silverstripe-text-target-length nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /src/TextTargetLengthExtension.php: -------------------------------------------------------------------------------- 1 | owner; 26 | $idealCharCount = (int)$idealCharCount; 27 | if (!$idealCharCount > 0) return $field; 28 | 29 | // Set defaults 30 | if ($minCharCount === null) $minCharCount = round($idealCharCount * .75); 31 | if ($maxCharCount === null) $maxCharCount = round($idealCharCount * 1.25); 32 | 33 | // Validate 34 | if (!($maxCharCount >= $idealCharCount && $idealCharCount >= $minCharCount)) return $field; 35 | 36 | // Activate 37 | $field->addExtraClass('target-length'); 38 | $field->setAttribute('data-target-ideal-length', $idealCharCount); 39 | $field->setAttribute('data-target-min-length', $minCharCount); 40 | $field->setAttribute('data-target-max-length', $maxCharCount); 41 | 42 | $field->setAttribute('data-hint-length-target', _t('TextTargetLength.LengthTarget', 'Length target: {value}% {remark}')); 43 | $field->setAttribute('data-hint-length-tooshort', _t('TextTargetLength.LengthTooShort', 'Keep going!')); 44 | $field->setAttribute('data-hint-length-toolong', _t('TextTargetLength.LengthTooLong', 'Too long')); 45 | $field->setAttribute('data-hint-length-adequate', _t('TextTargetLength.LengthAdequate', 'Okay')); 46 | $field->setAttribute('data-hint-length-ideal', _t('TextTargetLength.LengthIdeal', 'Great!')); 47 | 48 | Requirements::javascript('jonom/silverstripe-text-target-length:client/javascript/text-target-length.js'); 49 | Requirements::css('jonom/silverstripe-text-target-length:client/css/text-target-length.css'); 50 | 51 | return $field; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Text Target Length for Silverstripe CMS 2 | 3 | ![Character limits in action](screenshots/character-count.gif) 4 | 5 | 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. 6 | 7 | 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. 8 | 9 | ## Requirements 10 | 11 | Silverstripe 4|5|6 (3.1+ in previous releases) 12 | 13 | ## Installation 14 | 15 | `composer require jonom/silverstripe-text-target-length` 16 | 17 | [Packagist listing](https://packagist.org/packages/jonom/silverstripe-text-target-length) 18 | 19 | ## How to use 20 | 21 | With the module installed you can call call `setTargetLength()` on `TextField`, `TextareaField` and `HTMLEditorField` form fields. 22 | 23 | ```php 24 | // Ideal length: 100 characters 25 | // Minimum: 75 (automatically set at 75% of ideal) 26 | // Maximum: 125 (automatically set at 125% of ideal) 27 | $field->setTargetLength(100); 28 | 29 | // Ideal length: 100 characters 30 | // Minimum: 25 31 | // Maximum: 150 32 | $field->setTargetLength(100, 25, 150); 33 | 34 | // Prefer to think in word count? 35 | // 6 characters per word works okay for English 36 | $field->setTargetLength(50*6); 37 | ``` 38 | 39 | ### Customise hint text 40 | 41 | This module supports translation through yml, so if you want to change the hint text that is displayed when users are typing, just create your own language file to override the one included in the module. 42 | 43 | ### Front-end use 44 | 45 | If you want to use this module outside of the CMS, you will need to load a copy of jQuery and jQuery Entwine in to the page. Example: 46 | 47 | ```php 48 | Requirements::javascript('silverstripe/admin:thirdparty/jquery/jquery.js'); 49 | Requirements::javascript('silverstripe/admin:thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); 50 | ``` 51 | 52 | ## Maintainer contact 53 | 54 | [Jono Menz](https://jonomenz.com) 55 | 56 | ## Sponsorship 57 | 58 | If you want to boost morale of the maintainer you're welcome to make a small monthly donation through [**GitHub**](https://github.com/sponsors/jonom), or a one time donation through [**PayPal**](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Z5HEZREZSKA6A). ❤️ Thank you! 59 | 60 | Please also feel free to [get in touch](https://jonomenz.com) if you want to hire the maintainer to develop a new feature, or discuss another opportunity. 61 | -------------------------------------------------------------------------------- /client/javascript/text-target-length.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | $.entwine('ss.targetlength', function($){ 3 | $('input.target-length, textarea.target-length').entwine({ 4 | updateCount: function() { 5 | var field = $(this); 6 | var countEl = field.siblings('p.target-length-count').first(); 7 | if (!countEl) return; 8 | var charCount = this.getText().length; 9 | if (field.data('previousCount') === charCount) return; 10 | var ideal = field.data('targetIdealLength'); 11 | var min = field.data('targetMinLength'); 12 | var max = field.data('targetMaxLength'); 13 | var targetFulfilled = Math.round((charCount / ideal)*20)*5; //5% increments 14 | var remark = field.data('hintLengthIdeal'); 15 | var remarkClass = 'good'; 16 | if ((charCount >= min && charCount < ((min + ideal) / 2)) || (charCount <= max && charCount > ((max + ideal) / 2))) { 17 | remark = field.data('hintLengthAdequate'); 18 | } else if (charCount < min) { 19 | remark = field.data('hintLengthTooshort'); 20 | remarkClass = 'under'; 21 | if (charCount === 0) remark = ''; 22 | } else if (charCount > max) { 23 | remark = field.data('hintLengthToolong'); 24 | remarkClass = 'over'; 25 | } 26 | countEl.attr('class', remarkClass + ' target-length-count'); 27 | countEl.html(field.data('hintLengthTarget').replace('{value}', targetFulfilled).replace('{remark}', remark)); 28 | field.data('previousCount', charCount); 29 | }, 30 | getText: function() { 31 | var field = $(this); 32 | if (field.hasClass('htmleditor')) { 33 | var editor = tinymce.get(field.attr('ID')); 34 | if (editor !== undefined) { 35 | return $(editor.getContent()).text(); 36 | } else { 37 | return $('
').html(field.val()).text(); 38 | } 39 | } 40 | return field.val(); 41 | }, 42 | onadd: function() { 43 | // Insert extra markup 44 | var field = $(this); 45 | field.parent().append('

'); 46 | this.updateCount(); 47 | // TinyMCE instances need some extra work 48 | if (field.hasClass('htmleditor')) { 49 | var editor = tinymce.get(field.attr('ID')); 50 | if (editor !== undefined) { 51 | function updateCount() { 52 | field.updateCount(); 53 | } 54 | editor.on('keyup', updateCount); 55 | editor.on('change', updateCount); 56 | editor.on('click', updateCount); 57 | editor.on('paste', updateCount); 58 | editor.on('input', updateCount); 59 | editor.on('init', updateCount); 60 | } 61 | } 62 | }, 63 | onpropertychange: function() { 64 | this.updateCount(); 65 | }, 66 | onchange: function() { 67 | this.updateCount(); 68 | }, 69 | onclick: function() { 70 | this.updateCount(); 71 | }, 72 | onkeyup: function() { 73 | this.updateCount(); 74 | }, 75 | oninput: function() { 76 | this.updateCount(); 77 | }, 78 | onpaste: function() { 79 | this.updateCount(); 80 | } 81 | }); 82 | }); 83 | }(jQuery)); 84 | --------------------------------------------------------------------------------