-
Notifications
You must be signed in to change notification settings - Fork 411
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
[image-url] Introducing the image-url package #352
Changes from 2 commits
515fcc5
225749a
fb21262
955fed1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": ["es2015", "react"], | ||
"plugins": [ | ||
"syntax-object-rest-spread", | ||
"transform-object-rest-spread", | ||
"syntax-class-properties", | ||
"transform-class-properties", | ||
"lodash" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see, neither of these plugins are actually needed and should also be removed. |
||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/lib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd probably want to add |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": [ | ||
"sanity", | ||
"sanity/react" | ||
], | ||
"parser": "babel-eslint" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think babel-eslint is needed |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
npm-debug.log | ||
package-lock.json | ||
/lib |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no example folder |
||
test | ||
src | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Tools to generate image urls from Sanity content | ||
|
||
## Getting started | ||
|
||
npm install --save @sanity/image-url | ||
|
||
## Usage | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe write a bit more on this part? :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO module.exports = require('./lib/builder').default |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@sanity/image-url", | ||
"version": "0.119.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be 0.120.0 |
||
"description": "Tools to generate image urls from Sanity content", | ||
"main": "index.js", | ||
"scripts": { | ||
"clean": "rimraf lib", | ||
"bloat": | ||
"browserify -t babelify --full-paths ./src/index.js | discify --open", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "bloat" script here is not that useful and can be removed (I also don't think it will work since |
||
"lint": "eslint .", | ||
"pre-commit-check": | ||
"npm test && (eslint . || (echo 'Warning: project has lint errors. Please fix and re-commit with `git commit --amend`' && echo))", | ||
"test": "mocha --recursive --require babel-register test" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. babel-register should probably be added to dev dependencies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to add eslint with Sanity config and a lint task |
||
}, | ||
"author": "Sanity.io <hello@sanity.io>", | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babelify": "^7.3.0", | ||
"browserify": "^14.3.0", | ||
"mocha": "^3.2.0", | ||
"remon": "^1.0.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think remon is used in this project? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neither is browserify/babelify if the bloat task is removed |
||
"rimraf": "^2.6.2", | ||
"should": "^11.1.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sanity-io/sanity.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/sanity-io/sanity/issues" | ||
}, | ||
"homepage": "https://www.sanity.io/", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"keywords": ["sanity", "cms", "headless", "realtime", "content", "image-url"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import urlForImage from './urlForImage' | ||
|
||
class ImageUrlBuilder { | ||
constructor(parent, options) { | ||
if (parent) { | ||
this.options = Object.assign({}, parent.options, options || {}) | ||
} else { | ||
this.options = options || {} | ||
} | ||
} | ||
|
||
_withOptions(options) { | ||
return new ImageUrlBuilder(this, options) | ||
} | ||
|
||
// The image to be represented. Accepts a Sanity 'image'-document, 'asset'-document or | ||
// _id of asset. To get the benefit of automatic hot-spot/crop integration with the content | ||
// studio, the 'image'-document must be provided. | ||
image(source) { | ||
return this._withOptions({source}) | ||
} | ||
|
||
// Specify the dataset | ||
dataset(dataset) { | ||
return this._withOptions({dataset}) | ||
} | ||
|
||
// Specify the projectId | ||
projectId(projectId) { | ||
return this._withOptions({projectId}) | ||
} | ||
|
||
// Specify the width of the image in pixels | ||
width(width) { | ||
return this._withOptions({width}) | ||
} | ||
|
||
// Specify the height of the image in pixels | ||
height(height) { | ||
return this._withOptions({height}) | ||
} | ||
|
||
// Specify focal point in fraction of image dimensions. Each component 0.0-1.0 | ||
focalPoint(x, y) { | ||
return this._withOptions({focalPoint: {x, y}}) | ||
} | ||
|
||
maxWidth(maxWidth) { | ||
return this._withOptions({maxWidth}) | ||
} | ||
|
||
minWidth(minWidth) { | ||
return this._withOptions({minWidth}) | ||
} | ||
|
||
maxHeight(maxHeight) { | ||
return this._withOptions({maxHeight}) | ||
} | ||
|
||
minHeight(minHeight) { | ||
return this._withOptions({minHeight}) | ||
} | ||
|
||
// Specify width and height in pixels | ||
size(width, height) { | ||
return this._withOptions({width, height}) | ||
} | ||
|
||
// Specify blur between 0 and 100 | ||
blur(blur) { | ||
return this._withOptions({blur}) | ||
} | ||
|
||
sharpen(sharpen) { | ||
return this._withOptions({sharpen}) | ||
} | ||
|
||
// Specify the desired rectangle of the image | ||
rect(left, top, width, height) { | ||
return this._withOptions({rect: {left, top, width, height}}) | ||
} | ||
|
||
// Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp' | ||
format(format) { | ||
return this._withOptions({format}) | ||
} | ||
|
||
invert(invert) { | ||
return this._withOptions({invert}) | ||
} | ||
|
||
sharpen(sharpen) { | ||
return this._withOptions({sharpen}) | ||
} | ||
|
||
// Rotation in degrees 0, 90, 180, 270 | ||
orientation(orientation) { | ||
return this._withOptions({orientation}) | ||
} | ||
|
||
// Compression quality 0-100 | ||
quality(quality) { | ||
return this._withOptions({quality}) | ||
} | ||
|
||
// Make it a download link. Parameter is default filename. | ||
forceDownload(download) { | ||
return this._withOptions({download}) | ||
} | ||
|
||
// Flip image horizontally | ||
flipHorizontal() { | ||
return this._withOptions({flipHorizontal: true}) | ||
} | ||
|
||
// Flip image verically | ||
flipVertical() { | ||
return this._withOptions({flipVertical: true}) | ||
} | ||
|
||
// Ignore crop/hotspot from image record, even when present | ||
ignoreImageParams() { | ||
return urlForImage({ignoreImageParams: true}) | ||
} | ||
|
||
// Gets the url based on the submitted parameters | ||
url() { | ||
return urlForImage(this.options) | ||
} | ||
|
||
|
||
// Synonym for url() | ||
toString() { | ||
return this.url() | ||
} | ||
} | ||
|
||
export default function urlBuilder(options) { | ||
// Did we get a SanityClient? | ||
if (options.clientConfig) { | ||
// Inherit config from client | ||
return new ImageUrlBuilder(null, { | ||
projectId: options.clientConfig.projectId, | ||
dataset: options.clientConfig.dataset, | ||
}) | ||
} | ||
|
||
// Or just accept the options as given | ||
return new ImageUrlBuilder(null, options) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default require('./builder') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of mixing ES import / commonjs require you can do |
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.
The react preset should be removed from here