Skip to content

Commit b5ab058

Browse files
committed
Update selector.js
1 parent ea8317b commit b5ab058

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

selector.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,44 @@
22
Package: Dom-Selector
33
Version: 2.0.0
44
Author: Shivam Dewangan https://github.com/shivamdevs
5+
Repository: https://github.com/shivamdevs/dom-selector
56
License: MIT License
67
*/
78

8-
(function (context, name, enableSelector) {
9+
(function (context, name) {
910
const style = document.createElement("style");
1011
style.setAttribute("DomSelector", "");
1112
style.setAttribute("type", "text/css");
1213
style.innerHTML = `
1314
dom-selector,
15+
dom-selector-auto,
1416
dom-selector * {
1517
box-sizing: border-box;
1618
display: block;
1719
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
1820
-webkit-font-smoothing: antialiased;
1921
-moz-osx-font-smoothing: grayscale;
2022
}
23+
dom-selector-auto {
24+
position: fixed;
25+
inset: auto 0 20px auto;
26+
background: #fff;
27+
padding: 5px 10px;
28+
border-radius: 20px 0 0 20px;
29+
box-shadow: -2px 2px 4px 2px #0002;
30+
border: 1px solid #0004;
31+
border-right: none;
32+
z-index: 999999999999999998;
33+
transition: all .2s ease;
34+
cursor: pointer;
35+
color: #000;
36+
}
37+
dom-selector-auto:hover {
38+
color: #ff8c00;
39+
}
40+
dom-selector-auto.auto {
41+
translate: calc(100% + 10px) 0;
42+
}
2143
dom-selector {
2244
z-index: 999999999999999999;
2345
position: fixed;
@@ -192,16 +214,16 @@
192214
wrap.appendChild(data);
193215
document.body.appendChild(wrap);
194216

195-
window.addEventListener('mouseover', function(e) {
217+
context.addEventListener('mouseover', function(e) {
196218
if (wrap.classList.contains('inactive') || wrap.classList.contains('selected')) return;
197219
wrap.classList.add('surfing');
198220
});
199-
window.addEventListener('mouseout', function (e) {
221+
context.addEventListener('mouseout', function (e) {
200222
if (wrap.classList.contains('inactive') || wrap.classList.contains('selected')) return;
201223
wrap.classList.remove('surfing');
202224
});
203-
window.addEventListener('mousemove', mapE);
204-
window.addEventListener('scroll', mapE);
225+
context.addEventListener('mousemove', mapE);
226+
context.addEventListener('scroll', mapE);
205227

206228
const history = {x:0,y:0};
207229

@@ -214,7 +236,7 @@
214236
const element = document.elementFromPoint(e.clientX, e.clientY);
215237
if (!element) return;
216238
const rect = element.getBoundingClientRect();
217-
const style = window.getComputedStyle(element);
239+
const style = context.getComputedStyle(element);
218240
setStyle(element);
219241

220242
info.innerHTML = `<span node>${element.nodeName.toLowerCase()}</span>`;
@@ -232,7 +254,7 @@
232254

233255
function setStyle(node) {
234256
const rect = node.getBoundingClientRect();
235-
const style = window.getComputedStyle(node);
257+
const style = context.getComputedStyle(node);
236258
wrap.style.top = rect.top + 'px';
237259
wrap.style.left = rect.left + 'px';
238260
wrap.style.width = rect.width + 'px';
@@ -283,9 +305,10 @@
283305

284306
context[name] = function (showPreview = false) {
285307
return new Promise((resolve, reject) => {
286-
if (!wrap.classList.contains('inactive') || wrap.classList.contains('selected')) reject('Error: Another instance of \'DomSelector\' is already running. Finish it before starting another one.');
308+
if (!wrap.classList.contains('inactive') && !wrap.classList.contains('selected')) reject('Error: Another instance of \'DomSelector\' is already running. Finish it before starting another one.');
309+
if (wrap.classList.contains('selected')) reset();
287310
wrap.classList.remove('inactive');
288-
window.addEventListener('mousedown', function (e) {
311+
context.addEventListener('mousedown', function (e) {
289312
if (wrap.classList.contains('inactive')) reject('Error: Another instance of \'DomSelector\' is already running. Finish it before starting another one.');
290313
if (showPreview) wrap.classList.add('selected');
291314
wrap.classList.remove('surfing');
@@ -295,4 +318,17 @@
295318
}, {once: true});
296319
});
297320
};
298-
}(window, 'DomSelector', true));
321+
let autoSelect = false;
322+
context[name].autoSelect = function (showPreview = false) {
323+
if (autoSelect) throw new Error('Error: AutoSelect is already enabled.');
324+
autoSelect = true;
325+
326+
const auto = document.createElement('dom-selector-auto');
327+
auto.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="20" fill="currentColor" height="20"><path xmlns="http://www.w3.org/2000/svg" d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM288 176c0-44.2-35.8-80-80-80s-80 35.8-80 80c0 48.8 46.5 111.6 68.6 138.6c6 7.3 16.8 7.3 22.7 0c22.1-27 68.6-89.8 68.6-138.6zm-48 0c0 17.7-14.3 32-32 32s-32-14.3-32-32s14.3-32 32-32s32 14.3 32 32z"></path></svg>`;
328+
document.body.appendChild(auto);
329+
auto.addEventListener('click', function(e) {
330+
this.classList.add('auto');
331+
context[name](showPreview).then((el) => { console.log(el); }).finally(() => { auto.classList.remove('auto') });
332+
});
333+
};
334+
}(window, 'DomSelector'));

0 commit comments

Comments
 (0)