ShapeInterface.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import ExpressionPropertyInterface from './ExpressionValueFactory';
  2. import propertyGroupFactory from './PropertyGroupFactory';
  3. import PropertyInterface from './PropertyInterface';
  4. import ShapePathInterface from './shapes/ShapePathInterface';
  5. const ShapeExpressionInterface = (function () {
  6. function iterateElements(shapes, view, propertyGroup) {
  7. var arr = [];
  8. var i;
  9. var len = shapes ? shapes.length : 0;
  10. for (i = 0; i < len; i += 1) {
  11. if (shapes[i].ty === 'gr') {
  12. arr.push(groupInterfaceFactory(shapes[i], view[i], propertyGroup));
  13. } else if (shapes[i].ty === 'fl') {
  14. arr.push(fillInterfaceFactory(shapes[i], view[i], propertyGroup));
  15. } else if (shapes[i].ty === 'st') {
  16. arr.push(strokeInterfaceFactory(shapes[i], view[i], propertyGroup));
  17. } else if (shapes[i].ty === 'tm') {
  18. arr.push(trimInterfaceFactory(shapes[i], view[i], propertyGroup));
  19. } else if (shapes[i].ty === 'tr') {
  20. // arr.push(transformInterfaceFactory(shapes[i],view[i],propertyGroup));
  21. } else if (shapes[i].ty === 'el') {
  22. arr.push(ellipseInterfaceFactory(shapes[i], view[i], propertyGroup));
  23. } else if (shapes[i].ty === 'sr') {
  24. arr.push(starInterfaceFactory(shapes[i], view[i], propertyGroup));
  25. } else if (shapes[i].ty === 'sh') {
  26. arr.push(ShapePathInterface(shapes[i], view[i], propertyGroup));
  27. } else if (shapes[i].ty === 'rc') {
  28. arr.push(rectInterfaceFactory(shapes[i], view[i], propertyGroup));
  29. } else if (shapes[i].ty === 'rd') {
  30. arr.push(roundedInterfaceFactory(shapes[i], view[i], propertyGroup));
  31. } else if (shapes[i].ty === 'rp') {
  32. arr.push(repeaterInterfaceFactory(shapes[i], view[i], propertyGroup));
  33. } else if (shapes[i].ty === 'gf') {
  34. arr.push(gradientFillInterfaceFactory(shapes[i], view[i], propertyGroup));
  35. } else {
  36. arr.push(defaultInterfaceFactory(shapes[i], view[i], propertyGroup));
  37. }
  38. }
  39. return arr;
  40. }
  41. function contentsInterfaceFactory(shape, view, propertyGroup) {
  42. var interfaces;
  43. var interfaceFunction = function _interfaceFunction(value) {
  44. var i = 0;
  45. var len = interfaces.length;
  46. while (i < len) {
  47. if (interfaces[i]._name === value || interfaces[i].mn === value || interfaces[i].propertyIndex === value || interfaces[i].ix === value || interfaces[i].ind === value) {
  48. return interfaces[i];
  49. }
  50. i += 1;
  51. }
  52. if (typeof value === 'number') {
  53. return interfaces[value - 1];
  54. }
  55. return null;
  56. };
  57. interfaceFunction.propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  58. interfaces = iterateElements(shape.it, view.it, interfaceFunction.propertyGroup);
  59. interfaceFunction.numProperties = interfaces.length;
  60. var transformInterface = transformInterfaceFactory(shape.it[shape.it.length - 1], view.it[view.it.length - 1], interfaceFunction.propertyGroup);
  61. interfaceFunction.transform = transformInterface;
  62. interfaceFunction.propertyIndex = shape.cix;
  63. interfaceFunction._name = shape.nm;
  64. return interfaceFunction;
  65. }
  66. function groupInterfaceFactory(shape, view, propertyGroup) {
  67. var interfaceFunction = function _interfaceFunction(value) {
  68. switch (value) {
  69. case 'ADBE Vectors Group':
  70. case 'Contents':
  71. case 2:
  72. return interfaceFunction.content;
  73. // Not necessary for now. Keeping them here in case a new case appears
  74. // case 'ADBE Vector Transform Group':
  75. // case 3:
  76. default:
  77. return interfaceFunction.transform;
  78. }
  79. };
  80. interfaceFunction.propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  81. var content = contentsInterfaceFactory(shape, view, interfaceFunction.propertyGroup);
  82. var transformInterface = transformInterfaceFactory(shape.it[shape.it.length - 1], view.it[view.it.length - 1], interfaceFunction.propertyGroup);
  83. interfaceFunction.content = content;
  84. interfaceFunction.transform = transformInterface;
  85. Object.defineProperty(interfaceFunction, '_name', {
  86. get: function () {
  87. return shape.nm;
  88. },
  89. });
  90. // interfaceFunction.content = interfaceFunction;
  91. interfaceFunction.numProperties = shape.np;
  92. interfaceFunction.propertyIndex = shape.ix;
  93. interfaceFunction.nm = shape.nm;
  94. interfaceFunction.mn = shape.mn;
  95. return interfaceFunction;
  96. }
  97. function fillInterfaceFactory(shape, view, propertyGroup) {
  98. function interfaceFunction(val) {
  99. if (val === 'Color' || val === 'color') {
  100. return interfaceFunction.color;
  101. } if (val === 'Opacity' || val === 'opacity') {
  102. return interfaceFunction.opacity;
  103. }
  104. return null;
  105. }
  106. Object.defineProperties(interfaceFunction, {
  107. color: {
  108. get: ExpressionPropertyInterface(view.c),
  109. },
  110. opacity: {
  111. get: ExpressionPropertyInterface(view.o),
  112. },
  113. _name: { value: shape.nm },
  114. mn: { value: shape.mn },
  115. });
  116. view.c.setGroupProperty(PropertyInterface('Color', propertyGroup));
  117. view.o.setGroupProperty(PropertyInterface('Opacity', propertyGroup));
  118. return interfaceFunction;
  119. }
  120. function gradientFillInterfaceFactory(shape, view, propertyGroup) {
  121. function interfaceFunction(val) {
  122. if (val === 'Start Point' || val === 'start point') {
  123. return interfaceFunction.startPoint;
  124. }
  125. if (val === 'End Point' || val === 'end point') {
  126. return interfaceFunction.endPoint;
  127. }
  128. if (val === 'Opacity' || val === 'opacity') {
  129. return interfaceFunction.opacity;
  130. }
  131. return null;
  132. }
  133. Object.defineProperties(interfaceFunction, {
  134. startPoint: {
  135. get: ExpressionPropertyInterface(view.s),
  136. },
  137. endPoint: {
  138. get: ExpressionPropertyInterface(view.e),
  139. },
  140. opacity: {
  141. get: ExpressionPropertyInterface(view.o),
  142. },
  143. type: {
  144. get: function () {
  145. return 'a';
  146. },
  147. },
  148. _name: { value: shape.nm },
  149. mn: { value: shape.mn },
  150. });
  151. view.s.setGroupProperty(PropertyInterface('Start Point', propertyGroup));
  152. view.e.setGroupProperty(PropertyInterface('End Point', propertyGroup));
  153. view.o.setGroupProperty(PropertyInterface('Opacity', propertyGroup));
  154. return interfaceFunction;
  155. }
  156. function defaultInterfaceFactory() {
  157. function interfaceFunction() {
  158. return null;
  159. }
  160. return interfaceFunction;
  161. }
  162. function strokeInterfaceFactory(shape, view, propertyGroup) {
  163. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  164. var _dashPropertyGroup = propertyGroupFactory(dashOb, _propertyGroup);
  165. function addPropertyToDashOb(i) {
  166. Object.defineProperty(dashOb, shape.d[i].nm, {
  167. get: ExpressionPropertyInterface(view.d.dataProps[i].p),
  168. });
  169. }
  170. var i;
  171. var len = shape.d ? shape.d.length : 0;
  172. var dashOb = {};
  173. for (i = 0; i < len; i += 1) {
  174. addPropertyToDashOb(i);
  175. view.d.dataProps[i].p.setGroupProperty(_dashPropertyGroup);
  176. }
  177. function interfaceFunction(val) {
  178. if (val === 'Color' || val === 'color') {
  179. return interfaceFunction.color;
  180. } if (val === 'Opacity' || val === 'opacity') {
  181. return interfaceFunction.opacity;
  182. } if (val === 'Stroke Width' || val === 'stroke width') {
  183. return interfaceFunction.strokeWidth;
  184. }
  185. return null;
  186. }
  187. Object.defineProperties(interfaceFunction, {
  188. color: {
  189. get: ExpressionPropertyInterface(view.c),
  190. },
  191. opacity: {
  192. get: ExpressionPropertyInterface(view.o),
  193. },
  194. strokeWidth: {
  195. get: ExpressionPropertyInterface(view.w),
  196. },
  197. dash: {
  198. get: function () {
  199. return dashOb;
  200. },
  201. },
  202. _name: { value: shape.nm },
  203. mn: { value: shape.mn },
  204. });
  205. view.c.setGroupProperty(PropertyInterface('Color', _propertyGroup));
  206. view.o.setGroupProperty(PropertyInterface('Opacity', _propertyGroup));
  207. view.w.setGroupProperty(PropertyInterface('Stroke Width', _propertyGroup));
  208. return interfaceFunction;
  209. }
  210. function trimInterfaceFactory(shape, view, propertyGroup) {
  211. function interfaceFunction(val) {
  212. if (val === shape.e.ix || val === 'End' || val === 'end') {
  213. return interfaceFunction.end;
  214. }
  215. if (val === shape.s.ix) {
  216. return interfaceFunction.start;
  217. }
  218. if (val === shape.o.ix) {
  219. return interfaceFunction.offset;
  220. }
  221. return null;
  222. }
  223. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  224. interfaceFunction.propertyIndex = shape.ix;
  225. view.s.setGroupProperty(PropertyInterface('Start', _propertyGroup));
  226. view.e.setGroupProperty(PropertyInterface('End', _propertyGroup));
  227. view.o.setGroupProperty(PropertyInterface('Offset', _propertyGroup));
  228. interfaceFunction.propertyIndex = shape.ix;
  229. interfaceFunction.propertyGroup = propertyGroup;
  230. Object.defineProperties(interfaceFunction, {
  231. start: {
  232. get: ExpressionPropertyInterface(view.s),
  233. },
  234. end: {
  235. get: ExpressionPropertyInterface(view.e),
  236. },
  237. offset: {
  238. get: ExpressionPropertyInterface(view.o),
  239. },
  240. _name: { value: shape.nm },
  241. });
  242. interfaceFunction.mn = shape.mn;
  243. return interfaceFunction;
  244. }
  245. function transformInterfaceFactory(shape, view, propertyGroup) {
  246. function interfaceFunction(value) {
  247. if (shape.a.ix === value || value === 'Anchor Point') {
  248. return interfaceFunction.anchorPoint;
  249. }
  250. if (shape.o.ix === value || value === 'Opacity') {
  251. return interfaceFunction.opacity;
  252. }
  253. if (shape.p.ix === value || value === 'Position') {
  254. return interfaceFunction.position;
  255. }
  256. if (shape.r.ix === value || value === 'Rotation' || value === 'ADBE Vector Rotation') {
  257. return interfaceFunction.rotation;
  258. }
  259. if (shape.s.ix === value || value === 'Scale') {
  260. return interfaceFunction.scale;
  261. }
  262. if ((shape.sk && shape.sk.ix === value) || value === 'Skew') {
  263. return interfaceFunction.skew;
  264. }
  265. if ((shape.sa && shape.sa.ix === value) || value === 'Skew Axis') {
  266. return interfaceFunction.skewAxis;
  267. }
  268. return null;
  269. }
  270. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  271. view.transform.mProps.o.setGroupProperty(PropertyInterface('Opacity', _propertyGroup));
  272. view.transform.mProps.p.setGroupProperty(PropertyInterface('Position', _propertyGroup));
  273. view.transform.mProps.a.setGroupProperty(PropertyInterface('Anchor Point', _propertyGroup));
  274. view.transform.mProps.s.setGroupProperty(PropertyInterface('Scale', _propertyGroup));
  275. view.transform.mProps.r.setGroupProperty(PropertyInterface('Rotation', _propertyGroup));
  276. if (view.transform.mProps.sk) {
  277. view.transform.mProps.sk.setGroupProperty(PropertyInterface('Skew', _propertyGroup));
  278. view.transform.mProps.sa.setGroupProperty(PropertyInterface('Skew Angle', _propertyGroup));
  279. }
  280. view.transform.op.setGroupProperty(PropertyInterface('Opacity', _propertyGroup));
  281. Object.defineProperties(interfaceFunction, {
  282. opacity: {
  283. get: ExpressionPropertyInterface(view.transform.mProps.o),
  284. },
  285. position: {
  286. get: ExpressionPropertyInterface(view.transform.mProps.p),
  287. },
  288. anchorPoint: {
  289. get: ExpressionPropertyInterface(view.transform.mProps.a),
  290. },
  291. scale: {
  292. get: ExpressionPropertyInterface(view.transform.mProps.s),
  293. },
  294. rotation: {
  295. get: ExpressionPropertyInterface(view.transform.mProps.r),
  296. },
  297. skew: {
  298. get: ExpressionPropertyInterface(view.transform.mProps.sk),
  299. },
  300. skewAxis: {
  301. get: ExpressionPropertyInterface(view.transform.mProps.sa),
  302. },
  303. _name: { value: shape.nm },
  304. });
  305. interfaceFunction.ty = 'tr';
  306. interfaceFunction.mn = shape.mn;
  307. interfaceFunction.propertyGroup = propertyGroup;
  308. return interfaceFunction;
  309. }
  310. function ellipseInterfaceFactory(shape, view, propertyGroup) {
  311. function interfaceFunction(value) {
  312. if (shape.p.ix === value) {
  313. return interfaceFunction.position;
  314. }
  315. if (shape.s.ix === value) {
  316. return interfaceFunction.size;
  317. }
  318. return null;
  319. }
  320. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  321. interfaceFunction.propertyIndex = shape.ix;
  322. var prop = view.sh.ty === 'tm' ? view.sh.prop : view.sh;
  323. prop.s.setGroupProperty(PropertyInterface('Size', _propertyGroup));
  324. prop.p.setGroupProperty(PropertyInterface('Position', _propertyGroup));
  325. Object.defineProperties(interfaceFunction, {
  326. size: {
  327. get: ExpressionPropertyInterface(prop.s),
  328. },
  329. position: {
  330. get: ExpressionPropertyInterface(prop.p),
  331. },
  332. _name: { value: shape.nm },
  333. });
  334. interfaceFunction.mn = shape.mn;
  335. return interfaceFunction;
  336. }
  337. function starInterfaceFactory(shape, view, propertyGroup) {
  338. function interfaceFunction(value) {
  339. if (shape.p.ix === value) {
  340. return interfaceFunction.position;
  341. }
  342. if (shape.r.ix === value) {
  343. return interfaceFunction.rotation;
  344. }
  345. if (shape.pt.ix === value) {
  346. return interfaceFunction.points;
  347. }
  348. if (shape.or.ix === value || value === 'ADBE Vector Star Outer Radius') {
  349. return interfaceFunction.outerRadius;
  350. }
  351. if (shape.os.ix === value) {
  352. return interfaceFunction.outerRoundness;
  353. }
  354. if (shape.ir && (shape.ir.ix === value || value === 'ADBE Vector Star Inner Radius')) {
  355. return interfaceFunction.innerRadius;
  356. }
  357. if (shape.is && shape.is.ix === value) {
  358. return interfaceFunction.innerRoundness;
  359. }
  360. return null;
  361. }
  362. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  363. var prop = view.sh.ty === 'tm' ? view.sh.prop : view.sh;
  364. interfaceFunction.propertyIndex = shape.ix;
  365. prop.or.setGroupProperty(PropertyInterface('Outer Radius', _propertyGroup));
  366. prop.os.setGroupProperty(PropertyInterface('Outer Roundness', _propertyGroup));
  367. prop.pt.setGroupProperty(PropertyInterface('Points', _propertyGroup));
  368. prop.p.setGroupProperty(PropertyInterface('Position', _propertyGroup));
  369. prop.r.setGroupProperty(PropertyInterface('Rotation', _propertyGroup));
  370. if (shape.ir) {
  371. prop.ir.setGroupProperty(PropertyInterface('Inner Radius', _propertyGroup));
  372. prop.is.setGroupProperty(PropertyInterface('Inner Roundness', _propertyGroup));
  373. }
  374. Object.defineProperties(interfaceFunction, {
  375. position: {
  376. get: ExpressionPropertyInterface(prop.p),
  377. },
  378. rotation: {
  379. get: ExpressionPropertyInterface(prop.r),
  380. },
  381. points: {
  382. get: ExpressionPropertyInterface(prop.pt),
  383. },
  384. outerRadius: {
  385. get: ExpressionPropertyInterface(prop.or),
  386. },
  387. outerRoundness: {
  388. get: ExpressionPropertyInterface(prop.os),
  389. },
  390. innerRadius: {
  391. get: ExpressionPropertyInterface(prop.ir),
  392. },
  393. innerRoundness: {
  394. get: ExpressionPropertyInterface(prop.is),
  395. },
  396. _name: { value: shape.nm },
  397. });
  398. interfaceFunction.mn = shape.mn;
  399. return interfaceFunction;
  400. }
  401. function rectInterfaceFactory(shape, view, propertyGroup) {
  402. function interfaceFunction(value) {
  403. if (shape.p.ix === value) {
  404. return interfaceFunction.position;
  405. }
  406. if (shape.r.ix === value) {
  407. return interfaceFunction.roundness;
  408. }
  409. if (shape.s.ix === value || value === 'Size' || value === 'ADBE Vector Rect Size') {
  410. return interfaceFunction.size;
  411. }
  412. return null;
  413. }
  414. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  415. var prop = view.sh.ty === 'tm' ? view.sh.prop : view.sh;
  416. interfaceFunction.propertyIndex = shape.ix;
  417. prop.p.setGroupProperty(PropertyInterface('Position', _propertyGroup));
  418. prop.s.setGroupProperty(PropertyInterface('Size', _propertyGroup));
  419. prop.r.setGroupProperty(PropertyInterface('Rotation', _propertyGroup));
  420. Object.defineProperties(interfaceFunction, {
  421. position: {
  422. get: ExpressionPropertyInterface(prop.p),
  423. },
  424. roundness: {
  425. get: ExpressionPropertyInterface(prop.r),
  426. },
  427. size: {
  428. get: ExpressionPropertyInterface(prop.s),
  429. },
  430. _name: { value: shape.nm },
  431. });
  432. interfaceFunction.mn = shape.mn;
  433. return interfaceFunction;
  434. }
  435. function roundedInterfaceFactory(shape, view, propertyGroup) {
  436. function interfaceFunction(value) {
  437. if (shape.r.ix === value || value === 'Round Corners 1') {
  438. return interfaceFunction.radius;
  439. }
  440. return null;
  441. }
  442. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  443. var prop = view;
  444. interfaceFunction.propertyIndex = shape.ix;
  445. prop.rd.setGroupProperty(PropertyInterface('Radius', _propertyGroup));
  446. Object.defineProperties(interfaceFunction, {
  447. radius: {
  448. get: ExpressionPropertyInterface(prop.rd),
  449. },
  450. _name: { value: shape.nm },
  451. });
  452. interfaceFunction.mn = shape.mn;
  453. return interfaceFunction;
  454. }
  455. function repeaterInterfaceFactory(shape, view, propertyGroup) {
  456. function interfaceFunction(value) {
  457. if (shape.c.ix === value || value === 'Copies') {
  458. return interfaceFunction.copies;
  459. } if (shape.o.ix === value || value === 'Offset') {
  460. return interfaceFunction.offset;
  461. }
  462. return null;
  463. }
  464. var _propertyGroup = propertyGroupFactory(interfaceFunction, propertyGroup);
  465. var prop = view;
  466. interfaceFunction.propertyIndex = shape.ix;
  467. prop.c.setGroupProperty(PropertyInterface('Copies', _propertyGroup));
  468. prop.o.setGroupProperty(PropertyInterface('Offset', _propertyGroup));
  469. Object.defineProperties(interfaceFunction, {
  470. copies: {
  471. get: ExpressionPropertyInterface(prop.c),
  472. },
  473. offset: {
  474. get: ExpressionPropertyInterface(prop.o),
  475. },
  476. _name: { value: shape.nm },
  477. });
  478. interfaceFunction.mn = shape.mn;
  479. return interfaceFunction;
  480. }
  481. return function (shapes, view, propertyGroup) {
  482. var interfaces;
  483. function _interfaceFunction(value) {
  484. if (typeof value === 'number') {
  485. value = value === undefined ? 1 : value;
  486. if (value === 0) {
  487. return propertyGroup;
  488. }
  489. return interfaces[value - 1];
  490. }
  491. var i = 0;
  492. var len = interfaces.length;
  493. while (i < len) {
  494. if (interfaces[i]._name === value) {
  495. return interfaces[i];
  496. }
  497. i += 1;
  498. }
  499. return null;
  500. }
  501. function parentGroupWrapper() {
  502. return propertyGroup;
  503. }
  504. _interfaceFunction.propertyGroup = propertyGroupFactory(_interfaceFunction, parentGroupWrapper);
  505. interfaces = iterateElements(shapes, view, _interfaceFunction.propertyGroup);
  506. _interfaceFunction.numProperties = interfaces.length;
  507. _interfaceFunction._name = 'Contents';
  508. return _interfaceFunction;
  509. };
  510. }());
  511. export default ShapeExpressionInterface;