Skip to content
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

Merged
merged 4 commits into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/@sanity/image-url/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": ["es2015", "react"],
Copy link
Member

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

"plugins": [
"syntax-object-rest-spread",
"transform-object-rest-spread",
"syntax-class-properties",
"transform-class-properties",
"lodash"
Copy link
Member

Choose a reason for hiding this comment

The 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.

]
}
1 change: 1 addition & 0 deletions packages/@sanity/image-url/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd probably want to add node_modules here too

7 changes: 7 additions & 0 deletions packages/@sanity/image-url/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": [
"sanity",
"sanity/react"
],
"parser": "babel-eslint"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think babel-eslint is needed

}
4 changes: 4 additions & 0 deletions packages/@sanity/image-url/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
package-lock.json
/lib
4 changes: 4 additions & 0 deletions packages/@sanity/image-url/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no example folder

test
src
.idea
8 changes: 8 additions & 0 deletions packages/@sanity/image-url/README.md
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe write a bit more on this part? :)

1 change: 1 addition & 0 deletions packages/@sanity/image-url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO ./lib/index.js is an unnecessary indirection, and it would be better to skip it, and instead require ./lib/builder.js directly here. Also i think there's an issue with mix of export / commonjs requires here that can be avoided with:

module.exports = require('./lib/builder').default

38 changes: 38 additions & 0 deletions packages/@sanity/image-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@sanity/image-url",
"version": "0.119.0",
Copy link
Member

Choose a reason for hiding this comment

The 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",
Copy link
Member

Choose a reason for hiding this comment

The 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 disc is not added as a dependency)

"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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babel-register should probably be added to dev dependencies?

Copy link
Member

Choose a reason for hiding this comment

The 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think remon is used in this project?

Copy link
Member

Choose a reason for hiding this comment

The 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"]
}
150 changes: 150 additions & 0 deletions packages/@sanity/image-url/src/builder.js
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)
}
1 change: 1 addition & 0 deletions packages/@sanity/image-url/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default require('./builder')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of mixing ES import / commonjs require you can do export {default} from './builder'. That said, I don't think this file is needed (see my other comment).

Loading