/
typo3-link.ts
694 lines (603 loc) · 25.7 KB
/
typo3-link.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
import { UI, Core, Engine, Typing, Link, LinkUtils, LinkActionsView, Widget, Utils } from '@typo3/ckeditor5-bundle';
import { default as modalObject, ModalElement } from '@typo3/backend/modal';
import type { AttributeElement, ViewElement, Schema, Writer } from '@ckeditor/ckeditor5-engine';
import type { GeneralHtmlSupport, DataFilter } from '@ckeditor/ckeditor5-html-support';
import type { GHSViewAttributes } from '@ckeditor/ckeditor5-html-support/src/utils';
const linkIcon = '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="m11.077 15 .991-1.416a.75.75 0 1 1 1.229.86l-1.148 1.64a.748.748 0 0 1-.217.206 5.251 5.251 0 0 1-8.503-5.955.741.741 0 0 1 .12-.274l1.147-1.639a.75.75 0 1 1 1.228.86L4.933 10.7l.006.003a3.75 3.75 0 0 0 6.132 4.294l.006.004zm5.494-5.335a.748.748 0 0 1-.12.274l-1.147 1.639a.75.75 0 1 1-1.228-.86l.86-1.23a3.75 3.75 0 0 0-6.144-4.301l-.86 1.229a.75.75 0 0 1-1.229-.86l1.148-1.64a.748.748 0 0 1 .217-.206 5.251 5.251 0 0 1 8.503 5.955zm-4.563-2.532a.75.75 0 0 1 .184 1.045l-3.155 4.505a.75.75 0 1 1-1.229-.86l3.155-4.506a.75.75 0 0 1 1.045-.184z"/></svg>';
export const LINK_ALLOWED_ATTRIBUTES = ['href', 'title', 'class', 'target', 'rel'];
export function addLinkPrefix(attribute: string): string {
const capitalizedAttribute = attribute.charAt(0).toUpperCase() + attribute.slice(1);
return 'link' + capitalizedAttribute;
}
export interface Typo3LinkDict {
attrs?: {
linkTitle?: string;
linkClass?: string;
linkTarget?: string;
linkRel?: string;
};
linkText?: string;
}
export class Typo3TextView extends UI.View {
declare public text: string | undefined;
constructor(locale?: Utils.Locale) {
super(locale);
this.set('text', undefined);
const bind = this.bindTemplate;
this.setTemplate({
tag: 'span',
attributes: {
class: ['ck', 'ck-linktext'],
title: bind.to('text'),
},
children: [{ text: bind.to('text') }]
});
}
}
/**
* Inspired by @ckeditor/ckeditor5-link/src/linkcommand.js
*/
export class Typo3LinkCommand extends Core.Command {
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement() || Utils.first(selection.getSelectedBlocks());
// A check for any integration that allows linking elements (e.g. `LinkImage`).
// Currently the selection reads attributes from text nodes only. See #7429 and #7465.
if (LinkUtils.isLinkableElement(selectedElement, model.schema)) {
this.value = selectedElement.getAttribute('linkHref') as string;
this.isEnabled = model.schema.checkAttribute(selectedElement, 'linkHref');
} else {
this.value = selection.getAttribute('linkHref') as string;
this.isEnabled = model.schema.checkAttributeInSelection(selection, 'linkHref');
}
}
execute(href: string, linkAttr: Typo3LinkDict = {}): void {
const model = this.editor.model;
const selection = model.document.selection;
model.change(writer => {
// If selection is collapsed then update selected link or insert new one at the place of caret.
if (selection.isCollapsed) {
const position = selection.getFirstPosition();
// When selection is inside text with `linkHref` attribute.
if (selection.hasAttribute('linkHref')) {
// Then update `linkHref` value.
const linkRange = Typing.findAttributeRange(position, 'linkHref', selection.getAttribute('linkHref') as string, model);
writer.setAttribute('linkHref', href, linkRange);
for (const [attribute, value] of Object.entries(this.composeLinkAttributes(linkAttr))) {
if (value !== null) {
writer.setAttribute(attribute, value, linkRange);
} else {
writer.removeAttribute(attribute, linkRange);
}
}
// Put the selection at the end of the updated link.
writer.setSelection(writer.createPositionAfter(linkRange.end.nodeBefore));
} else if (href !== '') {
// If not then insert text node with `linkHref` attribute in place of caret.
// However, since selection is collapsed, attribute value will be used as data for text node.
// So, if `href` is empty, do not create text node.
const attributes = Utils.toMap(selection.getAttributes() as any);
attributes.set('linkHref', href);
for (const [attribute, value] of Object.entries(this.composeLinkAttributes(linkAttr))) {
if (value !== null) {
attributes.set(attribute, value);
}
}
const { end: positionAfter } = model.insertContent(writer.createText(href, attributes as any), position);
// Put the selection at the end of the inserted link.
// Using end of range returned from insertContent in case nodes with the same attributes got merged.
writer.setSelection(positionAfter);
}
// Remove the `linkHref` attribute and all link decorators from the selection.
// It stops adding a new content into the link element.
this.removeLinkAttributesFromSelection(writer, this.getLinkAttributesAllowedOnText(model.schema));
} else {
// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges
// omitting nodes where the `linkHref` attribute is disallowed.
const ranges = model.schema.getValidRanges((selection.getRanges() as unknown) as any[], 'linkHref');
// But for the first, check whether the `linkHref` attribute is allowed on selected blocks (e.g. the "image" element).
const allowedRanges = [];
for (const element of selection.getSelectedBlocks()) {
if (model.schema.checkAttribute(element, 'linkHref')) {
allowedRanges.push(writer.createRangeOn(element));
}
}
// Ranges that accept the `linkHref` attribute. Since we will iterate over `allowedRanges`, let's clone it.
const rangesToUpdate = allowedRanges.slice();
// For all selection ranges we want to check whether given range is inside an element that accepts the `linkHref` attribute.
// If so, we don't want to propagate applying the attribute to its children.
for (const range of ranges) {
if (this.isRangeToUpdate(range, allowedRanges)) {
rangesToUpdate.push(range);
}
}
for (const range of rangesToUpdate) {
writer.setAttribute('linkHref', href, range);
for (const [attribute, value] of Object.entries(this.composeLinkAttributes(linkAttr))) {
if (value !== null) {
writer.setAttribute(attribute, value, range);
} else {
writer.removeAttribute(attribute, range);
}
}
}
}
});
}
private getLinkAttributesAllowedOnText(schema: Schema): Array<string> {
const textAttributes = schema.getDefinition('$text').allowAttributes;
return textAttributes.filter(attribute => attribute.startsWith('link') || attribute === 'htmlA');
}
private removeLinkAttributesFromSelection(writer: Writer, linkAttributes: Array<string>): void {
writer.removeSelectionAttribute('linkHref');
for (const attribute of linkAttributes) {
writer.removeSelectionAttribute(attribute);
}
}
private composeLinkAttributes(linkAttr: Typo3LinkDict): Record<string, GHSViewAttributes|string|null> {
const attrs: Record<string, GHSViewAttributes|string> = {};
for (const [attribute, value] of Object.entries(linkAttr.attrs)) {
if (attribute === 'linkClass') {
const htmlSupport: GeneralHtmlSupport = this.editor.plugins.get('GeneralHtmlSupport');
const ghsAttributeName = htmlSupport.getGhsAttributeNameForElement('a');
const selection = this.editor.model.document.selection;
let htmlA: GHSViewAttributes;
if (selection.hasAttribute(ghsAttributeName)) {
htmlA = { ...(selection.getAttribute(ghsAttributeName) as GHSViewAttributes) };
} else {
htmlA = {};
}
const classes = value.replace(/\s+/g, ' ').trim();
if (classes !== '') {
htmlA.classes = classes.split(' ');
} else if ('classes' in htmlA) {
delete htmlA.classes;
}
attrs[ghsAttributeName] = Object.keys(htmlA).length !== 0 ? htmlA : null;
} else {
attrs[attribute] = value !== '' ? value : null;
}
}
return attrs;
}
private isRangeToUpdate(range: Engine.Range, allowedRanges: Engine.Range[]) {
for (const allowedRange of allowedRanges) {
// A range is inside an element that will have the `linkHref` attribute. Do not modify its nodes.
if (allowedRange.containsRange(range)) {
return false;
}
}
return true;
}
}
/**
* Inspired by @ckeditor/ckeditor5-link/src/unlinkcommand.js
*/
export class Typo3UnlinkCommand extends Core.Command {
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement();
if (LinkUtils.isLinkableElement(selectedElement, model.schema)) {
this.value = selectedElement.getAttribute('linkHref');
this.isEnabled = model.schema.checkAttribute(selectedElement, 'linkHref');
} else {
this.value = selection.getAttribute('linkHref');
this.isEnabled = model.schema.checkAttributeInSelection(selection, 'linkHref');
}
}
execute(): void {
const model = this.editor.model;
const selection = model.document.selection;
model.change(writer => {
// Get ranges to unlink.
const rangesToUnlink = selection.isCollapsed
? [Typing.findAttributeRange(
selection.getFirstPosition(),
'linkHref',
selection.getAttribute('linkHref') as string,
model
)]
: model.schema.getValidRanges((selection.getRanges() as unknown) as any[], 'linkHref');
// Remove `linkHref` attribute from specified ranges.
for (const range of rangesToUnlink) {
writer.removeAttribute('linkHref', range);
writer.removeAttribute('linkTarget', range);
writer.removeAttribute('linkTitle', range);
writer.removeAttribute('linkRel', range);
}
});
}
}
export class Typo3LinkEditing extends Core.Plugin {
static readonly pluginName = 'Typo3LinkEditing';
init(): void {
const editor = this.editor;
// @todo: Why is this needed? Remove.
(window as any).editor = editor;
// @todo YAML additionalAttributes is not implemented yet
editor.model.schema.extend('$text', { allowAttributes: ['linkTitle', 'linkTarget', 'linkRel', 'linkDataRteError'] });
const ghsDataFilter: DataFilter = editor.plugins.get('DataFilter');
ghsDataFilter.loadAllowedConfig([{ name: 'a', classes: true }]);
// linkDataRteError <=> data-rte-error
// This is used for marking broken links (e.g. by linkvalidator) when editing in RTE.
// Broken links are styled differently. This will not get persisted to the database.
editor.conversion.for('downcast').attributeToElement({
model: 'linkDataRteError',
view: (value: string|null, { writer }) => {
const linkElement = writer.createAttributeElement('a', { 'data-rte-error': value }, { priority: 5 });
writer.setCustomProperty('linkDataRteError', true, linkElement);
return linkElement;
}
});
editor.conversion.for('upcast').elementToAttribute({
view: { name: 'a', attributes: { 'data-rte-error': true } },
model: { key: 'linkDataRteError', value: (viewElement: ViewElement) => viewElement.getAttribute('data-rte-error') }
});
// linkTitle <=> title
editor.conversion.for('downcast').attributeToElement({
model: 'linkTitle',
view: (value: string|null, { writer }) => {
const linkElement = writer.createAttributeElement('a', { title: value }, { priority: 5 });
writer.setCustomProperty('linkTitle', true, linkElement);
return linkElement;
}
});
editor.conversion.for('upcast').elementToAttribute({
view: { name: 'a', attributes: { title: true } },
model: { key: 'linkTitle', value: (viewElement: ViewElement) => viewElement.getAttribute('title') }
});
// linkTarget <=> target
editor.conversion.for('downcast').attributeToElement({
model: 'linkTarget',
view: (value, { writer }) => {
const linkElement = writer.createAttributeElement('a', { target: value }, { priority: 5 });
writer.setCustomProperty('linkTarget', true, linkElement);
return linkElement;
}
});
editor.conversion.for('upcast').elementToAttribute({
view: { name: 'a', attributes: { target: true } },
model: { key: 'linkTarget', value: (viewElement: ViewElement) => viewElement.getAttribute('target') }
});
// linkRel <=> rel
editor.conversion.for('downcast').attributeToElement({
model: 'linkRel',
view: (value, { writer }) => {
const linkElement = writer.createAttributeElement('a', { rel: value }, { priority: 5 });
writer.setCustomProperty('linkRel', true, linkElement);
return linkElement;
}
});
editor.conversion.for('upcast').elementToAttribute({
view: { name: 'a', attributes: { rel: true } },
model: { key: 'linkRel', value: (viewElement: ViewElement) => viewElement.getAttribute('rel') }
});
// overrides 'link' command, 'unlink' command is taken from CKEditor5's `LinkEditing`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
editor.commands.add('link', new Typo3LinkCommand(editor));
editor.commands.add('unlink', new Typo3UnlinkCommand(editor));
}
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export class Typo3LinkActionsView extends LinkActionsView {
_createPreviewButton() {
const textView = new Typo3TextView(this.locale);
const t = this.t;
textView.bind('text').to(this, 'href', href => {
return href || t('This link has no URL');
});
return textView;
}
}
const VISUAL_SELECTION_MARKER_NAME = 'link-ui';
export class Typo3LinkUI extends Core.Plugin {
static readonly pluginName = 'Typo3LinkUI';
static readonly requires = [UI.ContextualBalloon];
balloon: UI.ContextualBalloon;
actionsView: Typo3LinkActionsView;
init() {
const editor = this.editor;
editor.editing.view.addObserver(Engine.ClickObserver);
this.actionsView = this.createActionsView();
this.balloon = editor.plugins.get(UI.ContextualBalloon);
this.createToolbarLinkButtons();
this.enableUserBalloonInteractions();
// Renders a fake visual selection marker on an expanded selection.
editor.conversion.for('editingDowncast').markerToHighlight({
model: VISUAL_SELECTION_MARKER_NAME,
view: {
classes: ['ck-fake-link-selection']
}
});
// Renders a fake visual selection marker on a collapsed selection.
editor.conversion.for('editingDowncast').markerToElement({
model: VISUAL_SELECTION_MARKER_NAME,
view: {
name: 'span',
classes: ['ck-fake-link-selection', 'ck-fake-link-selection_collapsed']
}
});
}
private createActionsView(): Typo3LinkActionsView {
const editor = this.editor;
const actionsView = new Typo3LinkActionsView(editor.locale);
const linkCommand = editor.commands.get('link');
const unlinkCommand = editor.commands.get('unlink');
actionsView.bind('href').to(linkCommand, 'value');
actionsView.editButtonView.bind('isEnabled').to(linkCommand);
actionsView.unlinkButtonView.bind('isEnabled').to(unlinkCommand);
// Open LinkBrowser after clicking on the "Edit" button.
this.listenTo(actionsView, 'edit', () => {
this.openLinkBrowser(editor);
});
// Execute unlink command after clicking on the "Unlink" button.
this.listenTo(actionsView, 'unlink', () => {
editor.execute('unlink');
this.hideUI();
});
// Close the panel on esc key press when the **actions have focus**.
actionsView.keystrokes.set('Esc', (data, cancel) => {
this.hideUI();
cancel();
});
return actionsView;
}
private createToolbarLinkButtons() {
const editor = this.editor;
const linkCommand = editor.commands.get('link');
const t = editor.t;
// Handle the `Ctrl+K` keystroke and show the panel.
editor.keystrokes.set(LinkUtils.LINK_KEYSTROKE, (keyEvtData, cancel) => {
// Prevent focusing the search bar in FF, Chrome and Edge. See https://github.com/ckeditor/ckeditor5/issues/4811.
cancel();
if (linkCommand.isEnabled) {
this.showUI();
}
});
// re-uses 'Link' plugin name -> original plugin 'Link' needs to be removed during runtime
editor.ui.componentFactory.add('link', locale => {
const linkButton = new UI.ButtonView(locale);
linkButton.isEnabled = true;
linkButton.label = t('Link');
linkButton.icon = linkIcon;
linkButton.keystroke = LinkUtils.LINK_KEYSTROKE;
linkButton.tooltip = true;
linkButton.isToggleable = true;
linkButton.bind('isEnabled').to(linkCommand, 'isEnabled');
linkButton.bind('isOn').to(linkCommand, 'value', value => !!value);
this.listenTo(linkButton, 'execute', () => this.showUI());
return linkButton;
});
}
private enableUserBalloonInteractions() {
const viewDocument = this.editor.editing.view.document;
this.listenTo(viewDocument, 'click', () => {
const parentLink = this.getSelectedLinkElement();
if (parentLink) {
this.showUI();
}
});
this.editor.keystrokes.set('Esc', (data, cancel) => {
if (this.isUIVisible()) {
this.hideUI();
cancel();
}
});
}
private addActionsView(): void {
if (this.areActionsInPanel()) {
return;
}
this.balloon.add({
view: this.actionsView,
position: this.getBalloonPositionData()
});
}
private hideUI(): void {
if (!this.isUIInPanel()) {
return;
}
const editor = this.editor;
this.stopListening(editor.ui, 'update');
this.stopListening(this.balloon, 'change:visibleView');
editor.editing.view.focus();
this.balloon.remove(this.actionsView);
this.hideFakeVisualSelection();
}
private showUI(): void {
if (!this.getSelectedLinkElement()) {
this.showFakeVisualSelection();
this.openLinkBrowser(this.editor);
} else {
this.addActionsView();
this.balloon.showStack('main');
}
this.startUpdatingUI();
}
private startUpdatingUI(): void {
const editor = this.editor;
const viewDocument = editor.editing.view.document;
let prevSelectedLink = this.getSelectedLinkElement();
let prevSelectionParent = getSelectionParent();
const update = () => {
const selectedLink = this.getSelectedLinkElement();
const selectionParent = getSelectionParent();
if ((prevSelectedLink && !selectedLink) ||
(!prevSelectedLink && selectionParent !== prevSelectionParent)) {
this.hideUI();
}
else if (this.isUIVisible()) {
this.balloon.updatePosition(this.getBalloonPositionData());
}
prevSelectedLink = selectedLink;
prevSelectionParent = selectionParent;
};
function getSelectionParent() {
return viewDocument.selection.focus.getAncestors()
.reverse()
.find(node => node.is('element'));
}
this.listenTo(editor.ui, 'update', update);
this.listenTo(this.balloon, 'change:visibleView', update);
}
private areActionsInPanel(): boolean {
return this.balloon.hasView(this.actionsView);
}
private areActionsVisible(): boolean {
return this.balloon.visibleView === this.actionsView;
}
private isUIInPanel(): boolean {
return this.areActionsInPanel();
}
private isUIVisible(): boolean {
return this.areActionsVisible();
}
private getBalloonPositionData(): any {
const view = this.editor.editing.view;
const model = this.editor.model;
const viewDocument = view.document;
let target = null;
if (model.markers.has(VISUAL_SELECTION_MARKER_NAME)) {
// There are cases when we highlight selection using a marker (#7705, #4721).
const markerViewElements = Array.from(this.editor.editing.mapper.markerNameToElements(VISUAL_SELECTION_MARKER_NAME));
const newRange = view.createRange(
view.createPositionBefore(markerViewElements[0]),
view.createPositionAfter(markerViewElements[markerViewElements.length - 1])
);
target = view.domConverter.viewRangeToDom(newRange);
} else {
// Make sure the target is calculated on demand at the last moment because a cached DOM range
// (which is very fragile) can desynchronize with the state of the editing view if there was
// any rendering done in the meantime. This can happen, for instance, when an inline widget
// gets unlinked.
target = () => {
const targetLink = this.getSelectedLinkElement();
return targetLink ?
// When selection is inside link element, then attach panel to this element.
view.domConverter.mapViewToDom(targetLink) :
// Otherwise attach panel to the selection.
view.domConverter.viewRangeToDom(viewDocument.selection.getFirstRange());
};
}
return { target };
}
private getSelectedLinkElement(): AttributeElement | null {
const view = this.editor.editing.view;
const selection = view.document.selection;
const selectedElement = selection.getSelectedElement();
// The selection is collapsed or some widget is selected (especially inline widget).
if (selection.isCollapsed || selectedElement && Widget.isWidget(selectedElement)) {
return this.findLinkElementAncestor(selection.getFirstPosition());
} else {
// The range for fully selected link is usually anchored in adjacent text nodes.
// Trim it to get closer to the actual link element.
const range = selection.getFirstRange().getTrimmed();
const startLink = this.findLinkElementAncestor(range.start);
const endLink = this.findLinkElementAncestor(range.end);
if (!startLink || startLink != endLink) {
return null;
}
// Check if the link element is fully selected.
if (view.createRangeIn(startLink).getTrimmed().isEqual(range)) {
return startLink;
} else {
return null;
}
}
}
private showFakeVisualSelection(): void {
const model = this.editor.model;
model.change(writer => {
const range = model.document.selection.getFirstRange();
if (model.markers.has(VISUAL_SELECTION_MARKER_NAME)) {
writer.updateMarker(VISUAL_SELECTION_MARKER_NAME, { range });
} else {
if (range.start.isAtEnd) {
const startPosition = range.start.getLastMatchingPosition(
({ item }) => !model.schema.isContent(item),
{
startPosition: null,
boundaries: range
}
);
writer.addMarker(VISUAL_SELECTION_MARKER_NAME, {
usingOperation: false,
affectsData: false,
range: writer.createRange(startPosition, range.end)
});
} else {
writer.addMarker(VISUAL_SELECTION_MARKER_NAME, {
usingOperation: false,
affectsData: false,
range
});
}
}
});
}
private hideFakeVisualSelection() {
const model = this.editor.model;
if (model.markers.has(VISUAL_SELECTION_MARKER_NAME)) {
model.change(writer => {
writer.removeMarker(VISUAL_SELECTION_MARKER_NAME);
});
}
}
private findLinkElementAncestor(position: any) {
return position.getAncestors().find((ancestor: any) => LinkUtils.isLinkElement(ancestor));
}
private openLinkBrowser(editor: Core.Editor): void {
const element = this.getSelectedLinkElement();
let additionalParameters = '';
if (element) {
additionalParameters += '&P[curUrl][url]=' + encodeURIComponent(element.getAttribute('href'));
['target', 'class', 'title', 'rel'].forEach((attrName) => {
const attrValue = element.getAttribute(attrName);
if (attrValue) {
additionalParameters += '&P[curUrl][' + attrName + ']=' + encodeURIComponent(attrValue);
}
});
}
this.openElementBrowser(
editor,
'Link',
this.makeUrlFromModulePath(
editor,
(editor.config.get('typo3link') as any)?.routeUrl,
additionalParameters
));
}
private makeUrlFromModulePath(editor: Core.Editor, routeUrl: string, parameters: string) {
return routeUrl
+ (routeUrl.indexOf('?') === -1 ? '?' : '&')
+ '&contentsLanguage=' + 'en'// editor.config.contentsLanguage
+ '&editorId=' + '123' // editor.id
+ (parameters ? parameters : '');
}
private openElementBrowser(editor: Core.Editor, title: string, url: string) {
modalObject.advanced({
type: modalObject.types.iframe,
title: title,
content: url,
size: modalObject.sizes.large,
callback: (currentModal: ModalElement) => {
// Add the instance to the iframe itself
currentModal.userData.editor = editor;
currentModal.userData.selectionStartPosition = editor.model.document.selection.getFirstPosition();
currentModal.userData.selectionEndPosition = editor.model.document.selection.getLastPosition();
// @todo: is this used at all?
// should maybe be a regular modal attribute then
currentModal.querySelector('.t3js-modal-body')?.setAttribute('id', '123' /*editor.id*/);
}
});
}
}
export default class Typo3Link extends Core.Plugin {
static readonly pluginName = 'Typo3Link';
static readonly requires = ['GeneralHtmlSupport', Link.LinkEditing, Link.AutoLink, Typo3LinkEditing, Typo3LinkUI];
static readonly overrides?: Array<typeof Core.Plugin> = [Link.Link];
}