Skip to content

Commit

Permalink
Flo code feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
koenbok committed Aug 2, 2016
1 parent fd32c4c commit 5d1fcb4
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions framer/Components/ShareComponent.coffee
Expand Up @@ -25,15 +25,15 @@ class ShareLayer extends Layer


defaultProps = defaultProps =
backgroundColor: null backgroundColor: null
width: options.parent.width if options and options.parent width: options.parent.width if options and options.parent # This is not correct
ignoreEvents: false ignoreEvents: false
style: style:
fontFamily: "Roboto, Helvetica Neue, Helvetica, Arial, sans-serif" fontFamily: "Roboto, Helvetica Neue, Helvetica, Arial, sans-serif"
textAlign: "left" textAlign: "left"
fontSize: "14px" fontSize: "14px"
color: "#111" color: "#111"
lineHeight: "1" lineHeight: "1"
webkitFontSmoothing: "antialiased"; webkitFontSmoothing: "antialiased"


@props = _.merge(defaultProps, options) @props = _.merge(defaultProps, options)


Expand All @@ -54,7 +54,7 @@ class Button extends ShareLayer
borderRadius: 3 borderRadius: 3
textAlign: "center" textAlign: "center"
paddingTop: "9px" paddingTop: "9px"
width: options.parent.width if options and options.parent width: options.parent.width if options and options.parent # This is not correct


@props = _.merge(defaultProps, options) @props = _.merge(defaultProps, options)


Expand All @@ -64,7 +64,7 @@ class Button extends ShareLayer


@onMouseOver -> @onMouseOver ->
@style.cursor = "pointer" @style.cursor = "pointer"
@states.switch('hover') @states.switch('hover') # Why mix ' and " (everywhere)


@onMouseOut -> @onMouseOut ->
@opacity = 1 @opacity = 1
Expand All @@ -74,8 +74,12 @@ class Button extends ShareLayer


# Share component # Share component
class ShareComponent class ShareComponent

constructor: (@shareInfo) -> constructor: (@shareInfo) ->


# Get the project id from the url
projectId = window.location.pathname.replace(/\//g, "")

# When fixed is set to true, the sheet won't hide on resize. # When fixed is set to true, the sheet won't hide on resize.
# This is triggered by a manual open / close action. # This is triggered by a manual open / close action.
@options = @options =
Expand All @@ -84,14 +88,14 @@ class ShareComponent
minAvailableSpace: 300 minAvailableSpace: 300
minAvailableSpaceFullScreen: 500 minAvailableSpaceFullScreen: 500
maxDescriptionLength: 145 maxDescriptionLength: 145
id: window.location.pathname.replace(/\//g, "") id: projectId


# See if a state is set # See if a state is set
@state = localStorage.getItem("framerShareSheetState-#{@options.id}") @state = localStorage.getItem("framerShareSheetState-#{@options.id}")
@options.fixed = if !@state then false else true @options.fixed = if !@state then false else true


@_checkData() @_checkData()
@render() if !Utils.isMobile() @render() unless Utils.isMobile()


render: -> render: ->
@_renderSheet() @_renderSheet()
Expand Down Expand Up @@ -121,16 +125,22 @@ class ShareComponent


_checkData: -> _checkData: ->


# Twitter handle # Remove leading @ from the Twitter handle
if @shareInfo.twitter and @shareInfo.twitter.charAt(0) is "@" if _.startsWith(@shareInfo.twitter, "@")
@shareInfo.twitter = @shareInfo.twitter.substring(1) @shareInfo.twitter[1..]


# Truncate title if too long # Truncate title if too long
if @shareInfo.title if @shareInfo.title

# Flo: put this on top as a utility method
truncate = (str, n) -> truncate = (str, n) ->
truncatedString = str truncatedString = str
truncatedString.substr(0, n-1).trim() + "…" truncatedString.substr(0, n-1).trim() + "…"


# Flo: turn this into variables with sensible names
a = 26
b = 34

if @shareInfo.twitter and @shareInfo.title.length > 26 if @shareInfo.twitter and @shareInfo.title.length > 26
@shareInfo.title = truncate(@shareInfo.title, 26) @shareInfo.title = truncate(@shareInfo.title, 26)
else if @shareInfo.title.length > 34 else if @shareInfo.title.length > 34
Expand Down Expand Up @@ -197,6 +207,7 @@ class ShareComponent


# Render CTA section # Render CTA section
_renderCTA: -> _renderCTA: ->

@cta = new ShareLayer @cta = new ShareLayer
parent: @sheet parent: @sheet
style: style:
Expand Down Expand Up @@ -249,6 +260,7 @@ class ShareComponent
parent: @info parent: @info
height: 16 height: 16


# If we miss a title from the json metadata, we fallback to the document url
fallbackTitle = _.replace(FramerStudioInfo.documentTitle, /\.framer$/, '') fallbackTitle = _.replace(FramerStudioInfo.documentTitle, /\.framer$/, '')


@credentialsTitle = new ShareLayer @credentialsTitle = new ShareLayer
Expand Down Expand Up @@ -311,6 +323,7 @@ class ShareComponent
showAuthor(@shareInfo.author) showAuthor(@shareInfo.author)


_renderDate: -> _renderDate: ->

verticalPosition = if @description then @description.maxY else @credentials.maxY verticalPosition = if @description then @description.maxY else @credentials.maxY


date = new Date(@shareInfo.date) date = new Date(@shareInfo.date)
Expand All @@ -328,28 +341,26 @@ class ShareComponent
letterSpacing: ".2px" letterSpacing: ".2px"


_enableUserSelect: (layer) -> _enableUserSelect: (layer) ->
if !layer.html then layer.html = "" layer.html = "" unless layer.html
layer._elementHTML.style["-webkit-user-select"] = "auto" layer._elementHTML.style["-webkit-user-select"] = "auto"
layer._elementHTML.style["cursor"] = "auto" layer._elementHTML.style["cursor"] = "auto"


_renderDescription: -> _renderDescription: ->


# See if there are any url's in the description and wrap them in anchor tags. Make sure linebreaks are wrapped in <br/ >'s.' # See if there are any urls in the description and wrap them in anchor tags. Make sure linebreaks are wrapped in <br/ >'s.'
parseDescription = (text) -> parseDescription = (text) ->
urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi
httpRegex = /^((http|https):\/\/)/ httpRegex = /^((http|https):\/\/)/
lineBreakRegex = /(?:\r\n|\r|\n)/g lineBreakRegex = /(?:\r\n|\r|\n)/g


urlified = text.replace urlRegex, (url) -> urlified = text.replace urlRegex, (url) ->


if !httpRegex.test(url) # Flo: try to use not/unless instead of !
href = "//#{url}" href = url
else href = "//#{url}" if not httpRegex.test(url)
href = url return "<a href='#{href}' style='-webkit-user-select: auto' target='_blank'>#{url}</a>"

"<a href='#{href}' style='-webkit-user-select: auto' target='_blank'>#{url}</a>"


urlified.replace lineBreakRegex, '<br />' urlified.replace(lineBreakRegex, "<br />")


@description = new ShareLayer @description = new ShareLayer
parent: @info parent: @info
Expand Down Expand Up @@ -412,6 +423,8 @@ class ShareComponent
@_enableUserSelect(@description) @_enableUserSelect(@description)


_renderButtons: -> _renderButtons: ->

# Flo: too complex; simplify
verticalPosition = if @date then @date.maxY else (if @descripion then @description.maxY else @credentials.maxY) verticalPosition = if @date then @date.maxY else (if @descripion then @description.maxY else @credentials.maxY)


@buttons = new ShareLayer @buttons = new ShareLayer
Expand Down Expand Up @@ -490,6 +503,9 @@ class ShareComponent
@_openSheet() @_openSheet()


_calculateAvailableSpace: => _calculateAvailableSpace: =>

# Flo can any of the numbers below (20, 95) be turned into variables with sensible names?

device = Framer.Device device = Framer.Device
@threshold = @options.minAvailableSpaceFullScreen @threshold = @options.minAvailableSpaceFullScreen
@availableSpace = Canvas.width @availableSpace = Canvas.width
Expand Down Expand Up @@ -538,7 +554,7 @@ class ShareComponent
@close.onClick => @close.onClick =>
@_closeSheet() @_closeSheet()
@options.fixed = true @options.fixed = true
localStorage.setItem("framerShareSheetState-#{@options.id}", 'closed') localStorage.setItem("framerShareSheetState-#{@options.id}", 'closed') # Flo: ' "


# Disable events propagating up to block unintented interactions # Disable events propagating up to block unintented interactions
@sheet.onTouchStart (event) -> event.stopPropagation() @sheet.onTouchStart (event) -> event.stopPropagation()
Expand All @@ -549,12 +565,12 @@ class ShareComponent
event.stopPropagation() event.stopPropagation()
@_openSheet() @_openSheet()
@options.fixed = true @options.fixed = true
localStorage.setItem("framerShareSheetState-#{@options.id}", 'open') localStorage.setItem("framerShareSheetState-#{@options.id}", 'open') # Ahem


# When the window resizes evaluate if the sheet needs to be hidden # When the window resizes evaluate if the sheet needs to be hidden
Canvas.onResize => Canvas.onResize =>
@_calculateAvailableSpace() @_calculateAvailableSpace()
@_openIfEnoughSpace() if !@options.fixed @_openIfEnoughSpace() unless @options.fixed


# Show hand cursor # Show hand cursor
_showPointer: (layer) -> _showPointer: (layer) ->
Expand Down

0 comments on commit 5d1fcb4

Please sign in to comment.