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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { extend } = require("sdk/core/heritage");
const { AutoRefreshHighlighter } = require("./auto-refresh");
const {
CanvasFrameAnonymousContentHelper, getComputedStyle,
createSVGNode, createNode } = require("./utils/markup");
const { setIgnoreLayoutChanges,
getNodeBounds } = require("devtools/shared/layout/utils");
// The minimum distance a line should be before it has an arrow marker-end
const ARROW_LINE_MIN_DISTANCE = 10;
var MARKER_COUNTER = 1;
/**
* The CssTransformHighlighter is the class that draws an outline around a
* transformed element and an outline around where it would be if untransformed
* as well as arrows connecting the 2 outlines' corners.
*/
function CssTransformHighlighter(highlighterEnv) {
AutoRefreshHighlighter.call(this, highlighterEnv);
this.markup = new CanvasFrameAnonymousContentHelper(this.highlighterEnv,
this._buildMarkup.bind(this));
}
CssTransformHighlighter.prototype = extend(AutoRefreshHighlighter.prototype, {
typeName: "CssTransformHighlighter",
ID_CLASS_PREFIX: "css-transform-",
_buildMarkup: function () {
let container = createNode(this.win, {
attributes: {
"class": "highlighter-container"
}
});
// The root wrapper is used to unzoom the highlighter when needed.
let rootWrapper = createNode(this.win, {
parent: container,
attributes: {
"id": "root",
"class": "root"
},
prefix: this.ID_CLASS_PREFIX
});
let svg = createSVGNode(this.win, {
nodeType: "svg",
parent: rootWrapper,
attributes: {
"id": "elements",
"hidden": "true",
"width": "100%",
"height": "100%"
},
prefix: this.ID_CLASS_PREFIX
});
// Add a marker tag to the svg root for the arrow tip
this.markerId = "arrow-marker-" + MARKER_COUNTER;
MARKER_COUNTER++;
let marker = createSVGNode(this.win, {
nodeType: "marker",
parent: svg,
attributes: {
"id": this.markerId,
"markerWidth": "10",
"markerHeight": "5",
"orient": "auto",
"markerUnits": "strokeWidth",
"refX": "10",
"refY": "5",
"viewBox": "0 0 10 10"
},
prefix: this.ID_CLASS_PREFIX
});
createSVGNode(this.win, {
nodeType: "path",
parent: marker,
attributes: {
"d": "M 0 0 L 10 5 L 0 10 z",
"fill": "#08C"
}
});
let shapesGroup = createSVGNode(this.win, {
nodeType: "g",
parent: svg
});
// Create the 2 polygons (transformed and untransformed)
createSVGNode(this.win, {
nodeType: "polygon",
parent: shapesGroup,
attributes: {
"id": "untransformed",
"class": "untransformed"
},
prefix: this.ID_CLASS_PREFIX
});
createSVGNode(this.win, {
nodeType: "polygon",
parent: shapesGroup,
attributes: {
"id": "transformed",
"class": "transformed"
},
prefix: this.ID_CLASS_PREFIX
});
// Create the arrows
for (let nb of ["1", "2", "3", "4"]) {
createSVGNode(this.win, {
nodeType: "line",
parent: shapesGroup,
attributes: {
"id": "line" + nb,
"class": "line",
"marker-end": "url(#" + this.markerId + ")"
},
prefix: this.ID_CLASS_PREFIX
});
}
return container;
},
/**
* Destroy the nodes. Remove listeners.
*/
destroy: function () {
AutoRefreshHighlighter.prototype.destroy.call(this);
this.markup.destroy();
},
getElement: function (id) {
return this.markup.getElement(this.ID_CLASS_PREFIX + id);
},
/**
* Show the highlighter on a given node
*/
_show: function () {
if (!this._isTransformed(this.currentNode)) {
this.hide();
return false;
}
return this._update();
},
/**
* Checks if the supplied node is transformed and not inline
*/
_isTransformed: function (node) {
let style = getComputedStyle(node);
return style && (style.transform !== "none" && style.display !== "inline");
},
_setPolygonPoints: function (quad, id) {
let points = [];
for (let point of ["p1", "p2", "p3", "p4"]) {
points.push(quad[point].x + "," + quad[point].y);
}
this.getElement(id).setAttribute("points", points.join(" "));
},
_setLinePoints: function (p1, p2, id) {
let line = this.getElement(id);
line.setAttribute("x1", p1.x);
line.setAttribute("y1", p1.y);
line.setAttribute("x2", p2.x);
line.setAttribute("y2", p2.y);
let dist = Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
if (dist < ARROW_LINE_MIN_DISTANCE) {
line.removeAttribute("marker-end");
} else {
line.setAttribute("marker-end", "url(#" + this.markerId + ")");
}
},
/**
* Update the highlighter on the current highlighted node (the one that was
* passed as an argument to show(node)).
* Should be called whenever node size or attributes change
*/
_update: function () {
setIgnoreLayoutChanges(true);
// Getting the points for the transformed shape
let quads = this.currentQuads.border;
if (!quads.length ||
quads[0].bounds.width <= 0 || quads[0].bounds.height <= 0) {
this._hideShapes();
return false;
}
let [quad] = quads;
// Getting the points for the untransformed shape
let untransformedQuad = getNodeBounds(this.win, this.currentNode);
this._setPolygonPoints(quad, "transformed");
this._setPolygonPoints(untransformedQuad, "untransformed");
for (let nb of ["1", "2", "3", "4"]) {
this._setLinePoints(untransformedQuad["p" + nb], quad["p" + nb], "line" + nb);
}
// Adapt to the current zoom
this.markup.scaleRootElement(this.currentNode, this.ID_CLASS_PREFIX + "root");
this._showShapes();
setIgnoreLayoutChanges(false, this.highlighterEnv.window.document.documentElement);
return true;
},
/**
* Hide the highlighter, the outline and the infobar.
*/
_hide: function () {
setIgnoreLayoutChanges(true);
this._hideShapes();
setIgnoreLayoutChanges(false, this.highlighterEnv.window.document.documentElement);
},
_hideShapes: function () {
this.getElement("elements").setAttribute("hidden", "true");
},
_showShapes: function () {
this.getElement("elements").removeAttribute("hidden");
}
});
exports.CssTransformHighlighter = CssTransformHighlighter;
|