8
0

autolink.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module link/autolink
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import TextWatcher from '@ckeditor/ckeditor5-typing/src/textwatcher';
  10. import getLastTextLine from '@ckeditor/ckeditor5-typing/src/utils/getlasttextline';
  11. const MIN_LINK_LENGTH_WITH_SPACE_AT_END = 4; // Ie: "t.co " (length 5).
  12. const URL_REG_EXP = new RegExp(
  13. // Group 1: Line start or after a space.
  14. '(^|\\s)' + // Match .
  15. // Group 2: Full detected URL.
  16. '(' +
  17. // Group 3 + 4: Protocol + domain.
  18. '(([a-z]{3,9}:(?:\\/\\/)?)(?:[\\w]+)?[a-z0-9.-]+|(?:www\\.|[\\w]+)[a-z0-9.-]+)' +
  19. // Group 5: Optional path + query string + location.
  20. '((?:\\/[+~%/.\\w\\-_]*)?\\??(?:[-+=&;%@.\\w_]*)#?(?:[.!/\\\\\\w]*))?' +
  21. ')$', 'i' );
  22. const URL_GROUP_IN_MATCH = 2;
  23. // Simplified email test - should be run over previously found URL.
  24. const EMAIL_REG_EXP = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
  25. /**
  26. * The auto link plugin.
  27. *
  28. * @extends module:core/plugin~Plugin
  29. */
  30. export default class AutoLink extends Plugin {
  31. /**
  32. * @inheritDoc
  33. */
  34. static get pluginName() {
  35. return 'AutoLink';
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. init() {
  41. const editor = this.editor;
  42. const watcher = new TextWatcher( editor.model, text => {
  43. // 1. Detect "space" after a text with a potential link.
  44. if ( !isSingleSpaceAtTheEnd( text ) ) {
  45. return;
  46. }
  47. // 2. Check text before "space" or "enter".
  48. const url = getUrlAtTextEnd( text.substr( 0, text.length - 1 ) );
  49. if ( url ) {
  50. return { url };
  51. }
  52. } );
  53. const input = editor.plugins.get( 'Input' );
  54. watcher.on( 'matched:data', ( evt, data ) => {
  55. const { batch, range, url } = data;
  56. if ( !input.isInput( batch ) ) {
  57. return;
  58. }
  59. this._applyAutoLink( url, range );
  60. } );
  61. watcher.bind( 'isEnabled' ).to( this );
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. afterInit() {
  67. this._enableEnterHandling();
  68. this._enableShiftEnterHandling();
  69. }
  70. _enableEnterHandling() {
  71. const editor = this.editor;
  72. const model = editor.model;
  73. const enterCommand = editor.commands.get( 'enter' );
  74. enterCommand.on( 'execute', () => {
  75. const position = model.document.selection.getFirstPosition();
  76. const rangeToCheck = model.createRange(
  77. model.createPositionAt( position.parent.previousSibling, 0 ),
  78. model.createPositionAt( position.parent.previousSibling, 'end' )
  79. );
  80. this._checkAndApplyAutoLinkOnRange( rangeToCheck );
  81. } );
  82. }
  83. _enableShiftEnterHandling() {
  84. const editor = this.editor;
  85. const model = editor.model;
  86. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  87. shiftEnterCommand.on( 'execute', () => {
  88. const position = model.document.selection.getFirstPosition();
  89. const rangeToCheck = model.createRange(
  90. model.createPositionAt( position.parent, 0 ),
  91. position.getShiftedBy( -1 )
  92. );
  93. this._checkAndApplyAutoLinkOnRange( rangeToCheck );
  94. } );
  95. }
  96. _checkAndApplyAutoLinkOnRange( rangeToCheck ) {
  97. const { text, range } = getLastTextLine( rangeToCheck, this.editor.model );
  98. const url = getUrlAtTextEnd( text );
  99. if ( url ) {
  100. this._applyAutoLink( url, range, 0 );
  101. }
  102. }
  103. _applyAutoLink( linkHref, range, additionalOffset = 1 ) {
  104. // Enqueue change to make undo step.
  105. this.editor.model.enqueueChange( writer => {
  106. const linkRange = writer.createRange(
  107. range.end.getShiftedBy( -( additionalOffset + linkHref.length ) ),
  108. range.end.getShiftedBy( -additionalOffset )
  109. );
  110. const linkHrefValue = isEmail( linkHref ) ? `mailto://${ linkHref }` : linkHref;
  111. writer.setAttribute( 'linkHref', linkHrefValue, linkRange );
  112. } );
  113. }
  114. }
  115. function isSingleSpaceAtTheEnd( text ) {
  116. return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' && text[ text.length - 2 ] !== ' ';
  117. }
  118. function getUrlAtTextEnd( text ) {
  119. const match = URL_REG_EXP.exec( text );
  120. return match ? match[ URL_GROUP_IN_MATCH ] : null;
  121. }
  122. function isEmail( linkHref ) {
  123. return EMAIL_REG_EXP.exec( linkHref );
  124. }