word-count-update.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global window, document, console, BalloonEditor */
  6. const maxCharacters = 120;
  7. const container = document.querySelector( '.demo-update' );
  8. const progressCircle = document.querySelector( '.demo-update__chart__circle' );
  9. const charactersBox = document.querySelector( '.demo-update__chart__characters' );
  10. const wordsBox = document.querySelector( '.demo-update__words' );
  11. const circleCircumference = Math.floor( 2 * Math.PI * progressCircle.getAttribute( 'r' ) );
  12. const sendButton = document.querySelector( '.demo-update__send' );
  13. sendButton.addEventListener( 'click', () => {
  14. window.alert( 'Post sent!' ); // eslint-disable-line no-alert
  15. } );
  16. BalloonEditor
  17. .create( document.querySelector( '#demo-update__editor' ), {
  18. toolbar: {
  19. items: [
  20. 'heading',
  21. '|',
  22. 'bold',
  23. 'italic',
  24. 'bulletedList',
  25. 'numberedList',
  26. '|',
  27. 'outdent',
  28. 'indent',
  29. '|',
  30. 'blockQuote',
  31. 'link',
  32. '|',
  33. 'mediaEmbed',
  34. 'insertTable',
  35. '|',
  36. 'undo',
  37. 'redo'
  38. ],
  39. viewportTopOffset: window.getViewportTopOffsetConfig()
  40. },
  41. placeholder: 'Text of the post',
  42. table: {
  43. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  44. },
  45. wordCount: {
  46. onUpdate: stats => {
  47. const charactersProgress = stats.characters / maxCharacters * circleCircumference;
  48. const isLimitExceeded = stats.characters > maxCharacters;
  49. const isCloseToLimit = !isLimitExceeded && stats.characters > maxCharacters * .8;
  50. const circleDashArray = Math.min( charactersProgress, circleCircumference );
  51. // Set the stroke of the circle to show how many characters were typed.
  52. progressCircle.setAttribute( 'stroke-dasharray', `${ circleDashArray },${ circleCircumference }` );
  53. // Display the number of characters in the progress chart. When the limit is exceeded,
  54. // display how many characters should be removed.
  55. if ( isLimitExceeded ) {
  56. charactersBox.textContent = `-${ stats.characters - maxCharacters }`;
  57. } else {
  58. charactersBox.textContent = stats.characters;
  59. }
  60. wordsBox.textContent = `Words in the post: ${ stats.words }`;
  61. // If the content length is close to the characters limit, add a CSS class to warn the user.
  62. container.classList.toggle( 'demo-update__limit-close', isCloseToLimit );
  63. // If the character limit is exceeded, add a CSS class that makes the content's background red.
  64. container.classList.toggle( 'demo-update__limit-exceeded', isLimitExceeded );
  65. // If the character limit is exceeded, disable the send button.
  66. sendButton.toggleAttribute( 'disabled', isLimitExceeded );
  67. }
  68. }
  69. } )
  70. .catch( err => {
  71. console.error( err.stack );
  72. } );