markers.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. window.document.getElementById( 'add-yellow' ).addEventListener( 'mousedown', e => {
  42. e.preventDefault();
  43. addHighlight( 'yellow' );
  44. } );
  45. window.document.getElementById( 'add-red' ).addEventListener( 'mousedown', e => {
  46. e.preventDefault();
  47. addHighlight( 'red' );
  48. } );
  49. window.document.getElementById( 'remove-marker' ).addEventListener( 'mousedown', e => {
  50. e.preventDefault();
  51. removeHighlight();
  52. } );
  53. window.document.getElementById( 'move-to-start' ).addEventListener( 'mousedown', e => {
  54. e.preventDefault();
  55. moveSelectionToStart();
  56. } );
  57. window.document.getElementById( 'move-left' ).addEventListener( 'mousedown', e => {
  58. e.preventDefault();
  59. moveSelectionByOffset( -1 );
  60. } );
  61. window.document.getElementById( 'move-right' ).addEventListener( 'mousedown', e => {
  62. e.preventDefault();
  63. moveSelectionByOffset( 1 );
  64. } );
  65. model.change( writer => {
  66. const root = model.document.getRoot();
  67. const range = new Range( new Position( root, [ 0, 10 ] ), new Position( root, [ 0, 16 ] ) );
  68. const name = 'highlight:yellow:' + uid();
  69. markerNames.push( name );
  70. writer.addMarker( name, { range, usingOperation: false } );
  71. } );
  72. } )
  73. .catch( err => {
  74. console.error( err.stack );
  75. } );
  76. function uid() {
  77. return _uid++;
  78. }
  79. function addHighlight( color ) {
  80. model.change( writer => {
  81. const range = Range.createFromRange( model.document.selection.getFirstRange() );
  82. const name = 'highlight:' + color + ':' + uid();
  83. markerNames.push( name );
  84. writer.addMarker( name, { range, usingOperation: false } );
  85. } );
  86. }
  87. function removeHighlight() {
  88. model.change( writer => {
  89. const pos = model.document.selection.getFirstPosition();
  90. for ( let i = 0; i < markerNames.length; i++ ) {
  91. const name = markerNames[ i ];
  92. const marker = model.markers.get( name );
  93. const range = marker.getRange();
  94. if ( range.containsPosition( pos ) || range.start.isEqual( pos ) || range.end.isEqual( pos ) ) {
  95. writer.removeMarker( name );
  96. markerNames.splice( i, 1 );
  97. break;
  98. }
  99. }
  100. } );
  101. }
  102. function moveSelectionToStart() {
  103. const range = model.document.selection.getFirstRange();
  104. if ( range.isFlat ) {
  105. model.change( writer => {
  106. writer.move( range, new Position( model.document.getRoot(), [ 0, 0 ] ) );
  107. } );
  108. }
  109. }
  110. function moveSelectionByOffset( offset ) {
  111. const range = model.document.selection.getFirstRange();
  112. const pos = offset < 0 ? range.start : range.end;
  113. if ( range.isFlat ) {
  114. model.change( writer => {
  115. writer.move( range, pos.getShiftedBy( offset ) );
  116. } );
  117. }
  118. }