-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-path.js
More file actions
94 lines (81 loc) · 2.65 KB
/
reverse-path.js
File metadata and controls
94 lines (81 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//@strict on
//@target Illustrator
const localized = {
emptySelection: {
en: 'Select item(s) to reverse',
fr: 'Choisissez des éléments à inverser',
},
error: {
en: 'Error',
fr: 'Erreur',
},
itemType: {
en: 'Unsupported item of type %1',
fr: 'Élément non supporté de type %1',
},
}
/**
* reversePoints :: [PathPoint] -> void
*/
function reversePoints(points) {
const pointsLength = points.length
const middlePointIndex = pointsLength / 2
for (var i = 0; i < middlePointIndex; i++) {
var point = points[i]
var prevPoint = points[pointsLength - i - 1]
var anchor = prevPoint.anchor
var left = prevPoint.leftDirection
var type = prevPoint.pointType
var right = prevPoint.rightDirection
prevPoint.anchor = point.anchor
prevPoint.leftDirection = point.rightDirection
prevPoint.pointType = point.pointType
prevPoint.rightDirection = point.leftDirection
point.anchor = anchor
point.leftDirection = right
point.pointType = type
point.rightDirection = left
}
}
/**
* reverseSelection :: Selection -> void
*
* Selection => [CompoundPathItem|GroupItem|PathItem|TextFrame]
*
* Memo:
* - all PathPoint related items have selectedPathPoints and pathPoints
* - PathItem and TextPath have selectedPathPoints
* - TextFrame(Item) has textPath
* - GroupItem has pathItems and compoundPathItems
* - CompountPathItem, Document, and Layer have pathItems
* - Document and Layer can't be selected
*/
function reverseSelection(selection) {
const selectionLength = selection.length
for (var i = 0; i < selectionLength; i++) {
var item = selection[i]
switch (item.typename) {
case 'CompoundPathItem':
reverseSelection(item.pathItems)
break;
case 'GroupItem':
reverseSelection(item.compoundPathItems)
reverseSelection(item.pathItems)
break;
case 'PathItem':
reversePoints(item.selectedPathPoints)
break;
case 'TextFrame':
reversePoints(item.textPath.selectedPathPoints)
break;
default:
alert(localize(localized.itemType, item.typename), localize(localized.error))
break;
}
}
}
if (documents.length && selection.length) {
reverseSelection(selection)
} else {
alert(localize(localized.emptySelection), localize(localized.error))
}