contextualballoon.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: () => {
  23. return { classes: 'highlight' };
  24. }
  25. } );
  26. this.editor.model.document.selection.markers.on( 'add', ( evt, item ) => this._add( item ) );
  27. this.editor.model.document.selection.markers.on( 'remove', ( evt, item ) => this._remove( item ) );
  28. }
  29. _add( marker ) {
  30. const view = new View();
  31. view.setTemplate( {
  32. tag: 'p',
  33. attributes: {
  34. class: 'custom-view'
  35. },
  36. children: [
  37. { text: 'View in separate stack.' }
  38. ]
  39. } );
  40. this._markerToView.set( marker, view );
  41. this.editor.plugins.get( ContextualBalloon ).add( {
  42. view,
  43. stackId: 'custom',
  44. position: {
  45. target: this._getMarkerDomElement( marker )
  46. }
  47. } );
  48. }
  49. _remove( marker ) {
  50. this.editor.plugins.get( ContextualBalloon ).remove( this._markerToView.get( marker ) );
  51. this._markerToView.delete( marker );
  52. }
  53. _getMarkerDomElement( marker ) {
  54. const editing = this.editor.editing;
  55. const viewElement = Array.from( editing.mapper.markerNameToElements( marker.name ).values() )[ 0 ];
  56. return editing.view.domConverter.mapViewToDom( viewElement );
  57. }
  58. }
  59. ClassicEditor
  60. .create( document.querySelector( '#editor' ), {
  61. plugins: [ ArticlePluginSet, BalloonToolbar, CustomStackHighlight ],
  62. toolbar: [ 'bold', 'link' ],
  63. balloonToolbar: [ 'bold', 'link' ]
  64. } )
  65. .then( editor => {
  66. window.editor = editor;
  67. editor.model.change( writer => {
  68. const root = editor.model.document.getRoot();
  69. [
  70. { id: 1, start: [ 1, 5 ], end: [ 1, 26 ] },
  71. { id: 2, start: [ 5, 5 ], end: [ 5, 26 ] },
  72. { id: 3, start: [ 10, 5 ], end: [ 10, 26 ] }
  73. ].forEach( data => {
  74. writer.addMarker( `highlight:${ data.id }`, {
  75. range: writer.createRange(
  76. writer.createPositionFromPath( root, data.start ),
  77. writer.createPositionFromPath( root, data.end )
  78. ),
  79. usingOperation: true
  80. } );
  81. } );
  82. } );
  83. } )
  84. .catch( err => {
  85. console.error( err.stack );
  86. } );