markers.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 buildModelConverter from '../../src/conversion/buildmodelconverter';
  16. import Position from '../../src/model/position';
  17. import Range from '../../src/model/range';
  18. const markerNames = [];
  19. let model = null;
  20. let _uid = 1;
  21. ClassicEditor.create( document.querySelector( '#editor' ), {
  22. plugins: [ Enter, Typing, Paragraph, Bold, Italic, List, Heading, Undo ],
  23. toolbar: [ 'headings', 'bold', 'italic', 'bulletedList', 'numberedList', 'undo', 'redo' ]
  24. } )
  25. .then( editor => {
  26. window.editor = editor;
  27. model = window.editor.editing.model;
  28. buildModelConverter().for( editor.editing.modelToView )
  29. .fromMarker( 'highlight' )
  30. .toVirtualSelection( data => {
  31. const color = data.markerName.split( ':' )[ 1 ];
  32. return {
  33. class: 'h-' + color,
  34. priority: 1
  35. };
  36. } );
  37. window.document.getElementById( 'add-yellow' ).addEventListener( 'mousedown', e => {
  38. e.preventDefault();
  39. addHighlight( 'yellow' );
  40. } );
  41. window.document.getElementById( 'add-red' ).addEventListener( 'mousedown', e => {
  42. e.preventDefault();
  43. addHighlight( 'red' );
  44. } );
  45. window.document.getElementById( 'remove-marker' ).addEventListener( 'mousedown', e => {
  46. e.preventDefault();
  47. removeHighlight();
  48. } );
  49. window.document.getElementById( 'move-to-start' ).addEventListener( 'mousedown', e => {
  50. e.preventDefault();
  51. moveSelectionToStart();
  52. } );
  53. window.document.getElementById( 'move-left' ).addEventListener( 'mousedown', e => {
  54. e.preventDefault();
  55. moveSelectionByOffset( -1 );
  56. } );
  57. window.document.getElementById( 'move-right' ).addEventListener( 'mousedown', e => {
  58. e.preventDefault();
  59. moveSelectionByOffset( 1 );
  60. } );
  61. model.enqueueChanges( () => {
  62. const root = model.getRoot();
  63. const range = new Range( new Position( root, [ 0, 10 ] ), new Position( root, [ 1, 5 ] ) );
  64. const name = 'highlight:yellow:' + uid();
  65. markerNames.push( name );
  66. model.markers.set( name, range );
  67. } );
  68. } )
  69. .catch( err => {
  70. console.error( err.stack );
  71. } );
  72. function uid() {
  73. return _uid++;
  74. }
  75. function addHighlight( color ) {
  76. model.enqueueChanges( () => {
  77. const range = Range.createFromRange( model.selection.getFirstRange() );
  78. const name = 'highlight:' + color + ':' + uid();
  79. markerNames.push( name );
  80. model.markers.set( name, range );
  81. } );
  82. }
  83. function removeHighlight() {
  84. model.enqueueChanges( () => {
  85. const pos = model.selection.getFirstPosition();
  86. for ( let i = 0; i < markerNames.length; i++ ) {
  87. const name = markerNames[ i ];
  88. const marker = model.markers.get( name );
  89. const range = marker.getRange();
  90. if ( range.containsPosition( pos ) || range.start.isEqual( pos ) || range.end.isEqual( pos ) ) {
  91. model.markers.remove( name );
  92. markerNames.splice( i, 1 );
  93. break;
  94. }
  95. }
  96. } );
  97. }
  98. function moveSelectionToStart() {
  99. const range = model.selection.getFirstRange();
  100. if ( range.isFlat ) {
  101. model.enqueueChanges( () => {
  102. model.batch().move( range, new Position( model.getRoot(), [ 0, 0 ] ) );
  103. } );
  104. }
  105. }
  106. function moveSelectionByOffset( offset ) {
  107. const range = model.selection.getFirstRange();
  108. const pos = offset < 0 ? range.start : range.end;
  109. if ( range.isFlat ) {
  110. model.enqueueChanges( () => {
  111. model.batch().move( range, pos.getShiftedBy( offset ) );
  112. } );
  113. }
  114. }