markers.js 4.0 KB

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