33 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
44 */
55
6- /* global window, document, console, ClassicEditor */
6+ /* global window, document, console, BalloonEditor */
77
8- ClassicEditor
9- . create ( document . querySelector ( '#demo-editor-update ' ) , {
8+ BalloonEditor
9+ . create ( document . querySelector ( '#demo-update__editor ' ) , {
1010 toolbar : {
1111 items : [
1212 'heading' ,
@@ -26,42 +26,61 @@ ClassicEditor
2626 ] ,
2727 viewportTopOffset : window . getViewportTopOffsetConfig ( )
2828 } ,
29+ placeholder : 'Text of the post' ,
2930 table : {
3031 contentToolbar : [ 'tableColumn' , 'tableRow' , 'mergeTableCells' ]
3132 }
3233 } )
3334 . then ( editor => {
35+ const maxCharacters = 120 ;
3436 const wordCountPlugin = editor . plugins . get ( 'WordCount' ) ;
37+ const container = document . querySelector ( '.demo-update' ) ;
38+ const progressCircle = document . querySelector ( '.demo-update__chart__circle' ) ;
39+ const charactersBox = document . querySelector ( '.demo-update__chart__characters' ) ;
40+ const wordsBox = document . querySelector ( '.demo-update__words' ) ;
41+ const sendButton = document . querySelector ( '.demo-update__send' ) ;
42+ const circleCircumference = Math . floor ( 2 * Math . PI * progressCircle . getAttribute ( 'r' ) ) ;
3543
36- const progressBar = document . querySelector ( '.customized-count progress' ) ;
37- const colorBox = document . querySelector ( '.customized-count__color-box' ) ;
44+ // Update the UI on editor load.
45+ updateWordCountStatsUI ( wordCountPlugin . characters , wordCountPlugin . words ) ;
3846
47+ // Update the UI as the content of the editor changes.
3948 wordCountPlugin . on ( 'update' , ( evt , data ) => {
40- const charactersHue = calculateHue ( {
41- characters : data . characters ,
42- greenUntil : 70 ,
43- maxCharacters : 120
44- } ) ;
45-
46- progressBar . value = data . words ;
47- colorBox . style . setProperty ( '--hue' , charactersHue ) ;
49+ updateWordCountStatsUI ( data . characters , data . words ) ;
4850 } ) ;
4951
50- // Calculates the hue based on the number of characters.
51- //
52- // For the character counter:
53- //
54- // * below greenUntil - Returns green.
55- // * between greenUntil and maxCharacters - Returns a hue between green and red.
56- // * above maxCharacters - Returns red.
57- function calculateHue ( { characters, greenUntil, maxCharacters } ) {
58- const greenHue = 70 ;
59- const redHue = 0 ;
60- const progress = Math . max ( 0 , Math . min ( 1 , ( characters - greenUntil ) / ( maxCharacters - greenUntil ) ) ) ; // 0-1
61- const discreetProgress = Math . floor ( progress * 10 ) / 10 ; // 0, 0.1, 0.2, ..., 1
52+ function updateWordCountStatsUI ( currentCharacters , currentWords ) {
53+ const charactersProgress = currentCharacters / maxCharacters * circleCircumference ;
54+ const isLimitExceeded = currentCharacters > maxCharacters ;
55+ const isCloseToLimit = ! isLimitExceeded && currentCharacters > maxCharacters * .8 ;
56+ const circleDashArray = Math . min ( charactersProgress , circleCircumference ) ;
57+
58+ // Set the stroke of the circle to show the how many characters were typed.
59+ progressCircle . setAttribute ( 'stroke-dasharray' , `${ circleDashArray } ,${ circleCircumference } ` ) ;
60+
61+ // Display the number of characters in the progress chart. When exceeded the limit,
62+ // display how many characters should be removed.
63+ if ( isLimitExceeded ) {
64+ charactersBox . textContent = `-${ currentCharacters - maxCharacters } ` ;
65+ } else {
66+ charactersBox . textContent = currentCharacters ;
67+ }
68+
69+ wordsBox . textContent = `Words in the post: ${ currentWords } ` ;
6270
63- return ( redHue - greenHue ) * discreetProgress + greenHue ;
71+ // If the content length is close to the characters limit, add a CSS class to warns the user.
72+ container . classList . toggle ( 'demo-update__limit-close' , isCloseToLimit ) ;
73+
74+ // If exceeded the characters limit, add a CSS class that makes the content's background red.
75+ container . classList . toggle ( 'demo-update__limit-exceeded' , isLimitExceeded ) ;
76+
77+ // If exceeded the characters limit, disable the send button.
78+ sendButton . toggleAttribute ( 'disabled' , isLimitExceeded ) ;
6479 }
80+
81+ sendButton . addEventListener ( 'click' , ( ) => {
82+ window . alert ( 'Post sent!' ) ; // eslint-disable-line no-alert
83+ } ) ;
6584 } )
6685 . catch ( err => {
6786 console . error ( err . stack ) ;
0 commit comments