contextualballoon.js 3.2 KB

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