markers.js 4.2 KB

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