@ckeditor/ckeditor5-media-embed 38.1.1 → 38.2.0-alpha.1
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 +4 -3
- package/src/augmentation.d.ts +25 -25
- package/src/augmentation.js +5 -5
- package/src/automediaembed.d.ts +51 -51
- package/src/automediaembed.js +131 -131
- package/src/converters.d.ts +38 -38
- package/src/converters.js +53 -53
- package/src/index.d.ts +15 -15
- package/src/index.js +13 -13
- package/src/mediaembed.d.ts +34 -34
- package/src/mediaembed.js +38 -38
- package/src/mediaembedcommand.d.ts +36 -36
- package/src/mediaembedcommand.js +71 -71
- package/src/mediaembedconfig.d.ts +278 -278
- package/src/mediaembedconfig.js +5 -5
- package/src/mediaembedediting.d.ts +31 -31
- package/src/mediaembedediting.js +231 -231
- package/src/mediaembedtoolbar.d.ts +30 -30
- package/src/mediaembedtoolbar.js +44 -44
- package/src/mediaembedui.d.ts +27 -27
- package/src/mediaembedui.js +99 -99
- package/src/mediaregistry.d.ts +66 -66
- package/src/mediaregistry.js +232 -232
- package/src/ui/mediaformview.d.ts +115 -115
- package/src/ui/mediaformview.js +194 -194
- package/src/utils.d.ts +67 -67
- package/src/utils.js +85 -85
- package/theme/icons/media/twitter.svg +0 -0
- package/build/media-embed.js.map +0 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ckeditor/ckeditor5-media-embed",
|
3
|
-
"version": "38.
|
3
|
+
"version": "38.2.0-alpha.1",
|
4
4
|
"description": "Media embed feature for CKEditor 5.",
|
5
5
|
"keywords": [
|
6
6
|
"ckeditor",
|
@@ -11,9 +11,10 @@
|
|
11
11
|
"ckeditor5-dll"
|
12
12
|
],
|
13
13
|
"main": "src/index.js",
|
14
|
+
"type": "module",
|
14
15
|
"dependencies": {
|
15
|
-
"@ckeditor/ckeditor5-ui": "38.
|
16
|
-
"ckeditor5": "38.
|
16
|
+
"@ckeditor/ckeditor5-ui": "38.2.0-alpha.1",
|
17
|
+
"ckeditor5": "38.2.0-alpha.1"
|
17
18
|
},
|
18
19
|
"engines": {
|
19
20
|
"node": ">=16.0.0",
|
package/src/augmentation.d.ts
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
import type { MediaEmbedConfig, AutoMediaEmbed, MediaEmbed, MediaEmbedEditing, MediaEmbedToolbar, MediaEmbedUI, MediaEmbedCommand } from './index';
|
6
|
-
declare module '@ckeditor/ckeditor5-core' {
|
7
|
-
interface EditorConfig {
|
8
|
-
/**
|
9
|
-
* The configuration of the {@link module:media-embed/mediaembed~MediaEmbed} feature.
|
10
|
-
*
|
11
|
-
* Read more in {@link module:media-embed/mediaembedconfig~MediaEmbedConfig}.
|
12
|
-
*/
|
13
|
-
mediaEmbed?: MediaEmbedConfig;
|
14
|
-
}
|
15
|
-
interface PluginsMap {
|
16
|
-
[AutoMediaEmbed.pluginName]: AutoMediaEmbed;
|
17
|
-
[MediaEmbed.pluginName]: MediaEmbed;
|
18
|
-
[MediaEmbedEditing.pluginName]: MediaEmbedEditing;
|
19
|
-
[MediaEmbedToolbar.pluginName]: MediaEmbedToolbar;
|
20
|
-
[MediaEmbedUI.pluginName]: MediaEmbedUI;
|
21
|
-
}
|
22
|
-
interface CommandsMap {
|
23
|
-
mediaEmbed: MediaEmbedCommand;
|
24
|
-
}
|
25
|
-
}
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
import type { MediaEmbedConfig, AutoMediaEmbed, MediaEmbed, MediaEmbedEditing, MediaEmbedToolbar, MediaEmbedUI, MediaEmbedCommand } from './index.js';
|
6
|
+
declare module '@ckeditor/ckeditor5-core' {
|
7
|
+
interface EditorConfig {
|
8
|
+
/**
|
9
|
+
* The configuration of the {@link module:media-embed/mediaembed~MediaEmbed} feature.
|
10
|
+
*
|
11
|
+
* Read more in {@link module:media-embed/mediaembedconfig~MediaEmbedConfig}.
|
12
|
+
*/
|
13
|
+
mediaEmbed?: MediaEmbedConfig;
|
14
|
+
}
|
15
|
+
interface PluginsMap {
|
16
|
+
[AutoMediaEmbed.pluginName]: AutoMediaEmbed;
|
17
|
+
[MediaEmbed.pluginName]: MediaEmbed;
|
18
|
+
[MediaEmbedEditing.pluginName]: MediaEmbedEditing;
|
19
|
+
[MediaEmbedToolbar.pluginName]: MediaEmbedToolbar;
|
20
|
+
[MediaEmbedUI.pluginName]: MediaEmbedUI;
|
21
|
+
}
|
22
|
+
interface CommandsMap {
|
23
|
+
mediaEmbed: MediaEmbedCommand;
|
24
|
+
}
|
25
|
+
}
|
package/src/augmentation.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
export {};
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
export {};
|
package/src/automediaembed.d.ts
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
/**
|
6
|
-
* @module media-embed/automediaembed
|
7
|
-
*/
|
8
|
-
import { type Editor, Plugin } from 'ckeditor5/src/core';
|
9
|
-
import { Clipboard } from 'ckeditor5/src/clipboard';
|
10
|
-
import { Delete } from 'ckeditor5/src/typing';
|
11
|
-
import { Undo } from 'ckeditor5/src/undo';
|
12
|
-
/**
|
13
|
-
* The auto-media embed plugin. It recognizes media links in the pasted content and embeds
|
14
|
-
* them shortly after they are injected into the document.
|
15
|
-
*/
|
16
|
-
export default class AutoMediaEmbed extends Plugin {
|
17
|
-
/**
|
18
|
-
* @inheritDoc
|
19
|
-
*/
|
20
|
-
static get requires(): readonly [typeof Clipboard, typeof Delete, typeof Undo];
|
21
|
-
/**
|
22
|
-
* @inheritDoc
|
23
|
-
*/
|
24
|
-
static get pluginName(): "AutoMediaEmbed";
|
25
|
-
/**
|
26
|
-
* The paste–to–embed `setTimeout` ID. Stored as a property to allow
|
27
|
-
* cleaning of the timeout.
|
28
|
-
*/
|
29
|
-
private _timeoutId;
|
30
|
-
/**
|
31
|
-
* The position where the `<media>` element will be inserted after the timeout,
|
32
|
-
* determined each time the new content is pasted into the document.
|
33
|
-
*/
|
34
|
-
private _positionToInsert;
|
35
|
-
/**
|
36
|
-
* @inheritDoc
|
37
|
-
*/
|
38
|
-
constructor(editor: Editor);
|
39
|
-
/**
|
40
|
-
* @inheritDoc
|
41
|
-
*/
|
42
|
-
init(): void;
|
43
|
-
/**
|
44
|
-
* Analyzes the part of the document between provided positions in search for a URL representing media.
|
45
|
-
* When the URL is found, it is automatically converted into media.
|
46
|
-
*
|
47
|
-
* @param leftPosition Left position of the selection.
|
48
|
-
* @param rightPosition Right position of the selection.
|
49
|
-
*/
|
50
|
-
private _embedMediaBetweenPositions;
|
51
|
-
}
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
/**
|
6
|
+
* @module media-embed/automediaembed
|
7
|
+
*/
|
8
|
+
import { type Editor, Plugin } from 'ckeditor5/src/core.js';
|
9
|
+
import { Clipboard } from 'ckeditor5/src/clipboard.js';
|
10
|
+
import { Delete } from 'ckeditor5/src/typing.js';
|
11
|
+
import { Undo } from 'ckeditor5/src/undo.js';
|
12
|
+
/**
|
13
|
+
* The auto-media embed plugin. It recognizes media links in the pasted content and embeds
|
14
|
+
* them shortly after they are injected into the document.
|
15
|
+
*/
|
16
|
+
export default class AutoMediaEmbed extends Plugin {
|
17
|
+
/**
|
18
|
+
* @inheritDoc
|
19
|
+
*/
|
20
|
+
static get requires(): readonly [typeof Clipboard, typeof Delete, typeof Undo];
|
21
|
+
/**
|
22
|
+
* @inheritDoc
|
23
|
+
*/
|
24
|
+
static get pluginName(): "AutoMediaEmbed";
|
25
|
+
/**
|
26
|
+
* The paste–to–embed `setTimeout` ID. Stored as a property to allow
|
27
|
+
* cleaning of the timeout.
|
28
|
+
*/
|
29
|
+
private _timeoutId;
|
30
|
+
/**
|
31
|
+
* The position where the `<media>` element will be inserted after the timeout,
|
32
|
+
* determined each time the new content is pasted into the document.
|
33
|
+
*/
|
34
|
+
private _positionToInsert;
|
35
|
+
/**
|
36
|
+
* @inheritDoc
|
37
|
+
*/
|
38
|
+
constructor(editor: Editor);
|
39
|
+
/**
|
40
|
+
* @inheritDoc
|
41
|
+
*/
|
42
|
+
init(): void;
|
43
|
+
/**
|
44
|
+
* Analyzes the part of the document between provided positions in search for a URL representing media.
|
45
|
+
* When the URL is found, it is automatically converted into media.
|
46
|
+
*
|
47
|
+
* @param leftPosition Left position of the selection.
|
48
|
+
* @param rightPosition Right position of the selection.
|
49
|
+
*/
|
50
|
+
private _embedMediaBetweenPositions;
|
51
|
+
}
|
package/src/automediaembed.js
CHANGED
@@ -1,131 +1,131 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
/**
|
6
|
-
* @module media-embed/automediaembed
|
7
|
-
*/
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
9
|
-
import { LiveRange, LivePosition } from 'ckeditor5/src/engine';
|
10
|
-
import { Clipboard } from 'ckeditor5/src/clipboard';
|
11
|
-
import { Delete } from 'ckeditor5/src/typing';
|
12
|
-
import { Undo } from 'ckeditor5/src/undo';
|
13
|
-
import { global } from 'ckeditor5/src/utils';
|
14
|
-
import MediaEmbedEditing from './mediaembedediting';
|
15
|
-
import { insertMedia } from './utils';
|
16
|
-
const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w-]+\.[\w-.~:/?#[\]@!$&'()*+,;=%]+$/;
|
17
|
-
/**
|
18
|
-
* The auto-media embed plugin. It recognizes media links in the pasted content and embeds
|
19
|
-
* them shortly after they are injected into the document.
|
20
|
-
*/
|
21
|
-
export default class AutoMediaEmbed extends Plugin {
|
22
|
-
/**
|
23
|
-
* @inheritDoc
|
24
|
-
*/
|
25
|
-
static get requires() {
|
26
|
-
return [Clipboard, Delete, Undo];
|
27
|
-
}
|
28
|
-
/**
|
29
|
-
* @inheritDoc
|
30
|
-
*/
|
31
|
-
static get pluginName() {
|
32
|
-
return 'AutoMediaEmbed';
|
33
|
-
}
|
34
|
-
/**
|
35
|
-
* @inheritDoc
|
36
|
-
*/
|
37
|
-
constructor(editor) {
|
38
|
-
super(editor);
|
39
|
-
this._timeoutId = null;
|
40
|
-
this._positionToInsert = null;
|
41
|
-
}
|
42
|
-
/**
|
43
|
-
* @inheritDoc
|
44
|
-
*/
|
45
|
-
init() {
|
46
|
-
const editor = this.editor;
|
47
|
-
const modelDocument = editor.model.document;
|
48
|
-
// We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
|
49
|
-
// After pasting, the content between those positions will be checked for a URL that could be transformed
|
50
|
-
// into media.
|
51
|
-
const clipboardPipeline = editor.plugins.get('ClipboardPipeline');
|
52
|
-
this.listenTo(clipboardPipeline, 'inputTransformation', () => {
|
53
|
-
const firstRange = modelDocument.selection.getFirstRange();
|
54
|
-
const leftLivePosition = LivePosition.fromPosition(firstRange.start);
|
55
|
-
leftLivePosition.stickiness = 'toPrevious';
|
56
|
-
const rightLivePosition = LivePosition.fromPosition(firstRange.end);
|
57
|
-
rightLivePosition.stickiness = 'toNext';
|
58
|
-
modelDocument.once('change:data', () => {
|
59
|
-
this._embedMediaBetweenPositions(leftLivePosition, rightLivePosition);
|
60
|
-
leftLivePosition.detach();
|
61
|
-
rightLivePosition.detach();
|
62
|
-
}, { priority: 'high' });
|
63
|
-
});
|
64
|
-
const undoCommand = editor.commands.get('undo');
|
65
|
-
undoCommand.on('execute', () => {
|
66
|
-
if (this._timeoutId) {
|
67
|
-
global.window.clearTimeout(this._timeoutId);
|
68
|
-
this._positionToInsert.detach();
|
69
|
-
this._timeoutId = null;
|
70
|
-
this._positionToInsert = null;
|
71
|
-
}
|
72
|
-
}, { priority: 'high' });
|
73
|
-
}
|
74
|
-
/**
|
75
|
-
* Analyzes the part of the document between provided positions in search for a URL representing media.
|
76
|
-
* When the URL is found, it is automatically converted into media.
|
77
|
-
*
|
78
|
-
* @param leftPosition Left position of the selection.
|
79
|
-
* @param rightPosition Right position of the selection.
|
80
|
-
*/
|
81
|
-
_embedMediaBetweenPositions(leftPosition, rightPosition) {
|
82
|
-
const editor = this.editor;
|
83
|
-
const mediaRegistry = editor.plugins.get(MediaEmbedEditing).registry;
|
84
|
-
// TODO: Use marker instead of LiveRange & LivePositions.
|
85
|
-
const urlRange = new LiveRange(leftPosition, rightPosition);
|
86
|
-
const walker = urlRange.getWalker({ ignoreElementEnd: true });
|
87
|
-
let url = '';
|
88
|
-
for (const node of walker) {
|
89
|
-
if (node.item.is('$textProxy')) {
|
90
|
-
url += node.item.data;
|
91
|
-
}
|
92
|
-
}
|
93
|
-
url = url.trim();
|
94
|
-
// If the URL does not match to universal URL regexp, let's skip that.
|
95
|
-
if (!url.match(URL_REGEXP)) {
|
96
|
-
urlRange.detach();
|
97
|
-
return;
|
98
|
-
}
|
99
|
-
// If the URL represents a media, let's use it.
|
100
|
-
if (!mediaRegistry.hasMedia(url)) {
|
101
|
-
urlRange.detach();
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
const mediaEmbedCommand = editor.commands.get('mediaEmbed');
|
105
|
-
// Do not anything if media element cannot be inserted at the current position (#47).
|
106
|
-
if (!mediaEmbedCommand.isEnabled) {
|
107
|
-
urlRange.detach();
|
108
|
-
return;
|
109
|
-
}
|
110
|
-
// Position won't be available in the `setTimeout` function so let's clone it.
|
111
|
-
this._positionToInsert = LivePosition.fromPosition(leftPosition);
|
112
|
-
// This action mustn't be executed if undo was called between pasting and auto-embedding.
|
113
|
-
this._timeoutId = global.window.setTimeout(() => {
|
114
|
-
editor.model.change(writer => {
|
115
|
-
this._timeoutId = null;
|
116
|
-
writer.remove(urlRange);
|
117
|
-
urlRange.detach();
|
118
|
-
let insertionPosition = null;
|
119
|
-
// Check if position where the media element should be inserted is still valid.
|
120
|
-
// Otherwise leave it as undefined to use document.selection - default behavior of model.insertContent().
|
121
|
-
if (this._positionToInsert.root.rootName !== '$graveyard') {
|
122
|
-
insertionPosition = this._positionToInsert;
|
123
|
-
}
|
124
|
-
insertMedia(editor.model, url, insertionPosition, false);
|
125
|
-
this._positionToInsert.detach();
|
126
|
-
this._positionToInsert = null;
|
127
|
-
});
|
128
|
-
editor.plugins.get(Delete).requestUndoOnBackspace();
|
129
|
-
}, 100);
|
130
|
-
}
|
131
|
-
}
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
/**
|
6
|
+
* @module media-embed/automediaembed
|
7
|
+
*/
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
9
|
+
import { LiveRange, LivePosition } from 'ckeditor5/src/engine.js';
|
10
|
+
import { Clipboard } from 'ckeditor5/src/clipboard.js';
|
11
|
+
import { Delete } from 'ckeditor5/src/typing.js';
|
12
|
+
import { Undo } from 'ckeditor5/src/undo.js';
|
13
|
+
import { global } from 'ckeditor5/src/utils.js';
|
14
|
+
import MediaEmbedEditing from './mediaembedediting.js';
|
15
|
+
import { insertMedia } from './utils.js';
|
16
|
+
const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w-]+\.[\w-.~:/?#[\]@!$&'()*+,;=%]+$/;
|
17
|
+
/**
|
18
|
+
* The auto-media embed plugin. It recognizes media links in the pasted content and embeds
|
19
|
+
* them shortly after they are injected into the document.
|
20
|
+
*/
|
21
|
+
export default class AutoMediaEmbed extends Plugin {
|
22
|
+
/**
|
23
|
+
* @inheritDoc
|
24
|
+
*/
|
25
|
+
static get requires() {
|
26
|
+
return [Clipboard, Delete, Undo];
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* @inheritDoc
|
30
|
+
*/
|
31
|
+
static get pluginName() {
|
32
|
+
return 'AutoMediaEmbed';
|
33
|
+
}
|
34
|
+
/**
|
35
|
+
* @inheritDoc
|
36
|
+
*/
|
37
|
+
constructor(editor) {
|
38
|
+
super(editor);
|
39
|
+
this._timeoutId = null;
|
40
|
+
this._positionToInsert = null;
|
41
|
+
}
|
42
|
+
/**
|
43
|
+
* @inheritDoc
|
44
|
+
*/
|
45
|
+
init() {
|
46
|
+
const editor = this.editor;
|
47
|
+
const modelDocument = editor.model.document;
|
48
|
+
// We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
|
49
|
+
// After pasting, the content between those positions will be checked for a URL that could be transformed
|
50
|
+
// into media.
|
51
|
+
const clipboardPipeline = editor.plugins.get('ClipboardPipeline');
|
52
|
+
this.listenTo(clipboardPipeline, 'inputTransformation', () => {
|
53
|
+
const firstRange = modelDocument.selection.getFirstRange();
|
54
|
+
const leftLivePosition = LivePosition.fromPosition(firstRange.start);
|
55
|
+
leftLivePosition.stickiness = 'toPrevious';
|
56
|
+
const rightLivePosition = LivePosition.fromPosition(firstRange.end);
|
57
|
+
rightLivePosition.stickiness = 'toNext';
|
58
|
+
modelDocument.once('change:data', () => {
|
59
|
+
this._embedMediaBetweenPositions(leftLivePosition, rightLivePosition);
|
60
|
+
leftLivePosition.detach();
|
61
|
+
rightLivePosition.detach();
|
62
|
+
}, { priority: 'high' });
|
63
|
+
});
|
64
|
+
const undoCommand = editor.commands.get('undo');
|
65
|
+
undoCommand.on('execute', () => {
|
66
|
+
if (this._timeoutId) {
|
67
|
+
global.window.clearTimeout(this._timeoutId);
|
68
|
+
this._positionToInsert.detach();
|
69
|
+
this._timeoutId = null;
|
70
|
+
this._positionToInsert = null;
|
71
|
+
}
|
72
|
+
}, { priority: 'high' });
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* Analyzes the part of the document between provided positions in search for a URL representing media.
|
76
|
+
* When the URL is found, it is automatically converted into media.
|
77
|
+
*
|
78
|
+
* @param leftPosition Left position of the selection.
|
79
|
+
* @param rightPosition Right position of the selection.
|
80
|
+
*/
|
81
|
+
_embedMediaBetweenPositions(leftPosition, rightPosition) {
|
82
|
+
const editor = this.editor;
|
83
|
+
const mediaRegistry = editor.plugins.get(MediaEmbedEditing).registry;
|
84
|
+
// TODO: Use marker instead of LiveRange & LivePositions.
|
85
|
+
const urlRange = new LiveRange(leftPosition, rightPosition);
|
86
|
+
const walker = urlRange.getWalker({ ignoreElementEnd: true });
|
87
|
+
let url = '';
|
88
|
+
for (const node of walker) {
|
89
|
+
if (node.item.is('$textProxy')) {
|
90
|
+
url += node.item.data;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
url = url.trim();
|
94
|
+
// If the URL does not match to universal URL regexp, let's skip that.
|
95
|
+
if (!url.match(URL_REGEXP)) {
|
96
|
+
urlRange.detach();
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
// If the URL represents a media, let's use it.
|
100
|
+
if (!mediaRegistry.hasMedia(url)) {
|
101
|
+
urlRange.detach();
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
const mediaEmbedCommand = editor.commands.get('mediaEmbed');
|
105
|
+
// Do not anything if media element cannot be inserted at the current position (#47).
|
106
|
+
if (!mediaEmbedCommand.isEnabled) {
|
107
|
+
urlRange.detach();
|
108
|
+
return;
|
109
|
+
}
|
110
|
+
// Position won't be available in the `setTimeout` function so let's clone it.
|
111
|
+
this._positionToInsert = LivePosition.fromPosition(leftPosition);
|
112
|
+
// This action mustn't be executed if undo was called between pasting and auto-embedding.
|
113
|
+
this._timeoutId = global.window.setTimeout(() => {
|
114
|
+
editor.model.change(writer => {
|
115
|
+
this._timeoutId = null;
|
116
|
+
writer.remove(urlRange);
|
117
|
+
urlRange.detach();
|
118
|
+
let insertionPosition = null;
|
119
|
+
// Check if position where the media element should be inserted is still valid.
|
120
|
+
// Otherwise leave it as undefined to use document.selection - default behavior of model.insertContent().
|
121
|
+
if (this._positionToInsert.root.rootName !== '$graveyard') {
|
122
|
+
insertionPosition = this._positionToInsert;
|
123
|
+
}
|
124
|
+
insertMedia(editor.model, url, insertionPosition, false);
|
125
|
+
this._positionToInsert.detach();
|
126
|
+
this._positionToInsert = null;
|
127
|
+
});
|
128
|
+
editor.plugins.get(Delete).requestUndoOnBackspace();
|
129
|
+
}, 100);
|
130
|
+
}
|
131
|
+
}
|
package/src/converters.d.ts
CHANGED
@@ -1,38 +1,38 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
import type { DowncastDispatcher } from 'ckeditor5/src/engine';
|
6
|
-
import type MediaRegistry from './mediaregistry';
|
7
|
-
import type { MediaOptions } from './utils';
|
8
|
-
/**
|
9
|
-
* Returns a function that converts the model "url" attribute to the view representation.
|
10
|
-
*
|
11
|
-
* Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
12
|
-
*
|
13
|
-
* ```html
|
14
|
-
* <figure class="media">
|
15
|
-
* <oembed url="foo"></oembed>
|
16
|
-
* </figure>
|
17
|
-
* ```
|
18
|
-
*
|
19
|
-
* or "non-semantic" (for the editing view pipeline):
|
20
|
-
*
|
21
|
-
* ```html
|
22
|
-
* <figure class="media">
|
23
|
-
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
24
|
-
* </figure>
|
25
|
-
* ```
|
26
|
-
*
|
27
|
-
* **Note:** Changing the model "url" attribute replaces the entire content of the
|
28
|
-
* `<figure>` in the view.
|
29
|
-
*
|
30
|
-
* @param registry The registry providing
|
31
|
-
* the media and their content.
|
32
|
-
* @param options options object with following properties:
|
33
|
-
* - elementName When set, overrides the default element name for semantic media embeds.
|
34
|
-
* - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
|
35
|
-
* - renderForEditingView When `true`, the converter will create a view specific for the
|
36
|
-
* editing pipeline (e.g. including CSS classes, content placeholders).
|
37
|
-
*/
|
38
|
-
export declare function modelToViewUrlAttributeConverter(registry: MediaRegistry, options: MediaOptions): (dispatcher: DowncastDispatcher) => void;
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
import type { DowncastDispatcher } from 'ckeditor5/src/engine.js';
|
6
|
+
import type MediaRegistry from './mediaregistry.js';
|
7
|
+
import type { MediaOptions } from './utils.js';
|
8
|
+
/**
|
9
|
+
* Returns a function that converts the model "url" attribute to the view representation.
|
10
|
+
*
|
11
|
+
* Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
12
|
+
*
|
13
|
+
* ```html
|
14
|
+
* <figure class="media">
|
15
|
+
* <oembed url="foo"></oembed>
|
16
|
+
* </figure>
|
17
|
+
* ```
|
18
|
+
*
|
19
|
+
* or "non-semantic" (for the editing view pipeline):
|
20
|
+
*
|
21
|
+
* ```html
|
22
|
+
* <figure class="media">
|
23
|
+
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
24
|
+
* </figure>
|
25
|
+
* ```
|
26
|
+
*
|
27
|
+
* **Note:** Changing the model "url" attribute replaces the entire content of the
|
28
|
+
* `<figure>` in the view.
|
29
|
+
*
|
30
|
+
* @param registry The registry providing
|
31
|
+
* the media and their content.
|
32
|
+
* @param options options object with following properties:
|
33
|
+
* - elementName When set, overrides the default element name for semantic media embeds.
|
34
|
+
* - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
|
35
|
+
* - renderForEditingView When `true`, the converter will create a view specific for the
|
36
|
+
* editing pipeline (e.g. including CSS classes, content placeholders).
|
37
|
+
*/
|
38
|
+
export declare function modelToViewUrlAttributeConverter(registry: MediaRegistry, options: MediaOptions): (dispatcher: DowncastDispatcher) => void;
|
package/src/converters.js
CHANGED
@@ -1,53 +1,53 @@
|
|
1
|
-
/**
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
-
*/
|
5
|
-
/**
|
6
|
-
* Returns a function that converts the model "url" attribute to the view representation.
|
7
|
-
*
|
8
|
-
* Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
9
|
-
*
|
10
|
-
* ```html
|
11
|
-
* <figure class="media">
|
12
|
-
* <oembed url="foo"></oembed>
|
13
|
-
* </figure>
|
14
|
-
* ```
|
15
|
-
*
|
16
|
-
* or "non-semantic" (for the editing view pipeline):
|
17
|
-
*
|
18
|
-
* ```html
|
19
|
-
* <figure class="media">
|
20
|
-
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
21
|
-
* </figure>
|
22
|
-
* ```
|
23
|
-
*
|
24
|
-
* **Note:** Changing the model "url" attribute replaces the entire content of the
|
25
|
-
* `<figure>` in the view.
|
26
|
-
*
|
27
|
-
* @param registry The registry providing
|
28
|
-
* the media and their content.
|
29
|
-
* @param options options object with following properties:
|
30
|
-
* - elementName When set, overrides the default element name for semantic media embeds.
|
31
|
-
* - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
|
32
|
-
* - renderForEditingView When `true`, the converter will create a view specific for the
|
33
|
-
* editing pipeline (e.g. including CSS classes, content placeholders).
|
34
|
-
*/
|
35
|
-
export function modelToViewUrlAttributeConverter(registry, options) {
|
36
|
-
const converter = (evt, data, conversionApi) => {
|
37
|
-
if (!conversionApi.consumable.consume(data.item, evt.name)) {
|
38
|
-
return;
|
39
|
-
}
|
40
|
-
const url = data.attributeNewValue;
|
41
|
-
const viewWriter = conversionApi.writer;
|
42
|
-
const figure = conversionApi.mapper.toViewElement(data.item);
|
43
|
-
const mediaContentElement = [...figure.getChildren()]
|
44
|
-
.find(child => child.getCustomProperty('media-content'));
|
45
|
-
// TODO: removing the wrapper and creating it from scratch is a hack. We can do better than that.
|
46
|
-
viewWriter.remove(mediaContentElement);
|
47
|
-
const mediaViewElement = registry.getMediaViewElement(viewWriter, url, options);
|
48
|
-
viewWriter.insert(viewWriter.createPositionAt(figure, 0), mediaViewElement);
|
49
|
-
};
|
50
|
-
return dispatcher => {
|
51
|
-
dispatcher.on('attribute:url:media', converter);
|
52
|
-
};
|
53
|
-
}
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
/**
|
6
|
+
* Returns a function that converts the model "url" attribute to the view representation.
|
7
|
+
*
|
8
|
+
* Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
9
|
+
*
|
10
|
+
* ```html
|
11
|
+
* <figure class="media">
|
12
|
+
* <oembed url="foo"></oembed>
|
13
|
+
* </figure>
|
14
|
+
* ```
|
15
|
+
*
|
16
|
+
* or "non-semantic" (for the editing view pipeline):
|
17
|
+
*
|
18
|
+
* ```html
|
19
|
+
* <figure class="media">
|
20
|
+
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
21
|
+
* </figure>
|
22
|
+
* ```
|
23
|
+
*
|
24
|
+
* **Note:** Changing the model "url" attribute replaces the entire content of the
|
25
|
+
* `<figure>` in the view.
|
26
|
+
*
|
27
|
+
* @param registry The registry providing
|
28
|
+
* the media and their content.
|
29
|
+
* @param options options object with following properties:
|
30
|
+
* - elementName When set, overrides the default element name for semantic media embeds.
|
31
|
+
* - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
|
32
|
+
* - renderForEditingView When `true`, the converter will create a view specific for the
|
33
|
+
* editing pipeline (e.g. including CSS classes, content placeholders).
|
34
|
+
*/
|
35
|
+
export function modelToViewUrlAttributeConverter(registry, options) {
|
36
|
+
const converter = (evt, data, conversionApi) => {
|
37
|
+
if (!conversionApi.consumable.consume(data.item, evt.name)) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
const url = data.attributeNewValue;
|
41
|
+
const viewWriter = conversionApi.writer;
|
42
|
+
const figure = conversionApi.mapper.toViewElement(data.item);
|
43
|
+
const mediaContentElement = [...figure.getChildren()]
|
44
|
+
.find(child => child.getCustomProperty('media-content'));
|
45
|
+
// TODO: removing the wrapper and creating it from scratch is a hack. We can do better than that.
|
46
|
+
viewWriter.remove(mediaContentElement);
|
47
|
+
const mediaViewElement = registry.getMediaViewElement(viewWriter, url, options);
|
48
|
+
viewWriter.insert(viewWriter.createPositionAt(figure, 0), mediaViewElement);
|
49
|
+
};
|
50
|
+
return dispatcher => {
|
51
|
+
dispatcher.on('attribute:url:media', converter);
|
52
|
+
};
|
53
|
+
}
|