markers.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /* globals console, window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  8. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  12. import List from '@ckeditor/ckeditor5-list/src/list';
  13. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  14. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  15. import Position from '../../src/model/position';
  16. import Range from '../../src/model/range';
  17. const markerNames = [];
  18. let model = null;
  19. let _uid = 1;
  20. ClassicEditor
  21. .create( document.querySelector( '#editor' ), {
  22. plugins: [ Enter, Typing, Paragraph, Bold, Italic, List, Heading, Undo ],
  23. toolbar: [ 'heading', '|', 'bold', 'italic', 'bulletedList', 'numberedList', 'undo', 'redo' ]
  24. } )
  25. .then( editor => {
  26. window.editor = editor;
  27. model = editor.model;
  28. editor.conversion.for( 'editingDowncast' ).markerToHighlight( {
  29. model: 'highlight',
  30. view: data => {
  31. const color = data.markerName.split( ':' )[ 1 ];
  32. return {
  33. classes: 'h-' + color,
  34. priority: 1
  35. };
  36. }
  37. } );
  38. editor.conversion.for( 'dataDowncast' ).markerToHighlight( {
  39. model: 'highlight',
  40. view: data => {
  41. const color = data.markerName.split( ':' )[ 1 ];
  42. return {
  43. classes: 'h-' + color,
  44. priority: 1
  45. };
  46. }
  47. } );
  48. window.document.getElementById( 'add-yellow' ).addEventListener( 'mousedown', e => {
  49. e.preventDefault();
  50. addHighlight( 'yellow' );
  51. } );
  52. window.document.getElementById( 'add-red' ).addEventListener( 'mousedown', e => {
  53. e.preventDefault();
  54. addHighlight( 'red' );
  55. } );
  56. window.document.getElementById( 'remove-marker' ).addEventListener( 'mousedown', e => {
  57. e.preventDefault();
  58. removeHighlight();
  59. } );
  60. window.document.getElementById( 'move-to-start' ).addEventListener( 'mousedown', e => {
  61. e.preventDefault();
  62. moveSelectionToStart();
  63. } );
  64. window.document.getElementById( 'move-left' ).addEventListener( 'mousedown', e => {
  65. e.preventDefault();
  66. moveSelectionByOffset( -1 );
  67. } );
  68. window.document.getElementById( 'move-right' ).addEventListener( 'mousedown', e => {
  69. e.preventDefault();
  70. moveSelectionByOffset( 1 );
  71. } );
  72. model.change( writer => {
  73. const root = model.document.getRoot();
  74. const range = new Range( new Position( root, [ 0, 10 ] ), new Position( root, [ 0, 16 ] ) );
  75. const name = 'highlight:yellow:' + uid();
  76. markerNames.push( name );
  77. writer.addMarker( name, { range, usingOperation: false, affectsData: true } );
  78. } );
  79. } )
  80. .catch( err => {
  81. console.error( err.stack );
  82. } );
  83. function uid() {
  84. return _uid++;
  85. }
  86. function addHighlight( color ) {
  87. model.change( writer => {
  88. const range = model.document.selection.getFirstRange();
  89. const name = 'highlight:' + color + ':' + uid();
  90. markerNames.push( name );
  91. writer.addMarker( name, { range, usingOperation: false } );
  92. } );
  93. }
  94. function removeHighlight() {
  95. model.change( writer => {
  96. const pos = model.document.selection.getFirstPosition();
  97. for ( let i = 0; i < markerNames.length; i++ ) {
  98. const name = markerNames[ i ];
  99. const marker = model.markers.get( name );
  100. const range = marker.getRange();
  101. if ( range.containsPosition( pos ) || range.start.isEqual( pos ) || range.end.isEqual( pos ) ) {
  102. writer.removeMarker( name );
  103. markerNames.splice( i, 1 );
  104. break;
  105. }
  106. }
  107. } );
  108. }
  109. function moveSelectionToStart() {
  110. const range = model.document.selection.getFirstRange();
  111. if ( range.isFlat ) {
  112. model.change( writer => {
  113. writer.move( range, new Position( model.document.getRoot(), [ 0, 0 ] ) );
  114. } );
  115. }
  116. }
  117. function moveSelectionByOffset( offset ) {
  118. const range = model.document.selection.getFirstRange();
  119. const pos = offset < 0 ? range.start : range.end;
  120. if ( range.isFlat ) {
  121. model.change( writer => {
  122. writer.move( range, pos.getShiftedBy( offset ) );
  123. } );
  124. }
  125. }