markers.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. import ViewAttributeElement from '../../src/view/attributeelement';
  19. const markerNames = [];
  20. let model = null;
  21. let _uid = 1;
  22. ClassicEditor.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. .toElement( data => {
  32. const color = data.name.split( ':' )[ 1 ];
  33. return new ViewAttributeElement( 'span', { class: 'h-' + color } );
  34. } );
  35. window.document.getElementById( 'add-yellow' ).addEventListener( 'mousedown', e => {
  36. e.preventDefault();
  37. addHighlight( 'yellow' );
  38. } );
  39. window.document.getElementById( 'add-red' ).addEventListener( 'mousedown', e => {
  40. e.preventDefault();
  41. addHighlight( 'red' );
  42. } );
  43. window.document.getElementById( 'remove-marker' ).addEventListener( 'mousedown', e => {
  44. e.preventDefault();
  45. removeHighlight();
  46. } );
  47. window.document.getElementById( 'move-to-start' ).addEventListener( 'mousedown', e => {
  48. e.preventDefault();
  49. moveSelectionToStart();
  50. } );
  51. window.document.getElementById( 'move-left' ).addEventListener( 'mousedown', e => {
  52. e.preventDefault();
  53. moveSelectionByOffset( -1 );
  54. } );
  55. window.document.getElementById( 'move-right' ).addEventListener( 'mousedown', e => {
  56. e.preventDefault();
  57. moveSelectionByOffset( 1 );
  58. } );
  59. model.enqueueChanges( () => {
  60. const root = model.getRoot();
  61. const range = new Range( new Position( root, [ 0, 10 ] ), new Position( root, [ 0, 16 ] ) );
  62. const name = 'highlight:yellow:' + uid();
  63. markerNames.push( name );
  64. model.markers.set( name, range );
  65. } );
  66. } )
  67. .catch( err => {
  68. console.error( err.stack );
  69. } );
  70. function uid() {
  71. return _uid++;
  72. }
  73. function addHighlight( color ) {
  74. model.enqueueChanges( () => {
  75. const range = Range.createFromRange( model.selection.getFirstRange() );
  76. const name = 'highlight:' + color + ':' + uid();
  77. markerNames.push( name );
  78. model.markers.set( name, range );
  79. } );
  80. }
  81. function removeHighlight() {
  82. model.enqueueChanges( () => {
  83. const pos = model.selection.getFirstPosition();
  84. for ( let i = 0; i < markerNames.length; i++ ) {
  85. const name = markerNames[ i ];
  86. const marker = model.markers.get( name );
  87. const range = marker.getRange();
  88. if ( range.containsPosition( pos ) || range.start.isEqual( pos ) || range.end.isEqual( pos ) ) {
  89. model.markers.remove( name );
  90. markerNames.splice( i, 1 );
  91. break;
  92. }
  93. }
  94. } );
  95. }
  96. function moveSelectionToStart() {
  97. const range = model.selection.getFirstRange();
  98. if ( range.isFlat ) {
  99. model.enqueueChanges( () => {
  100. model.batch().move( range, new Position( model.getRoot(), [ 0, 0 ] ) );
  101. } );
  102. }
  103. }
  104. function moveSelectionByOffset( offset ) {
  105. const range = model.selection.getFirstRange();
  106. const pos = offset < 0 ? range.start : range.end;
  107. if ( range.isFlat ) {
  108. model.enqueueChanges( () => {
  109. model.batch().move( range, pos.getShiftedBy( offset ) );
  110. } );
  111. }
  112. }