word-count-update.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. 'blockQuote',
  27. 'link',
  28. '|',
  29. 'mediaEmbed',
  30. 'insertTable',
  31. '|',
  32. 'undo',
  33. 'redo'
  34. ],
  35. viewportTopOffset: window.getViewportTopOffsetConfig()
  36. },
  37. placeholder: 'Text of the post',
  38. table: {
  39. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  40. },
  41. wordCount: {
  42. onUpdate: stats => {
  43. const charactersProgress = stats.characters / maxCharacters * circleCircumference;
  44. const isLimitExceeded = stats.characters > maxCharacters;
  45. const isCloseToLimit = !isLimitExceeded && stats.characters > maxCharacters * .8;
  46. const circleDashArray = Math.min( charactersProgress, circleCircumference );
  47. // Set the stroke of the circle to show how many characters were typed.
  48. progressCircle.setAttribute( 'stroke-dasharray', `${ circleDashArray },${ circleCircumference }` );
  49. // Display the number of characters in the progress chart. When the limit is exceeded,
  50. // display how many characters should be removed.
  51. if ( isLimitExceeded ) {
  52. charactersBox.textContent = `-${ stats.characters - maxCharacters }`;
  53. } else {
  54. charactersBox.textContent = stats.characters;
  55. }
  56. wordsBox.textContent = `Words in the post: ${ stats.words }`;
  57. // If the content length is close to the characters limit, add a CSS class to warn the user.
  58. container.classList.toggle( 'demo-update__limit-close', isCloseToLimit );
  59. // If the character limit is exceeded, add a CSS class that makes the content's background red.
  60. container.classList.toggle( 'demo-update__limit-exceeded', isLimitExceeded );
  61. // If the character limit is exceeded, disable the send button.
  62. sendButton.toggleAttribute( 'disabled', isLimitExceeded );
  63. }
  64. }
  65. } )
  66. .catch( err => {
  67. console.error( err.stack );
  68. } );