Skip to content

Commit

Permalink
Added gutters to 'divide' - see #20
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaMinaei committed Jun 11, 2014
1 parent b8352fa commit e158764
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "griddify",
"private": "true",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.4",
"description": "A tiny photoshop panel to make guides and grids",
"main": "index.js",
"scripts": {
Expand Down
64 changes: 42 additions & 22 deletions scripts/coffee/commands/divide.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parseDivisions = require './divide/parseDivisions'
_ = require 'photoshopjs-core'

module.exports = divide = (orientation, divisions) ->
Expand All @@ -6,24 +7,7 @@ module.exports = divide = (orientation, divisions) ->

throw Error "orientation '#{orientation}' isn't in ['vertical', 'horizontal', 'both']"

unless typeof divisions is 'string' or typeof divisions is 'number'

throw Error "divisions must be a string"

divisions = String(divisions).replace /^\s+/, ''
.replace /\s+$/, ''

unless divisions.match /^[0-9]+$/

throw Error "Divisions must be a number. Given: '#{divisions}'"

d = parseInt divisions

unless d isnt 0

throw Error "Wrong value for divisions: '#{divisions}'"

divisions = d
{divisions, gutters} = parseDivisions divisions

doc = _.docs.active

Expand Down Expand Up @@ -55,17 +39,17 @@ module.exports = divide = (orientation, divisions) ->
to = bounds[2]
method = 'addVertical'

add from, to, method
addSeries from, to, method

horizontal = ->

from = bounds[1]
to = bounds[3]
method = 'addHorizontal'

add from, to, method
addSeries from, to, method

add = (from, to, method) ->
addSeries = (from, to, method) ->

len = to - from

Expand All @@ -77,7 +61,43 @@ module.exports = divide = (orientation, divisions) ->

cur += piece

doc.guides[method] cur
addSingle cur, method

return

addSingle = (at, method) ->

for p in positions

doc.guides[method] p + at

return

positions = []

do ->

if gutters.length is 0

positions.push 0

return

absolutes = [0]

cur = 0

for g in gutters

cur += g

absolutes.push cur

len = cur

for p in absolutes

positions.push p - (len / 2)

return

Expand Down
54 changes: 54 additions & 0 deletions scripts/coffee/commands/divide/parseDivisions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = (str) ->

conf =

divisions: 0

hasGutters: no

gutters: []

unless typeof str is 'string' or typeof str is 'number'

throw Error "divisions must be a string"

str = String(str).replace /^\s+/, ''
.replace /\s+$/, ''

nums = str.split /\s+/

if nums.length is 0

throw Error "Divisions must contain numbers"

divisions = nums.shift()

unless divisions.match /^[0-9]+$/

throw Error "Divisions must be a number. Given: '#{divisions}'"

divisions = parseInt divisions

if divisions is 0

throw Error "Divisions cannot be zero"

conf.divisions = divisions

for gutter, i in nums

conf.hasGutters = yes

if isNaN gutter

throw Error "gutter '#{gutter}' is not a number"

gutter = parseFloat gutter

if gutter <= 0

throw Error "Gutters must be bigger than zero"

conf.gutters.push gutter

conf

0 comments on commit e158764

Please sign in to comment.