| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module link/linkediting
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import {
- downcastAttributeToElement
- } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
- import { upcastElementToAttribute } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
- import LinkCommand from './linkcommand';
- import UnlinkCommand from './unlinkcommand';
- import { createLinkElement } from './utils';
- import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
- import findLinkRange from './findlinkrange';
- import '../theme/link.css';
- const HIGHLIGHT_CLASSES = [ 'ck', 'ck-link_selected' ];
- /**
- * The link engine feature.
- *
- * It introduces the `linkHref="url"` attribute in the model which renders to the view as a `<a href="url">` element.
- *
- * @extends module:core/plugin~Plugin
- */
- export default class LinkEditing extends Plugin {
- /**
- * @inheritDoc
- */
- init() {
- const editor = this.editor;
- // Allow link attribute on all inline nodes.
- editor.model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
- editor.conversion.for( 'downcast' )
- .add( downcastAttributeToElement( { model: 'linkHref', view: createLinkElement } ) );
- editor.conversion.for( 'upcast' )
- .add( upcastElementToAttribute( {
- view: {
- name: 'a',
- attributes: {
- href: true
- }
- },
- model: {
- key: 'linkHref',
- value: viewElement => viewElement.getAttribute( 'href' )
- }
- } ) );
- // Create linking commands.
- editor.commands.add( 'link', new LinkCommand( editor ) );
- editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
- // Enable two-step caret movement for `linkHref` attribute.
- bindTwoStepCaretToAttribute( editor.editing.view, editor.model, this, 'linkHref' );
- // Setup highlight over selected link.
- this._setupLinkHighlight();
- }
- /**
- * Adds a visual highlight style to a link in which the selection is anchored.
- * Together with two-step caret movement, they indicate that the user is typing inside the link.
- *
- * Highlight is turned on by adding `.ck .ck-link_selected` classes to the link in the view:
- *
- * * the classes are removed before conversion has started, as callbacks added with `'highest'` priority
- * to {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} events,
- * * the classes are added in the view post fixer, after other changes in the model tree were converted to the view.
- *
- * This way, adding and removing highlight does not interfere with conversion.
- *
- * @private
- */
- _setupLinkHighlight() {
- const editor = this.editor;
- const view = editor.editing.view;
- const highlightedLinks = new Set();
- // Adding the class.
- view.document.registerPostFixer( writer => {
- const selection = editor.model.document.selection;
- if ( selection.hasAttribute( 'linkHref' ) ) {
- const modelRange = findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ) );
- const viewRange = editor.editing.mapper.toViewRange( modelRange );
- // There might be multiple `a` elements in the `viewRange`, for example, when the `a` element is
- // broken by a UIElement.
- for ( const item of viewRange.getItems() ) {
- if ( item.is( 'a' ) ) {
- writer.addClass( HIGHLIGHT_CLASSES, item );
- highlightedLinks.add( item );
- }
- }
- }
- } );
- // Removing the class.
- editor.conversion.for( 'editingDowncast' ).add( dispatcher => {
- // Make sure the highlight is removed on every possible event, before conversion is started.
- dispatcher.on( 'insert', removeHighlight, { priority: 'highest' } );
- dispatcher.on( 'remove', removeHighlight, { priority: 'highest' } );
- dispatcher.on( 'attribute', removeHighlight, { priority: 'highest' } );
- dispatcher.on( 'selection', removeHighlight, { priority: 'highest' } );
- function removeHighlight() {
- view.change( writer => {
- for ( const item of highlightedLinks.values() ) {
- writer.removeClass( HIGHLIGHT_CLASSES, item );
- highlightedLinks.delete( item );
- }
- } );
- }
- } );
- }
- }
|