8
0

1.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import TreeWalker from '../../../../src/model/treewalker';
  9. import Position from '../../../../src/model/position';
  10. import Range from '../../../../src/model/range';
  11. import LivePosition from '../../../../src/model/liveposition';
  12. import buildModelConverter from '../../../../src/conversion/buildmodelconverter';
  13. import buildViewConverter from '../../../../src/conversion/buildviewconverter';
  14. import AttributeElement from '../../../../src/view/attributeelement';
  15. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  16. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  17. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  18. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  19. class Link extends Plugin {
  20. init() {
  21. const editor = this.editor;
  22. const data = editor.data;
  23. const editing = editor.editing;
  24. // Allow bold attribute on all inline nodes.
  25. editor.model.schema.extend( '$text', { allowAttributes: 'link' } );
  26. // Build converter from model to view for data and editing pipelines.
  27. buildModelConverter().for( data.modelToView, editing.modelToView )
  28. .fromAttribute( 'link' )
  29. .toElement( href => new AttributeElement( 'a', { href } ) );
  30. // Build converter from view to model for data pipeline.
  31. buildViewConverter().for( data.viewToModel )
  32. .fromElement( 'a' )
  33. .toAttribute( viewElement => ( { key: 'link', value: viewElement.getAttribute( 'href' ) } ) );
  34. }
  35. }
  36. const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;
  37. class AutoLinker extends Plugin {
  38. init() {
  39. this.editor.model.document.on( 'change', ( event, type, changes, batch ) => {
  40. if ( type != 'insert' ) {
  41. return;
  42. }
  43. for ( const value of changes.range.getItems( { singleCharacters: true } ) ) {
  44. const walker = new TreeWalker( {
  45. direction: 'backward',
  46. startPosition: Position.createAfter( value )
  47. } );
  48. const currentValue = walker.next().value;
  49. const text = currentValue.item.data;
  50. if ( !text ) {
  51. return;
  52. }
  53. const matchedUrl = urlRegex.exec( text );
  54. if ( !matchedUrl ) {
  55. return;
  56. }
  57. const url = matchedUrl[ 0 ];
  58. const offset = _getLastPathPart( currentValue.nextPosition.path ) + matchedUrl.index;
  59. const livePos = LivePosition.createFromParentAndOffset( currentValue.item.parent, offset );
  60. this.editor.model.enqueueChange( batch, writer => {
  61. const urlRange = Range.createFromPositionAndShift( livePos, url.length );
  62. writer.setAttribute( 'link', url, urlRange );
  63. } );
  64. }
  65. } );
  66. }
  67. }
  68. function _getLastPathPart( path ) {
  69. return path[ path.length - 1 ];
  70. }
  71. ClassicEditor.create( document.querySelector( '#editor' ), {
  72. plugins: [ Enter, Typing, Paragraph, Undo, Link, AutoLinker ],
  73. toolbar: [ 'undo', 'redo' ]
  74. } );