markers.js 3.9 KB

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