Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 23, 2020
0 parents commit 214d6ef
Show file tree
Hide file tree
Showing 11 changed files with 765 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^capture\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
*.Rproj
19 changes: 19 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Package: capture
Title: Capture Screenshot in Web Page
Version: 0.0.0.9000
Authors@R:
c(person(given = "Victor",
family = "Perrier",
role = c("aut", "cre"),
email = "[email protected]"),
person(given = "Fanny",
family = "Meyer",
role = "aut"))
Description: What the package does (one paragraph).
License: GPL-3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Imports:
htmltools
595 changes: 595 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(html_dependency_domtoimage)
importFrom(htmltools,htmlDependency)
importFrom(utils,packageVersion)
25 changes: 25 additions & 0 deletions R/capture.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#' @title Capture Screenshot
#'
#' @description Add a button to take a screenshot of a specified element.
#'
#' @param selector A CSS selector, for example \code{body} to target the whole page or \code{#<ID>} to target a specific ouput.
#' @param filename Name of the file that will be created.
#' @param ... Arguments passed to HTML button.
#'
#' @return an HTML tag.
#' @export
#'
#' @examples
capture <- function(selector, filename, ...) {
tagList(
tags$button(
class = "btn btn-default btn-capture-screenshot",
`data-selector` = selector,
`data-filename` = filename,
...
),
html_dependency_domtoimage()
)
}

23 changes: 23 additions & 0 deletions R/dependencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#' HTML dependencies used by capture
#'
#' @return an \code{\link[htmltools]{htmlDependency}}.
#' @export
#'
#' @importFrom utils packageVersion
#' @importFrom htmltools htmlDependency
#'
#' @name html-dependencies
#'
html_dependency_domtoimage <- function() {
htmlDependency(
name = "dom-to-image",
version = "2.6.0",
src = list(file = "assets"),
package = "capture",
script = c(
"dom-to-image/dom-to-image.min.js",
"js/capture.js"
)
)
}
26 changes: 26 additions & 0 deletions inst/assets/dom-to-image/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
The MIT License (MIT)

Copyright 2015 Anatolii Saienko
https://github.com/tsayen

Copyright 2012 Paul Bakaus
http://paulbakaus.com/

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.
2 changes: 2 additions & 0 deletions inst/assets/dom-to-image/dom-to-image.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions inst/assets/js/capture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* Copyright (c) 2020 dreamRs
*
* capture, JavaScript bindings to take screenshot of an element of the page
*
* @version 0.0.1
*/

/*jshint
browser:true,
devel: true
*/
/*global domtoimage */

(function() {
function download(filename, dataImage) {
var element = document.createElement("a");
element.setAttribute("href", dataImage);
element.setAttribute("download", filename);

element.style.display = "none";
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
}

document.addEventListener("click", function(e) {
var el = e.target;
if (el.classList.contains("btn-capture-screenshot") === false) {
return;
}
var toCapture = el.getAttribute("data-selector");
var node = document.querySelector(toCapture);
var fileName = el.getAttribute("data-filename");
domtoimage
.toPng(node)
.then(function(dataUrl) {
download(fileName, dataUrl);
})
.catch(function(error) {
console.error("Capture: oops, something went wrong!", error);
});
});
})();

15 changes: 15 additions & 0 deletions man/html-dependencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 214d6ef

Please sign in to comment.