Commit a237acb5 by Edgar HIPP

Update API version 1:mandatory getImage,getSize

parent 78061767
...@@ -6,6 +6,8 @@ fs=require('fs') ...@@ -6,6 +6,8 @@ fs=require('fs')
class ImageModule class ImageModule
constructor:(@options={})-> constructor:(@options={})->
if !@options.centered? then @options.centered=false if !@options.centered? then @options.centered=false
if !@options.getImage? then throw new Error("You should pass getImage")
if !@options.getSize? then throw new Error("You should pass getSize")
@qrQueue=[] @qrQueue=[]
@imageNumber=1 @imageNumber=1
handleEvent:(event,eventData)-> handleEvent:(event,eventData)->
...@@ -35,29 +37,25 @@ class ImageModule ...@@ -35,29 +37,25 @@ class ImageModule
xmlTemplater.replaceXml(subContent,text) xmlTemplater.replaceXml(subContent,text)
convertPixelsToEmus:(pixel)-> convertPixelsToEmus:(pixel)->
Math.round(pixel * 9525) Math.round(pixel * 9525)
getSizeFromData:(imgData)->
[150,150]
getImageFromData:(imgData)->
fs.readFileSync(imgData)
replaceTag:-> replaceTag:->
scopeManager=@manager.getInstance('scopeManager') scopeManager=@manager.getInstance('scopeManager')
templaterState=@manager.getInstance('templaterState') templaterState=@manager.getInstance('templaterState')
tag = templaterState.textInsideTag.substr(1) tag = templaterState.textInsideTag.substr(1)
imgData=scopeManager.getValueFromScope(tag) tagValue = scopeManager.getValue(tag)
tagXml=@manager.getInstance('xmlTemplater').tagXml tagXml=@manager.getInstance('xmlTemplater').tagXml
startEnd= "<#{tagXml}></#{tagXml}>" startEnd= "<#{tagXml}></#{tagXml}>"
if imgData=='undefined' then return @replaceBy(startEnd,tagXml) if !tagValue? then return @replaceBy(startEnd,tagXml)
try try
imgBuffer=@getImageFromData(imgData) imgBuffer=@options.getImage(tagValue)
catch e catch e
return @replaceBy(startEnd,tagXml) return @replaceBy(startEnd,tagXml)
imageRels=@imgManager.loadImageRels(); imageRels=@imgManager.loadImageRels();
if imageRels if imageRels
rId=imageRels.addImageRels(@getNextImageName(),imgBuffer) rId=imageRels.addImageRels(@getNextImageName(),imgBuffer)
sizePixel=@getSizeFromData(imgBuffer) sizePixel=@options.getSize(imgBuffer, tagValue)
size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])] size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])]
if @options.centered==false if @options.centered==false
...@@ -72,10 +70,10 @@ class ImageModule ...@@ -72,10 +70,10 @@ class ImageModule
xmlTemplater=@manager.getInstance('xmlTemplater') xmlTemplater=@manager.getInstance('xmlTemplater')
imR=new ImgReplacer(xmlTemplater,@imgManager) imR=new ImgReplacer(xmlTemplater,@imgManager)
imR.getDataFromString=(result,cb)=> imR.getDataFromString=(result,cb)=>
if @getImageFromDataAsync? if @getImageAsync?
@getImageFromDataAsync(result,cb) @getImageAsync(result,cb)
else else
cb(null,@getImageFromData(result)) cb(null,@getImage(result))
imR.pushQrQueue=(num)=> imR.pushQrQueue=(num)=>
@qrQueue.push(num) @qrQueue.push(num)
imR.popQrQueue=(num)=> imR.popQrQueue=(num)=>
......
...@@ -12,6 +12,15 @@ fileNames=[ ...@@ -12,6 +12,15 @@ fileNames=[
'qrExample2.docx', 'qrExample2.docx',
] ]
opts={}
beforeEach ()->
opts={}
opts.getImage=(tagValue)->
fs.readFileSync(tagValue,'binary')
opts.getSize=(imgBuffer,tagValue)->
[150,150]
opts.centered = false
ImageModule=require('../js/index.js') ImageModule=require('../js/index.js')
docX={} docX={}
...@@ -33,7 +42,7 @@ for name in fileNames ...@@ -33,7 +42,7 @@ for name in fileNames
describe 'image adding with {% image} syntax', ()-> describe 'image adding with {% image} syntax', ()->
it 'should work with one image',()-> it 'should work with one image',()->
name='imageExample.docx' name='imageExample.docx'
imageModule=new ImageModule({centered:false}) imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule) docX[name].attachModule(imageModule)
out=docX[name] out=docX[name]
.load(docX[name].loadedContent) .load(docX[name].loadedContent)
...@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()-> ...@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()->
it 'should work with centering',()-> it 'should work with centering',()->
d=new DocxGen() d=new DocxGen()
name='imageExample.docx' name='imageExample.docx'
imageModule=new ImageModule({centered:true}) opts.centered = true
imageModule=new ImageModule(opts)
d.attachModule(imageModule) d.attachModule(imageModule)
out=d out=d
.load(docX[name].loadedContent) .load(docX[name].loadedContent)
...@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()-> ...@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()->
it 'should work with loops',()-> it 'should work with loops',()->
name='imageLoopExample.docx' name='imageLoopExample.docx'
imageModule=new ImageModule({centered:true}) opts.centered = true
imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule) docX[name].attachModule(imageModule)
out=docX[name] out=docX[name]
...@@ -127,7 +138,7 @@ describe 'image adding with {% image} syntax', ()-> ...@@ -127,7 +138,7 @@ describe 'image adding with {% image} syntax', ()->
it 'should work with image in header/footer',()-> it 'should work with image in header/footer',()->
name='imageHeaderFooterExample.docx' name='imageHeaderFooterExample.docx'
imageModule=new ImageModule({centered:false}) imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule) docX[name].attachModule(imageModule)
out=docX[name] out=docX[name]
.load(docX[name].loadedContent) .load(docX[name].loadedContent)
...@@ -176,10 +187,10 @@ describe 'qrcode replacing',-> ...@@ -176,10 +187,10 @@ describe 'qrcode replacing',->
zip=null zip=null
before (done)-> before (done)->
name='qrExample.docx' name='qrExample.docx'
opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule=new ImageModule({qrCode:true}) imageModule.getImage=(imgData)->
imageModule.getImageFromData=(imgData)->
d=fs.readFileSync('examples/'+imgData,'binary') d=fs.readFileSync('examples/'+imgData,'binary')
d d
...@@ -211,9 +222,10 @@ describe 'qrcode replacing',-> ...@@ -211,9 +222,10 @@ describe 'qrcode replacing',->
before (done)-> before (done)->
name='qrExample2.docx' name='qrExample2.docx'
imageModule=new ImageModule({qrCode:true}) opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule.getImageFromData=(imgData)-> imageModule.getImage=(imgData)->
d=fs.readFileSync('examples/'+imgData,'binary') d=fs.readFileSync('examples/'+imgData,'binary')
d d
...@@ -246,7 +258,8 @@ describe 'qrcode replacing',-> ...@@ -246,7 +258,8 @@ describe 'qrcode replacing',->
before (done)-> before (done)->
d=new DocxGen() d=new DocxGen()
name='noImage.docx' name='noImage.docx'
imageModule=new ImageModule({qrCode:true}) opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule.finished=()-> imageModule.finished=()->
zip=d.getZip() zip=d.getZip()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment