| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'merges multiple paths in one if possible';
|
|---|
| 8 |
|
|---|
| 9 | exports.params = {
|
|---|
| 10 | collapseRepeated: true,
|
|---|
| 11 | force: false,
|
|---|
| 12 | leadingZero: true,
|
|---|
| 13 | negativeExtraSpace: true,
|
|---|
| 14 | noSpaceAfterFlags: true
|
|---|
| 15 | };
|
|---|
| 16 |
|
|---|
| 17 | var path2js = require('./_path.js').path2js,
|
|---|
| 18 | js2path = require('./_path.js').js2path,
|
|---|
| 19 | intersects = require('./_path.js').intersects;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Merge multiple Paths into one.
|
|---|
| 23 | *
|
|---|
| 24 | * @param {Object} item current iteration item
|
|---|
| 25 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 26 | *
|
|---|
| 27 | * @author Kir Belevich, Lev Solntsev
|
|---|
| 28 | */
|
|---|
| 29 | exports.fn = function(item, params) {
|
|---|
| 30 |
|
|---|
| 31 | if (!item.isElem() || item.isEmpty()) return;
|
|---|
| 32 |
|
|---|
| 33 | var prevContentItem = null,
|
|---|
| 34 | prevContentItemKeys = null;
|
|---|
| 35 |
|
|---|
| 36 | item.content = item.content.filter(function(contentItem) {
|
|---|
| 37 |
|
|---|
| 38 | if (prevContentItem &&
|
|---|
| 39 | prevContentItem.isElem('path') &&
|
|---|
| 40 | prevContentItem.isEmpty() &&
|
|---|
| 41 | prevContentItem.hasAttr('d') &&
|
|---|
| 42 | contentItem.isElem('path') &&
|
|---|
| 43 | contentItem.isEmpty() &&
|
|---|
| 44 | contentItem.hasAttr('d')
|
|---|
| 45 | ) {
|
|---|
| 46 |
|
|---|
| 47 | if (!prevContentItemKeys) {
|
|---|
| 48 | prevContentItemKeys = Object.keys(prevContentItem.attrs);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | var contentItemAttrs = Object.keys(contentItem.attrs),
|
|---|
| 52 | equalData = prevContentItemKeys.length == contentItemAttrs.length &&
|
|---|
| 53 | contentItemAttrs.every(function(key) {
|
|---|
| 54 | return key == 'd' ||
|
|---|
| 55 | prevContentItem.hasAttr(key) &&
|
|---|
| 56 | prevContentItem.attr(key).value == contentItem.attr(key).value;
|
|---|
| 57 | }),
|
|---|
| 58 | prevPathJS = path2js(prevContentItem),
|
|---|
| 59 | curPathJS = path2js(contentItem);
|
|---|
| 60 |
|
|---|
| 61 | if (equalData && (params.force || !intersects(prevPathJS, curPathJS))) {
|
|---|
| 62 | js2path(prevContentItem, prevPathJS.concat(curPathJS), params);
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | prevContentItem = contentItem;
|
|---|
| 68 | prevContentItemKeys = null;
|
|---|
| 69 | return true;
|
|---|
| 70 |
|
|---|
| 71 | });
|
|---|
| 72 |
|
|---|
| 73 | };
|
|---|