Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Image gallery insert for post/product description tinymce editor

givanz799d91981102
diff --git a/public/js/libs/tinymce-options.js b/public/js/libs/tinymce-options.js
index c8de593..dda8312 100755
--- a/public/js/libs/tinymce-options.js
+++ b/public/js/libs/tinymce-options.js
@@ -129,6 +129,309 @@ let tinyMceOptions = {
if (has_changes) {
has_changes.value = "0";
}
+
+ let gallery;
+ editor.on( 'click keyup', function() {
+ let node = editor.selection.getNode();
+
+ if (gallery) {
+ gallery.removeAttribute('data-mce-selected');
+ }
+ gallery = node.closest('[data-component-gallery]');
+ if ( gallery ) {
+ gallery.setAttribute( 'data-mce-selected', '2' );
+ }
+ } );
+
+ editor.on( 'dblclick ', function() {
+ let node = editor.selection.getNode();
+ if (node.tagName == "IMG" || node.hasAttribute("data-mce-object")) {
+ let obj = node.getAttribute("data-mce-object");
+ if (obj == "video" || obj == "audio") {
+ node = node.querySelector("video, audio");
+ }
+ if (!Vvveb.MediaModal) {
+ Vvveb.MediaModal = new MediaModal(true);
+ Vvveb.MediaModal.mediaPath = mediaPath;
+ }
+ Vvveb.MediaModal.type = "single";
+ Vvveb.MediaModal.open(null, function (file) {
+ node.setAttribute("src", file);
+ /*
+ editor.insertContent(editor.dom.createHTML('img', {
+ src: file,
+ "class": "align-center"
+ }));*/
+ })
+ }
+ } );
+
+ editor.ui.registry.addButton('gallery', {
+ tooltip: 'Insert gallery',
+ icon: 'gallery',
+ onAction: function () {
+ let galleryHTML = `<div class="gallery masonry has-shadow" data-component-gallery="">
+ <div class="item"><a href="#"><img src="../../media/posts/1.jpg"> </a></div>
+ </div>`;
+
+ editor.insertContent(galleryHTML);
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ gallery.setAttribute( 'data-mce-selected', '2' );
+ let img = gallery.querySelector("img");
+ editor.selection.select(img);
+
+
+ if (!Vvveb.MediaModal) {
+ Vvveb.MediaModal = new MediaModal(true);
+ Vvveb.MediaModal.mediaPath = mediaPath;
+ }
+ Vvveb.MediaModal.type = "multitple";
+ Vvveb.MediaModal.open(null, function (src, file, files) {
+ if (src) {
+ let imgs = "";
+ for (img of src.split(",")) if (img) {
+ imgs +=
+ '<div class="item"><a href="#">' + editor.dom.createHTML('img', {
+ src: img,
+ "class": "align-center"
+ }) + "</a></div>";
+ //gallery.append(generateElements(image)[0]);
+ //editor.insertContent(image);
+ }
+ editor.insertContent(imgs);
+ }
+ });
+ }});
+
+
+ editor.ui.registry.addButton( 'removegallery', {
+ tooltip: 'Remove gallery',
+ icon: 'remove',
+ onAction: function() {
+ //editor.fire( 'cut' );
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ editor.selection.setNode(gallery.prevElementSibling);
+ gallery.remove();
+ }
+ } );
+
+ editor.ui.registry.addButton( 'addimage', {
+ tooltip: 'Add',
+ icon: 'plus',
+ onAction: function() {
+ let galleryItemHTML = `<div class="item"><a href="#"><img src="../../media/posts/1.jpg"></a></div>`;
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ editor.selection.setCursorLocation(gallery, gallery.children.length);
+
+
+ if (!Vvveb.MediaModal) {
+ Vvveb.MediaModal = new MediaModal(true);
+ Vvveb.MediaModal.mediaPath = mediaPath;
+ }
+ Vvveb.MediaModal.type = "multiple";
+ Vvveb.MediaModal.open(null, function (src, file, files) {
+ if (src) {
+ let imgs = "";
+ for (img of src.split(",")) if (img) {
+ imgs +=
+ '<div class="item"><a href="#">' + editor.dom.createHTML('img', {
+ src: img,
+ "class": "align-center"
+ }) + "</a></div>";
+ //gallery.append(generateElements(image)[0]);
+ //editor.insertContent(image);
+ }
+ editor.insertContent(imgs);
+ }
+ });
+
+ //gallery.append(generateElements(galleryItemHTML)[0]);
+ //editor.insertContent(galleryItemHTML);
+ }
+ } );
+
+ editor.ui.registry.addButton( 'editimage', {
+ tooltip: 'Edit',
+ icon: 'edit-block',
+ onAction: function() {
+ if (!Vvveb.MediaModal) {
+ Vvveb.MediaModal = new MediaModal(true);
+ Vvveb.MediaModal.mediaPath = mediaPath;
+ }
+ Vvveb.MediaModal.type = "single";
+ Vvveb.MediaModal.open(null, function (file) {
+ let node = editor.selection.getNode();
+ node.setAttribute("src", file);
+ /*
+ editor.insertContent(editor.dom.createHTML('img', {
+ src: file,
+ "class": "align-center"
+ }));*/
+ })
+ }
+ } );
+
+ editor.ui.registry.addButton( 'upimage', {
+ tooltip: 'Up',
+ icon: 'chevron-up',
+ onAction: function() {
+ let img = editor.selection.getNode();
+ let node = img.closest('div.item') || img;
+
+ oldParent = node.parentNode;
+ oldNextSibling = node.nextSibling;
+
+ next = node.previousElementSibling;
+
+ if (next) {
+ next.before(node);
+ } else {
+ //node.parentNode.before(node);
+ }
+ editor.selection.select(img);
+ }
+ } );
+
+
+ editor.ui.registry.addButton( 'downimage', {
+ tooltip: 'Down',
+ icon: 'chevron-down',
+ onAction: function() {
+ let img = editor.selection.getNode();
+ let node = img.closest('div.item') || img;
+
+ oldParent = node.parentNode;
+ oldNextSibling = node.nextSibling;
+
+ next = node.nextElementSibling;
+
+ if (next) {
+ next.after(node);
+ } else {
+ //node.parentNode.after(node);
+ }
+ editor.selection.select(img);
+ }
+ } );
+
+ editor.ui.registry.addButton('alignment', {
+ type: 'listbox',
+ text: 'Alignment',
+ icon: '',
+ onAction: function(e) {
+ tinyMCE.execCommand(this.value());
+ },
+ values: [
+ {icon: 'alignleft', value: 'JustifyLeft'},
+ {icon: 'alignright', value: 'JustifyRight'},
+ {icon: 'aligncenter', value: 'JustifyCenter'},
+ {icon: 'alignjustify', value: 'JustifyFull'},
+ ],
+ onPostRender: function() {
+ // Select the firts item by default
+ this.value('JustifyLeft');
+ }
+ });
+
+ let masonryState = true;
+ let shadowState = true;
+ editor.ui.registry.addMenuButton('gallery-options', {
+ text: '',
+ tooltip: 'Gallery options',
+ icon: 'gallery',
+ fetch: (callback) => {
+ const items = [
+ {
+ type: 'togglemenuitem',
+ active: true,
+ icon: 'table-cell-properties',
+ text: 'masonry',
+ onAction: () => {
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ gallery.classList.toggle("masonry");
+ masonryState = gallery.classList.contains("masonry");
+ },
+ onSetup: (api) => {
+ api.setActive(masonryState);
+ return () => {};
+ }
+ },
+ {
+ type: 'togglemenuitem',
+ active: true,
+ text: 'Shadow',
+ icon: 'select-all',
+ onAction: () => {
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ gallery.classList.toggle("has-shadow");
+ shadowState = gallery.classList.contains("has-shadow");
+ },
+ onSetup: (api) => {
+ api.setActive(shadowState);
+ return () => {};
+ }
+ }
+ ];
+ callback(items);
+ }
+ });
+
+
+ let columnsItems = [];
+ let activeColumn = 4;
+ for (let i = 1;i <= 12;i++) {
+ columnsItems.push({
+ type: 'togglemenuitem',
+ value: i,
+ text: i + ' Column' + (i > 1 ? 's':''),
+ onAction: () => {
+ let node = editor.selection.getNode();
+ let gallery = node.closest('[data-component-gallery]');
+ gallery.style.columnCount = i;
+ activeColumn = i;
+ },
+ onSetup: (api) => {
+ if (api.setActive) {
+ api.setActive(activeColumn == i);
+ }
+ return () => {};
+ }
+ });
+ }
+ editor.ui.registry.addMenuButton('columns', {
+ text: '',
+ tooltip: 'Columns',
+ icon: 'duplicate-column',
+ fetch: (callback) => {
+ callback(columnsItems);
+ }
+ });
+
+ editor.ui.registry.addButton('alignmentv2', {
+ type: 'menubutton',
+ text: 'Alignment v2',
+ icon: '',
+ onAction: function(e) {
+ tinyMCE.execCommand(this.value());
+ },
+ menu: [
+ {icon: 'alignleft', onAction: function() { tinyMCE.execCommand('JustifyLeft'); }},
+ {icon: 'alignright', onAction: function() { tinyMCE.execCommand('JustifyRight'); }}
+ ]
+ });
+
+ editor.ui.registry.addContextToolbar('imagealignment', {
+ predicate: (node) => node.hasAttribute("data-component-gallery"),
+ items: 'addimage gallery-options columns layout shadow removegallery',
+ position: 'node',
+ scope: 'node'
+ });
+
editor.ui.registry.addButton('quickmedia', {
icon: 'image',
@@ -138,18 +441,25 @@ let tinyMceOptions = {
Vvveb.MediaModal = new MediaModal(true);
Vvveb.MediaModal.mediaPath = mediaPath;
}
- Vvveb.MediaModal.open(null, function (file) {
- editor.insertContent("<div>" + editor.dom.createHTML('img', {
- src: file,
- "class": "align-center"
- }) + "</div>");
+ Vvveb.MediaModal.type = "single";
+ Vvveb.MediaModal.open(null, function (src, file, files) {
+ if (src) {
+ let imgs = "";
+ for (img of src.split(",")) if (img) {
+ imgs +=
+ "<div>" + editor.dom.createHTML('img', {
+ src: img,
+ "class": "align-center"
+ }) + "</div>";
+ }
+ editor.insertContent(imgs);
+ }
});
//editor.fire("blur");
editor.dispatch("blur");
- //console.log(editor);
}
});
-
+
editor.on('GetContent',function (ed, o) {
if (ed.content.indexOf('<div class="post-content">') > -1) {
@@ -159,15 +469,12 @@ let tinyMceOptions = {
});
/*
editor.on('NodeChange',function (ed, e) {
- //console.log(ed);
fixWrapper(this.contentDocument.body); // if wrapper has been deleted, add it back
});
editor.on('NewBlock',function (ed, e) {
- //console.log(ed);
fixWrapper(this.contentDocument.body); // if wrapper has been deleted, add it back
});
editor.on('Change',function (ed, e) {
- //console.log(ed);
fixWrapper(this.contentDocument.body); // if wrapper has been deleted, add it back
});
*/
@@ -225,7 +532,15 @@ let tinyMceOptions = {
action: function () {
editor.execCommand('mceInsertTable', false, { rows: 2, columns: 2 });
}
- }, {
+ },
+ {
+ text: 'Gallery',
+ icon: 'gallery',
+ action: function () {
+ editor.ui.registry.getAll().buttons['gallery'].onAction();
+ }
+ },
+ {
text: 'Image',
icon: 'image',
action: function () {
@@ -235,6 +550,7 @@ let tinyMceOptions = {
Vvveb.MediaModal = new MediaModal(true);
Vvveb.MediaModal.mediaPath = mediaPath;
}
+ Vvveb.MediaModal.type = "single";
Vvveb.MediaModal.open(null, function (file) {
editor.insertContent("<div>" + editor.dom.createHTML('img', {
src: file,
@@ -242,18 +558,31 @@ let tinyMceOptions = {
}) + "</div>");
});
}
- },
- {
+ }, {
+ text: 'Video',
+ icon: 'embed',
+ action: function () {
+ let header = `<video class="align-center" src="../../media/demo/sample.webm" controls="true">`;
+ editor.execCommand('mceInsertContent', false, header);
+ editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'Audio',
+ icon: 'embed',
+ action: function () {
+ let header = `<figure class="align-center" data-component-audio><audio src="../../media/demo/sample.webm" controls="true"></audio></figure>`;
+ editor.execCommand('mceInsertContent', false, header);
+ editor.selection.select(editor.selection.getNode());
+ }
+ }, {
type: 'separator'
- },
- {
+ }, {
text: 'Bulleted list',
icon: 'unordered-list',
action: function () {
editor.execCommand('InsertUnorderedList', false);
}
- },
- {
+ }, {
text: 'Numbered list',
icon: 'ordered-list',
action: function () {
@@ -265,7 +594,114 @@ let tinyMceOptions = {
action: function () {
editor.execCommand('InsertHorizontalRule');
}
- },/*
+ }, {
+ type: 'separator'
+ }, {
+ text: 'Header',
+ icon: 'unselected',
+ action: function () {
+ let header = `<div class="p-5 mb-4 bg-body-tertiary rounded-3 text-center">
+ <div class="container-fluid py-5">
+ <h1 class="display-5 fw-bold">Custom jumbotron</h1>
+ <p class="fs-4">Using a series of utilities, you can create this jumbotron, just like the one in previous versions of Bootstrap. Check out the examples below for how you can remix and restyle it to your liking.</p>
+
+ <img src="../../media/placeholder.svg" class="align-center my-4" width="250">
+
+ <a href="#" class="btn btn-primary btn-lg" type="button">Example button</a>
+ </div>
+ </div>`;
+ editor.execCommand('mceInsertContent', false, header);
+ //editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'Header dark',
+ icon: 'color-swatch',
+ action: function () {
+ let header = `<div class="text-center">
+ <div class="h-100 p-5 text-bg-dark rounded-3">
+ <h1 class="display-5 fw-bold">Custom jumbotron</h1>
+ <p class="fs-4">Using a series of utilities, you can create this jumbotron, just like the one in previous versions of Bootstrap. Check out the examples below for how you can remix and restyle it to your liking.</p>
+ <img src="../../media/placeholder.svg" class="align-center my-4" width="250">
+ <a href="#" class="btn btn-light btn-lg" type="button">Example button</a>
+ </div>
+ </div>`;
+ editor.execCommand('mceInsertContent', false, header);
+ //editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'Card',
+ icon: 'copy',
+ action: function () {
+ let header = `<div class="card align-center w-25">
+ <img src="../../media/placeholder.svg" class="card-img-top align-center" width="250">
+ <div class="card-body">
+ <h5 class="card-title">Card title</h5>
+ <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
+ <a href="#" class="btn btn-primary">Go somewhere</a>
+ </div>
+ </div>`;
+ editor.execCommand('mceInsertContent', false, header);
+ //editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'Button',
+ icon: 'link',
+ action: function () {
+ let header = `<a href="#" type="button" class="btn btn-primary">Primary</a>`;
+ editor.execCommand('mceInsertContent', false, header);
+ //editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'Alert',
+ icon: 'warning',
+ action: function () {
+ let header = `<p class="alert alert-primary" role="alert">
+ 💡 A simple primary alert—check it out!
+ </p>`;
+ editor.execCommand('mceInsertContent', false, header);
+ //editor.selection.select(editor.selection.getNode());
+ }
+ }, {
+ text: 'File Download',
+ icon: 'new-document',
+ action: function () {
+ let header = `<a href="#" class="btn btn-primary" type="button" download><span>Download file</span> <i class="la la-download"></i></a>`;
+ editor.execCommand('mceInsertContent', false, header);
+
+ let node = editor.selection.getNode();
+ editor.selection.select(node);
+ node = node.querySelector("a") || node;
+
+ if (!Vvveb.MediaModal) {
+ Vvveb.MediaModal = new MediaModal(true);
+ Vvveb.MediaModal.mediaPath = mediaPath;
+ }
+ Vvveb.MediaModal.type = "single";
+
+ Vvveb.MediaModal.open(null, function (file) {
+ node.setAttribute("href", file);
+ node.setAttribute("data-mce-href", file);
+ });
+ }
+ }, {
+ text: 'Accordion',
+ icon: 'accordion',
+ action: function () {
+ let summary = `<details class="align-center">
+ <summary>More information </summary>
+ <p>Here is the source data that is discussed in the article ...</p>
+ </details>`;
+ editor.execCommand('mceInsertContent', false, summary);
+ }
+ }, {
+ text: 'Code',
+ icon: 'sourcecode',
+ action: function () {
+ let code = `<pre class="p-4"><code>print "Hello world!"</code></pre>`;
+ editor.execCommand('mceInsertContent', false, code);
+ }
+ },
+ /*
{
type: 'separator'
}, {
@@ -333,7 +769,7 @@ let tinyMceOptions = {
onAction: onAction,
fetch: (pattern) => {
return new Promise((resolve) => {
- fetch(linkUrl + "&"+ new URLSearchParams({text:pattern}))
+ fetch(linkUrl + "&"+ new URLSearchParams({text:pattern}))
.then((response) => {
if (!response.ok) { throw new Error(response) }
return response.json()
@@ -378,7 +814,7 @@ let tinyMceOptions = {
//valid_children : '-p[img],h1[img],h2[img],h3[img],h4[img],+body[img],div[img],div[h1],div[h2],div[h3]',
//editimage_cors_hosts: ['picsum.photos'],
menubar: false,//'file edit view insert format tools table help',
- toolbar: 'undo redo | bold italic underline strikethrough | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | forecolor backcolor removeformat | insertfile image media link anchor | ltr rtl table accordion | fullscreen preview code print | charmap emoticons advlist visualblocks searchreplace',
+ toolbar: 'undo redo | bold italic underline strikethrough | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | forecolor backcolor removeformat | insertfile image gallery media link anchor | ltr rtl table accordion | fullscreen preview code print | charmap emoticons advlist visualblocks searchreplace',
toolbar_sticky: true,
//toolbar_sticky_offset: isSmallScreen ? 102 : 108,
autosave_ask_before_unload: true,
@@ -409,7 +845,8 @@ let tinyMceOptions = {
Vvveb.MediaModal = new MediaModal(true);
Vvveb.MediaModal.mediaPath = mediaPath;
}
- Vvveb.MediaModal.open(null, callback);
+ Vvveb.MediaModal.type = "single";
+ Vvveb.MediaModal.open(null, (src, file, files) => callback(src));
},/*
templates: [{
title: 'New Table',
@@ -428,9 +865,9 @@ let tinyMceOptions = {
template_mdate_format: '[Date Modified (MDATE): %m/%d/%Y : %H:%M:%S]',
height: 600,
image_caption: true,
- quickbars_insert_toolbar: 'quickmedia quicklink quicktable',
+ quickbars_insert_toolbar: 'quickmedia quicklink quicktable gallery',
quickbars_selection_toolbar: 'bold italic | quicklink h2 h3 h4 blockquote | quickimage quicktable',
- //quickbars_image_toolbar: 'alignleft aligncenter alignright | rotateleft rotateright | imageoptions',
+ quickbars_image_toolbar: 'alignleft aligncenter alignright | rotateleft rotateright imageoptions upimage downimage | editimage',
noneditable_noneditable_class: 'mceNonEditable',
toolbar_mode: 'wrap',
contextmenu: 'link image editimage table',
@@ -450,8 +887,10 @@ let tinyMceOptions = {
force_p_newlines : true,
*/
//extended_valid_elements:"div[*]",
+ //extended_valid_elements : "iframe[src|width|height|name|align]",
//invalid_elements: "",
//forced_root_block : "div",
+ //extended_valid_elements : "script[language|type|src]|iframe[src|width|height|name|align]",
//valid_elements: "*[*]",
apply_source_formatting : false,
verify_html : false,