Skip to content

Commit

Permalink
feat(embed): export scores as png
Browse files Browse the repository at this point in the history
  • Loading branch information
gierschv committed Jul 5, 2017
1 parent 10ac08a commit 28ea1f8
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.4.0

* Add method: `getPNG`

## v0.3.0

* Add methods: `getEmbedConfig`, `setEditorConfig`, `edit`
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ When instantiating `Flat.Embed`, you can pass options in the second parameter. I
* [`loadJSON`](#loadjsonscore-object-promisevoid-error): Load Flat JSON file
* [`getMusicXML`](#getmusicxmloptions-object-promisestringuint8array-error): Get the score in MusicXML (compressed or not)
* [`getJSON`](#getjson-object): Get the score data in Flat JSON format
* [`getPNG`](#getpngoptions-object-promisestringuint8array-error): Get the score as a PNG file
* [`getScoreMeta`](#getscoremeta-object): Get the metadata from the current score (for hosted scores)
* [`fullscreen`](#fullscreenstate-bool-promisevoid-error): Toggle fullscreen mode
* [`play`](#play-promisevoid-error): Start playback
Expand Down Expand Up @@ -272,6 +273,26 @@ embed.getJSON().then(function (data) {
});
```

### `getPNG(options?: object): Promise<string|Uint8Array, Error>`

Get the current displayed score as a PNG file

```js
// PNG
embed.getPNG().then(function (png) {
// PNG file as a Uint8Array
console.log(png);
});
```

```js
// PNG
embed.getPNG({result: 'dataURL'}).then(function (png) {
// PNG file as a DataURL
console.log(png);
});
```

### `getScoreMeta(): object`

Get the score metadata of the hosted score. The object will have the same format that the one returned [by our API `GET /v2/scores/{score}`](https://flat.io/developers/api/reference/#operation/getScore).
Expand Down
27 changes: 27 additions & 0 deletions dist/embed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/embed.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/embed.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/embed.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"rollup": "^0.42.0",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-hypothetical": "^1.2.1",
"rollup-plugin-node-resolve": "^3.0.0",
"uglify-js": "^3.0.0"
}
Expand Down
16 changes: 12 additions & 4 deletions rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const hypothetical = require('rollup-plugin-hypothetical');

// Banner
const pkg = require('./package.json');
Expand Down Expand Up @@ -37,6 +38,12 @@ const build = () => {
rollup.rollup({
entry: 'src/embed.js',
plugins: [
hypothetical({
allowRealFiles: true,
files: {
'./node_modules/core-js/library/modules/es6.object.to-string.js': 'export default null'
}
}),
babel({
runtimeHelpers: true,
exclude: 'node_modules/**'
Expand All @@ -62,14 +69,15 @@ const build = () => {
fs.writeFileSync('dist/embed.js.map', map.toString());

const minified = uglifyJs.minify(code, {
fromString: true,
inSourceMap: map,
outSourceMap: 'dist/embed.min.js.map',
sourceMap: {
content: map,
filename: 'dist/embed.min.js.map'
},
output: {
preamble: banner
},
mangle: {
except: ['Embed']
reserved: ['Embed']
}
});

Expand Down
24 changes: 23 additions & 1 deletion src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Embed {
if (typeof options !== 'object') {
return reject(new TypeError('Options must be an object'));
}
this.call('getMusicXML', options).then(function (data) {
this.call('getMusicXML', options).then((data) => {
// Plain XML
if (typeof data === 'string') {
return resolve(data);
Expand All @@ -182,6 +182,28 @@ class Embed {
});
}

/**
* Convert the displayed score in PNG
*
* @return {Promise}
* @fullfill {Uint8Array} PNG File
* @reject {Error} Conversion error
*/
getPNG(options) {
return new Promise((resolve, reject) => {
options = options || {};
if (typeof options !== 'object') {
return reject(new TypeError('Options must be an object'));
}
this.call('getPNG', options).then((data) => {
if (typeof data === 'string') {
return resolve(data);
}
return resolve(new Uint8Array(data));
}).catch(reject);
});
}

/**
* Get the metadata of the score (for scores hosted on Flat)
*
Expand Down

0 comments on commit 28ea1f8

Please sign in to comment.