Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Fixed manual test for #475.
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkups committed Feb 2, 2018
1 parent c447639 commit 133c508
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions tests/manual/tickets/475/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

import TreeWalker from '../../../../src/model/treewalker';
import Position from '../../../../src/model/position';
import Range from '../../../../src/model/range';
import LivePosition from '../../../../src/model/liveposition';

Expand Down Expand Up @@ -45,51 +42,45 @@ class Link extends Plugin {
}
}

const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;

class AutoLinker extends Plugin {
init() {
this.editor.model.document.on( 'change', ( event, type, changes, batch ) => {
if ( type != 'insert' ) {
return;
}
this.editor.model.document.on( 'change', () => {
const changes = this.editor.model.document.differ.getChanges();

for ( const value of changes.range.getItems( { singleCharacters: true } ) ) {
const walker = new TreeWalker( {
direction: 'backward',
startPosition: Position.createAfter( value )
} );
for ( const entry of changes ) {
if ( entry.type != 'insert' || entry.name != '$text' || !entry.position.textNode ) {
continue;
}

const currentValue = walker.next().value;
const text = currentValue.item.data;
const textNode = entry.position.textNode;
const text = textNode.data;

if ( !text ) {
return;
}

const matchedUrl = urlRegex.exec( text );

if ( !matchedUrl ) {
return;
const regexp = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
let match;

while ( ( match = regexp.exec( text ) ) !== null ) {
const index = match.index;
const url = match[ 0 ];
const length = url.length;

if ( entry.position.offset + entry.length == index + length ) {
const livePos = LivePosition.createFromParentAndOffset( textNode.parent, index );
this.editor.model.enqueueChange( writer => {
const urlRange = Range.createFromPositionAndShift( livePos, length );
writer.setAttribute( 'link', url, urlRange );
} );
return;
}
}

const url = matchedUrl[ 0 ];
const offset = _getLastPathPart( currentValue.nextPosition.path ) + matchedUrl.index;
const livePos = LivePosition.createFromParentAndOffset( currentValue.item.parent, offset );

this.editor.model.enqueueChange( batch, writer => {
const urlRange = Range.createFromPositionAndShift( livePos, url.length );
writer.setAttribute( 'link', url, urlRange );
} );
}
} );
}
}

function _getLastPathPart( path ) {
return path[ path.length - 1 ];
}

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Link, AutoLinker ],
toolbar: [ 'undo', 'redo' ]
Expand Down

0 comments on commit 133c508

Please sign in to comment.