-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathlinkediting.ts
701 lines (580 loc) · 22.7 KB
/
linkediting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module link/linkediting
*/
import {
Plugin,
type Editor
} from 'ckeditor5/src/core';
import {
MouseObserver,
type Model,
type Schema,
type Writer,
type ViewElement,
type ModelDeleteContentEvent,
type ModelInsertContentEvent,
type ViewDocumentKeyDownEvent,
type ViewDocumentMouseDownEvent,
type ViewDocumentClickEvent,
type ViewDocumentSelectionChangeEvent
} from 'ckeditor5/src/engine';
import {
Input,
TwoStepCaretMovement,
inlineHighlight,
findAttributeRange,
type ViewDocumentDeleteEvent
} from 'ckeditor5/src/typing';
import {
ClipboardPipeline,
type ClipboardContentInsertionEvent
} from 'ckeditor5/src/clipboard';
import { keyCodes, env } from 'ckeditor5/src/utils';
import LinkCommand from './linkcommand';
import UnlinkCommand from './unlinkcommand';
import ManualDecorator from './utils/manualdecorator';
import {
createLinkElement,
ensureSafeUrl,
getLocalizedDecorators,
normalizeDecorators,
openLink,
addLinkProtocolIfApplicable,
type NormalizedLinkDecoratorAutomaticDefinition,
type NormalizedLinkDecoratorManualDefinition
} from './utils';
import '../theme/link.css';
const HIGHLIGHT_CLASS = 'ck-link_selected';
const DECORATOR_AUTOMATIC = 'automatic';
const DECORATOR_MANUAL = 'manual';
const EXTERNAL_LINKS_REGEXP = /^(https?:)?\/\//;
/**
* The link engine feature.
*
* It introduces the `linkHref="url"` attribute in the model which renders to the view as a `<a href="url">` element
* as well as `'link'` and `'unlink'` commands.
*/
export default class LinkEditing extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'LinkEditing' as const;
}
/**
* @inheritDoc
*/
public static get requires() {
// Clipboard is required for handling cut and paste events while typing over the link.
return [ TwoStepCaretMovement, Input, ClipboardPipeline ] as const;
}
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
editor.config.define( 'link', {
addTargetToExternalLinks: false
} );
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
// Allow link attribute on all inline nodes.
editor.model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
editor.conversion.for( 'dataDowncast' )
.attributeToElement( { model: 'linkHref', view: createLinkElement } );
editor.conversion.for( 'editingDowncast' )
.attributeToElement( { model: 'linkHref', view: ( href, conversionApi ) => {
return createLinkElement( ensureSafeUrl( href ), conversionApi );
} } );
editor.conversion.for( 'upcast' )
.elementToAttribute( {
view: {
name: 'a',
attributes: {
href: true
}
},
model: {
key: 'linkHref',
value: ( viewElement: ViewElement ) => viewElement.getAttribute( 'href' )
}
} );
// Create linking commands.
editor.commands.add( 'link', new LinkCommand( editor ) );
editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
const linkDecorators = getLocalizedDecorators( editor.t, normalizeDecorators( editor.config.get( 'link.decorators' ) ) );
this._enableAutomaticDecorators( linkDecorators
.filter( ( item ): item is NormalizedLinkDecoratorAutomaticDefinition => item.mode === DECORATOR_AUTOMATIC ) );
this._enableManualDecorators( linkDecorators
.filter( ( item ): item is NormalizedLinkDecoratorManualDefinition => item.mode === DECORATOR_MANUAL ) );
// Enable two-step caret movement for `linkHref` attribute.
const twoStepCaretMovementPlugin = editor.plugins.get( TwoStepCaretMovement );
twoStepCaretMovementPlugin.registerAttribute( 'linkHref' );
// Setup highlight over selected link.
inlineHighlight( editor, 'linkHref', 'a', HIGHLIGHT_CLASS );
// Handle link following by CTRL+click or ALT+ENTER
this._enableLinkOpen();
// Change the attributes of the selection in certain situations after the link was inserted into the document.
this._enableInsertContentSelectionAttributesFixer();
// Handle a click at the beginning/end of a link element.
this._enableClickingAfterLink();
// Handle typing over the link.
this._enableTypingOverLink();
// Handle removing the content after the link element.
this._handleDeleteContentAfterLink();
// Handle adding default protocol to pasted links.
this._enableClipboardIntegration();
}
/**
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorAutomaticDefinition automatic decorators}
* and registers a {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher downcast dispatcher}
* for each one of them. Downcast dispatchers are obtained using the
* {@link module:link/utils/automaticdecorators~AutomaticDecorators#getDispatcher} method.
*
* **Note**: This method also activates the automatic external link decorator if enabled with
* {@link module:link/linkconfig~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}.
*/
private _enableAutomaticDecorators( automaticDecoratorDefinitions: Array<NormalizedLinkDecoratorAutomaticDefinition> ): void {
const editor = this.editor;
// Store automatic decorators in the command instance as we do the same with manual decorators.
// Thanks to that, `LinkImageEditing` plugin can re-use the same definitions.
const command: LinkCommand = editor.commands.get( 'link' )!;
const automaticDecorators = command.automaticDecorators;
// Adds a default decorator for external links.
if ( editor.config.get( 'link.addTargetToExternalLinks' ) ) {
automaticDecorators.add( {
id: 'linkIsExternal',
mode: DECORATOR_AUTOMATIC,
callback: url => !!url && EXTERNAL_LINKS_REGEXP.test( url ),
attributes: {
target: '_blank',
rel: 'noopener noreferrer'
}
} );
}
automaticDecorators.add( automaticDecoratorDefinitions );
if ( automaticDecorators.length ) {
editor.conversion.for( 'downcast' ).add( automaticDecorators.getDispatcher() );
}
}
/**
* Processes an array of configured {@link module:link/linkconfig~LinkDecoratorManualDefinition manual decorators},
* transforms them into {@link module:link/utils/manualdecorator~ManualDecorator} instances and stores them in the
* {@link module:link/linkcommand~LinkCommand#manualDecorators} collection (a model for manual decorators state).
*
* Also registers an {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement attribute-to-element}
* converter for each manual decorator and extends the {@link module:engine/model/schema~Schema model's schema}
* with adequate model attributes.
*/
private _enableManualDecorators( manualDecoratorDefinitions: Array<NormalizedLinkDecoratorManualDefinition> ): void {
if ( !manualDecoratorDefinitions.length ) {
return;
}
const editor = this.editor;
const command: LinkCommand = editor.commands.get( 'link' )!;
const manualDecorators = command.manualDecorators;
manualDecoratorDefinitions.forEach( decoratorDefinition => {
editor.model.schema.extend( '$text', { allowAttributes: decoratorDefinition.id } );
// Keeps reference to manual decorator to decode its name to attributes during downcast.
const decorator = new ManualDecorator( decoratorDefinition );
manualDecorators.add( decorator );
editor.conversion.for( 'downcast' ).attributeToElement( {
model: decorator.id,
view: ( manualDecoratorValue, { writer, schema }, { item } ) => {
// Manual decorators for block links are handled e.g. in LinkImageEditing.
if ( !( item.is( 'selection' ) || schema.isInline( item ) ) ) {
return;
}
if ( manualDecoratorValue ) {
const element = writer.createAttributeElement( 'a', decorator.attributes, { priority: 5 } );
if ( decorator.classes ) {
writer.addClass( decorator.classes, element );
}
for ( const key in decorator.styles ) {
writer.setStyle( key, decorator.styles[ key ], element );
}
writer.setCustomProperty( 'link', true, element );
return element;
}
}
} );
editor.conversion.for( 'upcast' ).elementToAttribute( {
view: {
name: 'a',
...decorator._createPattern()
},
model: {
key: decorator.id
}
} );
} );
}
/**
* Attaches handlers for {@link module:engine/view/document~Document#event:enter} and
* {@link module:engine/view/document~Document#event:click} to enable link following.
*/
private _enableLinkOpen(): void {
const editor = this.editor;
const view = editor.editing.view;
const viewDocument = view.document;
this.listenTo<ViewDocumentClickEvent>( viewDocument, 'click', ( evt, data ) => {
const shouldOpen = env.isMac ? data.domEvent.metaKey : data.domEvent.ctrlKey;
if ( !shouldOpen ) {
return;
}
let clickedElement: Element | null = data.domTarget;
if ( clickedElement.tagName.toLowerCase() != 'a' ) {
clickedElement = clickedElement.closest( 'a' );
}
if ( !clickedElement ) {
return;
}
const url = clickedElement.getAttribute( 'href' );
if ( !url ) {
return;
}
evt.stop();
data.preventDefault();
openLink( url );
}, { context: '$capture' } );
// Open link on Alt+Enter.
this.listenTo<ViewDocumentKeyDownEvent>( viewDocument, 'keydown', ( evt, data ) => {
const linkCommand: LinkCommand = editor.commands.get( 'link' )!;
const url = linkCommand!.value;
const shouldOpen = !!url && data.keyCode === keyCodes.enter && data.altKey;
if ( !shouldOpen ) {
return;
}
evt.stop();
openLink( url );
} );
}
/**
* Starts listening to {@link module:engine/model/model~Model#event:insertContent} and corrects the model
* selection attributes if the selection is at the end of a link after inserting the content.
*
* The purpose of this action is to improve the overall UX because the user is no longer "trapped" by the
* `linkHref` attribute of the selection and they can type a "clean" (`linkHref`–less) text right away.
*
* See https://github.com/ckeditor/ckeditor5/issues/6053.
*/
private _enableInsertContentSelectionAttributesFixer(): void {
const editor = this.editor;
const model = editor.model;
const selection = model.document.selection;
this.listenTo<ModelInsertContentEvent>( model, 'insertContent', () => {
const nodeBefore = selection.anchor!.nodeBefore;
const nodeAfter = selection.anchor!.nodeAfter;
// NOTE: ↰ and ↱ represent the gravity of the selection.
// The only truly valid case is:
//
// ↰
// ...<$text linkHref="foo">INSERTED[]</$text>
//
// If the selection is not "trapped" by the `linkHref` attribute after inserting, there's nothing
// to fix there.
if ( !selection.hasAttribute( 'linkHref' ) ) {
return;
}
// Filter out the following case where a link with the same href (e.g. <a href="foo">INSERTED</a>) is inserted
// in the middle of an existing link:
//
// Before insertion:
// ↰
// <$text linkHref="foo">l[]ink</$text>
//
// Expected after insertion:
// ↰
// <$text linkHref="foo">lINSERTED[]ink</$text>
//
if ( !nodeBefore ) {
return;
}
// Filter out the following case where the selection has the "linkHref" attribute because the
// gravity is overridden and some text with another attribute (e.g. <b>INSERTED</b>) is inserted:
//
// Before insertion:
//
// ↱
// <$text linkHref="foo">[]link</$text>
//
// Expected after insertion:
//
// ↱
// <$text bold="true">INSERTED</$text><$text linkHref="foo">[]link</$text>
//
if ( !nodeBefore.hasAttribute( 'linkHref' ) ) {
return;
}
// Filter out the following case where a link is a inserted in the middle (or before) another link
// (different URLs, so they will not merge). In this (let's say weird) case, we can leave the selection
// attributes as they are because the user will end up writing in one link or another anyway.
//
// Before insertion:
//
// ↰
// <$text linkHref="foo">l[]ink</$text>
//
// Expected after insertion:
//
// ↰
// <$text linkHref="foo">l</$text><$text linkHref="bar">INSERTED[]</$text><$text linkHref="foo">ink</$text>
//
if ( nodeAfter && nodeAfter.hasAttribute( 'linkHref' ) ) {
return;
}
model.change( writer => {
removeLinkAttributesFromSelection( writer, getLinkAttributesAllowedOnText( model.schema ) );
} );
}, { priority: 'low' } );
}
/**
* Starts listening to {@link module:engine/view/document~Document#event:mousedown} and
* {@link module:engine/view/document~Document#event:selectionChange} and puts the selection before/after a link node
* if clicked at the beginning/ending of the link.
*
* The purpose of this action is to allow typing around the link node directly after a click.
*
* See https://github.com/ckeditor/ckeditor5/issues/1016.
*/
private _enableClickingAfterLink(): void {
const editor = this.editor;
const model = editor.model;
editor.editing.view.addObserver( MouseObserver );
let clicked = false;
// Detect the click.
this.listenTo<ViewDocumentMouseDownEvent>( editor.editing.view.document, 'mousedown', () => {
clicked = true;
} );
// When the selection has changed...
this.listenTo<ViewDocumentSelectionChangeEvent>( editor.editing.view.document, 'selectionChange', () => {
if ( !clicked ) {
return;
}
// ...and it was caused by the click...
clicked = false;
const selection = model.document.selection;
// ...and no text is selected...
if ( !selection.isCollapsed ) {
return;
}
// ...and clicked text is the link...
if ( !selection.hasAttribute( 'linkHref' ) ) {
return;
}
const position = selection.getFirstPosition()!;
const linkRange = findAttributeRange( position, 'linkHref', selection.getAttribute( 'linkHref' ), model );
// ...check whether clicked start/end boundary of the link.
// If so, remove the `linkHref` attribute.
if ( position.isTouching( linkRange.start ) || position.isTouching( linkRange.end ) ) {
model.change( writer => {
removeLinkAttributesFromSelection( writer, getLinkAttributesAllowedOnText( model.schema ) );
} );
}
} );
}
/**
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and {@link module:engine/model/model~Model#insertContent}
* and checks whether typing over the link. If so, attributes of removed text are preserved and applied to the inserted text.
*
* The purpose of this action is to allow modifying a text without loosing the `linkHref` attribute (and other).
*
* See https://github.com/ckeditor/ckeditor5/issues/4762.
*/
private _enableTypingOverLink(): void {
const editor = this.editor;
const view = editor.editing.view;
// Selection attributes when started typing over the link.
let selectionAttributes: IterableIterator<[ string, unknown ]> | null = null;
// Whether pressed `Backspace` or `Delete`. If so, attributes should not be preserved.
let deletedContent = false;
// Detect pressing `Backspace` / `Delete`.
this.listenTo<ViewDocumentDeleteEvent>( view.document, 'delete', () => {
deletedContent = true;
}, { priority: 'high' } );
// Listening to `model#deleteContent` allows detecting whether selected content was a link.
// If so, before removing the element, we will copy its attributes.
this.listenTo<ModelDeleteContentEvent>( editor.model, 'deleteContent', () => {
const selection = editor.model.document.selection;
// Copy attributes only if anything is selected.
if ( selection.isCollapsed ) {
return;
}
// When the content was deleted, do not preserve attributes.
if ( deletedContent ) {
deletedContent = false;
return;
}
// Enabled only when typing.
if ( !isTyping( editor ) ) {
return;
}
if ( shouldCopyAttributes( editor.model ) ) {
selectionAttributes = selection.getAttributes();
}
}, { priority: 'high' } );
// Listening to `model#insertContent` allows detecting the content insertion.
// We want to apply attributes that were removed while typing over the link.
this.listenTo( editor.model, 'insertContent', ( evt, [ element ] ) => {
deletedContent = false;
// Enabled only when typing.
if ( !isTyping( editor ) ) {
return;
}
if ( !selectionAttributes ) {
return;
}
editor.model.change( writer => {
for ( const [ attribute, value ] of selectionAttributes! ) {
writer.setAttribute( attribute, value, element );
}
} );
selectionAttributes = null;
}, { priority: 'high' } );
}
/**
* Starts listening to {@link module:engine/model/model~Model#deleteContent} and checks whether
* removing a content right after the "linkHref" attribute.
*
* If so, the selection should not preserve the `linkHref` attribute. However, if
* the {@link module:typing/twostepcaretmovement~TwoStepCaretMovement} plugin is active and
* the selection has the "linkHref" attribute due to overriden gravity (at the end), the `linkHref` attribute should stay untouched.
*
* The purpose of this action is to allow removing the link text and keep the selection outside the link.
*
* See https://github.com/ckeditor/ckeditor5/issues/7521.
*/
private _handleDeleteContentAfterLink(): void {
const editor = this.editor;
const model = editor.model;
const selection = model.document.selection;
const view = editor.editing.view;
// A flag whether attributes `linkHref` attribute should be preserved.
let shouldPreserveAttributes = false;
// A flag whether the `Backspace` key was pressed.
let hasBackspacePressed = false;
// Detect pressing `Backspace`.
this.listenTo<ViewDocumentDeleteEvent>( view.document, 'delete', ( evt, data ) => {
hasBackspacePressed = data.direction === 'backward';
}, { priority: 'high' } );
// Before removing the content, check whether the selection is inside a link or at the end of link but with 2-SCM enabled.
// If so, we want to preserve link attributes.
this.listenTo<ModelDeleteContentEvent>( model, 'deleteContent', () => {
// Reset the state.
shouldPreserveAttributes = false;
const position = selection.getFirstPosition()!;
const linkHref = selection.getAttribute( 'linkHref' );
if ( !linkHref ) {
return;
}
const linkRange = findAttributeRange( position, 'linkHref', linkHref, model );
// Preserve `linkHref` attribute if the selection is in the middle of the link or
// the selection is at the end of the link and 2-SCM is activated.
shouldPreserveAttributes = linkRange.containsPosition( position ) || linkRange.end.isEqual( position );
}, { priority: 'high' } );
// After removing the content, check whether the current selection should preserve the `linkHref` attribute.
this.listenTo<ModelDeleteContentEvent>( model, 'deleteContent', () => {
// If didn't press `Backspace`.
if ( !hasBackspacePressed ) {
return;
}
hasBackspacePressed = false;
// Disable the mechanism if inside a link (`<$text url="foo">F[]oo</$text>` or <$text url="foo">Foo[]</$text>`).
if ( shouldPreserveAttributes ) {
return;
}
// Use `model.enqueueChange()` in order to execute the callback at the end of the changes process.
editor.model.enqueueChange( writer => {
removeLinkAttributesFromSelection( writer, getLinkAttributesAllowedOnText( model.schema ) );
} );
}, { priority: 'low' } );
}
/**
* Enables URL fixing on pasting.
*/
private _enableClipboardIntegration(): void {
const editor = this.editor;
const model = editor.model;
const defaultProtocol = this.editor.config.get( 'link.defaultProtocol' );
if ( !defaultProtocol ) {
return;
}
this.listenTo<ClipboardContentInsertionEvent>( editor.plugins.get( 'ClipboardPipeline' ), 'contentInsertion', ( evt, data ) => {
model.change( writer => {
const range = writer.createRangeIn( data.content );
for ( const item of range.getItems() ) {
if ( item.hasAttribute( 'linkHref' ) ) {
const newLink = addLinkProtocolIfApplicable( item.getAttribute( 'linkHref' ) as string, defaultProtocol );
writer.setAttribute( 'linkHref', newLink, item );
}
}
} );
} );
}
}
/**
* Make the selection free of link-related model attributes.
* All link-related model attributes start with "link". That includes not only "linkHref"
* but also all decorator attributes (they have dynamic names), or even custom plugins.
*/
function removeLinkAttributesFromSelection( writer: Writer, linkAttributes: Array<string> ): void {
writer.removeSelectionAttribute( 'linkHref' );
for ( const attribute of linkAttributes ) {
writer.removeSelectionAttribute( attribute );
}
}
/**
* Checks whether selection's attributes should be copied to the new inserted text.
*/
function shouldCopyAttributes( model: Model ): boolean {
const selection = model.document.selection;
const firstPosition = selection.getFirstPosition()!;
const lastPosition = selection.getLastPosition()!;
const nodeAtFirstPosition = firstPosition.nodeAfter;
// The text link node does not exist...
if ( !nodeAtFirstPosition ) {
return false;
}
// ...or it isn't the text node...
if ( !nodeAtFirstPosition.is( '$text' ) ) {
return false;
}
// ...or isn't the link.
if ( !nodeAtFirstPosition.hasAttribute( 'linkHref' ) ) {
return false;
}
// `textNode` = the position is inside the link element.
// `nodeBefore` = the position is at the end of the link element.
const nodeAtLastPosition = lastPosition.textNode || lastPosition.nodeBefore;
// If both references the same node selection contains a single text node.
if ( nodeAtFirstPosition === nodeAtLastPosition ) {
return true;
}
// If nodes are not equal, maybe the link nodes has defined additional attributes inside.
// First, we need to find the entire link range.
const linkRange = findAttributeRange( firstPosition, 'linkHref', nodeAtFirstPosition.getAttribute( 'linkHref' ), model );
// Then we can check whether selected range is inside the found link range. If so, attributes should be preserved.
return linkRange.containsRange( model.createRange( firstPosition, lastPosition ), true );
}
/**
* Checks whether provided changes were caused by typing.
*/
function isTyping( editor: Editor ): boolean {
const currentBatch = editor.model.change( writer => writer.batch );
return currentBatch.isTyping;
}
/**
* Returns an array containing names of the attributes allowed on `$text` that describes the link item.
*/
function getLinkAttributesAllowedOnText( schema: Schema ): Array<string> {
const textAttributes = schema.getDefinition( '$text' )!.allowAttributes;
return textAttributes.filter( attribute => attribute.startsWith( 'link' ) );
}