1.js 2.7 KB

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