-
Notifications
You must be signed in to change notification settings - Fork 84
FileAttachments: Stdlib Implementation #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bfdb699
Adding a basic implementation of remote file attachments, with resolv…
jashkenas d0c10dc
Don't expose raw FileAttachment from the standard library
jashkenas 811d89b
Bump d3-require to latest
jashkenas 590fffd
Tweak ResolveFileAttachment Implementation Strategy
jashkenas 793fd80
Throw an error when resolve() fails to find a file by name.
jashkenas e06f788
Rename ResolveFileAttachment to FileAttachments
jashkenas 6e897e9
Shorten.
mbostock 942218a
Update fileAttachment.js
mbostock e6907a5
Update fileAttachment.js
mbostock 83a82ce
Update fileAttachment.js
mbostock e44ff24
Update fileAttachment.js
mbostock 5565a33
3.2.0-rc.1
mbostock 6c35a62
Set crossOrigin automatically.
mbostock 66d99a0
3.2.0-rc.2
mbostock 603e75e
Add FileAttachment.prototype.stream.
mbostock 300ed53
3.2.0-rc.3
mbostock f14ae58
Fix FileAttachment().image() with relative paths
jashkenas aeeba14
Fix relative path.
mbostock d5e65c7
3.2.0-rc.4
mbostock d134b72
Remove fragment identifier from DOM.uid
rreusser 034f115
Improve href creation in DOM.uid
rreusser c0bdb06
3.2.0-rc.5
mbostock 4fd1f08
Adding FileAttachment to the README
jashkenas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
async function remote_fetch(file) { | ||
const response = await fetch(await file.url()); | ||
if (!response.ok) throw new Error(`Unable to load file: ${file.name}`); | ||
return response; | ||
} | ||
|
||
class FileAttachment { | ||
constructor(resolve, name) { | ||
Object.defineProperties(this, { | ||
_resolve: {value: resolve}, | ||
name: {value: name, enumerable: true} | ||
}); | ||
} | ||
async url() { | ||
const url = await this._resolve(this.name); | ||
if (url == null) throw new Error(`Unknown file: ${this.name}`); | ||
return url; | ||
} | ||
async blob() { | ||
return (await remote_fetch(this)).blob(); | ||
} | ||
async arrayBuffer() { | ||
return (await remote_fetch(this)).arrayBuffer(); | ||
} | ||
async text() { | ||
return (await remote_fetch(this)).text(); | ||
} | ||
async json() { | ||
return (await remote_fetch(this)).json(); | ||
} | ||
async stream() { | ||
return (await remote_fetch(this)).body; | ||
} | ||
async image() { | ||
const url = await this.url(); | ||
return new Promise((resolve, reject) => { | ||
const i = new Image; | ||
if (new URL(url, document.baseURI).origin !== new URL(location).origin) { | ||
i.crossOrigin = "anonymous"; | ||
} | ||
i.onload = () => resolve(i); | ||
i.onerror = () => reject(new Error(`Unable to load file: ${this.name}`)); | ||
i.src = url; | ||
}); | ||
} | ||
} | ||
|
||
export default function FileAttachments(resolve) { | ||
return (name) => new FileAttachment(resolve, name); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {default as FileAttachments} from "./fileAttachment"; | ||
export {default as Library} from "./library"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarnpkg.com?