Handle non-existing elements
Browse files- assets/scripts/src/overlay.js +9 -5
- assets/scripts/src/sholo.js +6 -2
- index.html +1 -0
assets/scripts/src/overlay.js
CHANGED
|
@@ -11,6 +11,7 @@ export default class Overlay {
|
|
| 11 |
this.lastSelectedPosition = new Position({});
|
| 12 |
|
| 13 |
this.window = window;
|
|
|
|
| 14 |
|
| 15 |
this.prepareContext();
|
| 16 |
this.setSize();
|
|
@@ -18,7 +19,7 @@ export default class Overlay {
|
|
| 18 |
|
| 19 |
// Prepares the overlay
|
| 20 |
prepareContext() {
|
| 21 |
-
const overlay = document.createElement('canvas');
|
| 22 |
|
| 23 |
this.overlay = overlay;
|
| 24 |
this.context = overlay.getContext('2d');
|
|
@@ -34,7 +35,6 @@ export default class Overlay {
|
|
| 34 |
// Highlights the dom element on the screen
|
| 35 |
highlight(element) {
|
| 36 |
if (!element) {
|
| 37 |
-
// @todo - clearing the overlay
|
| 38 |
return;
|
| 39 |
}
|
| 40 |
|
|
@@ -48,6 +48,10 @@ export default class Overlay {
|
|
| 48 |
this.draw();
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
draw() {
|
| 52 |
// Reset the overlay
|
| 53 |
this.context.clearRect(0, 0, this.overlay.width, this.overlay.height);
|
|
@@ -56,15 +60,15 @@ export default class Overlay {
|
|
| 56 |
|
| 57 |
// Cut out the cleared region
|
| 58 |
this.context.clearRect(
|
| 59 |
-
this.selectedPosition.left - window.scrollX,
|
| 60 |
-
this.selectedPosition.top - window.scrollY,
|
| 61 |
(this.selectedPosition.right - this.selectedPosition.left),
|
| 62 |
(this.selectedPosition.bottom - this.selectedPosition.top),
|
| 63 |
);
|
| 64 |
|
| 65 |
// Append the overlay if not there already
|
| 66 |
if (!this.overlay.parentNode) {
|
| 67 |
-
document.body.appendChild(this.overlay);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
|
|
|
| 11 |
this.lastSelectedPosition = new Position({});
|
| 12 |
|
| 13 |
this.window = window;
|
| 14 |
+
this.document = document;
|
| 15 |
|
| 16 |
this.prepareContext();
|
| 17 |
this.setSize();
|
|
|
|
| 19 |
|
| 20 |
// Prepares the overlay
|
| 21 |
prepareContext() {
|
| 22 |
+
const overlay = this.document.createElement('canvas');
|
| 23 |
|
| 24 |
this.overlay = overlay;
|
| 25 |
this.context = overlay.getContext('2d');
|
|
|
|
| 35 |
// Highlights the dom element on the screen
|
| 36 |
highlight(element) {
|
| 37 |
if (!element) {
|
|
|
|
| 38 |
return;
|
| 39 |
}
|
| 40 |
|
|
|
|
| 48 |
this.draw();
|
| 49 |
}
|
| 50 |
|
| 51 |
+
clear() {
|
| 52 |
+
this.document.body.removeChild(this.overlay);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
draw() {
|
| 56 |
// Reset the overlay
|
| 57 |
this.context.clearRect(0, 0, this.overlay.width, this.overlay.height);
|
|
|
|
| 60 |
|
| 61 |
// Cut out the cleared region
|
| 62 |
this.context.clearRect(
|
| 63 |
+
this.selectedPosition.left - this.window.scrollX,
|
| 64 |
+
this.selectedPosition.top - this.window.scrollY,
|
| 65 |
(this.selectedPosition.right - this.selectedPosition.left),
|
| 66 |
(this.selectedPosition.bottom - this.selectedPosition.top),
|
| 67 |
);
|
| 68 |
|
| 69 |
// Append the overlay if not there already
|
| 70 |
if (!this.overlay.parentNode) {
|
| 71 |
+
this.document.body.appendChild(this.overlay);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
assets/scripts/src/sholo.js
CHANGED
|
@@ -21,7 +21,11 @@ export default class Sholo {
|
|
| 21 |
throw new Error('Element can only be string or the dom element');
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
}
|
|
|
|
| 21 |
throw new Error('Element can only be string or the dom element');
|
| 22 |
}
|
| 23 |
|
| 24 |
+
if (domElement) {
|
| 25 |
+
const element = new Element(domElement);
|
| 26 |
+
this.overlay.highlight(element);
|
| 27 |
+
} else {
|
| 28 |
+
this.overlay.clear();
|
| 29 |
+
}
|
| 30 |
}
|
| 31 |
}
|
index.html
CHANGED
|
@@ -46,6 +46,7 @@
|
|
| 46 |
<script>
|
| 47 |
const nodesToSelect = [
|
| 48 |
'.section__header',
|
|
|
|
| 49 |
'.section__how',
|
| 50 |
'.section__examples'
|
| 51 |
];
|
|
|
|
| 46 |
<script>
|
| 47 |
const nodesToSelect = [
|
| 48 |
'.section__header',
|
| 49 |
+
'asdfsfd',
|
| 50 |
'.section__how',
|
| 51 |
'.section__examples'
|
| 52 |
];
|