|
@@ -9,8 +9,10 @@
|
|
|
|
|
|
|
|
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
import TextWatcher from '@ckeditor/ckeditor5-typing/src/textwatcher';
|
|
import TextWatcher from '@ckeditor/ckeditor5-typing/src/textwatcher';
|
|
|
|
|
+import getLastTextLine from '@ckeditor/ckeditor5-typing/src/utils/getlasttextline';
|
|
|
|
|
|
|
|
const regexp = /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)) $/;
|
|
const regexp = /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)) $/;
|
|
|
|
|
+const regExpII = /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*))$/;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* The auto link plugin.
|
|
* The auto link plugin.
|
|
@@ -51,18 +53,61 @@ export default class AutoLink extends Plugin {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const url = match[ 1 ];
|
|
|
|
|
|
|
+ applyAutoLink( match[ 1 ], range, editor );
|
|
|
|
|
+ } );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Enqueue change to make undo step.
|
|
|
|
|
- editor.model.enqueueChange( writer => {
|
|
|
|
|
- const linkRange = writer.createRange(
|
|
|
|
|
- range.end.getShiftedBy( -( 1 + url.length ) ),
|
|
|
|
|
- range.end.getShiftedBy( -1 )
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @inheritDoc
|
|
|
|
|
+ */
|
|
|
|
|
+ afterInit() {
|
|
|
|
|
+ const editor = this.editor;
|
|
|
|
|
|
|
|
- // TODO: use command for decorators support.
|
|
|
|
|
- writer.setAttribute( 'linkHref', url, linkRange );
|
|
|
|
|
- } );
|
|
|
|
|
- } );
|
|
|
|
|
|
|
+ const enterCommand = editor.commands.get( 'enter' );
|
|
|
|
|
+ const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
|
|
|
|
|
+
|
|
|
|
|
+ shiftEnterCommand.on( 'execute', () => {
|
|
|
|
|
+ const position = editor.model.document.selection.getFirstPosition();
|
|
|
|
|
+
|
|
|
|
|
+ const rangeToCheck = editor.model.createRange(
|
|
|
|
|
+ editor.model.createPositionAt( position.parent, 0 ),
|
|
|
|
|
+ position.getShiftedBy( -1 )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ checkAndApplyAutoLinkOnRange( rangeToCheck, editor );
|
|
|
|
|
+ }, { priority: 'low' } );
|
|
|
|
|
+
|
|
|
|
|
+ enterCommand.on( 'execute', () => {
|
|
|
|
|
+ const position = editor.model.document.selection.getFirstPosition();
|
|
|
|
|
+
|
|
|
|
|
+ const rangeToCheck = editor.model.createRange(
|
|
|
|
|
+ editor.model.createPositionAt( position.parent.previousSibling, 0 ),
|
|
|
|
|
+ editor.model.createPositionAt( position.parent.previousSibling, 'end' )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ checkAndApplyAutoLinkOnRange( rangeToCheck, editor );
|
|
|
|
|
+ }, { priority: 'low' } );
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function applyAutoLink( linkHref, range, editor, additionalOffset = 1 ) {
|
|
|
|
|
+ // Enqueue change to make undo step.
|
|
|
|
|
+ editor.model.enqueueChange( writer => {
|
|
|
|
|
+ const linkRange = writer.createRange(
|
|
|
|
|
+ range.end.getShiftedBy( -( additionalOffset + linkHref.length ) ),
|
|
|
|
|
+ range.end.getShiftedBy( -additionalOffset )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ writer.setAttribute( 'linkHref', linkHref, linkRange );
|
|
|
|
|
+ } );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function checkAndApplyAutoLinkOnRange( rangeToCheck, editor ) {
|
|
|
|
|
+ const { text, range } = getLastTextLine( rangeToCheck, editor.model );
|
|
|
|
|
+
|
|
|
|
|
+ const match = regExpII.exec( text );
|
|
|
|
|
+
|
|
|
|
|
+ if ( match ) {
|
|
|
|
|
+ applyAutoLink( match[ 1 ], range, editor, 0 );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|