contextualballoon.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals window, document, console:false */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import BalloonToolbar from '../../../src/toolbar/balloon/balloontoolbar';
  9. import ContextualBalloon from '../../../src/panel/balloon/contextualballoon';
  10. import View from '../../../src/view';
  11. class CustomStackHighlight {
  12. static get requires() {
  13. return [ ContextualBalloon ];
  14. }
  15. constructor( editor ) {
  16. this.editor = editor;
  17. this._markerToView = new Map();
  18. }
  19. init() {
  20. this.editor.conversion.for( 'editingDowncast' ).markerToHighlight( {
  21. model: 'highlight',
  22. view: data => {
  23. const color = data.markerName.split( ':' )[ 2 ];
  24. return { classes: 'highlight ' + color };
  25. }
  26. } );
  27. this.editor.model.document.selection.markers.on( 'add', ( evt, item ) => this._add( item ) );
  28. this.editor.model.document.selection.markers.on( 'remove', ( evt, item ) => this._remove( item ) );
  29. }
  30. _add( marker ) {
  31. const view = new View();
  32. view.setTemplate( {
  33. tag: 'p',
  34. attributes: {
  35. class: 'custom-view'
  36. },
  37. children: [
  38. { text: 'View in separate stack.' }
  39. ]
  40. } );
  41. this._markerToView.set( marker, view );
  42. this.editor.plugins.get( ContextualBalloon ).add( {
  43. view,
  44. stackId: 'custom-' + marker.name.split( ':' )[ 1 ],
  45. position: {
  46. target: this._getMarkerDomElement( marker )
  47. }
  48. } );
  49. }
  50. _remove( marker ) {
  51. this.editor.plugins.get( ContextualBalloon ).remove( this._markerToView.get( marker ) );
  52. this._markerToView.delete( marker );
  53. }
  54. _getMarkerDomElement( marker ) {
  55. const editing = this.editor.editing;
  56. const viewRange = editing.mapper.toViewRange( marker.getRange() );
  57. return editing.view.domConverter.viewRangeToDom( viewRange );
  58. }
  59. }
  60. ClassicEditor
  61. .create( document.querySelector( '#editor' ), {
  62. plugins: [ ArticlePluginSet, BalloonToolbar, CustomStackHighlight ],
  63. toolbar: [ 'bold', 'link' ],
  64. balloonToolbar: [ 'bold', 'link' ]
  65. } )
  66. .then( editor => {
  67. window.editor = editor;
  68. editor.model.change( writer => {
  69. const root = editor.model.document.getRoot();
  70. [
  71. { id: 1, start: [ 1, 5 ], end: [ 1, 26 ], color: 'yellow' },
  72. { id: 2, start: [ 1, 2 ], end: [ 1, 33 ], color: 'green' },
  73. { id: 3, start: [ 5, 20 ], end: [ 5, 35 ], color: 'blue' },
  74. { id: 4, start: [ 5, 15 ], end: [ 5, 40 ], color: 'pink' },
  75. { id: 5, start: [ 5, 10 ], end: [ 5, 45 ], color: 'yellow' }
  76. ].forEach( data => {
  77. writer.addMarker( `highlight:${ data.id }:${ data.color }`, {
  78. range: writer.createRange(
  79. writer.createPositionFromPath( root, data.start ),
  80. writer.createPositionFromPath( root, data.end )
  81. ),
  82. usingOperation: true
  83. } );
  84. } );
  85. } );
  86. } )
  87. .catch( err => {
  88. console.error( err.stack );
  89. } );