8
0

1.js 2.7 KB

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