Add UI/models to allow uploading images

This commit is contained in:
Andrew Miner
2015-06-05 22:47:40 -07:00
parent 94cd133748
commit dd760018df
12 changed files with 361 additions and 10 deletions
+46
View File
@@ -0,0 +1,46 @@
###
Crafting Guide - markdown_image.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
{Event} = require '../constants'
########################################################################################################################
module.exports = class MarkdownImage extends BaseModel
@MIMETYPES = [
{patern:/\.png$/i, text:'image/png'}
{patern:/\.gif$/i, text:'image/gif'}
{patern:/\.jpg$/i, text:'image/jpeg'}
{patern:/\.jpeg$/i, text:'image/jpeg'}
]
constructor: (attributes={}, options={})->
attributes.encodedData ?= null
attributes.fileName ?= ''
attributes.path ?= ''
attributes.sha ?= null
super
@on Event.change + ':fileName', => @_mimeType = null
# Property Methods #############################################################################
getFullPath: ->
return "#{@path}/#{@fileName}"
getMimeType: ->
if not @_mimeType?
for mimeType in MarkdownImage.MIMETYPES
if mimeType.pattern.test @fileName
@_mimeType = mimeType.text
break
return @_mimeType
Object.defineProperties @prototype,
fullPath: {get:@prototype.getFullPath}
@@ -0,0 +1,66 @@
###
Crafting Guide - markdown_image_list.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
{Event} = require '../constants'
MarkdownImage = require './markdown_image'
########################################################################################################################
module.exports = class MarkdownImageList extends BaseModel
constructor: (attributes={}, options={})->
attributes.markdownText ?= null
super attributes, options
@_images = {}
@on Event.change + ':markdownText', => @_analyzeMarkdownText()
@_analyzeMarkdownText()
# Property Methods #############################################################################
getAll: ->
fileNames = (fileName for fileName, image of @_images).sort()
result = []
for fileName in fileNames
result.push @_images[fileName]
return result
Object.defineProperties @prototype,
all: {get:@prototype.getAll}
# Private Methods ##############################################################################
_analyzeMarkdownText: ->
regex = /\!\[([^\]]*)\]\(([^\)]*)\)/g
newImages = {}
changed = false
if @markdownText?
while true
match = regex.exec @markdownText
break unless match?
fileName = match[2]
image = @_images[fileName]
if not image?
image = new MarkdownImage fileName:fileName
@trigger Event.add, this, image
changed = true
newImages[fileName] = image
for fileName, image of @_images
if not newImages[fileName]?
@trigger Event.remove, this, image
changed = true
if changed
@_images = newImages
@trigger Event.change, this