qbank_dam-8.x-1.4/js/ckeditor5_plugins/qbank/src/insertqbankmediacommand.js
js/ckeditor5_plugins/qbank/src/insertqbankmediacommand.js
import { Command } from 'ckeditor5/src/core';
export default class InsertQBankMediaCommand extends Command {
execute() {
const { model } = this.editor;
//Callback
var cbAddImageToEditor = function (qbankMedia) {
const { selection } = model.document;
model.change((writer) => {
model.insertContent(
createQBankMedia(
writer,
{
src: qbankMedia.attributes.src,
type: qbankMedia.attributes.type,
mimetype: qbankMedia.attributes.mimetype,
filename: qbankMedia.attributes.filename,
thumbnail: qbankMedia.attributes.thumbnail,
"data-entity-uuid": qbankMedia.attributes["data-entity-uuid"],
"data-entity-type": qbankMedia.attributes["data-entity-type"]
}
),
selection.getFirstPosition()
);
});
}
// Open QBank Image selection using connector
Drupal.ckeditor5.openDialog(
Drupal.url('qbank_dam/dialog/qbank'),
cbAddImageToEditor,
{ width: '100%' }
);
//Method to test the CKeditor5 link (DO NOT DELETE)
// const { selection } = model.document;
// model.change((writer) => {
// model.insertContent(
// createQBankMedia(
// writer,
// {
// src: 'http://google.com',
// type: 'document',
// mimetype: 'application/pdf',
// filename: 'test file.pdf',
// thumbnail: ''
// }
// ),
// selection.getFirstPosition()
// );
// });
}
refresh() {
const { model } = this.editor;
const { selection } = model.document;
// Determine if the cursor (selection) is in a position where adding a image is permitted
const allowedIn = model.schema.findAllowedParent(
selection.getFirstPosition(),
'qbankMedia',
);
// set null if not allowed
this.isEnabled = allowedIn !== null;
}
}
function createQBankMedia(writer, attributes) {
//Create Element
try {
const type = attributes.type;
switch (type) {
case 'video':
const qbankVideo = writer.createElement('qbankVideo', {
controls: true,
width: '100%',
height: '100%',
"data-entity-uuid": attributes["data-entity-uuid"],
"data-entity-type": attributes["data-entity-type"]
});
const qbankVideoSource = writer.createElement('qbankSource', {
src: attributes.src ?? '',
type: attributes.mimetype ?? 'video/mp4'
});
writer.append(qbankVideoSource, qbankVideo);
return qbankVideo;
case 'audio':
const qbankAudio = writer.createElement('qbankAudio', {
controls: true,
"data-entity-uuid": attributes["data-entity-uuid"],
"data-entity-type": attributes["data-entity-type"]
});
const qbankSource = writer.createElement('qbankSource', {
src: attributes.src ?? '',
type: attributes.mimetype ?? 'video/mp4'
});
writer.append(qbankSource, qbankAudio);
return qbankAudio;
case 'image':
const qbankImage = writer.createElement(
'qbankMedia',
{
src: attributes.src ?? '',
"data-entity-uuid": attributes["data-entity-uuid"],
"data-entity-type": attributes["data-entity-type"]
}
)
return qbankImage;
default:
const paragraph = writer.createElement('paragraph');
const link = writer.createElement('qbankLink', {
href: attributes.src ?? '',
target: '_blank',
"data-entity-uuid": attributes["data-entity-uuid"],
"data-entity-type": attributes["data-entity-type"]
});
const text = writer.createText(attributes.filename)
writer.append(text, link)
writer.append(link, paragraph)
return paragraph
}
} catch (exception) {
console.log('QBank ::', exception);
}
}
