475.js 2.9 KB

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