From c54b56429421b26e9fa188dbdd74eec246471c27 Mon Sep 17 00:00:00 2001 From: Raffael Date: Thu, 20 Jun 2019 21:31:19 +0200 Subject: [PATCH] added tinymce getimage plugin --- CHANGELOG.md | 4 + admin.php | 1 + assets/getImage.js | 195 +++++++++++++++++++++++++++++++++++++++++++++ bootstrap.php | 2 +- 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 assets/getImage.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 1476769..9ea1d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.1.2 + +* added tynymce (wysiwyg) plugin to resize images properly with the `/getImage` url + ## 0.1.1 * started to implement GUI to change CpMultiplane settings - the most (important) options are implemented diff --git a/admin.php b/admin.php index 0eae6d2..535a032 100644 --- a/admin.php +++ b/admin.php @@ -4,6 +4,7 @@ // add custom assets $this('admin')->addAssets('cpmultiplanegui:assets/field-simple-gallery.tag'); + $this('admin')->addAssets('cpmultiplanegui:assets/getImage.js'); if ($this->module('cockpit')->hasaccess('cpmultiplanegui', 'manage')) { diff --git a/assets/getImage.js b/assets/getImage.js new file mode 100644 index 0000000..314e2ac --- /dev/null +++ b/assets/getImage.js @@ -0,0 +1,195 @@ + +App.$(document).on('init-wysiwyg-editor', function(e, editor) { + + tinymce.PluginManager.add('mpgetimage', function(ed) { + + var openDialog = function() { + + var node = ed.selection.getNode(), + width = '800', + height = '', + method = 'bestFit' + quality = '80' + options = [], + asset = {}, + asset_id = null, + src = node.getAttribute('src'); + + if (src) { + + // for some reason, URLSearchParams doesn't detect the first param after "?" + var query = src.split('?')[1]; + + var params = new URLSearchParams(query); + + if (params.has('w')) width = params.get('w'); + if (params.has('h')) height = params.get('h'); + if (params.has('m')) method = params.get('m'); + if (params.has('q')) quality = params.get('q'); + if (params.has('src')) asset_id = params.get('src'); + + } + + var dialog = UIkit.modal.dialog([ +'
', + '
'+App.i18n.get('Select asset')+'
', + '', + '
', + '
', + '', + '', + '
', + '
', + '', + '', + '
', + '
', + '', + '', + '
', + '
', + '', + '', + '
', + '
', + '', +'
' + ].join(''), {modal:false}); + + // large dialog + dialog.dialog.addClass('uk-modal-dialog-large'); + + // mount content of dialog + riot.mount(dialog.element[0], '*', options); + + var selectButton = dialog.dialog.find('.js-select-button'), + assetsComponent = dialog.dialog.find('cp-assets')[0]._tag; + + selectButton.on('click', function() { + + if (!asset || !asset.mime) { + App.ui.notify('No image selected', 'danger'); + return; + } + + var width = dialog.dialog.find('[data-width]')[0].value, + height = dialog.dialog.find('[data-height]')[0].value, + method = dialog.dialog.find('[data-method]')[0].value, + quality = dialog.dialog.find('[data-quality]')[0].value, + content = ''; + + if (asset.mime.match(/^image\//)) { + + // might break with sub folders... needs some more tests + var src = SITE_URL + '/getImage?src=' + asset._id; + + // skip default and empty values + if (!!width.trim() && width != '800') src += '&w=' + width; + if (!!height.trim()) src += '&h=' + height; + if (!!method.trim() && method != 'bestFit') src += '&m=' + method; + if (!!quality.trim() && quality != '80') src += '&q=' + quality; + + content = ''+(asset.title || 'image')+''; + } else { + // to do... + content = ''+asset.title+''; + } + + ed.insertContent(content); + + dialog.hide(); + + }); + + dialog.on('selectionchange', function(e, s) { + + if (Array.isArray(s) && s.length) { + + // make multiple select to single select + s = s.slice(-1); + assetsComponent.selected = s; + + asset = s[0]; + + } + + }); + + dialog.on('show.uk.modal', function(e) { + + if (asset_id) { + + setTimeout(function() { + + var selectedAsset = assetsComponent.assets.find(obj => { + return obj._id === asset_id; + }); + + asset = selectedAsset; + + assetsComponent.selected.push(selectedAsset); + assetsComponent.update(); + + }, 500); + + } + + }); + + dialog.show(); + + } + + ed.addMenuItem('mpgetimage', { + icon: 'image', + text: App.i18n.get('Edit/Insert image'), + onclick: function(){ + openDialog(); + }, + context: 'insert', + prependToContext: true + }); + + ed.addButton('mpgetimage', { + icon: 'image', + tooltip: App.i18n.get('Edit/Insert image'), + onclick: function(){ + openDialog(); + }, + stateSelector: 'img', + }); + + }); + +/* + // to do... + + // don't enable automatically, if EditorFormats addon is installed + if (editor.settings.modified === undefined) { + + // enable plugin + editor.settings.plugins = editor.settings.plugins + ' mpgetimage'; + + // add toolbar button + if (typeof editor.settings.toolbar == 'undefined') { + // add default toolbar buttons + editor.settings.toolbar = 'undo redo | styleselect | bold italic | alignleft' + + 'aligncenter alignright alignjustify | ' + + 'bullist numlist outdent indent | link image'; + } + editor.settings.toolbar += ' | mpgetimage'; + + } + */ + + // enable plugin and toolbar button + editor.settings.plugins = editor.settings.plugins + ' mpgetimage'; + editor.settings.toolbar += ' | mpgetimage'; + +}); diff --git a/bootstrap.php b/bootstrap.php index 7f08862..541d091 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -5,7 +5,7 @@ * @see https://github.com/raffaelj/cockpit_CpMultiplaneGUI * @see https://github.com/agentejo/cockpit/ * - * @version 0.1.1 + * @version 0.1.2 * @author Raffael Jesche * @license MIT */