diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3f59ac9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 MCProHosting + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..7016c66 --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +# redimage + +Simple redactor plugin to allow embedding of external images. How does Redactor not already have this built-in? I don't know. + +# License + +MIT diff --git a/redimage.js b/redimage.js new file mode 100644 index 0000000..6f5ffa3 --- /dev/null +++ b/redimage.js @@ -0,0 +1,97 @@ +(function () { + + var plugins = this.RedactorPlugins = this.RedactorPlugins || {}; + + /** + * Redimage plugin. Little hack there - we're attaching properties to the + * function itself rather than using an object literal, so that a consumer + * of this plugins can modify/override functions as they like. + * + * @return {[type]} [description] + */ + plugins.redimage = function () { + return plugins.redimage; + }; + + /** + * Returns the template string for the modal popup. + * + * @return {String} + */ + plugins.redimage.getTemplate = function() { + return String() + + '
' + + '' + + '' + + '' + + '' + + '
'; + }; + + /** + * Initialises the plugin, adding its image to the Redactor toolbar. + */ + plugins.redimage.init = function () { + var button = this.button.add('redimage-btn', 'Link Image'); + this.button.setAwesome('redimage-btn', 'fa-photo'); + this.button.addCallback(button, this.redimage.show); + }; + + /** + * Displays the modal popup. + */ + plugins.redimage.show = function () { + // Create a new modal template and load the popup + this.modal.addTemplate('redimage', this.redimage.getTemplate()); + this.modal.load('redimage', 'Insert Image', 500); + + // Add a cancel button. + this.modal.createCancelButton(); + + // Add a button to finish and insert the image. + var button = this.modal.createActionButton(this.lang.get('insert')); + button.on('click', plugins.redimage.insert); + + // Show the popup. + this.selection.save(); + this.modal.show(); + }; + + /** + * Inserts an image into the Redactor document. + */ + plugins.redimage.insert = function () { + var link = escape($('#redactor-redimage-link').val()); + var title = escape($('#redactor-redimage-title').val()); + + this.insert.html('' + title + ''); + this.modal.close(); + }; + + /** + * Used by `escape` to convert characters to HTML entities. From lodash. + * + * @param {string} match The matched character to escape. + * @returns {string} Returns the escaped character. + */ + function escapeHtmlChar(match) { + return { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' + }[match]; + } + /** + * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their + * corresponding HTML entities. From lodash. + * + * @param {string} string The string to escape. + * @returns {string} Returns the escaped string. + */ + function escape (str) { + return str.replace(/[&<>"'"]/g, escapeHtmlChar); + } + +}).call(this); diff --git a/test/index.html b/test/index.html new file mode 100644 index 0000000..cdd7771 --- /dev/null +++ b/test/index.html @@ -0,0 +1,26 @@ + + + + Redimage + + + + + +
+ +
+ + + + + + +