summaryrefslogtreecommitdiff
path: root/assets/js/src
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-04-12 18:44:42 -0400
committerFrederic Guillot <fred@kanboard.net>2015-04-12 18:44:42 -0400
commit3b403a1a4b33443ee853556e40d4fe89d3399387 (patch)
tree3b3105c3a83b00fd7bda76aeda4ddde788e9d663 /assets/js/src
parent2a150dd3be96692a98c034233a9ae29ff6f219ac (diff)
Add screenshot support for tasks (copy/paste images directly)
Diffstat (limited to 'assets/js/src')
-rw-r--r--assets/js/src/base.js5
-rw-r--r--assets/js/src/screenshot.js102
2 files changed, 107 insertions, 0 deletions
diff --git a/assets/js/src/base.js b/assets/js/src/base.js
index 0aea1691..7ca3c234 100644
--- a/assets/js/src/base.js
+++ b/assets/js/src/base.js
@@ -263,6 +263,11 @@ var Kanboard = (function() {
}
}
});
+
+ // Screenshot
+ if (Kanboard.Exists("screenshot-zone")) {
+ Kanboard.Screenshot.Init();
+ }
}
};
diff --git a/assets/js/src/screenshot.js b/assets/js/src/screenshot.js
new file mode 100644
index 00000000..fef37356
--- /dev/null
+++ b/assets/js/src/screenshot.js
@@ -0,0 +1,102 @@
+Kanboard.Screenshot = (function() {
+
+ var pasteCatcher = null;
+
+ // Setup event listener and workarounds
+ function init()
+ {
+ if (! window.Clipboard) {
+
+ // Create a contenteditable element
+ pasteCatcher = document.createElement("div");
+ pasteCatcher.setAttribute("contenteditable", "");
+ pasteCatcher.style.opacity = 0;
+ document.body.appendChild(pasteCatcher);
+
+ // Make sure it is always in focus
+ pasteCatcher.focus();
+ document.addEventListener("click", function() { pasteCatcher.focus(); });
+ }
+
+ window.addEventListener("paste", pasteHandler);
+ }
+
+ // Paste event callback
+ function pasteHandler(e)
+ {
+ // Firefox doesn't have the property e.clipboardData.items (only Chrome)
+ if (e.clipboardData && e.clipboardData.items) {
+
+ var items = e.clipboardData.items;
+
+ if (items) {
+
+ for (var i = 0; i < items.length; i++) {
+
+ // Find an image in pasted elements
+ if (items[i].type.indexOf("image") !== -1) {
+
+ var blob = items[i].getAsFile();
+
+ // Get the image as base64 data
+ var reader = new FileReader();
+ reader.onload = function(event) {
+ createImage(event.target.result);
+ };
+
+ reader.readAsDataURL(blob);
+ }
+ }
+ }
+ }
+ else {
+
+ // Handle Firefox
+ setTimeout(checkInput, 100);
+ }
+ }
+
+ // Parse the input in the paste catcher element
+ function checkInput()
+ {
+ var child = pasteCatcher.childNodes[0];
+ pasteCatcher.innerHTML = "";
+
+ if (child) {
+ // If the user pastes an image, the src attribute
+ // will represent the image as a base64 encoded string.
+ if (child.tagName === "IMG") {
+ createImage(child.src);
+ }
+ }
+ }
+
+ // Creates a new image from a given source
+ function createImage(blob)
+ {
+ var pastedImage = new Image();
+ pastedImage.src = blob;
+
+ // Send the image content to the form variable
+ pastedImage.onload = function() {
+ var sourceSplit = blob.split("base64,");
+ var sourceString = sourceSplit[1];
+ $("input[name=screenshot]").val(sourceString);
+ };
+
+ document.getElementById("screenshot-inner").style.display = "none";
+ document.getElementById("screenshot-zone").className = "screenshot-pasted";
+ document.getElementById("screenshot-zone").appendChild(pastedImage);
+ }
+
+ jQuery(document).ready(function() {
+
+ if (Kanboard.Exists("screenshot-zone")) {
+ init();
+ }
+ });
+
+ return {
+ Init: init
+ };
+})(); \ No newline at end of file