1.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 Range from '../../../../src/model/range';
  9. import LivePosition from '../../../../src/model/liveposition';
  10. import buildModelConverter from '../../../../src/conversion/buildmodelconverter';
  11. import buildViewConverter from '../../../../src/conversion/buildviewconverter';
  12. import AttributeElement from '../../../../src/view/attributeelement';
  13. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  14. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  15. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  16. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  17. class Link extends Plugin {
  18. init() {
  19. const editor = this.editor;
  20. const data = editor.data;
  21. const editing = editor.editing;
  22. // Allow bold attribute on all inline nodes.
  23. editor.model.schema.extend( '$text', { allowAttributes: 'link' } );
  24. // Build converter from model to view for data and editing pipelines.
  25. buildModelConverter().for( data.modelToView, editing.modelToView )
  26. .fromAttribute( 'link' )
  27. .toElement( href => new AttributeElement( 'a', { href } ) );
  28. // Build converter from view to model for data pipeline.
  29. buildViewConverter().for( data.viewToModel )
  30. .fromElement( 'a' )
  31. .toAttribute( viewElement => ( { key: 'link', value: viewElement.getAttribute( 'href' ) } ) );
  32. }
  33. }
  34. class AutoLinker extends Plugin {
  35. init() {
  36. this.editor.model.document.on( 'change', () => {
  37. const changes = this.editor.model.document.differ.getChanges();
  38. for ( const entry of changes ) {
  39. if ( entry.type != 'insert' || entry.name != '$text' || !entry.position.textNode ) {
  40. continue;
  41. }
  42. const textNode = entry.position.textNode;
  43. const text = textNode.data;
  44. if ( !text ) {
  45. return;
  46. }
  47. const regexp = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
  48. let match;
  49. while ( ( match = regexp.exec( text ) ) !== null ) {
  50. const index = match.index;
  51. const url = match[ 0 ];
  52. const length = url.length;
  53. if ( entry.position.offset + entry.length == index + length ) {
  54. const livePos = LivePosition.createFromParentAndOffset( textNode.parent, index );
  55. this.editor.model.enqueueChange( writer => {
  56. const urlRange = Range.createFromPositionAndShift( livePos, length );
  57. writer.setAttribute( 'link', url, urlRange );
  58. } );
  59. return;
  60. }
  61. }
  62. }
  63. } );
  64. }
  65. }
  66. ClassicEditor.create( document.querySelector( '#editor' ), {
  67. plugins: [ Enter, Typing, Paragraph, Undo, Link, AutoLinker ],
  68. toolbar: [ 'undo', 'redo' ]
  69. } );