@ckeditor/ckeditor5-media-embed 48.1.1 → 48.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/ckeditor5-metadata.json +108 -12
- package/dist/augmentation.d.ts +9 -1
- package/dist/index-content.css +45 -1
- package/dist/index-editor.css +8 -5
- package/dist/index.css +49 -6
- package/dist/index.css.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +920 -11
- package/dist/index.js.map +1 -1
- package/dist/mediaembed.d.ts +1 -1
- package/dist/mediaembedconfig.d.ts +233 -10
- package/dist/mediaembedresize/constants.d.ts +17 -0
- package/dist/mediaembedresize/mediaembedresizeediting.d.ts +47 -0
- package/dist/mediaembedresize/mediaembedresizehandles.d.ts +42 -0
- package/dist/mediaembedresize/resizemediaembedcommand.d.ts +40 -0
- package/dist/mediaembedresize.d.ts +30 -0
- package/dist/mediaembedstyle/constants.d.ts +29 -0
- package/dist/mediaembedstyle/mediaembedstylecommand.d.ts +72 -0
- package/dist/mediaembedstyle/mediaembedstyleediting.d.ts +43 -0
- package/dist/mediaembedstyle/mediaembedstyleui.d.ts +81 -0
- package/dist/mediaembedstyle/utils.d.ts +23 -0
- package/dist/mediaembedstyle.d.ts +34 -0
- package/dist/mediaembedtoolbar.d.ts +0 -1
- package/dist/translations/af.js +1 -1
- package/dist/translations/af.umd.js +1 -1
- package/dist/translations/ar.js +1 -1
- package/dist/translations/ar.umd.js +1 -1
- package/dist/translations/ast.js +1 -1
- package/dist/translations/ast.umd.js +1 -1
- package/dist/translations/az.js +1 -1
- package/dist/translations/az.umd.js +1 -1
- package/dist/translations/be.js +1 -1
- package/dist/translations/be.umd.js +1 -1
- package/dist/translations/bg.js +1 -1
- package/dist/translations/bg.umd.js +1 -1
- package/dist/translations/bn.js +1 -1
- package/dist/translations/bn.umd.js +1 -1
- package/dist/translations/bs.js +1 -1
- package/dist/translations/bs.umd.js +1 -1
- package/dist/translations/ca.js +1 -1
- package/dist/translations/ca.umd.js +1 -1
- package/dist/translations/cs.js +1 -1
- package/dist/translations/cs.umd.js +1 -1
- package/dist/translations/da.js +1 -1
- package/dist/translations/da.umd.js +1 -1
- package/dist/translations/de-ch.js +1 -1
- package/dist/translations/de-ch.umd.js +1 -1
- package/dist/translations/de.js +1 -1
- package/dist/translations/de.umd.js +1 -1
- package/dist/translations/el.js +1 -1
- package/dist/translations/el.umd.js +1 -1
- package/dist/translations/en-au.js +1 -1
- package/dist/translations/en-au.umd.js +1 -1
- package/dist/translations/en-gb.js +1 -1
- package/dist/translations/en-gb.umd.js +1 -1
- package/dist/translations/en.js +1 -1
- package/dist/translations/en.umd.js +1 -1
- package/dist/translations/eo.js +1 -1
- package/dist/translations/eo.umd.js +1 -1
- package/dist/translations/es-co.js +1 -1
- package/dist/translations/es-co.umd.js +1 -1
- package/dist/translations/es.js +1 -1
- package/dist/translations/es.umd.js +1 -1
- package/dist/translations/et.js +1 -1
- package/dist/translations/et.umd.js +1 -1
- package/dist/translations/eu.js +1 -1
- package/dist/translations/eu.umd.js +1 -1
- package/dist/translations/fa.js +1 -1
- package/dist/translations/fa.umd.js +1 -1
- package/dist/translations/fi.js +1 -1
- package/dist/translations/fi.umd.js +1 -1
- package/dist/translations/fr.js +1 -1
- package/dist/translations/fr.umd.js +1 -1
- package/dist/translations/gl.js +1 -1
- package/dist/translations/gl.umd.js +1 -1
- package/dist/translations/gu.js +1 -1
- package/dist/translations/gu.umd.js +1 -1
- package/dist/translations/he.js +1 -1
- package/dist/translations/he.umd.js +1 -1
- package/dist/translations/hi.js +1 -1
- package/dist/translations/hi.umd.js +1 -1
- package/dist/translations/hr.js +1 -1
- package/dist/translations/hr.umd.js +1 -1
- package/dist/translations/hu.js +1 -1
- package/dist/translations/hu.umd.js +1 -1
- package/dist/translations/hy.js +1 -1
- package/dist/translations/hy.umd.js +1 -1
- package/dist/translations/id.js +1 -1
- package/dist/translations/id.umd.js +1 -1
- package/dist/translations/it.js +1 -1
- package/dist/translations/it.umd.js +1 -1
- package/dist/translations/ja.js +1 -1
- package/dist/translations/ja.umd.js +1 -1
- package/dist/translations/jv.js +1 -1
- package/dist/translations/jv.umd.js +1 -1
- package/dist/translations/kk.js +1 -1
- package/dist/translations/kk.umd.js +1 -1
- package/dist/translations/km.js +1 -1
- package/dist/translations/km.umd.js +1 -1
- package/dist/translations/kn.js +1 -1
- package/dist/translations/kn.umd.js +1 -1
- package/dist/translations/ko.js +1 -1
- package/dist/translations/ko.umd.js +1 -1
- package/dist/translations/ku.js +1 -1
- package/dist/translations/ku.umd.js +1 -1
- package/dist/translations/lt.js +1 -1
- package/dist/translations/lt.umd.js +1 -1
- package/dist/translations/lv.js +1 -1
- package/dist/translations/lv.umd.js +1 -1
- package/dist/translations/ms.js +1 -1
- package/dist/translations/ms.umd.js +1 -1
- package/dist/translations/nb.js +1 -1
- package/dist/translations/nb.umd.js +1 -1
- package/dist/translations/ne.js +1 -1
- package/dist/translations/ne.umd.js +1 -1
- package/dist/translations/nl.js +1 -1
- package/dist/translations/nl.umd.js +1 -1
- package/dist/translations/no.js +1 -1
- package/dist/translations/no.umd.js +1 -1
- package/dist/translations/oc.js +1 -1
- package/dist/translations/oc.umd.js +1 -1
- package/dist/translations/pl.js +1 -1
- package/dist/translations/pl.umd.js +1 -1
- package/dist/translations/pt-br.js +1 -1
- package/dist/translations/pt-br.umd.js +1 -1
- package/dist/translations/pt.js +1 -1
- package/dist/translations/pt.umd.js +1 -1
- package/dist/translations/ro.js +1 -1
- package/dist/translations/ro.umd.js +1 -1
- package/dist/translations/ru.js +1 -1
- package/dist/translations/ru.umd.js +1 -1
- package/dist/translations/si.js +1 -1
- package/dist/translations/si.umd.js +1 -1
- package/dist/translations/sk.js +1 -1
- package/dist/translations/sk.umd.js +1 -1
- package/dist/translations/sl.js +1 -1
- package/dist/translations/sl.umd.js +1 -1
- package/dist/translations/sq.js +1 -1
- package/dist/translations/sq.umd.js +1 -1
- package/dist/translations/sr-latn.js +1 -1
- package/dist/translations/sr-latn.umd.js +1 -1
- package/dist/translations/sr.js +1 -1
- package/dist/translations/sr.umd.js +1 -1
- package/dist/translations/sv.js +1 -1
- package/dist/translations/sv.umd.js +1 -1
- package/dist/translations/th.js +1 -1
- package/dist/translations/th.umd.js +1 -1
- package/dist/translations/ti.js +1 -1
- package/dist/translations/ti.umd.js +1 -1
- package/dist/translations/tk.js +1 -1
- package/dist/translations/tk.umd.js +1 -1
- package/dist/translations/tr.js +1 -1
- package/dist/translations/tr.umd.js +1 -1
- package/dist/translations/tt.js +1 -1
- package/dist/translations/tt.umd.js +1 -1
- package/dist/translations/ug.js +1 -1
- package/dist/translations/ug.umd.js +1 -1
- package/dist/translations/uk.js +1 -1
- package/dist/translations/uk.umd.js +1 -1
- package/dist/translations/ur.js +1 -1
- package/dist/translations/ur.umd.js +1 -1
- package/dist/translations/uz.js +1 -1
- package/dist/translations/uz.umd.js +1 -1
- package/dist/translations/vi.js +1 -1
- package/dist/translations/vi.umd.js +1 -1
- package/dist/translations/zh-cn.js +1 -1
- package/dist/translations/zh-cn.umd.js +1 -1
- package/dist/translations/zh.js +1 -1
- package/dist/translations/zh.umd.js +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
4
|
*/
|
|
5
5
|
import { Command, Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
|
|
6
|
-
import { isWidget, toWidget, findOptimalInsertionRange, Widget, WidgetToolbarRepository } from '@ckeditor/ckeditor5-widget/dist/index.js';
|
|
6
|
+
import { isWidget, toWidget, findOptimalInsertionRange, Widget, WidgetToolbarRepository, WidgetResize } from '@ckeditor/ckeditor5-widget/dist/index.js';
|
|
7
7
|
import { logWarning, toArray, first, global, FocusTracker, KeystrokeHandler } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
|
8
|
-
import { IconView, Template, View, submitHandler, LabeledFieldView, createLabeledInputText, Dialog, ButtonView, MenuBarMenuListItemButtonView, CssTransitionDisablerMixin } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
9
|
-
import { IconMediaPlaceholder, IconMedia } from '@ckeditor/ckeditor5-icons/dist/index.js';
|
|
8
|
+
import { IconView, Template, View, submitHandler, LabeledFieldView, createLabeledInputText, Dialog, ButtonView, MenuBarMenuListItemButtonView, CssTransitionDisablerMixin, createDropdown, SplitButtonView, addToolbarToDropdown } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
9
|
+
import { IconMediaPlaceholder, IconMedia, IconObjectInlineRight, IconObjectRight, IconObjectCenter, IconObjectLeft, IconObjectInlineLeft } from '@ckeditor/ckeditor5-icons/dist/index.js';
|
|
10
10
|
import { ModelLivePosition, ModelLiveRange } from '@ckeditor/ckeditor5-engine/dist/index.js';
|
|
11
11
|
import { Clipboard } from '@ckeditor/ckeditor5-clipboard/dist/index.js';
|
|
12
12
|
import { Delete } from '@ckeditor/ckeditor5-typing/dist/index.js';
|
|
@@ -488,7 +488,7 @@ const mediaPlaceholderIconViewBox = '0 0 64 42';
|
|
|
488
488
|
],
|
|
489
489
|
html: (match)=>{
|
|
490
490
|
const id = match[1];
|
|
491
|
-
return '<div
|
|
491
|
+
return '<div>' + `<iframe src="https://www.dailymotion.com/embed/video/${id}" ` + 'width="1280" height="720" ' + 'style="width: 100%; height: auto; aspect-ratio: 16 / 9; border: 0; display: block;" ' + 'frameborder="0" allowfullscreen allow="autoplay">' + '</iframe>' + '</div>';
|
|
492
492
|
}
|
|
493
493
|
},
|
|
494
494
|
{
|
|
@@ -500,7 +500,9 @@ const mediaPlaceholderIconViewBox = '0 0 64 42';
|
|
|
500
500
|
],
|
|
501
501
|
html: (match)=>{
|
|
502
502
|
const id = match[1];
|
|
503
|
-
|
|
503
|
+
const isTrack = id.startsWith('track/');
|
|
504
|
+
const iframeStyle = isTrack ? 'width: 100%; height: 80px; border: 0; display: block;' : 'width: 100%; height: auto; aspect-ratio: 100 / 126; border: 0; display: block;';
|
|
505
|
+
return '<div>' + `<iframe src="https://open.spotify.com/embed/${id}" ` + `width="300" height="${isTrack ? '80' : '378'}" ` + `style="${iframeStyle}" ` + 'frameborder="0" allowtransparency="true" allow="encrypted-media">' + '</iframe>' + '</div>';
|
|
504
506
|
}
|
|
505
507
|
},
|
|
506
508
|
{
|
|
@@ -515,7 +517,7 @@ const mediaPlaceholderIconViewBox = '0 0 64 42';
|
|
|
515
517
|
html: (match)=>{
|
|
516
518
|
const id = match[1];
|
|
517
519
|
const time = match[2];
|
|
518
|
-
return '<div
|
|
520
|
+
return '<div>' + `<iframe src="https://www.youtube.com/embed/${id}${time ? `?start=${time}` : ''}" ` + 'width="1280" height="720" ' + 'style="width: 100%; height: auto; aspect-ratio: 16 / 9; border: 0; display: block;" ' + 'frameborder="0" allow="autoplay; encrypted-media" referrerpolicy="strict-origin-when-cross-origin" ' + 'allowfullscreen>' + '</iframe>' + '</div>';
|
|
519
521
|
}
|
|
520
522
|
},
|
|
521
523
|
{
|
|
@@ -531,7 +533,7 @@ const mediaPlaceholderIconViewBox = '0 0 64 42';
|
|
|
531
533
|
],
|
|
532
534
|
html: (match)=>{
|
|
533
535
|
const id = match[1];
|
|
534
|
-
return '<div
|
|
536
|
+
return '<div>' + `<iframe src="https://player.vimeo.com/video/${id}" ` + 'width="1280" height="720" ' + 'style="width: 100%; height: auto; aspect-ratio: 16 / 9; border: 0; display: block;" ' + 'frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>' + '</iframe>' + '</div>';
|
|
535
537
|
}
|
|
536
538
|
},
|
|
537
539
|
{
|
|
@@ -787,7 +789,8 @@ const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w-]+\.[\w-.~:/?#[\]@!$&'()*+,;=%]+$/;
|
|
|
787
789
|
return;
|
|
788
790
|
}
|
|
789
791
|
const mediaEmbedCommand = editor.commands.get('mediaEmbed');
|
|
790
|
-
// Do not anything if media element cannot be inserted at the current position
|
|
792
|
+
// Do not anything if media element cannot be inserted at the current position.
|
|
793
|
+
// See https://github.com/ckeditor/ckeditor5-media-embed/issues/47.
|
|
791
794
|
if (!mediaEmbedCommand.isEnabled) {
|
|
792
795
|
urlRange.detach();
|
|
793
796
|
return;
|
|
@@ -1067,7 +1070,7 @@ function getFormValidators(t, registry) {
|
|
|
1067
1070
|
/**
|
|
1068
1071
|
* The media embed plugin.
|
|
1069
1072
|
*
|
|
1070
|
-
* For a detailed overview, check the {@glink features/media-embed Media Embed feature documentation}.
|
|
1073
|
+
* For a detailed overview, check the {@glink features/media-embed/media-embed Media Embed feature documentation}.
|
|
1071
1074
|
*
|
|
1072
1075
|
* This is a "glue" plugin which loads the following plugins:
|
|
1073
1076
|
*
|
|
@@ -1097,6 +1100,165 @@ function getFormValidators(t, registry) {
|
|
|
1097
1100
|
}
|
|
1098
1101
|
}
|
|
1099
1102
|
|
|
1103
|
+
/**
|
|
1104
|
+
* Built-in style options provided by the plugin. Integrators can refer to these by
|
|
1105
|
+
* name in {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles`}
|
|
1106
|
+
* to opt out, override individual fields, or coexist with custom styles.
|
|
1107
|
+
*
|
|
1108
|
+
* @internal
|
|
1109
|
+
*/ const DEFAULT_OPTIONS = {
|
|
1110
|
+
alignLeft: {
|
|
1111
|
+
name: 'alignLeft',
|
|
1112
|
+
title: 'Left aligned media',
|
|
1113
|
+
icon: IconObjectInlineLeft,
|
|
1114
|
+
className: 'media-style-align-left'
|
|
1115
|
+
},
|
|
1116
|
+
alignBlockLeft: {
|
|
1117
|
+
name: 'alignBlockLeft',
|
|
1118
|
+
title: 'Left aligned media',
|
|
1119
|
+
icon: IconObjectLeft,
|
|
1120
|
+
className: 'media-style-block-align-left'
|
|
1121
|
+
},
|
|
1122
|
+
alignCenter: {
|
|
1123
|
+
name: 'alignCenter',
|
|
1124
|
+
title: 'Centered media',
|
|
1125
|
+
icon: IconObjectCenter,
|
|
1126
|
+
isDefault: true
|
|
1127
|
+
},
|
|
1128
|
+
alignBlockRight: {
|
|
1129
|
+
name: 'alignBlockRight',
|
|
1130
|
+
title: 'Right aligned media',
|
|
1131
|
+
icon: IconObjectRight,
|
|
1132
|
+
className: 'media-style-block-align-right'
|
|
1133
|
+
},
|
|
1134
|
+
alignRight: {
|
|
1135
|
+
name: 'alignRight',
|
|
1136
|
+
title: 'Right aligned media',
|
|
1137
|
+
icon: IconObjectInlineRight,
|
|
1138
|
+
className: 'media-style-align-right'
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
/**
|
|
1142
|
+
* Short icon-name aliases that can be used as the `icon` value in a media style
|
|
1143
|
+
* option definition. Matches the alias set exposed by the image styles feature so
|
|
1144
|
+
* the two APIs feel symmetrical.
|
|
1145
|
+
*
|
|
1146
|
+
* @internal
|
|
1147
|
+
*/ const DEFAULT_ICONS = {
|
|
1148
|
+
inlineLeft: IconObjectInlineLeft,
|
|
1149
|
+
left: IconObjectLeft,
|
|
1150
|
+
center: IconObjectCenter,
|
|
1151
|
+
right: IconObjectRight,
|
|
1152
|
+
inlineRight: IconObjectInlineRight
|
|
1153
|
+
};
|
|
1154
|
+
/**
|
|
1155
|
+
* Built-in dropdown groupings. Each entry references built-in style component names. If any
|
|
1156
|
+
* items are filtered out by configuration, the dropdown is rebuilt from the remaining names
|
|
1157
|
+
* (or skipped entirely if fewer than two remain).
|
|
1158
|
+
*
|
|
1159
|
+
* @internal
|
|
1160
|
+
*/ const DEFAULT_DROPDOWN_DEFINITIONS = [
|
|
1161
|
+
{
|
|
1162
|
+
name: 'mediaEmbed:wrapText',
|
|
1163
|
+
title: 'Wrap text',
|
|
1164
|
+
items: [
|
|
1165
|
+
'mediaEmbed:alignLeft',
|
|
1166
|
+
'mediaEmbed:alignRight'
|
|
1167
|
+
],
|
|
1168
|
+
defaultItem: 'mediaEmbed:alignLeft'
|
|
1169
|
+
},
|
|
1170
|
+
{
|
|
1171
|
+
name: 'mediaEmbed:breakText',
|
|
1172
|
+
title: 'Break text',
|
|
1173
|
+
items: [
|
|
1174
|
+
'mediaEmbed:alignBlockLeft',
|
|
1175
|
+
'mediaEmbed:alignCenter',
|
|
1176
|
+
'mediaEmbed:alignBlockRight'
|
|
1177
|
+
],
|
|
1178
|
+
defaultItem: 'mediaEmbed:alignCenter'
|
|
1179
|
+
}
|
|
1180
|
+
];
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
* Normalizes the {@link module:media-embed/mediaembedconfig~MediaStyleConfig#options style options}
|
|
1184
|
+
* provided by the integrator. Each entry is resolved into a full
|
|
1185
|
+
* {@link module:media-embed/mediaembedconfig~MediaStyleOptionDefinition} and invalid entries
|
|
1186
|
+
* are filtered out with a console warning.
|
|
1187
|
+
*
|
|
1188
|
+
* @internal
|
|
1189
|
+
*/ function normalizeStyles(configuredStyles) {
|
|
1190
|
+
const configured = configuredStyles.options || [];
|
|
1191
|
+
return configured.map((entry)=>normalizeDefinition(entry)).filter((entry)=>isValidOption(entry));
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Resolves a single config entry into a style option definition. A string entry is first
|
|
1195
|
+
* promoted to its object form (`{ name }`) and then shallow-merged on top of the matching
|
|
1196
|
+
* built-in default — entries without a matching built-in pass through unchanged and are
|
|
1197
|
+
* rejected by {@link ~isValidOption} if they lack required fields.
|
|
1198
|
+
*
|
|
1199
|
+
* Also resolves icon-name aliases (`'left'`, `'inlineLeft'`, etc.) to the corresponding
|
|
1200
|
+
* SVG sources from {@link module:media-embed/mediaembedstyle/constants~DEFAULT_ICONS}.
|
|
1201
|
+
*/ function normalizeDefinition(entry) {
|
|
1202
|
+
// Spread of `undefined` is a no-op, so unknown-name overrides produce a clean passthrough.
|
|
1203
|
+
const override = typeof entry === 'string' ? {
|
|
1204
|
+
name: entry
|
|
1205
|
+
} : entry;
|
|
1206
|
+
const definition = {
|
|
1207
|
+
...DEFAULT_OPTIONS[override.name],
|
|
1208
|
+
...override
|
|
1209
|
+
};
|
|
1210
|
+
if (typeof definition.icon === 'string' && DEFAULT_ICONS[definition.icon]) {
|
|
1211
|
+
definition.icon = DEFAULT_ICONS[definition.icon];
|
|
1212
|
+
}
|
|
1213
|
+
return definition;
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Validates a normalized style option. `name`, `title`, and `icon` are always required.
|
|
1217
|
+
* `className` is required unless the entry is the default style (defaults encode as
|
|
1218
|
+
* attribute-absence and intentionally have no class). Emits a console warning and returns
|
|
1219
|
+
* `false` when any of these checks fails.
|
|
1220
|
+
*/ function isValidOption(option) {
|
|
1221
|
+
if (!option.name || !option.title || !option.icon || !option.isDefault && !option.className) {
|
|
1222
|
+
warnInvalidStyle({
|
|
1223
|
+
style: option
|
|
1224
|
+
});
|
|
1225
|
+
return false;
|
|
1226
|
+
}
|
|
1227
|
+
return true;
|
|
1228
|
+
}
|
|
1229
|
+
function warnInvalidStyle(info) {
|
|
1230
|
+
/**
|
|
1231
|
+
* The media style configuration provided in the editor config is invalid. The warning is
|
|
1232
|
+
* emitted in two situations:
|
|
1233
|
+
*
|
|
1234
|
+
* * An entry in {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles.options`}
|
|
1235
|
+
* does not reference a built-in style by name (`'alignLeft'`, `'alignBlockLeft'`,
|
|
1236
|
+
* `'alignCenter'`, `'alignBlockRight'`, `'alignRight'`) and does not follow the
|
|
1237
|
+
* {@link module:media-embed/mediaembedconfig~MediaStyleOptionDefinition} shape —
|
|
1238
|
+
* `name`, `title`, `icon`, and (unless `isDefault: true`) `className` are required.
|
|
1239
|
+
* The offending entry is reported under the `style` parameter.
|
|
1240
|
+
* * A dropdown entry placed inline in
|
|
1241
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#toolbar `config.mediaEmbed.toolbar`}
|
|
1242
|
+
* does not follow the {@link module:media-embed/mediaembedconfig~MediaStyleDropdownDefinition} shape
|
|
1243
|
+
* (`name` and every `items[]` entry must use the full `mediaEmbed:` prefix, `defaultItem` must
|
|
1244
|
+
* be one of the `items`, `title` must be a non-empty string), or its `items[]` reference styles
|
|
1245
|
+
* that are not in the resolved
|
|
1246
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles`} list.
|
|
1247
|
+
* The offending entry is reported under the `dropdown` parameter.
|
|
1248
|
+
*
|
|
1249
|
+
* @error media-style-configuration-definition-invalid
|
|
1250
|
+
*/ logWarning('media-style-configuration-definition-invalid', info);
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Type guard for toolbar config entries shaped like a media style dropdown definition. The
|
|
1254
|
+
* discriminator is `defaultItem` — generic toolbar groupings use `items` + `label` and never
|
|
1255
|
+
* carry a `defaultItem` field.
|
|
1256
|
+
*
|
|
1257
|
+
* @internal
|
|
1258
|
+
*/ function isMediaStyleDropdown(item) {
|
|
1259
|
+
return typeof item === 'object' && item !== null && typeof item.defaultItem === 'string';
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1100
1262
|
/**
|
|
1101
1263
|
* The media embed toolbar plugin. It creates a toolbar for media embed that shows up when the media element is selected.
|
|
1102
1264
|
*
|
|
@@ -1128,11 +1290,758 @@ function getFormValidators(t, registry) {
|
|
|
1128
1290
|
const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
|
|
1129
1291
|
widgetToolbarRepository.register('mediaEmbed', {
|
|
1130
1292
|
ariaLabel: t('Media toolbar'),
|
|
1131
|
-
items: editor.config.get('mediaEmbed.toolbar') || [],
|
|
1293
|
+
items: normalizeDeclarativeConfig(editor.ui.componentFactory, editor.config.get('mediaEmbed.toolbar') || []),
|
|
1132
1294
|
getRelatedElement: getSelectedMediaViewWidget
|
|
1133
1295
|
});
|
|
1134
1296
|
}
|
|
1135
1297
|
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Flattens dropdown definitions to their factory names, dropping any `mediaEmbed:`-prefixed
|
|
1300
|
+
* name the style UI did not register — otherwise the toolbar crashes with `componentfactory-item-missing`.
|
|
1301
|
+
* Non-string entries (e.g. generic `{ label, items }` toolbar groupings) pass through unchanged.
|
|
1302
|
+
*/ function normalizeDeclarativeConfig(factory, config) {
|
|
1303
|
+
return config.map((item)=>isMediaStyleDropdown(item) ? item.name : item).filter((item)=>typeof item !== 'string' || !item.startsWith('mediaEmbed:') || factory.has(item));
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
* The resize media embed command.
|
|
1308
|
+
*/ class ResizeMediaEmbedCommand extends Command {
|
|
1309
|
+
/**
|
|
1310
|
+
* @inheritDoc
|
|
1311
|
+
*/ refresh() {
|
|
1312
|
+
const element = getSelectedMediaModelWidget(this.editor.model.document.selection);
|
|
1313
|
+
this.isEnabled = !!element;
|
|
1314
|
+
if (!element || !element.hasAttribute('resizedWidth')) {
|
|
1315
|
+
this.value = null;
|
|
1316
|
+
} else {
|
|
1317
|
+
this.value = element.getAttribute('resizedWidth');
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Executes the command.
|
|
1322
|
+
*
|
|
1323
|
+
* ```ts
|
|
1324
|
+
* // Sets the width as a percentage of the parent width:
|
|
1325
|
+
* editor.execute( 'resizeMediaEmbed', { width: '50%' } );
|
|
1326
|
+
*
|
|
1327
|
+
* // Removes the resize and restores the default width:
|
|
1328
|
+
* editor.execute( 'resizeMediaEmbed', { width: null } );
|
|
1329
|
+
* ```
|
|
1330
|
+
*
|
|
1331
|
+
* @param options
|
|
1332
|
+
* @param options.width The new width of the media embed as a CSS `width` value
|
|
1333
|
+
* (e.g. `'50%'`), or `null` to remove the resize.
|
|
1334
|
+
* @fires execute
|
|
1335
|
+
*/ execute(options) {
|
|
1336
|
+
const model = this.editor.model;
|
|
1337
|
+
const mediaElement = getSelectedMediaModelWidget(model.document.selection);
|
|
1338
|
+
if (mediaElement) {
|
|
1339
|
+
model.change((writer)=>{
|
|
1340
|
+
writer.setAttribute('resizedWidth', options.width, mediaElement);
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
1348
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
1349
|
+
*/ /**
|
|
1350
|
+
* @module media-embed/mediaembedresize/constants
|
|
1351
|
+
*/ /**
|
|
1352
|
+
* The view class applied to a resized media embed figure.
|
|
1353
|
+
*
|
|
1354
|
+
* Shared between the editing plugin (which toggles it via downcast of `resizedWidth` and consumes
|
|
1355
|
+
* it on upcast) and the handles plugin (which adds it during drag and strips it on commit), so
|
|
1356
|
+
* both layers agree on the exact class name.
|
|
1357
|
+
*
|
|
1358
|
+
* @internal
|
|
1359
|
+
*/ const RESIZED_MEDIA_CLASS = 'media_resized';
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* The media embed resize editing feature.
|
|
1363
|
+
*
|
|
1364
|
+
* It adds the ability to resize each media embed using handles.
|
|
1365
|
+
*/ class MediaEmbedResizeEditing extends Plugin {
|
|
1366
|
+
/**
|
|
1367
|
+
* @inheritDoc
|
|
1368
|
+
*/ static get requires() {
|
|
1369
|
+
return [
|
|
1370
|
+
MediaEmbedEditing
|
|
1371
|
+
];
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* @inheritDoc
|
|
1375
|
+
*/ static get pluginName() {
|
|
1376
|
+
return 'MediaEmbedResizeEditing';
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* @inheritDoc
|
|
1380
|
+
* @internal
|
|
1381
|
+
*/ static get licenseFeatureCode() {
|
|
1382
|
+
return 'MER';
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* @inheritDoc
|
|
1386
|
+
*/ static get isOfficialPlugin() {
|
|
1387
|
+
return true;
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* @inheritDoc
|
|
1391
|
+
*/ static get isPremiumPlugin() {
|
|
1392
|
+
return true;
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* @inheritDoc
|
|
1396
|
+
*/ init() {
|
|
1397
|
+
const editor = this.editor;
|
|
1398
|
+
editor.commands.add('resizeMediaEmbed', new ResizeMediaEmbedCommand(editor));
|
|
1399
|
+
this._registerConverters();
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* @inheritDoc
|
|
1403
|
+
*/ afterInit() {
|
|
1404
|
+
this._registerSchema();
|
|
1405
|
+
}
|
|
1406
|
+
_registerSchema() {
|
|
1407
|
+
const schema = this.editor.model.schema;
|
|
1408
|
+
schema.extend('media', {
|
|
1409
|
+
allowAttributes: [
|
|
1410
|
+
'resizedWidth'
|
|
1411
|
+
]
|
|
1412
|
+
});
|
|
1413
|
+
schema.setAttributeProperties('resizedWidth', {
|
|
1414
|
+
isFormatting: true
|
|
1415
|
+
});
|
|
1416
|
+
}
|
|
1417
|
+
/**
|
|
1418
|
+
* Registers media embed resize converters.
|
|
1419
|
+
*/ _registerConverters() {
|
|
1420
|
+
const editor = this.editor;
|
|
1421
|
+
// Downcast (model → view): resizedWidth → style.width + class on <figure>.
|
|
1422
|
+
editor.conversion.for('downcast').add((dispatcher)=>dispatcher.on('attribute:resizedWidth:media', (evt, data, conversionApi)=>{
|
|
1423
|
+
if (!conversionApi.consumable.consume(data.item, evt.name)) {
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
const viewWriter = conversionApi.writer;
|
|
1427
|
+
const figure = conversionApi.mapper.toViewElement(data.item);
|
|
1428
|
+
if (data.attributeNewValue !== null) {
|
|
1429
|
+
viewWriter.setStyle('width', data.attributeNewValue, figure);
|
|
1430
|
+
viewWriter.addClass(RESIZED_MEDIA_CLASS, figure);
|
|
1431
|
+
} else {
|
|
1432
|
+
viewWriter.removeStyle('width', figure);
|
|
1433
|
+
viewWriter.removeClass(RESIZED_MEDIA_CLASS, figure);
|
|
1434
|
+
}
|
|
1435
|
+
}));
|
|
1436
|
+
// Upcast: style.width on <figure class="media"> → resizedWidth.
|
|
1437
|
+
//
|
|
1438
|
+
// The `hasClass` guard lives in the value callback (not the matcher) because the `media`
|
|
1439
|
+
// class is already consumed upstream by MediaEmbedEditing. Without the guard, any figure
|
|
1440
|
+
// with a width style would match and could race image resize's upcast on image figures.
|
|
1441
|
+
editor.conversion.for('upcast').attributeToAttribute({
|
|
1442
|
+
view: {
|
|
1443
|
+
name: 'figure',
|
|
1444
|
+
styles: {
|
|
1445
|
+
width: /.+/
|
|
1446
|
+
}
|
|
1447
|
+
},
|
|
1448
|
+
model: {
|
|
1449
|
+
key: 'resizedWidth',
|
|
1450
|
+
value: (viewElement)=>{
|
|
1451
|
+
if (!viewElement.hasClass('media')) {
|
|
1452
|
+
return null;
|
|
1453
|
+
}
|
|
1454
|
+
return viewElement.getStyle('width');
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
});
|
|
1458
|
+
// Consume the media_resized class during upcast so it does not cause conversion issues.
|
|
1459
|
+
editor.conversion.for('upcast').add((dispatcher)=>{
|
|
1460
|
+
dispatcher.on('element:figure', (evt, data, conversionApi)=>{
|
|
1461
|
+
conversionApi.consumable.consume(data.viewItem, {
|
|
1462
|
+
classes: [
|
|
1463
|
+
RESIZED_MEDIA_CLASS
|
|
1464
|
+
]
|
|
1465
|
+
});
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* The media embed resize by handles feature.
|
|
1473
|
+
*
|
|
1474
|
+
* It adds the ability to resize each media embed using handles.
|
|
1475
|
+
*/ class MediaEmbedResizeHandles extends Plugin {
|
|
1476
|
+
/**
|
|
1477
|
+
* @inheritDoc
|
|
1478
|
+
*/ static get requires() {
|
|
1479
|
+
return [
|
|
1480
|
+
WidgetResize
|
|
1481
|
+
];
|
|
1482
|
+
}
|
|
1483
|
+
/**
|
|
1484
|
+
* @inheritDoc
|
|
1485
|
+
*/ static get pluginName() {
|
|
1486
|
+
return 'MediaEmbedResizeHandles';
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* @inheritDoc
|
|
1490
|
+
*/ static get isOfficialPlugin() {
|
|
1491
|
+
return true;
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* @inheritDoc
|
|
1495
|
+
*/ init() {
|
|
1496
|
+
const command = this.editor.commands.get('resizeMediaEmbed');
|
|
1497
|
+
this.bind('isEnabled').to(command);
|
|
1498
|
+
this._setupResizerCreator();
|
|
1499
|
+
}
|
|
1500
|
+
/**
|
|
1501
|
+
* Attaches a resizer to every newly inserted media widget. Walks only the ranges
|
|
1502
|
+
* reported by the differ — never the whole document — so unrelated inserts (e.g.
|
|
1503
|
+
* pressing Enter to create a paragraph) cost only the differ check.
|
|
1504
|
+
*
|
|
1505
|
+
* Each resizer's `isEnabled` is bound to the plugin in {@link #_attachResizer},
|
|
1506
|
+
* so it auto-tracks the resize command's state.
|
|
1507
|
+
*/ _setupResizerCreator() {
|
|
1508
|
+
const editor = this.editor;
|
|
1509
|
+
const model = editor.model;
|
|
1510
|
+
const widgetResize = editor.plugins.get(WidgetResize);
|
|
1511
|
+
// Low priority ensures downcast has run before we look up view elements.
|
|
1512
|
+
this.listenTo(model.document, 'change:data', ()=>{
|
|
1513
|
+
for (const change of model.document.differ.getChanges()){
|
|
1514
|
+
if (change.type !== 'insert' || change.name === '$text') {
|
|
1515
|
+
continue;
|
|
1516
|
+
}
|
|
1517
|
+
const insertedRange = model.createRange(change.position, change.position.getShiftedBy(change.length));
|
|
1518
|
+
for (const item of insertedRange.getItems()){
|
|
1519
|
+
if (!item.is('element', 'media')) {
|
|
1520
|
+
continue;
|
|
1521
|
+
}
|
|
1522
|
+
const viewElement = editor.editing.mapper.toViewElement(item);
|
|
1523
|
+
/* istanbul ignore if: paranoid check — conversion has run at this point -- @preserve */ if (!viewElement) {
|
|
1524
|
+
continue;
|
|
1525
|
+
}
|
|
1526
|
+
if (!widgetResize.getResizerByViewElement(viewElement)) {
|
|
1527
|
+
this._attachResizer(item, viewElement);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
}, {
|
|
1532
|
+
priority: 'low'
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Attaches a resizer to a single media widget.
|
|
1537
|
+
*/ _attachResizer(modelElement, widgetView) {
|
|
1538
|
+
const editor = this.editor;
|
|
1539
|
+
const editingView = editor.editing.view;
|
|
1540
|
+
const resizer = editor.plugins.get(WidgetResize).attachTo({
|
|
1541
|
+
unit: '%',
|
|
1542
|
+
modelElement,
|
|
1543
|
+
viewElement: widgetView,
|
|
1544
|
+
editor,
|
|
1545
|
+
getHandleHost: (domWidgetElement)=>domWidgetElement.querySelector('.ck-media__wrapper'),
|
|
1546
|
+
getResizeHost: (domWidgetElement)=>domWidgetElement,
|
|
1547
|
+
onCommit: (newValue)=>{
|
|
1548
|
+
editingView.change((writer)=>writer.removeClass(RESIZED_MEDIA_CLASS, widgetView));
|
|
1549
|
+
editor.execute('resizeMediaEmbed', {
|
|
1550
|
+
width: newValue
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
});
|
|
1554
|
+
resizer.bind('isEnabled').to(this);
|
|
1555
|
+
resizer.on('updateSize', ()=>{
|
|
1556
|
+
if (!widgetView.hasClass(RESIZED_MEDIA_CLASS)) {
|
|
1557
|
+
editingView.change((writer)=>writer.addClass(RESIZED_MEDIA_CLASS, widgetView));
|
|
1558
|
+
}
|
|
1559
|
+
});
|
|
1560
|
+
// Redraw once the view has flushed to DOM — otherwise the freshly inserted resizer
|
|
1561
|
+
// UIElement has no inline styles and handles cluster at the top-left corner (visible
|
|
1562
|
+
// after drag-and-drop until another event eventually triggers a redraw).
|
|
1563
|
+
editingView.once('render', ()=>resizer.redraw());
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
/**
|
|
1568
|
+
* The media embed resize plugin.
|
|
1569
|
+
*
|
|
1570
|
+
* It adds a possibility to resize each media embed using handles.
|
|
1571
|
+
*/ class MediaEmbedResize extends Plugin {
|
|
1572
|
+
/**
|
|
1573
|
+
* @inheritDoc
|
|
1574
|
+
*/ static get requires() {
|
|
1575
|
+
return [
|
|
1576
|
+
MediaEmbedResizeEditing,
|
|
1577
|
+
MediaEmbedResizeHandles
|
|
1578
|
+
];
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* @inheritDoc
|
|
1582
|
+
*/ static get pluginName() {
|
|
1583
|
+
return 'MediaEmbedResize';
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* @inheritDoc
|
|
1587
|
+
*/ static get isOfficialPlugin() {
|
|
1588
|
+
return true;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
/**
|
|
1593
|
+
* The media embed style command. It is used to apply a style option (e.g. an alignment) to a
|
|
1594
|
+
* selected media embed.
|
|
1595
|
+
*
|
|
1596
|
+
* The set of accepted style values comes from the resolved
|
|
1597
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles`}
|
|
1598
|
+
* options. Values not in that set are silently rejected by {@link #execute}.
|
|
1599
|
+
*/ class MediaEmbedStyleCommand extends Command {
|
|
1600
|
+
/**
|
|
1601
|
+
* Resolved styles indexed by `name`. Used to look up the `isDefault` flag at execute time
|
|
1602
|
+
* (any default-marked style clears the attribute) and to validate that a requested style
|
|
1603
|
+
* is part of the resolved options list.
|
|
1604
|
+
*/ _styles;
|
|
1605
|
+
/**
|
|
1606
|
+
* The `name` of the first style with `isDefault: true` in the resolved options, or `null`
|
|
1607
|
+
* when the integrator did not designate a default. Exposed via {@link #value} when the
|
|
1608
|
+
* selected media has no `mediaStyle` attribute, so the default-state UI button can light up.
|
|
1609
|
+
* Does not gate the "clear attribute" branch in {@link #execute} — that uses the per-style
|
|
1610
|
+
* `isDefault` flag so multi-default configs behave consistently with the downcast.
|
|
1611
|
+
*/ _defaultStyleName;
|
|
1612
|
+
/**
|
|
1613
|
+
* Creates an instance of the media embed style command.
|
|
1614
|
+
*
|
|
1615
|
+
* @param editor The editor instance.
|
|
1616
|
+
* @param styles The resolved list of style options that this command will accept.
|
|
1617
|
+
*/ constructor(editor, styles){
|
|
1618
|
+
super(editor);
|
|
1619
|
+
this._styles = new Map(styles.map((style)=>[
|
|
1620
|
+
style.name,
|
|
1621
|
+
style
|
|
1622
|
+
]));
|
|
1623
|
+
const defaultStyle = styles.find((style)=>style.isDefault);
|
|
1624
|
+
this._defaultStyleName = defaultStyle ? defaultStyle.name : null;
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* @inheritDoc
|
|
1628
|
+
*/ refresh() {
|
|
1629
|
+
const element = getSelectedMediaModelWidget(this.editor.model.document.selection);
|
|
1630
|
+
this.isEnabled = !!element;
|
|
1631
|
+
if (!element) {
|
|
1632
|
+
this.value = false;
|
|
1633
|
+
} else if (element.hasAttribute('mediaStyle')) {
|
|
1634
|
+
const styleName = element.getAttribute('mediaStyle');
|
|
1635
|
+
// A previously-applied style may have been dropped from the resolved options list
|
|
1636
|
+
// (e.g. by a config change). Fall back to the effective default so the UI stays
|
|
1637
|
+
// consistent with what the downcast actually renders (no class for unknown names).
|
|
1638
|
+
this.value = this._styles.has(styleName) ? styleName : this._defaultStyleName ?? false;
|
|
1639
|
+
} else {
|
|
1640
|
+
this.value = this._defaultStyleName ?? false;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Executes the command and applies the chosen style to the currently selected media embed.
|
|
1645
|
+
*
|
|
1646
|
+
* ```ts
|
|
1647
|
+
* editor.execute( 'mediaStyle', { value: 'alignLeft' } );
|
|
1648
|
+
* editor.execute( 'mediaStyle', { value: 'alignCenter' } ); // removes the attribute — alignCenter is the built-in default
|
|
1649
|
+
* editor.execute( 'mediaStyle', { value: null } ); // removes the attribute
|
|
1650
|
+
* ```
|
|
1651
|
+
*
|
|
1652
|
+
* The default style is encoded on the model as the absence of the `mediaStyle` attribute.
|
|
1653
|
+
* Passing any `isDefault: true` style name (or `null`) therefore clears the attribute. Values
|
|
1654
|
+
* that are neither falsy, an `isDefault` style, nor present in the resolved options list are
|
|
1655
|
+
* silently rejected.
|
|
1656
|
+
*
|
|
1657
|
+
* @param options
|
|
1658
|
+
* @param options.value The name of the style to apply, or `null` to clear the alignment.
|
|
1659
|
+
* @fires execute
|
|
1660
|
+
*/ execute(options) {
|
|
1661
|
+
const model = this.editor.model;
|
|
1662
|
+
const element = getSelectedMediaModelWidget(model.document.selection);
|
|
1663
|
+
const requestedStyle = options.value;
|
|
1664
|
+
// Falsy value or any `isDefault: true` style clears the attribute. Default styles encode
|
|
1665
|
+
// as attribute-absence on the model. The downcast emits no class for them.
|
|
1666
|
+
if (!requestedStyle || this._styles.get(requestedStyle)?.isDefault) {
|
|
1667
|
+
model.change((writer)=>{
|
|
1668
|
+
writer.removeAttribute('mediaStyle', element);
|
|
1669
|
+
});
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
// Reject names that are not part of the resolved options list.
|
|
1673
|
+
if (!this._styles.has(requestedStyle)) {
|
|
1674
|
+
return;
|
|
1675
|
+
}
|
|
1676
|
+
model.change((writer)=>{
|
|
1677
|
+
writer.setAttribute('mediaStyle', requestedStyle, element);
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/**
|
|
1683
|
+
* The media embed style engine plugin. It extends the schema with the `mediaStyle` attribute,
|
|
1684
|
+
* registers the {@link module:media-embed/mediaembedstyle/mediaembedstylecommand~MediaEmbedStyleCommand} command,
|
|
1685
|
+
* and adds the converters that apply alignment CSS classes to the figure.
|
|
1686
|
+
*/ class MediaEmbedStyleEditing extends Plugin {
|
|
1687
|
+
/**
|
|
1688
|
+
* The resolved list of media style options. Built once from
|
|
1689
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles`}
|
|
1690
|
+
* during {@link #init} and consumed by both the command and the UI plugin (single source of truth).
|
|
1691
|
+
*
|
|
1692
|
+
* @internal
|
|
1693
|
+
* @readonly
|
|
1694
|
+
*/ normalizedStyles;
|
|
1695
|
+
/**
|
|
1696
|
+
* @inheritDoc
|
|
1697
|
+
*/ static get requires() {
|
|
1698
|
+
return [
|
|
1699
|
+
MediaEmbedEditing
|
|
1700
|
+
];
|
|
1701
|
+
}
|
|
1702
|
+
/**
|
|
1703
|
+
* @inheritDoc
|
|
1704
|
+
*/ static get pluginName() {
|
|
1705
|
+
return 'MediaEmbedStyleEditing';
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* @inheritDoc
|
|
1709
|
+
*/ static get isOfficialPlugin() {
|
|
1710
|
+
return true;
|
|
1711
|
+
}
|
|
1712
|
+
/**
|
|
1713
|
+
* @inheritDoc
|
|
1714
|
+
*/ init() {
|
|
1715
|
+
const editor = this.editor;
|
|
1716
|
+
const schema = editor.model.schema;
|
|
1717
|
+
editor.config.define('mediaEmbed.styles', {
|
|
1718
|
+
options: Object.keys(DEFAULT_OPTIONS)
|
|
1719
|
+
});
|
|
1720
|
+
this.normalizedStyles = normalizeStyles(editor.config.get('mediaEmbed.styles'));
|
|
1721
|
+
schema.extend('media', {
|
|
1722
|
+
allowAttributes: [
|
|
1723
|
+
'mediaStyle'
|
|
1724
|
+
]
|
|
1725
|
+
});
|
|
1726
|
+
schema.setAttributeProperties('mediaStyle', {
|
|
1727
|
+
isFormatting: true
|
|
1728
|
+
});
|
|
1729
|
+
editor.commands.add('mediaStyle', new MediaEmbedStyleCommand(editor, this.normalizedStyles));
|
|
1730
|
+
this._registerConverters();
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Registers the downcast and upcast converters for the `mediaStyle` attribute.
|
|
1734
|
+
*/ _registerConverters() {
|
|
1735
|
+
const editor = this.editor;
|
|
1736
|
+
// Runtime lookup of style `name` → CSS `className`. Excludes the default style, which is
|
|
1737
|
+
// encoded as the absence of the attribute (no class). Iteration order follows the configured
|
|
1738
|
+
// options order — the upcast relies on this so the last matching class wins on a figure
|
|
1739
|
+
// that has multiple alignment classes.
|
|
1740
|
+
const styleClassMap = new Map(this.normalizedStyles.filter((style)=>!style.isDefault && style.className).map((style)=>[
|
|
1741
|
+
style.name,
|
|
1742
|
+
style.className
|
|
1743
|
+
]));
|
|
1744
|
+
// Downcast: `mediaStyle` → CSS class on the <figure>. Covers both editing and data pipelines.
|
|
1745
|
+
editor.conversion.for('downcast').add((dispatcher)=>dispatcher.on('attribute:mediaStyle:media', (evt, data, conversionApi)=>{
|
|
1746
|
+
if (!conversionApi.consumable.consume(data.item, evt.name)) {
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
const figure = conversionApi.mapper.toViewElement(data.item);
|
|
1750
|
+
const viewWriter = conversionApi.writer;
|
|
1751
|
+
const oldClass = styleClassMap.get(data.attributeOldValue);
|
|
1752
|
+
const newClass = styleClassMap.get(data.attributeNewValue);
|
|
1753
|
+
if (oldClass) {
|
|
1754
|
+
viewWriter.removeClass(oldClass, figure);
|
|
1755
|
+
}
|
|
1756
|
+
if (newClass) {
|
|
1757
|
+
viewWriter.addClass(newClass, figure);
|
|
1758
|
+
}
|
|
1759
|
+
}));
|
|
1760
|
+
// Upcast: alignment class on a media <figure> → `mediaStyle` attribute. Runs at `low`
|
|
1761
|
+
// priority so the main media upcast creates the `media` model element first.
|
|
1762
|
+
editor.conversion.for('upcast').add((dispatcher)=>{
|
|
1763
|
+
dispatcher.on('element:figure', (_evt, data, conversionApi)=>{
|
|
1764
|
+
if (!data.modelRange) {
|
|
1765
|
+
return;
|
|
1766
|
+
}
|
|
1767
|
+
const modelElement = first(data.modelRange.getItems());
|
|
1768
|
+
if (!modelElement || !modelElement.is('element', 'media')) {
|
|
1769
|
+
return;
|
|
1770
|
+
}
|
|
1771
|
+
// Iterate in insertion order — last consumed class wins when multiple
|
|
1772
|
+
// alignment classes are present on the same figure.
|
|
1773
|
+
for (const [styleName, className] of styleClassMap){
|
|
1774
|
+
if (conversionApi.consumable.consume(data.viewItem, {
|
|
1775
|
+
classes: className
|
|
1776
|
+
})) {
|
|
1777
|
+
conversionApi.writer.setAttribute('mediaStyle', styleName, modelElement);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}, {
|
|
1781
|
+
priority: 'low'
|
|
1782
|
+
});
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
/**
|
|
1788
|
+
* The media embed style UI plugin.
|
|
1789
|
+
*
|
|
1790
|
+
* It registers a button for every style in the resolved
|
|
1791
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#styles `config.mediaEmbed.styles`}
|
|
1792
|
+
* list, and the default split-button dropdowns (`mediaEmbed:wrapText`, `mediaEmbed:breakText`)
|
|
1793
|
+
* — filtered to the styles that survived configuration. The resulting components can be placed
|
|
1794
|
+
* in the {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#toolbar media embed toolbar}.
|
|
1795
|
+
*/ class MediaEmbedStyleUI extends Plugin {
|
|
1796
|
+
/**
|
|
1797
|
+
* @inheritDoc
|
|
1798
|
+
*/ static get requires() {
|
|
1799
|
+
return [
|
|
1800
|
+
MediaEmbedStyleEditing
|
|
1801
|
+
];
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* @inheritDoc
|
|
1805
|
+
*/ static get pluginName() {
|
|
1806
|
+
return 'MediaEmbedStyleUI';
|
|
1807
|
+
}
|
|
1808
|
+
/**
|
|
1809
|
+
* @inheritDoc
|
|
1810
|
+
*/ static get isOfficialPlugin() {
|
|
1811
|
+
return true;
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* @inheritDoc
|
|
1815
|
+
*/ init() {
|
|
1816
|
+
const titles = this._getLocalizedTitles();
|
|
1817
|
+
for (const button of this._getButtonDefinitions(titles)){
|
|
1818
|
+
this._createButton(button);
|
|
1819
|
+
}
|
|
1820
|
+
for (const dropdown of this._getDropdownDefinitions(titles)){
|
|
1821
|
+
this._createDropdown(dropdown);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Returns the alignment button definitions sourced from the resolved options list.
|
|
1826
|
+
*/ _getButtonDefinitions(titles) {
|
|
1827
|
+
const editing = this.editor.plugins.get(MediaEmbedStyleEditing);
|
|
1828
|
+
return editing.normalizedStyles.map((option)=>({
|
|
1829
|
+
name: option.name,
|
|
1830
|
+
label: titles[option.title] || option.title,
|
|
1831
|
+
icon: option.icon
|
|
1832
|
+
}));
|
|
1833
|
+
}
|
|
1834
|
+
/**
|
|
1835
|
+
* Returns the localized titles of the built-in styles and dropdowns.
|
|
1836
|
+
*/ _getLocalizedTitles() {
|
|
1837
|
+
const t = this.editor.t;
|
|
1838
|
+
return {
|
|
1839
|
+
'Left aligned media': t('Left aligned media'),
|
|
1840
|
+
'Centered media': t('Centered media'),
|
|
1841
|
+
'Right aligned media': t('Right aligned media'),
|
|
1842
|
+
'Wrap text': t('Wrap text'),
|
|
1843
|
+
'Break text': t('Break text')
|
|
1844
|
+
};
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Returns the split-button dropdown definitions, filtered to the styles present in the
|
|
1848
|
+
* resolved options list. Combines the {@link module:media-embed/mediaembedstyle/constants~DEFAULT_DROPDOWN_DEFINITIONS
|
|
1849
|
+
* built-in dropdowns} with custom dropdowns declared inline in
|
|
1850
|
+
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig#toolbar `config.mediaEmbed.toolbar`}.
|
|
1851
|
+
*
|
|
1852
|
+
* A dropdown with fewer than two items is skipped — a single-item dropdown carries no value
|
|
1853
|
+
* over the flat button. If the configured `defaultItem` was filtered out, the first surviving
|
|
1854
|
+
* item becomes the default.
|
|
1855
|
+
*
|
|
1856
|
+
* When a *custom* dropdown's items reference styles that are not in the resolved options list,
|
|
1857
|
+
* a console warning is emitted (the integrator's config was not fully honored). Built-in
|
|
1858
|
+
* dropdowns auto-skip silently — they are added by the plugin, not the integrator.
|
|
1859
|
+
*/ _getDropdownDefinitions(titles) {
|
|
1860
|
+
const editor = this.editor;
|
|
1861
|
+
const editing = editor.plugins.get(MediaEmbedStyleEditing);
|
|
1862
|
+
const availableComponentNames = new Set(editing.normalizedStyles.map(({ name })=>`mediaEmbed:${name}`));
|
|
1863
|
+
const dropdowns = [];
|
|
1864
|
+
const resolveDropdown = (definition, warnOnFilter)=>{
|
|
1865
|
+
const items = definition.items.filter((itemName)=>availableComponentNames.has(itemName));
|
|
1866
|
+
if (warnOnFilter && items.length !== definition.items.length) {
|
|
1867
|
+
warnInvalidDropdown({
|
|
1868
|
+
dropdown: definition
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
if (items.length < 2) {
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1874
|
+
const defaultItem = availableComponentNames.has(definition.defaultItem) ? definition.defaultItem : items[0];
|
|
1875
|
+
dropdowns.push({
|
|
1876
|
+
name: definition.name,
|
|
1877
|
+
title: titles[definition.title] || definition.title,
|
|
1878
|
+
items,
|
|
1879
|
+
defaultItem
|
|
1880
|
+
});
|
|
1881
|
+
};
|
|
1882
|
+
for (const definition of DEFAULT_DROPDOWN_DEFINITIONS){
|
|
1883
|
+
resolveDropdown(definition, false);
|
|
1884
|
+
}
|
|
1885
|
+
for (const definition of this._collectCustomDropdowns()){
|
|
1886
|
+
resolveDropdown(definition, true);
|
|
1887
|
+
}
|
|
1888
|
+
return dropdowns;
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Scans `config.mediaEmbed.toolbar` for entries shaped like a dropdown definition
|
|
1892
|
+
* (objects with both `items` and `defaultItem`) and returns the valid ones. `defaultItem`
|
|
1893
|
+
* is the discriminator between our split-button dropdowns and generic toolbar groupings
|
|
1894
|
+
* (which use `items` + `label` and have no `defaultItem`).
|
|
1895
|
+
*
|
|
1896
|
+
* Invalid entries (wrong name prefix, `defaultItem` missing from `items`) are warned and
|
|
1897
|
+
* dropped here. Items that reference filtered-out styles are filtered later by
|
|
1898
|
+
* {@link #_getDropdownDefinitions}, alongside the same logic that applies to built-in
|
|
1899
|
+
* dropdowns.
|
|
1900
|
+
*/ _collectCustomDropdowns() {
|
|
1901
|
+
const toolbarConfig = this.editor.config.get('mediaEmbed.toolbar') || [];
|
|
1902
|
+
return toolbarConfig.filter((item)=>isMediaStyleDropdown(item) && isValidCustomDropdown(item));
|
|
1903
|
+
}
|
|
1904
|
+
/**
|
|
1905
|
+
* Registers a single alignment toggle button in the component factory.
|
|
1906
|
+
*/ _createButton(definition) {
|
|
1907
|
+
const editor = this.editor;
|
|
1908
|
+
const componentName = `mediaEmbed:${definition.name}`;
|
|
1909
|
+
editor.ui.componentFactory.add(componentName, (locale)=>{
|
|
1910
|
+
const command = editor.commands.get('mediaStyle');
|
|
1911
|
+
const view = new ButtonView(locale);
|
|
1912
|
+
view.set({
|
|
1913
|
+
label: definition.label,
|
|
1914
|
+
icon: definition.icon,
|
|
1915
|
+
tooltip: true,
|
|
1916
|
+
isToggleable: true
|
|
1917
|
+
});
|
|
1918
|
+
view.bind('isEnabled').to(command, 'isEnabled');
|
|
1919
|
+
view.bind('isOn').to(command, 'value', (value)=>value === definition.name);
|
|
1920
|
+
view.on('execute', ()=>{
|
|
1921
|
+
editor.execute('mediaStyle', {
|
|
1922
|
+
value: definition.name
|
|
1923
|
+
});
|
|
1924
|
+
editor.editing.view.focus();
|
|
1925
|
+
});
|
|
1926
|
+
return view;
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Registers a split-button dropdown grouping a set of alignment buttons. The action button
|
|
1931
|
+
* reflects whichever child option is currently `isOn`, falling back to the dropdown's
|
|
1932
|
+
* `defaultItem` when nothing is active.
|
|
1933
|
+
*/ _createDropdown(definition) {
|
|
1934
|
+
const editor = this.editor;
|
|
1935
|
+
const factory = editor.ui.componentFactory;
|
|
1936
|
+
factory.add(definition.name, (locale)=>{
|
|
1937
|
+
// Build child buttons via the factory; pick the default by name.
|
|
1938
|
+
const buttonViews = definition.items.map((itemName)=>factory.create(itemName));
|
|
1939
|
+
const defaultButton = buttonViews[definition.items.indexOf(definition.defaultItem)];
|
|
1940
|
+
// Resolve the currently-active child or fall back to the default. Used by the
|
|
1941
|
+
// reactive `icon` and `label` bindings on the split button.
|
|
1942
|
+
const activeOrDefault = (...areOn)=>{
|
|
1943
|
+
const index = areOn.findIndex(Boolean);
|
|
1944
|
+
return index < 0 ? defaultButton : buttonViews[index];
|
|
1945
|
+
};
|
|
1946
|
+
// Build the dropdown shell.
|
|
1947
|
+
const dropdownView = createDropdown(locale, SplitButtonView);
|
|
1948
|
+
const splitButtonView = dropdownView.buttonView;
|
|
1949
|
+
addToolbarToDropdown(dropdownView, buttonViews, {
|
|
1950
|
+
enableActiveItemFocusOnDropdownOpen: true
|
|
1951
|
+
});
|
|
1952
|
+
splitButtonView.set({
|
|
1953
|
+
label: getDropdownButtonTitle(definition.title, defaultButton.label),
|
|
1954
|
+
class: null,
|
|
1955
|
+
tooltip: true
|
|
1956
|
+
});
|
|
1957
|
+
splitButtonView.arrowView.unbind('label');
|
|
1958
|
+
splitButtonView.arrowView.set({
|
|
1959
|
+
label: definition.title
|
|
1960
|
+
});
|
|
1961
|
+
// Reactive: action button mirrors the active child's icon and label.
|
|
1962
|
+
splitButtonView.bind('icon').toMany(buttonViews, 'isOn', (...areOn)=>activeOrDefault(...areOn).icon);
|
|
1963
|
+
splitButtonView.bind('label').toMany(buttonViews, 'isOn', (...areOn)=>getDropdownButtonTitle(definition.title, activeOrDefault(...areOn).label));
|
|
1964
|
+
// Reactive: split button shows the "active" state when any child is on.
|
|
1965
|
+
splitButtonView.bind('isOn').toMany(buttonViews, 'isOn', (...areOn)=>areOn.some(Boolean));
|
|
1966
|
+
splitButtonView.bind('class').toMany(buttonViews, 'isOn', (...areOn)=>areOn.some(Boolean) ? 'ck-splitbutton_flatten' : undefined);
|
|
1967
|
+
// Action click: re-fire the default child when nothing is active; otherwise toggle the dropdown.
|
|
1968
|
+
this.listenTo(splitButtonView, 'execute', ()=>{
|
|
1969
|
+
if (buttonViews.some(({ isOn })=>isOn)) {
|
|
1970
|
+
dropdownView.isOpen = !dropdownView.isOpen;
|
|
1971
|
+
} else {
|
|
1972
|
+
defaultButton.fire('execute');
|
|
1973
|
+
}
|
|
1974
|
+
});
|
|
1975
|
+
// Reactive: dropdown is enabled when any child is enabled.
|
|
1976
|
+
dropdownView.bind('isEnabled').toMany(buttonViews, 'isEnabled', (...areEnabled)=>areEnabled.some(Boolean));
|
|
1977
|
+
// Refocus the editing view so the dropdown action doesn't steal caret position.
|
|
1978
|
+
this.listenTo(dropdownView, 'execute', ()=>{
|
|
1979
|
+
editor.editing.view.focus();
|
|
1980
|
+
});
|
|
1981
|
+
return dropdownView;
|
|
1982
|
+
});
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
/**
|
|
1986
|
+
* Combines the dropdown title and the default action item label for the split-button label.
|
|
1987
|
+
*/ function getDropdownButtonTitle(dropdownTitle, buttonTitle) {
|
|
1988
|
+
return `${dropdownTitle}: ${buttonTitle}`;
|
|
1989
|
+
}
|
|
1990
|
+
/**
|
|
1991
|
+
* Validates a user-supplied dropdown definition. Emits a console warning under
|
|
1992
|
+
* `media-style-configuration-definition-invalid` and returns `false` when any of these rules is
|
|
1993
|
+
* broken: `name` must start with `mediaEmbed:`, `title` must be a non-empty string, `items` must
|
|
1994
|
+
* be non-empty and every entry must be a `mediaEmbed:`-prefixed string, and `defaultItem` must be
|
|
1995
|
+
* one of the `items`.
|
|
1996
|
+
*
|
|
1997
|
+
* Item-membership against the resolved styles is checked separately, downstream, alongside the
|
|
1998
|
+
* same logic that applies to built-in dropdowns.
|
|
1999
|
+
*/ function isValidCustomDropdown(definition) {
|
|
2000
|
+
const valid = definition.name.startsWith('mediaEmbed:') && typeof definition.title === 'string' && definition.title.length > 0 && definition.items.length > 0 && definition.items.every((name)=>typeof name === 'string' && name.startsWith('mediaEmbed:')) && definition.items.includes(definition.defaultItem);
|
|
2001
|
+
if (!valid) {
|
|
2002
|
+
warnInvalidDropdown({
|
|
2003
|
+
dropdown: definition
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
return valid;
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Emits a console warning under `media-style-configuration-definition-invalid` for an invalid
|
|
2010
|
+
* or partially-honored dropdown definition. Called from {@link ~isValidCustomDropdown} for
|
|
2011
|
+
* structural problems and from {@link MediaEmbedStyleUI#_getDropdownDefinitions} when items
|
|
2012
|
+
* reference styles that are not in the resolved options list.
|
|
2013
|
+
*/ function warnInvalidDropdown(info) {
|
|
2014
|
+
logWarning('media-style-configuration-definition-invalid', info);
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
/**
|
|
2018
|
+
* The media embed style plugin.
|
|
2019
|
+
*
|
|
2020
|
+
* This is a "glue" plugin which loads the following plugins:
|
|
2021
|
+
* * {@link module:media-embed/mediaembedstyle/mediaembedstyleediting~MediaEmbedStyleEditing},
|
|
2022
|
+
* * {@link module:media-embed/mediaembedstyle/mediaembedstyleui~MediaEmbedStyleUI}
|
|
2023
|
+
*
|
|
2024
|
+
* For a detailed overview, check the {@glink features/media-embed/media-embed-styles Media embed styles feature documentation}.
|
|
2025
|
+
*/ class MediaEmbedStyle extends Plugin {
|
|
2026
|
+
/**
|
|
2027
|
+
* @inheritDoc
|
|
2028
|
+
*/ static get requires() {
|
|
2029
|
+
return [
|
|
2030
|
+
MediaEmbedStyleEditing,
|
|
2031
|
+
MediaEmbedStyleUI
|
|
2032
|
+
];
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* @inheritDoc
|
|
2036
|
+
*/ static get pluginName() {
|
|
2037
|
+
return 'MediaEmbedStyle';
|
|
2038
|
+
}
|
|
2039
|
+
/**
|
|
2040
|
+
* @inheritDoc
|
|
2041
|
+
*/ static get isOfficialPlugin() {
|
|
2042
|
+
return true;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
1136
2045
|
|
|
1137
|
-
export { AutoMediaEmbed, MediaEmbed, MediaEmbedCommand, MediaEmbedEditing, MediaEmbedToolbar, MediaEmbedUI, MediaRegistry, MediaFormView as _MediaFormView, createMediaFigureElement as _createMediaFigureElement, getSelectedMediaModelWidget as _getSelectedMediaModelWidget, getSelectedMediaViewWidget as _getSelectedMediaViewWidget, insertMedia as _insertMedia, isMediaWidget as _isMediaWidget, modelToViewUrlAttributeConverter as _modelToViewUrlAttributeMediaConverter, toMediaWidget as _toMediaWidget };
|
|
2046
|
+
export { AutoMediaEmbed, MediaEmbed, MediaEmbedCommand, MediaEmbedEditing, MediaEmbedResize, MediaEmbedResizeEditing, MediaEmbedResizeHandles, MediaEmbedStyle, MediaEmbedStyleCommand, MediaEmbedStyleEditing, MediaEmbedStyleUI, MediaEmbedToolbar, MediaEmbedUI, MediaRegistry, ResizeMediaEmbedCommand, MediaFormView as _MediaFormView, createMediaFigureElement as _createMediaFigureElement, getSelectedMediaModelWidget as _getSelectedMediaModelWidget, getSelectedMediaViewWidget as _getSelectedMediaViewWidget, insertMedia as _insertMedia, isMediaWidget as _isMediaWidget, modelToViewUrlAttributeConverter as _modelToViewUrlAttributeMediaConverter, toMediaWidget as _toMediaWidget };
|
|
1138
2047
|
//# sourceMappingURL=index.js.map
|