2 * Provides the media manager dialog for selecting media for Redactor editors.
4 * @author Matthias Schmidt
5 * @copyright 2001-2021 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Media/Manager/Editor
10 import MediaManager from "./Base";
11 import * as Core from "../../Core";
12 import { Media, MediaInsertType, MediaManagerEditorOptions, MediaUploadSuccessEventData } from "../Data";
13 import * as EventHandler from "../../Event/Handler";
14 import * as DomTraverse from "../../Dom/Traverse";
15 import * as Language from "../../Language";
16 import * as UiDialog from "../../Ui/Dialog";
17 import * as Clipboard from "../../Controller/Clipboard";
18 import { OnDropPayload } from "../../Ui/Redactor/DragAndDrop";
19 import DomUtil from "../../Dom/Util";
21 interface PasteFromClipboard {
25 class MediaManagerEditor extends MediaManager<MediaManagerEditorOptions> {
26 protected _activeButton;
27 protected readonly _buttons: HTMLCollectionOf<HTMLElement>;
28 protected _mediaToInsert: Map<number, Media>;
29 protected _mediaToInsertByClipboard: boolean;
30 protected _uploadData: OnDropPayload | PasteFromClipboard | null;
31 protected _uploadId: number | null;
33 constructor(options: Partial<MediaManagerEditorOptions>) {
34 options = Core.extend(
43 this._forceClipboard = true;
44 this._activeButton = null;
45 const context = this._options.editor ? this._options.editor.core.toolbar()[0] : undefined;
46 this._buttons = (context || window.document).getElementsByClassName(
47 this._options.buttonClass || "jsMediaEditorButton",
48 ) as HTMLCollectionOf<HTMLElement>;
49 Array.from(this._buttons).forEach((button) => {
50 button.addEventListener("click", (ev) => this._click(ev));
52 this._mediaToInsert = new Map<number, Media>();
53 this._mediaToInsertByClipboard = false;
54 this._uploadData = null;
55 this._uploadId = null;
57 if (this._options.editor && !this._options.editor.opts.woltlab.attachments) {
58 const editorId = this._options.editor.$editor[0].dataset.elementId as string;
60 const uuid1 = EventHandler.add("com.woltlab.wcf.redactor2", `dragAndDrop_${editorId}`, (data: OnDropPayload) =>
61 this._editorUpload(data),
63 const uuid2 = EventHandler.add(
64 "com.woltlab.wcf.redactor2",
65 `pasteFromClipboard_${editorId}`,
66 (data: OnDropPayload) => this._editorUpload(data),
69 EventHandler.add("com.woltlab.wcf.redactor2", `destroy_${editorId}`, () => {
70 EventHandler.remove("com.woltlab.wcf.redactor2", `dragAndDrop_${editorId}`, uuid1);
71 EventHandler.remove("com.woltlab.wcf.redactor2", `dragAndDrop_${editorId}`, uuid2);
74 EventHandler.add("com.woltlab.wcf.media.upload", "success", (data) => this._mediaUploaded(data));
78 protected _addButtonEventListeners(): void {
79 super._addButtonEventListeners();
81 if (!this._mediaManagerMediaList) {
85 DomTraverse.childrenByTag(this._mediaManagerMediaList, "LI").forEach((listItem) => {
86 const insertIcon = listItem.querySelector(".jsMediaInsertButton");
88 insertIcon.classList.remove("jsMediaInsertButton");
89 insertIcon.addEventListener("click", (ev) => this._openInsertDialog(ev));
95 * Builds the dialog to setup inserting media files.
97 protected _buildInsertDialog(): void {
98 let thumbnailOptions = "";
100 this._getThumbnailSizes().forEach((thumbnailSize) => {
105 Language.get("wcf.media.insert.imageSize." + thumbnailSize) +
108 thumbnailOptions += '<option value="original">' + Language.get("wcf.media.insert.imageSize.original") + "</option>";
111 <div class="section">
112 <dl class="thumbnailSizeSelection">
113 <dt>${Language.get("wcf.media.insert.imageSize")}</dt>
115 <select name="thumbnailSize">
121 <div class="formSubmit">
122 <button class="buttonPrimary">${Language.get("wcf.global.button.insert")}</button>
126 _dialogSetup: () => {
128 id: this._getInsertDialogId(),
130 onClose: () => this._editorClose(),
131 onSetup: (content) => {
132 content.querySelector(".buttonPrimary")!.addEventListener("click", (ev) => this._insertMedia(ev));
134 DomUtil.show(content.querySelector(".thumbnailSizeSelection") as HTMLElement);
136 title: Language.get("wcf.media.insert"),
144 protected _click(event: Event): void {
145 this._activeButton = event.currentTarget;
150 protected _dialogShow(): void {
153 // check if data needs to be uploaded
154 if (this._uploadData) {
155 const fileUploadData = this._uploadData as OnDropPayload;
156 if (fileUploadData.file) {
157 this._upload.uploadFile(fileUploadData.file);
159 const blobUploadData = this._uploadData as PasteFromClipboard;
160 this._uploadId = this._upload.uploadBlob(blobUploadData.blob);
163 this._uploadData = null;
168 * Handles pasting and dragging and dropping files into the editor.
170 protected _editorUpload(data: OnDropPayload): void {
171 this._uploadData = data;
177 * Returns the id of the insert dialog based on the media files to be inserted.
179 protected _getInsertDialogId(): string {
180 return ["mediaInsert", ...this._mediaToInsert.keys()].join("-");
184 * Returns the supported thumbnail sizes (excluding `original`) for all media images to be inserted.
186 protected _getThumbnailSizes(): string[] {
187 return ["small", "medium", "large"]
189 const sizeSupported = Array.from(this._mediaToInsert.values()).every((media) => {
190 return media[size + "ThumbnailType"] !== null;
199 .filter((s) => s !== null) as string[];
203 * Inserts media files into the editor.
205 protected _insertMedia(event?: Event | null, thumbnailSize?: string, closeEditor = false): void {
206 if (closeEditor === undefined) closeEditor = true;
208 // update insert options with selected values if method is called by clicking on 'insert' button
211 UiDialog.close(this._getInsertDialogId());
213 const dialogContent = (event.currentTarget as HTMLElement).closest(".dialogContent")!;
214 const thumbnailSizeSelect = dialogContent.querySelector("select[name=thumbnailSize]") as HTMLSelectElement;
215 thumbnailSize = thumbnailSizeSelect.value;
218 if (this._options.callbackInsert !== null) {
219 this._options.callbackInsert(this._mediaToInsert, MediaInsertType.Separate, thumbnailSize);
221 this._options.editor!.buffer.set();
223 this._mediaToInsert.forEach((media) => this._insertMediaItem(thumbnailSize, media));
226 if (this._mediaToInsertByClipboard) {
227 Clipboard.unmark("com.woltlab.wcf.media", Array.from(this._mediaToInsert.keys()));
230 this._mediaToInsert = new Map<number, Media>();
231 this._mediaToInsertByClipboard = false;
233 // close manager dialog
235 UiDialog.close(this);
240 * Inserts a single media item into the editor.
242 protected _insertMediaItem(thumbnailSize: string | undefined, media: Media): void {
245 ["small", "medium", "large", "original"].some((size) => {
246 if (media[size + "ThumbnailHeight"] != 0) {
249 if (thumbnailSize == size) {
257 thumbnailSize = available;
259 if (!thumbnailSize) {
260 thumbnailSize = "original";
263 let link = media.link;
264 if (thumbnailSize !== "original") {
265 link = media[thumbnailSize + "ThumbnailLink"];
268 this._options.editor!.insert.html(
269 `<img src="${link}" class="woltlabSuiteMedia" data-media-id="${media.mediaID}" data-media-size="${thumbnailSize}">`,
272 this._options.editor!.insert.text(`[wsm='${media.mediaID}'][/wsm]`);
277 * Is called after media files are successfully uploaded to insert copied media.
279 protected _mediaUploaded(data: MediaUploadSuccessEventData): void {
280 if (this._uploadId !== null && this._upload === data.upload) {
282 this._uploadId === data.uploadId ||
283 (Array.isArray(this._uploadId) && this._uploadId.indexOf(data.uploadId) !== -1)
285 this._mediaToInsert = new Map<number, Media>(data.media.entries());
286 this._insertMedia(null, "medium", false);
288 this._uploadId = null;
294 * Handles clicking on the insert button.
296 protected _openInsertDialog(event: Event): void {
297 const target = event.currentTarget as HTMLElement;
299 this.insertMedia([~~target.dataset.objectId!]);
303 * Is called to insert the media files with the given ids into an editor.
305 public clipboardInsertMedia(mediaIds: number[]): void {
306 this.insertMedia(mediaIds, true);
310 * Prepares insertion of the media files with the given ids.
312 public insertMedia(mediaIds: number[], insertedByClipboard?: boolean): void {
313 this._mediaToInsert = new Map<number, Media>();
314 this._mediaToInsertByClipboard = insertedByClipboard || false;
316 // open the insert dialog if all media files are images
317 let imagesOnly = true;
318 mediaIds.forEach((mediaId) => {
319 const media = this._media.get(mediaId)!;
320 this._mediaToInsert.set(media.mediaID, media);
322 if (!media.isImage) {
328 const thumbnailSizes = this._getThumbnailSizes();
329 if (thumbnailSizes.length) {
330 UiDialog.close(this);
331 const dialogId = this._getInsertDialogId();
332 if (UiDialog.getDialog(dialogId)) {
333 UiDialog.openStatic(dialogId, null);
335 this._buildInsertDialog();
338 this._insertMedia(undefined, "original");
345 public getMode(): string {
349 public setupMediaElement(media: Media, mediaElement: HTMLElement): void {
350 super.setupMediaElement(media, mediaElement);
352 // add media insertion icon
353 const buttons = mediaElement.querySelector("nav.buttonGroupNavigation > ul")!;
355 const listItem = document.createElement("li");
356 listItem.className = "jsMediaInsertButton";
357 listItem.dataset.objectId = media.mediaID.toString();
358 buttons.appendChild(listItem);
360 listItem.innerHTML = `
362 <span class="icon icon16 fa-plus jsTooltip" title="${Language.get("wcf.global.button.insert")}"></span>
363 <span class="invisible">${Language.get("wcf.global.button.insert")}</span>
368 Core.enableLegacyInheritance(MediaManagerEditor);
370 export = MediaManagerEditor;