-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
pasteplaintext.ts
103 lines (83 loc) · 2.98 KB
/
pasteplaintext.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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module clipboard/pasteplaintext
*/
import { Plugin } from '@ckeditor/ckeditor5-core';
import type { DocumentFragment, Schema, ViewDocumentKeyDownEvent } from '@ckeditor/ckeditor5-engine';
import ClipboardObserver from './clipboardobserver.js';
import ClipboardPipeline, { type ClipboardContentInsertionEvent } from './clipboardpipeline.js';
/**
* The plugin detects the user's intention to paste plain text.
*
* For example, it detects the <kbd>Ctrl/Cmd</kbd> + <kbd>Shift</kbd> + <kbd>V</kbd> keystroke.
*/
export default class PastePlainText extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'PastePlainText' as const;
}
/**
* @inheritDoc
*/
public static get requires() {
return [ ClipboardPipeline ] as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const model = editor.model;
const view = editor.editing.view;
const viewDocument = view.document;
const selection = model.document.selection;
let shiftPressed = false;
view.addObserver( ClipboardObserver );
this.listenTo<ViewDocumentKeyDownEvent>( viewDocument, 'keydown', ( evt, data ) => {
shiftPressed = data.shiftKey;
} );
editor.plugins.get( ClipboardPipeline ).on<ClipboardContentInsertionEvent>( 'contentInsertion', ( evt, data ) => {
// Plain text can be determined based on the event flag (#7799) or auto-detection (#1006). If detected,
// preserve selection attributes on pasted items.
if ( !shiftPressed && !isPlainTextFragment( data.content, model.schema ) ) {
return;
}
model.change( writer => {
// Formatting attributes should be preserved.
const textAttributes = Array.from( selection.getAttributes() )
.filter( ( [ key ] ) => model.schema.getAttributeProperties( key ).isFormatting );
if ( !selection.isCollapsed ) {
model.deleteContent( selection, { doNotAutoparagraph: true } );
}
// Also preserve other attributes if they survived the content deletion (because they were not fully selected).
// For example linkHref is not a formatting attribute but it should be preserved if pasted text was in the middle
// of a link.
textAttributes.push( ...selection.getAttributes() );
const range = writer.createRangeIn( data.content );
for ( const item of range.getItems() ) {
if ( item.is( '$textProxy' ) ) {
writer.setAttributes( textAttributes, item );
}
}
} );
} );
}
}
/**
* Returns true if specified `documentFragment` represents a plain text.
*/
function isPlainTextFragment( documentFragment: DocumentFragment, schema: Schema ): boolean {
if ( documentFragment.childCount > 1 ) {
return false;
}
const child = documentFragment.getChild( 0 )!;
if ( schema.isObject( child ) ) {
return false;
}
return Array.from( child.getAttributeKeys() ).length == 0;
}