@ckeditor/ckeditor5-clipboard 0.0.0-nightly-next-20260111.0 → 0.0.0-nightly-20260112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +30 -22
- package/{dist → src}/augmentation.d.ts +0 -4
- package/src/augmentation.js +5 -0
- package/{dist → src}/clipboard.d.ts +0 -4
- package/src/clipboard.js +66 -0
- package/{dist → src}/clipboardmarkersutils.d.ts +0 -4
- package/src/clipboardmarkersutils.js +500 -0
- package/{dist → src}/clipboardobserver.d.ts +0 -4
- package/src/clipboardobserver.js +75 -0
- package/{dist → src}/clipboardpipeline.d.ts +0 -4
- package/src/clipboardpipeline.js +287 -0
- package/{dist → src}/dragdrop.d.ts +0 -4
- package/src/dragdrop.js +619 -0
- package/{dist → src}/dragdropblocktoolbar.d.ts +0 -4
- package/src/dragdropblocktoolbar.js +124 -0
- package/{dist → src}/dragdroptarget.d.ts +0 -4
- package/src/dragdroptarget.js +390 -0
- package/{dist → src}/index.d.ts +0 -4
- package/src/index.js +23 -0
- package/{dist → src}/lineview.d.ts +0 -4
- package/src/lineview.js +46 -0
- package/{dist → src}/pasteplaintext.d.ts +0 -4
- package/src/pasteplaintext.js +102 -0
- package/{dist → src}/utils/normalizeclipboarddata.d.ts +0 -4
- package/src/utils/normalizeclipboarddata.js +28 -0
- package/{dist → src}/utils/plaintexttohtml.d.ts +0 -4
- package/src/utils/plaintexttohtml.js +39 -0
- package/{dist → src}/utils/viewtoplaintext.d.ts +0 -4
- package/src/utils/viewtoplaintext.js +96 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module clipboard/pasteplaintext
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import { ClipboardObserver } from './clipboardobserver.js';
|
|
10
|
+
import { ClipboardPipeline } from './clipboardpipeline.js';
|
|
11
|
+
/**
|
|
12
|
+
* The plugin detects the user's intention to paste plain text.
|
|
13
|
+
*
|
|
14
|
+
* For example, it detects the <kbd>Ctrl/Cmd</kbd> + <kbd>Shift</kbd> + <kbd>V</kbd> keystroke.
|
|
15
|
+
*/
|
|
16
|
+
export class PastePlainText extends Plugin {
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
static get pluginName() {
|
|
21
|
+
return 'PastePlainText';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
static get isOfficialPlugin() {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get requires() {
|
|
33
|
+
return [ClipboardPipeline];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
init() {
|
|
39
|
+
const editor = this.editor;
|
|
40
|
+
const model = editor.model;
|
|
41
|
+
const view = editor.editing.view;
|
|
42
|
+
const selection = model.document.selection;
|
|
43
|
+
view.addObserver(ClipboardObserver);
|
|
44
|
+
editor.plugins.get(ClipboardPipeline).on('contentInsertion', (evt, data) => {
|
|
45
|
+
if (!isUnformattedInlineContent(data.content, model)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
model.change(writer => {
|
|
49
|
+
// Formatting attributes should be preserved.
|
|
50
|
+
const textAttributes = Array.from(selection.getAttributes())
|
|
51
|
+
.filter(([key]) => model.schema.getAttributeProperties(key).isFormatting);
|
|
52
|
+
if (!selection.isCollapsed) {
|
|
53
|
+
model.deleteContent(selection, { doNotAutoparagraph: true });
|
|
54
|
+
}
|
|
55
|
+
// Also preserve other attributes if they survived the content deletion (because they were not fully selected).
|
|
56
|
+
// For example linkHref is not a formatting attribute but it should be preserved if pasted text was in the middle
|
|
57
|
+
// of a link.
|
|
58
|
+
textAttributes.push(...selection.getAttributes());
|
|
59
|
+
const range = writer.createRangeIn(data.content);
|
|
60
|
+
for (const item of range.getItems()) {
|
|
61
|
+
for (const attribute of textAttributes) {
|
|
62
|
+
if (model.schema.checkAttribute(item, attribute[0])) {
|
|
63
|
+
writer.setAttribute(attribute[0], attribute[1], item);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns true if specified `documentFragment` represents the unformatted inline content.
|
|
73
|
+
*/
|
|
74
|
+
function isUnformattedInlineContent(documentFragment, model) {
|
|
75
|
+
let range = model.createRangeIn(documentFragment);
|
|
76
|
+
// We consider three scenarios here. The document fragment may include:
|
|
77
|
+
//
|
|
78
|
+
// 1. Only text and inline objects. Then it could be unformatted inline content.
|
|
79
|
+
// 2. Exactly one block element on top-level, eg. <p>Foobar</p> or <h2>Title</h2>.
|
|
80
|
+
// In this case, check this element content, it could be treated as unformatted inline content.
|
|
81
|
+
// 3. More block elements or block objects, then it is not unformatted inline content.
|
|
82
|
+
//
|
|
83
|
+
// We will check for scenario 2. specifically, and if it happens, we will unwrap it and follow with the regular algorithm.
|
|
84
|
+
//
|
|
85
|
+
if (documentFragment.childCount == 1) {
|
|
86
|
+
const child = documentFragment.getChild(0);
|
|
87
|
+
if (child.is('element') && model.schema.isBlock(child) && !model.schema.isObject(child) && !model.schema.isLimit(child)) {
|
|
88
|
+
// Scenario 2. as described above.
|
|
89
|
+
range = model.createRangeIn(child);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const child of range.getItems()) {
|
|
93
|
+
if (!model.schema.isInline(child)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
const attributeKeys = Array.from(child.getAttributeKeys());
|
|
97
|
+
if (attributeKeys.find(key => model.schema.getAttributeProperties(key).isFormatting)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
1
|
/**
|
|
6
2
|
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
7
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module clipboard/utils/normalizeclipboarddata
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Removes some popular browser quirks out of the clipboard data (HTML).
|
|
10
|
+
* Removes all HTML comments. These are considered an internal thing and it makes little sense if they leak into the editor data.
|
|
11
|
+
*
|
|
12
|
+
* @param data The HTML data to normalize.
|
|
13
|
+
* @returns Normalized HTML.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export function normalizeClipboardData(data) {
|
|
17
|
+
return data
|
|
18
|
+
.replace(/<span(?: class="Apple-converted-space"|)>(\s+)<\/span>/g, (fullMatch, spaces) => {
|
|
19
|
+
// Handle the most popular and problematic case when even a single space becomes an nbsp;.
|
|
20
|
+
// Decode those to normal spaces. Read more in https://github.com/ckeditor/ckeditor5-clipboard/issues/2.
|
|
21
|
+
if (spaces.length == 1) {
|
|
22
|
+
return ' ';
|
|
23
|
+
}
|
|
24
|
+
return spaces;
|
|
25
|
+
})
|
|
26
|
+
// Remove all HTML comments.
|
|
27
|
+
.replace(/<!--[\s\S]*?-->/g, '');
|
|
28
|
+
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
1
|
/**
|
|
6
2
|
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
7
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module clipboard/utils/plaintexttohtml
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Converts plain text to its HTML-ized version.
|
|
10
|
+
*
|
|
11
|
+
* @param text The plain text to convert.
|
|
12
|
+
* @returns HTML generated from the plain text.
|
|
13
|
+
*/
|
|
14
|
+
export function plainTextToHtml(text) {
|
|
15
|
+
text = text
|
|
16
|
+
// Encode &.
|
|
17
|
+
.replace(/&/g, '&')
|
|
18
|
+
// Encode <>.
|
|
19
|
+
.replace(/</g, '<')
|
|
20
|
+
.replace(/>/g, '>')
|
|
21
|
+
// Creates a paragraph for each double line break.
|
|
22
|
+
.replace(/\r?\n\r?\n/g, '</p><p>')
|
|
23
|
+
// Creates a line break for each single line break.
|
|
24
|
+
.replace(/\r?\n/g, '<br>')
|
|
25
|
+
// Replace tabs with four spaces.
|
|
26
|
+
.replace(/\t/g, ' ')
|
|
27
|
+
// Preserve trailing spaces (only the first and last one – the rest is handled below).
|
|
28
|
+
.replace(/^\s/, ' ')
|
|
29
|
+
.replace(/\s$/, ' ')
|
|
30
|
+
// Preserve other subsequent spaces now.
|
|
31
|
+
.replace(/\s\s/g, ' ');
|
|
32
|
+
if (text.includes('</p><p>') || text.includes('<br>')) {
|
|
33
|
+
// If we created paragraphs above, add the trailing ones.
|
|
34
|
+
text = `<p>${text}</p>`;
|
|
35
|
+
}
|
|
36
|
+
// TODO:
|
|
37
|
+
// * What about '\nfoo' vs ' foo'?
|
|
38
|
+
return text;
|
|
39
|
+
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
1
|
/**
|
|
6
2
|
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
7
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
// Elements which should not have empty-line padding.
|
|
6
|
+
// Most `view.ContainerElement` want to be separate by new-line, but some are creating one structure
|
|
7
|
+
// together (like `<li>`) so it is better to separate them by only one "\n".
|
|
8
|
+
const smallPaddingElements = ['figcaption', 'li'];
|
|
9
|
+
const listElements = ['ol', 'ul'];
|
|
10
|
+
/**
|
|
11
|
+
* Converts {@link module:engine/view/item~ViewItem view item} and all of its children to plain text.
|
|
12
|
+
*
|
|
13
|
+
* @param converter The converter instance.
|
|
14
|
+
* @param viewItem View item to convert.
|
|
15
|
+
* @returns Plain text representation of `viewItem`.
|
|
16
|
+
*/
|
|
17
|
+
export function viewToPlainText(converter, viewItem) {
|
|
18
|
+
if (viewItem.is('$text') || viewItem.is('$textProxy')) {
|
|
19
|
+
return viewItem.data;
|
|
20
|
+
}
|
|
21
|
+
if (viewItem.is('element', 'img') && viewItem.hasAttribute('alt')) {
|
|
22
|
+
return viewItem.getAttribute('alt');
|
|
23
|
+
}
|
|
24
|
+
if (viewItem.is('element', 'br')) {
|
|
25
|
+
return '\n'; // Convert soft breaks to single line break (#8045).
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Item is a document fragment, attribute element or container element. It doesn't
|
|
29
|
+
* have it's own text value, so we need to convert its children elements.
|
|
30
|
+
*/
|
|
31
|
+
let text = '';
|
|
32
|
+
let prev = null;
|
|
33
|
+
for (const child of viewItem.getChildren()) {
|
|
34
|
+
text += newLinePadding(child, prev) + viewToPlainText(converter, child);
|
|
35
|
+
prev = child;
|
|
36
|
+
}
|
|
37
|
+
// If item is a raw element, the only way to get its content is to render it and read the text directly from DOM.
|
|
38
|
+
if (viewItem.is('rawElement')) {
|
|
39
|
+
const doc = document.implementation.createHTMLDocument('');
|
|
40
|
+
const tempElement = doc.createElement('div');
|
|
41
|
+
viewItem.render(tempElement, converter);
|
|
42
|
+
text += domElementToPlainText(tempElement);
|
|
43
|
+
}
|
|
44
|
+
return text;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recursively converts DOM element and all of its children to plain text.
|
|
48
|
+
*/
|
|
49
|
+
function domElementToPlainText(element) {
|
|
50
|
+
let text = '';
|
|
51
|
+
if (element.nodeType === Node.TEXT_NODE) {
|
|
52
|
+
return element.textContent;
|
|
53
|
+
}
|
|
54
|
+
else if (element.tagName === 'BR') {
|
|
55
|
+
return '\n';
|
|
56
|
+
}
|
|
57
|
+
for (const child of element.childNodes) {
|
|
58
|
+
text += domElementToPlainText(child);
|
|
59
|
+
}
|
|
60
|
+
return text;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns new line padding to prefix the given elements with.
|
|
64
|
+
*/
|
|
65
|
+
function newLinePadding(element, previous) {
|
|
66
|
+
if (!previous) {
|
|
67
|
+
// Don't add padding to first elements in a level.
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
if (element.is('element', 'li') && !element.isEmpty && element.getChild(0).is('containerElement')) {
|
|
71
|
+
// Separate document list items with empty lines.
|
|
72
|
+
return '\n\n';
|
|
73
|
+
}
|
|
74
|
+
if (listElements.includes(element.name) && listElements.includes(previous.name)) {
|
|
75
|
+
/**
|
|
76
|
+
* Because `<ul>` and `<ol>` are ViewAttributeElements, two consecutive lists will not have any padding between
|
|
77
|
+
* them (see the `if` statement below). To fix this, we need to make an exception for this case.
|
|
78
|
+
*/
|
|
79
|
+
return '\n\n';
|
|
80
|
+
}
|
|
81
|
+
if (!element.is('containerElement') && !previous.is('containerElement')) {
|
|
82
|
+
// Don't add padding between non-container elements.
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
if (smallPaddingElements.includes(element.name) || smallPaddingElements.includes(previous.name)) {
|
|
86
|
+
// Add small padding between selected container elements.
|
|
87
|
+
return '\n';
|
|
88
|
+
}
|
|
89
|
+
// Do not add padding around the elements that won't be rendered.
|
|
90
|
+
if (element.is('element') && element.getCustomProperty('dataPipeline:transparentRendering') ||
|
|
91
|
+
previous.is('element') && previous.getCustomProperty('dataPipeline:transparentRendering')) {
|
|
92
|
+
return '';
|
|
93
|
+
}
|
|
94
|
+
// Add empty lines between container elements.
|
|
95
|
+
return '\n\n';
|
|
96
|
+
}
|