sprite.js 767 B

123456789101112131415161718192021222324252627282930
  1. const { renderer } = require('posthtml-svg-mode');
  2. const defaultFactory = require('./sprite-factory');
  3. class Sprite {
  4. constructor({ tree, filename }) {
  5. this.tree = tree;
  6. this.filename = filename;
  7. }
  8. /**
  9. * @param {Object} options
  10. * @param {Array<SpriteSymbol>} options.symbols
  11. * @param {string} options.filename Output sprite filename
  12. * @param {Function<Promise<PostHTMLProcessingResult>>} [options.factory]
  13. * @return {Promise<Sprite>}
  14. */
  15. static create(options) {
  16. const { symbols, filename, factory = defaultFactory } = options;
  17. return factory({ symbols }).then(({ tree }) => new Sprite({ tree, filename }));
  18. }
  19. /**
  20. * @return {string}
  21. */
  22. render() {
  23. return renderer(this.tree);
  24. }
  25. }
  26. module.exports = Sprite;