8
0

highlight.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global 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 Undo from '@ckeditor/ckeditor5-undo/src/undo';
  11. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  14. import List from '@ckeditor/ckeditor5-list/src/list';
  15. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  16. import buildModelConverter from '../../src/conversion/buildmodelconverter';
  17. import buildViewConverter from '../../src/conversion/buildviewconverter';
  18. import ModelRange from '../../src/model/range';
  19. import ModelElement from '../../src/model/element';
  20. import ViewContainerElement from '../../src/view/containerelement';
  21. import ViewText from '../../src/view/text';
  22. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  23. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  24. import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  25. class FancyWidget extends Plugin {
  26. static get requires() {
  27. return [ Widget ];
  28. }
  29. init() {
  30. const editor = this.editor;
  31. const schema = editor.model.schema;
  32. const data = editor.data;
  33. const editing = editor.editing;
  34. // Configure schema.
  35. schema.register( 'fancywidget', {
  36. isObject: true
  37. } );
  38. schema.extend( 'fancywidget', { allowIn: '$root' } );
  39. // Build converter from model to view for editing pipeline.
  40. buildModelConverter().for( editing.modelToView )
  41. .fromElement( 'fancywidget' )
  42. .toElement( () => {
  43. const widgetElement = new ViewContainerElement( 'figure', { class: 'fancy-widget' }, new ViewText( 'widget' ) );
  44. return toWidget( widgetElement );
  45. } );
  46. // Build converter from view element to model element for data pipeline.
  47. buildViewConverter().for( data.viewToModel )
  48. .fromElement( 'figure' )
  49. .toElement( () => new ModelElement( 'fancywidget' ) );
  50. }
  51. }
  52. ClassicEditor.create( global.document.querySelector( '#editor' ), {
  53. plugins: [ Enter, Typing, Paragraph, Undo, Heading, Bold, Italic, List, FancyWidget ],
  54. toolbar: [ 'headings', 'undo', 'redo', 'bold', 'italic', 'numberedList', 'bulletedList' ]
  55. } )
  56. .then( editor => {
  57. window.editor = editor;
  58. buildModelConverter()
  59. .for( editor.editing.modelToView )
  60. .fromMarker( 'marker' )
  61. .toHighlight( data => ( { class: 'highlight-' + data.markerName.split( ':' )[ 1 ] } ) );
  62. document.getElementById( 'add-marker-yellow' ).addEventListener( 'mousedown', evt => {
  63. addMarker( editor, 'yellow' );
  64. evt.preventDefault();
  65. } );
  66. document.getElementById( 'add-marker-blue' ).addEventListener( 'mousedown', evt => {
  67. addMarker( editor, 'blue' );
  68. evt.preventDefault();
  69. } );
  70. document.getElementById( 'add-marker-red' ).addEventListener( 'mousedown', evt => {
  71. addMarker( editor, 'red' );
  72. evt.preventDefault();
  73. } );
  74. document.getElementById( 'remove-marker-yellow' ).addEventListener( 'mousedown', evt => {
  75. removeMarker( editor, 'yellow' );
  76. evt.preventDefault();
  77. } );
  78. document.getElementById( 'remove-marker-blue' ).addEventListener( 'mousedown', evt => {
  79. removeMarker( editor, 'blue' );
  80. evt.preventDefault();
  81. } );
  82. document.getElementById( 'remove-marker-red' ).addEventListener( 'mousedown', evt => {
  83. removeMarker( editor, 'red' );
  84. evt.preventDefault();
  85. } );
  86. document.getElementById( 'remove-markers' ).addEventListener( 'mousedown', evt => {
  87. const markers = editor.model.markers;
  88. editor.model.change( () => {
  89. for ( const marker of markers ) {
  90. markers.remove( marker );
  91. }
  92. } );
  93. evt.preventDefault();
  94. } );
  95. } )
  96. .catch( err => {
  97. console.error( err.stack );
  98. } );
  99. function addMarker( editor, color ) {
  100. editor.model.change( () => {
  101. const range = ModelRange.createFromRange( editor.model.document.selection.getFirstRange() );
  102. editor.model.markers.set( 'marker:' + color, range );
  103. } );
  104. }
  105. function removeMarker( editor, color ) {
  106. editor.model.change( () => {
  107. editor.model.markers.remove( 'marker:' + color );
  108. } );
  109. }