From dea2974961c25ebb62ef6804f8d3ecd7838eb98b Mon Sep 17 00:00:00 2001 From: CobusT Date: Fri, 21 Oct 2022 14:11:26 -0700 Subject: [PATCH 1/2] Update README.md Added properties to image() and added zip() --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a07ca81f..2d5ee2c6 100644 --- a/README.md +++ b/README.md @@ -336,10 +336,10 @@ If array is true, an array of arrays is returned; otherwise, the first ro # *attachment*.image() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") -Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. Consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown. +Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. Consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown. Image properties can be passed in as JavaScript properties. ```js -const image = await FileAttachment("sunset.jpg").image(); +const image = await FileAttachment("sunset.jpg").image({width: 400, style: "border: 1px solid black"}); ``` # *attachment*.arrayBuffer() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") @@ -400,6 +400,16 @@ const document = await FileAttachment("cars.xml").xml(); Returns a promise to an [HTMLDocument](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDocument) containing the contents of the file. +```js +const archive = await FileAttachment("archive.zip").zip(); +const fileNames = archive.filenames; +const readMe = archive.file("Readme.txt").text(); +``` + +# *attachment*.zip() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") + +Returns a promise to a list of Files in the ZIP archive. Files can be read from the archive. + ```js const document = await FileAttachment("index.html").html(); ``` From 9e7c203203683030294c46aa2873e8c58216421e Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Fri, 28 Oct 2022 14:26:03 -0700 Subject: [PATCH 2/2] Update README (#311) --- README.md | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2d5ee2c6..28003118 100644 --- a/README.md +++ b/README.md @@ -334,12 +334,18 @@ const data = await FileAttachment("cars.tsv").tsv(); If array is true, an array of arrays is returned; otherwise, the first row is assumed to be the header row and an array of objects is returned, and the returned array has a data.columns property that is an array of column names. (See d3.tsvParseRows.) If typed is true, [automatic type inference](https://observablehq.com/@d3/d3-autotype) is applied; only use this feature if you know your data is compatible. -# *attachment*.image() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") +# *attachment*.image(options) [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") -Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. Consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown. Image properties can be passed in as JavaScript properties. +Returns a promise to a file loaded as an [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image). The promise resolves when the image has finished loading, making this useful for reading the image pixels in Canvas, or for loading the image into a WebGL texture. Consider [*attachment*.url](#attachment_url) if you want to embed an image in HTML or Markdown. ```js -const image = await FileAttachment("sunset.jpg").image({width: 400, style: "border: 1px solid black"}); +const image = await FileAttachment("sunset.jpg").image(); +``` + +If desired, additional image properties can be passed in as *options*. + +```js +const image = await FileAttachment("sunset.jpg").image({width: 400, height: 400}); ``` # *attachment*.arrayBuffer() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") @@ -401,17 +407,15 @@ const document = await FileAttachment("cars.xml").xml(); Returns a promise to an [HTMLDocument](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDocument) containing the contents of the file. ```js -const archive = await FileAttachment("archive.zip").zip(); -const fileNames = archive.filenames; -const readMe = archive.file("Readme.txt").text(); +const document = await FileAttachment("index.html").html(); ``` # *attachment*.zip() [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") -Returns a promise to a list of Files in the ZIP archive. Files can be read from the archive. +Returns a promise to a [ZipArchive](#zip-archives) containing the contents of the file. ```js -const document = await FileAttachment("index.html").html(); +const archive = await FileAttachment("archive.zip").zip(); ``` # FileAttachments(resolve) [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") @@ -420,6 +424,26 @@ const document = await FileAttachment("index.html").html(); Returns a [*FileAttachment*](#FileAttachment) function given the specified *resolve* function. The *resolve* function is a function that takes a *name* and returns either an object {url, mimeType} representing the requested file if it exists, or null if the file does not exist. The url field (though not the object itself!) may be represented as a Promise if the URL is not yet known, such as for a file that is currently being uploaded. The mimeType must be a string, or undefined if the mime type is not known. For backwards compatibility, the *resolve* function may instead return just a URL, either a string or a promise. +#### Zip archives + +# *archive*.filenames [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") + +Returns an array of paths representing the files contained within the archive. + +```js +const archive = await FileAttachment("archive.zip").zip(); +console.log(archive.filenames); +``` + +# *archive*.file(path) [<>](https://github.com/observablehq/stdlib/blob/main/src/fileAttachment.mjs "Source") + +Returns a [file attachment](#file-attachments) for the file with the specified *path*. One of the file attachment methods can then be called to access the contents of the file. For example, to read a text file, use [*attachment*.text](#attachment_text). + +```js +const archive = await FileAttachment("archive.zip").zip(); +const text = await archive.file("readme.txt").text(); +``` + ### Generators # Generators.disposable(value, dispose) [<>](https://github.com/observablehq/stdlib/blob/main/src/generators/disposable.mjs "Source")