1.js 2.7 KB

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